Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

import of command class imports _all_ classes #13

Open
oversize opened this issue Dec 18, 2017 · 3 comments
Open

import of command class imports _all_ classes #13

oversize opened this issue Dec 18, 2017 · 3 comments

Comments

@oversize
Copy link

oversize commented Dec 18, 2017

When the command file (e.g. hello.py) Imports a class by itself, this class is then picked up by the getmembers() call in cli.py. E.g. i had the Path Class from pathlib imported in the command and then the Command always failed.

I have added some output to the script which you can see below. As far as i understand the Path Class is picked up as well and then fails to get executed.

Am i doing something terribly wrong? I will have a look into the inspect library and see if i can extend the check to lets say "not only be a class, but a child of Base as well".

`[msch:....de/deployment]$ vicdeploy tagmyself

// The options
{'--help': False,
'--version': False,
'tagmyself': True}

// contents of skele.comands (Including the Path class, that then is call via command(options)
[('Base', <class 'vicdeploy.commands.base.Base'>), ('Path', <class 'pathlib.Path'>), ('Tagmyself', <class 'vicdeploy.commands.tagmyself.Tagmyself'>)]

// The error traceback
Traceback (most recent call last):
File "/usr/local/opt/pyenv/versions/3.6.2/bin/vicdeploy", line 11, in
load_entry_point('vicdeploy', 'console_scripts', 'vicdeploy')()
File "/Users/msch/src/microservice-deployment/vicdeploy/cli.py", line 41, in main
cmd = cmd(options)
File "/usr/local/opt/pyenv/versions/3.6.2/lib/python3.6/pathlib.py", line 979, in new
self = cls._from_parts(args, init=False)
File "/usr/local/opt/pyenv/versions/3.6.2/lib/python3.6/pathlib.py", line 654, in _from_parts
drv, root, parts = self._parse_args(args)
File "/usr/local/opt/pyenv/versions/3.6.2/lib/python3.6/pathlib.py", line 638, in _parse_args
a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not Dict`

@rdegges thank you for this skeleton, has helped me alot in the last days.

@pinkerltm
Copy link

pinkerltm commented Feb 6, 2018

@oversize, @rdegges I am facing similar problems. 'commands' in cli.py is colliding with 'commands' from pyodbc which I am importing in one of the command scripts.

Until now I have only found 2 ways to avoid this:

  1. find another variable name in cli.py that won't collide with any possibly reserved name in the future (ugh... that looks like weeks of fun with Debugging)

  2. Import modules dynamically in the constructor of each class with 'importutils', but I am not quite sure if that will solve the Problem and I have to bind the modules to class variables which is kind of weird

Do you already have a better solution on this? I like the architecture of the cli-Skeleton, but these issues really freak me out and I would like to improve the skeleton to work as it should or dump it at all for my current project

@oversize
Copy link
Author

oversize commented Feb 7, 2018

Unfortunately not. My current workaround is "don't import classes as toplevel imports" ...

As far as i understand it, a third possibility would be to test in cli.py loop whether or not the class really is a (direct) subclass of "Base" only executing if it is.

@alexmacniven
Copy link

If you use the format of skele write run's the Write.run() function then replacing;

command = [command[1] for command in skele.commands if command[0] != 'Base'][0]

with the following;

command = [command[1] for command in skele.commands if command[0] == k.capitalize()][0]

offers a solution.

All we're doing here is specifically looking for the option name (in this case 'write') in the list of classes, then initiating a an object and calling run as usual.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants