示例#1
0
def database_connector_wrapper(**kwargs):
    """
    Create db instance from params.

    If required params are not provided, try to get them from env vars.
    """
    required_params_mapping = {
        'database': 'POSTGRES_DB',
        'user': '******',
        'password': '******',
        'host': 'POSTGRES_HOST',
        'port': 'POSTGRES_PORT'
    }

    default_params = {'autocommit': True, 'autorollback': True}

    missing = set(required_params_mapping.keys()).difference(kwargs.keys())
    invalid_params = set(kwargs.keys()).difference(
        required_params_mapping.keys())

    # check provided params are enough to instantiate db connection
    for param in missing:
        kwargs[param] = os.environ[required_params_mapping[param]]

    # remove unsupported options
    [kwargs.pop(param) for param in invalid_params]

    kwargs.update(default_params)

    return peewee.PostgresqlDatabase(**kwargs)
def storage_psql():
    # TODO: Move database operations to StorageManager
    test_db_name = 'test_' + PG_DATABASE
    with psycopg2.connect(user=PG_USER, password=PG_PASSWORD,
                          host=PG_HOST) as conn:
        conn.set_isolation_level(
            psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        with conn.cursor() as cursor:
            cursor.execute(f'DROP DATABASE IF EXISTS {test_db_name}')
            cursor.execute(f'CREATE DATABASE {test_db_name}')

    psql_db = peewee.PostgresqlDatabase(
        database=test_db_name,
        user=PG_USER,
        password=PG_PASSWORD,
        host=PG_HOST,
        port=PG_PORT,
        isolation_level=psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)

    storage = StorageManager(psql_db)
    yield storage
    storage.clean_db()

    with psycopg2.connect(user=PG_USER, password=PG_PASSWORD,
                          host=PG_HOST) as conn:
        conn.set_isolation_level(
            psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
        with conn.cursor() as cursor:
            cursor.execute(f'DROP DATABASE {test_db_name}')
示例#3
0
    def get_database(self):
        """Returns the correct database driver

        Returns:
            :obj:`pw.Database`
        Raises:
            AttributeError: if `--username` or `--password` not passed
                (if `--dbtype [postgres|mysql]`)

        """
        if self.get('dbtype') == 'sqlite':
            return pw.SqliteDatabase(self.get('dbname'))
        else:
            if self.get('username') is None or self.get('password') is None:
                raise AttributeError('[ERROR] dbtype %s requires username and'
                                     ' password.\n' % str(self.get('dbtype')))
            if self.get('hostname') is None:
                self.set('hostname', 'localhost')
            if self.get('dbtype') == 'mysql':
                if self.get('port') is None or self.get('port') == '':
                    self.set('port', str(3306))
                return pw.MySQLDatabase(self.get('dbname'),
                                        user=self.get('username'),
                                        password=self.get('password'),
                                        host=self.get('hostname'),
                                        port=int(self.get('port')))
            elif self.get('dbtype') == 'postgres':
                if self.get('port') is None or self.get('port') == '':
                    self.set('port', str(5432))
                return pw.PostgresqlDatabase(self.get('dbname'),
                                             user=self.get('username'),
                                             password=self.get('password'),
                                             host=self.get('hostname'),
                                             port=int(self.get('port')))
示例#4
0
    def __init__(self,
                 db_name,
                 uname="test",
                 pwd="test",
                 host="localhost",
                 debug=False):
        if debug:
            self.db = peewee.SqliteDatabase("testing.db")
        else:
            self.db = peewee.PostgresqlDatabase(db_name,
                                                user=uname,
                                                password=pwd,
                                                host=host)

        # This is taken mostly from the Peewee sample app
        class BaseModel(peewee.Model):
            class Meta:
                database = self.db

        class Question(BaseModel):
            body = peewee.TextField(unique=True)
            last_date = peewee.DateField(null=True)
            company = peewee.TextField()
            data_structure = peewee.TextField()
            leetcode = peewee.CharField(max_length=2083, null=True)

        class Admin(BaseModel):
            user_id = peewee.CharField(max_length=19, unique=True)

        self.Question = Question
        self.Admin = Admin
示例#5
0
文件: cli.py 项目: glensc/tracboat
def migrate(ctx, usermap, usermap_file, fallback_user, trac_uri, ssl_verify,
            gitlab_project_name, gitlab_db_user, gitlab_db_password, gitlab_db_name,
            gitlab_db_path, gitlab_uploads_path, gitlab_version, wiki_path,
            from_export_file, mock, mock_path):
    '''migrate a Trac instance'''
    LOG = logging.getLogger(ctx.info_name)
    # 0. Build usermap
    umap = {}
    config_file = ctx.obj.get('config-file', None)
    if config_file:
        umap.update(toml.load(config_file)['usermap'])
    for mapfile in usermap_file:
        umap.update(toml.load(mapfile)['usermap'])
    umap.update({m[0]: m[1] for m in usermap})
    # 1. Retrieve trac project
    if from_export_file:
        LOG.info('loading Trac instance from export file: %s', from_export_file)
        fmt = _detect_format(from_export_file)
        LOG.debug('detected file format: %s', fmt)
        with open(from_export_file, 'r') as f:
            content = f.read()
        project = _loads(content, format=fmt)
    else:
        LOG.info('crawling Trac instance: %s', sanitize_url(trac_uri))
        source = trac.connect(trac_uri, encoding='UTF-8', use_datetime=True,
                              ssl_verify=ssl_verify)
        project = trac.project_get(source, collect_authors=True)
    # 2. Connect to database
    if mock:
        LOG.info('migrating Trac project to mock GitLab')
        mock_path = path.abspath(path.join(mock_path, gitlab_project_name))
        db_connector = peewee.SqliteDatabase(path.join(mock_path, 'gitlab', 'database.sqlite3'))
        gitlab_uploads_path = path.join(mock_path, 'gitlab', 'uploads')
        wiki_path = path.join(mock_path, 'wiki')
    else:
        LOG.info('migrating Trac project to GitLab')
        db_connector = \
            peewee.PostgresqlDatabase(gitlab_db_name, user=gitlab_db_user,
                                      password=gitlab_db_password,
                                      host=gitlab_db_path)
    # 3. Migrate
    LOG.debug('Trac: %s', sanitize_url(trac_uri))
    LOG.debug('GitLab project: %s', gitlab_project_name)
    LOG.debug('GitLab version: %s', gitlab_version)
    LOG.debug('GitLab db path: %s', gitlab_db_path)
    LOG.debug('GitLab db name: %s', gitlab_db_name)
    LOG.debug('GitLab uploads: %s', gitlab_uploads_path)
    LOG.debug('GitLab fallback user: %s', fallback_user)
    migrate_trac.migrate(
        trac=project,
        gitlab_project_name=gitlab_project_name,
        gitlab_version=gitlab_version,
        gitlab_db_connector=db_connector,
        output_wiki_path=wiki_path,
        output_uploads_path=gitlab_uploads_path,
        gitlab_fallback_user=fallback_user,
        usermap=umap
    )
    LOG.info('migration done.')
示例#6
0
def con_db():
    dbhandle = peewee.PostgresqlDatabase(
        "online_store",
        user="******",
        password="******",
        #host="onlinestore_postgres_1"
        host="localhost")
    return dbhandle
示例#7
0
def init_db_connection(*args, **kwargs):
    db = peewee.PostgresqlDatabase(
        "postgres",
        user="******",
        password=os.getenv("POSTGRES_PASSWORD"),
        host="bot_postgres",
        port=5432,
    )
    database_proxy.initialize(db)
示例#8
0
    def POSTGRES(env):
        """
        Return a peewee database handler with the given user configuration.

        """
        if env.POSTGRES_HOST is None:
            return peewee.SqliteDatabase(None)
        else:
            return peewee.PostgresqlDatabase(None)
示例#9
0
 def connection(self, database=None):
     database = (database or self.database)
     connect = peewee.PostgresqlDatabase(
         database,
         host = self.host,
         port = self.port,
         user = self.user,
         password = self.password
     )
     return connect
示例#10
0
def main2(args):

    db = peewee.PostgresqlDatabase(
        'postgres',
        user=args.username,
        password=args.password,
        host=args.host, port=args.port
    )
    database_proxy.initialize(db)
    print(db)
示例#11
0
def connect():
    # if 'HEROKU' in os.environ:
    parse.uses_netloc.append('postgres')
    url = parse.urlparse(os.environ['DATABASE_URL'])
    return pw.PostgresqlDatabase(database=url.path[1:],
                                 user=url.username,
                                 password=url.password,
                                 host=url.hostname,
                                 port=url.port,
                                 autorollback=True)
示例#12
0
def give_data():
    type = request.values['type']
    database = peewee.PostgresqlDatabase('instagram',
                                         user='******',
                                         password='******',
                                         host='localhost')
    dates = []
    values = []
    bg_colors = []
    border_colors = []
    if type == 'profile_statistic':
        for item in reversed(Profile.select().order_by(
                Profile.id.desc()).limit(10)):
            dates.append(item.date)
            values.append(item.count)
            border_colors.append('rgba(255,99,132,1)')
            bg_colors.append('rgba(54, 162, 235, 0.2)')
        response = {
            'success': 'OK',
            'dates': dates,
            'values': values,
            'bg_colors': bg_colors,
            "border_colors": border_colors
        }
    elif type == 'unfollow_statistic':
        for item in reversed(Friendship.select().order_by(
                Friendship.id.desc()).limit(10)):
            dates.append(item.date)
            values.append(item.unfollowed_counter)
            border_colors.append('rgba(255,99,132,1)')
            bg_colors.append('rgba(54, 162, 235, 0.2)')
        response = {
            'success': 'OK',
            'dates': dates,
            'values': values,
            'bg_colors': bg_colors,
            "border_colors": border_colors
        }
    elif type == 'follow_statistic':
        for item in reversed(Friendship.select().order_by(
                Friendship.id.desc()).limit(10)):
            dates.append(item.date)
            values.append(item.followed_counter)
            border_colors.append('rgba(255,99,132,1)')
            bg_colors.append('rgba(54, 162, 235, 0.2)')
        response = {
            'success': 'OK',
            'dates': dates,
            'values': values,
            'bg_colors': bg_colors,
            "border_colors": border_colors
        }
    else:
        response = {'success': 'Not OK :('}
    return jsonify(response)
示例#13
0
def init_db(settings: Settings = None):
    if not settings:
        settings = Settings()

    db.initialize(
        peewee.PostgresqlDatabase(
            settings.database_name,
            user=settings.database_user,
            host=settings.database_host,
            password=settings.database_password,
        ))
    db.obj._state = PeeweeConnectionState()
示例#14
0
def _create_postgres_database(config: Dict[str, str]) -> pw.Database:
    try:
        return pw.PostgresqlDatabase(
            database=config['Database'],
            user=config.get('Username'),
            password=config.get('Password'),
            host=config.get('Host'),
            port=int(config.get('Port')),
        )
    except KeyError:
        raise KeyError(
            'You need to specify database name in \'Database\' parameter')
示例#15
0
 def _init_postgres(self):
     """Initializes PostgreSQL database."""
     db_name = self.config.get('db_name', 'tiny_pacs_db')
     host = self.config.get('host', 'localhost')
     port = self.config.get('port', 'port')
     user = self.config.get('user', 'postgres')
     password = self.config.get('password', 'postgres')
     self.log_info('Initializing PostgreSQL database with parameters: %s, %d %s',
                   host, port, user)
     DB.initialize(peewee.PostgresqlDatabase(
         db_name, host=host, port=port, user=user, password=password
     ))
示例#16
0
    def __init__(self):
        """
		Создает объект ShopDatabase.
		Args:
			config_name (str): имя конфига с параметрами подключения
		"""
        config = configparser.ConfigParser()
        config.read('config.ini')
        connection_params = config['Connection']
        database_name = connection_params['database']
        del connection_params['database']

        self.db = pw.PostgresqlDatabase(database_name, **connection_params)
示例#17
0
 def init_db_connection(credentials):
     print("Attempting to connect to DB")
     try:
         db = pw.PostgresqlDatabase(credentials['dbName'],
                                    host=credentials['host'],
                                    port=credentials['port'],
                                    user=credentials['username'],
                                    password=credentials['password'])
         #connect_timeout=2)
         print("Connected to DB successfully")
         return db
     except Exception as e:
         raise e
示例#18
0
def load_db():
    db_proxy = peewee.Proxy()
    if 'HEROKU' in os.environ:
        from six.moves.urllib.parse import urlparse, uses_netloc
        uses_netloc.append('postgres')
        url = urlparse(os.environ["DATABASE_URL"])
        db_kwargs = {'database': url.path[1:], 'user': url.username, 'password': url.password,
                     'host': url.hostname, 'port': url.port}
        db = peewee.PostgresqlDatabase(**db_kwargs)
    else:
        default_pragmas = {'journal_mode': 'wal', 'foreign_keys': 1, 'ignore_check_constraints': 0}
        db = peewee.SqliteDatabase(conf.DB_PATH, pragmas=default_pragmas)

    db_proxy.initialize(db)
    return db, db_proxy
示例#19
0
class Conexion():
    DB_NAME = os.getenv("DB_NAME")
    DB_HOST = os.getenv("DB_HOST")
    DB_PORT = os.getenv("DB_PORT")
    DB_USER = os.getenv("DB_USER")
    DB_PASS = os.getenv("DB_PASS")
    DB_SCHEMA = os.getenv("DB_SCHEMA")

    database = pw.PostgresqlDatabase(database=DB_NAME,
                                     host=DB_HOST,
                                     port=DB_PORT,
                                     user=DB_USER,
                                     password=DB_PASS)
    schema = DB_SCHEMA
    pw = pw
示例#20
0
def init_db():
    """Initializes database."""

    user = '******'
    password = '******'
    dbname = 'license_gen'
    host = '127.0.0.1'
    port = 5432
    db = pw.PostgresqlDatabase(dbname,
                               user=user,
                               password=password,
                               sslmode='disable',
                               host=host,
                               port=port)

    database_proxy.initialize(db)
示例#21
0
 def _connect_db(self):
     if self.db_settings is None:
         raise ValueError("DATABASE is not set")
     db_params = self.db_settings.get("params")
     db_engine = self.db_settings.get("engine")
     if db_engine.lower() == "sqlite":
         adapter = self.db_settings.get("adapter", "sqlite3")
         self.db = peewee.SqliteDatabase(**db_params)
     elif db_engine.lower() == "mysql":
         adapter = self.db_settings.get("adapter", "pymysql")
         self.db = peewee.MySQLDatabase(**db_params)
     elif db_engine.lower() == "postgresql":
         adapter = self.db_settings.get("adapter", "psycopg2")
         self.db = peewee.PostgresqlDatabase(**db_params)
     else:
         raise ValueError("DATABASE engine is not supported")
     self.db_pool = adbapi.ConnectionPool(adapter, **db_params)
示例#22
0
def get_connector(_cache=[]):

    if len(_cache) > 0:
        return _cache[0]

    database_url = os.environ.get('DATABASE_URL')
    urlp = urllib.parse.urlparse(database_url)

    database = peewee.PostgresqlDatabase(urlp.path[1:],
                                         host=urlp.hostname,
                                         user=urlp.username,
                                         password=urlp.password,
                                         port=urlp.port)
    database.connect()

    _cache.append(database)
    return database
示例#23
0
    def __init__(self, db_name, uname="test", pwd="test", host="localhost"):
        self.db = peewee.PostgresqlDatabase(db_name,
                                            user=uname,
                                            password=pwd,
                                            host=host)

        # This is taken mostly from the Peewee sample app
        class BaseModel(peewee.Model):
            class Meta:
                database = self.db

        class Resume(BaseModel):
            user_id = peewee.TextField(unique=True)
            resume = peewee.TextField()
            id = peewee.AutoField()

        self.Resume = Resume
    def connect(self, conn_dict={}, try_connect=True, autocommit=True):
        env = Environment()
        CDB = ConfigDB(env)

        if not conn_dict:
            conn_dict = CDB.read().get('database')

        # Se o banco for SQLite
        ''''
		if str(conn_dict['banco']) == '1':
			sqlite_db = peewee.SqliteDatabase("%s.db" % conn_dict['database'])
			if try_connect:
				try:
					sqlite_db.connect()
					sqlite_db.close()
					return sqlite_db
				except:
					sqlite_db.close()
					return False
		'''
        if str(conn_dict['banco']) == '0':
            psql_db = peewee.PostgresqlDatabase(
                conn_dict.get('dbname') or conn_dict.get('database'),
                user=conn_dict['user'],
                password=conn_dict['password'],
                host=conn_dict['host'],
                port=conn_dict['port'],
                autocommit=autocommit,
                autorollback=autocommit,
            )

            if try_connect:
                try:
                    psql_db.connect()
                except:
                    a = QApplication(sys.argv)
                    msg = QMessageBox()
                    msg.setIcon(QMessageBox.Information)
                    msg.setWindowTitle("Informação")
                    msg.setText(
                        "Não foi possível conectar com o banco de dados!\nPor favor verifique suas configurações de conexão."
                    )
                    sys.exit(msg.exec_())
            return psql_db
示例#25
0
def get_db():
    import peewee
    if  "PRODUCTION" in os.environ:
        DB_NAME = os.environ["DB_NAME"]
        DB_USER = os.environ["DB_USER"]
        DB_PASSWORD = os.environ["DB_PASSWORD"]
        DB_HOST = os.environ["DB_HOST"]
        DB_PORT = int(os.environ["DB_PORT"])
        return peewee.PostgresqlDatabase(
            DB_NAME, user=DB_USER, password=DB_PASSWORD,
            host=DB_HOST, port=DB_PORT
        )
    else:
        return peewee.SqliteDatabase(
            "./db.sqlite3", pragmas={
                'journal_mode': 'wal',
                'cache_size': -1024 * 64
            }
        )
def initialize_database_manager():
    """Initialize the database connection."""
    global reminder

    # Check if we are in production or in local testing
    if HEROKU_PG_DATABASE_URL:
        # Obtain the database url and split it into necesary parts
        parse.uses_netloc.append('postgres')
        db_url = parse.urlparse(HEROKU_PG_DATABASE_URL)

        # Create the database object
        database = peewee.PostgresqlDatabase(
            db_url.path[1:],
            user=db_url.username,
            password=db_url.password,
            host=db_url.hostname,
            port=db_url.port)
    else:
        with open(TESTING_VARS['REMINDER_DATA_FILE'], 'r') as f:
            reminder_test_data = json.loads(f.read()).get('event_reminder')

        test_data = [(k['event_id'], k['reminder_datetime'])
                     for k in reminder_test_data]

        # Createconnection and connect
        conn = sqlite3.connect(TESTING_VARS['DB_NAME'])
        c = conn.cursor()
        # Create table
        c.execute('DROP TABLE IF EXISTS \'event_reminder\'')
        c.execute('CREATE TABLE event_reminder(id INTEGER PRIMARY KEY, \
            event_id varchar(255) NOT NULL UNIQUE,reminder_datetime datetime \
            NOT NULL);')
        # Insert the test data into the database
        c.executemany('INSERT INTO event_reminder (event_id, \
            reminder_datetime) VALUES (?, ?)', test_data)
        # Save (commit) the changes and close
        conn.commit()
        conn.close()
        database = peewee.SqliteDatabase(TESTING_VARS['DB_NAME'])

    # Create the reminder object and initialize it with the database
    reminder = Reminder()
    reminder.initialize(database)
示例#27
0
def create_app(application=None, default_settings='cajitos_site.settings'):
    application = Flask(__name__, instance_relative_config=True)

    application.config.from_object(default_settings)

    with application.app_context():

        configure_app(application)

    bcrypt.init_app(application)
    login_manager.init_app(application)
    mail.init_app(application)
    moment.init_app(application)
    bootstrap = Bootstrap(application)
    babel.init_app(application)

    if application.config['TESTING']:
        db.initialize(pw.SqliteDatabase(**application.config['DATABASE']))
    else:
        db.initialize(pw.PostgresqlDatabase(**application.config['DATABASE']))
    #  TODO optimize registering blueprints
    from cajitos_site.users.routes import users
    from cajitos_site.bar.routes import bar
    from cajitos_site.blog.routes import blog
    from cajitos_site.misc.routes import misc
    from cajitos_site.errors.routes import errors
    from cajitos_site.service.routes import service
    application.register_blueprint(users)
    application.register_blueprint(blog)
    application.register_blueprint(misc)
    application.register_blueprint(errors)
    application.register_blueprint(service)
    application.register_blueprint(bar)

    # Register models
    from . import models as models
    application.models = models

    application.logger.info('App created')

    return application
示例#28
0
    def __call__(self, *args, **kwargs) -> Iterator[ExportContext]:
        self.logger.info("Exportation data")

        self.export: "PostgresExportPolicy" = self.model_templating(
            *args, model=self.notebook.export)

        db = peewee.PostgresqlDatabase(
            database=self.export.database,
            user=self.export.user,
            password=self.export.password,
            host=self.export.host,
            port=self.export.port,
        )
        db.connect()

        for sql in self.export.sql_before:
            db.execute_sql(sql)

        if self.export.sql is None:
            query = "SELECT {columns}\nFROM {table}{where}{order_by}".format(
                columns=",".join(self.export.columns),
                table=self.export.table,
                where=self.export.where,
                order_by=self.export.order_by,
            )
        else:
            query = self.export.sql
        cursor = db.execute_sql(query)
        d = list(cursor.fetchall())
        for chunk in chunker(
                d,
                size=self.export.chunk_size,
        ):
            yield ExportContext(
                columns=self.export.columns,
                data=chunk,
                data_orient=DataOrient.values,
            )

        for sql in self.export.sql_after:
            db.execute_sql(sql)
示例#29
0
    def Conn(self):
        # confs = DbConfig.select()
        leDbHost = DbConfig().get(main_key='setting', minor_key='leDbHost')
        leDbPort = DbConfig().get(main_key='setting', minor_key='leDbPort')
        leDbName = DbConfig().get(main_key='setting', minor_key='leDbName')
        leDbUsername = DbConfig().get(main_key='setting', minor_key='leDbUsername')
        leDbPassword = DbConfig().get(main_key='setting', minor_key='leDbPassword')

        try:
            conn = peewee.PostgresqlDatabase(
                leDbName.value
                , host=leDbHost.value
                , port=int(leDbPort.value)
                , user=leDbUsername.value
                , password=leDbPassword.value
                , encoding='utf8'
            )
            return conn
        except Exception as e:
            print(e)

        pass
示例#30
0
def init_db():
    driver = get_config('DATABASE', 'driver')
    config = get_config(driver)

    if driver == 'POSTGRESQL':
        db = peewee.PostgresqlDatabase(config['database'],
                                       user=config['username'],
                                       host=config['host'],
                                       password=config['password'],
                                       port=int(config['port']))
    elif driver == 'SQLITE':
        from playhouse.sqlite_ext import SqliteExtDatabase
        db = SqliteExtDatabase(
            os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         config['database']))
    elif driver == 'MYSQL':
        db = peewee.MySQLDatabase(config['database'],
                                  user=config['username'],
                                  host=config['host'],
                                  password=config['password'],
                                  port=int(config['port']))
    else:
        raise peewee.NotSupportedError
    return db