Skip to content

Commit

Permalink
Format code with black and isort, enforce formatting on CI. (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
jettify committed Mar 16, 2023
1 parent a4d9f83 commit e10c051
Show file tree
Hide file tree
Showing 19 changed files with 504 additions and 348 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jobs:
- name: Lint
run: |
make lint
make checkfmt
- name: Test
run: |
Expand Down
20 changes: 9 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Some simple testing tasks (sorry, UNIX only).

FLAGS=
FILES := aioodbc tests examples setup.py


checkrst:
Expand Down Expand Up @@ -37,18 +38,15 @@ doc:
make -C docs html
@echo "open file://`pwd`/docs/_build/html/index.html"

docker_build:
make -C ci build
black:
black -l 79 $(FILES)

# NOTE: we start crashing if running tests with -n auto
fmt:
isort ${FILES}
black -l 79 ${FILES}

docker_test:
docker run --rm -v /$$(pwd):/aioodbc -v /var/run/docker.sock:/var/run/docker.sock --name aioodbc-test-$$(date +%s) --net=host -e PYTHONASYNCIODEBUG=$(PYTHONASYNCIODEBUG) -it jettify/aioodbc-test:latest py.test -sv tests $(FLAGS)

docker_cov:
docker run --rm -v /$$(pwd):/aioodbc -v /var/run/docker.sock:/var/run/docker.sock --name aioodbc-test-$$(date +%s) --net=host -e PYTHONASYNCIODEBUG=$(PYTHONASYNCIODEBUG) -it jettify/aioodbc-test:latest py.test -sv --cov-report term --cov-report html --cov tests --cov aioodbc $(FLAGS)

docker_clean:
docker rm -v -f $$(docker ps -a -q -f 'name=aioodbc')
checkfmt:
isort --check-only --diff $(FILES)
black -l 79 --check $(FILES)

.PHONY: all flake test vtest cov clean doc
10 changes: 5 additions & 5 deletions aioodbc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import asyncio
from pyodbc import dataSources as _dataSources

from .connection import connect, Connection
from .pool import create_pool, Pool
from pyodbc import dataSources as _dataSources

from .connection import Connection, connect
from .pool import Pool, create_pool

__version__ = '0.3.3'
__all__ = ['connect', 'Connection', 'create_pool', 'Pool', 'dataSources']
__version__ = "0.3.3"
__all__ = ["connect", "Connection", "create_pool", "Pool", "dataSources"]

(connect, Connection, create_pool, Pool) # pyflakes

Expand Down
112 changes: 82 additions & 30 deletions aioodbc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
from functools import partial

import pyodbc

from .cursor import Cursor
from .utils import _ContextManager, _is_conn_close_error


__all__ = ['connect', 'Connection']


def connect(*, dsn, autocommit=False, ansi=False, timeout=0, loop=None,
executor=None, echo=False, after_created=None, **kwargs):
__all__ = ["connect", "Connection"]


def connect(
*,
dsn,
autocommit=False,
ansi=False,
timeout=0,
loop=None,
executor=None,
echo=False,
after_created=None,
**kwargs
):
"""Accepts an ODBC connection string and returns a new Connection object.
The connection string can be passed as the string `str`, as a list of
Expand All @@ -34,32 +44,70 @@ def connect(*, dsn, autocommit=False, ansi=False, timeout=0, loop=None,
connection is connected. Must be an async unary function, or leave it
as None.
"""
return _ContextManager(_connect(dsn=dsn, autocommit=autocommit,
ansi=ansi, timeout=timeout, loop=loop,
executor=executor, echo=echo,
after_created=after_created, **kwargs))


async def _connect(*, dsn, autocommit=False, ansi=False, timeout=0, loop=None,
executor=None, echo=False, after_created=None, **kwargs):
return _ContextManager(
_connect(
dsn=dsn,
autocommit=autocommit,
ansi=ansi,
timeout=timeout,
loop=loop,
executor=executor,
echo=echo,
after_created=after_created,
**kwargs
)
)


async def _connect(
*,
dsn,
autocommit=False,
ansi=False,
timeout=0,
loop=None,
executor=None,
echo=False,
after_created=None,
**kwargs
):
loop = loop or asyncio.get_event_loop()
conn = Connection(dsn=dsn, autocommit=autocommit, ansi=ansi,
timeout=timeout, echo=echo, loop=loop, executor=executor,
after_created=after_created, **kwargs)
conn = Connection(
dsn=dsn,
autocommit=autocommit,
ansi=ansi,
timeout=timeout,
echo=echo,
loop=loop,
executor=executor,
after_created=after_created,
**kwargs
)
await conn._connect()
return conn


class Connection:
""" Connection objects manage connections to the database.
"""Connection objects manage connections to the database.
Connections should only be created by the aioodbc.connect function.
"""

_source_traceback = None

def __init__(self, *, dsn, autocommit=False, ansi=None,
timeout=0, executor=None, echo=False, loop=None,
after_created=None, **kwargs):
def __init__(
self,
*,
dsn,
autocommit=False,
ansi=None,
timeout=0,
executor=None,
echo=False,
loop=None,
after_created=None,
**kwargs
):
self._executor = executor
self._loop = loop or asyncio.get_event_loop()
self._conn = None
Expand All @@ -83,10 +131,14 @@ def _execute(self, func, *args, **kwargs):

async def _connect(self):
# create pyodbc connection
f = self._execute(pyodbc.connect, self._dsn,
autocommit=self._autocommit, ansi=self._ansi,
timeout=self._timeout,
**self._kwargs)
f = self._execute(
pyodbc.connect,
self._dsn,
autocommit=self._autocommit,
ansi=self._ansi,
timeout=self._timeout,
**self._kwargs
)
self._conn = await f
if self._posthook is not None:
await self._posthook(self._conn)
Expand Down Expand Up @@ -223,13 +275,13 @@ def __del__(self):
self._conn.close()
self._conn = None

warnings.warn("Unclosed connection {!r}".format(self),
ResourceWarning)
warnings.warn(
"Unclosed connection {!r}".format(self), ResourceWarning
)

context = {'connection': self,
'message': 'Unclosed connection'}
context = {"connection": self, "message": "Unclosed connection"}
if self._source_traceback is not None:
context['source_traceback'] = self._source_traceback
context["source_traceback"] = self._source_traceback
self._loop.call_exception_handler(context)

async def __aenter__(self):
Expand Down
53 changes: 36 additions & 17 deletions aioodbc/cursor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pyodbc

from .log import logger
from .utils import PY_352, _is_conn_close_error


__all__ = ['Cursor']
__all__ = ["Cursor"]


class Cursor:
Expand All @@ -24,7 +24,7 @@ def __init__(self, pyodbc_cursor, connection, echo=False):
async def _run_operation(self, func, *args, **kwargs):
# execute func in thread pool of attached to cursor connection
if not self._conn:
raise pyodbc.OperationalError('Cursor is closed.')
raise pyodbc.OperationalError("Cursor is closed.")

try:
result = await self._conn._execute(func, *args, **kwargs)
Expand Down Expand Up @@ -242,36 +242,52 @@ def statistics(self, catalog=None, schema=None, unique=False, quick=True):
:param quick: if True, CARDINALITY and PAGES are returned only if
they are readily available from the server
"""
fut = self._run_operation(self._impl.statistics, catalog=catalog,
schema=schema, unique=unique, quick=quick)
fut = self._run_operation(
self._impl.statistics,
catalog=catalog,
schema=schema,
unique=unique,
quick=quick,
)
return fut

def rowIdColumns(self, table, catalog=None, schema=None, # nopep8
nullable=True):
def rowIdColumns(
self, table, catalog=None, schema=None, nullable=True # nopep8
):
"""Executes SQLSpecialColumns with SQL_BEST_ROWID which creates a
result set of columns that uniquely identify a row
"""
fut = self._run_operation(self._impl.rowIdColumns, table,
catalog=catalog, schema=schema,
nullable=nullable)
fut = self._run_operation(
self._impl.rowIdColumns,
table,
catalog=catalog,
schema=schema,
nullable=nullable,
)
return fut

def rowVerColumns(self, table, catalog=None, schema=None, # nopep8
nullable=True):
def rowVerColumns(
self, table, catalog=None, schema=None, nullable=True # nopep8
):
"""Executes SQLSpecialColumns with SQL_ROWVER which creates a
result set of columns that are automatically updated when any
value in the row is updated.
"""
fut = self._run_operation(self._impl.rowVerColumns, table,
catalog=catalog, schema=schema,
nullable=nullable)
fut = self._run_operation(
self._impl.rowVerColumns,
table,
catalog=catalog,
schema=schema,
nullable=nullable,
)
return fut

def primaryKeys(self, table, catalog=None, schema=None): # nopep8
"""Creates a result set of column names that make up the primary key
for a table by executing the SQLPrimaryKeys function."""
fut = self._run_operation(self._impl.primaryKeys, table,
catalog=catalog, schema=schema)
fut = self._run_operation(
self._impl.primaryKeys, table, catalog=catalog, schema=schema
)
return fut

def foreignKeys(self, *a, **kw): # nopep8
Expand Down Expand Up @@ -316,9 +332,12 @@ def rollback(self):
return fut

if PY_352:

def __aiter__(self):
return self

else:

async def __aiter__(self):
return self

Expand Down
1 change: 0 additions & 1 deletion aioodbc/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

import logging


# Name the logger after the package.
logger = logging.getLogger(__package__)

0 comments on commit e10c051

Please sign in to comment.