mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
Use setuptools to install Python modules
Instead of using relative imports, add support for installing the config and db Python modules to a proper location using setuptools. Change all git-interface scripts to access those modules from the search path. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
parent
1946486a67
commit
dc3fd60715
10 changed files with 66 additions and 42 deletions
0
aurweb/__init__.py
Normal file
0
aurweb/__init__.py
Normal file
31
aurweb/config.py
Normal file
31
aurweb/config.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import configparser
|
||||
import os
|
||||
|
||||
_parser = None
|
||||
|
||||
|
||||
def _get_parser():
|
||||
global _parser
|
||||
|
||||
if not _parser:
|
||||
_parser = configparser.RawConfigParser()
|
||||
if 'AUR_CONFIG' in os.environ:
|
||||
path = os.environ.get('AUR_CONFIG')
|
||||
else:
|
||||
relpath = "/../conf/config"
|
||||
path = os.path.dirname(os.path.realpath(__file__)) + relpath
|
||||
_parser.read(path)
|
||||
|
||||
return _parser
|
||||
|
||||
|
||||
def get(section, option):
|
||||
return _get_parser().get(section, option)
|
||||
|
||||
|
||||
def getboolean(section, option):
|
||||
return _get_parser().getboolean(section, option)
|
||||
|
||||
|
||||
def getint(section, option):
|
||||
return _get_parser().getint(section, option)
|
51
aurweb/db.py
Normal file
51
aurweb/db.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
import mysql.connector
|
||||
import sqlite3
|
||||
|
||||
import aurweb.config
|
||||
|
||||
|
||||
class Connection:
|
||||
_conn = None
|
||||
_paramstyle = None
|
||||
|
||||
def __init__(self):
|
||||
aur_db_backend = aurweb.config.get('database', 'backend')
|
||||
|
||||
if aur_db_backend == 'mysql':
|
||||
aur_db_host = aurweb.config.get('database', 'host')
|
||||
aur_db_name = aurweb.config.get('database', 'name')
|
||||
aur_db_user = aurweb.config.get('database', 'user')
|
||||
aur_db_pass = aurweb.config.get('database', 'password')
|
||||
aur_db_socket = aurweb.config.get('database', 'socket')
|
||||
self._conn = mysql.connector.connect(host=aur_db_host,
|
||||
user=aur_db_user,
|
||||
passwd=aur_db_pass,
|
||||
db=aur_db_name,
|
||||
unix_socket=aur_db_socket,
|
||||
buffered=True)
|
||||
self._paramstyle = mysql.connector.paramstyle
|
||||
elif aur_db_backend == 'sqlite':
|
||||
aur_db_name = aurweb.config.get('database', 'name')
|
||||
self._conn = sqlite3.connect(aur_db_name)
|
||||
self._paramstyle = sqlite3.paramstyle
|
||||
else:
|
||||
raise ValueError('unsupported database backend')
|
||||
|
||||
def execute(self, query, params=()):
|
||||
if self._paramstyle in ('format', 'pyformat'):
|
||||
query = query.replace('%', '%%').replace('?', '%s')
|
||||
elif self._paramstyle == 'qmark':
|
||||
pass
|
||||
else:
|
||||
raise ValueError('unsupported paramstyle')
|
||||
|
||||
cur = self._conn.cursor()
|
||||
cur.execute(query, params)
|
||||
|
||||
return cur
|
||||
|
||||
def commit(self):
|
||||
self._conn.commit()
|
||||
|
||||
def close(self):
|
||||
self._conn.close()
|
Loading…
Add table
Add a link
Reference in a new issue