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]
Beispiel #3
0
 def __init__(self):
     
     self.PROVIDER = Parameter()
     self.LOCATION = Parameter()
     self.USER = Parameter()
     self.PASSWD = Parameter()
     self.CONNECT_WITH = Parameter() 
     self.tweak()
     self.models = []
     self.builder = Builder()
     self.connection = None        
     self.preload()
Beispiel #4
0
    def __init__(self, config=None):
        
        #поля конфигурационного файла
        self.PROVIDER = Parameter()
        self.LOCATION = Parameter()
        self.USER = Parameter()
        self.PASSWD = Parameter()
        self.CONNECT_WITH = Parameter() 
        self.DATA_TYPE = Parameter(Data)
        
        tweak(self, "Database", config)

        self.connection = None   
Beispiel #5
0
class PDO(Tweak("Database")):
    
    def __init__(self):
        
        self.PROVIDER = Parameter()
        self.LOCATION = Parameter()
        self.USER = Parameter()
        self.PASSWD = Parameter()
        self.CONNECT_WITH = Parameter() 
        self.tweak()
        self.models = []
        self.builder = Builder()
        self.connection = None        
        self.preload()
        
    def connect(self):
        _args = []
        for arg in self.CONNECT_WITH:
            try:
                _args.append(getattr(self, arg))
            except AttributeError as err:
                raise err
            print(_args)
            self.connection = self.PROVIDER.connect(*_args)
        if self.connection:             
            print("connected")
            
    def regiserModel(self, model):
        model.PDO = self
        self.models.append(model)
        
    def preload(self):
        QueryCreate.builder = self.builder.create(Names.Create.definition)
        QueryInsert.builder = self.builder.create(Names.Insert.definition)
        QuerySelect.builder = self.builder.create(Names.Select.definition)
        QueryUpdate.builder = self.builder.create(Names.Update.definition)
        QueryDelete.builder = self.builder.create(Names.Delete.definition)
        Expression.builder = self.builder.create(Names.Expression.definition)
        
    def gosql(self, function):
        if self.connection:
            return self.connection.execute(function())
        else:
            raise Exception("No connection")
    
    def execute(self, query):
        if self.connection:
            return self.connection.execute(query)
        else:
            raise Exception("No connection")            
Beispiel #6
0
    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)
Beispiel #7
0
    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))
Beispiel #8
0
class PDO():
    
    def __init__(self, config=None):
        
        #поля конфигурационного файла
        self.PROVIDER = Parameter()
        self.LOCATION = Parameter()
        self.USER = Parameter()
        self.PASSWD = Parameter()
        self.CONNECT_WITH = Parameter() 
        self.DATA_TYPE = Parameter(Data)
        
        tweak(self, "Database", config)

        self.connection = None   

    def connect(self):
        _args = []
        for arg in self.CONNECT_WITH:
            try:
                _args.append(getattr(self, arg))
            except AttributeError as err:
                raise err
            self.connection = self.PROVIDER.connect(*_args)
        return self.connection           
               
    def gosql(self, function):
        if self.connection:
            cursor = self.connection.execute(function())
            return self.DATA_TYPE(cursor) if self.DATA_TYPE else cursor
        else:
            raise Exception("No connection")
    
    def execute(self, query):
        if self.connection:
            cursor = self.connection.execute(query)
            return self.DATA_TYPE(cursor) if self.DATA_TYPE else cursor
        else:
            raise Exception("No connection")