print traceback even when handling exception
[mevemon] / package / src / mevemon.py
index 602f3b1..00ebe20 100755 (executable)
@@ -24,6 +24,12 @@ from eveapi import eveapi
 import fetchimg
 import apicache
 import os.path
+import traceback
+
+#conic is used for connection handling
+import conic
+#import socket for handling socket exceptions
+import socket
 
 # we will store our preferences in gconf
 import gnome.gconf
@@ -40,10 +46,16 @@ class mEveMon():
     abstract the EVE API and settings code from the UI code.
 
     """
+
+    GCONF_DIR = "/apps/maemo/mevemon"
+
     def __init__(self):
         self.program = hildon.Program()
         self.program.__init__()
         self.gconf = gnome.gconf.client_get_default()
+        #NOTE: remove this after a few releases
+        self.update_settings()
+        self.connect()
         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
                 apicache.cache_handler(debug=False))
         self.gui = gui.mEveMonUI(self)
@@ -54,12 +66,26 @@ class mEveMon():
     def quit(self, *args):
         gtk.main_quit()
 
+    def update_settings(self):
+        """
+        Update from the old pre 0.3 settings to the new settings layout.
+        We should remove this eventually, once no one is using pre-0.3 mEveMon
+        """
+        uid = self.gconf.get_string("%s/eve_uid" % self.GCONF_DIR)
+        
+        if uid:
+            key = self.gconf.get_string("%s/eve_api_key" % self.GCONF_DIR)
+            self.add_account(uid, key)
+            self.gconf.unset("%s/eve_uid" % self.GCONF_DIR)
+            self.gconf.unset("%s/eve_api_key" % self.GCONF_DIR)
+
+
     def get_accounts(self):
         """
         Returns a dictionary containing uid:api_key pairs gathered from gconf
         """
         accounts = {}
-        entries = self.gconf.all_entries("/apps/maemo/mevemon/accounts")
+        entries = self.gconf.all_entries("%s/accounts" % self.GCONF_DIR)
 
         for entry in entries:
             key = os.path.basename(entry.get_key())
@@ -72,19 +98,19 @@ class mEveMon():
         """
         Returns the api key associated with the given uid.
         """
-        return self.gconf.get_string("/apps/maemo/mevemon/accounts/%s" % uid) or ''
+        return self.gconf.get_string("%s/accounts/%s" % (self.GCONF_DIR, uid)) or ''
 
     def remove_account(self, uid):
         """
         Removes the provided uid key from gconf
         """
-        self.gconf.unset("/apps/maemo/mevemon/accounts/%s" % uid)
+        self.gconf.unset("%s/accounts/%s" % (self.GCONF_DIR, uid))
 
     def add_account(self, uid, api_key):
         """
         Adds the provided uid:api_key pair to gconf.
         """
-        self.gconf.set_string("/apps/maemo/mevemon/accounts/%s" % uid, api_key)
+        self.gconf.set_string("%s/accounts/%s" % (self.GCONF_DIR, uid), api_key)
 
     def get_auth(self, uid):
         """
@@ -95,7 +121,8 @@ class mEveMon():
 
         try:
             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
-        except eveapi.Error:
+        except:
+            traceback.print_exc()
             return None
 
         return auth
@@ -107,8 +134,9 @@ class mEveMon():
         """
         try:
             sheet = self.get_auth(uid).character(char_id).CharacterSheet()
-        except eveapi.Error:
+        except:
             # TODO: we should really have a logger that logs this error somewhere
+            traceback.print_exc()
             return None
 
         return sheet
@@ -144,7 +172,8 @@ class mEveMon():
         try:
             chars = self.cached_api.eve.CharacterName(ids=char_id).characters
             name = chars[0].characterName
-        except eveapi.Error:
+        except:
+            traceback.print_exc()
             return None
 
         return name
@@ -159,15 +188,33 @@ class mEveMon():
         try:
             chars = self.cached_api.eve.CharacterID(names=name).characters
             char_id = chars[0].characterID
-        except eveapi.Error:
+            char_name = chars[0].name
+        except:
+            traceback.print_exc()
             return None
 
         return char_id
 
-    
+    def get_chars_from_acct(self, uid):
+        """
+        Returns a list of characters associated with the provided user ID.
+        """
+        auth = self.get_auth(uid)
+        if not auth:
+            return None
+        else:
+            try:
+                api_char_list = auth.account.Characters()
+                char_list = [char.name for char in api_char_list.characters]
+            except:
+                traceback.print_exc()
+                return None
+
+        return char_list
+
     def get_characters(self):
         """
-        Returns a list of (character_name, image_path) pairs from all the
+        Returns a list of (character_name, image_path, uid) tuples from all the
         accounts that are registered to mEveMon.
         
         If there is an authentication issue, then instead of adding a valid
@@ -183,24 +230,25 @@ class mEveMon():
         if not acct_dict:
             return [placeholder_chars]
 
-        for uid, api_key in acct_dict.items():
-            auth = self.cached_api.auth(userID=uid, apiKey=api_key)
-            try:
-                api_char_list = auth.account.Characters()
+        for uid in acct_dict.keys():
+            char_names = self.get_chars_from_acct(uid)
+            
+            if not char_names:
+                ui_char_list.append(placeholder_chars)
+            else:
                 # append each char we get to the list we'll return to the
                 # UI --danny
-                for character in api_char_list.characters:
-                    ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ), uid) )
-            except eveapi.Error:
-                ui_char_list.append(placeholder_chars)
-
+                for char_name in char_names:
+                    ui_char_list.append((char_name, self.get_portrait(char_name, 64) , uid) )
+        
         return ui_char_list
 
     def get_portrait(self, char_name, size):
         """
-        Returns the relative path of the retrieved portrait
+        Returns the file path of the retrieved portrait
         """
         char_id = self.char_name2id(char_name)
+        
         return fetchimg.portrait_filename(char_id, size)
 
     def get_skill_tree(self):
@@ -209,7 +257,8 @@ class mEveMon():
         """
         try:
             tree = self.cached_api.eve.SkillTree()
-        except eveapi.Error:
+        except:
+            traceback.print_exc()
             return None
         
         return tree
@@ -222,11 +271,22 @@ class mEveMon():
         """
         try:
             skill = self.get_auth(uid).character(char_id).SkillInTraining()
-        except eveapi.Error:
+        except:
+            traceback.print_exc()
             return None
 
         return skill
 
+    def connection_cb(self, connection, event, mgc):
+        pass    
+
+
+    def connect(self):
+        connection = conic.Connection()
+        #why 0xAA55?
+        connection.connect("connection-event", self.connection_cb, 0xAA55)
+        assert(connection.request_connection(conic.CONNECT_FLAG_NONE))
+
 
 if __name__ == "__main__":
     app = mEveMon()