Skip to content

Commit 40541a4

Browse files
committed
Working version
1 parent ecebf5a commit 40541a4

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

fprettify/__init__.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -1943,13 +1943,15 @@ def get_config_file_list(start_dir):
19431943
dir = parent
19441944
return config_file_list
19451945

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

1950-
if argparse.__name__ == "configargparse":
1951-
arguments['args_for_setting_config_path'] = ['-c', '--config-file']
1952-
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."
1951+
if argparse.__name__ == "configargparse":
1952+
arguments['args_for_setting_config_path'] = ['-c', '--config-file']
1953+
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."
1954+
return arguments
19531955

19541956
def get_arg_parser(args):
19551957
"""helper function to create the parser object"""
@@ -2027,9 +2029,17 @@ def get_arg_parser(args):
20272029
version='%(prog)s 0.3.7')
20282030
return parser
20292031

2030-
parser = get_arg_parser(arguments)
2031-
2032-
args = parser.parse_args(argv[1:])
2032+
def pars_args_with_config_file(directory):
2033+
"""
2034+
Parse arguments together with the config file.
2035+
Requires configargparse package.
2036+
"""
2037+
filearguments = get_argparse_arguments()
2038+
filearguments['default_config_files'] = ['~/.fprettify.rc'] \
2039+
+ get_config_file_list(directory if directory != '-' else os.getcwd())
2040+
file_argparser = get_arg_parser(filearguments)
2041+
file_args = file_argparser.parse_args(argv[1:])
2042+
return file_args
20332043

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

2059+
def build_case_dict(args):
2060+
"""helper function to build case dictionary"""
2061+
return {
2062+
'keywords' : file_args.case[0],
2063+
'procedures' : file_args.case[1],
2064+
'operators' : file_args.case[2],
2065+
'constants' : file_args.case[3]
2066+
}
2067+
2068+
parser = get_arg_parser(get_argparse_arguments())
2069+
2070+
args = parser.parse_args(argv[1:])
2071+
20492072
# support legacy input:
20502073
if 'stdin' in args.path and not os.path.isfile('stdin'):
20512074
args.path = ['-' if _ == 'stdin' else _ for _ in args.path]
@@ -2064,6 +2087,7 @@ def build_ws_dict(args):
20642087
sys.stderr.write("%s is a directory. Use --recursive option\n" % directory)
20652088
sys.exit(1)
20662089

2090+
20672091
if not args.recursive:
20682092
filenames = [directory]
20692093
else:
@@ -2077,46 +2101,34 @@ def build_ws_dict(args):
20772101

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

2104+
file_args = args
2105+
if argparse.__name__ == "configargparse":
2106+
file_args = pars_args_with_config_file(directory)
2107+
20802108
# Prune excluded patterns from list of child directories
20812109
# https://stackoverflow.com/a/19859907
20822110
dirnames[:] = [dirname for dirname in dirnames if not any(
2083-
[fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern)
2084-
for exclude_pattern in args.exclude]
2111+
fnmatch(dirname,exclude_pattern) or fnmatch(os.path.join(dirpath,dirname),exclude_pattern)
2112+
for exclude_pattern in file_args.exclude
20852113
)]
20862114

20872115
for ffile in [os.path.join(dirpath, f) for f in files
20882116
if any(f.endswith(_) for _ in ext)
2089-
and not any([
2117+
and not any(
20902118
fnmatch(f,exclude_pattern)
2091-
for exclude_pattern in args.exclude])]:
2119+
for exclude_pattern in file_args.exclude)]:
20922120
filenames.append(ffile)
20932121

20942122
for filename in filenames:
20952123

20962124
# reparse arguments using the file's list of config files
2097-
filearguments = arguments
2125+
file_args = args
20982126
if argparse.__name__ == "configargparse":
2099-
filearguments['default_config_files'] = ['~/.fprettify.rc'] \
2100-
+ get_config_file_list(os.path.dirname(os.path.abspath(filename)) if filename != '-' else os.getcwd())
2101-
file_argparser = get_arg_parser(filearguments)
2102-
file_args = file_argparser.parse_args(argv[1:])
2103-
2104-
# Skip excluded files but not if they we specified explicitly
2105-
# on the command line.
2106-
if filename != directory and any(
2107-
fnmatch(os.path.basename(filename), exclude_pattern)
2108-
for exclude_pattern in file_args.exclude):
2109-
if file_args.debug:
2110-
sys.stderr.write("Skipping excluded file %s\n" % filename)
2111-
continue
2127+
dirname = os.path.dirname(os.path.abspath(filename))
2128+
file_args = pars_args_with_config_file(dirname)
21122129

21132130
ws_dict = build_ws_dict(file_args)
2114-
case_dict = {
2115-
'keywords' : file_args.case[0],
2116-
'procedures' : file_args.case[1],
2117-
'operators' : file_args.case[2],
2118-
'constants' : file_args.case[3]
2119-
}
2131+
case_dict = build_case_dict(file_args)
21202132

21212133
stdout = file_args.stdout or directory == '-'
21222134
diffonly=file_args.diff

fprettify/tests/__init__.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ def test_config_file(self):
429429
config_file = joinpath(dirname, ".fprettify.rc")
430430
conf_string = "case=[1,1,1,2]\nexclude=[excluded*]"
431431

432+
excluded_subdir = joinpath(TEMP_DIR, 'excluded_subdir/')
433+
subdir = joinpath(TEMP_DIR, 'subdir/')
434+
432435
def create_file(fname, string):
433436
with io.open(fname, 'w', encoding='utf-8') as infile:
434437
infile.write(string)
@@ -442,11 +445,6 @@ def check_output_file(fname, str_exp):
442445
create_file(excluded_file, instring)
443446
create_file(config_file, conf_string)
444447

445-
excluded_subdir = joinpath(TEMP_DIR, 'excluded_subdir/')
446-
os.mkdir(excluded_subdir)
447-
file_in_excluded_subdir = joinpath(excluded_subdir, 'aliens.F90')
448-
create_file(file_in_excluded_subdir, instring)
449-
450448
# testing stdin --> stdout
451449
# In this case, the config file will not be read,
452450
# because it is not located in CWD.
@@ -462,16 +460,27 @@ def check_output_file(fname, str_exp):
462460
self.assertEqual(outstring_with_config, outstr.strip())
463461

464462
# testing recursive mode
463+
os.mkdir(subdir)
464+
file_in_subdir = joinpath(subdir, 'aliens.F90')
465+
create_file(file_in_subdir, instring)
466+
config_file_in_subdir = joinpath(subdir, ".fprettify.rc")
467+
# Config file in subdir should take precedence.
468+
create_file(config_file_in_subdir, "case=[2,2,2,2]")
469+
470+
os.mkdir(excluded_subdir)
471+
file_in_excluded_subdir = joinpath(excluded_subdir, 'aliens.F90')
472+
create_file(file_in_excluded_subdir, instring)
473+
465474
p1 = subprocess.Popen([RUNSCRIPT, '--recursive', dirname],
466475
stdout=subprocess.PIPE)
467476
p1.wait()
468477

469478
check_output_file(alien_file, outstring_with_config)
470-
# Excluded file should not be touched at all.
479+
# Excluded files and directories should not be touched at all.
471480
check_output_file(excluded_file, instring)
472-
# File in excluded directory should not be touched at all.
473-
# TODO: Make this work!
474-
#check_output_file(file_in_excluded_subdir, instring)
481+
check_output_file(file_in_excluded_subdir, instring)
482+
# subdir contains a different config file which should take precedence.
483+
check_output_file(file_in_subdir, outstring_without_config)
475484

476485
except: # pragma: no cover
477486
self.removeTmpDir()

0 commit comments

Comments
 (0)