Files
OpenBB/custom_pre_commit/check_reserved_args.py
Theodore Aptekarev 775d218891 Add parser for input command arguments (The Forward Slash PR) (#2064)
* Add helper command to filter unix paths and other edge cases

* Remove sort argument filtering from parsing

* Make --file arg mandatory for loading files

* Refactor queue parsing logic

* Enable adding controller level input parsing filters

* Remove the github repo path hack because it's not needed anymore

* Fix typos and line length linting

* Update unix path parser to match known extensions

* Update oss custom filters to not require space after slash arg

* Fix tests and failing linters

* Add pre-commit hook to check for reserved arguments validity

* Fix tests

* Fix finviz compare screener tests

* Fix screener test

* Fix screener test

* Fix float representation in pd dataframe on finviz screener tests

* Update test records

* Update test records

* Remove explicit float formatting in tests

* Update tests

* Remove prepending file argument

* Update long arguments that have lost their short default to file

* Update arguments in docs

* Update portfolio test data

* Fix econometrics/desc throwing error on empty df and fix tests

* Update tests

* Fix econometrics load autocompletion

* Fix portfolio tests giving different results every time

* Allow loading openbb scripts from arbitrary path

* Fix passing path as a terminal argument

* Fix guess game being case sensitive

* Silence bandit warning about using pseudo-random generators

* Update gov tests

* Fix screener sort fields that have forward slash

* Make controllers apply custom argument filters on entry

Co-authored-by: DidierRLopes <dro.lopes@campus.fct.unl.pt>
2022-08-05 12:51:23 +03:00

71 lines
1.8 KiB
Python

"""Check reserved command arguments in Controllers"""
import os
import sys
import glob
import re
from typing import Optional, Any
RESERVED_ARGS = {
"f": "file",
}
def process_file(file_path: str, exp: str):
"""Lint file
Checks the file for short reserved arguments that don't match with the reserved
long arguments
Parameters
----------
file_path : str
Full path to the file
exp : str
Regular expression to find all reserved args
"""
errors_found = 0
with open(file_path) as f:
code = f.read()
match = re.search(pattern=exp, string=code)
if match:
short_arg = match.group()[2]
long_arg: Optional[Any] = re.search(
pattern=r"((?<=\--).*?(?=\"))", string=match.group()
)
if long_arg is not None:
long_arg = long_arg.group()
if long_arg != RESERVED_ARGS[short_arg]:
print(
f"{file_path}: "
f"'-{short_arg}' argument expected '{RESERVED_ARGS[short_arg]}'"
f" but assigned to '{long_arg}'"
)
errors_found += 1
return errors_found
# pylint:disable=anomalous-backslash-in-string
def main():
expressions = []
for sa in RESERVED_ARGS:
expressions.append(rf'("-{sa}(.|\n)*?\--.*?\S*)') # noqa: W605
exp = f"""({"|".join(expressions)})"""
base = os.path.abspath(os.path.dirname(__file__))
root = os.path.abspath(os.path.join(base, ".."))
errors_found = 0
for file_path in glob.iglob(
os.path.join(root, "**", "*controller.py"), recursive=True
):
errors_found += process_file(file_path=file_path, exp=exp)
if errors_found == 0:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
main()