Example #1
0
 def test_attributes(self):
     #set & get
     b = Bag(BAG_DATA)
     b.setAttr('phone.home', private=False)
     assert not b.getAttr('phone.home', 'private')
     b.setAttr('phone.home', private=True, test='is a test')
     assert b['phone.home?test'] == 'is a test' #diverso dalla documentazione track
     assert b.getAttr('phone.#sim=vadophone', 'fbplayer') == 'gattuso'
     assert b['phone.#sim=vadophone?fbplayer'] == 'gattuso'
     b.delAttr('phone.home', 'test')
     assert not b['phone.home?test']
Example #2
0
 def test_attributes(self):
     #set & get
     b = Bag(BAG_DATA)
     b.setAttr('phone.home', private=False)
     assert not b.getAttr('phone.home', 'private')
     b.setAttr('phone.home', private=True, test='is a test')
     assert b[
         'phone.home?test'] == 'is a test'  #diverso dalla documentazione track
     assert b.getAttr('phone.#sim=vadophone', 'fbplayer') == 'gattuso'
     assert b['phone.#sim=vadophone?fbplayer'] == 'gattuso'
     b.delAttr('phone.home', 'test')
     assert not b['phone.home?test']
Example #3
0
 def test_update(self):
     b = Bag(BAG_DATA)
     c = Bag()
     c.setItem('hobbie.sport', 'soccer', role='forward')
     c.setItem('name', 'John K.')
     b.update(c)
     assert b['name'] == 'John K.'
     assert b.getAttr('hobbie.sport', 'role') == 'forward'
Example #4
0
 def test_update(self):
     b = Bag(BAG_DATA)
     c = Bag()
     c.setItem('hobbie.sport', 'soccer', role='forward')
     c.setItem('name', 'John K.')
     b.update(c)
     assert b['name'] == 'John K.'
     assert b.getAttr('hobbie.sport', 'role') == 'forward'
Example #5
0
 def _updateColsPermission(self,cols_permission,updater,allcols,editMode):
     result = Bag()
     for colname in allcols:
         currattr = cols_permission.getAttr(colname) or dict()
         updattr = updater.getAttr(colname) or {}
         if currattr or updattr:
             if editMode:
                 currattr['colname'] = colname
             result.setItem(colname,None,_attributes=currattr)
             currattr = result.getAttr(colname)
             for permission in ('forbidden','readonly'):
                 last = currattr.get(permission)
                 upd = updattr.get(permission)
                 if editMode:
                     currattr[permission] = upd
                     if last is not None:
                         currattr['%s_inherited' %permission] = last
                 else:
                     currattr[permission] = upd if upd is not None else last
     return result
Example #6
0
 def _updateColsPermission(self, cols_permission, updater, allcols,
                           editMode):
     result = Bag()
     for colname in allcols:
         currattr = cols_permission.getAttr(colname) or dict()
         updattr = updater.getAttr(colname) or {}
         if currattr or updattr:
             if editMode:
                 currattr['colname'] = colname
             result.setItem(colname, None, _attributes=currattr)
             currattr = result.getAttr(colname)
             for permission in ('forbidden', 'readonly'):
                 last = currattr.get(permission)
                 upd = updattr.get(permission)
                 if editMode:
                     currattr[permission] = upd
                     if last is not None:
                         currattr['%s_inherited' % permission] = last
                 else:
                     currattr[permission] = upd if upd is not None else last
     return result
Example #7
0
class DbStoresHandler(object):
    """Handler for using multi-database"""
        
    def __init__(self, db):
        self.db = db
        if db.application:
            self.config_folder = os.path.join(db.application.instanceFolder, 'dbstores')
        else:
            self.config_folder = None
        self.dbstores = {}
        self.load_config()
        self.create_stores()
        
    def load_config(self):
        """TODO"""
        self.config = Bag()
        if self.config_folder and os.path.isdir(self.config_folder):
            self.config = Bag(self.config_folder)['#0'] or Bag()
            
    def save_config(self):
        """TODO"""
        config = self.config.digest('#a.file_name,#v.#0?#')
        try:
            if os.path.isdir(self.config_folder):
                config_files = os.listdir(self.config_folder)
                for config_file in config_files:
                    filepath = os.path.join(self.config_folder, config_file)
                    if os.path.isfile(filepath):
                        os.remove(filepath)
        except OSError:
            pass
        for name, params in config:
            dbstore_config = Bag()
            dbstore_config.setItem('db', None, **params)
            dbstore_config.toXml(os.path.join(self.config_folder, '%s.xml' % name), autocreate=True)
            
    def create_stores(self, check=False):
        """TODO"""
        for name in self.config.digest('#a.file_name'):
            self.add_store(name, check=check)
            
    def add_store(self, storename, check=False,dbattr=None):
        """TODO
        
        :param storename: TODO
        :param check: TODO"""
        attr = dbattr or self.config.getAttr('%s_xml.db' % storename)
        self.dbstores[storename] = dict(database=attr.get('dbname', storename),
                                        host=attr.get('host', self.db.host), user=attr.get('user', self.db.user),
                                        password=attr.get('password', self.db.password),
                                        port=attr.get('port', self.db.port),
                                        remote_host=attr.get('remote_host'),
                                        remote_port=attr.get('remote_port'))
        if check:
            self.dbstore_align(storename)
            
    def drop_dbstore_config(self, storename):
        """TODO
        
        :param storename: TODO"""
        return self.config.pop('%s_xml' % storename)
    
    def drop_store(self,storename):
        config = self.drop_dbstore_config(storename)
        if not config:
            return
        try:
            self.db.dropDb(config['db?dbname'])
        except Exception:
            print Exception
        self.save_config()
        
        
    def add_dbstore_config(self, storename, dbname=None, host=None, user=None, password=None, port=None, save=True):
        """TODO
        
        :param storename: TODO
        :param dbname: the database name
        :param host: the database server host
        :param user: the username
        :param password: the username's password
        :param port: TODO
        :param save: TODO"""
        self.config.setItem('%s_xml' % storename, None, file_name=storename)
        self.config.setItem('%s_xml.db' % storename, None, dbname=dbname, host=host, user=user, password=password,
                            port=port)
        if save:
            self.save_config()
            self.load_config()
            self.add_store(storename, check=True)
            
    def dbstore_check(self, storename, verbose=False):
        """checks if dbstore exists and if it needs to be aligned
        
        :param storename: TODO
        :param verbose: TODO"""
        with self.db.tempEnv(storename=storename):
            self.db.use_store(storename)
            changes = self.db.model.check()
            if changes and not verbose:
                return False
            elif changes and verbose:
                return changes
            else: #not changes
                return True
                
    def dbstore_align(self, storename, changes=None):
        """TODO
        
        :param storename: TODO
        :param changes: TODO. """
        with self.db.tempEnv(storename=storename):
            changes = changes or self.db.model.check()
            if changes:
                self.db.model.applyModelChanges()
Example #8
0
class DbStoresHandler(object):
    """Handler for using multi-database"""
    def __init__(self, db):
        self.db = db
        self.config_folder = os.path.join(db.application.instanceFolder,
                                          'dbstores')
        self.dbstores = {}
        self.load_config()
        self.create_stores()

    def load_config(self):
        """add???"""
        self.config = Bag()
        if os.path.isdir(self.config_folder):
            self.config = Bag(self.config_folder)['#0'] or Bag()

    def save_config(self):
        """add???"""
        try:
            shutil.rmtree(self.config_folder, True)
        except OSError:
            pass
        for name, params in self.config.digest('#a.file_name,#v.#0?#'):
            dbstore_config = Bag()
            dbstore_config.setItem('db', None, **params)
            dbstore_config.toXml(os.path.join(self.config_folder,
                                              '%s.xml' % name),
                                 autocreate=True)

    def create_stores(self):
        """add???"""
        for name in self.config.digest('#a.file_name'):
            self.add_store(name)

    def add_store(self, storename, check=False):
        """add???
        
        :param storename: add???
        :param check: add???. Default value is ``False``
        """
        attr = self.config.getAttr('%s_xml.db' % storename)
        self.dbstores[storename] = dict(database=attr.get('dbname', storename),
                                        host=attr.get('host', self.db.host),
                                        user=attr.get('user', self.db.user),
                                        password=attr.get(
                                            'password', self.db.password),
                                        port=attr.get('port', self.db.port))
        if check:
            self.dbstore_align(storename)

    def drop_dbstore_config(self, storename):
        """add???
        
        :param storename: add???
        """
        self.config.pop('%s_xml' % storename)

    def add_dbstore_config(self,
                           storename,
                           dbname=None,
                           host=None,
                           user=None,
                           password=None,
                           port=None,
                           save=True):
        """add???
        
        :param storename: add???
        :param dbname: add???. Default value is ``None``
        :param host: add???. Default value is ``None``
        :param user: add???. Default value is ``None``
        :param password: add???. Default value is ``None``
        :param port: add???. Default value is ``None``
        :param save: add???. Default value is ``True``
        """
        self.config.setItem('%s_xml' % storename, None, file_name=storename)
        self.config.setItem('%s_xml.db' % storename,
                            None,
                            dbname=dbname,
                            host=host,
                            user=user,
                            password=password,
                            port=port)
        if save:
            self.save_config()
            self.load_config()
            self.add_store(storename, check=True)

    def dbstore_check(self, storename, verbose=False):
        """checks if dbstore exists and if it needs to be aligned
        
        :param storename: add???
        :param verbose: add???. Default value is ``False``
        """
        with self.db.tempEnv(storename=storename):
            self.db.use_store(storename)
            changes = self.db.model.check()
            if changes and not verbose:
                return False
            elif changes and verbose:
                return changes
            else:  #not changes
                return True

    def dbstore_align(self, storename, changes=None):
        """add???
        
        :param storename: add???
        :param changes: add???. Default value is ``None``
        """
        with self.db.tempEnv(storename=storename):
            self.db.use_store(storename)
            changes = changes or self.db.model.check()
            if changes:
                self.db.model.applyModelChanges()
            self.db.use_store('_main_db')
Example #9
0
class DbStoresHandler(object):
    """Handler for using multi-database"""

    def __init__(self, db):
        self.db = db
        self.config_folder = os.path.join(db.application.instanceFolder, "dbstores")
        self.dbstores = {}
        self.load_config()
        self.create_stores()

    def load_config(self):
        """add???"""
        self.config = Bag()
        if os.path.isdir(self.config_folder):
            self.config = Bag(self.config_folder)["#0"] or Bag()

    def save_config(self):
        """add???"""
        try:
            shutil.rmtree(self.config_folder, True)
        except OSError:
            pass
        for name, params in self.config.digest("#a.file_name,#v.#0?#"):
            dbstore_config = Bag()
            dbstore_config.setItem("db", None, **params)
            dbstore_config.toXml(os.path.join(self.config_folder, "%s.xml" % name), autocreate=True)

    def create_stores(self):
        """add???"""
        for name in self.config.digest("#a.file_name"):
            self.add_store(name)

    def add_store(self, storename, check=False):
        """add???
        
        :param storename: add???
        :param check: add???. Default value is ``False``
        """
        attr = self.config.getAttr("%s_xml.db" % storename)
        self.dbstores[storename] = dict(
            database=attr.get("dbname", storename),
            host=attr.get("host", self.db.host),
            user=attr.get("user", self.db.user),
            password=attr.get("password", self.db.password),
            port=attr.get("port", self.db.port),
        )
        if check:
            self.dbstore_align(storename)

    def drop_dbstore_config(self, storename):
        """add???
        
        :param storename: add???
        """
        self.config.pop("%s_xml" % storename)

    def add_dbstore_config(self, storename, dbname=None, host=None, user=None, password=None, port=None, save=True):
        """add???
        
        :param storename: add???
        :param dbname: add???. Default value is ``None``
        :param host: add???. Default value is ``None``
        :param user: add???. Default value is ``None``
        :param password: add???. Default value is ``None``
        :param port: add???. Default value is ``None``
        :param save: add???. Default value is ``True``
        """
        self.config.setItem("%s_xml" % storename, None, file_name=storename)
        self.config.setItem(
            "%s_xml.db" % storename, None, dbname=dbname, host=host, user=user, password=password, port=port
        )
        if save:
            self.save_config()
            self.load_config()
            self.add_store(storename, check=True)

    def dbstore_check(self, storename, verbose=False):
        """checks if dbstore exists and if it needs to be aligned
        
        :param storename: add???
        :param verbose: add???. Default value is ``False``
        """
        with self.db.tempEnv(storename=storename):
            self.db.use_store(storename)
            changes = self.db.model.check()
            if changes and not verbose:
                return False
            elif changes and verbose:
                return changes
            else:  # not changes
                return True

    def dbstore_align(self, storename, changes=None):
        """add???
        
        :param storename: add???
        :param changes: add???. Default value is ``None``
        """
        with self.db.tempEnv(storename=storename):
            self.db.use_store(storename)
            changes = changes or self.db.model.check()
            if changes:
                self.db.model.applyModelChanges()
            self.db.use_store("_main_db")