Beispiel #1
0
 def execute(self):
     #Generating and connecting output
     #listen to user, open read file, or other
     #configure output, write file, or other
     #print self._opt
     if self._opt["expression"] and self._opt["outputfiledic"]: #input type: expression #output: file
         myexpression = {"input":self._opt["expression"]}
         outputdic = parse_shell_dict(self._opt["outputfiledic"])
         resultdic = self.__maingt(myexpression, outputdic)
         resultdic = bool_dict_values(resultdic)
         from .Shell import save_result_to_output
         save_result_to_output(resultdic, outputdic)
         return resultdic
     elif self._opt["expression"] and not self._opt["outputfiledic"]:
         myexpression = {"input":self._opt["expression"]}
         result = self.__maingt(myexpression)["output"]
         #result = bool_dict_values(str(result["output"]))
         from pydsl.Function.Function import Error
         if isinstance(result, Error):
             print(result)
         else:
             print(result)
         return result #FIXME: Solo en el modo expresion se espera de resultado para test 
     elif self._opt["inputfiledic"]:
         inputdic = parse_shell_dict(self._opt["inputfiledic"])
         outputdic = {"output":"stdout"}
         if self._opt["outputfiledic"]:
             outputdic = parse_shell_dict(self._opt["outputfiledic"])
         stringdic = open_files_dict(inputdic)
         resultdic = self.__maingt(stringdic)
         resultdic = bool_dict_values(resultdic)
         from pydsl.Interaction.Shell import save_result_to_output
         save_result_to_output(resultdic, outputdic)
         #close_files_dict(inputdic)
     elif self._opt["pipemode"]:
         from pydsl.Interaction.Shell import StreamFileToTransformerInteraction
         assert(len(self.__maingt.inputchanneldic) == 1)
         assert(len(self.__maingt.outputchanneldic) == 1)
         inputname = list(self.__maingt.inputchanneldic.keys())[0]
         outputname = list(self.__maingt.outputchanneldic.keys())[0]
         interactor = StreamFileToTransformerInteraction(self.__maingt, {inputname:"stdin"} , {outputname:"stdout"})
         interactor.start()
     elif not self._opt["inputfiledic"] and not self._opt["outputfiledic"] and not self._opt["expression"]:
         from pydsl.Interaction.Shell import CommandLineToTransformerInteraction
         interactor = CommandLineToTransformerInteraction(self.__maingt)
         interactor.start()
     else:
         raise Exception
     return True
Beispiel #2
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
Beispiel #3
0
def checker(expression = None, outputfiledic = None, inputfiledic = None, pipemode = None, **kwargs ):
    #Generating and connecting output
    #listen to user, open read file, or other
    #configure output, write file, or other
    from pydsl.Function.Python import HostPythonTransformer
    maingt = HostPythonTransformer({"input":"cstring"},{"output":"TrueFalse"},{"checker":"GrammarChecker"}, checkfun) 
    if expression and outputfiledic: #input type: expression #output: file
        myexpression = {"input":expression}
        outputdic = parse_shell_dict(outputfiledic)
        resultdic = maingt(myexpression, outputdic)
        resultdic = bool_dict_values(resultdic)
        from pydsl.Interaction.Shell import save_result_to_output
        save_result_to_output(resultdic, outputdic)
        return resultdic
    elif expression and not outputfiledic:
        myexpression = {"input":expression}
        result = maingt(myexpression)["output"]
        #result = bool_dict_values(str(result["output"]))
        print(result)
        return result #FIXME: Only expression mode expects a returned result
    elif inputfiledic:
        inputdic = parse_shell_dict(inputfiledic)
        outputdic = {"output":"stdout"}
        if outputfiledic:
            outputdic = parse_shell_dict(outputfiledic)
        stringdic = open_files_dict(inputdic)
        resultdic = maingt(stringdic)
        resultdic = bool_dict_values(resultdic)
        from pydsl.Interaction.Shell import save_result_to_output
        save_result_to_output(resultdic, outputdic)
    elif pipemode:
        from pydsl.Interaction.Shell import StreamFileToTransformerInteraction
        assert(len(maingt.inputchanneldic) == 1)
        assert(len(maingt.outputchanneldic) == 1)
        inputname = list(maingt.inputchanneldic.keys())[0]
        outputname = list(maingt.outputchanneldic.keys())[0]
        interactor = StreamFileToTransformerInteraction(maingt, {inputname:"stdin"} , {outputname:"stdout"})
        interactor.start()
    elif not inputfiledic and not outputfiledic and not expression:
        from pydsl.Interaction.Shell import CommandLineToTransformerInteraction
        interactor = CommandLineToTransformerInteraction(maingt)
        interactor.start()
    else:
        raise Exception
    return True