def __init__(self, config, db_config, web, plugin_path): super().__init__() self.db = db_connect( host = db_config.get(c.DB_HOST_KEY), user = db_config.get(c.DB_USER_KEY) ) self.config = config self.web = web self.plugin_collection = PluginCollection(self, plugin_path) self.token = self.config.get(c.GLOBIBOT_TOKEN_KEY) self.masters = [ str(id) for id in self.config.get(c.MASTER_IDS_KEY, []) ] self.enabled_servers = [ str(id) for id in self.config.get(c.ENABLED_SERVERS_KEY, []) ] self.web.add_routes('bot', *api.routes(self))
def quick_cursor(dsn, cursor_factory=None): conn = db_connect(dsn) if cursor_factory is not None: cur = conn.cursor(cursor_factory=cursor_factory) else: cur = conn.cursor() try: yield cur finally: conn.close()
def connect(dbname, host=None, port=None, user=None, password=None): connect_string = "dbname={}".format(dbname) if host is not None: connect_string += " host={}".format(host) if port is not None: connect_string += " port={}".format(port) if user is not None: connect_string += " user={}".format(user) if password is not None: connect_string += " password={}".format(password) try: # print connect_string db = db_connect(connect_string) cursor = db.cursor() return db, cursor except: print "Something wrong with database connection"
def connect(self): """make a connection to pgsql raises ConnectionError if fails""" self.close() error = None try: self.connection = db_connect(self.dsn) except: ## We can work without the db connection and call it when needed t, val, tb = exc_info() del t, tb error = format_msg(val) if error is not None: self.LOG(E_ERR, self._prefix + 'connection to database failed: ' + error) raise ConnectionError, error self.connection.set_isolation_level(0) self.cursor = self.connection.cursor() self.LOG(E_TRACE, self._prefix + 'I\'ve got a cursor from the driver')
def __init__(self, config, db_config, web, plugin_path): super().__init__() self.db = db_connect(host=db_config.get(c.DB_HOST_KEY), user=db_config.get(c.DB_USER_KEY)) self.config = config self.web = web self.plugin_collection = PluginCollection(self, plugin_path) self.token = self.config.get(c.GLOBIBOT_TOKEN_KEY) self.masters = [ str(id) for id in self.config.get(c.MASTER_IDS_KEY, []) ] # self.enabled_servers = [ # str(id) for id in # self.config.get(c.ENABLED_SERVERS_KEY, []) # ] self.web.add_routes('bot', *api.routes(self))
def get_raw_connection(dsn: str): conn = db_connect(dsn) conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) return conn
def pg_connection(conf: Any) -> Any: dbname = conf.db_name host = conf.db_host user = conf.db_user pswd = conf.db_pass return db_connect(host=host, dbname=dbname, user=user, password=pswd)
#!/usr/bin/env python3 import subprocess, os, time, threading, json from Config import Config from psycopg2 import connect as db_connect config = Config() db_conn = db_connect( "host='%s' user='******' password='******' dbname='%s'" % (config.settings['db_host'], config.settings['db_username'], config.settings['db_password'], config.settings['db_database'])) db_conn.autocommit = True db_cur = db_conn.cursor() values = [] from flask import Flask, Response from flask import render_template app = Flask(__name__) opts = app.jinja_options.copy() opts.update(dict(variable_start_string='^^', variable_end_string='^^')) # this de-confuses flask template replacement with vue's app.jinja_options = opts app.debug = True @app.route('/') def index():