Skip to content

Commit cb00637

Browse files
committed
refactor: move config file read to inner method for debug_server_parser
1 parent abbe440 commit cb00637

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

fortls/__init__.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -528,33 +528,39 @@ def locate_config(root: str) -> str | None:
528528
config_path = os.path.join(root, f)
529529
return config_path
530530

531-
ensure_file_accessible(args.debug_filepath)
532-
# Get preprocessor definitions from config file
533-
pp_suffixes = None
534-
pp_defs = {}
535-
include_dirs = set()
536-
if args.debug_rootpath:
531+
def read_config(root: str | None):
532+
pp_suffixes = None
533+
pp_defs = {}
534+
include_dirs = set()
535+
if root is None:
536+
return pp_suffixes, pp_defs, include_dirs
537+
537538
# Check for config files
538-
config_path = locate_config(args.debug_rootpath)
539-
config_exists = os.path.isfile(config_path)
540-
if config_exists:
541-
try:
542-
with open(config_path, encoding="utf-8") as fhandle:
543-
config_dict = json.load(fhandle)
544-
pp_suffixes = config_dict.get("pp_suffixes", None)
545-
pp_defs = config_dict.get("pp_defs", {})
546-
for path in config_dict.get("include_dirs", set()):
547-
include_dirs.update(
548-
only_dirs(resolve_globs(path, args.debug_rootpath))
549-
)
550-
551-
if isinstance(pp_defs, list):
552-
pp_defs = {key: "" for key in pp_defs}
553-
except ValueError as e:
554-
print(f"Error {e} while parsing '{args.config}' settings file")
539+
config_path = locate_config(root)
540+
if not os.path.isfile(config_path):
541+
return pp_suffixes, pp_defs, include_dirs
542+
543+
try:
544+
with open(config_path, encoding="utf-8") as fhandle:
545+
config_dict = json.load(fhandle)
546+
pp_suffixes = config_dict.get("pp_suffixes", None)
547+
pp_defs = config_dict.get("pp_defs", {})
548+
for path in config_dict.get("include_dirs", set()):
549+
include_dirs.update(only_dirs(resolve_globs(path, root)))
550+
551+
if isinstance(pp_defs, list):
552+
pp_defs = {key: "" for key in pp_defs}
553+
except ValueError as e:
554+
print(f"Error {e} while parsing '{config_path}' settings file")
555+
556+
return pp_suffixes, pp_defs, include_dirs
555557

556558
print("\nTesting parser")
557559
separator()
560+
561+
ensure_file_accessible(args.debug_filepath)
562+
pp_suffixes, pp_defs, include_dirs = read_config(args.debug_rootpath)
563+
558564
print(f' File = "{args.debug_filepath}"')
559565
file_obj = FortranFile(args.debug_filepath, pp_suffixes)
560566
err_str, _ = file_obj.load_from_disk()

0 commit comments

Comments
 (0)