Beispiel #1
0
class ViewSystem(dict):
    
    def __init__(self, config, site):
        
        self.site = site
        self.ROUTES = Parameter()
        self.VIEWS = Parameter()
        tweak(self, "Site", config)
        assert isinstance(self.VIEWS, Section)
        #у секций ленивый импорт     
        self.VIEWS.preload()
        
        self.rules = []
        self.make_rule = RuleFactory()      
        #rule = self.make_rule(name)
        
        for name, value in self.VIEWS.items():
            view = value()
            view.Site = site     
            self.VIEWS[name] = view
        
        for pattern, tpl in self.ROUTES.items():
            
            rule = self.make_rule(pattern)
            arr = tpl.split(".")
            if len(arr) == 2:
                view_name, method_name = arr
            elif len(arr) == 1:
                view_name, method_name = arr[0], "__call__"
            elif len(arr) > 2:
                raise Exception("Слишком детальный маршрут: " + tpl)

            assert view_name in self.VIEWS.keys()
            
            rule.setObject(self.VIEWS[view_name])
            rule.setMethod(method_name)
            
            self.rules.append(rule)
    
    def request(self, httpreq):
        
        for rule in self.rules:
            caller = rule.check(httpreq.PATH)
            if caller:
                result = caller(request=httpreq)
                if result:
                    if isinstance(result, (list, tuple)):
                        httpreq.extend(result)
                    else:
                        httpreq.append(result)
                return httpreq
        
        raise Exception("404")
Beispiel #2
0
class StorageSystem(object):
    def __init__(self, site_config):

        self.storages = {}
        self.FOLDERS = Parameter()

        tweak(self, "Site", site_config)

        assert isinstance(self.FOLDERS, Section)
        self.FOLDERS.preload()

        for name, params in self.FOLDERS.items():

            path = params["path"]
            extensions = params["extensions"]
            conversions = params["conversions"]
            fakepath = params["fakepath"]

            assert extensions

            self.add_storage(Storage(name, path, extensions, conversions=conversions, fakepath=fakepath))

    def __getattribute__(self, name):

        default = object.__getattribute__

        storages = default(self, "storages")
        if name in storages:
            logger.debug("'%s' storage requested", name)
            return storages[name]
        else:
            return default(self, name)

    def __call__(self, name):
        return self.__getattribute__(name)

    def add_storage(self, storage):

        assert storage
        self.storages[storage._name] = storage

    def del_storage(self, storage=None, name=None):

        assert storage.name or name in self.categories
        del self.storages[storage.name or name]