Mercurial > p > roundup > code
comparison roundup/test/memorydb.py @ 8260:617d85ce4ac3
chore(ruff): variable renames, formatting, sort imports, use with open
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sat, 04 Jan 2025 22:34:47 -0500 |
| parents | 506c86823abb |
| children |
comparison
equal
deleted
inserted
replaced
| 8259:d7cc63d7a857 | 8260:617d85ce4ac3 |
|---|---|
| 1 '''Implement an in-memory hyperdb for testing purposes. | 1 '''Implement an in-memory hyperdb for testing purposes. |
| 2 ''' | 2 ''' |
| 3 | 3 |
| 4 import os | |
| 4 import shutil | 5 import shutil |
| 5 import os | |
| 6 import time | 6 import time |
| 7 | 7 |
| 8 from roundup import date | 8 from roundup import configuration, date, hyperdb, password, roundupdb, security |
| 9 from roundup import hyperdb | 9 from roundup.anypy.strings import s2b |
| 10 from roundup import roundupdb | 10 from roundup.backends import back_anydbm, indexer_common, indexer_dbm, sessions_dbm |
| 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 | 11 from roundup.support import ensureParentsExist |
| 19 from roundup.anypy.strings import s2b | |
| 20 from roundup.test.tx_Source_detector import init as tx_Source_init | 12 from roundup.test.tx_Source_detector import init as tx_Source_init |
| 21 | 13 |
| 22 default_prefix = '../../share/roundup/templates/classic' | 14 default_prefix = '../../share/roundup/templates/classic' |
| 23 | 15 |
| 24 | 16 |
| 49 # load standard schema | 41 # load standard schema |
| 50 if not prefix.startswith('/'): | 42 if not prefix.startswith('/'): |
| 51 prefix = os.path.join(os.path.dirname(__file__), prefix) | 43 prefix = os.path.join(os.path.dirname(__file__), prefix) |
| 52 | 44 |
| 53 schema = os.path.join(prefix, 'schema.py') | 45 schema = os.path.join(prefix, 'schema.py') |
| 54 vars = hyperdb.__dict__ | 46 hyperdb_vars = hyperdb.__dict__ |
| 55 vars['Class'] = Class | 47 hyperdb_vars['Class'] = Class |
| 56 vars['FileClass'] = FileClass | 48 hyperdb_vars['FileClass'] = FileClass |
| 57 vars['IssueClass'] = IssueClass | 49 hyperdb_vars['IssueClass'] = IssueClass |
| 58 vars['db'] = db | 50 hyperdb_vars['db'] = db |
| 59 fd = open(schema) | 51 |
| 60 exec(compile(fd.read(), schema, 'exec'), vars) | 52 with open(schema) as fd: |
| 61 fd.close() | 53 exec(compile(fd.read(), schema, 'exec'), hyperdb_vars) |
| 62 | 54 |
| 63 initial_data = os.path.join(prefix, 'initial_data.py') | 55 initial_data = os.path.join(prefix, 'initial_data.py') |
| 64 vars = dict(db=db, admin_email='admin@test.com', | 56 admin_vars = {"db": db, "admin_email": "admin@test.com", |
| 65 adminpw=password.Password('sekrit', config=db.config)) | 57 "adminpw": password.Password('sekrit', config=db.config)} |
| 66 fd = open(initial_data) | 58 with open(initial_data) as fd: |
| 67 exec(compile(fd.read(), initial_data, 'exec'), vars) | 59 exec(compile(fd.read(), initial_data, 'exec'), admin_vars) |
| 68 fd.close() | |
| 69 | 60 |
| 70 # load standard detectors | 61 # load standard detectors |
| 71 dirname = os.path.join(prefix, 'detectors') | 62 dirname = os.path.join(prefix, 'detectors') |
| 72 for fn in os.listdir(dirname): | 63 for fn in os.listdir(dirname): |
| 73 if not fn.endswith('.py'): continue # noqa: E701 | 64 if not fn.endswith('.py'): continue # noqa: E701 |
| 74 vars = {} | 65 exec_vars = {} |
| 75 with open(os.path.join(dirname, fn)) as fd: | 66 with open(os.path.join(dirname, fn)) as fd: |
| 76 exec(compile(fd.read(), | 67 exec(compile(fd.read(), |
| 77 os.path.join(dirname, fn), 'exec'), vars) | 68 os.path.join(dirname, fn), 'exec'), exec_vars) |
| 78 vars['init'](db) | 69 exec_vars['init'](db) |
| 79 | 70 |
| 80 tx_Source_init(db) | 71 tx_Source_init(db) |
| 81 | 72 |
| 82 ''' | 73 ''' |
| 83 status = Class(db, "status", name=String()) | 74 status = Class(db, "status", name=String()) |
| 198 if '__timestamp' in newvalues: | 189 if '__timestamp' in newvalues: |
| 199 try: | 190 try: |
| 200 float(newvalues['__timestamp']) | 191 float(newvalues['__timestamp']) |
| 201 except ValueError: | 192 except ValueError: |
| 202 if infoid in self: | 193 if infoid in self: |
| 203 del(newvalues['__timestamp']) | 194 del (newvalues['__timestamp']) |
| 204 else: | 195 else: |
| 205 newvalues['__timestamp'] = time.time() | 196 newvalues['__timestamp'] = time.time() |
| 206 self[infoid].update(newvalues) | 197 self[infoid].update(newvalues) |
| 207 | 198 |
| 208 def list(self): | 199 def list(self): |
| 299 self.items = self.__class__.memdb.get('items', {}) | 290 self.items = self.__class__.memdb.get('items', {}) |
| 300 self.ids = self.__class__.memdb.get('ids', {}) | 291 self.ids = self.__class__.memdb.get('ids', {}) |
| 301 self.journals = self.__class__.memdb.get('journals', {}) | 292 self.journals = self.__class__.memdb.get('journals', {}) |
| 302 | 293 |
| 303 def filename(self, classname, nodeid, property=None, create=0): | 294 def filename(self, classname, nodeid, property=None, create=0): |
| 304 shutil.copyfile(__file__, __file__+'.dummy') | 295 shutil.copyfile(__file__, __file__ + '.dummy') |
| 305 return __file__+'.dummy' | 296 return __file__ + '.dummy' |
| 306 | 297 |
| 307 def filesize(self, classname, nodeid, property=None, create=0): | 298 def filesize(self, classname, nodeid, property=None, create=0): |
| 308 return len(self.getfile(classname, nodeid, property)) | 299 return len(self.getfile(classname, nodeid, property)) |
| 309 | 300 |
| 310 def post_init(self): | 301 def post_init(self): |
| 421 # | 412 # |
| 422 def newid(self, classname): | 413 def newid(self, classname): |
| 423 self.ids[classname] += 1 | 414 self.ids[classname] += 1 |
| 424 return str(self.ids[classname]) | 415 return str(self.ids[classname]) |
| 425 | 416 |
| 426 def setid(self, classname, id): | 417 def setid(self, classname, nodeid): |
| 427 self.ids[classname] = int(id) | 418 self.ids[classname] = int(nodeid) |
| 428 | 419 |
| 429 # | 420 # |
| 430 # Journal | 421 # Journal |
| 431 # | 422 # |
| 432 def doSaveJournal(self, classname, nodeid, action, params, creator, | 423 def doSaveJournal(self, classname, nodeid, action, params, creator, |
| 475 for key in db: | 466 for key in db: |
| 476 # get the journal for this db entry | 467 # get the journal for this db entry |
| 477 kept_journals = [] | 468 kept_journals = [] |
| 478 for entry in db[key]: | 469 for entry in db[key]: |
| 479 # unpack the entry | 470 # unpack the entry |
| 480 (nodeid, date_stamp, self.journaltag, action, | 471 (_nodeid, date_stamp, self.journaltag, action, |
| 481 params) = entry | 472 _params) = entry |
| 482 date_stamp = date_stamp.serialise() | 473 date_stamp = date_stamp.serialise() |
| 483 # if the entry is after the pack date, _or_ the initial | 474 # if the entry is after the pack date, _or_ the initial |
| 484 # create entry, then it stays | 475 # create entry, then it stays |
| 485 if date_stamp > pack_before or action == 'create': | 476 if date_stamp > pack_before or action == 'create': |
| 486 kept_journals.append(entry) | 477 kept_journals.append(entry) |
| 500 back_anydbm.Class.__init__(self, db, classname, **properties) | 491 back_anydbm.Class.__init__(self, db, classname, **properties) |
| 501 | 492 |
| 502 def export_files(self, dirname, nodeid): | 493 def export_files(self, dirname, nodeid): |
| 503 dest = self.exportFilename(dirname, nodeid) | 494 dest = self.exportFilename(dirname, nodeid) |
| 504 ensureParentsExist(dest) | 495 ensureParentsExist(dest) |
| 505 f = open(dest, 'wb') | 496 with open(dest, 'wb') as f: |
| 506 f.write(self.db.files[self.classname, nodeid, None]) | 497 f.write(self.db.files[self.classname, nodeid, None]) |
| 507 f.close() | |
| 508 | 498 |
| 509 def import_files(self, dirname, nodeid): | 499 def import_files(self, dirname, nodeid): |
| 510 source = self.exportFilename(dirname, nodeid) | 500 source = self.exportFilename(dirname, nodeid) |
| 511 f = open(source, 'rb') | 501 with open(source, 'rb') as f: |
| 512 self.db.files[self.classname, nodeid, None] = f.read() | 502 self.db.files[self.classname, nodeid, None] = f.read() |
| 513 f.close() | |
| 514 mime_type = None | 503 mime_type = None |
| 515 props = self.getprops() | 504 props = self.getprops() |
| 516 if 'type' in props: | 505 if 'type' in props: |
| 517 mime_type = self.get(nodeid, 'type') | 506 mime_type = self.get(nodeid, 'type') |
| 518 if not mime_type: | 507 if not mime_type: |
