<commit_before># lookup.py
# Copyright (C) 2006 <NAME> <EMAIL>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import os, stat, posixpath, re
from mako import exceptions, util
from mako.template import Template

try:
    import threading
except:
    import dummy_threading as threading
    
class TemplateCollection(object):
    def has_template(self, uri):
        try:
            self.get_template(uri)
            return True
        except exceptions.TemplateLookupException, e:
            return False
    def get_template(self, uri):
        raise NotImplementedError()
        
class TemplateLookup(TemplateCollection):
    def __init__(self, directories=None, module_directory=None, filesystem_checks=False, collection_size=-1, format_exceptions=False, error_handler=None, output_encoding=None):
        self.directories = directories or []
        self.module_directory = module_directory
        self.filesystem_checks = filesystem_checks
        self.collection_size = collection_size
        self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'output_encoding':output_encoding, 'module_directory':module_directory}
        if collection_size == -1:
            self.__collection = {}
        else:
            self.__collection = util.LRUCache(collection_size)
        self._mutex = threading.Lock()
        
    def get_template(self, uri):
        try:
            if self.filesystem_checks:
                return self.__check(uri, self.__collection[uri])
            else:
                return self.__collection[uri]
        except KeyError:
            self._mutex.acquire()
            try:
                try:
                    return self.__collection[uri]
                except KeyError:
                    for dir in self.directories:
                        srcfile = posixpath.join(dir, uri)
                        if os.access(srcfile, os.F_OK):
                            return self.__load(srcfile, uri)
                    else:
                        raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri)
            finally:
                self._mutex.release()

    def __ident_from_uri(self, uri):
        return re.sub(r"\W", "_", uri)
        
    def __load(self, filename, uri):
        try:
            self.__collection[uri] = Template(identifier=self.__ident_from_uri(uri), description=uri, filename=filename, lookup=self, **self.template_args)
            return self.__collection[uri]
        except:
            self.__collection.pop(uri, None)
            raise
            
    def __check(self, uri, template):
        if template.filename is None:
            return template
        if not os.access(template.filename, os.F_OK):
            self.__collection.pop(uri, None)
            raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri)
        elif template.module._modified_time < os.stat(template.filename)[stat.ST_MTIME]:
            return __load(template.filename, uri)
        else:
            return template
            
    def put_string(self, uri, text):
        lock = sync.NameLock(uri)
        lock.acquire()
        try:
            self.__collection[uri] = Template(text, lookup=self, description=uri, **self.template_args)
        finally:
            lock.release()
    def put_template(self, uri, template):
        lock = sync.NameLock(uri)
        lock.acquire()
        try:
            self.__collection[uri] = template
        finally:
            lock.release()
        
            
            <commit_msg>took out extra mutex thing<commit_after># lookup.py
# Copyright (C) 2006 <NAME> <EMAIL>
#
# This module is part of Mako and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import os, stat, posixpath, re
from mako import exceptions, util
from mako.template import Template

try:
    import threading
except:
    import dummy_threading as threading
    
class TemplateCollection(object):
    def has_template(self, uri):
        try:
            self.get_template(uri)
            return True
        except exceptions.TemplateLookupException, e:
            return False
    def get_template(self, uri):
        raise NotImplementedError()
        
class TemplateLookup(TemplateCollection):
    def __init__(self, directories=None, module_directory=None, filesystem_checks=False, collection_size=-1, format_exceptions=False, error_handler=None, output_encoding=None):
        self.directories = directories or []
        self.module_directory = module_directory
        self.filesystem_checks = filesystem_checks
        self.collection_size = collection_size
        self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'output_encoding':output_encoding, 'module_directory':module_directory}
        if collection_size == -1:
            self.__collection = {}
        else:
            self.__collection = util.LRUCache(collection_size)
        self._mutex = threading.Lock()
        
    def get_template(self, uri):
        try:
            if self.filesystem_checks:
                return self.__check(uri, self.__collection[uri])
            else:
                return self.__collection[uri]
        except KeyError:
            self._mutex.acquire()
            try:
                try:
                    return self.__collection[uri]
                except KeyError:
                    for dir in self.directories:
                        srcfile = posixpath.join(dir, uri)
                        if os.access(srcfile, os.F_OK):
                            return self.__load(srcfile, uri)
                    else:
                        raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri)
            finally:
                self._mutex.release()

    def __ident_from_uri(self, uri):
        return re.sub(r"\W", "_", uri)
        
    def __load(self, filename, uri):
        try:
            self.__collection[uri] = Template(identifier=self.__ident_from_uri(uri), description=uri, filename=filename, lookup=self, **self.template_args)
            return self.__collection[uri]
        except:
            self.__collection.pop(uri, None)
            raise
            
    def __check(self, uri, template):
        if template.filename is None:
            return template
        if not os.access(template.filename, os.F_OK):
            self.__collection.pop(uri, None)
            raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri)
        elif template.module._modified_time < os.stat(template.filename)[stat.ST_MTIME]:
            return __load(template.filename, uri)
        else:
            return template
            
    def put_string(self, uri, text):
        self.__collection[uri] = Template(text, lookup=self, description=uri, **self.template_args)
    def put_template(self, uri, template):
        self.__collection[uri] = template
        
            
            
{'fraction_non_alphanumeric': 0.08837144059475383, 'fraction_numerical': 0.0016832655351381681, 'mean_word_length': 2.123083661848445, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5479532', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1831, 'n_words': 440}