Skip to content

Commit

Permalink
misc: apply black formatting to all Python code.
Browse files Browse the repository at this point in the history
  • Loading branch information
skosukhin committed Jan 9, 2024
1 parent e07ff87 commit d171918
Show file tree
Hide file tree
Showing 7 changed files with 796 additions and 533 deletions.
573 changes: 340 additions & 233 deletions mkhelper/depgen.py

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions mkhelper/depgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import sys


def open23(name, mode='r'):
def open23(name, mode="r"):
if sys.version_info < (3, 0, 0):
return open(name, mode)
else:
return open(name, mode, encoding='latin-1')
return open(name, mode, encoding="latin-1")


def map23(foo, iterable):
Expand All @@ -23,7 +23,7 @@ def file_in_dir(f, d):
return True


def find_unquoted_string(string, line, quotes='\'"'):
def find_unquoted_string(string, line, quotes="'\""):
skip = 0
quote = None
while 1:
Expand All @@ -40,7 +40,7 @@ def find_unquoted_string(string, line, quotes='\'"'):
quote = c
elif quote == c:
quote = None
elif c == '\\' and quote:
elif c == "\\" and quote:
escaped = True

if quote:
Expand All @@ -59,19 +59,21 @@ def find(self, filename, root_includer=None, current_includer=None):
return filename
elif self.include_order:
for inc_type in self.include_order:
if inc_type == 'cwd' and os.path.isfile(filename):
if inc_type == "cwd" and os.path.isfile(filename):
return filename
elif inc_type == 'src' and root_includer:
candidate = os.path.join(os.path.dirname(root_includer),
filename)
elif inc_type == "src" and root_includer:
candidate = os.path.join(
os.path.dirname(root_includer), filename
)
if os.path.isfile(candidate):
return candidate
elif inc_type == 'inc' and current_includer:
candidate = os.path.join(os.path.dirname(current_includer),
filename)
elif inc_type == "inc" and current_includer:
candidate = os.path.join(
os.path.dirname(current_includer), filename
)
if os.path.isfile(candidate):
return candidate
elif inc_type == 'flg' and self.include_dirs:
elif inc_type == "flg" and self.include_dirs:
for d in self.include_dirs:
candidate = os.path.join(d, filename)
if os.path.isfile(candidate):
Expand Down Expand Up @@ -120,7 +122,7 @@ def readline(self):
stream = self._stream_stack.pop()
if self._close_stack.pop():
stream.close()
return ''
return ""


class StdStreamWrapper:
Expand Down
115 changes: 69 additions & 46 deletions mkhelper/depgen/fortran_parser.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import re

from depgen import IncludeFinder, StreamStack, file_in_dir, open23, \
find_unquoted_string
from depgen import (
IncludeFinder,
StreamStack,
file_in_dir,
open23,
find_unquoted_string,
)


class FortranParser:
_re_include = re.compile(r'^\s*include\s+(\'|")(.*?)\1', re.I)
_re_line_continue_start = re.compile(r'^(.*)&\s*$')
_re_line_continue_end = re.compile(r'^\s*&')
_re_module_provide = re.compile(r'^\s*module\s+(?!procedure\s)(\w+)', re.I)
_re_line_continue_start = re.compile(r"^(.*)&\s*$")
_re_line_continue_end = re.compile(r"^\s*&")
_re_module_provide = re.compile(r"^\s*module\s+(?!procedure\s)(\w+)", re.I)
_re_module_require = re.compile(
r'^\s*use(?:\s+|(?:\s*,\s*((?:non_)?intrinsic))?\s*::\s*)(\w+)', re.I)

def __init__(self,
include_order=None,
include_dirs=None,
include_roots=None,
intrinsic_mods=None,
external_mods=None):
r"^\s*use(?:\s+|(?:\s*,\s*((?:non_)?intrinsic))?\s*::\s*)(\w+)", re.I
)

def __init__(
self,
include_order=None,
include_dirs=None,
include_roots=None,
intrinsic_mods=None,
external_mods=None,
):
self.include_roots = include_roots

if intrinsic_mods:
Expand Down Expand Up @@ -66,7 +74,8 @@ def parse(self, stream):
continue

line = match.group(1) + re.sub(
FortranParser._re_line_continue_end, '', next_line)
FortranParser._re_line_continue_end, "", next_line
)

match = FortranParser._re_line_continue_start.match(line)

Expand All @@ -79,88 +88,102 @@ def parse(self, stream):
self.module_callback(module_name)
if self.debug_callback:
self.debug_callback(
line, 'declared module \'%s\'' % module_name)
line, "declared module '%s'" % module_name
)
continue

# module required
match = FortranParser._re_module_require.match(line)
if match:
module_nature = match.group(1).lower() \
if match.group(1) is not None else ''
module_nature = (
match.group(1).lower()
if match.group(1) is not None
else ""
)
module_name = match.group(2).lower()
if module_nature == 'intrinsic':
if module_nature == "intrinsic":
if self.debug_callback:
self.debug_callback(
line, 'ignored module usage (\'%s\' '
'is explicitly intrinsic)'
% module_name)
elif (module_name in self.intrinsic_mods and
module_nature != 'non_intrinsic'):
line,
"ignored module usage ('%s' "
"is explicitly intrinsic)" % module_name,
)
elif (
module_name in self.intrinsic_mods
and module_nature != "non_intrinsic"
):
if self.debug_callback:
self.debug_callback(
line, 'ignored module usage (\'%s\' '
'is implicitly intrinsic)'
% module_name)
line,
"ignored module usage ('%s' "
"is implicitly intrinsic)" % module_name,
)
elif module_name in self.external_mods:
if self.debug_callback:
self.debug_callback(
line, 'ignored module usage (\'%s\' '
'is external)' % module_name)
line,
"ignored module usage ('%s' "
"is external)" % module_name,
)
else:
if self.use_module_callback:
self.use_module_callback(module_name)
if self.debug_callback:
self.debug_callback(
line, 'used module \'%s\'' % module_name)
line, "used module '%s'" % module_name
)
continue

# include statement
match = FortranParser._re_include.match(line)
if match:
filename = match.group(2)
filepath = self._include_finder.find(
filename,
s.root_name,
s.current_name)
filename, s.root_name, s.current_name
)
if filepath:
if not self.include_roots or any(
[file_in_dir(filepath, d)
for d in self.include_roots]):
s.add(open23(filepath, 'r'))
[
file_in_dir(filepath, d)
for d in self.include_roots
]
):
s.add(open23(filepath, "r"))
if self.include_callback:
self.include_callback(filepath)
if self.debug_callback:
self.debug_callback(
line, 'included file \'%s\''
% filepath)
line, "included file '%s'" % filepath
)
elif self.debug_callback:
self.debug_callback(
line,
'ignored (file \'%s\' '
'is not in the source roots)' % filepath)
"ignored (file '%s' "
"is not in the source roots)" % filepath,
)
elif self.debug_callback:
self.debug_callback(line,
'ignored (file not found)')
self.debug_callback(
line, "ignored (file not found)"
)
continue

@staticmethod
def _split_semicolons(line):
while 1:
idx = find_unquoted_string(';', line)
idx = find_unquoted_string(";", line)
if idx < 0:
if line and not line.isspace():
yield line
break
else:
prefix = line[:idx]
if prefix and not prefix.isspace():
yield prefix + '\n'
line = line[idx + 1:]
yield prefix + "\n"
line = line[idx + 1 :]

@staticmethod
def _delete_comments(line):
comment_idx = find_unquoted_string('!', line)
comment_idx = find_unquoted_string("!", line)
if comment_idx >= 0:
line = line[:comment_idx]
return line

18 changes: 10 additions & 8 deletions mkhelper/depgen/line_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
class LCProcessor:
_re_lc = re.compile(r'^#\s*[1-9]\d*\s*"(.*?)"\s*(?:[1-9]\d*)?')

def __init__(self, stream,
include_roots=None):
def __init__(self, stream, include_roots=None):
self.include_roots = include_roots

# Callbacks:
Expand All @@ -28,19 +27,22 @@ def readline(self):
filepath = match.group(1)
if os.path.isfile(filepath):
if not self.include_roots or any(
[file_in_dir(filepath, d)
for d in self.include_roots]):
[file_in_dir(filepath, d) for d in self.include_roots]
):
if self.lc_callback:
self.lc_callback(filepath)
if self.debug_callback:
self.debug_callback(
line, 'accepted file \'%s\'' % filepath)
line, "accepted file '%s'" % filepath
)
elif self.debug_callback:
self.debug_callback(
line, 'ignored (file \'%s\' '
'is not in the source roots)' % filepath)
line,
"ignored (file '%s' "
"is not in the source roots)" % filepath,
)
elif self.debug_callback:
self.debug_callback(line, 'ignored (file not found)')
self.debug_callback(line, "ignored (file not found)")
continue

return line
Expand Down
Loading

0 comments on commit d171918

Please sign in to comment.