[gevice] added validations (sql, IO)
- From: Alejandro Valdes Jimenez <avaldes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gevice] added validations (sql, IO)
- Date: Tue, 18 Dec 2012 19:09:19 +0000 (UTC)
commit 9f07c66ad956c23e89a15e2774fc9419efb5c707
Author: Alejandro ValdÃs Jimenez <avaldes gnome org>
Date: Tue Dec 18 16:04:24 2012 -0300
added validations (sql, IO)
ChangeLog | 6 ++
data/other/testing.db | Bin 5120 -> 5120 bytes
src/gevice.py | 46 +++++++++++--------
src/gevicedatabase.py | 124 ++++++++++++++++++++++++++++++++++---------------
src/test.py | 5 +-
5 files changed, 121 insertions(+), 60 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index a92600e..65895aa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,10 @@
2012-12-18 Alejandro ValdéJimenez <avaldes gnome org>
+ * data/other/testing.db:
+ * src/gevice.py:
+ * src/gevicedatabase.py: added validations (sql, IO)
+ * src/test.py:
+
+2012-12-18 Alejandro ValdéJimenez <avaldes gnome org>
* gevice.in: update
2012-12-18 Alejandro ValdéJimenez <avaldes gnome org>
diff --git a/data/other/testing.db b/data/other/testing.db
index 53f10cd..de1de80 100644
Binary files a/data/other/testing.db and b/data/other/testing.db differ
diff --git a/src/gevice.py b/src/gevice.py
index 439dd19..8ae6c32 100755
--- a/src/gevice.py
+++ b/src/gevice.py
@@ -286,12 +286,11 @@ class Gevice:
if (self.dbsqlite):
self.gdbase = gevicedatabase.GeviceDatabase (self.dbsqlite)
-
- if (self.gdbase.valid_file == False):
- result = self.show_infobar_message (_("File not valid"), Gtk.MessageType.ERROR)
+ if (self.gdbase.file_is_valid() == False):
+ result = self.show_infobar_message (self.gdbase.get_error(), Gtk.MessageType.ERROR)
self.dbsqlite = None
- else:
- tree = self.gdbase.get_data()
+ elif (self.gdbase.is_connected()):
+ tree = self.gdbase.get_tree()
self.gmodel.treeview.set_model(self.gmodel.treestore)
@@ -312,6 +311,9 @@ class Gevice:
action_open.set_property ("tooltip",_("Disconnect from database"))
action_open.set_property ("label",_("Disconnect"))
self.connected_to_database = True
+ else:
+ result = self.show_infobar_message (_("Not connected to database"), Gtk.MessageType.ERROR)
+ self.dbsqlite = None
else:
action_open = self.actiongroup_window.get_action("Open")
action_open.set_property ("stock-id",Gtk.STOCK_DISCONNECT)
@@ -391,27 +393,33 @@ class Gevice:
# save devices into database
self.gmodel.treestore.foreach(self.copy_device_to_dbase,None)
- for connected in self.devices_to_connect:
- self.gdbase.insert_connections(connected)
+ if (self.gdbase.get_status_sql()):
+ for connected in self.devices_to_connect:
+ self.gdbase.insert_connections(connected)
- ### FIXME: Validate to error
- result = self.show_infobar_message (_("Model saved"), Gtk.MessageType.INFO)
- ###result = self.show_infobar_message (_("Model Not saved"), Gtk.MessageType.ERROR)
+ if (self.gdbase.get_status_sql()):
+ self.gdbase.apply_commit()
+ result = self.show_infobar_message (_("Model saved"), Gtk.MessageType.INFO)
+ else:
+ self.gdbase.apply_rollback()
+ result = self.show_infobar_message (self.gdbase.get_error(), Gtk.MessageType.ERROR)
def copy_device_to_dbase (self,model,path,iter,data):
data = self.gmodel.treestore.get(iter,0,1)
self.gdbase.insert_device (data)
-
- if (self.gmodel.treestore.iter_has_child(iter)):
- iterc = self.gmodel.treestore.iter_children(iter)
- next = iterc
+ if (self.gdbase.get_status_sql() == False):
+ return
+ else:
+ if (self.gmodel.treestore.iter_has_child(iter)):
+ iterc = self.gmodel.treestore.iter_children(iter)
+ next = iterc
- while next:
- child = self.gmodel.treestore.get(next,0)
- self.devices_to_connect.append ("'" + data[0] + "','" + child[0] + "'")
- next2 = self.gmodel.treestore.iter_next(next)
- next = next2
+ while next:
+ child = self.gmodel.treestore.get(next,0)
+ self.devices_to_connect.append ("'" + data[0] + "','" + child[0] + "'")
+ next2 = self.gmodel.treestore.iter_next(next)
+ next = next2
def on_toggle_tree (self,action):
if (action.get_active() == True):
diff --git a/src/gevicedatabase.py b/src/gevicedatabase.py
index 16b1506..8763bc4 100644
--- a/src/gevicedatabase.py
+++ b/src/gevicedatabase.py
@@ -57,75 +57,119 @@ class GeviceDatabase:
@param dbsqlite The filename of sqlite database.
"""
# SQLite format 3 (header string)
- self.magic = ('\x53', '\x51', '\x4c', '\x69', '\x74', '\x65', '\x20', '\x66', '\x6f', '\x72', '\x6d', '\x61', '\x74', '\x20', '\x33', '\x00')
- self.dbsqlite = dbsqlite
- self.cur = None
- self.devices_to_connect = []
- self.treedevice = Node("root", None)
- self.valid_file = False
+ self.__magic = ('\x53', '\x51', '\x4c', '\x69', '\x74', '\x65', '\x20', '\x66', '\x6f', '\x72', '\x6d', '\x61', '\x74', '\x20', '\x33', '\x00')
+ self.__dbsqlite = dbsqlite
+ self.__cur = None
+ self.__treedevice = Node("root", None)
+ self.__valid_file = False
+ self.__conn = None
+ self.__status_sql = False
+ self.__error_string = None
- self.valid_file = self.__validate_file()
- if self.valid_file:
+ self.__validate_file()
+ if self.__valid_file:
self.__connect()
- if (self.conn):
- self.cur = self.conn.cursor()
+ if (self.__conn):
+ self.__cur = self.__conn.cursor()
if (self.__if_enable_foreign_key() == False):
self.__enable_foreign_key()
- self.load_data()
+ self.__load_data()
+
+ def get_error(self):
+ """
+ Return the error occurred.
+ """
+ return self.__error_string
+
+ def apply_commit(self):
+ """
+ Apply commit.
+ """
+ self.__conn.commit()
+ print "commit()"
+
+ def apply_rollback(self):
+ """
+ Rollback transactions.
+ """
+ self.__conn.rollback()
+ print "rollback()"
+
+ def get_status_sql(self):
+ """
+ Return status of SQL executed.
+ """
+ return self.__status_sql
+
+ def is_connected(self):
+ """
+ Return status of connection.
+ """
+ return self.__conn
def __validate_file(self):
"""
Verify valid format of file
"""
- with open(self.dbsqlite, 'rb') as handle:
- s = unpack('cccccccccccccccc', handle.read(16))
- if s == self.magic:
- return True
- else:
- return False
+ try:
+ with open(self.__dbsqlite, 'rb') as handle:
+ s = unpack('cccccccccccccccc', handle.read(16))
+ if s == self.__magic:
+ self.__valid_file = True
+ except IOError as e:
+ print "I/O error({0}): {1}".format(e.errno, e.strerror)
+ self.__error_string = "I/O error({0}): {1}".format(e.errno, e.strerror)
- def get_data(self):
+ def get_tree(self):
"""
Return a tree of devices.
"""
- return self.treedevice
+ return self.__treedevice
+
+ def file_is_valid(self):
+ """
+ Return if file is a valid sqlite3.
+ """
+ return self.__valid_file
def __connect (self):
"""
Connect to database.
- """
- self.conn = sqlite3.connect(self.dbsqlite)
+ """
+ try:
+ self.__conn = sqlite3.connect(self.__dbsqlite)
+ except sqlite3.Error as e:
+ print "An error occurred:", e.args[0]
def __enable_foreign_key (self):
"""
Enable foreign key.
"""
- self.cur.execute ("PRAGMA foreign_keys = ON ")
+ self.__cur.execute ("PRAGMA foreign_keys = ON ")
def __if_enable_foreign_key(self):
"""
Verify if foreign key is enabled.
"""
- self.cur.execute ("PRAGMA foreign_keys")
- rows = self.cur.fetchall()
+ self.__cur.execute ("PRAGMA foreign_keys")
+ rows = self.__cur.fetchall()
if (rows[0][0] == 0):
return False
else:
return True
- def execute_sql_select (self,sql):
+ def __execute_sql_select (self,sql):
"""
Execute a select SQL.
@param sql to execute.
"""
- self.cur.execute(sql)
- rows = self.cur.fetchall()
- self.conn.commit()
+ self.__cur.execute(sql)
+ rows = self.__cur.fetchall()
return rows
- def load_data (self):
+ def __load_data (self):
"""
Read and load the data into tree of devices.
"""
@@ -133,14 +177,14 @@ class GeviceDatabase:
sql = "select device.id_dev from device where device.id_dev "
sql = sql + "not in (select id_devc from connect order by id_devc)"
- rows = self.execute_sql_select(sql)
+ rows = self.__execute_sql_select(sql)
if rows:
# for each root device, get your children
token = None
for row in rows:
if (not row[0] == token):
- self.__process_row (row[0], self.treedevice)
+ self.__process_row (row[0], self.__treedevice)
token = row[0]
def __process_row (self,device,rootnode):
@@ -152,14 +196,14 @@ class GeviceDatabase:
# get data of device
sql = "select id_dev, ip_dev from device where id_dev='" + str(device) + "'"
- row = self.execute_sql_select(sql)
+ row = self.__execute_sql_select(sql)
if row:
node = Node(row[0][0], row[0][1])
rootnode.add_child(node)
# get children of device
sql2 = "select id_devc from connect where id_devp='" + str(device) + "'"
- rows = self.execute_sql_select(sql2)
+ rows = self.__execute_sql_select(sql2)
if rows:
for children in rows:
@@ -179,8 +223,7 @@ class GeviceDatabase:
"""
sql = "insert into device (id_dev,ip_dev) "
sql = sql + "values ('" + data[0] + "','" + data[1] + "');"
- st = self.__execute_sql(sql)
- self.conn.commit()
+ self.__execute_sql(sql)
def insert_connections (self,links):
"""
@@ -189,11 +232,16 @@ class GeviceDatabase:
"""
sql = "insert into connect (id_devp,id_devc) values (" + links + ");"
self.__execute_sql (sql)
- self.conn.commit()
def __execute_sql (self,sql):
"""
Execute a SQL.
"""
- self.cur.execute(sql)
- self.conn.commit()
+ self.__status_sql = False
+
+ try:
+ self.__cur.execute(sql)
+ self.__status_sql = True
+ except sqlite3.Error as e:
+ print "An error occurred -----:", e.args[0]
+ self.__error_string = e.args[0]
diff --git a/src/test.py b/src/test.py
index bf8d7ed..46f0b3f 100644
--- a/src/test.py
+++ b/src/test.py
@@ -11,12 +11,11 @@ def show_data(tree):
process_node(n,"--")
dbase = gevicedatabase.GeviceDatabase("../data/other/testing.db")
-tree = dbase.get_data()
+tree = dbase.get_tree()
+show_data(tree)
#data = ("appn6","127.0.0.1")
#dbase.insert_device(data)
#link = "'distn2','appn6'"
#dbase.insert_connections(link)
-tree = dbase.get_data()
-show_data(tree)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]