def do_show(conf): conf.logger.debug('Using database: %s', conf.database_uri) conn = sqlite_connect(conf.database_uri) conn.row_factory = sqlite3.Row conn.create_function('md5', 1, lambda s: hashlib.md5(s).hexdigest()) call_table = [ (conf.computers, log_computer_table), (conf.software, log_software_table), (conf.partitions, log_partition_table), (conf.slaves, log_slave_table), (conf.params, log_params), (conf.network, log_network) ] if not any(flag for flag, func in call_table): to_call = [func for flag, func in call_table] else: to_call = [func for flag, func in call_table if flag] for idx, func in enumerate(to_call): func(conf.logger, conn) if idx < len(to_call) - 1: conf.logger.info(' ')
def do_show(conf): # Because this command uses logging facility to output, # setup a logger which displays on stdout and uses a pager # to paginate input, honoring $PAGER. output = sys.stdout if output.isatty(): output = StringIO() proxy_show_logger = logging.getLogger(__name__) handler = logging.StreamHandler(output) handler.setLevel(logging.DEBUG) formatter = logging.Formatter('%(message)s') handler.setFormatter(formatter) proxy_show_logger.addHandler(handler) proxy_show_logger.propagate = False proxy_show_logger.debug('Using database: %s', conf.database_uri) conn = sqlite_connect(conf.database_uri) conn.row_factory = sqlite3.Row conn.create_function('md5', 1, lambda s: hashlib.md5(str2bytes(s)).hexdigest()) call_table = [ (conf.computers, log_computer_table), (conf.software, log_software_table), (conf.partitions, log_partition_table), (conf.slaves, log_slave_table), (conf.params, log_params), (conf.network, log_network) ] if not any(flag for flag, func in call_table): to_call = [func for flag, func in call_table] else: to_call = [func for flag, func in call_table if flag] for idx, func in enumerate(to_call): func(proxy_show_logger, conn) if idx < len(to_call) - 1: proxy_show_logger.info(' ') if sys.stdout.isatty(): pager = subprocess.Popen( os.getenv('PAGER', 'less --quit-if-one-screen --no-init'), close_fds=True, shell=True, stdin=subprocess.PIPE,) pager.communicate(str2bytes(output.getvalue()))
def setUp(self): super(TestCliProxyShow, self).setUp() self.db_file = tempfile.NamedTemporaryFile(delete=False, suffix='.db') self.db_file.close() self.conf.database_uri = self.db_file.name self.conf.logger = self.logger # load database schema = bytes2str(pkg_resources.resource_string( 'slapos.tests', os.path.join('test_slapproxy', 'database_dump_version_current.sql'))) db = sqlite_connect(self.db_file.name) db.cursor().executescript(schema) db.commit() # by default we simulate being invoked with "show all" arguments self.conf.computers = True self.conf.software = True self.conf.partitions = True self.conf.slaves = True self.conf.params = True self.conf.network = True
def connect_db(): return sqlite_connect(app.config['DATABASE_URI'])
def connectDB(): # if first connection, create an empty db at DATABASE_URI path conn = sqlite_connect(app.config['DATABASE_URI']) conn.close()
def connect(self): self.connection = sqlite_connect(self.uri) self.cursor = self.connection.cursor()