Skip to content

Commit

Permalink
Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhollas committed May 27, 2021
1 parent ecebf5a commit 40541a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
74 changes: 43 additions & 31 deletions fprettify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,13 +1943,15 @@ def get_config_file_list(start_dir):
dir = parent
return config_file_list

arguments = {'prog': argv[0],
def get_argparse_arguments():
arguments = {'prog': argv[0],
'description': 'Auto-format modern Fortran source files.',
'formatter_class': argparse.ArgumentDefaultsHelpFormatter}

if argparse.__name__ == "configargparse":
arguments['args_for_setting_config_path'] = ['-c', '--config-file']
arguments['description'] = arguments['description'] + " Config files ('.fprettify.rc') in the home (~) directory and any such files located in parent directories of the input file will be used. When the standard input is used, the search is started from the current directory."
if argparse.__name__ == "configargparse":
arguments['args_for_setting_config_path'] = ['-c', '--config-file']
arguments['description'] = arguments['description'] + " Config files ('.fprettify.rc') in the home (~) directory and any such files located in parent directories of the input file will be used. When the standard input is used, the search is started from the current directory."
return arguments

def get_arg_parser(args):
"""helper function to create the parser object"""
Expand Down Expand Up @@ -2027,9 +2029,17 @@ def get_arg_parser(args):
version='%(prog)s 0.3.7')
return parser

parser = get_arg_parser(arguments)

args = parser.parse_args(argv[1:])
def pars_args_with_config_file(directory):
"""
Parse arguments together with the config file.
Requires configargparse package.
"""
filearguments = get_argparse_arguments()
filearguments['default_config_files'] = ['~/.fprettify.rc'] \
+ get_config_file_list(directory if directory != '-' else os.getcwd())
file_argparser = get_arg_parser(filearguments)
file_args = file_argparser.parse_args(argv[1:])
return file_args

def build_ws_dict(args):
"""helper function to build whitespace dictionary"""
Expand All @@ -2046,6 +2056,19 @@ def build_ws_dict(args):
ws_dict['intrinsics'] = args.whitespace_intrinsics
return ws_dict

def build_case_dict(args):
"""helper function to build case dictionary"""
return {
'keywords' : file_args.case[0],
'procedures' : file_args.case[1],
'operators' : file_args.case[2],
'constants' : file_args.case[3]
}

parser = get_arg_parser(get_argparse_arguments())

args = parser.parse_args(argv[1:])

# support legacy input:
if 'stdin' in args.path and not os.path.isfile('stdin'):
args.path = ['-' if _ == 'stdin' else _ for _ in args.path]
Expand All @@ -2064,6 +2087,7 @@ def build_ws_dict(args):
sys.stderr.write("%s is a directory. Use --recursive option\n" % directory)
sys.exit(1)


if not args.recursive:
filenames = [directory]
else:
Expand All @@ -2077,46 +2101,34 @@ def build_ws_dict(args):

for dirpath, dirnames, files in os.walk(directory,topdown=True):

file_args = args
if argparse.__name__ == "configargparse":
file_args = pars_args_with_config_file(directory)

# Prune excluded patterns from list of child directories
# https://stackoverflow.com/a/19859907
dirnames[:] = [dirname for dirname in dirnames if not any(
[fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern)
for exclude_pattern in args.exclude]
fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern)
for exclude_pattern in file_args.exclude
)]

for ffile in [os.path.join(dirpath, f) for f in files
if any(f.endswith(_) for _ in ext)
and not any([
and not any(
fnmatch(f,exclude_pattern)
for exclude_pattern in args.exclude])]:
for exclude_pattern in file_args.exclude)]:
filenames.append(ffile)

for filename in filenames:

# reparse arguments using the file's list of config files
filearguments = arguments
file_args = args
if argparse.__name__ == "configargparse":
filearguments['default_config_files'] = ['~/.fprettify.rc'] \
+ get_config_file_list(os.path.dirname(os.path.abspath(filename)) if filename != '-' else os.getcwd())
file_argparser = get_arg_parser(filearguments)
file_args = file_argparser.parse_args(argv[1:])

# Skip excluded files but not if they we specified explicitly
# on the command line.
if filename != directory and any(
fnmatch(os.path.basename(filename), exclude_pattern)
for exclude_pattern in file_args.exclude):
if file_args.debug:
sys.stderr.write("Skipping excluded file %s\n" % filename)
continue
dirname = os.path.dirname(os.path.abspath(filename))
file_args = pars_args_with_config_file(dirname)

ws_dict = build_ws_dict(file_args)
case_dict = {
'keywords' : file_args.case[0],
'procedures' : file_args.case[1],
'operators' : file_args.case[2],
'constants' : file_args.case[3]
}
case_dict = build_case_dict(file_args)

stdout = file_args.stdout or directory == '-'
diffonly=file_args.diff
Expand Down
27 changes: 18 additions & 9 deletions fprettify/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ def test_config_file(self):
config_file = joinpath(dirname, ".fprettify.rc")
conf_string = "case=[1,1,1,2]\nexclude=[excluded*]"

excluded_subdir = joinpath(TEMP_DIR, 'excluded_subdir/')
subdir = joinpath(TEMP_DIR, 'subdir/')

def create_file(fname, string):
with io.open(fname, 'w', encoding='utf-8') as infile:
infile.write(string)
Expand All @@ -442,11 +445,6 @@ def check_output_file(fname, str_exp):
create_file(excluded_file, instring)
create_file(config_file, conf_string)

excluded_subdir = joinpath(TEMP_DIR, 'excluded_subdir/')
os.mkdir(excluded_subdir)
file_in_excluded_subdir = joinpath(excluded_subdir, 'aliens.F90')
create_file(file_in_excluded_subdir, instring)

# testing stdin --> stdout
# In this case, the config file will not be read,
# because it is not located in CWD.
Expand All @@ -462,16 +460,27 @@ def check_output_file(fname, str_exp):
self.assertEqual(outstring_with_config, outstr.strip())

# testing recursive mode
os.mkdir(subdir)
file_in_subdir = joinpath(subdir, 'aliens.F90')
create_file(file_in_subdir, instring)
config_file_in_subdir = joinpath(subdir, ".fprettify.rc")
# Config file in subdir should take precedence.
create_file(config_file_in_subdir, "case=[2,2,2,2]")

os.mkdir(excluded_subdir)
file_in_excluded_subdir = joinpath(excluded_subdir, 'aliens.F90')
create_file(file_in_excluded_subdir, instring)

p1 = subprocess.Popen([RUNSCRIPT, '--recursive', dirname],
stdout=subprocess.PIPE)
p1.wait()

check_output_file(alien_file, outstring_with_config)
# Excluded file should not be touched at all.
# Excluded files and directories should not be touched at all.
check_output_file(excluded_file, instring)
# File in excluded directory should not be touched at all.
# TODO: Make this work!
#check_output_file(file_in_excluded_subdir, instring)
check_output_file(file_in_excluded_subdir, instring)
# subdir contains a different config file which should take precedence.
check_output_file(file_in_subdir, outstring_without_config)

except: # pragma: no cover
self.removeTmpDir()
Expand Down

0 comments on commit 40541a4

Please sign in to comment.