[gevice] enable run app with line command parameter (filename sqlite)



commit 2a6a5a16d59501f9a5772b4f557e2a495d9430b9
Author: Alejandro ValdÃs Jimenez <avaldes gnome org>
Date:   Tue Dec 18 00:57:42 2012 -0300

    enable run app with line command parameter (filename sqlite)

 ChangeLog             |    4 +++
 data/other/testing.db |  Bin 5120 -> 5120 bytes
 src/gevice.py         |   62 ++++++++++++++++++++++++++++++------------------
 src/gevicedatabase.py |   31 +++++++++++++++--------
 4 files changed, 63 insertions(+), 34 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 3b7d3f2..4f95461 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,8 @@
 2012-12-18  Alejandro ValdéJimenez  <avaldes gnome org>
+	* src/gevice.py: enable run app with line command parameter (filename sqlite)
+	* src/gevicedatabase.py:
+
+2012-12-18  Alejandro ValdéJimenez  <avaldes gnome org>
 	* data/other/testing.db: testing
 	* src/test.py: add script to test gevicedatabase.py
 
diff --git a/data/other/testing.db b/data/other/testing.db
index 37a654f..53f10cd 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 dcfb24c..439dd19 100755
--- a/src/gevice.py
+++ b/src/gevice.py
@@ -18,6 +18,7 @@
 
 from gi.repository import Gtk, GdkPixbuf, Vte, GLib
 
+import sys
 import os
 import os.path
 
@@ -38,7 +39,8 @@ gettext.bind_textdomain_codeset (config.PACKAGE, "UTF-8");
 from gettext import gettext as _
 
 class Gevice:
-  def __init__ (self):
+  def __init__ (self,openfilename=None):
+    self.dbsqlite = openfilename
     self.connected_to_database = False
     self.context_id = None
     self.session = "ssh"
@@ -127,6 +129,10 @@ class Gevice:
     # show all
     self.window_main.show_all()
     self.infobar.hide()
+
+    if self.dbsqlite:
+      self.on_action_open(None)
+      
     return
   
   def create_actionsgroups (self):
@@ -275,30 +281,37 @@ class Gevice:
     self.gmodel.treeview.set_model(None)
     
     if (not self.connected_to_database):
-      dbsqlite = self.get_filename()
-      if (dbsqlite):
-        self.gdbase = gevicedatabase.GeviceDatabase (dbsqlite)
-        tree = self.gdbase.get_data()
+      if (self.dbsqlite == None):
+        self.dbsqlite = self.get_filename()
+
+      if (self.dbsqlite):
+        self.gdbase = gevicedatabase.GeviceDatabase (self.dbsqlite)
         
-        self.gmodel.treeview.set_model(self.gmodel.treestore)
+        if (self.gdbase.valid_file == False):
+          result = self.show_infobar_message (_("File not valid"), Gtk.MessageType.ERROR)
+          self.dbsqlite = None
+        else:
+          tree = self.gdbase.get_data()
         
-        for n in tree.children:
-          itern = self.gmodel.treestore.append(None)
-          self.gmodel.insert_new_item (itern,n.name,n.ip)
-          self.insert_data_into_model(n, itern)
+          self.gmodel.treeview.set_model(self.gmodel.treestore)
         
-        self.gmodel.treeview.expand_all()
-        self.actiongroup_model.set_sensitive(True)
-        self.actiongroup_device.set_sensitive (True)
+          for n in tree.children:
+            itern = self.gmodel.treestore.append(None)
+            self.gmodel.insert_new_item (itern,n.name,n.ip)
+            self.insert_data_into_model(n, itern)
         
-        self.update_statusbar (dbsqlite)
-        self.actiongroup_device.set_sensitive (False)
+          self.gmodel.treeview.expand_all()
+          self.actiongroup_model.set_sensitive(True)
+          self.actiongroup_device.set_sensitive (True)
         
-        action_open = self.actiongroup_window.get_action("Open")
-        action_open.set_property ("stock-id",Gtk.STOCK_CONNECT)
-        action_open.set_property ("tooltip",_("Disconnect from database"))
-        action_open.set_property ("label",_("Disconnect"))
-        self.connected_to_database = True
+          self.update_statusbar (self.dbsqlite)
+          self.actiongroup_device.set_sensitive (False)
+        
+          action_open = self.actiongroup_window.get_action("Open")
+          action_open.set_property ("stock-id",Gtk.STOCK_CONNECT)
+          action_open.set_property ("tooltip",_("Disconnect from database"))
+          action_open.set_property ("label",_("Disconnect"))
+          self.connected_to_database = True
     else:
       action_open = self.actiongroup_window.get_action("Open")
       action_open.set_property ("stock-id",Gtk.STOCK_DISCONNECT)
@@ -309,6 +322,7 @@ class Gevice:
       self.actiongroup_model.set_sensitive (False)
       self.actiongroup_device.set_sensitive (False)
       self.connected_to_database = False
+      self.dbsqlite = None
       
       self.update_statusbar("")
     
@@ -379,8 +393,6 @@ class Gevice:
 
     for connected in self.devices_to_connect:
       self.gdbase.insert_connections(connected)
-
-    ###self.gdbase.conn.commit()
     
     ### FIXME: Validate to error
     result = self.show_infobar_message (_("Model saved"), Gtk.MessageType.INFO)
@@ -541,5 +553,9 @@ class Gevice:
     self.gevicefind.show_interface()
 
 if __name__ == "__main__":
-  gevice = Gevice()
+  if len(sys.argv)>1:
+    gevice = Gevice(sys.argv[1])
+  else:
+    gevice = Gevice()
+    
   gevice.main()
diff --git a/src/gevicedatabase.py b/src/gevicedatabase.py
index 0d66e6e..16b1506 100644
--- a/src/gevicedatabase.py
+++ b/src/gevicedatabase.py
@@ -54,7 +54,7 @@ class Node(object):
 class GeviceDatabase:
   def __init__ (self,dbsqlite):
     """
-    @parama dbsqlite The filename of sqlite database.
+    @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')
@@ -62,19 +62,28 @@ class GeviceDatabase:
     self.cur = None
     self.devices_to_connect = []
     self.treedevice = Node("root", None)
+    self.valid_file = False
+    
+    self.valid_file = self.__validate_file()
+    if self.valid_file:
+      self.__connect()
+      if (self.conn):
+        self.cur = self.conn.cursor()
+          
+        if (self.__if_enable_foreign_key() == False):
+          self.__enable_foreign_key()
+          self.load_data()
 
-    # verify valid format of file
-    with open(dbsqlite, 'rb') as handle:
+  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:
-        self.__connect()
-        
-        if (self.conn):
-          self.cur = self.conn.cursor()
-          
-          if (self.__if_enable_foreign_key() == False):
-            self.__enable_foreign_key()
-            self.load_data()
+        return True
+      else:
+        return False
 
   def get_data(self):
     """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]