@@ -1943,13 +1943,15 @@ def get_config_file_list(start_dir):
1943
1943
dir = parent
1944
1944
return config_file_list
1945
1945
1946
- arguments = {'prog' : argv [0 ],
1946
+ def get_argparse_arguments ():
1947
+ arguments = {'prog' : argv [0 ],
1947
1948
'description' : 'Auto-format modern Fortran source files.' ,
1948
1949
'formatter_class' : argparse .ArgumentDefaultsHelpFormatter }
1949
1950
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
1953
1955
1954
1956
def get_arg_parser (args ):
1955
1957
"""helper function to create the parser object"""
@@ -2027,9 +2029,17 @@ def get_arg_parser(args):
2027
2029
version = '%(prog)s 0.3.7' )
2028
2030
return parser
2029
2031
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
2033
2043
2034
2044
def build_ws_dict (args ):
2035
2045
"""helper function to build whitespace dictionary"""
@@ -2046,6 +2056,19 @@ def build_ws_dict(args):
2046
2056
ws_dict ['intrinsics' ] = args .whitespace_intrinsics
2047
2057
return ws_dict
2048
2058
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
+
2049
2072
# support legacy input:
2050
2073
if 'stdin' in args .path and not os .path .isfile ('stdin' ):
2051
2074
args .path = ['-' if _ == 'stdin' else _ for _ in args .path ]
@@ -2064,6 +2087,7 @@ def build_ws_dict(args):
2064
2087
sys .stderr .write ("%s is a directory. Use --recursive option\n " % directory )
2065
2088
sys .exit (1 )
2066
2089
2090
+
2067
2091
if not args .recursive :
2068
2092
filenames = [directory ]
2069
2093
else :
@@ -2077,46 +2101,34 @@ def build_ws_dict(args):
2077
2101
2078
2102
for dirpath , dirnames , files in os .walk (directory ,topdown = True ):
2079
2103
2104
+ file_args = args
2105
+ if argparse .__name__ == "configargparse" :
2106
+ file_args = pars_args_with_config_file (directory )
2107
+
2080
2108
# Prune excluded patterns from list of child directories
2081
2109
# https://stackoverflow.com/a/19859907
2082
2110
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
2085
2113
)]
2086
2114
2087
2115
for ffile in [os .path .join (dirpath , f ) for f in files
2088
2116
if any (f .endswith (_ ) for _ in ext )
2089
- and not any ([
2117
+ and not any (
2090
2118
fnmatch (f ,exclude_pattern )
2091
- for exclude_pattern in args .exclude ] )]:
2119
+ for exclude_pattern in file_args .exclude )]:
2092
2120
filenames .append (ffile )
2093
2121
2094
2122
for filename in filenames :
2095
2123
2096
2124
# reparse arguments using the file's list of config files
2097
- filearguments = arguments
2125
+ file_args = args
2098
2126
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 )
2112
2129
2113
2130
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 )
2120
2132
2121
2133
stdout = file_args .stdout or directory == '-'
2122
2134
diffonly = file_args .diff
0 commit comments