From 68df56499e339aba8aa5fa6bd879f9a690ffe7b9 Mon Sep 17 00:00:00 2001 From: Riley Chang <130063517+tkngaejcpi@users.noreply.github.com> Date: Thu, 21 Sep 2023 01:01:21 -0600 Subject: [PATCH] Fix/use absolute path in file manipulations (#44) * Fix/use absolute path in file manipulations (#43) * Fix/change suffix limit 'csv' to '.csv' * update patch version --------- Co-authored-by: Jarrett Ye --- pyproject.toml | 2 +- src/fsrs_optimizer/__main__.py | 85 ++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fd5a670..dfd5175 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "FSRS-Optimizer" -version = "4.14.2" +version = "4.14.3" readme = "README.md" dependencies = [ "matplotlib>=3.7.0", diff --git a/src/fsrs_optimizer/__main__.py b/src/fsrs_optimizer/__main__.py index 56a040f..dde9a6c 100644 --- a/src/fsrs_optimizer/__main__.py +++ b/src/fsrs_optimizer/__main__.py @@ -4,6 +4,7 @@ import json import pytz import os +import functools from pathlib import Path import matplotlib.pyplot as plt @@ -82,13 +83,13 @@ def remembered_fallback_prompt(key: str, pretty: str = None): optimizer = fsrs_optimizer.Optimizer() if filepath.endswith(".apkg") or filepath.endswith(".colpkg"): optimizer.anki_extract( - f"../{filepath}", + f"{filepath}", remembered_fallbacks["filter_out_suspended_cards"] == "y", filter_out_flags, ) else: # copy the file to the current directory and rename it as revlog.csv - shutil.copyfile(f"../{filepath}", "revlog.csv") + shutil.copyfile(f"{filepath}", "revlog.csv") analysis = optimizer.create_time_series( remembered_fallbacks["timezone"], remembered_fallbacks["revlog_start_date"], @@ -153,11 +154,9 @@ def remembered_fallback_prompt(key: str, pretty: str = None): with open("evaluation.json", "w+") as f: json.dump(evaluation, f) - -if __name__ == "__main__": - config_save = os.path.expanduser(".fsrs_optimizer") - +def create_arg_parser(): parser = argparse.ArgumentParser() + parser.add_argument("filenames", nargs="+") parser.add_argument( "-y", @@ -175,36 +174,50 @@ def remembered_fallback_prompt(key: str, pretty: str = None): "-o", "--out", help="File to APPEND the automatically generated profile to." ) + return parser + +if __name__ == "__main__": + config_save = os.path.expanduser(".fsrs_optimizer") + + parser = create_arg_parser() args = parser.parse_args() + def lift(file_or_dir): + return os.listdir(file_or_dir) if os.path.isdir(file_or_dir) else [ file_or_dir ] + + def flatten(fl): + return sum(fl, []) + + def mapC(f): + return lambda x: map(f, x) + + def filterC(f): + return lambda x: filter(f, x) + + def pipe(functions, value): + return functools.reduce(lambda out, f: f(out), functions, value) + curdir = os.getcwd() - for filename in args.filenames: - if os.path.isdir(filename): - files = [ - f - for f in os.listdir(filename) - if f.lower().endswith(".apkg") or f.lower().endswith(".colpkg") - ] - files = [os.path.join(filename, f) for f in files] - for file_path in files: - try: - print(f"Processing {file_path}") - process(file_path, args.flags) - except Exception as e: - print(e) - print(f"Failed to process {file_path}") - finally: - plt.close("all") - os.chdir(curdir) - continue - else: - try: - print(f"Processing {filename}") - process(filename, args.flags) - except Exception as e: - print(e) - print(f"Failed to process {filename}") - finally: - plt.close("all") - os.chdir(curdir) - continue + + files = pipe([ + mapC(lift), # map file to [ file ], dir to [ file1, file2, ... ] + flatten, # flatten into [ file1, file2, ... ] + mapC(os.path.abspath), # map to absolute path + filterC(lambda f: not os.path.isdir(f)), # file filter + filterC(lambda f: + f.lower().endswith(".apkg") or + f.lower().endswith(".colpkg") or + f.lower().endswith(".csv")) # extension filter + ], args.filenames) + + for filename in files: + try: + print(f"Processing {filename}") + process(filename, args.flags) + except Exception as e: + print(e) + print(f"Failed to process {filename}") + finally: + plt.close("all") + os.chdir(curdir) + continue