Exemplo n.º 1
0
 def login(self, *args):
     fields = EntityDescription.get(GSCharacter._table)
     if not fields:
         return
     data = dict(zip(fields[2], args[0]))
     cid = data['ID']
     if cid in self.__online_dict:
         return
     user = GSCharacter(cid)
     user.load(**data)
     self.__online_dict[cid] = user
Exemplo n.º 2
0
 def syncdb(self, user):
     tables = EntityDescription.keys()
     for table in tables:
         attr = getattr(user, table, None)
         if attr and attr.sync:
             loop.spawn_callback(attr.sync)
Exemplo n.º 3
0
def getEntityClass(table):

    assert table in EntityDescription

    _Class = None

    _desc = EntityDescription.get(table, None)

    if _desc:
        _table_name = table

        _type, _primary_field, _fields, _time_fields, _ = _desc

        if _type == EntityType.SINGLE:
            #_attribs = ['rlock', 'dirty_state'] + list(_fields)
            #_Class   = namedtuple(_table_name.capitalize(), _attribs)
            _Class   = EntityClass()

            _Class.sync   = sync_s
            _Class.load   = load_s
            _Class.value  = value_s
            _Class.init   = partial(entity_init_s, _Class)

            _Class.new    = new
            _Class.update = update
            _Class.delete = delete

            _Class.name   = '{0}Entity'.format(_table_name.capitalize())

        else:
            _attribs           = ['dirty_state'] + list(_fields)
            _Class             = EntityClass() #namedtuple(, 'foreign_field dirty_state')
            _Class.name        = '{0}Manager'.format(_table_name.capitalize())
            _Class.cache       = {}
            _Class.EntityClass = EntityClass()
            _Class.EntityClass.Manager = _Class

            _Class.EntityClass.table_name    =  _table_name
            _Class.EntityClass.primary_field = _primary_field
            _Class.EntityClass.fields        = _fields
            _Class.EntityClass.time_fields   = _time_fields if _time_fields else tuple()

            _Class.EntityClass.sync   = sync_s
            _Class.EntityClass.value  = value_s
            _Class.EntityClass.new    = new
            _Class.EntityClass.update = update
            _Class.EntityClass.delete = delete
            _Class.EntityClass.fields = _fields

            _Class.sync  = sync_m
            _Class.load  = load_m
            _Class.value = value_m
            _Class.init  = partial(entity_init_m, _Class)

            _Class.rlock       = Lock()
            _Class.dirty_state = DirtyState.NONE

        _Class.entity_type   = _type
        _Class.primary_field = _primary_field
        _Class.key           = key
        _Class.fields        = _fields
        _Class.time_fields   = _time_fields if _time_fields else tuple()
        _Class.table_name    = _table_name

        for _related_table, _related_desc in EntityDescription.items():
            if _related_table == table: continue

            _foreign = _related_desc[-1]

            if _foreign:
                _foreign_table, _foreign_field = _foreign
                if _foreign_table == table:
                    _related_class = getEntityClass(_related_table)

                    _related_type = _related_desc[0]
                    if _related_type == EntityType.MULTIPLE:
                        _related_class._replace(foreign_field = _foreign_field)

                    setattr(_Class, _related_table, _related_class)

        if _type == EntityType.MULTIPLE:
            _Class     = _Class(foreign_field = '', dirty_state = DirtyState.NONE)
            _Class.get = partial(get, manager=_Class)

    else:
        raise Exception('No such Table in EntityDescription. table:{0}'.format(table))

    return _Class