<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2008 <NAME> & <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import re
from copy import copy
import logging

class Router:
    """ Handles the url routing... """
    
    class __impl:
        def __init__(self):
            self.__routing_root = {
                'controller': 'welcome',
                'action': 'index',
            }
            self.__routing_table = []            
            
        def connect(self, pattern, tbl = {}):
            """ Add routing pattern """
            p = pattern
            mat = re.findall(':([^/]+)', p)
            for i in range(len(mat)):
                p = p.replace(':' + mat[i], '([^/]+)')
                tbl[mat[i]] = i

            if p[0] != '^': p = '^' + p
            if p[-1] != '$': p += '$'

            self.__routing_table.append({
                'pattern': p,
                'mlist': mat, 
                'm': copy(tbl),
            })
        
        def root(self, map = {}):
            """ Set the root (/) routing... """
            self.__routing_root['controller'] = map.get('controller', self.__routing_root['controller'])
            self.__routing_root['action'] = map.get('action', self.__routing_root['action'])
        
        def resolve(self, url):
            # add default routing
            self.connect('/:controller/:action')
            self.connect('/:controller', {'action': 'index'})
            
            """ Resolve the url to the correct mapping """
            if url == '/':
                return self.__routing_root

            
            for rule in self.__routing_table:
                mat = re.findall(rule['pattern'], url)
                if mat:
                    logging.error(rule)
                    logging.error(mat)
                    if isinstance(mat[0], tuple):
                        for i in range(len(mat[0])):
                            rule['m'][rule['mlist'][i]] = mat[0][i]
                    elif isinstance(mat[0], basestring) and rule['mlist']:
                        rule['m'][rule['mlist'][0]] = mat[0]
                        
                    return rule['m']
                
            return None
    
    __instance = None<commit_msg>Avoid the connected pattern and don't modify the routing table.<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2008 <NAME> & <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import re
from copy import copy
import logging

class Router:
    """ Handles the url routing... """
    
    class __impl:
        def __init__(self):
            self.__routing_root = {
                'controller': 'welcome',
                'action': 'index',
            }
            self.__pattern_table = []
            self.__routing_table = []
            
        def connect(self, pattern, tbl = {}):
            """ Add routing pattern """
            if not pattern in self.__pattern_table:
                p = pattern
                mat = re.findall(':([^/]+)', p)
                for i in range(len(mat)):
                    p = p.replace(':' + mat[i], '([^/]+)')
                    tbl[mat[i]] = i
    
                if p[0] != '^': p = '^' + p
                if p[-1] != '$': p += '$'
    
                self.__routing_table.append({
                    'pattern': p,
                    'mlist': mat, 
                    'm': copy(tbl),
                })
                self.__pattern_table.append(pattern)
        
        def root(self, map = {}):
            """ Set the root (/) routing... """
            self.__routing_root['controller'] = map.get('controller', self.__routing_root['controller'])
            self.__routing_root['action'] = map.get('action', self.__routing_root['action'])
        
        def resolve(self, url):
            # add default routing
            self.connect('/:controller/:action')
            self.connect('/:controller', {'action': 'index'})
            
            """ Resolve the url to the correct mapping """
            if url == '/':
                return self.__routing_root

            logging.error(self.__routing_table)
            
            for rule in self.__routing_table:
                mat = re.findall(rule['pattern'], url)
                mapping = copy(rule['m'])
                if mat:
                    if isinstance(mat[0], tuple):
                        for i in range(len(mat[0])):
                            mapping[rule['mlist'][i]] = mat[0][i]
                    elif isinstance(mat[0], basestring) and rule['mlist']:
                        mapping[rule['mlist'][0]] = mat[0]
                        
                    return mapping
                
            return None
    
    __instance = None
{'fraction_non_alphanumeric': 0.12947514772332291, 'fraction_numerical': 0.005908932916232186, 'mean_word_length': 1.5397175639894087, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1515116', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1475, 'n_words': 438}