11'''Implement an in-memory hyperdb for testing purposes.
22'''
33
4- import shutil
54import os
5+ import shutil
66import time
77
8- from roundup import date
9- from roundup import hyperdb
10- from roundup import roundupdb
11- from roundup import security
12- from roundup import password
13- from roundup import configuration
14- from roundup .backends import back_anydbm
15- from roundup .backends import indexer_dbm
16- from roundup .backends import sessions_dbm
17- from roundup .backends import indexer_common
18- from roundup .support import ensureParentsExist
8+ from roundup import configuration , date , hyperdb , password , roundupdb , security
199from roundup .anypy .strings import s2b
10+ from roundup .backends import back_anydbm , indexer_common , indexer_dbm , sessions_dbm
11+ from roundup .support import ensureParentsExist
2012from roundup .test .tx_Source_detector import init as tx_Source_init
2113
2214default_prefix = '../../share/roundup/templates/classic'
@@ -51,31 +43,30 @@ def create(journaltag, create=True, debug=False, prefix=default_prefix):
5143 prefix = os .path .join (os .path .dirname (__file__ ), prefix )
5244
5345 schema = os .path .join (prefix , 'schema.py' )
54- vars = hyperdb .__dict__
55- vars ['Class' ] = Class
56- vars ['FileClass' ] = FileClass
57- vars ['IssueClass' ] = IssueClass
58- vars ['db' ] = db
59- fd = open ( schema )
60- exec ( compile ( fd . read (), schema , 'exec' ), vars )
61- fd .close ( )
46+ hyperdb_vars = hyperdb .__dict__
47+ hyperdb_vars ['Class' ] = Class
48+ hyperdb_vars ['FileClass' ] = FileClass
49+ hyperdb_vars ['IssueClass' ] = IssueClass
50+ hyperdb_vars ['db' ] = db
51+
52+ with open ( schema ) as fd :
53+ exec ( compile ( fd .read (), schema , 'exec' ), hyperdb_vars )
6254
6355 initial_data = os .path .join (prefix , 'initial_data.py' )
64- vars = dict (
db = db ,
admin_email = '[email protected] ' ,
65- adminpw = password .Password ('sekrit' , config = db .config ))
66- fd = open (initial_data )
67- exec (compile (fd .read (), initial_data , 'exec' ), vars )
68- fd .close ()
56+ admin_vars = {
"db" :
db ,
"admin_email" :
"[email protected] " ,
57+ "adminpw" : password .Password ('sekrit' , config = db .config )}
58+ with open (initial_data ) as fd :
59+ exec (compile (fd .read (), initial_data , 'exec' ), admin_vars )
6960
7061 # load standard detectors
7162 dirname = os .path .join (prefix , 'detectors' )
7263 for fn in os .listdir (dirname ):
7364 if not fn .endswith ('.py' ): continue # noqa: E701
74- vars = {}
65+ exec_vars = {}
7566 with open (os .path .join (dirname , fn )) as fd :
7667 exec (compile (fd .read (),
77- os .path .join (dirname , fn ), 'exec' ), vars )
78- vars ['init' ](db )
68+ os .path .join (dirname , fn ), 'exec' ), exec_vars )
69+ exec_vars ['init' ](db )
7970
8071 tx_Source_init (db )
8172
@@ -200,7 +191,7 @@ def set(self, infoid, **newvalues):
200191 float (newvalues ['__timestamp' ])
201192 except ValueError :
202193 if infoid in self :
203- del (newvalues ['__timestamp' ])
194+ del (newvalues ['__timestamp' ])
204195 else :
205196 newvalues ['__timestamp' ] = time .time ()
206197 self [infoid ].update (newvalues )
@@ -301,8 +292,8 @@ def __init__(self, config, journaltag=None):
301292 self .journals = self .__class__ .memdb .get ('journals' , {})
302293
303294 def filename (self , classname , nodeid , property = None , create = 0 ):
304- shutil .copyfile (__file__ , __file__ + '.dummy' )
305- return __file__ + '.dummy'
295+ shutil .copyfile (__file__ , __file__ + '.dummy' )
296+ return __file__ + '.dummy'
306297
307298 def filesize (self , classname , nodeid , property = None , create = 0 ):
308299 return len (self .getfile (classname , nodeid , property ))
@@ -423,8 +414,8 @@ def newid(self, classname):
423414 self .ids [classname ] += 1
424415 return str (self .ids [classname ])
425416
426- def setid (self , classname , id ):
427- self .ids [classname ] = int (id )
417+ def setid (self , classname , nodeid ):
418+ self .ids [classname ] = int (nodeid )
428419
429420 #
430421 # Journal
@@ -477,8 +468,8 @@ def pack(self, pack_before):
477468 kept_journals = []
478469 for entry in db [key ]:
479470 # unpack the entry
480- (nodeid , date_stamp , self .journaltag , action ,
481- params ) = entry
471+ (_nodeid , date_stamp , self .journaltag , action ,
472+ _params ) = entry
482473 date_stamp = date_stamp .serialise ()
483474 # if the entry is after the pack date, _or_ the initial
484475 # create entry, then it stays
@@ -502,15 +493,13 @@ def __init__(self, db, classname, **properties):
502493 def export_files (self , dirname , nodeid ):
503494 dest = self .exportFilename (dirname , nodeid )
504495 ensureParentsExist (dest )
505- f = open (dest , 'wb' )
506- f .write (self .db .files [self .classname , nodeid , None ])
507- f .close ()
496+ with open (dest , 'wb' ) as f :
497+ f .write (self .db .files [self .classname , nodeid , None ])
508498
509499 def import_files (self , dirname , nodeid ):
510500 source = self .exportFilename (dirname , nodeid )
511- f = open (source , 'rb' )
512- self .db .files [self .classname , nodeid , None ] = f .read ()
513- f .close ()
501+ with open (source , 'rb' ) as f :
502+ self .db .files [self .classname , nodeid , None ] = f .read ()
514503 mime_type = None
515504 props = self .getprops ()
516505 if 'type' in props :
0 commit comments