wooey / clinto

This converts an assortment of python command line interfaces into a language agnostic build spec for usage in GUI creation.
BSD 3-Clause "New" or "Revised" License
17 stars 6 forks source link

Generic parser interface #6

Closed Chris7 closed 8 years ago

Chris7 commented 9 years ago

This is the beginning work on a generic parser interface that will provide a general framework for adding additional argument parsers.

Chris7 commented 9 years ago

Everything needs to be redone to do this correctly. I'm going to do a PR to get the api generic prior to the release of Wooey, then we can add in extra parsers.

Here is how I believe it should be done: 1) collect all parsers (subparsers, etc.) 2) collect all containers/groups 3) collect all arguments of those groups/containers

Currently we operate in reverse -- we get a parser, then find its arguments, then what container it is.

mfitzp commented 9 years ago

For support for other parsers (click, etc.) and for other languages, it would probably be a good idea define parser classes with attributes defining a few tests prior to attempting to parse, e.g.

class BaseParser(object):

    extensions = []
    regex = ''

    def check_valid(script_path, script_src):
        return os.path.splitext(script_path) in self.extensions and self.contains in script_src

class PythonParser(BaseParser):
    extensions = ['.py'] # Check file extensions in this

class PythonArgParse(PythonParser):
    contains = 'argparse' # Check file contains this

    def parse()
        pass

class PythonArgParse(PythonParser):
    contains = '@click' # Check file contains this

    def parse()
        pass

class RParser(BaseParser):
    extensions = ['.R', '.RScript'] # Check file extensions in this

class RArgParse(RParser):

    def parse()
        pass

parsers = [
    PythonArgParse,
    PythonClick,
    RArgParse,
]

script_path = '...'
with open(script_path, 'r') as f:
    script_src = f.read()

for parser in parsers:
    if parser.check_valid(script_path, script_src)
         break
else:
    # Could not find parser
    return False

return parser.parse(script_path, script_src)    
mfitzp commented 8 years ago

@Chris should this be closed now the generic parser backend is in?

Chris7 commented 8 years ago

Yeah, I would need to rework all this anyways.