Exemple #1
0
    def test_setdefault(self):
        dsn = 'scheme://*****:*****@host/foo'
        r = dsnparse.parse(dsn)
        self.assertEqual(None, r.port)

        r.setdefault('port', 1234)
        self.assertEqual(1234, r.port)

        r = dsnparse.parse(dsn, port=1235)
        self.assertEqual(1235, r.port)
Exemple #2
0
    def test_setdefault(self):
        dsn = 'scheme://*****:*****@host/foo'
        r = dsnparse.parse(dsn)
        self.assertEqual(None, r.port)

        r.setdefault('port', 1234)
        self.assertEqual(1234, r.port)

        r = dsnparse.parse(dsn, port=1235)
        self.assertEqual(1235, r.port)
Exemple #3
0
    def test_database(self):
        dsn = "sqlite:///the/path"
        r = dsnparse.parse(dsn)
        self.assertEqual("/the/path", r.database)
        self.assertEqual("/the/path", r.dbname)

        dsn = "postgresql://*****:*****@host:1234/dbname"
        r = dsnparse.parse(dsn)
        self.assertEqual("dbname", r.database)
        self.assertEqual("dbname", r.dbname)

        dsn = "postgresql://*****:*****@host:1234/dbname/"
        r = dsnparse.parse(dsn)
        self.assertEqual("dbname", r.dbname)
Exemple #4
0
    def test_database(self):
        dsn = "sqlite:///the/path"
        r = dsnparse.parse(dsn)
        self.assertEqual("/the/path", r.database)
        self.assertEqual("/the/path", r.dbname)

        dsn = "postgresql://*****:*****@host:1234/dbname"
        r = dsnparse.parse(dsn)
        self.assertEqual("dbname", r.database)
        self.assertEqual("dbname", r.dbname)

        dsn = "postgresql://*****:*****@host:1234/dbname/"
        r = dsnparse.parse(dsn)
        self.assertEqual("dbname", r.dbname)
Exemple #5
0
    def test_username_password(self):
        dsn = "scheme://*****:*****@"
        r = dsnparse.parse(dsn)
        self.assertEqual("foo", r.username)
        self.assertEqual("bar+che/baz", r.password)

        dsn = "scheme://example.com/?username=foo2&password=che-baz"
        r = dsnparse.parse(dsn)
        self.assertEqual("foo2", r.username)
        self.assertEqual("che-baz", r.password)

        dsn = "scheme://*****:*****@example.com/?username=foo3&password=bar3"
        with self.assertRaises(ValueError):
            r = dsnparse.parse(dsn)
Exemple #6
0
def configure(dsn):
    """
    configure an interface to be used to query a backend

    you use this function to configure an Interface using a dsn, then you can get
    that interface using the get_interface() method

    example dsn -- common.cache.RedisInterface://localhost:6379/0?timeout=5

    dsn -- string -- a properly formatted prom dsn, see DsnConnection for how to format the dsn
    """
    global interfaces

    c = dsnparse.parse(dsn)
    assert c.fragment not in interfaces, 'a connection named "{}" has already been configured'.format(c.name)

    # compensate for passing pw as username (eg, not doing //:password@ but instead //password@)
    password = None
    if c.password:
        password = c.password
    elif c.username:
        password = c.username

    interface_module_name, interface_class_name = c.scheme.rsplit('.', 1)
    interface_module = importlib.import_module(interface_module_name)
    interface_class = getattr(interface_module, interface_class_name)

    i = interface_class(host=c.host, port=c.port, db=c.paths[0], password=password, **c.query)
    set_interface(i, c.fragment)
    return i
Exemple #7
0
def create_engine(db_uri, print_sql, auto_create):
    dsn = dsnparse.parse(db_uri)
    if len(dsn.paths) != 1:
        raise ValueError("Bad value for uri: %s" % db_uri)
    path ,= dsn.paths
    if len(dsn.schemes) > 1:
        raise NotImplementedError("Preql doesn't support multiple schemes")
    scheme ,= dsn.schemes
    if scheme == 'sqlite':
        if not auto_create and path != ':memory:':
            if not Path(path).exists():
                raise ConnectError("File %r doesn't exist. To create it, set auto_create to True" % path)
        return SqliteInterface(path, print_sql=print_sql)
    elif scheme == 'postgres':
        return PostgresInterface(dsn.host, dsn.port, path, dsn.user, dsn.password, print_sql=print_sql)
    elif scheme == 'mysql':
        return MysqlInterface(dsn.host, dsn.port, path, dsn.user, dsn.password, print_sql=print_sql)
    elif scheme == 'git':
        return GitInterface(path, print_sql=print_sql)
    elif scheme == 'duck':
        return DuckInterface(path, print_sql=print_sql)
    elif scheme == 'bigquery':
        return BigQueryInterface(path, print_sql=print_sql)

    raise NotImplementedError(f"Scheme {dsn.scheme} currently not supported")
Exemple #8
0
    def test_parse_custom_class(self):
        class CustomParseResult(dsnparse.ParseResult):
            pass

        dsn = 'scheme://*****:*****@host:1234/bar/che?option1=opt_val1#anchor'
        r = dsnparse.parse(dsn, parse_class=CustomParseResult)
        self.assertTrue(isinstance(r, CustomParseResult))
Exemple #9
0
    def test_parse_custom_class(self):
        class CustomParseResult(dsnparse.ParseResult):
            pass

        dsn = 'scheme://*****:*****@host:1234/bar/che?option1=opt_val1#anchor'
        r = dsnparse.parse(dsn, parse_class=CustomParseResult)
        self.assertTrue(isinstance(r, CustomParseResult))
Exemple #10
0
def init_context(app_config_prefix_name, dsn=None, read_only=False):
    READ_SOURCE = ''
    DEVELOP_MODE = True
    name = app_config_prefix_name

    config = get_by_env_prefix(name)
    DEVELOP_MODE = config.get('DEBUG')
    MYSQL_DSN = dsn or config.get('MYSQL_DSN')

    dsn = dsnparse.parse(MYSQL_DSN)
    host = dsn.host
    port = dsn.port or 3306
    username = dsn.username or 'root'
    password = dsn.password or ''
    database = dsn.paths[0]

    assert host
    assert database

    DB_READ_ONLY = config.get('MQSQL_READ_ONLY') or read_only
    READ_SOURCE = 'envcfg'

    return SQLContext(APP=app_config_prefix_name,
                      DEVELOP_MODE=DEVELOP_MODE,
                      DB_HOST=host,
                      DB_PORT=port,
                      DB_USER=username,
                      DB_PASSWD=password,
                      DB_NAME=database,
                      READ_SOURCE=READ_SOURCE,
                      DB_READ_ONLY=DB_READ_ONLY)
Exemple #11
0
def create_engine(db_uri, print_sql):
    dsn = dsnparse.parse(db_uri)
    if len(dsn.paths) != 1:
        raise ValueError("Bad value for uri: %s" % db_uri)
    path, = dsn.paths
    if len(dsn.schemes) > 1:
        raise NotImplementedError("Preql doesn't support multiple schemes")
    scheme, = dsn.schemes
    if scheme == 'sqlite':
        return SqliteInterface(path, print_sql=print_sql)
    elif scheme == 'postgres':
        return PostgresInterface(dsn.host,
                                 dsn.port,
                                 path,
                                 dsn.user,
                                 dsn.password,
                                 print_sql=print_sql)
    elif scheme == 'mysql':
        return MysqlInterface(dsn.host,
                              dsn.port,
                              path,
                              dsn.user,
                              dsn.password,
                              print_sql=print_sql)
    elif scheme == 'git':
        return GitInterface(path, print_sql=print_sql)
    elif scheme == 'duck':
        return DuckInterface(path, print_sql=print_sql)

    raise NotImplementedError(f"Scheme {dsn.scheme} currently not supported")
Exemple #12
0
    def __init__(self, dsn):

        d = {'options': {}, 'hosts': []}
        p = dsnparse.parse(dsn)

        # get the scheme, which is actually our interface_name
        d['interface_name'] = p.scheme

        dsn_hosts = []
        if p.hostname:
            d['hosts'].append((p.hostname, getattr(p, 'port', None)))

        if p.query:
            d['options'] = p.query

        if p.username:
            d['username'] = p.username

        if p.password:
            d['password'] = p.password

        if p.fragment:
            d['name'] = p.fragment

        super(DsnConnection, self).__init__(**d)
Exemple #13
0
    def database():
        rtn = OrderedDict()

        dsn = config("findex:database:connection")
        rtn["dsn (RFC-1738)"] = _item(data=dsn, cls="info")

        encoding = DatabaseStatus.raw_query("SHOW SERVER_ENCODING", cls="ok")
        if "UTF8" not in encoding.data:
            encoding.cls = "warning"
        rtn["encoding"] = encoding

        dsn_parsed = dsnparse.parse(dsn)
        dsn_blob = {
            "user": dsn_parsed.username,
            "pass": dsn_parsed.password,
            "host": dsn_parsed.host,
            "port":
            5432 if not isinstance(dsn_parsed.port, int) else dsn_parsed.port,
            "db": dsn_parsed.paths[0]
        }

        for k, v in dsn_blob.items():
            rtn["db_%s" % k] = _item(data=v, cls="ok")

        rtn["Size on Disk"] = DatabaseStatus.get_size()
        return rtn
Exemple #14
0
    def __init__(self, dsn):

        d = {'options': {}, 'hosts': []}
        p = dsnparse.parse(dsn)

        # get the scheme, which is actually our interface_name
        d['interface_name'] = p.scheme

        dsn_hosts = []
        if p.hostname:
            d['hosts'].append((p.hostname, getattr(p, 'port', None)))

        if p.query:
            d['options'] = p.query

        if p.username:
            d['username'] = p.username

        if p.password:
            d['password'] = p.password

        if p.fragment:
            d['name'] = p.fragment

        super(DsnConnection, self).__init__(**d)
Exemple #15
0
 def test___getitem__(self):
     dsn = 'scheme://*****:*****@host:1234/foo'
     r = dsnparse.parse(dsn)
     self.assertEqual('scheme', r[0])
     self.assertEqual('username:password@host:1234', r[1])
     self.assertEqual('/foo', r[2])
     self.assertEqual('', r[3])
     self.assertEqual({}, r[4])
     self.assertEqual('', r[5])
Exemple #16
0
def parse_db_uri(db_uri: Text) -> None:
    """ parse line of """
    db = dsnparse.parse(db_uri)
    os.environ["SANIC_DB_HOST"] = db.host
    os.environ["SANIC_DB_DATABASE"] = db.database
    os.environ["SANIC_DB_USER"] = db.user
    if db.port:
        os.environ["SANIC_DB_PORT"] = str(db.port)
    os.environ["SANIC_DB_PASSWORD"] = db.password
Exemple #17
0
    def __init__(self, **kwargs):
        self.dsn = kwargs.get("dsn", None)
        self.table_name = kwargs.get("table_name")
        self.control_table_name = kwargs.get("control_table_name")

        # get db name
        info = dsnparse.parse(self.dsn)
        self.db_name = info.paths.pop() if len(info.paths) == 1 else ""
        assert self.db_name != "", "No database name provided in DSN connection string"
Exemple #18
0
 def test___getitem__(self):
     dsn = 'scheme://*****:*****@host:1234/foo'
     r = dsnparse.parse(dsn)
     self.assertEqual('scheme', r[0])
     self.assertEqual('username:password@host:1234', r[1])
     self.assertEqual('/foo', r[2])
     self.assertEqual('', r[3])
     self.assertEqual({}, r[4])
     self.assertEqual('', r[5])
Exemple #19
0
	def __init__(self, dsn):
		############## Connect to AqWiki

		db_config = dsnparse.parse(dsn)

		self.dbconnection = mysql.connect(
		    user=db_config.username,
		    passwd=db_config.password,
		    db=db_config.paths[0],
		    charset='utf8')
Exemple #20
0
    def execute(self, context: Dict[str, Any]) -> None:
        env = Env()
        db_connection, _ = env(self.db_conn_id).split("?")
        scheme = dsnparse.parse(db_connection).scheme.replace("postgresql", "pgsql")
        username = dsnparse.parse(db_connection).username
        password = dsnparse.parse(db_connection).password
        paths = "/".join(dsnparse.parse(db_connection).paths)
        hostloc = dsnparse.parse(db_connection).hostloc
        db_url = "".join([scheme, "://", username, "@", hostloc, "/", paths])

        source_url = f"{db_url}/{self.source_table}"
        if self.key_column is not None:
            # `pg_comparator` expects an alternate key to be passed in as part of the DB URL and
            # not as a separate command line argument. The alternate key is specified as the
            # first query parameter and has no value.
            source_url += f"?{self.key_column}"
        target_url = f"{db_url}/{self.target_table}"

        self.log.info(
            "Start change data capture (with pg_comparator) from: '%s' to: '%s" ".",
            source_url,
            target_url,
        )
        arguments = [
            "/usr/bin/pg_comparator",
            "--do-it",
            "--synchronize",
            "--max-ratio=2.0",
            "--env-pass=db_pass_env_var",
            f"--prefix={self.target_table}_cmp",
        ]
        if self.use_pg_copy:
            arguments.extend(["--pg-copy=128", "--no-async"])
        if self.use_key:
            arguments.append("--use-key")
        if self.no_deletes:
            arguments.append("--skip-deletes")
        db_pass_env_var = {"db_pass_env_var": password}
        arguments.extend([source_url, target_url])
        self.log.info("Executing: '%s'.", " ".join(arguments))
        result = subprocess.run(arguments, capture_output=True, check=True, env=db_pass_env_var)
        self.log.debug(result.stdout)
Exemple #21
0
def configure(dsn, connection_class=DsnConnection):
    """
    configure an interface to be used to send messages to a backend

    you use this function to configure an Interface using a dsn, then you can get
    that interface using the get_interface() method

    dsn -- string -- a properly formatted prom dsn, see DsnConnection for how to format the dsn
    """
    #global interfaces
    c = dsnparse.parse(dsn, parse_class=connection_class)
    set_interface(c.interface, c.name)
Exemple #22
0
def run_command(module, host, port, config, gino_var, db, no_auth, user):
    click.echo(
        f"Run Gino Admin Panel on host {host} and port {port}. Config: {config}. "
        f"Module with models file: {module}. Gino Engine variable {db}")
    if not os.path.isfile(module):
        # todo: need to add load by python module name
        click.echo(
            f"Cannot found {module}. Please path to python file with DB Models"
        )
        exit(1)
    spec = importlib.util.spec_from_file_location("db", module)
    module_obj = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module_obj)

    db_models = []

    for name in dir(module_obj):
        if name == gino_var:
            gino_var = getattr(module_obj, name)
            continue
        if not name.startswith("__"):
            mod = getattr(module_obj, name)
            if getattr(mod, "__tablename__", None):
                db_models.append(mod)
    if db:
        db = dsnparse.parse(db)
        os.environ["SANIC_DB_HOST"] = db.host
        os.environ["SANIC_DB_DATABASE"] = db.database
        os.environ["SANIC_DB_USER"] = db.user
        if db.port:
            os.environ["SANIC_DB_PORT"] = str(db.port)
        os.environ["SANIC_DB_PASSWORD"] = db.password
    if user:
        if len(user.split(":")) <= 1:
            click.echo(
                f"--user must be provided in format login:password. You set {user}"
            )
            exit(1)
        os.environ["SANIC_ADMIN_USER"] = user.split(":")[0]
        os.environ["SANIC_ADMIN_PASSWORD"] = user.split(":")[1]
    click.echo(f"Models that will be displayed in Admin Panel: \n {db_models}")
    if config:
        pairs = config.split(";")
        prepared_config = {}
        for pair in pairs:
            prepared_config[pair.split("=")[0]] = pair.split("=")[1]
        click.echo(f"Run Gino Admin with config: {prepared_config}")
    else:
        prepared_config = {}
    if no_auth:
        os.environ["ADMIN_AUTH_DISABLE"] = "1"
    create_admin_app(gino_var, db_models, prepared_config, host, port)
Exemple #23
0
def fetch_pg_env_vars(postgres_conn_id="postgres_default"):
    # Need to get rid of trailing '&'
    # moved from to here due to circular import error
    from . import env
    stripped_env = env("AIRFLOW_CONN_POSTGRES_DEFAULT")[:-1]
    pg_conn_info = dsnparse.parse(stripped_env)
    return {
        "PGHOST": pg_conn_info.host,
        "PGPORT": str(pg_conn_info.port),
        "PGDATABASE": pg_conn_info.paths[0],
        "PGUSER": pg_conn_info.username,
        "PGPASSWORD": pg_conn_info.password,
    }
Exemple #24
0
def configure(dsn, connection_class=DsnConnection):
    """
    configure an interface to be used to query a backend

    you use this function to configure an Interface using a dsn, then you can get
    that interface using the get_interface() method

    dsn -- string -- a properly formatted prom dsn, see DsnConnection for how to format the dsn
    """
    c = dsnparse.parse(dsn, parse_class=connection_class)
    inter = c.interface
    set_interface(inter, c.name)
    return inter
Exemple #25
0
def run_admin(host, port, db, password, listen, interface, **kwargs):
    environ_dsn = os.environ.get('REDIS_URL', None)
    if environ_dsn:
        dsn_parsed = dsnparse.parse(environ_dsn)
        host = host or dsn_parsed.host
        port = port or dsn_parsed.port
        password = dsn_parsed.password
    conn = redis.Redis(host, int(port or 6379), int(db or 0), password)
    # FIXME - fix --no-structlog flag
    # tiger = TaskTiger(setup_structlog=kwargs['structlog'], connection=conn)
    tiger = TaskTiger(setup_structlog=False, connection=conn)
    app = Flask(__name__)
    admin = Admin(app, url='/')
    admin.add_view(TaskTigerView(tiger, name='TaskTiger',
                                 endpoint='tasktiger'))
    app.run(debug=False, port=int(listen or 5000), host=interface)
Exemple #26
0
    def __init__(self, dsn, migrations_dir=None):
        r = dsnparse.parse(dsn)

        self.client = pyorient.OrientDB(r.host or "localhost", r.port or 2424)
        self.session_id = self.client.connect(r.user, r.password)

        self.db = None
        self.parsed_dsn = r

        self.migration_manager = None

        if self.parsed_dsn.dbname:
            self.ensure_db_from_dsn()

            if migrations_dir is not None:
                self.init_migration_manager(migrations_dir)
Exemple #27
0
def establish_conn(engine, dsn):
    db_module = import_module(engine)
    if engine in (None, ):
        conn = db_module.connect(dsn)
    else:
        args = dsnparse.parse(dsn)
        argsdict = {
            'host': args.hostname or None,
            'port': args.port or None,
            'db': args.paths[0] or None,
            'user': args.username or None,
            'passwd': args.password or None,
        }
        argsdict = {a: b for a, b in argsdict.items() if b is not None}
        conn = db_module.connect(**argsdict)
    return conn
Exemple #28
0
 def test_unpack(self):
     dsn = 'scheme://*****:*****@host:1234/foo'
     dsn_test = {
         'scheme': 'scheme',
         'netloc': 'username:password@host:1234',
         'path': '/foo',
         'params': "",
         'query': {},
         'fragment': ''
     }
     scheme, netloc, path, params, query, fragment = dsnparse.parse(dsn)
     self.assertEqual('scheme', scheme)
     self.assertEqual('username:password@host:1234', netloc)
     self.assertEqual('/foo', path)
     self.assertEqual('', params)
     self.assertEqual({}, query)
     self.assertEqual('', fragment)
Exemple #29
0
 def test_unpack(self):
     dsn = 'scheme://*****:*****@host:1234/foo'
     dsn_test = {
         'scheme': 'scheme',
         'netloc': 'username:password@host:1234',
         'path': '/foo',
         'params': "",
         'query': {},
         'fragment': ''
     }
     scheme, netloc, path, params, query, fragment = dsnparse.parse(dsn)
     self.assertEqual('scheme', scheme)
     self.assertEqual('username:password@host:1234', netloc)
     self.assertEqual('/foo', path)
     self.assertEqual('', params)
     self.assertEqual({}, query)
     self.assertEqual('', fragment)
Exemple #30
0
    def __init__(self, dsn):
        self.dsn = dsnparse.parse(dsn)

        self.dbname = self.dsn.dbname
        self.user = self.dsn.username
        self.password = self.dsn.password
        self.host = self.dsn.hostname
        self.port = self.dsn.port

        if self.dsn.scheme.startswith("postgres"):
            self.conn = psycopg2.connect(
                dbname=self.dbname,
                user=self.user,
                password=self.password,
                host=self.host,
                port=self.port,
                cursor_factory=psycopg2.extras.RealDictCursor,
            )
            self.conn.autocommit = True
        else:
            raise ValueError("{} is unsupported".format(self.dsn.scheme))
Exemple #31
0
    def get_size():
        dsn = dsnparse.parse(config("findex:database:connection"))
        db_name = dsn.paths[0]
        sql = """
        SELECT
            pg_size_pretty(pg_database_size(pg_database.datname)) AS size
        FROM pg_database WHERE datname=:db_name;
        """
        res = DatabaseStatus.raw_query(sql, {"db_name": db_name})
        if res.cls != "ok":
            return res

        data_dir = DatabaseStatus.raw_query("""show data_directory;""")
        if data_dir.cls != "ok":
            res.data += " (error fetching data_dir)"
            return res
        else:
            res.cls = "info"

        res.data += " @ %s" % data_dir.data
        return res
Exemple #32
0
def create_engine(db_uri, print_sql, auto_create):
    if db_uri.startswith(_SQLITE_SCHEME):
        # Parse sqlite:// ourselves, to allow for sqlite://c:/path/to/db
        path = db_uri[len(_SQLITE_SCHEME):]
        if not auto_create and path != ':memory:':
            if not Path(path).exists():
                raise ConnectError("File %r doesn't exist. To create it, set auto_create to True" % path)
        return SqliteInterface(path, print_sql=print_sql)

    dsn = dsnparse.parse(db_uri)
    if len(dsn.schemes) > 1:
        raise NotImplementedError("Preql doesn't support multiple schemes")
    scheme ,= dsn.schemes

    if len(dsn.paths) == 0:
        path = ''
    elif len(dsn.paths) == 1:
        path ,= dsn.paths
    else:
        raise ValueError("Bad value for uri, too many paths: %s" % db_uri)

    if scheme == 'postgres':
        return PostgresInterface(dsn.host, dsn.port, path, dsn.user, dsn.password, print_sql=print_sql)
    elif scheme == 'mysql':
        return MysqlInterface(dsn.host, dsn.port, path, dsn.user, dsn.password, print_sql=print_sql)
    elif scheme == 'git':
        return GitInterface(path, print_sql=print_sql)
    elif scheme == 'duck':
        return DuckInterface(path, print_sql=print_sql)
    elif scheme == 'bigquery':
        return BigQueryInterface(path, print_sql=print_sql)
    elif scheme == 'snowflake':
        return SnowflakeInterface(dsn.host, dsn.user, dsn.password, path, **dsn.query, print_sql=print_sql)
    if scheme == 'redshift':
        return RedshiftInterface(dsn.host, dsn.port, path, dsn.user, dsn.password, print_sql=print_sql)

    raise NotImplementedError(f"Scheme {dsn.scheme} currently not supported")
Exemple #33
0
def configure(dsn):
    """
    configure an interface to be used to query a backend

    you use this function to configure an Interface using a dsn, then you can get
    that interface using the get_interface() method

    example dsn -- common.cache.RedisInterface://localhost:6379/0?timeout=5

    dsn -- string -- a properly formatted prom dsn, see DsnConnection for how to format the dsn
    """
    global interfaces

    c = dsnparse.parse(dsn)
    if c.fragment in interfaces:
        raise ValueError('a connection named "{}" has already been configured'.format(c.fragment))

    # compensate for passing pw as username (eg, not doing //:password@ but instead //password@)
    password = None
    if c.password:
        password = c.password
    elif c.username:
        password = c.username

    interface_module_name, interface_class_name = c.scheme.rsplit('.', 1)
    interface_module = importlib.import_module(interface_module_name)
    interface_class = getattr(interface_module, interface_class_name)

    connection_config = dict(host=c.host, port=c.port, password=password, **c.query)
    paths = c.paths
    if paths:
        connection_config['db'] = paths[0]

    i = interface_class(**connection_config)
    set_interface(i, c.fragment)
    return i
Exemple #34
0
import dsnparse
from peewee import (
    MySQLDatabase,
    Model,
    CharField,
    TextField,
    ForeignKeyField,
    UUIDField,
    DateTimeField,
)
from playhouse.shortcuts import model_to_dict

from .app import app


dsn = dsnparse.parse(app.config["DSN"])

db = MySQLDatabase(
    "app", user=dsn.user, password=dsn.password, host=dsn.host, port=dsn.port
)

isoformat = "%Y-%m-%dT%H:%M:%S.%f"


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

    id = UUIDField(primary_key=True, default=uuid.uuid4)
    created_at = DateTimeField(formats=[isoformat], default=datetime.datetime.utcnow)
    updated_at = DateTimeField(formats=[isoformat], default=datetime.datetime.utcnow)
Exemple #35
0
    def test_parse(self):
        tests = [
            ('scheme://:password@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
             {
                 'scheme': 'scheme',
                 'schemes': ['scheme'],
                 'username': '',
                 'password': '******',
                 'netloc': ':password@host:1234',
                 'host': 'host',
                 'hostloc': 'host:1234',
                 'path': '/bar/che',
                 'paths': ['bar', 'che'],
                 'hostname': 'host',
                 'query': {
                     'option1': 'opt_val1',
                     'option2': 'opt_val2'
                 },
                 'fragment': 'anchor'
             }),
            ('scheme://username@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
             {
                 'scheme': 'scheme',
                 'schemes': ['scheme'],
                 'username': '******',
                 'password': None,
                 'netloc': 'username@host:1234',
                 'host': 'host',
                 'hostloc': 'host:1234',
                 'path': '/bar/che',
                 'paths': ['bar', 'che'],
                 'hostname': 'host',
                 'query': {
                     'option1': 'opt_val1',
                     'option2': 'opt_val2'
                 },
                 'fragment': 'anchor'
             }),
            ('scheme://*****:*****@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
             {
                 'scheme': 'scheme',
                 'schemes': ['scheme'],
                 'username': '******',
                 'password': '******',
                 'netloc': 'username:password@host:1234',
                 'host': 'host',
                 'hostloc': 'host:1234',
                 'path': '/bar/che',
                 'paths': ['bar', 'che'],
                 'hostname': 'host',
                 'query': {
                     'option1': 'opt_val1',
                     'option2': 'opt_val2'
                 },
                 'fragment': 'anchor'
             }),
            ('scheme://localhost', {
                'scheme': 'scheme',
                'schemes': ['scheme'],
                'netloc': 'localhost',
                'host': 'localhost',
                'hostloc': 'localhost',
                'path': '',
                'paths': [],
                'hostname': 'localhost',
                'query': {}
            }),
            ('scheme1+scheme2://username:[email protected]:9000/?opt=opt_val1&opt=opt_val2#anchor',
             {
                 'scheme': 'scheme1+scheme2',
                 'schemes': ['scheme1', 'scheme2'],
                 'username': '******',
                 'password': '******',
                 'netloc': 'username:[email protected]:9000',
                 'host': 'host.com',
                 'hostloc': 'host.com:9000',
                 'path': '/',
                 'paths': [],
                 'hostname': 'host.com',
                 'query': {
                     'opt': ['opt_val1', 'opt_val2']
                 },
                 'fragment': 'anchor'
             }),
        ]

        for dsn, test_out in tests:
            r = dsnparse.parse(dsn)
            for k, v in test_out.items():
                self.assertEqual(v, getattr(r, k))

        with self.assertRaises(ValueError):
            r = dsnparse.parse('//host.com:1234')
Exemple #36
0
 def test_parse_rel_path(self):
     dsn = 'scheme.Foo://./bar/che.db'
     r = dsnparse.parse(dsn)
     self.assertIsNone(r.hostname)
     self.assertEqual('./bar/che.db', r.path)
Exemple #37
0
    def test_parse(self):
        tests = [
            (
                'scheme://:password@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
                {
                    'scheme': 'scheme',
                    'schemes': ['scheme'],
                    'username': '',
                    'password': '******',
                    'netloc': ':password@host:1234',
                    'host': 'host',
                    'hostloc': 'host:1234',
                    'path': '/bar/che',
                    'paths': ['bar', 'che'],
                    'hostname': 'host',
                    'query': {'option1': 'opt_val1', 'option2': 'opt_val2'},
                    'fragment': 'anchor'
                }
            ),
            (
                'scheme://username@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
                {
                    'scheme': 'scheme',
                    'schemes': ['scheme'],
                    'username': '******',
                    'password': None,
                    'netloc': 'username@host:1234',
                    'host': 'host',
                    'hostloc': 'host:1234',
                    'path': '/bar/che',
                    'paths': ['bar', 'che'],
                    'hostname': 'host',
                    'query': {'option1': 'opt_val1', 'option2': 'opt_val2'},
                    'fragment': 'anchor'
                }
            ),
            (
                'scheme://*****:*****@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor',
                {
                    'scheme': 'scheme',
                    'schemes': ['scheme'],
                    'username': '******',
                    'password': '******',
                    'netloc': 'username:password@host:1234',
                    'host': 'host',
                    'hostloc': 'host:1234',
                    'path': '/bar/che',
                    'paths': ['bar', 'che'],
                    'hostname': 'host',
                    'query': {'option1': 'opt_val1', 'option2': 'opt_val2'},
                    'fragment': 'anchor'
                }
            ),
            (
                'scheme://localhost',
                {
                    'scheme': 'scheme',
                    'schemes': ['scheme'],
                    'netloc': 'localhost',
                    'host': 'localhost',
                    'hostloc': 'localhost',
                    'path': '',
                    'paths': [],
                    'hostname': 'localhost',
                    'query': {}
                }
            ),
            (
                'scheme1+scheme2://username:[email protected]:9000/?opt=opt_val1&opt=opt_val2#anchor',
                {
                    'scheme': 'scheme1+scheme2',
                    'schemes': ['scheme1', 'scheme2'],
                    'username': '******',
                    'password': '******',
                    'netloc': 'username:[email protected]:9000',
                    'host': 'host.com',
                    'hostloc': 'host.com:9000',
                    'path': '/',
                    'paths': [],
                    'hostname': 'host.com',
                    'query': {'opt': ['opt_val1', 'opt_val2']},
                    'fragment': 'anchor'
                }
            ),
        ]

        for dsn, test_out in tests:
            r = dsnparse.parse(dsn)
            for k, v in test_out.items():
                self.assertEqual(v, getattr(r, k))

        with self.assertRaises(ValueError):
            r = dsnparse.parse('//host.com:1234')
Exemple #38
0
 def test_parse_rel_path(self):
     dsn = 'scheme.Foo://./bar/che.db'
     r = dsnparse.parse(dsn)
     self.assertIsNone(r.hostname)
     self.assertEqual('./bar/che.db', r.path)
Exemple #39
0
 def test_parse_memory(self):
     dsn = 'scheme.Foo://:memory:?opt=val'
     r = dsnparse.parse(dsn)
     self.assertIsNone(r.hostname)
     self.assertIsNone(r.port)
     self.assertEqual(':memory:', r.path)
Exemple #40
0
 def test_geturl(self):
     dsn = 'scheme://*****:*****@host:1234/bar/che?option1=opt_val1&option2=opt_val2#anchor'
     r = dsnparse.parse(dsn)
     self.assertEqual(dsn, r.geturl())
Exemple #41
0
ALLOWED_HOSTS = []
if not DEBUG:
    ALLOWED_HOSTS.extend([
        'yablog-django.herokuapp.com',
        '*'
    ])


DATABASES = {}
if DEBUG:
    DATABASES.update({'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }})
else:
    dsn = dsnparse.parse(os.environ['CLEARDB_DATABASE_URL'])
    DATABASES.update({
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': dsn.database,
            'HOST': dsn.host,
            'USER': dsn.username,
            'PASSWORD': dsn.password,
        }
    })


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = []
MANAGERS = ADMINS

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '.herokuapp.com:localhost').split(':')

# For Heroku
import dj_database_url
DATABASES = {'default': dj_database_url.config(default='postgres://*****:*****@localhost:5432/db')}

# Turn on PostGIS
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

# Redis setup - two DBs (default + sessions)
redis_dsn = dsnparse.parse(os.environ.get('REDIS_URL', 'redis://localhost:6379'))
CACHES = {
    "default": {
        "BACKEND": "redis_cache.RedisCache",
        "LOCATION": "{0}:{1}".format(redis_dsn.hostname, redis_dsn.port),
        "OPTIONS": {
            "PARSER_CLASS": "redis.connection.HiredisParser",
            "PASSWORD": redis_dsn.password,
            "DB": 0,
        }
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
Exemple #43
0
 def test_parse_abs_path(self):
     dsn = 'scheme.Foo:///bar/che.db'
     r = dsnparse.parse(dsn)
     self.assertEqual('scheme.Foo', r.scheme)
     self.assertEqual('/bar/che.db', r.path)
Exemple #44
0
    if o in ("-u", "--user"): USER=a
    elif o in ("-p", "--pass"): PASS=a
    elif o in ("-d", "--dsn"): DSN=a
    elif o in ("-v", "--verbose"): VERBOSE=True

if USER == "" or PASS == "" or DSN == "":
    usage()
    sys.exit(1)

if len(args) == 0:
    print("At least one URL is required")
    usage()
    sys.exit(1)

try:
    dsn_parts = dsnparse.parse(DSN)
except:
    print("Invalid DSN value")
    sys.exit(1)

try:
    db_con = MySQLdb.connect(dsn_parts.host, dsn_parts.username, dsn_parts.password, dsn_parts.paths[0])
    dbc = db_con.cursor()
except:
    print("DB connection failed")
    sys.exit(1)

MY_DIR = os.path.dirname(os.path.abspath(__file__))
    
chrome_opts = webdriver.ChromeOptions()
chrome_opts.add_argument("--headless")
Exemple #45
0
 def test_parse_abs_path(self):
     dsn = 'scheme.Foo:///bar/che.db'
     r = dsnparse.parse(dsn)
     self.assertEqual('scheme.Foo', r.scheme)
     self.assertEqual('/bar/che.db', r.path)