Esempio n. 1
0
    def parse_route(self, route_in, debug=1, tracking=False):
        ##        self.connections = []
        self.connection_dict = {}
        self.filter_dict = {}
        self.token_counter = 0
        self.filter_counter = 0
        self.branch_counter = 0
        route2 = route_in.strip()
        if not route2:  # No input to parse
            return None, None, None
        # Make input into a branch, to simplify start/end handling. Therefore
        # it must not already start or end with parentheses. The newline is
        # needed before the final parenthesis to allow a trailing comment,
        # which would otherwise hide it, giving an "Unexpected end of file"
        # syntax error.
##        wrapped_route = '$$$start$$$(%s\n)' % route2
        wrapped_route = route2
        ##        print '**10490** Parsing "%s"' % wrapped_route.replace('\n', '...')
        print '**10490** Parsing "%s"' % wrapped_route
        route3 = self.parser.parse(wrapped_route,
                                   debug=debug,
                                   tracking=tracking)
        print '\n**10430** route2 = "%s"' % route2
        print '**10470** route3 = "%s"' % route3
        ##        return None, None, None # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<REMOVE
        if route3:
            ##route4 = route3.split('None ')[1]  # Remove leading None
            ##if not route4[0] == '(' or not route4[-1] == ')':
            ##raise SyntaxError, 'Bad parentheses in "%s"' % route4
            ##route_with_prefixes = route4[1:-1]  # Remove outer parentheses
            route_with_prefixes = route3  # Remove outer parentheses
            route5 = fut.strip_prefixes(route_with_prefixes,
                                        prefix_regex=r'\d\d\d\d' + k_label_sep)
            ##            self.connections.sort()
            connections1_iter = sorted(self.connection_dict.iteritems())
            ##            connections2_gen = (link.split() for link in self.connections)
            connections2_gen = (conn_key.split() + [filter_to]
                                for conn_key, filter_to in connections1_iter)
            connections3 = [
                self.remake_connection(*link2) for link2 in connections2_gen
            ]
            ##filters_gen = (fut.strip_prefix(link.split()[0], k_label_sep)
            ##for link in self.connections)
            # Maintain the order
            ##filters = [filt for filt in set(filters_gen)]
            filter_names = []
            ##            for filt in filters_gen:
            for filt in sorted(self.connection_dict.iterkeys()):
                if filt not in filter_names:
                    filter_names.append(filt)
            return route5, connections3, filter_names
        else:
            return None, None, None
 def test_strip_prefixes(self):
     regex = r'\d\d\d\d~'
     self.assertEquals(fut.strip_prefixes('1234~xx', regex), 'xx')
     self.assertEquals(fut.strip_prefixes('123~xx', regex), '123~xx')
     self.assertEquals(fut.strip_prefixes('123~xx (5678~yy) 9876~zz', 
                                          regex), '123~xx (yy) zz')
     self.assertEquals(fut.strip_prefixes('1234~2345~', regex), '')
     self.assertEquals(fut.strip_prefixes('qwert0000~yui', regex), 
                       'qwertyui')
     self.assertEquals(fut.strip_prefixes(None, regex), None)
     self.assertEquals(fut.strip_prefixes(123, regex), 123)
Esempio n. 3
0
    def parse_route(self, route_in, debug=1, tracking=False): 
##        self.connections = []
        self.connection_dict = {}
        self.filter_dict = {}
        self.token_counter = 0
        self.filter_counter = 0
        self.branch_counter = 0
        route2 = route_in.strip()
        if not route2:  # No input to parse
            return None, None, None
        # Make input into a branch, to simplify start/end handling. Therefore
        # it must not already start or end with parentheses. The newline is
        # needed before the final parenthesis to allow a trailing comment,
        # which would otherwise hide it, giving an "Unexpected end of file"
        # syntax error.
##        wrapped_route = '$$$start$$$(%s\n)' % route2
        wrapped_route = route2
##        print '**10490** Parsing "%s"' % wrapped_route.replace('\n', '...')
        print '**10490** Parsing "%s"' % wrapped_route
        route3 = self.parser.parse(wrapped_route, debug=debug, 
                                   tracking=tracking)
        print '\n**10430** route2 = "%s"' % route2
        print '**10470** route3 = "%s"' % route3
##        return None, None, None # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<REMOVE
        if route3:
            ##route4 = route3.split('None ')[1]  # Remove leading None
            ##if not route4[0] == '(' or not route4[-1] == ')':
                ##raise SyntaxError, 'Bad parentheses in "%s"' % route4
            ##route_with_prefixes = route4[1:-1]  # Remove outer parentheses
            route_with_prefixes = route3  # Remove outer parentheses
            route5 = fut.strip_prefixes(route_with_prefixes,
                                        prefix_regex=r'\d\d\d\d' + k_label_sep)
##            self.connections.sort()
            connections1_iter = sorted(self.connection_dict.iteritems())
##            connections2_gen = (link.split() for link in self.connections)
            connections2_gen = (conn_key.split() + [filter_to]
                                for conn_key, filter_to in connections1_iter)
            connections3 = [self.remake_connection(*link2) 
                            for link2 in connections2_gen]
            ##filters_gen = (fut.strip_prefix(link.split()[0], k_label_sep) 
                           ##for link in self.connections)
            # Maintain the order
            ##filters = [filt for filt in set(filters_gen)]
            filter_names = []
##            for filt in filters_gen:
            for filt in sorted(self.connection_dict.iterkeys()):
                if filt not in filter_names:
                    filter_names.append(filt)
            return route5, connections3, filter_names
        else:
            return None, None, None
Esempio n. 4
0
 def test_strip_prefixes(self):
     regex = r'\d\d\d\d~'
     self.assertEquals(fut.strip_prefixes('1234~xx', regex), 'xx')
     self.assertEquals(fut.strip_prefixes('123~xx', regex), '123~xx')
     self.assertEquals(
         fut.strip_prefixes('123~xx (5678~yy) 9876~zz', regex),
         '123~xx (yy) zz')
     self.assertEquals(fut.strip_prefixes('1234~2345~', regex), '')
     self.assertEquals(fut.strip_prefixes('qwert0000~yui', regex),
                       'qwertyui')
     self.assertEquals(fut.strip_prefixes(None, regex), None)
     self.assertEquals(fut.strip_prefixes(123, regex), 123)
Esempio n. 5
0
    def parse_route(self, route_in, debug=False, tracking=False): 
        self.connections = []
        self.filter_dict = {}
        self.token_counter = 0
##        print '**16200** self = %s' % self
##        pprint.pprint(self.__dict__)
##        print '**16210** in parse_route, self.token_counter = %s' % (
##            self.token_counter)
        self.filter_counter = 0
        self.branch_counter = 0
        route2 = route_in.strip()
        if not route2:  # No input to parse
            return None, None, None
        # Make input into a branch, to simplify start/end handling. Therefore
        # it must not already start or end with parentheses. The newline is
        # needed before the final parenthesis to allow a trailing comment,
        # which would otherwise hide it, giving an "Unexpected end of file"
        # syntax error.
        route3 = self.parser.parse('(%s\n)' % route2, 
                                   debug=debug, tracking=tracking)
        if route3:
            route4 = route3.split('None ')[1]  # Remove leading None
            if not route4[0] == '(' or not route4[-1] == ')':
                raise SyntaxError, 'Bad parentheses in "%s"' % route4
            route_with_prefixes = route4[1:-1]  # Remove outer parentheses
            route5 = fut.strip_prefixes(route_with_prefixes,
                                         prefix_regex=r'\d\d\d\d' + k_label_sep)
            self.connections.sort()
            connections2_gen = (link.split() for link in self.connections)
            connections3 = [self.remake_connection(*link2) 
                            for link2 in connections2_gen]
            filters_gen = (fut.strip_prefix(link.split()[0], k_label_sep) 
                           for link in self.connections)
            # Maintain the order
            ##filters = [filt for filt in set(filters_gen)]
            filter_names = []
            for filt in filters_gen:
                if filt not in filter_names:
                    filter_names.append(filt)
            ##print '**10560** route5 = \n    %s\n-----------' % (
                ##route5.replace(' ', '\n    '))
            ##if connections3:
                ##self.print_connections(connections3)
                
            return route5, connections3, filter_names
        else:
            return None, None, None