Skip to content

Commit 8b3134f

Browse files
committed
Cache config file contents
1 parent e474cad commit 8b3134f

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Diff for: wpiformat/wpiformat/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def proc_pipeline(name):
9090
try:
9191
config_file = Config(os.path.dirname(name), ".wpiformat")
9292
except OSError:
93+
print(
94+
"Warning: '.wpiformat' file not found. Looking for deprecated '.styleguide' file."
95+
)
9396
# TODO: Remove handling for deprecated .styleguide file
9497
config_file = Config(os.path.dirname(name), ".styleguide")
9598
print("Warning: found deprecated '.styleguide' file. Rename to '.wpiformat'.")

Diff for: wpiformat/wpiformat/config.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import os
44
import regex
5-
import sys
65

76

87
class Config:
8+
# Dict from filepath to file contents
9+
config_cache: dict[str, list[str]] = {}
10+
911
def __init__(self, directory, file_name):
1012
"""Constructor for Config object.
1113
@@ -34,22 +36,23 @@ def read_file(directory, file_name):
3436
Returns tuple of file name and list containing file contents or triggers
3537
program exit.
3638
"""
37-
file_found = False
38-
while not file_found:
39+
while True:
40+
filepath = os.path.join(directory, file_name)
3941
try:
40-
with open(directory + os.sep + file_name, "r") as file_contents:
41-
file_found = True
42-
return (
43-
os.path.join(directory, file_name),
44-
file_contents.read().splitlines(),
45-
)
42+
# If filepath in config cache, return cached version instead
43+
if filepath in Config.config_cache:
44+
return filepath, Config.config_cache[filepath]
45+
46+
with open(filepath, "r") as file_contents:
47+
contents = file_contents.read().splitlines()
48+
Config.config_cache[filepath] = contents
49+
return filepath, contents
4650
except OSError as e:
4751
# .git files are ignored, which are created within submodules
4852
if os.path.isdir(directory + os.sep + ".git"):
49-
print(
50-
f"Error: config file '{file_name}' not found in '{directory}'"
51-
)
52-
raise e
53+
raise OSError(
54+
f"config file '{file_name}' not found in '{directory}'"
55+
) from e
5356
directory = os.path.dirname(directory)
5457

5558
def group(self, group_name):

0 commit comments

Comments
 (0)