2121
2222default_prefix = '../../share/roundup/templates/classic'
2323
24+
2425def new_config (debug = False , prefix = default_prefix ):
2526 if not prefix .startswith ('/' ):
26- prefix = os .path .join (os .path .dirname (__file__ ), prefix )
27+ prefix = os .path .join (os .path .dirname (__file__ ), prefix )
2728 config = configuration .CoreConfig ()
2829 config .detectors = configuration .UserConfig (
2930 os .path .join (prefix , "detectors/config.ini" ))
3031 config .ext = configuration .UserConfig (
3132 os .path .join (prefix , "extensions/config.ini" ))
3233 config .DATABASE = "db"
33- #config.logging = MockNull()
34+ # config.logging = MockNull()
3435 # these TRACKER_WEB and MAIL_DOMAIN values are used in mailgw tests
3536 if debug :
3637 config .LOGGING_LEVEL = "DEBUG"
3738 config .MAIL_DOMAIN = "your.tracker.email.domain.example"
3839 config .TRACKER_WEB = "http://tracker.example/cgi-bin/roundup.cgi/bugs/"
3940 return config
4041
42+
4143def create (journaltag , create = True , debug = False , prefix = default_prefix ):
4244 # "Nuke" in-memory db
4345 db_nuke ('' )
@@ -46,7 +48,7 @@ def create(journaltag, create=True, debug=False, prefix=default_prefix):
4648
4749 # load standard schema
4850 if not prefix .startswith ('/' ):
49- prefix = os .path .join (os .path .dirname (__file__ ), prefix )
51+ prefix = os .path .join (os .path .dirname (__file__ ), prefix )
5052
5153 schema = os .path .join (prefix , 'schema.py' )
5254 vars = hyperdb .__dict__
@@ -60,15 +62,15 @@ def create(journaltag, create=True, debug=False, prefix=default_prefix):
6062
6163 initial_data = os .path .join (prefix , 'initial_data.py' )
6264 vars = dict (
db = db ,
admin_email = '[email protected] ' ,
63- adminpw = password .Password ('sekrit' ))
65+ adminpw = password .Password ('sekrit' ))
6466 fd = open (initial_data )
6567 exec (compile (fd .read (), initial_data , 'exec' ), vars )
6668 fd .close ()
6769
6870 # load standard detectors
6971 dirname = os .path .join (prefix , 'detectors' )
7072 for fn in os .listdir (dirname ):
71- if not fn .endswith ('.py' ): continue
73+ if not fn .endswith ('.py' ): continue # noqa: E701
7274 vars = {}
7375 with open (os .path .join (dirname , fn )) as fd :
7476 exec (compile (fd .read (),
@@ -108,7 +110,8 @@ def create(journaltag, create=True, debug=False, prefix=default_prefix):
108110 '''
109111 if create :
110112 db .user .create (username = "fred" , roles = 'User' ,
111- password = password .
Password (
'sekrit' ),
address = '[email protected] ' )
113+ password = password .Password ('sekrit' ),
114+ 112115
113116 db .security .addPermissionToRole ('User' , 'Email Access' )
114117 '''
@@ -125,22 +128,29 @@ def create(journaltag, create=True, debug=False, prefix=default_prefix):
125128 '''
126129 return db
127130
131+
128132class cldb (dict ):
129133 def __init__ (self , ** values ):
130134 super (cldb , self ).__init__ ()
131135 for key , value in values .items ():
132136 super (cldb , self ).__setitem__ (s2b (key ), value )
137+
133138 def __getitem__ (self , key ):
134139 return super (cldb , self ).__getitem__ (s2b (key ))
140+
135141 def __setitem__ (self , key , value ):
136142 return super (cldb , self ).__setitem__ (s2b (key ), value )
143+
137144 def __delitem__ (self , key ):
138145 return super (cldb , self ).__delitem__ (s2b (key ))
146+
139147 def __contains__ (self , key ):
140148 return super (cldb , self ).__contains__ (s2b (key ))
149+
141150 def close (self ):
142151 pass
143152
153+
144154class BasicDatabase (dict ):
145155 ''' Provide a nice encapsulation of an anydbm store.
146156
@@ -150,31 +160,39 @@ def __init__(self, **values):
150160 super (BasicDatabase , self ).__init__ ()
151161 for k , v in values .items ():
152162 super (BasicDatabase , self ).__setitem__ (s2b (k ), v )
163+
153164 def __getitem__ (self , key ):
154165 if key not in self :
155166 d = self [key ] = {}
156167 return d
157168 return super (BasicDatabase , self ).__getitem__ (s2b (key ))
169+
158170 def __setitem__ (self , key , value ):
159171 return super (BasicDatabase , self ).__setitem__ (s2b (key ), value )
172+
160173 def __delitem__ (self , key ):
161174 return super (BasicDatabase , self ).__delitem__ (s2b (key ))
175+
162176 def __contains__ (self , key ):
163177 return super (BasicDatabase , self ).__contains__ (s2b (key ))
178+
164179 def exists (self , infoid ):
165180 return infoid in self
166181 _marker = []
182+
167183 def get (self , infoid , value , default = _marker ):
168184 if infoid not in self :
169185 if default is self ._marker :
170186 raise KeyError
171187 else :
172188 return default
173189 return self [infoid ].get (value , default )
190+
174191 def getall (self , infoid ):
175192 if infoid not in self :
176193 raise KeyError (infoid )
177194 return self [infoid ]
195+
178196 def set (self , infoid , ** newvalues ):
179197 if '__timestamp' in newvalues :
180198 try :
@@ -185,28 +203,37 @@ def set(self, infoid, **newvalues):
185203 else :
186204 newvalues ['__timestamp' ] = time .time ()
187205 self [infoid ].update (newvalues )
206+
188207 def list (self ):
189208 return list (self .keys ())
209+
190210 def destroy (self , infoid ):
191211 del self [infoid ]
212+
192213 def commit (self ):
193214 pass
215+
194216 def close (self ):
195217 pass
218+
196219 def updateTimestamp (self , sessid ):
197220 sess = self .get (sessid , '__timestamp' , None )
198221 now = time .time ()
199222 if sess is None or now > sess + 60 :
200223 self .set (sessid , __timestamp = now )
224+
201225 def clean (self ):
202226 pass
203227
228+
204229class Sessions (BasicDatabase , sessions_dbm .Sessions ):
205230 name = 'sessions'
206231
232+
207233class OneTimeKeys (BasicDatabase , sessions_dbm .OneTimeKeys ):
208234 name = 'otks'
209235
236+
210237class Indexer (indexer_dbm .Indexer ):
211238 def __init__ (self , db ):
212239 indexer_common .Indexer .__init__ (self , db )
@@ -219,17 +246,19 @@ def load_index(self, reload=0, wordlist=None):
219246 if self .index_loaded () and not reload :
220247 return 0
221248 self .words = {}
222- self .files = {'_TOP' :(0 ,None )}
249+ self .files = {'_TOP' : (0 , None )}
223250 self .fileids = {}
224251 self .changed = 0
225252
226253 def save_index (self ):
227254 pass
255+
228256 def force_reindex (self ):
229257 # TODO I'm concerned that force_reindex may not be tested by
230258 # testForcedReindexing if the functionality can just be removed
231259 pass
232260
261+
233262class Database (back_anydbm .Database ):
234263 """A database for storing records containing flexible data types.
235264
@@ -252,17 +281,17 @@ def __init__(self, config, journaltag=None):
252281 self .tx_files = {}
253282 self .security = security .Security (self )
254283 self .stats = {'cache_hits' : 0 , 'cache_misses' : 0 , 'get_items' : 0 ,
255- 'filtering' : 0 }
284+ 'filtering' : 0 }
256285 self .sessions = Sessions ()
257286 self .otks = OneTimeKeys ()
258287 self .indexer = Indexer (self )
259288 roundupdb .Database .__init__ (self )
260289
261290 # anydbm bits
262- self .cache = {} # cache of nodes loaded or created
263- self .dirtynodes = {} # keep track of the dirty nodes by class
264- self .newnodes = {} # keep track of the new nodes by class
265- self .destroyednodes = {}# keep track of the destroyed nodes by class
291+ self .cache = {} # cache of nodes loaded or created
292+ self .dirtynodes = {} # keep track of the dirty nodes by class
293+ self .newnodes = {} # keep track of the new nodes by class
294+ self .destroyednodes = {} # keep track of the destroyed nodes by class
266295 self .transactions = []
267296 self .tx_Source = None
268297 # persistence across re-open
@@ -293,14 +322,14 @@ def reindex(self, classname=None, show_progress=False):
293322 pass
294323
295324 def __repr__ (self ):
296- return '<memorydb instance at %x>' % id (self )
325+ return '<memorydb instance at %x>' % id (self )
297326
298327 def storefile (self , classname , nodeid , property , content ):
299328 if isinstance (content , str ):
300329 content = s2b (content )
301330 self .tx_files [classname , nodeid , property ] = content
302331 self .transactions .append ((self .doStoreFile , (classname , nodeid ,
303- property )))
332+ property )))
304333
305334 def getfile (self , classname , nodeid , property ):
306335 if (classname , nodeid , property ) in self .tx_files :
@@ -340,19 +369,22 @@ def __getattr__(self, classname):
340369 def addclass (self , cl ):
341370 cn = cl .classname
342371 if cn in self .classes :
343- raise ValueError ('Class "%s" already defined.' % cn )
372+ raise ValueError ('Class "%s" already defined.' % cn )
344373 self .classes [cn ] = cl
345374 if cn not in self .items :
346375 self .items [cn ] = cldb ()
347376 self .ids [cn ] = 0
348377
349378 # add default Edit and View permissions
350379 self .security .addPermission (name = "Create" , klass = cn ,
351- description = "User is allowed to create " + cn )
380+ description = "User is allowed to create " +
381+ cn )
352382 self .security .addPermission (name = "Edit" , klass = cn ,
353- description = "User is allowed to edit " + cn )
383+ description = "User is allowed to edit " +
384+ cn )
354385 self .security .addPermission (name = "View" , klass = cn ,
355- description = "User is allowed to access " + cn )
386+ description = "User is allowed to access " +
387+ cn )
356388
357389 def getclasses (self ):
358390 """Return a list of the names of all existing classes."""
@@ -366,7 +398,7 @@ def getclass(self, classname):
366398 try :
367399 return self .classes [classname ]
368400 except KeyError :
369- raise KeyError ('There is no class called "%s"' % classname )
401+ raise KeyError ('There is no class called "%s"' % classname )
370402
371403 #
372404 # Class DBs
@@ -389,20 +421,21 @@ def getCachedJournalDB(self, classname):
389421 def newid (self , classname ):
390422 self .ids [classname ] += 1
391423 return str (self .ids [classname ])
424+
392425 def setid (self , classname , id ):
393426 self .ids [classname ] = int (id )
394427
395428 #
396429 # Journal
397430 #
398431 def doSaveJournal (self , classname , nodeid , action , params , creator ,
399- creation ):
432+ creation ):
400433 if creator is None :
401434 creator = self .getuid ()
402435 if creation is None :
403436 creation = date .Date ()
404- self .journals .setdefault (classname , {}).setdefault (nodeid ,
405- []).append ((nodeid , creation , creator , action , params ))
437+ self .journals .setdefault (classname , {}).setdefault (
438+ nodeid , []).append ((nodeid , creation , creator , action , params ))
406439
407440 def doSetJournal (self , classname , nodeid , journal ):
408441 self .journals .setdefault (classname , {})[nodeid ] = journal
@@ -424,11 +457,11 @@ def getjournal(self, classname, nodeid):
424457 if not cache_creation :
425458 cache_creation = date .Date ()
426459 res .append ((cache_nodeid , cache_creation , cache_creator ,
427- cache_action , cache_params ))
460+ cache_action , cache_params ))
428461 try :
429462 res += self .journals .get (classname , {})[nodeid ]
430463 except KeyError :
431- if res : return res
464+ if res : return res # noqa: E701
432465 raise IndexError (nodeid )
433466 return res
434467
@@ -440,22 +473,23 @@ def pack(self, pack_before):
440473 db = self .journals [classname ]
441474 for key in db :
442475 # get the journal for this db entry
443- l = []
444- last_set_entry = None
476+ kept_journals = []
445477 for entry in db [key ]:
446478 # unpack the entry
447479 (nodeid , date_stamp , self .journaltag , action ,
448- params ) = entry
480+ params ) = entry
449481 date_stamp = date_stamp .serialise ()
450482 # if the entry is after the pack date, _or_ the initial
451483 # create entry, then it stays
452484 if date_stamp > pack_before or action == 'create' :
453- l .append (entry )
454- db [key ] = l
485+ kept_journals .append (entry )
486+ db [key ] = kept_journals
487+
455488
456489class Class (back_anydbm .Class ):
457490 pass
458491
492+
459493class FileClass (back_anydbm .FileClass ):
460494 def __init__ (self , db , classname , ** properties ):
461495 if 'content' not in properties :
@@ -484,7 +518,8 @@ def import_files(self, dirname, nodeid):
484518 mime_type = self .default_mime_type
485519 if props ['content' ].indexme :
486520 self .db .indexer .add_text ((self .classname , nodeid , 'content' ),
487- self .get (nodeid , 'content' ), mime_type )
521+ self .get (nodeid , 'content' ), mime_type )
522+
488523
489524# deviation from spec - was called ItemClass
490525class IssueClass (Class , roundupdb .IssueClass ):
@@ -512,9 +547,11 @@ def __init__(self, db, classname, **properties):
512547# Methods to check for existence and nuke the db
513548# We don't support multiple named databases
514549
550+
515551def db_exists (name ):
516552 return bool (Database .memdb )
517553
554+
518555def db_nuke (name ):
519556 Database .memdb = {}
520557
0 commit comments