diff --git a/.gitignore b/.gitignore index 45b5450..60e763c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,18 @@ venv/ build/ clanvas.egg-info/ dist/ + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets +!*.code-workspace + +# Built Visual Studio Code Extensions +*.vsix + +# test coverage +.coverage +.coverage.* \ No newline at end of file diff --git a/clanvas/clanvas.py b/clanvas/clanvas.py index d44904f..e64b4f8 100644 --- a/clanvas/clanvas.py +++ b/clanvas/clanvas.py @@ -11,7 +11,7 @@ import cmd2 import colorama from canvasapi import Canvas -from cmd2 import Cmd +from cmd2 import Cmd, Settable from .completion import apply_completers from .config import InvalidClanvasConfigurationException, parse_clanvas_config_file @@ -26,14 +26,28 @@ class Clanvas(cmd2.Cmd): CLANVAS_CATEGORY = 'Clanvas' def __init__(self, base_url, access_token, *args, **kwargs): - super(Clanvas, self).__init__(*args, **kwargs) + + # The `cmd2` library will attempt to inspect the object + # for attributes. Since one attribute is `prompt` defined + # as a property that checks for `self.canvas`, we need to + # ensure that `self.canvas` is initialized to avoid + # an AttributeError during initialization. + self.canvas = None + + # Explicitly set allow_cli_args to False to prevent cmd2 from parsing CLI args + # Since we already handle CLI args in the main function + super(Clanvas, self).__init__(*args, allow_cli_args=False, **kwargs) self.default_to_shell = True - self.allow_cli_args = False - self.settable.update({'prompt_format': 'prompt format string'}) - self.settable.update({'verbosity': 'default command verbosity (NORMAL/VERBOSE/DEBUG)'}) - self.settable.pop('prompt') + self.add_settable(Settable('prompt_format', + val_type=str, + description = 'prompt format string', settable_object=self)) + self.add_settable(Settable('verbosity', + val_type=Verbosity.__getitem__, + description='default command verbosity (NORMAL/VERBOSE/DEBUG)', + choices=(Verbosity.__members__.values()), + settable_object=self)) self.url = base_url self.host = urlparse(base_url).netloc @@ -77,13 +91,13 @@ def list_assignments_cached(self, course_id): return sorted(course.get_assignments(), key=lambda t: t.created_at_date) def get_verbosity(self) -> Verbosity: - return Verbosity[self.verbosity] + return self.verbosity prompt_format = (Fore.LIGHTGREEN_EX + '{login_id}@{host}' + Style.RESET_ALL + ':' + Fore.LIGHTYELLOW_EX + '{pwc}' + Style.RESET_ALL + ':' + Fore.LIGHTBLUE_EX + '{pwd} ' + Style.RESET_ALL + '$ ').replace('\x1b', "\\x1b") - verbosity = 'NORMAL' + verbosity = Verbosity.NORMAL canvas_path = expanduser('~/canvas') diff --git a/clanvas/outputter.py b/clanvas/outputter.py index 87e5738..e4eb278 100644 --- a/clanvas/outputter.py +++ b/clanvas/outputter.py @@ -7,6 +7,22 @@ class Verbosity(Enum): VERBOSE = 2 DEBUG = 3 + def __str__(self): + return self.name + + def __repr__(self): + return str(self) + + def __eq__(self, value): + if isinstance(value, str): + # Convert strings to Verbosity enum members before comparison + return super().__eq__(Verbosity.__getitem__(value)) + + return super().__eq__(value) + + def __hash__(self): + return super().__hash__() + class Outputter: def __init__(self, printfn, verbosityfn): diff --git a/setup.py b/setup.py index 636b0c7..0334755 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ author='Mark Lalor', author_email='markwlalor@gmail.com', install_requires=[ - 'canvasapi==0.12.0', 'cmd2==0.9.12', 'tabulate>=0.8.3', 'tree-format>=0.1.2', + 'canvasapi==3.3.0', 'cmd2==2.5.6', 'tabulate>=0.9.0', 'tree-format>=0.1.2', 'html2text', 'colorama', 'pytz', 'tzlocal' ], extras_require={ diff --git a/tests/config/__init__.py b/tests/config/__init__.py new file mode 100644 index 0000000..e69de29