def connect(self, dbname, server='sqlite', create=False, user='', password='', host='', port=None, **kws): "connect to an existing database" creds = dict(user=user, password=password, host=host, port=port, server=server) self.dbname = dbname if not self.isScanDB(dbname, **creds) and create: engine, meta = create_scandb(dbname, create=True, **creds) self.engine = engine self.metadata = meta self.metadata.reflect() if self.engine is None: raise ValueError("Cannot use '%s' as a Scan Database!" % dbname) self.conn = self.engine.connect() self.session = sessionmaker(bind=self.engine)() tabs, classes, mapprops, mapkeys = map_scandb(self.metadata) self.tables, self.classes = tabs, classes self.mapprops, self.mapkeys = mapprops, mapkeys self.status_codes = {} for row in self.getall('status'): self.status_codes[row.name] = row.id atexit.register(self.close)
def create_newdb(self, dbname, connect=False, **kws): "create a new, empty database" create_scandb(dbname, **kws) if connect: time.sleep(0.5) self.connect(dbname, backup=False, **kws)
statid = self.status_codes.get('requested', 1) kws.update({'arguments': arguments, 'output_file': output_file, 'output_value': output_value, 'status_id': statid}) row = self.__addRow(cls, ('command',), (command,), **kws) self.session.add(row) self.commit() return row def set_command_status(self, id, status): """set the status of a command (by id)""" cls, table = self._get_table('commands') if status not in self.status_codes: status = 'unknown' statid = self.status_codes[status] table.update(whereclause="id='%i'" % id).execute(status_id=statid) def cancel_command(self, id): """cancel command""" self.set_command_status(id, 'canceled') self.commit() if __name__ == '__main__': dbname = 'Test.sdb' create_scandb(dbname) print '''%s created and initialized.''' % dbname