<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import base64 <EOL> import logging <EOL> import sys <EOL> from collections import defaultdict <EOL> import vobject <EOL> def list_clean ( string ) : <EOL> """<STR_LIT>""" <EOL> string = string . split ( '<STR_LIT:U+002C>' ) <EOL> rstring = list ( ) <EOL> for element in string : <EOL> rstring . append ( element . strip ( '<STR_LIT:U+0020>' ) ) <EOL> return rstring <EOL> NO_STRINGS = [ u"<STR_LIT:n>" , "<STR_LIT:n>" , u"<STR_LIT>" , "<STR_LIT>" ] <EOL> YES_STRINGS = [ u"<STR_LIT:y>" , "<STR_LIT:y>" , u"<STR_LIT:yes>" , "<STR_LIT:yes>" ] <EOL> PROPERTIES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> PROPS_ALL = [ '<STR_LIT>' , '<STR_LIT:N>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> PROPS_ALLOWED = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> PROPS_ONCE = [ '<STR_LIT>' , '<STR_LIT:N>' , '<STR_LIT>' ] <EOL> PROPS_LIST = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> PROPS_BIN = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> RTEXT = '<STR_LIT>' <EOL> NTEXT = '<STR_LIT>' <EOL> BTEXT = '<STR_LIT>' <EOL> def get_names ( display_name ) : <EOL> first_name , last_name = '<STR_LIT>' , display_name <EOL> if display_name . find ( '<STR_LIT:U+002C>' ) > <NUM_LIT:0> : <EOL> last_name , first_name = display_name . split ( '<STR_LIT:U+002C>' ) <EOL> elif display_name . find ( '<STR_LIT:U+0020>' ) : <EOL> name_list = display_name . split ( '<STR_LIT:U+0020>' ) <EOL> last_name = '<STR_LIT>' . join ( name_list [ - <NUM_LIT:1> ] ) <EOL> first_name = '<STR_LIT:U+0020>' . join ( name_list [ : - <NUM_LIT:1> ] ) <EOL> return first_name . strip ( ) . capitalize ( ) , last_name . strip ( ) . capitalize ( ) <EOL> def fix_vobject ( vcard ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in vcard . contents : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> fname = vcard . contents [ '<STR_LIT:n>' ] [ <NUM_LIT:0> ] . valueRepr ( ) <EOL> fname = fname . strip ( ) <EOL> vcard . add ( '<STR_LIT>' ) <EOL> vcard . fn . value = fname <EOL> return vcard <EOL> def vcard_from_vobject ( vcard ) : <EOL> vcard = fix_vobject ( vcard ) <EOL> vdict = VCard ( ) <EOL> if vcard . name != "<STR_LIT>" : <EOL> raise Exception <EOL> for line in vcard . getChildren ( ) : <EOL> line . transformFromNative ( ) <EOL> property_name = line . name <EOL> property_value = line . value <EOL> try : <EOL> if line . ENCODING_paramlist == [ u'<STR_LIT:b>' ] or line . ENCODING_paramlist == [ u'<STR_LIT:B>' ] : <EOL> property_value = base64 . b64encode ( line . value ) <EOL> except AttributeError : <EOL> pass <EOL> if type ( property_value ) == list : <EOL> property_value = ( '<STR_LIT:U+002C>' ) . join ( property_value ) <EOL> vdict [ property_name ] . append ( ( property_value , line . params , ) ) <EOL> return vdict <EOL> def vcard_from_string ( vcard_string ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vcard = vobject . readOne ( vcard_string ) <EOL> except vobject . base . ParseError as error : <EOL> raise Exception ( error ) <EOL> return vcard_from_vobject ( vcard ) <EOL> def vcard_from_email ( display_name , email ) : <EOL> fname , lname = get_names ( display_name ) <EOL> vcard = vobject . vCard ( ) <EOL> vcard . add ( '<STR_LIT:n>' ) <EOL> vcard . n . value = vobject . vcard . Name ( family = lname , given = fname ) <EOL> vcard . add ( '<STR_LIT>' ) <EOL> vcard . fn . value = display_name <EOL> vcard . add ( '<STR_LIT:email>' ) <EOL> vcard . email . value = email <EOL> vcard . email . type_param = '<STR_LIT>' <EOL> return vcard_from_vobject ( vcard ) <EOL> def cards_from_file ( cards_f ) : <EOL> collector = list ( ) <EOL> for vcard in vobject . readComponents ( cards_f ) : <EOL> collector . append ( vcard_from_vobject ( vcard ) ) <EOL> return collector <EOL> class VCard ( defaultdict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ddict = '<STR_LIT>' ) : <EOL> if ddict == '<STR_LIT>' : <EOL> defaultdict . __init__ ( self , list ) <EOL> else : <EOL> defaultdict . __init__ ( self , list , ddict ) <EOL> self . href = '<STR_LIT>' <EOL> self . account = '<STR_LIT>' <EOL> self . etag = '<STR_LIT>' <EOL> self . edited = <NUM_LIT:0> <EOL> def serialize ( self ) : <EOL> return self . items ( ) . __repr__ ( ) <EOL> @ property <EOL> def name ( self ) : <EOL> return unicode ( self [ '<STR_LIT:N>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) if self [ '<STR_LIT:N>' ] else '<STR_LIT>' <EOL> @ name . setter <EOL> def name ( self , value ) : <EOL> if not self [ '<STR_LIT:N>' ] : <EOL> self [ '<STR_LIT:N>' ] = [ ( '<STR_LIT>' , { } ) ] <EOL> self [ '<STR_LIT:N>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] = value <EOL> @ property <EOL> def fname ( self ) : <EOL> return unicode ( self [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) if self [ '<STR_LIT>' ] else '<STR_LIT>' <EOL> @ fname . setter <EOL> def fname ( self , value ) : <EOL> self [ '<STR_LIT>' ] [ <NUM_LIT:0> ] = ( value , { } ) <EOL> def alt_keys ( self ) : <EOL> keylist = self . keys ( ) <EOL> for one in [ x for x in [ '<STR_LIT>' , '<STR_LIT:N>' , '<STR_LIT>' ] if x in keylist ] : <EOL> keylist . remove ( one ) <EOL> keylist . sort ( ) <EOL> return keylist <EOL> def print_email ( self ) : <EOL> """<STR_LIT>""" <EOL> collector = list ( ) <EOL> try : <EOL> for one in self [ '<STR_LIT>' ] : <EOL> try : <EOL> typelist = '<STR_LIT:U+002C>' . join ( one [ <NUM_LIT:1> ] [ u'<STR_LIT>' ] ) <EOL> except KeyError : <EOL> typelist = '<STR_LIT>' <EOL> collector . append ( one [ <NUM_LIT:0> ] + "<STR_LIT:\t>" + self . fname + "<STR_LIT:\t>" + typelist ) <EOL> return '<STR_LIT:\n>' . join ( collector ) <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> def print_tel ( self ) : <EOL> """<STR_LIT>""" <EOL> collector = list ( ) <EOL> try : <EOL> for one in self [ '<STR_LIT>' ] : <EOL> try : <EOL> typelist = '<STR_LIT:U+002C>' . join ( one [ <NUM_LIT:1> ] [ u'<STR_LIT>' ] ) <EOL> except KeyError : <EOL> typelist = '<STR_LIT>' <EOL> collector . append ( self . fname + "<STR_LIT:\t>" + one [ <NUM_LIT:0> ] + "<STR_LIT:\t>" + typelist ) <EOL> return '<STR_LIT:\n>' . join ( collector ) <EOL> except KeyError : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def pretty ( self ) : <EOL> return self . _pretty_base ( self . alt_keys ( ) ) <EOL> @ property <EOL> def pretty_min ( self ) : <EOL> return self . _pretty_base ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def _pretty_base ( self , keylist ) : <EOL> collector = list ( ) <EOL> if sys . stdout . isatty ( ) : <EOL> collector . append ( '<STR_LIT:\n>' + BTEXT + '<STR_LIT>' + self . fname + NTEXT ) <EOL> else : <EOL> collector . append ( '<STR_LIT:\n>' + '<STR_LIT>' + self . fname ) <EOL> for key in keylist : <EOL> for value in self [ key ] : <EOL> try : <EOL> types = '<STR_LIT>' + '<STR_LIT:U+002CU+0020>' . join ( value [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) + '<STR_LIT:)>' <EOL> except KeyError : <EOL> types = '<STR_LIT>' <EOL> line = key + types + '<STR_LIT>' + value [ <NUM_LIT:0> ] <EOL> collector . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( collector ) <EOL> def _line_helper ( self , line ) : <EOL> collector = list ( ) <EOL> for key in line [ <NUM_LIT:1> ] . keys ( ) : <EOL> collector . append ( key + '<STR_LIT:=>' + '<STR_LIT:U+002C>' . join ( line [ <NUM_LIT:1> ] [ key ] ) ) <EOL> if collector == list ( ) : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return ( '<STR_LIT:;>' + '<STR_LIT:;>' . join ( collector ) ) <EOL> @ property <EOL> def vcf ( self ) : <EOL> """<STR_LIT>""" <EOL> import string <EOL> import random <EOL> def generate_random_uid ( ) : <EOL> """<STR_LIT>""" <EOL> choice = string . ascii_uppercase + string . digits <EOL> return '<STR_LIT>' . join ( [ random . choice ( choice ) for _ in range ( <NUM_LIT> ) ] ) <EOL> if '<STR_LIT>' not in self . keys ( ) : <EOL> self [ '<STR_LIT>' ] = [ ( generate_random_uid ( ) , dict ( ) ) ] <EOL> collector = list ( ) <EOL> collector . append ( '<STR_LIT>' ) <EOL> collector . append ( '<STR_LIT>' ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT:N>' ] : <EOL> try : <EOL> collector . append ( key + '<STR_LIT::>' + self [ key ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> except IndexError : <EOL> collector . append ( key + '<STR_LIT::>' ) <EOL> for prop in self . alt_keys ( ) : <EOL> for line in self [ prop ] : <EOL> types = self . _line_helper ( line ) <EOL> collector . append ( prop + types + '<STR_LIT::>' + line [ <NUM_LIT:0> ] ) <EOL> collector . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( collector ) </s>
