示例#1
0
class PDatabaseFactory:

    def __init__(self, config):
        self.cfg = config
        self.instances = {}
        self.defaut_instance = self.cfg.get('db', 'database')
        self.sqlite_db_path = self.cfg.get('sqlite', 'path')
        self.database_proxy = DatabaseProxy()

    def get_instance(self, instance: str = None):
        if not instance:
            instance = self.defaut_instance
        if instance not in self.instances.keys():
            if instance == 'sqlite':
                instance_obj = CSqliteExtDatabase(self.sqlite_db_path, autoconnect=False)
            elif instance == 'sqlite-app-test':
                PACKAGR_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
                instance_obj = CSqliteExtDatabase(os.path.join(PACKAGR_DIR, 'mediastrends_test.db'))
            elif instance == 'memory':
                instance_obj = CSqliteExtDatabase(':memory:')
            else:
                raise ValueError("Instance %s not defined" % (instance))
            self.instances[instance] = instance_obj

        instance = self.instances[instance]
        self.database_proxy.initialize(instance)

        return instance
示例#2
0
class Database:
	"""Инициализатор базы данных"""
	__database_proxy = DatabaseProxy()

	@classmethod
	def init(cls):
		"""Инициализация базы данных"""
		cfg = Config.get_database_config()
		db_name = cfg.pop('db_name', "mu")
		psql_db = PostgresqlDatabase(db_name, **cfg)
		cls.__database_proxy.initialize(psql_db)

	@classmethod
	def get(cls):
		"""Получение прокси базы данных"""
		return cls.__database_proxy
示例#3
0
class Database:
    """SQL System"""

    __db = DatabaseProxy()
    __migrator = None

    @classmethod
    def setup(cls):
        """sets up the SQL System"""
        mode = CONFIG["database"]["mode"].value.lower()
        if mode == "sqlite3":
            database = SqliteExtDatabase(
                PROGRAMCONFIGLOCATION + "/Tackem.db",
                pragmas={
                    "journal_mode": "wal",
                    "foreign_keys": 0
                },
            )

        elif mode == "mysql":
            database = MySQLDatabase(
                CONFIG["database"]["database"].value,
                user=CONFIG["database"]["username"].value,
                password=CONFIG["database"]["password"].value,
                host=CONFIG["database"]["host"].value,
                port=CONFIG["database"]["port"].value,
            )
        else:
            print(mode)

        cls.__db.initialize(database)

        if mode == "sqlite3":
            cls.__migrator = SqliteMigrator(cls.__db)
        elif mode == "mysql":
            cls.__migrator = MySQLMigrator(cls.__db)
        TableVersion.create_table()

    @classproperty
    def db(cls) -> DatabaseProxy:
        """returns the DB"""
        return cls.__db

    @classproperty
    def migrator(cls) -> SchemaMigrator:
        """returns the migrator"""
        return cls.__migrator
示例#4
0
import pymysql

from peewee import (
    CharField,
    DatabaseProxy,
    DateTimeField,
    Model,
    MySQLDatabase,
    SqliteDatabase,
)

# Create a proxy for the DB.  This is because we don't know what kind of DB
# we are connecting to until runtime.  We can create either a Sqlite DB or
# an RDS DB.  The Sqlite DB is a local DB stored in a file and can be very
# useful for testing.
database_proxy = DatabaseProxy()

db = None


class BaseModel(Model):
    class Meta:
        database = database_proxy


class Schedule(BaseModel):

    instance_id = CharField(max_length=20, unique=True, null=False)
    start_at = DateTimeField(null=False)
    stop_at = DateTimeField(null=False)
示例#5
0
    CompositeKey,
    DatabaseProxy,
    DateTimeField,
    FloatField,
    ForeignKeyField,
    IntegerField,
    Model,
    PostgresqlDatabase,
    TextField,
)

from scripts.pipelines import entity_pipelines

DATABASE_CONFIG = "config.ini"

input_database = DatabaseProxy()
output_database = DatabaseProxy()


class InputModel(Model):  # type: ignore
    """
    Input models are read but never written. These specifications of input models in this file
    only need to define the fields we want to read, and they may not be a complete specification
    of the table (i.e. they might leave out indexes or columns).
    """
    class Meta:
        database = input_database


class Metadata(InputModel):
    metadata_id = TextField()
示例#6
0
文件: database.py 项目: Terrance/IMMP
 class Meta:
     database = DatabaseProxy()
示例#7
0
import os
import logging
import time

from peewee import (DatabaseProxy, SqliteDatabase, MySQLDatabase, Model)
from playhouse.shortcuts import ReconnectMixin

from ..config import get_db_config

LOGGER = logging.getLogger(__name__)

db = DatabaseProxy()


class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
    def __init__(self, *args, **kwargs):
        self.max_attempts = kwargs.pop("max_attempts", None)
        if self.max_attempts:
            self.max_attempts = int(self.max_attempts)

        super().__init__(*args, **kwargs)

    def connect(self, *args, **kwargs):
        delay = 1

        attempt_n = 1

        while True:
            try:
                super().connect(*args, **kwargs)
示例#8
0
文件: db.py 项目: nm17/osmcards
    MySQLMigrator,
    PostgresqlMigrator
)
from peewee import (
    fn,
    DatabaseProxy,
    Model,
    CharField,
    TextField,
    IntegerField,
    ForeignKeyField,
    BooleanField,
    DateTimeField
)

database = DatabaseProxy()


def set_up_logging():
    import logging
    logger = logging.getLogger('peewee')
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.DEBUG)


def init_app(app):
    def open_db():
        database.connect()

    def close_db(exception):
        if not database.is_closed():
示例#9
0
from peewee import Model, CharField, DateTimeField, DatabaseProxy
from datetime import datetime

# NOTE: It may be more conventional to name this file ORM.py

db = DatabaseProxy()  # Set to DatabaseProxy() to enable setting db at runtime

# There are other ways to do this
# http://docs.peewee-orm.com/en/latest/peewee/database.html#setting-the-database-at-run-time


# Base Database model, actual models should inherit this
class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    def __init__(self, *args: str, **kwargs: str):  # PLEASE CHECK
        super(User, self).__init__(*args, **kwargs)
        now = datetime.now()
        self.join_date = now
        self.last_changed = now

    # Should we have an numerical ID field or just use username as key?
    username = CharField(unique=True)
    password = CharField
    email = CharField
    join_date = DateTimeField
    last_changed = DateTimeField  # Last time user was updated
示例#10
0
from peewee import Model, AutoField, CharField, DateTimeField, SqliteDatabase
from peewee import IntegrityError
from peewee import ForeignKeyField, DatabaseProxy, OperationalError
import datetime

database_proxy = DatabaseProxy()


class BaseModel(Model):
    class Meta:
        database = database_proxy


class SimpleTable(BaseModel):
    id = AutoField()
    code = CharField(unique=True, max_length=50)


class JoinTable(BaseModel):
    id = AutoField()
    code = CharField(unique=True, max_length=50)
    creation_time = DateTimeField(default=datetime.datetime.now)
    simple = ForeignKeyField(SimpleTable, backref='joins')


# ==============================================================================
# if app.config['DEBUG']:
#     database = SqliteDatabase('local.db')
# elif app.config['TESTING']:
#     database = SqliteDatabase(':memory:')
# else:
示例#11
0
# -*- coding: utf-8 -*-

# @Time    : 2020/7/15 上午11:55
# @Author  : Hanley
# @File    : base_model.py
# @Desc    :

import datetime

from peewee import (CharField, DateTimeField, Model, IntegerField,
                    SmallIntegerField, DatabaseProxy)
from commons.initlog import logging
from utils.database_util import RetryConnectMysql

_mysql = RetryConnectMysql.connect_mysql()
database_proxy = DatabaseProxy()
database_proxy.initialize(_mysql)


class BaseModel(Model):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    class Meta:
        database = database_proxy


class CommonBase(BaseModel):

    status = SmallIntegerField(null=False,
                               default=1,
示例#12
0
from peewee import fn, DoesNotExist
import time
from elasticsearch import Elasticsearch
from playhouse.shortcuts import model_to_dict
from playhouse.postgres_ext import Match
import six
import math
import zerorpc

logger = get_logger('search project learning')

app = Flask(__name__, static_folder='statics', static_url_path='/static')
api = Api(app)
app.config['MAX_CONTENT_LENGTH'] = 256 * 1024 * 1024
es = Elasticsearch(['es01:9200'])
psql_db = DatabaseProxy()


def json_encoder_default(obj):
    datetime_format = "%Y/%m/%d %H:%M:%S"
    date_format = "%Y/%m/%d"
    time_format = "%H:%M:%S"
    # if isinstance(obj, Decimal):
    #     return str(obj)
    if isinstance(obj, datetime.datetime):
        return obj.strftime(datetime_format)
    if isinstance(obj, datetime.date):
        return obj.strftime(date_format)
    if isinstance(obj, datetime.time):
        return obj.strftime(time_format)
    if isinstance(obj, memoryview):
示例#13
0
文件: main.py 项目: dailing/mask_rcnn
from peewee import PostgresqlDatabase, Model, TextField,\
 BlobField, DateTimeField, IntegerField, IntegrityError,\
 DatabaseProxy
import playhouse.db_url
from playhouse.postgres_ext import JSONField, ArrayField
import datetime
import psycopg2
import math
import base64
import uuid
from peewee import fn
import time

import util.tasks as tasks

psql_db = DatabaseProxy()


class BaseModel(Model):
    """A base model that will use our Postgresql database"""
    class Meta:
        database = psql_db


class ImageStorage(BaseModel):
    md5 = TextField(unique=True)
    payload = BlobField()
    timestamp = DateTimeField(default=datetime.datetime.now)
    session_name = TextField()

    class Meta:
示例#14
0
 def __init__(self, config):
     self.cfg = config
     self.instances = {}
     self.defaut_instance = self.cfg.get('db', 'database')
     self.sqlite_db_path = self.cfg.get('sqlite', 'path')
     self.database_proxy = DatabaseProxy()
示例#15
0
from peewee import DatabaseProxy, Model, CharField, DateTimeField
from playhouse.db_url import connect

database_proxy = DatabaseProxy()  # Create a proxy for our db.


class BaseModel(Model):
    class Meta:
        database = database_proxy  # Use proxy for our DB.


class Forecast(BaseModel):
    weather_type = CharField()
    temperature = CharField()
    date = DateTimeField()


def init_db(url):
    """
    The function to dynamic initialization of the database depending on the transmitted url
    ex.:
    - sqlite:///my_database.db will create a SqliteDatabase
    - postgresql://postgres:my_password@localhost:5432/my_database will create a PostgresqlDatabase instance.
    - mysql://user:passwd@ip:port/my_db will create a MySQLDatabase
    @param url: str
    @return:None
    """
    database = connect(url)
    database_proxy.initialize(database)
    database_proxy.create_tables([Forecast])
示例#16
0
from peewee import ForeignKeyField, CharField, IntegerField, DateTimeField, TextField

# Local imports
from rewardify._util import rarity_filter

from rewardify.password import PasswordField

from rewardify.rarity import Rarity
from rewardify.rarity import RarityField

from rewardify.probability import ProbabilityField

# GENERAL SETUP
# #############

DATABASE_PROXY = DatabaseProxy()


class BaseModel(Model):
    """
    This is the abstract base class for all the models of this program. It defines the connection to a database proxy
    (a placeholder for a database), which can be bound to an actual instance of a database at runtime (when the login
    info to the database has been read from a config file possibly)

    CHANGELOG

    Added 09.06.2019
    """
    class Meta:
        database = DATABASE_PROXY
示例#17
0
from datetime import datetime

from peewee import Model, CharField, DatabaseProxy, ForeignKeyField, BooleanField, DateTimeField, IntegerField, \
    CompositeKey, DateField

from config import config

db_proxy = DatabaseProxy()


class Migration(Model):
    version = IntegerField()

    class Meta:
        database = db_proxy


class TimeSlot(Model):
    start_date_time = DateTimeField()
    length_min = IntegerField()

    class Meta:
        database = db_proxy


class Appointment(Model):
    claim_token = CharField(null=True)
    claimed_at = DateTimeField(null=True)
    booked = BooleanField()
    time_slot = ForeignKeyField(TimeSlot, backref='appointments')
from typing import List
from uuid import uuid4

from peewee import DatabaseProxy, Model, PostgresqlDatabase, SqliteDatabase, TextField, UUIDField
from psycopg2 import connect
from psycopg2.errors import DuplicateDatabase
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

from .configuration import Configuration, EntityStruct, _IN_USER_DIR, _try_dir

DB = DatabaseProxy()


class BaseModel(Model):
    class Meta:
        database = DB


class Entity(BaseModel):
    """Some stub entity"""

    uuid = UUIDField()
    data = TextField()


def _create_psql_database(db_name, user, password, host, port):
    conn = connect(dbname="postgres",
                   user=user,
                   password=password,
                   host=host,
                   port=port)