Skip to content

Commit

Permalink
Fix/use absolute path in file manipulations (#44)
Browse files Browse the repository at this point in the history
* Fix/use absolute path in file manipulations (#43)

* Fix/change suffix limit 'csv' to '.csv'

* update patch version

---------

Co-authored-by: Jarrett Ye <[email protected]>
  • Loading branch information
tkngaejcpi and L-M-Sherlock authored Sep 21, 2023
1 parent b04bdcc commit 68df564
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
85 changes: 49 additions & 36 deletions src/fsrs_optimizer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import pytz
import os
import functools
from pathlib import Path

import matplotlib.pyplot as plt
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
Expand All @@ -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

0 comments on commit 68df564

Please sign in to comment.