]> git.datanom.net - securemail.git/commitdiff
Some documentation
authorMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 21:28:34 +0000 (23:28 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 21:28:34 +0000 (23:28 +0200)
config.py
db.py

index a0c1353445d29b20c1820be855c6c7de37ac636f..bd716427924b7c19523817d58d963ccdefde8b65 100644 (file)
--- a/config.py
+++ b/config.py
 # You should have received a copy of the GNU General Public License
 # along with SecureMail.  If not, see <https://www.gnu.org/licenses/>.
 
+######   REQUIREMENTS   #######
+# python3-nacl                #
+# python3-mysqldb             #
+# python3-psycopg2            #
+# python3-apsw                #
+# mysql, postgresql or sqlite #
+###############################
+
 #DBTYPE = "mysql"
-DBTYPE = "postgresql"
-DBHOST = "localhost"
-#DBPORT = 3306
-DBPORT = 5432
-DBUID = "backend"
-DBPWD = "clV77B2ZJQxr"
-DBNAME = "securemail"
+#DBHOST = "localhost"       # default value
+#DBPORT = 3306              # default value
+#DBTYPE = "postgresql"
+#DBHOST = "localhost"       # default value
+#DBPORT = 5432              # default value
+DBTYPE = "sqlite"
+#DBUID = "backend"          # default value
+#DBPWD = "clV77B2ZJQxr"     # default value
+DBNAME = "securemail"       # if DBTYPE is sqlite: ./DBNAME + .db
diff --git a/db.py b/db.py
index 45863bc17470f6bc56dc36b629ca27ae8b877700..9715b89a162d8201064e5bc871fd6ede5e158ae7 100644 (file)
--- a/db.py
+++ b/db.py
 # You should have received a copy of the GNU General Public License
 # along with SecureMail.  If not, see <https://www.gnu.org/licenses/>.
 
+# sqlite
+# create table account (
+# id int auto_increment,
+# token char(128) unique not null,
+# cipher text not null,
+# primary key (id));
+#
 # mysql
 # create table account (
 # id int auto_increment,
 # primary key (id));
 
 import base64
-from config import DBTYPE, DBHOST, DBPORT, DBUID, DBPWD, DBNAME
+from config import DBTYPE, DBNAME
+try:
+    from config import DBUID
+except ImportError:
+    DBUID = 'backend'
+try:
+    from config import DBPWD
+except ImportError:
+    DBPWD = 'clV77B2ZJQxr'
+try:
+    from config import DBHOST
+except ImportError:
+    DBHOST = 'localhost'
+try:
+    from config import DBPORT
+except ImportError:
+    if DBTYPE == 'mysql':
+        DBPORT = 3306
+    elif DBTYPE == 'postgresql':
+        DBPORT = 5432
 from cryptonize import Cryptonize
 
 class Singleton:
@@ -52,12 +78,14 @@ class DB:
     def get_connection(self):
         if self.conn is None:
             if DBTYPE == 'mysql':
-                #import MySQLdb
                 import MySQLdb
                 self.conn = MySQLdb.connect(host=DBHOST, port=DBPORT, user=DBUID, password=DBPWD, database=DBNAME)
             elif DBTYPE == 'postgresql':
                 import psycopg2
                 self.conn = psycopg2.connect(host=DBHOST, port=DBPORT, user=DBUID, password=DBPWD, dbname=DBNAME)
+            elif DBTYPE == 'sqlite':
+                import apsw
+                self.conn = apsw.Connection('./{0}.db'.format(DBNAME))
             else:
                 raise ValueError('{0}: Unsupported database'.format(DBTYPE))
         return self.conn
@@ -89,15 +117,25 @@ class DBInterface:
             from MySQLdb import Error as DBError
         elif DBTYPE == 'postgresql':
             from psycopg2 import Error as DBError
+        elif DBTYPE == 'sqlite':
+            from apsw import Error as DBError
         conn = DB().get_connection()
         cursor = conn.cursor()
         raw = base64.b64encode(cipher)
         try:
-            cursor.execute("insert into account(token, cipher) values(%s, %s)", (key, raw))
-            conn.commit()
+            if DBTYPE != 'sqlite':
+                cursor.execute("insert into account(token, cipher) values(%s, %s)", (key, raw))
+                conn.commit()
+            else:
+                cursor.execute('begin')
+                cursor.execute("insert into account(token, cipher) values(?, ?)", (key, raw))
+                cursor.execute('commit')
         except DBError as e:
             print (e)
-            conn.rollback()
+            if DBTYPE != 'sqlite':
+                conn.rollback()
+            else:
+                cursor.execute('rollback')
             raise e
         finally:
             cursor.close()
This page took 0.036177 seconds and 5 git commands to generate.