ppf: Add configuration file for database connection. Modify
authorMariana Marasoiu <mariana.marasoiu@gmail.com>
Tue, 9 Aug 2011 10:17:13 +0000 (13:17 +0300)
committerroot p2p-next-02 <root@p2p-next-02.grid.pub.ro>
Wed, 10 Aug 2011 08:40:55 +0000 (11:40 +0300)
DatabaseAccess.py to use config file.

ppf/db-mysql/DatabaseAccess.py
ppf/db-mysql/database.cnf [new file with mode: 0644]

index 2e10578..9f18c5e 100644 (file)
@@ -3,6 +3,7 @@
 import sys
 import MySQLdb
 import os.path
+import yaml
 
 DEBUG = False
 
@@ -19,17 +20,34 @@ class DatabaseAccess:
     """
     operators={'eq':'=', 'neq':'<>', 'gt':'>', 'gte':'>=', 'lt':'<', 'lte':'<=', 'lk':'LIKE'}
 
-    def __init__ (self, dbname):
-        self.dbname = dbname
+    def __init__ (self, dbconf):
+        self.dbconf = dbconf
 
     def connect(self):
-#      if not os.path.isfile(self.dbname):
-#              return False
-        self.conn = MySQLdb.Connection(db = self.dbname,
-                                    user = "root",
-                                    passwd = "p2p4th3m42232")
-        self.cursor = self.conn.cursor()
-        return True
+        if not os.path.isfile(self.dbconf):
+            print("Configuration file database.cnf doesn't exist!")
+            return False
+        f=open("database.cnf")
+        confMap=yaml.load(f)
+        f.close()
+        try:
+            if ('port' in confMap['configuration']):
+                self.conn = MySQLdb.Connection(db = confMap['configuration']['database'],
+                                        user = confMap['configuration']['username'],
+                                        host = confMap['configuration']['hostname'],
+                                        passwd = confMap['configuration']['password'],
+                                        port = confMap['configuration']['port'])
+                self.cursor = self.conn.cursor()
+                return True
+            else:
+                self.conn = MySQLdb.Connection(db = confMap['configuration']['database'],
+                                        user = confMap['configuration']['username'],
+                                        host = confMap['configuration']['hostname'],
+                                        passwd = confMap['configuration']['password'])
+                self.cursor = self.conn.cursor()
+                return True
+        except MySQLdb.Error, e:
+            print "[select] error: ", e.args[0]
 
     def disconnect(self):
         self.cursor.close()
@@ -303,7 +321,7 @@ def main():
     """
 
     if len(sys.argv) != 2:
-        print "Usage: python DatabaseAccess dbfile"
+        print "Usage: python DatabaseAccess dbconf_file"
         sys.exit(2)
 
     dba = DatabaseAccess(sys.argv[1])
@@ -356,3 +374,4 @@ def main():
 
 if __name__ == "__main__":
     sys.exit(main())
+
diff --git a/ppf/db-mysql/database.cnf b/ppf/db-mysql/database.cnf
new file mode 100644 (file)
index 0000000..4a9a299
--- /dev/null
@@ -0,0 +1,10 @@
+# Connection configuration file for MySQL database parsers
+# Use YAML data serialization standard
+# Options: adapter, database, hostname, username, password, port
+
+configuration:
+        adapter:  mysql
+        database: p2p_logs_test
+        hostname: localhost
+        username: p2p
+        password: p2pn3xt