#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009
# Klaus Umbach <treibholz@users.sourceforge.net>
# Sebastian Cabrera <z3po@users.sourceforge.net>
'''spdInterface Module.
The Modules handles all the exchange between user and spdCore.
Usage:
import spdInterface
Interface = spdInterface.Interface()
Interface.main()
or force a clientmode with:
Interface = spdInterface.Interface('CLI')
if building your own interface, name it "whatever" and pass "whatever"
to the Interface Class. The Module must be in spds spdInterfaces path.'''
import os
import string
import spdConfig
import spdCore
class Interface(object): # {{{
"""The Interface Module.
Interface between client and Core."""
spddir = os.path.expanduser('~/.spd/')
client = None
core = None
def __init__(self,clientmode=False): # {{{
"""init procedure"""
if not os.access(self.spddir, os.F_OK):
os.mkdir(self.spddir)
self.ConfigObject = spdConfig.Config()
self.configversion = self.ConfigObject.getConfigVersion()
if not self.configversion:
print 'ConfigVersion not found...setting Default one\n'
self.ConfigObject.setConfigVersion('0.3-alpha')
self.configversion = self.ConfigObject.getConfigVersion()
__blackhole = raw_input('Press any key')
if not clientmode:
clientmode = self.ConfigObject.getClientmode()
if not clientmode:
print 'Default Clientmode not found...setting it to CLI\n'
self.ConfigObject.setClientmode('cli')
clientmode = self.ConfigObject.getClientmode()
__blackhole = raw_input('Press any key')
self.client = __import__('spdInterfaces.' + clientmode.lower(), fromlist=['',])
passwordfield = self.ConfigObject.getPasswordfield()
if passwordfield:
self.client.passwordfield
# }}}
def __findPasswords(self,args): # {{{
"""Find the passwords by searchlist/searchstring handed.
print the results in CLIENTMODE.
# searchlist: optional string or list of keywords to search for."""
if 'byid' in args:
byid = args['byid']
if 'args' in args and args['args'] != []:
searchString = args['args']
else:
searchString = '%'
if type(searchString).__name__ == 'list':
searchlist = searchString
else:
searchlist = string.split(searchString)
passdict = self.core.searchEntry(searchlist,byid)
return passdict
# }}}
def updatePreset(self, preset): # {{{
self.ConfigObject.setPreset(preset)
# }}}
def loadStore(self): # {{{
self.core = spdCore.Core(self.ConfigObject)
# }}}
def saveData(self, __blackhole='nothing'): # {{{
self.core.saveData()
# }}}
def fetchData(self, __blackhole='nothing'): # {{{
self.core.fetchData()
# }}}
def printPasswords(self,args): # {{{
passdict = self.__findPasswords(args)
self.client.printPasswords(passdict, args)
# }}}
def deleteEntry(self, args): # {{{
__passdict = self.__findPasswords(args)
if len(__passdict) == 0:
raise ValueError('deleteEntry','No matches found')
self.client.printPasswords(__passdict, args)
ids = self.client.deleteEntry(__passdict)
self.core.delEntry(ids)
# }}}
def editEntry(self,args): # {{{
__passdict = self.__findPasswords(args)
if len(__passdict) > 0:
__newpassdict = self.client.editEntry(__passdict)
self.core.editEntry(__newpassdict)
else:
raise ValueError('no matches for ' + str(args["args"]) + 'found')
# }}}
def deleteColumn(self,args): # {{{
if 'args' in args:
columns = args['args']
else:
columns = []
for column in columns:
if column in self.core.getColumns(showid=False):
self.core.delColumn(column)
else:
raise ValueError('column ' + column + ' is not part of the passwordfile')
# }}}
def renameColumn(self,args): # {{{
if 'args' in args:
columns = args['args']
else:
columns = []
if len(columns) != 2:
raise ValueError('You need to give old and new columnname as argument')
else:
oldcolumn = columns[0]
newcolumn = columns[1]
if oldcolumn in self.core.getColumns(showid=False):
self.core.renameColumn(oldcolumn, newcolumn)
else:
raise ValueError('column ' + column + ' is not part of the passwordfile')
# }}}
def addColumn(self,args): # {{{
if 'args' in args:
columns = args['args']
else:
columns = []
for column in columns:
self.core.addColumn(column)
# }}}
def addEntry(self, args): # {{{
Entry = self.client.addEntry(self.core.getColumns(showid=False))
self.core.addEntry(Entry)
# }}}
def main(self): # {{{
functions, args = self.client.parseArgs()
if 'preset' in args:
self.updatePreset(args['preset'])
self.loadStore()
if type(functions).__name__ == 'tuple':
for function in functions:
getattr(self, function)(args)
else:
getattr(self, functions)(args)
# }}}
# }}}
if __name__ == '__main__':
Iface = Interface()
Iface.main()
# EOF
# vim:filetype=python:foldmethod=marker:autoindent:expandtab:tabstop=4