from koe.common import pluginlib from koe.interfaces.stubs import BitBucketAPI def main(): kos, ns = pluginlib.register('bitbucket') sid = kos.bind_service('bitbucket', BitBucket(102400), 'BitBucketAPI.BitBucket') try: print "bitbucket plugin registered" kos.run() finally: print "bitbucket plugin exiting" kos.unbind_service(sid) class BitBucket: """Stores an arbitrary name strings up a specified total size. You can Set, Get, and Delete strings by name. If the total size of all strings exceeds the maximum size (passed as argument to constructor) then the Set operation will raise BucketFull. You can see what is in the BitBucket with List.""" def __init__(self, size): self.max_size = size self.cur_size = 0 self.entries = {} def Set(self, name, value): size = len(value) if (size + self.cur_size) > self.max_size: raise BitBucketAPI.BucketFull, "%d bytes available" % \ (self.max_size - self.cur_size) self.entries[name] = value self.cur_size = self.cur_size + size def Get(self, name): try: return self.entries[name] except KeyError: raise BitBucketAPI.NotFound, name def List(self): return self.entries.keys() def Delete(self, name): try: self.cur_size = self.cur_size - len(self.entries[name]) del self.entries[name] except KeyError: raise BitBucketAPI.NotFound, name if __name__ == "__main__": main()