Skip to content

Commit

Permalink
linter
Browse files Browse the repository at this point in the history
  • Loading branch information
m32 committed Aug 24, 2024
1 parent 283b66f commit 2bd3f72
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 104 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ jobs:
steps:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- run: pip install --upgrade pip setuptools
Expand All @@ -21,6 +19,6 @@ jobs:
- run: pip install python-tds Sqlalchemy
- run: mypy --ignore-missing-imports . || true
- run: pytest || true
- run: pytest --doctest-modules || true
- run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true
- run: safety check
# - run: pytest --doctest-modules || true
# - run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true
# - run: safety check
33 changes: 22 additions & 11 deletions createdb.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#!/usr/bin/env vpython3
import pytds

from t import username, userpass


class Main(object):
def __init__(self):
self.conn = pytds.connect(dsn='127.0.0.1', user=username, password=userpass, database='master', autocommit=True)
self.conn = pytds.connect(
dsn="127.0.0.1",
user=username,
password=userpass,
database="master",
autocommit=True,
)

def close(self):
self.conn.close()

def main(self):
sqls = (
'''\
"""\
DROP DATABASE IF EXISTS testing;
''',
'''\
""",
"""\
CREATE DATABASE testing ON (
NAME = testing_dat,
FILENAME = '/var/opt/mssql/data/testing_mdf.mdf',
Expand All @@ -26,26 +34,29 @@ def main(self):
SIZE = 5MB,
FILEGROWTH = 5MB
)
''',
'''\
""",
"""\
USE testing
''',
'''\
""",
"""\
DROP SCHEMA IF EXISTS test_schema
''',
'''\
""",
"""\
CREATE SCHEMA test_schema;
''',
""",
)
c = self.conn.cursor()
for sql in sqls:
c.execute(sql)
c.close()


def main():
cls = Main()
try:
cls.main()
finally:
cls.close()


main()
15 changes: 15 additions & 0 deletions linter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
xrun() {
echo "********************** $*"
vpy3 $*
}

xrun bandit --recursive --skip B101 . # B101 is assert statements
xrun black --check . || true
xrun codespell --quiet-level=2 # --ignore-words-list="" --skip=""
xrun flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
xrun isort --check-only --profile black . || true
xrun mypy --ignore-missing-imports . || true
#xrun pytest || true
#xrun pytest --doctest-modules || true
#xrun safety check
78 changes: 41 additions & 37 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
import sys
import os
import re
import sys

if sys.version_info < (2, 6):
raise Exception("SQLAlchemy TDS requires Python 2.6 or higher.")

from setuptools import setup

v = open(os.path.join(os.path.dirname(__file__), 'sqlalchemy_pytds', '__init__.py'))
VERSION = re.compile(r".*__version__ = '(.*?)'", re.S).match(v.read()).group(1)
v.close()
with open(
os.path.join(os.path.dirname(__file__), "sqlalchemy_pytds", "__init__.py")
) as fp:
rem = re.compile(r".*__version__ = \"(.*?)\"", re.S).match(fp.read())
assert rem is not None
VERSION = rem.group(1)

readme = os.path.join(os.path.dirname(__file__), 'README.rst')
readme = os.path.join(os.path.dirname(__file__), "README.rst")
requires = [
'python-tds',
'SQLAlchemy >= 2.0',
"python-tds",
"SQLAlchemy >= 2.0",
]

setup(name='sqlalchemy_pytds',
version=VERSION,
description="A Microsoft SQL Server TDS connector for SQLAlchemy.",
long_description=open(readme).read(),
long_description_content_type='text/x-rst',
author='Grzegorz Makarewicz',
author_email='[email protected]',
url='https://github.com/m32/sqlalchemy-tds',
license='MIT',
platforms=["any"],
packages=['sqlalchemy_pytds'],
classifiers=[
'Development Status :: 4 - Beta',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Database :: Front-Ends',
],
keywords='SQLAlchemy Microsoft SQL Server',
install_requires = requires,
include_package_data=True,
tests_require=['nose >= 0.11'],
test_suite="nose.collector",
entry_points={
'sqlalchemy.dialects': [
'mssql.pytds = sqlalchemy_pytds.dialect:MSDialect_pytds',
]
}
setup(
name="sqlalchemy_pytds",
version=VERSION,
description="A Microsoft SQL Server TDS connector for SQLAlchemy.",
long_description=open(readme).read(),
long_description_content_type="text/x-rst",
author="Grzegorz Makarewicz",
author_email="[email protected]",
url="https://github.com/m32/sqlalchemy-tds",
license="MIT",
platforms=["any"],
packages=["sqlalchemy_pytds"],
classifiers=[
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Topic :: Database :: Front-Ends",
],
keywords="SQLAlchemy Microsoft SQL Server",
install_requires=requires,
include_package_data=True,
tests_require=["nose >= 0.11"],
test_suite="nose.collector",
entry_points={
"sqlalchemy.dialects": [
"mssql.pytds = sqlalchemy_pytds.dialect:MSDialect_pytds",
]
},
)
2 changes: 1 addition & 1 deletion sqlalchemy_pytds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.0.1'
__version__ = "1.0.1"

from sqlalchemy.dialects import registry

Expand Down
22 changes: 11 additions & 11 deletions sqlalchemy_pytds/connector.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# connectors/pytds.py
import re

import pytds
import pytds.login
from pytds import tds_base, tds_session
from sqlalchemy.connectors import Connector
from sqlalchemy.util import asbool

import re
import pytds
from pytds import tds

prevexecute = pytds.Cursor.execute


def execute(self, operation, params=None):
#print('execute:', operation, params)
# print('execute:', operation, params)
if operation[:15] == "EXEC sp_columns":
proc = "sp_columns"
params = {
Expand All @@ -35,21 +35,21 @@ def execute(self, operation, params=None):
def process_tabname(self):
r = self._reader
total_length = r.get_smallint()
if not tds.tds_base.IS_TDS71_PLUS(self):
if not tds_base.IS_TDS71_PLUS(self):
name_length = r.get_smallint()
tds.skipall(r, total_length)
tds_base.skipall(r, total_length)


def process_colinfo(self):
r = self._reader
total_length = r.get_smallint()
tds.skipall(r, total_length)
tds_base.skipall(r, total_length)


tds._token_map.update(
tds_session._token_map.update(
{
tds.tds_base.TDS_TABNAME_TOKEN: lambda self: process_tabname(self),
tds.tds_base.TDS_COLINFO_TOKEN: lambda self: process_colinfo(self),
tds_base.TDS_TABNAME_TOKEN: lambda self: process_tabname(self),
tds_base.TDS_COLINFO_TOKEN: lambda self: process_colinfo(self),
}
)

Expand Down
50 changes: 32 additions & 18 deletions sqlalchemy_pytds/dialect.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from sqlalchemy.dialects.mssql.base import MSDialect, MSSQLCompiler, MSIdentifierPreparer, MSExecutionContext
from sqlalchemy import util
from sqlalchemy.dialects.mssql.base import (
MSDialect,
MSExecutionContext,
MSIdentifierPreparer,
MSSQLCompiler,
)

from .connector import PyTDSConnector

_server_side_id = util.counter()


class SSCursor(object):
def __init__(self, c):
self._c = c
Expand All @@ -11,21 +19,26 @@ def __init__(self, c):
self._row = 0

def execute(self, statement, parameters):
_name = 'tc%08X' % _server_side_id()
sql = 'DECLARE ' + _name + ' CURSOR GLOBAL SCROLL STATIC READ_ONLY FOR ' + statement
#print(sql, parameters)
_name = "tc%08X" % _server_side_id()
sql = (
"DECLARE "
+ _name
+ " CURSOR GLOBAL SCROLL STATIC READ_ONLY FOR "
+ statement
)
# print(sql, parameters)
self._c.execute(sql, parameters)
self._name = _name
sql = 'OPEN ' + _name
sql = "OPEN " + _name
self._c.execute(sql)
self._c.execute('SELECT @@CURSOR_ROWS AS nrows')
self._c.execute("SELECT @@CURSOR_ROWS AS nrows")
self._nrows = self._c.fetchone()[0]
return self.fetchone()

def close(self):
sql = 'CLOSE '+self._name
sql = "CLOSE " + self._name
self._c.execute(sql)
sql = 'DEALLOCATE '+self._name
sql = "DEALLOCATE " + self._name
self._c.execute(sql)
self._c.close()
self._name = None
Expand All @@ -35,7 +48,7 @@ def close(self):
def fetchone(self):
if not (0 <= self._row < self._nrows):
return None
sql = 'FETCH ABSOLUTE %d FROM %s' % (self._row+1, self._name)
sql = "FETCH ABSOLUTE %d FROM %s" % (self._row + 1, self._name)
self._c.execute(sql)
return self._c.fetchone()

Expand Down Expand Up @@ -74,7 +87,7 @@ def goto(self, value):
return False

def __getattr__(self, name):
#print('getattr(%s)' %name)
# print('getattr(%s)' %name)
return getattr(self._c, name)


Expand All @@ -91,9 +104,11 @@ class MSExecutionContext_pytds(MSExecutionContext):

def pre_exec(self):
super(MSExecutionContext_pytds, self).pre_exec()
if self._select_lastrowid and \
self.dialect.use_scope_identity and \
len(self.parameters[0]):
if (
self._select_lastrowid
and self.dialect.use_scope_identity
and len(self.parameters[0])
):
self._embedded_scope_identity = True

self.statement += "; select scope_identity()"
Expand All @@ -113,7 +128,7 @@ def post_exec(self):
super(MSExecutionContext_pytds, self).post_exec()

def create_cursor(self):
usess = self.execution_options.get('stream_results', None)
usess = self.execution_options.get("stream_results", None)
if usess:
self._is_server_side = True
return SSCursor(self._dbapi_connection.cursor())
Expand All @@ -126,7 +141,7 @@ class MSDialect_pytds(PyTDSConnector, MSDialect):

execution_ctx_cls = MSExecutionContext_pytds
statement_compiler = MSSQLCompiler_pytds
preparer = MSSQLIdentifierPreparer_pytds
preparer = MSSQLIdentifierPreparer_pytds # type: ignore

supports_server_side_cursors = True
supports_statement_cache = True
Expand All @@ -141,9 +156,8 @@ def __init__(self, server_side_cursors=False, **params):
self.server_side_cursors = server_side_cursors

def set_isolation_level(self, connection, level):
if level == 'AUTOCOMMIT':
if level == "AUTOCOMMIT":
connection.autocommit(True)
else:
connection.autocommit(False)
super(MSDialect_pytds, self).set_isolation_level(connection,
level)
super(MSDialect_pytds, self).set_isolation_level(connection, level)
Loading

0 comments on commit 2bd3f72

Please sign in to comment.