<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> try : <EOL> from pycarddav import model <EOL> import xdg . BaseDirectory <EOL> import sys <EOL> import sqlite3 <EOL> import logging <EOL> from os import path <EOL> except ImportError , error : <EOL> print ( error ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> OK = <NUM_LIT:0> <EOL> NEW = <NUM_LIT:1> <EOL> CHANGED = <NUM_LIT:2> <EOL> DELETED = <NUM_LIT:9> <EOL> class SQLiteDb ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> db_path = None , <EOL> encoding = "<STR_LIT:utf-8>" , <EOL> errors = "<STR_LIT:strict>" , <EOL> debug = False ) : <EOL> if db_path is None : <EOL> db_path = xdg . BaseDirectory . save_data_path ( '<STR_LIT>' ) + '<STR_LIT>' <EOL> self . db_path = path . expanduser ( db_path ) <EOL> self . conn = sqlite3 . connect ( self . db_path ) <EOL> self . cursor = self . conn . cursor ( ) <EOL> self . encoding = encoding <EOL> self . errors = errors <EOL> self . debug = debug <EOL> self . display_all = False <EOL> self . print_function = "<STR_LIT>" <EOL> self . _create_default_tables ( ) <EOL> self . _check_table_version ( ) <EOL> def __del__ ( self ) : <EOL> self . conn . close ( ) <EOL> def search ( self , search_string , accounts , where = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if where not in ( '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> search_str = '<STR_LIT:%>' + search_string + '<STR_LIT:%>' <EOL> sql_fmt = '<STR_LIT>' <EOL> if where == '<STR_LIT>' : <EOL> sql_fmt += '<STR_LIT>' <EOL> sql_args = ( search_str , search_str ) <EOL> else : <EOL> sql_fmt += where + '<STR_LIT>' <EOL> sql_args = ( search_str , ) <EOL> result = list ( ) <EOL> for account in accounts : <EOL> rows = self . sql_ex ( sql_fmt . format ( account + '<STR_LIT>' ) , sql_args ) <EOL> result . extend ( ( self . get_vcard_from_data ( account , * r ) for r in rows ) ) <EOL> return result <EOL> def _dump ( self , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s ) <EOL> return result <EOL> def _check_table_version ( self ) : <EOL> """<STR_LIT>""" <EOL> database_version = <NUM_LIT:11> <EOL> self . cursor . execute ( '<STR_LIT>' ) <EOL> result = self . cursor . fetchone ( ) <EOL> if result is None : <EOL> stuple = ( database_version , ) <EOL> self . cursor . execute ( '<STR_LIT>' , <EOL> stuple ) <EOL> self . conn . commit ( ) <EOL> elif not result [ <NUM_LIT:0> ] == database_version : <EOL> raise Exception ( str ( self . db_path ) + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def _create_default_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . cursor . execute ( '''<STR_LIT>''' ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> except Exception as error : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' + str ( error ) + "<STR_LIT:\n>" ) <EOL> self . conn . commit ( ) <EOL> try : <EOL> self . cursor . execute ( '''<STR_LIT>''' ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> except Exception as error : <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' + str ( error ) + "<STR_LIT:\n>" ) <EOL> self . conn . commit ( ) <EOL> self . _check_table_version ( ) <EOL> def sql_ex ( self , statement , stuple = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . cursor . execute ( statement , stuple ) <EOL> result = self . cursor . fetchall ( ) <EOL> self . conn . commit ( ) <EOL> return result <EOL> def check_account_table ( self , account_name , resource ) : <EOL> count_sql_s = """<STR_LIT>""" <EOL> self . cursor . execute ( count_sql_s , ( account_name , resource ) ) <EOL> result = self . cursor . fetchone ( ) <EOL> if ( result [ <NUM_LIT:0> ] != <NUM_LIT:0> ) : <EOL> return <EOL> sql_s = """<STR_LIT>""" . format ( account_name + '<STR_LIT>' ) <EOL> self . sql_ex ( sql_s ) <EOL> sql_s = '<STR_LIT>' <EOL> self . sql_ex ( sql_s , ( account_name + '<STR_LIT>' , resource ) ) <EOL> logging . debug ( "<STR_LIT>" . format ( account_name ) ) <EOL> def needs_update ( self , href , account_name , etag = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> stuple = ( href , ) <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s , stuple ) <EOL> if len ( result ) is <NUM_LIT:0> : <EOL> return True <EOL> elif etag != result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : <EOL> return True <EOL> else : <EOL> return False <EOL> def update ( self , vcard , account_name , href = '<STR_LIT>' , etag = '<STR_LIT>' , status = OK ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( vcard , ( str , unicode ) ) : <EOL> try : <EOL> vcard_s = vcard . decode ( '<STR_LIT:utf-8>' ) <EOL> except UnicodeEncodeError : <EOL> vcard_s = vcard <EOL> try : <EOL> vcard = model . vcard_from_string ( vcard ) <EOL> except Exception as error : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( href ) ) <EOL> logging . debug ( error ) <EOL> logging . info ( vcard ) <EOL> return <EOL> else : <EOL> vcard_s = vcard . vcf <EOL> if href == '<STR_LIT>' : <EOL> href = get_random_href ( ) <EOL> stuple = ( etag , vcard . name , vcard . fname , vcard_s , status , href , href ) <EOL> sql_s = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) ) <EOL> self . sql_ex ( sql_s , stuple ) <EOL> def update_href ( self , old_href , new_href , account_name , etag = '<STR_LIT>' , status = OK ) : <EOL> """<STR_LIT>""" <EOL> stuple = ( new_href , etag , status , old_href ) <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> self . sql_ex ( sql_s , stuple ) <EOL> def href_exists ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> if len ( self . sql_ex ( sql_s , ( href , ) ) ) == <NUM_LIT:0> : <EOL> return False <EOL> else : <EOL> return True <EOL> def get_etag ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> etag = self . sql_ex ( sql_s , ( href , ) ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> return etag <EOL> def delete_vcard_from_db ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> stuple = ( href , ) <EOL> logging . debug ( "<STR_LIT>" + str ( href ) ) <EOL> self . sql_ex ( '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) , <EOL> stuple ) <EOL> def get_all_href_from_db ( self , accounts ) : <EOL> """<STR_LIT>""" <EOL> result = list ( ) <EOL> for account in accounts : <EOL> rows = self . sql_ex ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( account + '<STR_LIT>' ) ) <EOL> result . extend ( ( self . get_vcard_from_data ( account , * r ) for r in rows ) ) <EOL> return result <EOL> def get_all_href_from_db_not_new ( self , accounts ) : <EOL> """<STR_LIT>""" <EOL> result = list ( ) <EOL> for account in accounts : <EOL> sql_s = '<STR_LIT>' . format ( account + '<STR_LIT>' ) <EOL> stuple = ( NEW , ) <EOL> hrefs = self . sql_ex ( sql_s , stuple ) <EOL> result = result + [ ( href [ <NUM_LIT:0> ] , account ) for href in hrefs ] <EOL> return result <EOL> def get_vcard_from_data ( self , account_name , href , vcard , etag ) : <EOL> """<STR_LIT>""" <EOL> vcard = model . vcard_from_string ( vcard ) <EOL> vcard . href = href <EOL> vcard . account = account_name <EOL> vcard . etag = etag <EOL> return vcard <EOL> def get_vcard_from_db ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s , ( href , ) ) <EOL> return self . get_vcard_from_data ( account_name , href , * result [ <NUM_LIT:0> ] ) <EOL> def get_changed ( self , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s , ( CHANGED , ) ) <EOL> return [ row [ <NUM_LIT:0> ] for row in result ] <EOL> def get_new ( self , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s , ( NEW , ) ) <EOL> return [ row [ <NUM_LIT:0> ] for row in result ] <EOL> def get_marked_delete ( self , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> result = self . sql_ex ( sql_s , ( DELETED , ) ) <EOL> return result <EOL> def mark_delete ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> self . sql_ex ( sql_s , ( DELETED , href , ) ) <EOL> def reset_flag ( self , href , account_name ) : <EOL> """<STR_LIT>""" <EOL> sql_s = '<STR_LIT>' . format ( account_name + '<STR_LIT>' ) <EOL> self . sql_ex ( sql_s , ( OK , href , ) ) <EOL> def get_random_href ( ) : <EOL> """<STR_LIT>""" <EOL> import random <EOL> tmp_list = list ( ) <EOL> for _ in xrange ( <NUM_LIT:3> ) : <EOL> rand_number = random . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> tmp_list . append ( "<STR_LIT>" . format ( rand_number ) ) <EOL> return "<STR_LIT:->" . join ( tmp_list ) . upper ( ) </s>
