예제 #1
0
	def ImportFromString(self, text):
		#alphabet + number + underbar
		nameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
		stringDoubleG = Literal('"') + SkipTo('"') + Literal('"')
		stringSingleG = Literal("'") + SkipTo("'") + Literal("'")
		stringG = stringDoubleG | stringSingleG
		#comment
		comment = (Literal('/*').suppress() + SkipTo('*/').suppress() + Literal('*/').suppress())
		digraphNameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
		digraphNameG.setParseAction( self.DigraphNameAction )
		nodeNameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
		nodeNameG.setParseAction( self.NodeNameAction )
		startG = Literal('digraph').suppress() + digraphNameG.suppress() + Literal('{').suppress()
		endG = Literal('}').suppress()

		attrBaseG = nameG+Literal('=').suppress()+(nameG ^ stringG)
		attrBaseG.setParseAction( self.AttributeAction )
		attrRepeatG = attrBaseG + ZeroOrMore( Literal(',').suppress() + attrBaseG )
		attrG = ZeroOrMore( Literal('[').suppress() + ZeroOrMore( attrRepeatG ) + Literal(']').suppress() )
		nodeG = nodeNameG + attrG
		edgeNameG = nameG + Literal('->').suppress() + nameG
		edgeNameG.setParseAction( self.EdgeNameAction )
		edgeG = edgeNameG + attrG
		sizeG = Literal('size') + Literal('=') + (quotedString | dblQuotedString)
		baseG = (sizeG | (nodeG ^ edgeG))  + Literal(';').suppress()
		baseG = baseG | comment
		baseG = ZeroOrMore(baseG)
		grammerG = startG + baseG + endG
		grammerG.parseString(text)
예제 #2
0
    def __init__(self):
        super(UpstreamParser, self).__init__()

        word = Word(alphanums + '-' + '_' + '.' + '/' + '$' + ':')
        upstream = Literal('upstream').suppress()
        lbrace = Literal('{').suppress()
        rbrace = Literal('}').suppress()
        semicolon = Literal(';').suppress()
        server = Literal('server').suppress()

        self.upstream_def = upstream + word

        self.server_def = upstream + word.suppress() + lbrace + ZeroOrMore(
            server + word + semicolon) + rbrace

        comment = Literal('#') + Optional(restOfLine)
        self.server_def.ignore(comment)
예제 #3
0
    def parse_as_insert(self) -> dict:
        ''' Parse insert. Example: 'insert into database.table (columns) values (values)'
        '''

        text = self._sql

        result = {'database': None, 'table': None, 'columns': [], 'values': []}

        word = Word(alphas)

        from_value = (QuotedString('`')
                      | originalTextFor(Word(printables, excludeChars='().`')))

        list_value = (
            quotedString
            | originalTextFor(
                OneOrMore(Word(printables, excludeChars="(),")
                          | nestedExpr())))

        expr = (word.suppress() + word.suppress() +
                (delimitedList(from_value, delim='.'))('db_table') +
                (Optional(originalTextFor(nestedExpr())))('columns') +
                word.suppress() + (originalTextFor(nestedExpr()))('values'))

        r = expr.parseString(text).asDict()

        if len(r['db_table']) == 2:
            result['database'] = r['db_table'][0]
            result['table'] = r['db_table'][1]
        else:
            result['table'] = r['db_table'][0]

        LPAR, RPAR = map(Suppress, "()")
        parenthesis_list_expr = LPAR + delimitedList(list_value) + RPAR

        if 'columns' in r:
            columns = r['columns']
            if isinstance(columns, list):
                if len(columns) != 1:
                    raise Exception(
                        f"Error when parse columns list: {columns}")
                columns_str = columns[0]
            else:
                columns_str = columns
            columns = parenthesis_list_expr.parseString(columns_str)
            result['columns'] = columns.asList()

        values = r['values']
        if isinstance(values, list):
            if len(values) != 1:
                raise Exception(f"Error when parse values list: {values}")
            values_str = values[0]
        else:
            values_str = values
        values = parenthesis_list_expr.parseString(values_str)
        result['values'] = values.asList()

        for i, val in enumerate(result['values']):
            if isinstance(val, str) and val.lower() == 'null':
                result['values'][i] = None
            elif val == '?':
                result['values'][i] = SQL_PARAMETER
            elif isinstance(val, str) and val.lower() == 'default':
                result['values'][i] = SQL_DEFAULT
            elif SqlStatementParser.is_int_str(val):
                result['values'][i] = int(val)
            elif SqlStatementParser.is_float_str(val):
                result['values'][i] = float(val)
            elif SqlStatementParser.is_quoted_str(val):
                result['values'][i] = SqlStatementParser.unquote(val)
            elif isinstance(val, str):
                # it should be in one case, only if server send function as argument, for example:
                # insert into table (datetime) values (now())
                raise Exception(f"Error: cant determine type of '{val}'")

        for i, val in enumerate(result['columns']):
            if SqlStatementParser.is_quoted_str(val):
                result['columns'][i] = SqlStatementParser.unquote(val)

        return result
예제 #4
0
파일: rcgParser.py 프로젝트: rwu17/P6
    def get_player_info(self, line):
        left_p = Literal("(").suppress()
        right_p = Literal(")").suppress()
        frame_number = Word(nums)

        # Frame and ball information
        show_frame = Word("show ") + frame_number
        ball = left_p + left_p + Literal(
            "b") + right_p + Word(nums + "-.") * 4 + right_p

        # Player information
        player_number = left_p + (Word("r") ^ Word("l")) + Word(nums) + right_p

        # Player positions
        player_position = Word(alphanums + "-.")

        # Player view mode - H for high and L for low
        view_mode = left_p + Literal("v") + (
            Word("h") ^ Word("l")) + Word(nums) + right_p
        stamina = left_p + Literal("s") + Word(nums + "-.") * 4 + right_p

        # Outer flag rules
        flag_pos = Word("lrbtc", max=1)
        field_side = Word("lr", max=1)
        distance_from_center = Word(nums)
        outer_flag = flag_pos + ZeroOrMore(field_side) + distance_from_center

        # Inner flag rules
        inner_flag_pos = Word("lrc", max=1)
        inner_flag = inner_flag_pos + (Word("b") ^ Word("t"))

        # Center flag
        center_flag = Literal("c")
        flag = left_p + Literal("f") + (outer_flag ^ inner_flag
                                        ^ center_flag) + right_p

        # Additional information
        additional = left_p + Literal("c") + Word(nums + "-.") * 11 + right_p

        player = left_p + Group(player_number) + Group(
            ZeroOrMore(
                player_position)) + Group(view_mode) + Group(stamina) + Group(
                    ZeroOrMore(flag)) + Group(additional) + right_p

        # Frame lines
        frame_line1 = show_frame.suppress() + ball.suppress() + (
            Group(player) * 11)
        frame_line2 = (Group(player) * 11)

        read_line = left_p + (frame_line1 + frame_line2) + right_p

        parsed_players = read_line.parseString(line)

        # Place information into dictionary to easily query information
        player_info = {}
        counter = 1
        for player in parsed_players:
            body_info = player[1]
            view_mode = player[2]
            stamina = player[3]

            player_info[str(counter)] = {
                "side": player[0][0],
                "unum": player[0][1],
                "type_id": body_info[0],
                "state": body_info[1],
                "pos_x": body_info[2],
                "pos_y": body_info[3],
                "vel_x": body_info[4],
                "vel_y": body_info[5],
                "body_angle": body_info[6],
                "head_angle": body_info[7],
                "view_quality": view_mode[1],
                "view_width": view_mode[2],
                "stamina": stamina[1],
                "stamina_effort": stamina[2],
                "stamina_recovery": stamina[3],
                "stamina_capacity": stamina[4]
            }

            counter += 1

        return player_info
예제 #5
0
rus_alphas = 'йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ'
rus_alphanums = 'йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ1234567890,.:;()«»-–'  #Короткое (среднее) тире – Alt + 0150; Дефиc - клавиша на клавиатуре

f = open('destiny.txt', 'r')
lines = f.readlines()
text = ' '.join(lines)

text = text.replace("\r", "")
text = text.replace("\n", "")

Dash = Literal('—')  #тире (длинное)	—	Alt + 0151
Number = Word(nums) + Dash.suppress()
Name = Word(nums).suppress() + Dash.suppress() + Word(rus_alphanums)

#Шаблон для поиска описания
Description = Name.suppress() + Word(rus_alphanums +
                                     ' ') + Literal('/').suppress()

destinyData = {}

for num in Number.searchString(text):
    key = int(num[0])
    destinyData[key] = dict({'number': num[0]})

key = 1
for name in Name.searchString(text):
    destinyData[key].update({'name': name[0]})
    key += 1

key = 1
for description in Description.searchString(text):
예제 #6
0
class ObfuscateBNF(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, get_obfuscated):
        """BNF grammar for source statements.

        Parameters
        ----------
        get_obfuscated : function
            Function to return the obfuscated name for an identifier.
        """
        self.get_obfuscated = get_obfuscated

        self.directive = oneOf("#:")
        self.comment = ~self.directive + pythonStyleComment

        self.separator = Word("~!@$%^&*()+`-={}|[]:;<>?,/.", max=2)

        self.string = \
            QuotedString(quoteChar='"', escChar='\\', multiline=False,
                         unquoteResults=False) |\
            QuotedString(quoteChar="'", escChar='\\', multiline=False,
                         unquoteResults=False)

        self.doc_string = \
            QuotedString(quoteChar='"""', escChar='\\', multiline=True,
                         unquoteResults=False) |\
            QuotedString(quoteChar="'''", escChar='\\', multiline=True,
                         unquoteResults=False)
        self.string_or_doc = self.doc_string | self.string
        self.triple_quote = Literal("'''") | Literal('"""')

        self.e = Literal('E') | Literal('e')
        self.point = Literal('.')

        self.plusorminus = Literal('+') | Literal('-')
        self.number = Word(nums)
        self.integer = Combine(Optional(self.plusorminus) + self.number)
        self.fnumber = Combine(
            self.integer +
            Optional(self.point + Optional(self.number)) +
            Optional(self.e + self.integer))

        self.tab = Literal('    ')

        self.ident = Word(alphas+'_', alphanums+'_')
        self.conseq_idents_numbs = OneOrMore(self.ident | self.fnumber)
        self.attrib = self.ident + OneOrMore('.'+self.ident)

        self.statement = (
            ZeroOrMore(
                (self.directive |
                 self.tab |
                 self.conseq_idents_numbs |
                 self.separator |
                 self.string_or_doc |
                 self.triple_quote)
            ) + Optional(self.comment).suppress()
        )

        self.attribs = (
            ZeroOrMore(
                (self.directive.suppress() |
                 self.tab.suppress() |
                 self.attrib |
                 self.ident.suppress() |
                 self.separator.suppress() |
                 self.fnumber.suppress() |
                 self.string_or_doc.suppress() |
                 self.triple_quote.suppress())
            ) + Optional(self.comment).suppress()
        )

        self.conseq_idents = (
            ZeroOrMore(
                (self.directive.suppress() |
                 self.tab.suppress() |
                 self.ident |
                 self.separator.suppress() |
                 self.fnumber.suppress() |
                 self.string.suppress())
            ) + Optional(self.comment).suppress()
        )

        self.conseq_idents_no_obfuscate = (
            ZeroOrMore(
                (self.directive.suppress() |
                 self.tab.suppress() |
                 self.ident |
                 self.separator.suppress() |
                 self.fnumber.suppress() |
                 self.string_or_doc.suppress() |
                 self.triple_quote.suppress())
            ) + Optional(self.comment).suppress()
        )

        self.attribs.setParseAction(self.add_attribs_reserveds)
        self.conseq_idents.setParseAction(self.add_conseq_idents)
        self.conseq_idents_no_obfuscate.setParseAction(
            self.add_conseq_idents_no_obfuscate)
        self.conseq_idents_numbs.setParseAction(
            self.transform_conseq_ident_numbs)
        self.directive.setParseAction(self.transform_directive)

    ###############
    # Parse actions
    ###############
    def add_conseq_idents(self, conseq_idents_list):
        """Add names to obfuscate to identifiers table.

        Parameters
        ----------
        conseq_idents_list : list
        """
        if 'import' not in conseq_idents_list[:] and \
                'except' not in conseq_idents_list[:]:
            add_identifiers(set(conseq_idents_list))

    def add_conseq_idents_no_obfuscate(
            self, conseq_idents_no_obfuscate_list):
        """Add names that are not obfuscated to identifiers table.

        Parameters
        ----------
        conseq_idents_no_obfuscate_list : list
        """
        # If an except error was not added to reserved list, don't obfuscate it
        if 'import' not in conseq_idents_no_obfuscate_list[:] and \
                'except' not in conseq_idents_no_obfuscate_list[:]:
            add_identifiers(set(conseq_idents_no_obfuscate_list),
                            do_obfuscate=False)

    def add_attribs_reserveds(self, attribs_list):
        """Add attributes of reserved names to reserved list.

        Take a list of attributes strings from a source statement, break
        it into lists of objects with their attributes, and add attributes
        that follow a reserved name to the reserved list.

        Example
         ------
         If r is reserved, then
            a.r.c + d.r.e
        would add c and e to reserveds.

        Parameters
        ----------
        attribs_list : list
        """
        if attribs_list:
            # Create an ordered list of attribute parents
            # Ex. a.b.c => [a, b]
            _attrib_list = [attribs_list[0]]
            is_last_token_an_attrib = True
            for token in attribs_list[1:]:
                if is_last_token_an_attrib and token != '.':
                    # End of attrib list reached. Process list.
                    add_attribs_reserveds_list(_attrib_list)
                    # Start new attrib list
                    _attrib_list = [token]
                    is_last_token_an_attrib = True
                elif is_last_token_an_attrib and token == '.':
                    is_last_token_an_attrib = False
                elif not is_last_token_an_attrib and token == '.':
                    continue  # Multiple dots, continue attrib list
                elif not is_last_token_an_attrib and token != '.':
                    _attrib_list.append(token)
                    is_last_token_an_attrib = True
            else:
                # Process last list
                if _attrib_list:
                    add_attribs_reserveds_list(_attrib_list)

    def transform_conseq_ident_numbs(self, conseq_ident_list):
        """Allow for non-name tokens in a statement.

        Names start with an alpha or underscore. Obfuscate these name tokens
        and simply copy unchanged other tokens.

        Parameters
        ----------
        conseq_ident_list : list

        Returns
        -------
        statement : str
        """
        return ' '.join([
            self.get_obfuscated(ident) if
            (ident[0].isalpha() or ident[0] == '_') else ident
            for ident in conseq_ident_list
        ])

    def transform_directive(self, directive_list):
        """Create a directive statement."""
        return ''.join([directive_list[0], ' '])
예제 #7
0
class ObfuscateBNF(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, get_obfuscated):
        """BNF grammar for source statements.

        Parameters
        ----------
        get_obfuscated : function
            Function to return the obfuscated name for an identifier.
        """
        self.get_obfuscated = get_obfuscated

        self.directive = oneOf("#:")
        self.comment = ~self.directive + pythonStyleComment

        self.separator = Word("~!@$%^&*()+`-={}|[]:;<>?,/.", max=2)

        self.string = \
            QuotedString(quoteChar='"', escChar='\\', multiline=False,
                         unquoteResults=False) |\
            QuotedString(quoteChar="'", escChar='\\', multiline=False,
                         unquoteResults=False)

        self.doc_string = \
            QuotedString(quoteChar='"""', escChar='\\', multiline=True,
                         unquoteResults=False) |\
            QuotedString(quoteChar="'''", escChar='\\', multiline=True,
                         unquoteResults=False)
        self.string_or_doc = self.doc_string | self.string
        self.triple_quote = Literal("'''") | Literal('"""')

        self.e = Literal('E') | Literal('e')
        self.point = Literal('.')

        self.plusorminus = Literal('+') | Literal('-')
        self.number = Word(nums)
        self.integer = Combine(Optional(self.plusorminus) + self.number)
        self.fnumber = Combine(self.integer +
                               Optional(self.point + Optional(self.number)) +
                               Optional(self.e + self.integer))

        self.tab = Literal('    ')

        self.ident = Word(alphas + '_', alphanums + '_')
        self.conseq_idents_numbs = OneOrMore(self.ident | self.fnumber)
        self.attrib = self.ident + OneOrMore('.' + self.ident)

        self.statement = (ZeroOrMore(
            (self.directive | self.tab | self.conseq_idents_numbs
             | self.separator | self.string_or_doc | self.triple_quote)) +
                          Optional(self.comment).suppress())

        self.attribs = (ZeroOrMore(
            (self.directive.suppress() | self.tab.suppress() | self.attrib
             | self.ident.suppress() | self.separator.suppress()
             | self.fnumber.suppress() | self.string_or_doc.suppress()
             | self.triple_quote.suppress())) +
                        Optional(self.comment).suppress())

        self.conseq_idents = (ZeroOrMore(
            (self.directive.suppress() | self.tab.suppress() | self.ident
             | self.separator.suppress() | self.fnumber.suppress()
             | self.string.suppress())) + Optional(self.comment).suppress())

        self.conseq_idents_no_obfuscate = (ZeroOrMore(
            (self.directive.suppress() | self.tab.suppress() | self.ident
             | self.separator.suppress() | self.fnumber.suppress()
             | self.string_or_doc.suppress() | self.triple_quote.suppress())) +
                                           Optional(self.comment).suppress())

        self.attribs.setParseAction(self.add_attribs_reserveds)
        self.conseq_idents.setParseAction(self.add_conseq_idents)
        self.conseq_idents_no_obfuscate.setParseAction(
            self.add_conseq_idents_no_obfuscate)
        self.conseq_idents_numbs.setParseAction(
            self.transform_conseq_ident_numbs)
        self.directive.setParseAction(self.transform_directive)

    ###############
    # Parse actions
    ###############
    def add_conseq_idents(self, conseq_idents_list):
        """Add names to obfuscate to identifiers table.

        Parameters
        ----------
        conseq_idents_list : list
        """
        if 'import' not in conseq_idents_list[:] and \
                'except' not in conseq_idents_list[:]:
            add_identifiers(set(conseq_idents_list))

    def add_conseq_idents_no_obfuscate(self, conseq_idents_no_obfuscate_list):
        """Add names that are not obfuscated to identifiers table.

        Parameters
        ----------
        conseq_idents_no_obfuscate_list : list
        """
        # If an except error was not added to reserved list, don't obfuscate it
        if 'import' not in conseq_idents_no_obfuscate_list[:] and \
                'except' not in conseq_idents_no_obfuscate_list[:]:
            add_identifiers(set(conseq_idents_no_obfuscate_list),
                            do_obfuscate=False)

    def add_attribs_reserveds(self, attribs_list):
        """Add attributes of reserved names to reserved list.

        Take a list of attributes strings from a source statement, break
        it into lists of objects with their attributes, and add attributes
        that follow a reserved name to the reserved list.

        Example
         ------
         If r is reserved, then
            a.r.c + d.r.e
        would add c and e to reserveds.

        Parameters
        ----------
        attribs_list : list
        """
        if attribs_list:
            # Create an ordered list of attribute parents
            # Ex. a.b.c => [a, b]
            _attrib_list = [attribs_list[0]]
            is_last_token_an_attrib = True
            for token in attribs_list[1:]:
                if is_last_token_an_attrib and token != '.':
                    # End of attrib list reached. Process list.
                    add_attribs_reserveds_list(_attrib_list)
                    # Start new attrib list
                    _attrib_list = [token]
                    is_last_token_an_attrib = True
                elif is_last_token_an_attrib and token == '.':
                    is_last_token_an_attrib = False
                elif not is_last_token_an_attrib and token == '.':
                    continue  # Multiple dots, continue attrib list
                elif not is_last_token_an_attrib and token != '.':
                    _attrib_list.append(token)
                    is_last_token_an_attrib = True
            else:
                # Process last list
                if _attrib_list:
                    add_attribs_reserveds_list(_attrib_list)

    def transform_conseq_ident_numbs(self, conseq_ident_list):
        """Allow for non-name tokens in a statement.

        Names start with an alpha or underscore. Obfuscate these name tokens
        and simply copy unchanged other tokens.

        Parameters
        ----------
        conseq_ident_list : list

        Returns
        -------
        statement : str
        """
        return ' '.join([
            self.get_obfuscated(ident) if
            (ident[0].isalpha() or ident[0] == '_') else ident
            for ident in conseq_ident_list
        ])

    def transform_directive(self, directive_list):
        """Create a directive statement."""
        return ''.join([directive_list[0], ' '])
예제 #8
0
	if dot['current'] != None :
		obj = dot['current']
		obj[toks[0]] = toks[1]

#alphabet + number + underbar
nameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
stringDoubleG = Literal('"') + SkipTo('"') + Literal('"')
stringSingleG = Literal("'") + SkipTo("'") + Literal("'")
stringG = stringDoubleG | stringSingleG
#comment
comment = (Literal('/*').suppress() + SkipTo('*/').suppress() + Literal('*/').suppress())
digraphNameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
digraphNameG.setParseAction( DigraphNameAction )
nodeNameG = Word( srange("[a-zA-Z_]"), srange("[a-zA-Z0-9_]") )
nodeNameG.setParseAction( NodeNameAction )
startG = Literal('digraph').suppress() + digraphNameG.suppress() + Literal('{').suppress()
endG = Literal('}').suppress()

attrBaseG = nameG+Literal('=').suppress()+(nameG ^ stringG)
attrBaseG.setParseAction( AttributeAction )
attrRepeatG = attrBaseG + ZeroOrMore( Literal(',').suppress() + attrBaseG )
attrG = ZeroOrMore( Literal('[').suppress() + ZeroOrMore( attrRepeatG ) + Literal(']').suppress() )
nodeG = nodeNameG + attrG
edgeNameG = nameG + Literal('->').suppress() + nameG
edgeNameG.setParseAction( EdgeNameAction )
edgeG = edgeNameG + attrG
sizeG = Literal('size') + Literal('=') + (quotedString | dblQuotedString)
baseG = (sizeG | (nodeG ^ edgeG))  + Literal(';').suppress()
#baseG = nodeG + Literal(';').suppress()
baseG = baseG | comment
baseG = ZeroOrMore(baseG)