py.lib
2017-04-09
Child:8dcee5f5df4f
0:f35759c0fdd0 Browse Files
init
.hgignore awNet/__init__.py awNet/db/__init__.py awNet/db/pg.py awNet/db/sqlite.py awNet/webapp/__init__.py awNet/webapp/win.py
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Sun Apr 09 12:13:01 2017 +0300 1.3 @@ -0,0 +1,3 @@ 1.4 +syntax: glob 1.5 +*/__pycache__/* 1.6 +*.pyc
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/awNet/__init__.py Sun Apr 09 12:13:01 2017 +0300 2.3 @@ -0,0 +1,4 @@ 2.4 +# -*- coding: utf-8 -*- 2.5 +# --- 2.6 +# Контейнер модулей собственного производства общего назначения 2.7 +# --- 2.8 \ No newline at end of file
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/awNet/db/__init__.py Sun Apr 09 12:13:01 2017 +0300 3.3 @@ -0,0 +1,4 @@ 3.4 +# -*- coding: utf-8 -*- 3.5 +# --- 3.6 +# Модули для работы с базами данных 3.7 +# --- 3.8 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/awNet/db/pg.py Sun Apr 09 12:13:01 2017 +0300 4.3 @@ -0,0 +1,31 @@ 4.4 +import postgresql.driver as DBM 4.5 +import postgresql.exceptions as DBErr 4.6 + 4.7 +def GetDB(*, name, user, passwd, host='localhost', port=5432): 4.8 + """ 4.9 + На случай когда лениво каждый раз вызывать базу со всеми регалиями, 4.10 + регалии сохраняем в специального вызывателя :))) 4.11 + """ 4.12 + def func (): 4.13 + return DB(name, user, passwd, host=host, port=port) 4.14 + return func 4.15 + 4.16 +class DB: 4.17 + def __init__(self, db_name, db_user, db_passwd, *, host='localhost', port=5432): 4.18 + self._db = DBM.connect( 4.19 + host = host, 4.20 + port = port, 4.21 + user = db_user, 4.22 + password = db_passwd, 4.23 + database = db_name 4.24 + ) 4.25 + self.xact = self._db.xact 4.26 + self.q = self._db.query 4.27 + self.pq = self._db.prepare 4.28 + self.ex = self._db.execute 4.29 + self.proc = self._db.proc 4.30 + self.close = self._db.close 4.31 + 4.32 + def __del__(self): 4.33 + if self._db.state != 'closed': 4.34 + self._db.close() 4.35 \ No newline at end of file
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/awNet/db/sqlite.py Sun Apr 09 12:13:01 2017 +0300 5.3 @@ -0,0 +1,54 @@ 5.4 +# -*- coding: utf-8 -*- 5.5 +# Dep 5.6 +from sqlite3 import connect as _sqlite_connect 5.7 +# Exceptions 5.8 +from sqlite3 import Warning, Error, DatabaseError, IntegrityError, ProgrammingError 5.9 + 5.10 +# --- # PROCS # --- # 5.11 +def GetDB(db_file): 5.12 + def func(): 5.13 + return DB(db_file) 5.14 + return func 5.15 + 5.16 +# --- # CLASS # --- # 5.17 +class DB: 5.18 + def __init__(self, db_file): 5.19 + self._con = None 5.20 + self._con = _sqlite_connect(db_file) 5.21 + self.q = self._con.execute 5.22 + self.qm = self._con.executemany 5.23 + self.commit = self._con.commit 5.24 + self.rollback = self._con.rollback 5.25 + 5.26 + # DB PREP 5.27 + self.cq("PRAGMA journal=WAL") 5.28 + 5.29 + def 5.30 + 5.31 + def __del__(self): 5.32 + if self._con == None: return 5.33 + try: 5.34 + self.rollback() 5.35 + self._con.close() 5.36 + except Error: 5.37 + pass 5.38 + except Warning: 5.39 + pass 5.40 + 5.41 + def cq(self, *a, **wa): 5.42 + try: 5.43 + res = self.q(*a, **wa) 5.44 + self.commit() 5.45 + return res 5.46 + except Exception as e: 5.47 + self.rollback() 5.48 + raise e 5.49 + 5.50 + def cqm(self, *a, **wa): 5.51 + try: 5.52 + res = self.qm(*a, **wa) 5.53 + self.commit() 5.54 + return res 5.55 + except Exception as e: 5.56 + self.rollback() 5.57 + raise e 5.58 \ No newline at end of file