<s> from pwman . util . crypto_engine import CryptoEngine <EOL> __DB_FORMAT__ = <NUM_LIT> <EOL> class DatabaseException ( Exception ) : <EOL> pass <EOL> class Database ( object ) : <EOL> def open ( self , dbver = None ) : <EOL> """<STR_LIT>""" <EOL> self . _open ( ) <EOL> enc = CryptoEngine . get ( ) <EOL> key = self . loadkey ( ) <EOL> if key is not None : <EOL> enc . set_cryptedkey ( key ) <EOL> else : <EOL> self . get_user_password ( ) <EOL> def _check_tables ( self ) : <EOL> try : <EOL> self . _cur . execute ( "<STR_LIT>" ) <EOL> version = self . _cur . fetchone ( ) <EOL> return version <EOL> except self . ProgrammingError : <EOL> self . _con . rollback ( ) <EOL> def _create_tables ( self ) : <EOL> if self . _check_tables ( ) : <EOL> return <EOL> try : <EOL> self . _cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:)>" ) <EOL> self . _cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT:)>" ) <EOL> self . _cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _cur . execute ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _cur . execute ( "<STR_LIT>" , <EOL> ( self . dbversion , ) ) <EOL> self . _con . commit ( ) <EOL> except self . ProgrammingError : <EOL> self . _con . rollback ( ) <EOL> def get_user_password ( self ) : <EOL> """<STR_LIT>""" <EOL> enc = CryptoEngine . get ( ) <EOL> newkey = enc . changepassword ( ) <EOL> return self . savekey ( newkey ) <EOL> def _clean_orphans ( self ) : <EOL> clean = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _cur . execute ( clean ) <EOL> self . _con . commit ( ) <EOL> def _get_node_tags ( self , node ) : <EOL> sql = "<STR_LIT>" . format ( self . _sub ) <EOL> self . _cur . execute ( sql , ( str ( node [ <NUM_LIT:0> ] ) , ) ) <EOL> tagids = self . _cur . fetchall ( ) <EOL> if tagids : <EOL> sql = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( '<STR_LIT:U+002C>' . join ( [ self . _sub ] * len ( tagids ) ) ) ) <EOL> tagids = [ str ( id [ <NUM_LIT:0> ] ) for id in tagids ] <EOL> self . _cur . execute ( sql , ( tagids ) ) <EOL> tags = self . _cur . fetchall ( ) <EOL> for t in tags : <EOL> yield t [ <NUM_LIT:0> ] <EOL> def _setnodetags ( self , nodeid , tags ) : <EOL> for tag in tags : <EOL> tid = self . _get_or_create_tag ( tag ) <EOL> self . _update_tag_lookup ( nodeid , tid ) <EOL> def _get_tag ( self , tagcipher ) : <EOL> sql_search = "<STR_LIT>" . format ( self . _sub ) <EOL> self . _cur . execute ( sql_search , ( [ tagcipher ] ) ) <EOL> rv = self . _cur . fetchone ( ) <EOL> return rv <EOL> def _get_or_create_tag ( self , tagcipher ) : <EOL> rv = self . _get_tag ( tagcipher ) <EOL> if rv : <EOL> return rv [ <NUM_LIT:0> ] <EOL> else : <EOL> self . _cur . execute ( self . _insert_tag_sql , ( [ tagcipher ] ) ) <EOL> try : <EOL> return self . _cur . fetchone ( ) [ <NUM_LIT:0> ] <EOL> except TypeError : <EOL> return self . _cur . lastrowid <EOL> def _update_tag_lookup ( self , nodeid , tid ) : <EOL> sql_lookup = "<STR_LIT>" . format ( <EOL> self . _sub , self . _sub ) <EOL> self . _cur . execute ( sql_lookup , ( nodeid , tid ) ) <EOL> self . _con . commit ( ) <EOL> def getnodes ( self , ids ) : <EOL> if ids : <EOL> sql = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( '<STR_LIT:U+002C>' . join ( self . _sub for i in ids ) ) ) <EOL> else : <EOL> sql = "<STR_LIT>" <EOL> self . _cur . execute ( sql , ( ids ) ) <EOL> nodes = self . _cur . fetchall ( ) <EOL> nodes_w_tags = [ ] <EOL> for node in nodes : <EOL> tags = list ( self . _get_node_tags ( node ) ) <EOL> nodes_w_tags . append ( list ( node ) + tags ) <EOL> return nodes_w_tags <EOL> def listnodes ( self , filter = None ) : <EOL> """<STR_LIT>""" <EOL> if not filter : <EOL> sql_all = "<STR_LIT>" <EOL> self . _cur . execute ( sql_all ) <EOL> ids = self . _cur . fetchall ( ) <EOL> return [ id [ <NUM_LIT:0> ] for id in ids ] <EOL> else : <EOL> tagid = self . _get_tag ( filter ) <EOL> if not tagid : <EOL> return [ ] <EOL> self . _cur . execute ( self . _list_nodes_sql , ( tagid ) ) <EOL> self . _con . commit ( ) <EOL> ids = self . _cur . fetchall ( ) <EOL> return [ id [ <NUM_LIT:0> ] for id in ids ] <EOL> def add_node ( self , node ) : <EOL> node_tags = list ( node ) <EOL> node , tags = node_tags [ : <NUM_LIT:4> ] , node_tags [ - <NUM_LIT:1> ] <EOL> self . _cur . execute ( self . _add_node_sql , ( node ) ) <EOL> try : <EOL> nid = self . _cur . fetchone ( ) [ <NUM_LIT:0> ] <EOL> except TypeError : <EOL> nid = self . _cur . lastrowid <EOL> self . _setnodetags ( nid , tags ) <EOL> self . _con . commit ( ) <EOL> def listtags ( self ) : <EOL> self . _clean_orphans ( ) <EOL> get_tags = "<STR_LIT>" <EOL> self . _cur . execute ( get_tags ) <EOL> tags = self . _cur . fetchall ( ) <EOL> if tags : <EOL> return [ t [ <NUM_LIT:0> ] for t in tags ] <EOL> return [ ] <EOL> def editnode ( self , nid , ** kwargs ) : <EOL> tags = kwargs . pop ( '<STR_LIT>' , None ) <EOL> sql = ( "<STR_LIT>" . format ( <EOL> '<STR_LIT:U+002C>' . join ( [ '<STR_LIT>' . format ( k , self . _sub ) for k in list ( kwargs ) ] ) , <EOL> self . _sub ) ) <EOL> self . _cur . execute ( sql , ( list ( kwargs . values ( ) ) + [ nid ] ) ) <EOL> if tags : <EOL> sql_clean = "<STR_LIT>" . format ( self . _sub ) <EOL> self . _cur . execute ( sql_clean , ( str ( nid ) , ) ) <EOL> self . _setnodetags ( nid , tags ) <EOL> self . _con . commit ( ) <EOL> def removenodes ( self , nid ) : <EOL> sql_clean = "<STR_LIT>" . format ( self . _sub ) <EOL> self . _cur . execute ( sql_clean , nid ) <EOL> sql_rm = "<STR_LIT>" . format ( self . _sub ) <EOL> self . _cur . execute ( sql_rm , nid ) <EOL> self . _con . commit ( ) <EOL> self . _con . commit ( ) <EOL> def fetch_crypto_info ( self ) : <EOL> self . _cur . execute ( "<STR_LIT>" ) <EOL> row = self . _cur . fetchone ( ) <EOL> return row <EOL> def save_crypto_info ( self , seed , digest ) : <EOL> """<STR_LIT>""" <EOL> self . _cur . execute ( "<STR_LIT>" ) <EOL> self . _cur . execute ( "<STR_LIT>" . format ( self . _sub , <EOL> self . _sub ) , <EOL> ( seed , digest ) ) <EOL> self . _con . commit ( ) <EOL> def loadkey ( self ) : <EOL> """<STR_LIT>""" <EOL> sql = "<STR_LIT>" <EOL> try : <EOL> self . _cur . execute ( sql ) <EOL> seed , digest = self . _cur . fetchone ( ) <EOL> return seed + u'<STR_LIT>' + digest <EOL> except TypeError : <EOL> return None <EOL> def savekey ( self , key ) : <EOL> salt , digest = key . split ( '<STR_LIT>' ) <EOL> sql = "<STR_LIT>" . format ( self . _sub , <EOL> self . _sub ) <EOL> self . _cur . execute ( "<STR_LIT>" ) <EOL> self . _cur . execute ( sql , ( salt , digest ) ) <EOL> self . _digest = digest . encode ( '<STR_LIT:utf-8>' ) <EOL> self . _salt = salt . encode ( '<STR_LIT:utf-8>' ) <EOL> self . _con . commit ( ) <EOL> def close ( self ) : <EOL> self . _clean_orphans ( ) <EOL> self . _cur . close ( ) <EOL> self . _con . close ( ) </s>
