Example #1
0
def parts(grammar, part, outputformat = None, expression = None, inputfile = None):
    from pydsl.Memory.Loader import load
    sgrammar = load(grammar) 
    if not part:
        print("Grouplist: " + "\n".join(sgrammar.groups()))
        return True
    if expression:
        myinput = expression
    elif inputfile:
        with open(inputfile, "rb") as f:
            myinput = f.read()
    else:
        raise Exception("No input method")
    result = sgrammar.get_groups(myinput, part)
    dataresults = []
    for left,right in result:
        dataresults.append(myinput[left:right])

    if outputformat == "str":
        for x in range(len(result)):
            print("(" + str(result[x][0]) + "," + str(result[x][1]) + ") " +  str(dataresults[x]))
        return True

    elif outputformat == "json":
        mylist = []
        for x in range(len(result)):
            mylist.append((result[x][0],result[x][1],dataresults[x]))
        import json
        print(json.dumps(mylist))
        return True
Example #2
0
 def __init__(self, inputgrammar, outputgrammar, blockdic):
     self.blockdic = blockdic
     if isinstance(inputgrammar, str):
         from pydsl.Memory.Loader import load
         inputgrammar = load(inputgrammar)
     from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser
     self.inputgrammar = inputgrammar
     self.outputgrammar = outputgrammar
     self.parser = RecursiveDescentParser(inputgrammar)
     self.blockdic = blockdic
Example #3
0
def maketestFromGT(gtname, inputexpression, outputdic):
    """Makes a test and returns True if it finishes successfully"""
    values = {"expression": inputexpression, "directreturn": True, "outputfiledic": None, "inputfiledic": None,
              "pipemode": None}
    from pydsl.Memory.Loader import load
    gt = load(gtname)
    result = gt(inputexpression)
    for key in outputdic.keys():
        if result[key] != outputdic[key]:
            return False
    return True
Example #4
0
def translate(transformer = None, expression = None, inputfiledic = None, outputfiledic = None, inputstreamdic = None, pipemode = None, **kwargs):
    """Read input file contents, creates grammar and transform objects, create connections, 
    and afterwards reads required input/launch main loop"""
    #Generating and connecting output
    #listen to user, open read file, or other
    #configure output, write file, or other
    #print self._opt
    mainfunc = load(transformer) 
    if expression and  outputfiledic: #input type: expression #output: file
        inputdic = parse_shell_dict(expression)
        if not isinstance(inputdic, dict):
            raise TypeError
        outputdic = parse_shell_dict(outputfiledic)
        resultdic = mainfunc(inputdic)
        save_result_to_output(resultdic, outputdic)
        return resultdic
    elif expression and not outputfiledic:
        myexpression = parse_shell_dict(expression)
        result = mainfunc(myexpression)
        if result:
            for key in result.keys():
                result[key] = str(result[key])
        print(result)
        return result #FIXME: this is the only condition that returns a result. Because of tests
    elif inputstreamdic and outputfiledic:
        interactor = StreamFileToTransformerInteraction(mainfunc, parse_shell_dict(inputstreamdic), parse_shell_dict(outputfiledic))
        interactor.start()
    elif inputfiledic and outputfiledic:
        inputdic = parse_shell_dict(inputfiledic)
        outputdic = parse_shell_dict(outputfiledic)
        stringdic = open_files_dict(inputdic)
        resultdic = mainfunc(stringdic)
        save_result_to_output(resultdic, outputdic)
        return resultdic
    elif pipemode:
        assert(len(mainfunc.inputchanneldic) == 1)
        assert(len(mainfunc.outputchanneldic) == 1)
        inputname = list(mainfunc.inputchanneldic.keys())[0]
        outputname = list(mainfunc.outputchanneldic.keys())[0]
        interactor = StreamFileToTransformerInteraction(mainfunc, {inputname:"stdin"} , {outputname:"stdout"})
        interactor.start()
    elif not inputfiledic and not outputfiledic and not expression:
        from pydsl.Interaction.Shell import CommandLineToTransformerInteraction
        interactor = CommandLineToTransformerInteraction(mainfunc)
        interactor.start()
    else:
        raise Exception
    return True
Example #5
0
 def _initHostT(self, namedic):
     """Inits aux GTs. if a requested aux GT isn't connected, This function will create them"""
     from pydsl.Memory.Loader import load
     for title, gttype in namedic.items():
         self._hostT[title] = load(gttype) 
         LOG.debug("loaded " + str(title) + "auxT")
Example #6
0
 def __init__(self, grammardict):
     from pydsl.Memory.Loader import load
     self.grammardict = {}
     for x in grammardict:
         self.grammardict[x] = load(grammardict[x])
Example #7
0
 def test_grammars(self):
     grammarlist = self.glibrary.all_names()
     from pydsl.Memory.Loader import load
     for grammar in grammarlist:
         load(grammar)
Example #8
0
 def __getattr__(cls, key):
     return load(key)
Example #9
0
 def _initHostT(self, namedic):
     """Inits auxiliary transformers """
     from pydsl.Memory.Loader import load
     for title, gttype in namedic.items():
         self._hostT[title] = load(gttype)
         LOG.debug("loaded " + str(title) + "auxT")
Example #10
0
 def testTranslator(self):
     translator = load("upperCase")
     y = translator(TokenList((("cstring", "abcde"), ("EOF_TYPE", ""))))
     self.assertEqual(y["output"], "ABCDE")