Skip to content

Renewed plugins #347

@pslacerda

Description

@pslacerda

In my opinion, extended PyMOL commands should be both more friendly and strict.

This is a proof of concept:

from pathlib import Path

@declare_plugin
def f(a: int, b: Path='/tmp/'):
    return f'{a} {b}'

This is the messy code:

def declare_plugin(name, function=None, _self=cmd):
    if function is None:
        name, function = name.__name__, name
    
    if function.__code__.co_argcount != len(function.__annotations__):
        raise Exception("Messy annotations")

    def inner(*args, **kwargs):
        eval_args = []
        eval_kwargs = {}
        

        args = list(args[:])
        kwargs = kwargs.copy()
        
        anns = list(function.__annotations__.keys())
        funcs = list(function.__annotations__.values())

        for idx, arg in enumerate(args):
            key = anns[idx]
            func = funcs[idx]
            eval_args.append(func(arg))
                
        for kwarg_key, kwarg_arg in kwargs.items():
            key = kwarg_key
            arg = kwarg_arg
            func = function.__annotations__[key]

            func = function.__annotations__[key]
            eval_kwargs[kwarg_key] = func(kwarg_arg)
        
        print(eval_args, eval_kwargs)
        return function(*eval_args, **eval_kwargs)


    _self.keyword[name] = [inner, 0,0,',', parsing.STRICT]
    _self.kwhash.append(name)
    _self.help_sc.append(name)
    return inner

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions