BstLabs / py-dynacli

DynaCLI is a cloud-friendly Python library for converting pure Python functions into Linux Shell commands.
https://bstlabs.github.io/py-dynacli/
MIT License
98 stars 5 forks source link

[BUG] Cannot import public functions in __init__.pu #94

Closed asterkin closed 2 years ago

asterkin commented 2 years ago

Describe the bug init.py is a natural place to keep common internal functions. If I import another public function there it will mees up.

To Reproduce

from _common.session import get_session

def _get_instance(instance_name: str, **kwargs: str):

    session = get_session(kwargs.get('profile', 'default'))
    ec2_client = session.client('ec2')
    ec2_resource = session.resource('ec2')
    instances = ec2_client.describe_instances(
        Filters=[
            dict(Name='tag:Name', Values=[instance_name]) 
        ]
    )
    instance = ec2_resource.Instance(instances.Reservations[0].Instances[0].InstanceId)
    return instance

  asterkin@asterkin-ideacentre-AIO-510-23ISH ~/clvm (main)$ ./clvm instance -h                                                                                                                                       
usage: clvm instance [-h] {start,get-session} ...                                                                                                                                                                  

positional arguments:                                                                                                                                                                                              
  {start,get-session}                                                                                                                                                                                              
    start              start vm instance                                                                                                                                                                           
    get-session        [ERROR] Missing command docstring                                                                                                                                                           

optional arguments:                                                                                                                                                                                                
  -h, --help           show this help message and exit       

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Extra info:**
 - OS: [e.g. Amazon Linux 2]
 - Affected Version
ShahriyarR commented 2 years ago

@asterkin this is expected behavior as per our implementation. The related issue: https://github.com/BstLabs/py-dynacli/issues/21

and related PR: https://github.com/BstLabs/py-dynacli/pull/29

If you recall, we have registered the imported modules as features as for the fix for nested features. I.E importing some function in the upper package for nested features.

The get_session will be set as known name and registered in _set_known_names function and then will be added as a command in _add_known_functions

ShahriyarR commented 2 years ago

The possible workaround and solution are to check if the imported function/module origin/package/path is public or not. If not then it will not be included if it is from the public path we consider it as a nested feature import.

asterkin commented 2 years ago

This shuld be applied to only public imports from the same set of search paths and root packages (in princippe one package could "import" a public command from another package).