def contains(self, container_id, object_id, recurse=False): """ Is the provided object immediately contained by the provided container object? Optionally supply recurse=True to check for any containment. """ location_ids = self.connection.runQuery(sql.interp( """SELECT id FROM object WHERE location_id = %s ORDER BY CASE WHEN id = %s THEN 0 ELSE 1 END """, container_id, object_id)) if(location_ids and location_ids[0]['id'] == object_id): return True while(recurse): container_ids = [x['id'] for x in location_ids] if(container_ids): location_ids = self.connection.runQuery(sql.interp( """SELECT id FROM object WHERE location_id IN %s ORDER BY CASE WHEN id = %s THEN 0 ELSE 1 END """, container_ids, object_id)) if(location_ids): if(location_ids[0]['id'] == object_id): return True else: recurse = False return False
def has_parent(self, child_id, object_id): """ Does this child have the provided object as an ancestor? """ parent_ids = [ x['id'] for x in self.connection.runQuery( sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id = %s ORDER BY weight DESC """, child_id)) ] while (parent_ids): if (object_id in parent_ids): return True parent_ids = [ x['id'] for x in self.connection.runQuery( sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id IN %s ORDER BY weight DESC """, parent_ids)) ] return False
def get_parents(self, object_id, recurse=False): """ Return a list of immediate parents for the given object. Optionally, pass recurse=True to fetch complete ancestry. """ #NOTE: the heavier a parent weight is, the more influence its inheritance has. # e.g., if considering inheritance by left-to-right, the leftmost ancestors will # have the heaviest weights. parent_ids = ancestor_ids = self.connection.runQuery(sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id = %s ORDER BY weight DESC """, object_id)) while(recurse): ancestor_ids = self.connection.runQuery(sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id IN %s ORDER BY weight DESC """, [x['id'] for x in ancestor_ids])) if(ancestor_ids): parent_ids.extend(ancestor_ids) else: recurse = False result = self.instantiate('object', *parent_ids) return [result] if isinstance(result, interface.Object) else result
def get_verb(self, origin_id, name, recurse=True): """ Get a verb by this name, recursing by default. """ v = None parents = [origin_id] while(parents): parent_id = parents.pop(0) v = self.connection.runQuery(sql.interp( """SELECT v.* FROM verb v INNER JOIN verb_name vn ON vn.verb_id = v.id WHERE vn.name = %s AND v.origin_id = %s """, name, parent_id)) if(v or not recurse): break else: results = self.connection.runQuery(sql.interp("SELECT parent_id FROM object_relation WHERE child_id = %s", parent_id)) parents.extend([result['parent_id'] for result in results]) if not(v): return None # return self.instantiate('verb', v[0], default_permissions=(name != 'set_default_permissions')) verb_id = v[0]['id'] if('verb-%s' % verb_id in self.cache): return self.cache['verb-%s' % verb_id] v = self._mkverb(v[0]) v.set_id(verb_id) v._source_id = origin_id return v
def has_parent(self, child_id, object_id): """ Does this child have the provided object as an ancestor? """ parent_ids = [ x['id'] for x in self.connection.runQuery(sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id = %s ORDER BY weight DESC """, child_id)) ] while(parent_ids): if(object_id in parent_ids): return True parent_ids = [ x['id'] for x in self.connection.runQuery(sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id IN %s ORDER BY weight DESC """, parent_ids)) ] return False
def contains(self, container_id, object_id, recurse=False): """ Is the provided object immediately contained by the provided container object? Optionally supply recurse=True to check for any containment. """ location_ids = self.connection.runQuery( sql.interp( """SELECT id FROM object WHERE location_id = %s ORDER BY CASE WHEN id = %s THEN 0 ELSE 1 END """, container_id, object_id)) if (location_ids and location_ids[0]['id'] == object_id): return True while (recurse): container_ids = [x['id'] for x in location_ids] if (container_ids): location_ids = self.connection.runQuery( sql.interp( """SELECT id FROM object WHERE location_id IN %s ORDER BY CASE WHEN id = %s THEN 0 ELSE 1 END """, container_ids, object_id)) if (location_ids): if (location_ids[0]['id'] == object_id): return True else: recurse = False return False
def find(self, container_id, name): """ Find an object immediately inside the provided container. """ match_ids = self.connection.runQuery( sql.interp( """SELECT id FROM object WHERE LOWER(name) = LOWER(%s) AND location_id = %s """, name, container_id)) match_ids.extend( self.connection.runQuery( sql.interp( """SELECT o.id FROM property p INNER JOIN object o ON p.origin_id = o.id WHERE p.name = 'name' AND LOWER(p.value) = LOWER(%s) AND o.location_id = %s """, '"%s"' % name, container_id))) match_ids.extend( self.connection.runQuery( sql.interp( """SELECT o.id FROM object o INNER JOIN object_alias oa ON oa.object_id = o.id WHERE LOWER(oa.alias) = LOWER(%s) AND o.location_id = %s """, name, container_id))) return self.instantiate('object', *match_ids)
def get_contents(self, container_id, recurse=False): """ Get the immediate contents of a provided object. Optionally supply recurse=True to fetch all contents. """ nested_location_ids = location_ids = self.connection.runQuery( sql.interp( """SELECT id FROM object WHERE location_id = %s """, container_id)) while (recurse): location_ids = self.connection.runQuery( sql.interp( """SELECT id FROM object WHERE location_id IN %s """, [x['id'] for x in location_ids])) if (location_ids): nested_location_ids.extend(location_ids) else: recurse = False result = self.instantiate('object', *nested_location_ids) return [result] if isinstance(result, interface.Object) else result
def find(self, container_id, name): """ Find an object immediately inside the provided container. """ match_ids = self.connection.runQuery(sql.interp( """SELECT id FROM object WHERE LOWER(name) = LOWER(%s) AND location_id = %s """, name, container_id)) match_ids.extend(self.connection.runQuery(sql.interp( """SELECT o.id FROM property p INNER JOIN object o ON p.origin_id = o.id WHERE p.name = 'name' AND LOWER(p.value) = LOWER(%s) AND o.location_id = %s """, '"%s"' % name, container_id))) match_ids.extend(self.connection.runQuery(sql.interp( """SELECT o.id FROM object o INNER JOIN object_alias oa ON oa.object_id = o.id WHERE LOWER(oa.alias) = LOWER(%s) AND o.location_id = %s """, name, container_id))) return self.instantiate('object', *match_ids)
def get_property(self, origin_id, name, recurse=True): """ Get a property defined on an ancestor of the given origin_id. """ p = None parents = [origin_id] while(parents): parent_id = parents.pop(0) p = self.connection.runQuery(sql.interp( """SELECT p.* FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, parent_id)) if(p or not recurse): break else: results = self.connection.runQuery(sql.interp("SELECT parent_id FROM object_relation WHERE child_id = %s", parent_id)) parents.extend([result['parent_id'] for result in results]) if not(p): return None # return self.instantiate('property', p[0]) property_id = p[0]['id'] if('property-%s' % property_id in self.cache): return self.cache['property-%s' % property_id] p = self._mkproperty(p[0]) p.set_id(property_id) p._source_id = origin_id return p
def get_parents(self, object_id, recurse=False): """ Return a list of immediate parents for the given object. Optionally, pass recurse=True to fetch complete ancestry. """ #NOTE: the heavier a parent weight is, the more influence its inheritance has. # e.g., if considering inheritance by left-to-right, the leftmost ancestors will # have the heaviest weights. parent_ids = ancestor_ids = self.connection.runQuery( sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id = %s ORDER BY weight DESC """, object_id)) while (recurse): ancestor_ids = self.connection.runQuery( sql.interp( """SELECT parent_id AS id FROM object_relation WHERE child_id IN %s ORDER BY weight DESC """, [x['id'] for x in ancestor_ids])) if (ancestor_ids): parent_ids.extend(ancestor_ids) else: recurse = False result = self.instantiate('object', *parent_ids) return [result] if isinstance(result, interface.Object) else result
def test_interp_args_1(self): query = sql.interp("SELECT * FROM some_table WHERE a = %s AND b = %s", 1, 'something') expecting = "SELECT * FROM some_table WHERE a = %s AND b = %s" % ( 1, repr('something')) self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def remove_parent(self, child_id, parent_id): """ Remove the given parent from this child's list of immediate ancestors. """ self.connection.runOperation(sql.interp( "DELETE FROM object_relation WHERE child_id = %s AND parent_id = %s", child_id, parent_id))
def add_parent(self, child_id, parent_id): """ Add the given parent to this child's list of immediate ancestors. """ self.connection.runOperation(sql.interp( "INSERT INTO object_relation (child_id, parent_id, weight) VALUES (%s, %s, 0)", child_id, parent_id))
def is_player(self, object_id): """ Is the given object the avatar for a player? """ result = self.connection.runQuery( sql.interp("SELECT id FROM player WHERE avatar_id = %s", object_id)) return bool(len(result))
def refs(self, key): """ How many objects in the store share the name given? """ result = self.connection.runQuery( sql.interp("SELECT COUNT(*) AS count FROM object WHERE name = %s", key)) return result[0]['count']
def get_verb_names(self, verb_id): """ Get a list of names for the given verb. """ result = self.connection.runQuery( sql.interp("SELECT name FROM verb_name WHERE verb_id = %s", verb_id)) return [x['name'] for x in result]
def get_aliases(self, object_id): """ Return all aliases for the given object ID. """ result = self.connection.runQuery( sql.interp("SELECT alias FROM object_alias WHERE object_id = %s", object_id)) return [x['alias'] for x in result]
def has(self, origin_id, item_type, name, recurse=True, unrestricted=True): """ Does the given origin item have a certain verb or property in its ancestry? """ if (item_type not in ('property', 'verb')): raise ValueError("Invalid item type: %s" % type) a = None parents = [origin_id] while (parents): object_id = parents.pop(0) if (item_type == 'verb'): a = self.connection.runQuery( sql.interp( """SELECT v.id FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE vn.name = %s AND v.origin_id = %s """, name, object_id)) elif (item_type == 'property'): a = self.connection.runQuery( sql.interp( """SELECT p.id FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, object_id)) if (a): if (unrestricted): return True elif (item_type == 'verb'): item = self.instantiate('verb', a[0]) return item.is_executable() elif (item_type == 'property'): item = self.instantiate('property', a[0]) return item.is_readable() elif (recurse): results = self.connection.runQuery( sql.interp( "SELECT parent_id FROM object_relation WHERE child_id = %s", object_id)) parents.extend([result['parent_id'] for result in results]) return False
def add_parent(self, child_id, parent_id): """ Add the given parent to this child's list of immediate ancestors. """ self.connection.runOperation( sql.interp( "INSERT INTO object_relation (child_id, parent_id, weight) VALUES (%s, %s, 0)", child_id, parent_id))
def remove_parent(self, child_id, parent_id): """ Remove the given parent from this child's list of immediate ancestors. """ self.connection.runOperation( sql.interp( "DELETE FROM object_relation WHERE child_id = %s AND parent_id = %s", child_id, parent_id))
def get_verb_list(self, origin_id): """ Get a list of verb id and names dictionaries. """ query = """SELECT v.id, %s(vn.name) AS names FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE v.origin_id = %%s GROUP BY v.id """ if(self.connection.isType('postgresql')): agg_function = "array_agg" verbs = self.connection.runQuery(sql.interp(query % agg_function, origin_id)) return [dict(id=v['id'], names=','.join(v['names'])) for v in verbs] else: agg_function = "group_concat" verbs = self.connection.runQuery(sql.interp(query % agg_function, origin_id)) return [dict(id=v['id'], names=v['names']) for v in verbs]
def is_unique_name(self, key): """ Has the given key been designated as a unique name? """ result = self.connection.runQuery(sql.build_select('object', dict( name = sql.RAW(sql.interp('LOWER(%%s) = LOWER(%s)', key)), unique_name = True ))) return bool(result)
def is_wizard(self, avatar_id): """ Does the given player have wizard rights? """ result = self.connection.runQuery( sql.interp( "SELECT id FROM player WHERE wizard = 't' AND avatar_id = %s", avatar_id)) return bool(len(result))
def is_unique_name(self, key): """ Has the given key been designated as a unique name? """ result = self.connection.runQuery( sql.build_select( 'object', dict(name=sql.RAW(sql.interp('LOWER(%%s) = LOWER(%s)', key)), unique_name=True))) return bool(result)
def get_property_list(self, origin_id): """ Get a list of property id and name dictionaries. """ properties = self.connection.runQuery(sql.interp( """SELECT p.id, p.name FROM property p WHERE p.origin_id = %s """, origin_id)) return [dict(id=p['id'], name=p['name']) for p in properties]
def get_ancestor_with(self, descendent_id, attribute_type, name): """ Return the ancestor object that provides the given attribute. """ if (attribute_type not in ('property', 'verb')): raise ValueError("Invalid attribute type: %s" % type) a = None parents = [descendent_id] while (parents): object_id = parents.pop(0) if (attribute_type == 'verb'): a = self.connection.runQuery( sql.interp( """SELECT v.origin_id AS id FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE vn.name = %s AND v.origin_id = %s """, name, object_id)) elif (attribute_type == 'property'): a = self.connection.runQuery( sql.interp( """SELECT p.origin_id AS id FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, object_id)) if (a): break else: results = self.connection.runQuery( sql.interp( "SELECT parent_id FROM object_relation WHERE child_id = %s", object_id)) parents.extend([result['parent_id'] for result in results]) if not (a): return None return self.instantiate('object', a[0])
def is_connected_player(self, avatar_id): """ Is the given player currently logged on? """ result = self.pool.runQuery(sql.interp( """SELECT 1 AS connected FROM player WHERE COALESCE(last_login, to_timestamp(0)) > COALESCE(last_logout, to_timestamp(0)) AND avatar_id = %s """, avatar_id)) return bool(result)
def get_property_list(self, origin_id): """ Get a list of property id and name dictionaries. """ properties = self.connection.runQuery( sql.interp( """SELECT p.id, p.name FROM property p WHERE p.origin_id = %s """, origin_id)) return [dict(id=p['id'], name=p['name']) for p in properties]
def get_access(self, object_id, type): """ Return the access list for a particular entity. """ return self.connection.runQuery(sql.interp( """SELECT a.*, p.name AS permission_name FROM access a INNER JOIN permission p ON a.permission_id = p.id WHERE %s_id = %%s ORDER BY a.weight """ % type, object_id))
def has(self, origin_id, item_type, name, recurse=True, unrestricted=True): """ Does the given origin item have a certain verb or property in its ancestry? """ if(item_type not in ('property', 'verb')): raise ValueError("Invalid item type: %s" % type) a = None parents = [origin_id] while(parents): object_id = parents.pop(0) if(item_type == 'verb'): a = self.connection.runQuery(sql.interp( """SELECT v.id FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE vn.name = %s AND v.origin_id = %s """, name, object_id)) elif(item_type == 'property'): a = self.connection.runQuery(sql.interp( """SELECT p.id FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, object_id)) if(a): if(unrestricted): return True elif(item_type == 'verb'): item = self.instantiate('verb', a[0]) return item.is_executable() elif(item_type == 'property'): item = self.instantiate('property', a[0]) return item.is_readable() elif(recurse): results = self.connection.runQuery(sql.interp("SELECT parent_id FROM object_relation WHERE child_id = %s", object_id)) parents.extend([result['parent_id'] for result in results]) return False
def is_connected_player(self, avatar_id): """ Is the given player currently logged on? """ result = self.connection.runQuery( sql.interp( """SELECT 1 AS connected FROM player WHERE COALESCE(last_login, to_timestamp(0)) > COALESCE(last_logout, to_timestamp(0)) AND avatar_id = %s """, avatar_id)) return bool(result)
def get_access(self, object_id, type): """ Return the access list for a particular entity. """ return self.connection.runQuery( sql.interp( """SELECT a.*, p.name AS permission_name FROM access a INNER JOIN permission p ON a.permission_id = p.id WHERE %s_id = %%s ORDER BY a.weight """ % type, object_id))
def get_verb_list(self, origin_id): """ Get a list of verb id and names dictionaries. """ verbs = self.pool.runQuery(sql.interp( """SELECT v.id, array_agg(vn.name) AS names FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE v.origin_id = %s GROUP BY v.id """, origin_id)) return [dict(id=v['id'], names=','.join(v['names'])) for v in verbs]
def get_object(self, key, return_list=False): """ Return the object specified by the provided key. If return_list is True, ambiguous object keys will return a list of matching objects. """ if (isinstance(key, str)): key = key.strip() try: key = int(key) except: pass if (key in ('', 'none', 'None', 'null', 'NULL', None)): return None items = None if (isinstance(key, str)): if (key.startswith('#')): end = key.find("(") if (end == -1): end = key.find(" ") if (end == -1): end = len(key) key = int(key[1:end]) else: items = self.connection.runQuery( sql.build_select('object', name=sql.RAW( sql.interp('LOWER(%%s) = LOWER(%s)', key)))) if (len(items) == 0): if (return_list): return [] else: raise errors.NoSuchObjectError(key) elif (len(items) > 1): if (return_list): return self.instantiate('object', *items) else: raise errors.AmbiguousObjectError(key, items) else: return self.instantiate('object', items[0]) if (isinstance(key, int)): if (key == -1): return None return self.load('object', key) else: raise ValueError("Invalid key type: %r" % repr(key))
def get_observing(self, object_id): """ Get the object that the provided object is observing. """ result = self.instantiate('object', *self.connection.runQuery(sql.interp( """SELECT o.* FROM object o INNER JOIN object_observer oo ON oo.object_id = o.id WHERE oo.observer_id = %s """, object_id))) if(isinstance(result, (list, tuple))): return result[0] if result else None return result
def get_observers(self, object_id): """ Get a list of objects currently observing the provided object. """ result = self.instantiate('object', *self.connection.runQuery(sql.interp( """SELECT o.* FROM object o INNER JOIN object_observer oo ON oo.observer_id = o.id WHERE oo.object_id = %s """, object_id))) if not(isinstance(result, (list, tuple))): result = [result] return result
def get_verb_list(self, origin_id): """ Get a list of verb id and names dictionaries. """ verbs = self.connection.runQuery( sql.interp( """SELECT v.id, array_agg(vn.name) AS names FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE v.origin_id = %s GROUP BY v.id """, origin_id)) return [dict(id=v['id'], names=','.join(v['names'])) for v in verbs]
def update_access(self, access_id, rule, access, accessor, permission, weight, subject, deleted): """ Modify an access rule. """ record = {} if not access_id else self.connection.runQuery( sql.interp( """SELECT a.*, p.name AS permission FROM access a INNER JOIN permission p ON a.permission_id = p.id WHERE a.id = %s """, access_id)) if (record): record = record[0] else: record = {} if (deleted): self.connection.runOperation( sql.build_delete('access', id=access_id)) return record['rule'] = rule record['type'] = access record['weight'] = weight record.pop('group', '') if (access == 'group'): record['"group"'] = accessor record['accessor_id'] = None else: record['"group"'] = None record['accessor_id'] = accessor.get_id() if (record.pop('permission', '') != permission): if (permission not in self.permission_list): raise ValueError("Unknown permission: %s" % permission) record['permission_id'] = self.permission_list[permission] if (subject.get_type() == 'object'): record['object_id'] = subject.get_id() elif (subject.get_type() == 'verb'): record['verb_id'] = subject.get_id() elif (subject.get_type() == 'property'): record['property_id'] = subject.get_id() if (access_id): self.connection.runOperation( sql.build_update('access', record, dict(id=access_id))) else: self.connection.runOperation(sql.build_insert('access', **record))
def get_contents(self, container_id, recurse=False): """ Get the immediate contents of a provided object. Optionally supply recurse=True to fetch all contents. """ nested_location_ids = location_ids = self.connection.runQuery(sql.interp( """SELECT id FROM object WHERE location_id = %s """, container_id)) while(recurse): location_ids = self.connection.runQuery(sql.interp( """SELECT id FROM object WHERE location_id IN %s """, [x['id'] for x in location_ids])) if(location_ids): nested_location_ids.extend(location_ids) else: recurse = False result = self.instantiate('object', *nested_location_ids) return [result] if isinstance(result, interface.Object) else result
def get_ancestor_with(self, descendent_id, attribute_type, name): """ Return the ancestor object that provides the given attribute. """ if(attribute_type not in ('property', 'verb')): raise ValueError("Invalid attribute type: %s" % type) a = None parents = [descendent_id] while(parents): object_id = parents.pop(0) if(attribute_type == 'verb'): a = self.connection.runQuery(sql.interp( """SELECT v.origin_id AS id FROM verb v INNER JOIN verb_name vn ON v.id = vn.verb_id WHERE vn.name = %s AND v.origin_id = %s """, name, object_id)) elif(attribute_type == 'property'): a = self.connection.runQuery(sql.interp( """SELECT p.origin_id AS id FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, object_id)) if(a): break else: results = self.connection.runQuery(sql.interp("SELECT parent_id FROM object_relation WHERE child_id = %s", object_id)) parents.extend([result['parent_id'] for result in results]) if not(a): return None return self.instantiate('object', a[0])
def get_verb(self, origin_id, name, recurse=True): """ Get a verb by this name, recursing by default. """ v = None parents = [origin_id] while (parents): parent_id = parents.pop(0) v = self.connection.runQuery( sql.interp( """SELECT v.* FROM verb v INNER JOIN verb_name vn ON vn.verb_id = v.id WHERE vn.name = %s AND v.origin_id = %s """, name, parent_id)) if (v or not recurse): break else: results = self.connection.runQuery( sql.interp( "SELECT parent_id FROM object_relation WHERE child_id = %s", parent_id)) parents.extend([result['parent_id'] for result in results]) if not (v): return None # return self.instantiate('verb', v[0], default_permissions=(name != 'set_default_permissions')) verb_id = v[0]['id'] if ('verb-%s' % verb_id in self.cache): return self.cache['verb-%s' % verb_id] v = self._mkverb(v[0]) v.set_id(verb_id) v._source_id = origin_id return v
def validate_password(self, avatar_id, password): """ Match the given password for the provided avatar. """ saved_crypt = self.connection.runQuery(sql.interp( """SELECT crypt FROM player WHERE avatar_id = %s """, avatar_id)) if not(saved_crypt): return False saved_crypt = saved_crypt[0]['crypt'] return crypt.crypt(password, saved_crypt[0:2]) == saved_crypt
def get_property(self, origin_id, name, recurse=True): """ Get a property defined on an ancestor of the given origin_id. """ p = None parents = [origin_id] while (parents): parent_id = parents.pop(0) p = self.connection.runQuery( sql.interp( """SELECT p.* FROM property p WHERE p.name = %s AND p.origin_id = %s """, name, parent_id)) if (p or not recurse): break else: results = self.connection.runQuery( sql.interp( "SELECT parent_id FROM object_relation WHERE child_id = %s", parent_id)) parents.extend([result['parent_id'] for result in results]) if not (p): return None # return self.instantiate('property', p[0]) property_id = p[0]['id'] if ('property-%s' % property_id in self.cache): return self.cache['property-%s' % property_id] p = self._mkproperty(p[0]) p.set_id(property_id) p._source_id = origin_id return p
def get_object(self, key, return_list=False): """ Return the object specified by the provided key. If return_list is True, ambiguous object keys will return a list of matching objects. """ if(isinstance(key, basestring)): key = key.strip() try: key = int(key) except: pass if(key in ('', 'none', 'None', 'null', 'NULL', None)): return None items = None if(isinstance(key, basestring)): if(key.startswith('#')): end = key.find("(") if(end == -1): end = key.find( " ") if(end == -1): end = len(key) key = int(key[1:end]) else: items = self.pool.runQuery(sql.build_select('object', name=sql.RAW(sql.interp('LOWER(%%s) = LOWER(%s)', key)))) if(len(items) == 0): if(return_list): return [] else: raise errors.NoSuchObjectError(key) elif(len(items) > 1): if(return_list): return self.instantiate('object', *items) else: raise errors.AmbiguousObjectError(key, items) else: return self.instantiate('object', items[0]) if(isinstance(key, int)): if(key == -1): return None return self.load('object', key) else: raise ValueError("Invalid key type: %r" % repr(key))
def get_observers(self, object_id): """ Get a list of objects currently observing the provided object. """ result = self.instantiate( 'object', *self.connection.runQuery( sql.interp( """SELECT o.* FROM object o INNER JOIN object_observer oo ON oo.observer_id = o.id WHERE oo.object_id = %s """, object_id))) if not (isinstance(result, (list, tuple))): result = [result] return result
def validate_password(self, avatar_id, password): """ Match the given password for the provided avatar. """ saved_crypt = self.connection.runQuery( sql.interp( """SELECT crypt FROM player WHERE avatar_id = %s """, avatar_id)) if not (saved_crypt): return False saved_crypt = saved_crypt[0]['crypt'] return crypt.crypt(password, saved_crypt[0:2]) == saved_crypt
def get_observing(self, object_id): """ Get the object that the provided object is observing. """ result = self.instantiate( 'object', *self.connection.runQuery( sql.interp( """SELECT o.* FROM object o INNER JOIN object_observer oo ON oo.object_id = o.id WHERE oo.observer_id = %s """, object_id))) if (isinstance(result, (list, tuple))): return result[0] if result else None return result
def update_access(self, access_id, rule, access, accessor, permission, weight, subject, deleted): """ Modify an access rule. """ record = {} if not access_id else self.connection.runQuery(sql.interp( """SELECT a.*, p.name AS permission FROM access a INNER JOIN permission p ON a.permission_id = p.id WHERE a.id = %s """, access_id)) if(record): record = record[0] else: record = {} if(deleted): self.connection.runOperation(sql.build_delete('access', id=access_id)) return record['rule'] = rule record['type'] = access record['weight'] = weight record.pop('group', '') if(access == 'group'): record['"group"'] = accessor record['accessor_id'] = None else: record['"group"'] = None record['accessor_id'] = accessor.get_id() if(record.pop('permission', '') != permission): if(permission not in self.permission_list): raise ValueError("Unknown permission: %s" % permission) record['permission_id'] = self.permission_list[permission] if(subject.get_type() == 'object'): record['object_id'] = subject.get_id() elif(subject.get_type() == 'verb'): record['verb_id'] = subject.get_id() elif(subject.get_type() == 'property'): record['property_id'] = subject.get_id() if(access_id): self.connection.runOperation(sql.build_update('access', record, dict(id=access_id))) else: self.connection.runOperation(sql.build_insert('access', **record))
def activate_default_grants(self): """ Setup the default grants verb (`set_default_permissions`). """ if(self.default_grants_active): return system = self.instantiate('object', default_permissions=False, id=1) result = self.connection.runQuery(sql.interp( """SELECT v.* FROM verb_name vn INNER JOIN verb v ON v.id = vn.verb_id WHERE vn.name = 'set_default_permissions' AND v.origin_id = %s """, system.get_id())) self.instantiate('verb', default_permissions=False, *result) self.default_grants_active = True
def activate_default_grants(self): """ Setup the default grants verb (`set_default_permissions`). """ if (self.default_grants_active): return system = self.instantiate('object', default_permissions=False, id=1) result = self.connection.runQuery( sql.interp( """SELECT v.* FROM verb_name vn INNER JOIN verb v ON v.id = vn.verb_id WHERE vn.name = 'set_default_permissions' AND v.origin_id = %s """, system.get_id())) self.instantiate('verb', default_permissions=False, *result) self.default_grants_active = True
def is_connected_player(self, avatar_id): """ Is the given player currently logged on? """ if(self.connection.isType('postgresql')): timestamp_function = "to_timestamp(0)" elif(self.connection.isType('sqlite')): timestamp_function = "date(0,'unixepoch')" elif(self.isType('mysql')): timestamp_function = "from_unixtime(0)" else: raise UnsupportedError("Unsupported database type.") result = self.connection.runQuery(sql.interp( """SELECT 1 AS connected FROM player WHERE COALESCE(last_login, %s) > COALESCE(last_logout, %s) AND avatar_id = %%s """ % (timestamp_function, timestamp_function), avatar_id)) return bool(result)
def test_interp_args_list(self): query = sql.interp("SELECT * FROM some_table WHERE a IN %s AND b = %s", [1, 2, 3], 'something') expecting = "SELECT * FROM some_table WHERE a IN (1,2,3) AND b = 'something'" self.assertEqual(query, expecting, 'Got "%s" when expecting "%s"' % (query, expecting))
def is_wizard(self, avatar_id): """ Does the given player have wizard rights? """ result = self.connection.runQuery(sql.interp("SELECT id FROM player WHERE wizard = '1' AND avatar_id = %s", avatar_id)) return bool(len(result))
def is_player(self, object_id): """ Is the given object the avatar for a player? """ result = self.connection.runQuery(sql.interp("SELECT id FROM player WHERE avatar_id = %s", object_id)) return bool(len(result))
def refs(self, key): """ How many objects in the store share the name given? """ result = self.connection.runQuery(sql.interp("SELECT COUNT(*) AS count FROM object WHERE name = %s", key)) return result[0]['count']
def get_verb_names(self, verb_id): """ Get a list of names for the given verb. """ result = self.connection.runQuery(sql.interp("SELECT name FROM verb_name WHERE verb_id = %s", verb_id)) return [x['name'] for x in result]
def test_interp_args_list(self): query = sql.interp("SELECT * FROM some_table WHERE a IN %s AND b = %s", [1,2,3], 'something') expecting = "SELECT * FROM some_table WHERE a IN (1,2,3) AND b = 'something'" self.failUnlessEqual(query, expecting, 'Got "%s" when expecting "%s"' % (sql, expecting))
def get_aliases(self, object_id): """ Return all aliases for the given object ID. """ result = self.connection.runQuery(sql.interp("SELECT alias FROM object_alias WHERE object_id = %s", object_id)) return [x['alias'] for x in result]