Skip to content

Commit a5ee6ad

Browse files
authored
add sqlite support (#10)
* update service apis * add sqlite support
1 parent 8edcfbc commit a5ee6ad

6 files changed

Lines changed: 74 additions & 7 deletions

File tree

.github/workflows/pip.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ jobs:
3030

3131
- name: pip-install-test
3232
run: |
33-
sudo pip install -UI pip && sudo pip install -UI setuptools wheel twine
33+
sudo pip3 install -UI pip && sudo pip3 install -UI setuptools wheel twine pyOpenSSL
3434
cd src
35-
sudo python setup.py sdist bdist_wheel
36-
pip install ./dist/*.tar.gz
35+
sudo python3 setup.py sdist bdist_wheel
36+
pip3 install ./dist/*.tar.gz
3737
aloha info -v
38+
pip3 list | sort
3839
3940
- name: pypi-publish
4041
env:
@@ -44,8 +45,8 @@ jobs:
4445
TWINE_PASSWORD_TEST: ${{ secrets.TWINE_PASSWORD_TEST }}
4546
run: |
4647
env | sort -f && cd src && ls -alh
47-
sudo python -c "import fcntl; fcntl.fcntl(1, fcntl.F_SETFL, 0)"
48-
sudo python setup.py sdist bdist_wheel
48+
sudo python3 -c "import fcntl; fcntl.fcntl(1, fcntl.F_SETFL, 0)"
49+
sudo python3 setup.py sdist bdist_wheel
4950
ls -alh ./dist
5051
if [ "${GITHUB_REPOSITORY}" = "QPod/aloha" ] && [ "${GITHUB_REF_NAME}" = "main" ] ; then
5152
twine upload dist/* --verbose -u "${TWINE_USERNAME}" -p "${TWINE_PASSWORD}" ;

src/aloha/db/mysql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ def execute_query(self, sql, *args, **kwargs):
3939
with self.db.connect() as conn:
4040
cur = conn.execute(text(sql), *args, **kwargs)
4141
return cur
42+
43+
@property
44+
def connection_str(self) -> str:
45+
return 'mysql://{user}:{password}@{host}:{port}/{dbname}'.format(**self._config)

src/aloha/db/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ def execute_query(self, sql, *args, **kwargs):
4343
with self.engine.connect() as conn:
4444
cur = conn.execute(text(sql), *args, **kwargs)
4545
return cur
46+
47+
@property
48+
def connection_str(self) -> str:
49+
return "postgresql://{user}:{password}@{host}:{port}/{dbname}".format(**self._config)

src/aloha/db/sqlite.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
__all__ = ('SqliteOperator',)
2+
3+
import sqlite3
4+
5+
from sqlalchemy import create_engine
6+
from sqlalchemy.sql import text
7+
8+
from .base import PasswordVault
9+
from ..logger import LOG
10+
11+
12+
class SqliteOperator:
13+
def __init__(self, db_config, **kwargs):
14+
self._connection_pattern = "sqlite://{dbname}"
15+
dbname = db_config.get('dbname', '')
16+
if len(dbname) > 0:
17+
dbname = '/%s' % dbname
18+
self._config = {'dbname': dbname}
19+
20+
if 'password' in db_config:
21+
try:
22+
import sqlcipher3
23+
except ImportError:
24+
raise RuntimeError('Python package required for encrypted sqlite3: sqlcipher3-binary')
25+
LOG.debug('Version of sqlcipher3 = %s' % sqlcipher3.sqlite_version)
26+
password_vault = PasswordVault.get_vault(db_config.get('vault_type'), db_config.get('vault_config'))
27+
password = password_vault.get_password(db_config.get('password', None))
28+
self._config['password'] = password
29+
self._connection_pattern = "sqlite+pysqlcipher://:{password}@/{dbname}"
30+
else:
31+
LOG.debug('Version of sqlite = %s' % sqlite3.sqlite_version)
32+
33+
try:
34+
self.db = create_engine(
35+
self._connection_pattern.format(**self._config), **kwargs
36+
)
37+
LOG.debug("Sqlite connected: %s" % self.connection_str)
38+
except Exception as e:
39+
LOG.exception(e)
40+
raise RuntimeError('Failed to connect to sqlite')
41+
42+
@property
43+
def connection(self):
44+
return self.db
45+
46+
def execute_query(self, sql, *args, **kwargs):
47+
with self.db.connect() as conn:
48+
cur = conn.execute(text(sql), *args, **kwargs)
49+
return cur
50+
51+
@property
52+
def connection_str(self) -> str:
53+
return self._connection_pattern.format(**self._config)

src/aloha/service/web.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
34
from tornado import web, httpserver
45
from tornado.routing import HostMatches
56

@@ -40,8 +41,12 @@ def init_handlers(config: dict):
4041
for m in modules:
4142
_handlers = _load_handlers(m)
4243
for h in _handlers:
44+
(url, class_handler) = h
4345
handlers.append(h)
44-
LOG.debug('Loaded module %s handler for API:\t%s' % h[::-1])
46+
s_log_msg = 'Loaded API module %-50s' % url
47+
if LOG.level < logging.INFO: # more verbose information
48+
s_log_msg += '\t from class %s' % str(class_handler)
49+
LOG.info(s_log_msg)
4550

4651
return [
4752
(HostMatches('(.*)'), handlers)

src/aloha/util/sys_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ..logger import LOG
99

10-
LOG.info('Using psutil == %s' % psutil.__version__)
10+
LOG.debug('Using psutil == %s' % psutil.__version__)
1111

1212

1313
def get_size(bytes, suffix="B"):

0 commit comments

Comments
 (0)