Menu

[3d58ca]: / src / spdInterface.py  Maximize  Restore  History

Download this file

194 lines (153 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/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