示例#1
0
文件: project.py 项目: rmk2/gravity
def annotate_project(annotation: Dict[str, str], config: BaseConfig) -> str:
    try:
        engine = get_engine(config)

        _projects = get_projects(config)

        _project = annotation.get('project')
        _description = annotation.get('description')
        _key = annotation.get('key')

        assert _project in [x.get('project_id') for x in _projects
                            ], f'Project {_project} does not exist'

        with engine.begin() as connection:
            connection.execute(project.update().where(
                project.c.project_id == _project).values(
                    description=_description, project_key=_key))

        message = f'Annotated project {_project}'
        logging.info(message)

        return message

    except Exception as e:
        logging.error(str(e))
        raise e
示例#2
0
文件: writer.py 项目: rmk2/gravity
def sqlite_writer(row: Dict[str, Any], config: BaseConfig) -> None:
    database = config.sqlite.database

    try:
        assert os.path.isfile(
            database), 'Database file {database} does not exist'
        engine = get_engine(config)

        # Override pysqlite's default transaction behaviour
        # cf. https://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
        @event.listens_for(engine, "connect")
        def do_connect(dbapi_connection, connection_record):
            # disable pysqlite's emitting of the BEGIN statement entirely.
            # also stops it from emitting COMMIT before any DDL.
            dbapi_connection.isolation_level = None
            cursor = dbapi_connection.cursor()
            cursor.execute("PRAGMA foreign_keys=ON")
            cursor.close()

        @event.listens_for(engine, "begin")
        def do_begin(connection):
            # emit our own BEGIN
            connection.execute("BEGIN EXCLUSIVE")

        with engine.begin() as connection:
            connection.execute(worklog.insert(), row)

    except Exception as e:
        logging.error(str(e))
        raise e
示例#3
0
文件: worklog.py 项目: rmk2/gravity
def modify_worklog(modifier: str, config: BaseConfig) -> str:
    try:
        engine = get_engine(config)

        delta = _parse_modifier(modifier)

        with engine.begin() as connection:
            select_query = worklog.select().order_by(
                worklog.c.worklog_id.desc()).limit(1)
            select_row = connection.execute(select_query).fetchone()

            _id = select_row['worklog_id']
            _timestamp = select_row['timestamp'] + delta

            connection.execute(worklog.update().where(
                worklog.c.worklog_id == _id).values(timestamp=_timestamp))

            message = f'Modified last worklog: {select_row["timestamp"]} → {_timestamp}'
            logging.info(
                f'Modified last worklog: {select_row["timestamp"]} → {_timestamp}'
            )

            return message

    except Exception as e:
        logging.error(str(e))
        raise e
示例#4
0
文件: writer.py 项目: rmk2/gravity
def postgresql_writer(row: Dict[str, Any], config: BaseConfig) -> None:
    try:
        engine = get_engine(config)

        with engine.begin() as connection:
            connection.execute(worklog.insert(), row)

    except Exception as e:
        logging.error(str(e))
        raise e
示例#5
0
def test_database(prepared_config, tmpdir) -> Tuple[BaseConfig, Engine, MetaData]:
    """Prepare a test database"""
    _config = prepared_config

    _config.set_override(name='driver', override='sqlite', group='backend')
    _config.set_override(name='database', override=os.path.join(tmpdir, 'test_database.sqlite'), group='sqlite')

    _engine = get_engine(_config)

    yield _config, _engine, _metadata
示例#6
0
文件: project.py 项目: rmk2/gravity
def insert_projects(projects: Sequence[Dict[str, str]],
                    config: BaseConfig) -> None:
    try:
        engine = get_engine(config)

        with engine.begin() as connection:
            connection.execute(project.insert(), projects)

    except Exception as e:
        logging.error(str(e))
        raise e
示例#7
0
文件: project.py 项目: rmk2/gravity
def remove_projects(projects: Sequence[str], config: BaseConfig) -> None:
    try:
        engine = get_engine(config)

        with engine.begin() as connection:
            connection.execute(project.update().where(
                project.c.project_id.in_(projects)).values(
                    deleted=datetime.now()))

    except Exception as e:
        logging.error(str(e))
        raise e
示例#8
0
def _get_actions(config: BaseConfig) -> Sequence[Union[Tuple[str, Any], None]]:
    try:
        engine = get_engine(config)

        with engine.begin() as connection:
            result = connection.execute(
                action.select().where(action.c.deleted == None))

            return result.fetchall()

    except Exception as e:
        logging.error(str(e))
        raise e
示例#9
0
文件: worklog.py 项目: rmk2/gravity
def remove_worklog(config: BaseConfig) -> str:
    try:
        engine = get_engine(config)

        with engine.begin() as connection:
            select_query = worklog.select().order_by(
                worklog.c.worklog_id.desc()).limit(1)
            select_row = connection.execute(select_query).fetchone()

            _id = select_row['worklog_id']

            connection.execute(
                worklog.delete().where(worklog.c.worklog_id == _id))

            message = 'Removed last worklog'
            logging.info(message)

            return message

    except Exception as e:
        logging.error(str(e))
        raise e