def entesten(): c1 = C(store=s, name=u'yes') c2 = C(store=s, name=u'no') A(store=s, reftoc=c1, type=u'testc') A(store=s, reftoc=c2, type=u'testc') A(store=s, reftoc=c1, type=u'testx') yesb = B(store=s, cref=c1, name=u'correct') B(store=s, cref=c2, name=u'not correct') s.checkpoint() q = list( s.query( B, AND(AND(C.name == u'yes', A.type == u'testc'), AND(C.storeID == B.cref, A.reftoc == C.storeID)), )) self.assertEquals(q, [yesb])
def getTotalIndex(self, month, year, day=0): """ Get totals by interface index """ if day > 0: volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, Volume.day == day)) else: volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year)) volumeTotalsByIndex = {} for volume in volumeRecs: index = volume.ifaceIndex if index in volumeTotalsByIndex: if volume.vIn: volumeTotalsByIndex[index][0] += volume.vIn if volume.vOut: volumeTotalsByIndex[index][1] += volume.vOut else: volumeTotalsByIndex[index] = [ volume.vIn or 0, volume.vOut or 0 ] return volumeTotalsByIndex
def testOneCondition(self): self.assertQuery(AND(A.type == u'Narf!'), '((%s = ?))' % (A.type.getColumnName(self.store), ), ['Narf!']) self.assertQuery(OR(A.type == u'Narf!'), '((%s = ?))' % (A.type.getColumnName(self.store), ), ['Narf!']) self.assertEquals(self.query(D, AND(D.one == 'd1.one')), [self.d1]) self.assertEquals(self.query(D, OR(D.one == 'd1.one')), [self.d1])
def testMultipleAndConditions(self): condition = AND(A.type == u'Narf!', A.type == u'Poiuyt!', A.type == u'Try to take over the world') expectedSQL = '((%s = ?) AND (%s = ?) AND (%s = ?))' expectedSQL %= (A.type.getColumnName(self.store), ) * 3 self.assertQuery(condition, expectedSQL, ['Narf!', 'Poiuyt!', 'Try to take over the world']) self.assertEquals( self.query( D, AND(D.one == 'd1.one', D.two == 'd1.two', D.three == 'd1.three')), [self.d1])
def insert(cls, store, searchClass, environment, indexType, result, searchType, searchValue): """ Insert an entry into the search index. @see: L{SearchEntry} """ with METRIC_SEARCH_INSERT_LATENCY.labels(searchClass.value, environment, indexType).time(): searchValue = cls._normalize(searchValue) entry = store.findUnique( SearchEntry, AND(SearchEntry.searchClass == searchClass.value, SearchEntry.environment == environment, SearchEntry.indexType == indexType, SearchEntry.result == result, SearchEntry.searchType == searchType), None) if entry is None: if searchValue != u'': SearchEntry(store=store, searchClass=searchClass.value, environment=environment, indexType=indexType, result=result, searchType=searchType, searchValue=searchValue) else: if searchValue == u'': entry.deleteFromStore() else: entry.searchValue = searchValue
def createFactoid(appStore, creator, key, value): """ Create a new factoid. If a factoid with the same key and value already exists, it is returned instead of creating a duplicate. @type appStore: C{axiom.store.Store} @type creator: C{unicode} @param creator: The name of the creator @type key: C{unicode} @param key: Factoid key @type value: C{unicode} @param value: Factoid value @rtype: L{Factoid} @return: The newly created factoid or the one that matches C{key} and C{value} """ factoid = appStore.findFirst( Factoid, AND(Factoid.key == key, Factoid.value == value)) if factoid is None: factoid = Factoid(store=appStore, creator=creator, editor=creator, key=key, value=value) return factoid
def asAccessibleTo(self, query): """ @param query: An Axiom query describing the Items to retrieve, which this role can access. @type query: an L{iaxiom.IQuery} provider. @return: an iterable which yields the shared proxies that are available to the given role, from the given query. """ # XXX TODO #2371: this method really *should* be returning an L{IQuery} # provider as well, but that is kind of tricky to do. Currently, doing # queries leaks authority, because the resulting objects have stores # and "real" items as part of their interface; having this be a "real" # query provider would obviate the need to escape the L{SharedProxy} # security constraints in order to do any querying. allRoles = list(self.allRoles()) count = 0 unlimited = query.cloneQuery(limit=None) for result in unlimited: allShares = list( query.store.query( Share, AND(Share.sharedItem == result, Share.sharedTo.oneOf(allRoles)))) interfaces = [] for share in allShares: interfaces += share.sharedInterfaces if allShares: count += 1 yield SharedProxy(result, interfaces, allShares[0].shareID) if count == query.limit: return
def recent(self, limit): """ Retrieve a list of recent donations. @param limit: The amount of donations to return. @type limit: L{int} @return: A list of donations. @rtype: L{list} of L{dict}s. """ def _cb(players, donations): donators = [] for donation in donations: player = players[donation.donator.steamID].copy() player['date'] = donation.date.asPOSIXTimestamp() player['amount'] = str(donation.amount) donators.append(player) return donators donations = [] steamids = set() for donation in self.store.query( Donation, AND(Donation.donator == Donator.storeID, Donator.anonymous == False, Donator.steamID != None), limit=limit, sort=Donation.date.descending): steamids.add(donation.donator.steamID) donations.append(donation) d = self.getPlayerSummaries(steamids) d.addCallback(_cb, donations) return d
def __eq__(self, other): if not isinstance(other, (AttributeTuple, tuple, list)): return NotImplemented return AND(*[ myAttr == otherAttr for (myAttr, otherAttr) in zip(self, other)])
def main(s): for ll in s.query(LendingLibrary): print 'found existing library' break else: print 'creating new library' ll = LendingLibrary(store=s) ll.initialize() ll.displayBooks() print '***' ll.shuffleLending() print '---' ll.displayBooks() print '***' ll.shuffleLending() print '---' print s.count( Book, AND(Book.author == u'Stephen King', Book.title == u'The Lions of al-Rassan')) print s.count( Book, OR(Book.author == u'Stephen King', Book.title == u'The Lions of al-Rassan'))
def getPortBreakdownForIp(self, month, year, ip, day=0, index=0): """ Get port breakdown for each IP on date """ a = [] if day > 0: a.append(Volume.day == day) if index: a.append(Volume.ifaceIndex == index) volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, Volume.localIp == ip, *a)) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: if volume.vIn: volumeByPort[volume.port][0] += volume.vIn if volume.vOut: volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn or 0, volume.vOut or 0] return volumeByPort
def getEntries(self, limit=None, discarded=False, deleted=False, sort=None, criteria=None): """ Retrieve all L{Entry}s given certain criteria. @type limit: C{int} or C{None} @param limit: The maximum number of entries to retrieve @type discarded: C{boolean} or C{None} @param discarded: If this value is not C{None}, only items with the specified value will be queried @type deleted: C{boolean} or C{None} @param deleted: If this value is not C{None}, only items with the specified value will be queried @rtype: C{iterable} @return: Entries that matching the specified criteria """ if criteria is None: criteria = [] criteria.append(LinkEntry.channel == self.channel) if discarded is not None: criteria.append(LinkEntry.isDiscarded == discarded) if deleted is not None: criteria.append(LinkEntry.isDeleted == deleted) if sort is None: sort = LinkEntry.modified.descending return self.store.query(LinkEntry, AND(*criteria), limit=limit, sort=sort)
def rowsAfterItem(self, item, count): """ Retrieve some rows after a given item, not including the given item. @param item: then L{Item} to request something after. @type item: this L{InequalityModel}'s L{itemType} attribute. @param count: The maximum number of rows to return. @type count: L{int} @return: A list of row data, ordered by the current sort column, beginning immediately after C{item}. """ currentSortAttribute = self.currentSortColumn.sortAttribute() value = currentSortAttribute.__get__(item, type(item)) firstQuery = self.inequalityQuery( AND(currentSortAttribute == value, self.itemType.storeID > item.storeID), count, True) results = self.constructRows(firstQuery) count -= len(results) if count: secondQuery = self.inequalityQuery(currentSortAttribute > value, count, True) results.extend(self.constructRows(secondQuery)) return results
def powerDown(self, powerup, interface=None): """ Remove a powerup. If no interface is specified, and the type of the object being installed has a "powerupInterfaces" attribute (containing either a sequence of interfaces, or a sequence of (interface, priority) tuples), the target will be powered down with this object on those interfaces. If this object has a "__getPowerupInterfaces__" method, it will be called with an iterable of (interface, priority) tuples. The iterable of (interface, priority) tuples it returns will then be uninstalled. (Note particularly that if powerups are added or removed to the collection described above between calls to powerUp and powerDown, more powerups or less will be removed than were installed.) """ if interface is None: for interface, priority in powerup._getPowerupInterfaces(): self.powerDown(powerup, interface) else: for cable in self.store.query( _PowerupConnector, AND( _PowerupConnector.item == self, _PowerupConnector.interface == unicode( qual(interface)), _PowerupConnector.powerup == powerup)): cable.deleteFromStore() return raise ValueError("Not powered up for %r with %r" % (interface, powerup))
def usernameAvailable(self, username, domain): """ Check to see if a username is available for the user to select. """ if len(username) < 2: return [False, u"Username too short"] for char in u"[ ,:;<>@()!\"'%&\\|\t\b": if char in username: return [ False, u"Username contains invalid character: '%s'" % char ] # The localpart is acceptable if it can be parsed as the local part # of an RFC 2821 address. try: parseAddress("<*****@*****.**>" % (username, )) except ArgumentError: return [False, u"Username fails to parse"] # The domain is acceptable if it is one which we actually host. if domain not in self.getAvailableDomains(): return [False, u"Domain not allowed"] query = self.store.query( userbase.LoginMethod, AND(userbase.LoginMethod.localpart == username, userbase.LoginMethod.domain == domain)) return [not bool(query.count()), u"Username already taken"]
def powerupsFor(self, interface): """ Returns powerups installed using C{powerUp}, in order of descending priority. Powerups found to have been deleted, either during the course of this powerupsFor iteration, during an upgrader, or previously, will not be returned. """ inMemoryPowerup = self._inMemoryPowerups.get(interface, None) if inMemoryPowerup is not None: yield inMemoryPowerup name = unicode(qual(interface), 'ascii') for cable in self.store.query( _PowerupConnector, AND(_PowerupConnector.interface == name, _PowerupConnector.item == self), sort=_PowerupConnector.priority.descending): pup = cable.powerup if pup is None: # this powerup was probably deleted during an upgrader. cable.deleteFromStore() else: indirector = IPowerupIndirector(pup, None) if indirector is not None: yield indirector.indirect(interface) else: yield pup
def getVolumeTotalByIp(self, month, year, day=0, index=0): """ Gets total volumes for each IP address on the set date and index """ a = [] if day > 0: a.append(Volume.day == day) if index: a.append(Volume.ifaceIndex == index) volumeRecs = self.store.query( Volume, AND(Volume.month == month, Volume.year == year, *a)) volumeTotalsByIp = {} for volume in volumeRecs: if volume.localIp in volumeTotalsByIp: if volume.vIn: volumeTotalsByIp[volume.localIp][0] += volume.vIn if volume.vOut: volumeTotalsByIp[volume.localIp][1] += volume.vOut else: volumeTotalsByIp[volume.localIp] = [ volume.vIn or 0, volume.vOut or 0 ] return volumeTotalsByIp
def rowsBeforeItem(self, item, count): """ The inverse of rowsAfterItem. @param item: then L{Item} to request rows before. @type item: this L{InequalityModel}'s L{itemType} attribute. @param count: The maximum number of rows to return. @type count: L{int} @return: A list of row data, ordered by the current sort column, beginning immediately after C{item}. """ currentSortAttribute = self.currentSortColumn.sortAttribute() value = currentSortAttribute.__get__(item, type(item)) firstQuery = self.inequalityQuery( AND(currentSortAttribute == value, self.itemType.storeID < item.storeID), count, False) results = self.constructRows(firstQuery) count -= len(results) if count: secondQuery = self.inequalityQuery(currentSortAttribute < value, count, False) results.extend(self.constructRows(secondQuery)) return results[::-1]
def test_multiplePlaceholderComparisons(self): """ Test that using multiple different placeholders in a comparison at once properly gives each a unique name. """ s = Store() p1 = Placeholder(PlaceholderTestItem) p2 = Placeholder(PlaceholderTestItem) query = ItemQuery( s, PlaceholderTestItem, AND(PlaceholderTestItem.attr == p1.attr, PlaceholderTestItem.other == p1.other, PlaceholderTestItem.attr == p2.attr, PlaceholderTestItem.characters == p2.characters)) sql, args = query._sqlAndArgs('SELECT', '*') self.assertEquals( sql, 'SELECT * ' 'FROM %s, %s AS placeholder_0, %s AS placeholder_1 ' 'WHERE ((%s = placeholder_0.[attr]) AND ' '(%s = placeholder_0.[other]) AND ' '(%s = placeholder_1.[attr]) AND ' '(%s = placeholder_1.[characters]))' % (PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s), PlaceholderTestItem.getTableName(s), PlaceholderTestItem.attr.getColumnName(s), PlaceholderTestItem.other.getColumnName(s), PlaceholderTestItem.attr.getColumnName(s), PlaceholderTestItem.characters.getColumnName(s))) self.assertEquals(args, [])
def _entryBy(self, eid=None, url=None, evenDeleted=False): """ Retrieve an L{LinkEntry} by certain criteria. This is most useful when only specifying either C{eid} or C{url} but not both. @type eid: C{unicode} @param eid: ID (just the numeric part) of the entry to find @type url: C{url} @param url: URL of the entry to find @rtype: L{LinkEntry} or C{None} @return: Entry matching the given criteria or C{None} if there isn't one """ criteria = [LinkEntry.channel == self.channel] if not evenDeleted: criteria.append(LinkEntry.isDeleted == False) if eid is not None: criteria.append(LinkEntry.eid == eid) if url is not None: criteria.append(LinkEntry.url == url) return self.store.findFirst(LinkEntry, AND(*criteria))
def contains(startAttribute, endAttribute, value): """ Return an L{axiom.iaxiom.IComparison} (an object that can be passed as the 'comparison' argument to Store.query/.sum/.count) which will constrain a query against 2 attributes for ranges which contain the given argument. The range is half-open. """ return AND(startAttribute <= value, value < endAttribute)
def getComments(self, initial=None): criteria = [LinkEntryComment.parent == self] if initial is not None: criteria.append(LinkEntryComment.initial == initial) return iter(self.store.query(LinkEntryComment, AND(*criteria), sort=LinkEntryComment.created.ascending))
def powerupsFor(self, interface, tables=(), comparison=None): """ Returns powerups installed using C{powerUp}, in order of descending priority. Powerups found to have been deleted, either during the course of this powerupsFor iteration, during an upgrader, or previously, will not be returned. """ inMemoryPowerup = self._inMemoryPowerups.get(interface, []) for pup in inMemoryPowerup: yield pup if self.store is None: return name = qual(interface) tables = (_PowerupConnector, ) + tables if comparison: comparison = AND(_PowerupConnector.interface == name, _PowerupConnector.item == self, _PowerupConnector.powerup == tables[1].storeID, comparison) else: comparison = AND( _PowerupConnector.interface == name, _PowerupConnector.item == self, ) q = self.store.query( tables, comparison, sort=_PowerupConnector.priority.descending).distinct() for entries in q: cable = entries[0] pup = cable.powerup if pup is None: # this powerup was probably deleted during an upgrader. cable.deleteFromStore() else: indirector = IPowerupIndirector(pup, None) if indirector is not None: yield indirector.indirect(interface) else: yield pup
def postOptions(self): s = self.parent.parent.getStore() ss = portal.IRealm(s).accountByAddress(u'mantissa', None).avatars.open() for obs in ss.query( stats.RemoteStatsObserver, AND(stats.RemoteStatsObserver.hostname == self['host'], stats.RemoteStatsObserver.port == int(self['port']))): obs.deleteFromStore()
def getPortTotals(self, month, year, day=0): if day > 0: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.day==day) ) else: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year) ) volumeByPort = {} for volume in volumeRecs: if volume.port in volumeByPort: volumeByPort[volume.port][0] += volume.vIn volumeByPort[volume.port][1] += volume.vOut else: volumeByPort[volume.port] = [volume.vIn, volume.vOut] return volumeByPort
def getSubscription(self, id, subscriber): """ Get the L{SubscribedFeed} for C{id} and C{subscriber}. """ return self.store.findUnique( SubscribedFeed, AND(SubscribedFeed.id == id, SubscribedFeed.subscriber == subscriber), default=None)
def getVolumeTotalByIp(self, month, year, day=0): if day > 0: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year, Volume.day==day) ) else: volumeRecs = self.store.query(Volume, AND(Volume.month==month, Volume.year==year) ) volumeTotalsByIp = {} for volume in volumeRecs: if volume.localIp in volumeTotalsByIp: volumeTotalsByIp[volume.localIp][0] += volume.vIn volumeTotalsByIp[volume.localIp][1] += volume.vOut else: volumeTotalsByIp[volume.localIp] = [volume.vIn, volume.vOut] return volumeTotalsByIp
def __delitem__(self, index): assert not isinstance(index, slice), 'slices are not supported (yet)' self._getListItem(index).deleteFromStore() if index < self.length - 1: for item in self.store.query( _ListItem, AND(_ListItem._container == self, _ListItem._index > index)): item._index -= 1 self.length -= 1
def allBounced(self): """ Did all of the addresses bounce? @return: L{True} if all addresses bounced. L{False} otherwise. """ return self.store.findFirst( DeliveryToAddress, AND(DeliveryToAddress.delivery == self, DeliveryToAddress.status.oneOf([UNSENT, SENT]))) is None
def postOptions(self): store = self.getStore() fact = store.findUnique(IRCBotFactoryFactory) svc = store.findUnique( IRCBotService, AND(IRCBotService.serviceID == self['id'], IRCBotService.factory == fact)) uninstallFrom(svc, store)