Allow stocks/load --file from anywhere (#4410)

* allow load file from anywhere

* ruff

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
This commit is contained in:
montezdesousa
2023-03-06 20:25:02 +00:00
committed by GitHub
parent 2e841b34d4
commit f2316b15e8
2 changed files with 14 additions and 18 deletions

View File

@@ -1088,6 +1088,13 @@ class StockBaseController(BaseController, metaclass=ABCMeta):
self.suffix = "" # To hold suffix for Yahoo Finance
self.add_info = stocks_helper.additional_info_about_ticker("")
self.TRY_RELOAD = True
self.USER_IMPORT_FILES = {
filepath.name: filepath
for file_type in ["csv"]
for filepath in (
get_current_user().preferences.USER_CUSTOM_IMPORTS_DIRECTORY / "stocks"
).rglob(f"*.{file_type}")
}
def call_load(self, other_args: List[str]):
"""Process load command."""
@@ -1208,25 +1215,10 @@ class StockBaseController(BaseController, metaclass=ABCMeta):
# This seems to block the .exe since the folder needs to be manually created
# This block makes sure that we only look for the file if the -f flag is used
# Adding files in the argparse choices, will fail for the .exe even without -f
STOCKS_CUSTOM_IMPORTS = (
get_current_user().preferences.USER_CUSTOM_IMPORTS_DIRECTORY
/ "stocks"
)
try:
file_list = [x.name for x in STOCKS_CUSTOM_IMPORTS.iterdir()]
if ns_parser.filepath not in file_list:
console.print(
f"[red]{ns_parser.filepath} not found in custom_imports/stocks/ "
"folder[/red]."
)
return
except Exception as e:
console.print(e)
return
df_stock_candidate = stocks_helper.load_custom(
str(STOCKS_CUSTOM_IMPORTS / ns_parser.filepath)
file_location = self.USER_IMPORT_FILES.get(
ns_parser.filepath, ns_parser.filepath
)
df_stock_candidate = stocks_helper.load_custom(str(file_location))
if df_stock_candidate.empty:
return
is_df = isinstance(df_stock_candidate, pd.DataFrame)

View File

@@ -90,6 +90,10 @@ class StocksController(StockBaseController):
if session and get_current_user().preferences.USE_PROMPT_TOOLKIT:
choices: dict = self.choices_default
choices["load"] = {
"--file": {c: None for c in self.USER_IMPORT_FILES},
"-f": "--file",
}
self.completer = NestedCompleter.from_nested_dict(choices)
def print_help(self):