businesscards/htdocs/include/bc.py

437 lines
12 KiB
Python
Raw Normal View History

2014-04-18 23:34:12 +04:00
#!/usr/bin/env python
class table:
def __init__(self, db, name, columns):
self.db = db # database connection
self.name = name # table name
self.columns = columns
self.dbc = self.db.cursor() # cursor object
self.debug = 0
self.order = ''
self.qfilter = ''
self.selectopts = ''
# def _sqlvalue(self, value):
# if value == None:
# return 'NULL'
# else:
# return value
def _getorder(self):
if len(self.order) > 0:
orderstr = 'ORDER BY `%s`' % self.order
else:
orderstr = ''
return orderstr
# def _enumquoted(self, list):
# result = ''
# for i in xrange(len(list)-1):
# result = '%s\'%s\', ' % (result, self._sqlvalue(list[i]))
# result = '%s%s' % (result, self._sqlvalue(list[i+1]))
# return result
# def _gensubstitute(self, list):
# result = ''
# for i in xrange(len(list)-1):
# result += '%r, '
# result = result + '%r'
# return result
def _listtotuple(self, list):
result = ()
for i in xrange(len(list)):
result += (list[i],)
return result
def _enumquotedreverse(self, list):
result = ''
if (len(list)) > 1:
for i in xrange(len(list)-1):
if list[i] == None:
result = result + 'NULL' + ', '
else:
result = result + '`%s`, ' % list[i]
result = result + '`' + list[i+1] + '`'
else:
result = list[0]
return result
def _replacelist(self, list):
result = ''
for i in xrange(len(list)-1):
result += '%s, '
result +='%s'
return result
def __getitem__(self, item):
self._query("SELECT %s %s FROM %s %s %s LIMIT %s, 1" % (self.selectopts, self._enumquotedreverse(self.columns), self.name, self.qfilter, self._getorder(), item))
return self.dbc.fetchone()
def _query(self, q, values = ()):
if self.debug == 1:
print "Query: %s" % (q)
self.dbc.execute(q, values)
def __iter__(self):
"creates a data set, and returns an iterator (self)"
q = "SELECT %s %s FROM %s %s %s" % (self.selectopts, self._enumquotedreverse(self.columns), self.name, self.qfilter, self._getorder())
self._query(q)
return self
def __len__(self):
self.__iter__()
return int(self.dbc.rowcount)
def setfilter(self, filterstr):
self.qfilter = filterstr
q = "SELECT %s %s FROM %s %s %s" % (self.selectopts, self._enumquotedreverse(self.columns), self.name, self.qfilter, self._getorder())
self._query(q)
def setorder(self, orderstr):
self.order = orderstr
q = "SELECT %s %s FROM %s %s %s" % (self.selectopts, self._enumquotedreverse(self.columns), self.name, self.qfilter, self._getorder())
self._query(q)
def next(self):
"returns the next item in the data set, or tells Python to stop"
r = self.dbc.fetchone()
if not r:
raise StopIteration
return r
def append(self, values):
q = 'INSERT INTO %s (%s) VALUES (%s)' % (self.name, self._enumquotedreverse(self.columns), self._replacelist(values))
self._query(q, self._listtotuple(values))
def delete_by_filter(self, qfilter):
q = 'DELETE FROM %s %s' % (self.name, qfilter)
self._query(q)
def create(self, parameters):
q = 'CREATE TABLE `%s` (' % self.name
for i in xrange(len(self.columns)-1):
q = '%s `%s` %s,' % (q, self.columns[i], parameters[i])
q = '%s `%s` %s)' % (q, self.columns[i+1], parameters[i+1])
self._query(q)
def create_if_not_exists(self, parameters):
q = 'CREATE TABLE IF NOT EXISTS `%s` (' % self.name
for i in xrange(len(self.columns)-1):
q = '%s `%s` %s,' % (q, self.columns[i], parameters[i])
q = '%s `%s` %s)' % (q, self.columns[i+1], parameters[i+1])
self._query(q)
class vcards:
class __card__:
def __init__(self):
r.company = ''
r.firstName = None
r.lastName = None
r.title = None
r.city = None
r.country = None
r.address = None
r.zipCode = None
r.state = None
r.email = None
r.webSite = None
r.phone = None
r.phone2 = None
r.fax = None
r.cellPhone = None
r.image = None
def __init__(self, vcfdata):
import vobject
vcard_delemiter = "\nEND:VCARD"
self.vcfdata = vcfdata
vcards = vcfdata.split(vcard_delemiter)
del(vcards[len(vcards)-1])
self.v = []
_nextPos = 0
for i in xrange(len(vcards)):
vcards[i] = vcards[i] + vcard_delemiter
self.v.append(vobject.readOne(vcards[i].replace(';QUOTED-PRINTABLE','')))
def _rmspaces(self, val):
try:
for i in xrange(len(val)):
if val[0] == ' ':
val = val[1:]
if val == '':
result = None
else:
result = val
except:
result = val
return result
def _normalize(self, val):
if type(val) == type([]):
result = ''
for i in xrange(len(val)-1):
result += '%s, ' % val[i]
result += '%s' % val[i + 1]
else:
result = val
result = self._rmspaces(result)
return result
def _getFirstName(self, vcard):
try:
result = vcard.n.value.given
except:
result = None
return self._normalize(result)
def _getLastName(self, vcard):
try:
result = vcard.n.value.family
except:
result = None
return self._normalize(result)
def _getCity(self, vcard):
try:
result = vcard.adr.value.city
except:
result = None
return self._normalize(result)
def _getCountry(self, vcard):
try:
result = vcard.adr.value.country
except:
result = None
return self._normalize(result)
def _getAddress(self, vcard):
try:
result = vcard.adr.value.street
except:
result = None
return self._normalize(result)
def _getZipCode(self, vcard):
try:
result = vcard.adr.value.code
except:
result = None
return self._normalize(result)
def _getState(self, vcard):
try:
result = vcard.adr.value.region
except:
result = None
return self._normalize(result)
def _getEmail(self, vcard):
try:
result = vcard.email.value
except:
result = None
return self._normalize(result)
def _getWebSite(self, vcard):
try:
result = vcard.url.value
except:
result = None
return self._normalize(result)
def _getTitle(self, vcard):
try:
result = vcard.title.value
except:
result = None
return self._normalize(result)
def _getCompany(self, vcard):
try:
result = vcard.org.value[0]
except:
result = None
return self._normalize(result)
def _getImage(self, vcard):
try:
result = vcard.photo.value
except:
result = None
return result
def _getPhone(self, vcard):
try:
tel = vcard.tel_list
for i in xrange(len(tel)):
pref = False
work = False
voice = False
cell = False
fax = False
for j in xrange(len(tel[i].singletonparams)):
if tel[i].singletonparams[j] == 'PREF': pref = True
if tel[i].singletonparams[j] == 'WORK': work = True
if tel[i].singletonparams[j] == 'VOICE': voice = True
if tel[i].singletonparams[j] == 'CELL': cell = True
if tel[i].singletonparams[j] == 'FAX': fax = True
if pref == True and work == True and voice == True and cell == False and fax == False:
found = 1
break
if found == True:
result = tel[i].value
else:
result = None
except:
result = None
return self._normalize(result)
def _getPhone2(self, vcard):
try:
tel = vcard.tel_list
for i in xrange(len(tel)):
pref = False
work = False
voice = False
cell = False
fax = False
for j in xrange(len(tel[i].singletonparams)):
if tel[i].singletonparams[j] == 'PREF': pref = True
if tel[i].singletonparams[j] == 'WORK': work = True
if tel[i].singletonparams[j] == 'VOICE': voice = True
if tel[i].singletonparams[j] == 'CELL': cell = True
if tel[i].singletonparams[j] == 'FAX': fax = True
if pref == False and work == True and voice == True and cell == False and fax == False:
found = 1
break
if found == True:
result = tel[i].value
else:
result = None
except:
result = None
return self._normalize(result)
def _getFax(self, vcard):
try:
tel = vcard.tel_list
for i in xrange(len(tel)):
pref = False
work = False
voice = False
cell = False
fax = False
for j in xrange(len(tel[i].singletonparams)):
if tel[i].singletonparams[j] == 'PREF': pref = True
if tel[i].singletonparams[j] == 'WORK': work = True
if tel[i].singletonparams[j] == 'VOICE': voice = True
if tel[i].singletonparams[j] == 'CELL': cell = True
if tel[i].singletonparams[j] == 'FAX': fax = True
if pref == False and work == True and voice == False and cell == False and fax == True:
found = 1
break
if found == True:
result = tel[i].value
else:
result = None
except:
result = None
return self._normalize(result)
def _getCellPhone(self, vcard):
try:
tel = vcard.tel_list
for i in xrange(len(tel)):
pref = False
work = False
voice = False
cell = False
fax = False
for j in xrange(len(tel[i].singletonparams)):
if tel[i].singletonparams[j] == 'PREF': pref = True
if tel[i].singletonparams[j] == 'WORK': work = True
if tel[i].singletonparams[j] == 'VOICE': voice = True
if tel[i].singletonparams[j] == 'CELL': cell = True
if tel[i].singletonparams[j] == 'FAX': fax = True
if pref == False and work == False and voice == True and cell == True and fax == False:
found = 1
break
if found == True:
result = tel[i].value
else:
result = None
except:
result = None
return self._normalize(result)
def __iter__(self):
return self
def __getitem__(self, item):
r = self.__card__
r.company = self._getCompany(self.v[item])
r.firstName = self._getFirstName(self.v[item])
r.lastName = self._getLastName(self.v[item])
r.title = self._getTitle(self.v[item])
r.city = self._getCity(self.v[item])
r.country = self._getCountry(self.v[item])
r.address = self._getAddress(self.v[item])
r.zipCode = self._getZipCode(self.v[item])
r.state = self._getState(self.v[item])
r.email = self._getEmail(self.v[item])
r.webSite = self._getWebSite(self.v[item])
r.phone = self._getPhone(self.v[item])
r.phone2 = self._getPhone2(self.v[item])
r.fax = self._getFax(self.v[item])
r.cellPhone = self._getCellPhone(self.v[item])
r.image = self._getImage(self.v[item])
return r
def next(self):
r = self.__card__
if not self.v[_nextPos]:
raise StopIteration
else:
r.company = self._getCompany(self.v[self._nextPos])
r.firstName = self._getFirstName(self.v[self._nextPos])
r.lastName = self._getLastName(self.v[self._nextPos])
r.title = self._getTitle(self.v[self._nextPos])
r.city = self._getCity(self.v[self._nextPos])
r.country = self._getCountry(self.v[self._nextPos])
r.address = self._getAddress(self.v[self._nextPos])
r.zipCode = self._getZipCode(self.v[self._nextPos])
r.state = self._getState(self.v[self._nextPos])
r.email = self._getEmail(self.v[self._nextPos])
r.webSite = self._getWebSite(self.v[self._nextPos])
r.phone = self._getPhone(self.v[self._nextPos])
r.phone2 = self._getPhone2(self.v[self._nextPos])
r.fax = self._getFax(self.v[self._nextPos])
r.cellPhone = self._getCellPhone(self.v[self._nextPos])
r.image = self._getImage(self.v[self._nextPos])
self._nextPos = self._nextPos + 1
return r
def __len__(self):
return len(self.v)
#
#for i in xrange(len(v)):
# print 'Company: %s' % _getCompany(v[i])
# print 'First name: %s' % _getFirstName(v[i])
# print 'Last name: %s' % _getLastName(v[i])
# print 'Title: %s' % _getTitle(v[i])
# print 'City: %s' % _getCity(v[i])
# print 'Country: %s' % _getCountry(v[i])
# print 'Address: %s' % _getAddress(v[i])
# print 'Zip code: %s ' % _getZipCode(v[i])
# print 'State: %s' % _getState(v[i])
# print 'Email: %s' % _getEmail(v[i])
# print 'Web site: %s' % _getWebSite(v[i])
# print 'Phone: %s' % _getPhone(v[i])
# print 'Phone2: %s' % _getPhone2(v[i])
# print 'Fax: %s' % _getFax(v[i])
# print 'Cell phone: %s' % _getCellPhone(v[i])
# print '------------------------------------------------------'