commit df68cc8bfb5e9448c6181a101aff505ed85aa4fc Author: Boris Tassou Date: Tue Dec 3 11:11:52 2019 +0100 First init diff --git a/manage_psql/__init__.py b/manage_psql/__init__.py new file mode 100755 index 0000000..f37056b --- /dev/null +++ b/manage_psql/__init__.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""Psql connection module.""" + +# /* +# * ---------------------------------------------------------------------------- +# * "THE BEER-WARE LICENSE" (Revision 42): +# * wrote this file. As long as you retain this notice you +# * can do whatever you want with this stuff. If we meet some day, and you think +# * this stuff is worth it, you can buy me a beer in return Boris Tassou +# * ---------------------------------------------------------------------------- +# */ + +import sys + +# Global name +__version__ = '1.0' +__author__ = 'Boris Tassou ' +__license__ = 'Beerware' + +try: + import psycopg2 +except ImportError: + print('Psycopg2 library not found. Books-shelf cannot start.') + sys.exit(1) diff --git a/manage_psql/__init__.pyc b/manage_psql/__init__.pyc new file mode 100644 index 0000000..49001b9 Binary files /dev/null and b/manage_psql/__init__.pyc differ diff --git a/manage_psql/__pycache__/__init__.cpython-35.pyc b/manage_psql/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..330800e Binary files /dev/null and b/manage_psql/__pycache__/__init__.cpython-35.pyc differ diff --git a/manage_psql/__pycache__/__init__.cpython-36.pyc b/manage_psql/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..a715e8d Binary files /dev/null and b/manage_psql/__pycache__/__init__.cpython-36.pyc differ diff --git a/manage_psql/__pycache__/manage_psql.cpython-35.pyc b/manage_psql/__pycache__/manage_psql.cpython-35.pyc new file mode 100644 index 0000000..70ad171 Binary files /dev/null and b/manage_psql/__pycache__/manage_psql.cpython-35.pyc differ diff --git a/manage_psql/__pycache__/manage_psql.cpython-36.pyc b/manage_psql/__pycache__/manage_psql.cpython-36.pyc new file mode 100644 index 0000000..fe010bd Binary files /dev/null and b/manage_psql/__pycache__/manage_psql.cpython-36.pyc differ diff --git a/manage_psql/conf.ini b/manage_psql/conf.ini new file mode 100755 index 0000000..29909f0 --- /dev/null +++ b/manage_psql/conf.ini @@ -0,0 +1,5 @@ +[psql] +host=localhost +database=books_shelf +user=books_shelf +passwd=blabli diff --git a/manage_psql/manage_psql.py b/manage_psql/manage_psql.py new file mode 100755 index 0000000..8681e60 --- /dev/null +++ b/manage_psql/manage_psql.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +"""Module psql connection.""" + +# /* +# * ---------------------------------------------------------------------------- +# * "THE BEER-WARE LICENSE" (Revision 42): +# * wrote this file. As long as you retain this notice you +# * can do whatever you want with this stuff. If we meet some day, and you think +# * this stuff is worth it, you can buy me a beer in return Boris Tassou +# * ---------------------------------------------------------------------------- +# */ + +from configparser import SafeConfigParser +import psycopg2 + + +class HandlePsql: + """Class to handle psql connection.""" + + conn = None + cur = None + + @staticmethod + def psql_conn(): + """Init psql connection.""" + # Read conf.ini + ini_conf = "/usr/local/Securmail-Administration/config/conf.ini" + config = SafeConfigParser() + config.read(ini_conf) + + # DB parameters + psql_host = config.get('psql', 'host') + psql_db = config.get('psql', 'database') + psql_user = config.get('psql', 'user') + psql_pwd = config.get('psql', 'passwd') + + # connexion DB + # http://initd.org/psycopg/docs/usage.html + # http://www.postgresqltutorial.com/postgresql-python/ + try: + global conn + conn = psycopg2.connect(host=psql_host, database=psql_db, user=psql_user, password=psql_pwd) + conn.cursor() + except (Exception, psycopg2.DatabaseError) as error: + print(error) + + @staticmethod + def psql_select(sql_select): + """Psql select request.""" + cur = conn.cursor() + cur.execute(sql_select) + sql_return = cur.fetchall() + return sql_return + + @staticmethod + def psql_delete(sql_delete): + """Psql delte request.""" + cur = conn.cursor() + cur.execute(sql_delete) + conn.commit() + + @staticmethod + def psql_insert(sql_insert): + """Psql insert request.""" + cur = conn.cursor() + cur.execute(sql_insert) + conn.commit() + + @staticmethod + def psql_update(sql_update): + """Psql insert request.""" + cur = conn.cursor() + cur.execute(sql_update) + conn.commit() + + @staticmethod + def psql_unconn(): + """End psql connection.""" + cur = conn.cursor() + cur.close() + conn.close() + +if __name__ == "__main__": + print("Module psql to handle the connection") diff --git a/manage_psql/manage_psql.pyc b/manage_psql/manage_psql.pyc new file mode 100644 index 0000000..b2f9e04 Binary files /dev/null and b/manage_psql/manage_psql.pyc differ