<s> from urlparse import urlparse <EOL> from . backends import Backend , MemoryBackend , RedisBackend <EOL> from . query import Query <EOL> class Taxon ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , backend ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( backend , Backend ) : <EOL> raise ValueError ( "<STR_LIT>" % backend ) <EOL> self . _backend = backend <EOL> @ property <EOL> def backend ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _backend <EOL> def tag ( self , tag , * items ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . tag_items ( tag , * items ) <EOL> def untag ( self , tag , * items ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . untag_items ( tag , * items ) <EOL> def remove ( self , * items ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . remove_items ( * items ) <EOL> def tags ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . all_tags ( ) <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . all_items ( ) <EOL> def query ( self , q ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( q , ( tuple , Query ) ) : <EOL> raise ValueError ( "<STR_LIT>" % q ) <EOL> return self . backend . query ( q ) <EOL> def find ( self , q ) : <EOL> """<STR_LIT>""" <EOL> _ , items = self . query ( q ) <EOL> return set ( items ) <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . backend . empty ( ) <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ , self . backend ) <EOL> class MemoryTaxon ( Taxon ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( MemoryTaxon , self ) . __init__ ( MemoryBackend ( ) ) <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ ) <EOL> class RedisTaxon ( Taxon ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dsn = '<STR_LIT>' , name = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _dsn = dsn <EOL> r = self . _redis_from_dsn ( self . _dsn ) <EOL> super ( RedisTaxon , self ) . __init__ ( RedisBackend ( r , name ) ) <EOL> def _redis_from_dsn ( self , dsn ) : <EOL> """<STR_LIT>""" <EOL> import redis <EOL> parts = urlparse ( dsn ) <EOL> _ , _ , netloc = parts . netloc . partition ( '<STR_LIT:@>' ) <EOL> netloc = netloc . rsplit ( '<STR_LIT::>' ) <EOL> host = netloc [ <NUM_LIT:0> ] <EOL> try : <EOL> port = int ( netloc [ <NUM_LIT:1> ] ) <EOL> except IndexError : <EOL> port = <NUM_LIT> <EOL> try : <EOL> db = int ( parts . path . strip ( '<STR_LIT:/>' ) ) <EOL> except ValueError : <EOL> db = <NUM_LIT:0> <EOL> return redis . Redis ( host = host , port = port , db = db ) <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ , self . _dsn , self . backend . name ) </s>
