Skip to content

Commit

Permalink
Merge branch 'master' into additional_unit_test
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanTConrad authored Dec 17, 2024
2 parents 42335a9 + cfa305d commit 82c1c70
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
30 changes: 24 additions & 6 deletions modernmetric/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,20 @@ def ArgParser(custom_args=None):

if not file_paths and not input_file: # No file passed in, read filelist from command line # noqa: E501
raise Exception("No filelist provided. Provide path to file list with --file=<path>") # noqa: E501

if input_file:
with open(input_file) as file:
data = json.load(file)
for file in data:
RUNARGS.files.append(file["path"])
if isinstance(data, dict) and "files" in data:
for file in data["files"]:
RUNARGS.files.append(file["path"])
elif isinstance(data, list):
if all(isinstance(item, dict) and "path" in
item for item in data):
for file in data:
RUNARGS.files.append(file["path"])
else:
RUNARGS.files.extend(data)

# Turn all paths to abs-paths right here
RUNARGS.oldfiles = {}
Expand All @@ -148,7 +157,18 @@ def ArgParser(custom_args=None):
# e.g. ["--file=path/to/filelist.json"]



def process_file(f, args, importer):
db_path = Path(Path.home(), args.cache_dir, args.cache_db)
if not db_path.parent.exists():
db_path.parent.mkdir(parents=True, exist_ok=True)

cache = None if args.no_cache else Cache(db_path, "modernmetric")
return file_process(f, args, importer, cache)


def main(custom_args=None, license_identifier: Union[int, str] = None):

if license_identifier:
report(
identifier=license_identifier,
Expand All @@ -159,8 +179,6 @@ def main(custom_args=None, license_identifier: Union[int, str] = None):
else:
_args = ArgParser()
_result = {"files": {}, "overall": {}}
# cache = (None if _args.no_cache else Cache(Path(_args.cache_db),
# "modernmetric"))

# Get importer
_importer = {}
Expand All @@ -180,8 +198,8 @@ def main(custom_args=None, license_identifier: Union[int, str] = None):
_overallCalc = get_modules_calculated(_args, **_importer)

with mp.Pool(processes=_args.jobs) as pool:
results = [pool.apply(file_process, args=(
f, _args, _importer)) for f in _args.files]
results = [pool.apply(process_file,
args=(f, _args, _importer)) for f in _args.files]

for x in results:
oldpath = _args.oldfiles[x[1]]
Expand Down
13 changes: 11 additions & 2 deletions modernmetric/fp.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ def file_process(
store.update(x.get_internal_store())

result = (res, _file, _lexer.name, tokens, store)
resDict = {
'res': res,
'file': _file,
'lexer_name': _lexer.name,
'tokens': tokens,
'store': store
}

# Store in cache if available
if cache is not None and not getattr(_args, 'no_cache', False):
cache.store(_file, result)
cache.set(_file, resDict)

return result

except Exception:
except Exception as e:
print(f"Error processing file {_file}: {e}", file=sys.stderr)
tokens = []
return (res, _file, _lexer.name, tokens, store)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pygments-tsx>=1.0.1
pygount
pytest
pytest-cov
cachehash~=1.0.2
cachehash~=1.0.3

0 comments on commit 82c1c70

Please sign in to comment.