Пример #1
0
    def handle(self):
        # todo - this may not read the entire data
        command = self.request.recv(2048)
        logging.debug('Received new command: %s', repr(command))

        try:
            self.vault.backup()
        except:
            logging.error('Unable to backup!')

        try:
            parsed_cmd = self.parser.parse(command, 'prog', semantics=ModelBuilderSemantics(
                types=[NoKwStr]
            ))
        except ParseException as e:
            logging.error('Parsing of incoming command failed.')
            self.request.sendall(Response('FAILED').to_json())
            return
        else:
            self.walker.walk(parsed_cmd)

            results = []
            exit_flag = False
            for cmd in self.walker.program_info.commands:
                logging.debug('Processing a command: %s', cmd.__class__.__name__)
                r = self.vault.command_process(self.walker.program_info, cmd)
                results.append(r)
                if isinstance(command, Exit):
                    results.append(r)
                    exit_flag = True
                    break

                if r.status == 'FAILED' or r.status == 'DENIED':
                    try:
                        logging.debug('Command has failed, restoring from backup.')
                        self.vault.restore()
                    except:
                        logging.exception('Unable to restore from a backup!')
                    # remove all previous results
                    results = [r]
                    break

            final_response = ''
            for r in results:
                final_response += '{}\n'.format(r.to_json())
            self.request.sendall(final_response)

            if exit_flag:
                self.vault.stop()
                sys.exit(0)

            for item in self.vault.item_store.keys():

                if self.vault.item_store[item].scope == "local":
                    del self.vault.item_store[item]
Пример #2
0
def compile(grammar, name=None, semantics=None, asmodel=False, **kwargs):
    cache = __compiled_grammar_cache

    key = grammar
    if key in cache:
        model = cache[key]
    else:
        gen = GrammarGenerator(name, **kwargs)
        model = cache[key] = gen.parse(grammar, **kwargs)

    if semantics is not None:
        model.semantics = semantics
    elif asmodel:
        model.semantics = ModelBuilderSemantics()

    return model
Пример #3
0
def compile(grammar,
            name=None,
            semantics=None,
            asmodel=False,
            config: ParserConfig = None,
            **settings):
    cache = __compiled_grammar_cache

    key = (name, grammar, id(semantics))
    if key in cache:
        model = cache[key]
    else:
        gen = GrammarGenerator(name, config=config, **settings)
        model = cache[key] = gen.parse(grammar, config=config, **settings)

    if semantics is not None:
        model.semantics = semantics
    elif asmodel:
        model.semantics = ModelBuilderSemantics()

    return model
Пример #4
0
def test_synth_model():
    grammar = '''
        start::ASeq
            =
            aseq
            $
            ;

        aseq
            =
            {'a'}+
            ;
    '''

    m = compile(grammar, 'ASeq')
    model = m.parse('a a a', semantics=ModelBuilderSemantics())
    assert 'ASeq' == type(model).__name__

    p = pickle.dumps(model)
    new_model = pickle.loads(p)
    assert 'ASeq' == type(new_model).__name__

    assert model.ast == new_model.ast
Пример #5
0
    def test_synth_model(self):
        grammar = '''
            start::ASeq
                =
                values:aseq
                $
                ;

            aseq
                =
                {'a'}+
                ;
        '''

        m = compile(grammar, 'ASeq')
        model = m.parse('a a a', semantics=ModelBuilderSemantics())
        self.assertEqual('ASeq', type(model).__name__)

        p = pickle.dumps(model)
        new_model = pickle.loads(p)
        self.assertEqual('ASeq', type(new_model).__name__)

        self.assertEqual(model._ast, new_model._ast)