improving our pylint scores
[mevemon] / package / src / mevemon.py
1 #!/usr/bin/env python
2 #
3 # mEveMon - A character monitor for EVE Online
4 # Copyright (c) 2010  Ryan and Danny Campbell, and the mEveMon Team
5 #
6 # mEveMon is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # mEveMon is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20
21 import hildon
22 import gtk
23 from eveapi import eveapi
24 import fetchimg
25 import apicache
26 import os.path
27
28 # we will store our preferences in gconf
29 import gnome.gconf
30
31 #ugly hack to check maemo version. any better way?
32 if hasattr(hildon, "StackableWindow"):
33     from ui.fremantle import gui
34 else:
35     from ui.diablo import gui
36
37 class mEveMon():
38     """
39     The controller class for mEvemon. The intent is to help
40     abstract the EVE API and settings code from the UI code.
41
42     """
43     def __init__(self):
44         self.program = hildon.Program()
45         self.program.__init__()
46         self.gconf = gnome.gconf.client_get_default()
47         self.cached_api = eveapi.EVEAPIConnection( cacheHandler = \
48                 apicache.cache_handler(debug=False))
49         self.gui = gui.mEveMonUI(self)
50
51     def run(self):
52         gtk.main()
53     
54     def quit(self, *args):
55         gtk.main_quit()
56
57     def get_accounts(self):
58         """
59         Returns a dictionary containing uid:api_key pairs gathered from gconf
60         """
61         accounts = {}
62         entries = self.gconf.all_entries("/apps/maemo/mevemon/accounts")
63
64         for entry in entries:
65             key = os.path.basename(entry.get_key())
66             value = entry.get_value().to_string()
67             accounts[key] = value
68
69         return accounts
70         
71     def get_api_key(self, uid):
72         """
73         Returns the api key associated with the given uid.
74         """
75         return self.gconf.get_string("/apps/maemo/mevemon/accounts/%s" % uid) or ''
76
77     def remove_account(self, uid):
78         """
79         Removes the provided uid key from gconf
80         """
81         self.gconf.unset("/apps/maemo/mevemon/accounts/%s" % uid)
82
83     def add_account(self, uid, api_key):
84         """
85         Adds the provided uid:api_key pair to gconf.
86         """
87         self.gconf.set_string("/apps/maemo/mevemon/accounts/%s" % uid, api_key)
88
89     def get_auth(self, uid):
90         """
91         Returns an authentication object to be used for eveapi calls
92         that require authentication.
93         """
94         api_key = self.get_api_key(uid)
95
96         try:
97             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
98         except eveapi.Error:
99             return None
100
101         return auth
102
103     def get_char_sheet(self, uid, char_id):
104         """
105         Returns an object containing information about the character specified
106         by the provided character ID.
107         """
108         try:
109             sheet = self.get_auth(uid).character(char_id).CharacterSheet()
110         except eveapi.Error:
111             # TODO: we should really have a logger that logs this error somewhere
112             return None
113
114         return sheet
115
116     def charid2uid(self, char_id):
117         """
118         Takes a character ID and returns the user ID of the account containing
119         the character.
120
121         Returns None if the character isn't found in any of the registered accounts.
122
123         """
124         acct_dict = self.get_accounts()
125         
126         for uid, api_key in acct_dict.items():
127             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
128             api_char_list = auth.account.Characters()
129             
130             for character in api_char_list.characters:
131                 if character.characterID == char_id:
132                     return uid
133
134         
135         return None
136     
137     def char_id2name(self, char_id):
138         """
139         Takes a character ID and returns the character name associated with
140         that ID.
141         The EVE API accepts a comma-separated list of IDs, but for now we
142         will just handle a single ID.
143         """
144         try:
145             chars = self.cached_api.eve.CharacterName(ids=char_id).characters
146             name = chars[0].characterName
147         except eveapi.Error:
148             return None
149
150         return name
151
152     def char_name2id(self, name):
153         """
154         Takes the name of an EVE character and returns the characterID.
155         
156         The EVE api accepts a comma separated list of names, but for now
157         we will just handle single names/
158         """
159         try:
160             chars = self.cached_api.eve.CharacterID(names=name).characters
161             char_id = chars[0].characterID
162         except eveapi.Error:
163             return None
164
165         return char_id
166
167     
168     def get_characters(self):
169         """
170         Returns a list of (character_name, image_path) pairs from all the
171         accounts that are registered to mEveMon.
172         
173         If there is an authentication issue, then instead of adding a valid
174         pair to the list, it appends an 'error message' 
175
176         """
177         ui_char_list = []
178         err_img = "/usr/share/mevemon/imgs/error.jpg"
179
180         placeholder_chars = ("Please check your API settings.", err_img, "0")
181         
182         acct_dict = self.get_accounts()
183         if not acct_dict:
184             return [placeholder_chars]
185
186         for uid, api_key in acct_dict.items():
187             auth = self.cached_api.auth(userID=uid, apiKey=api_key)
188             try:
189                 api_char_list = auth.account.Characters()
190                 # append each char we get to the list we'll return to the
191                 # UI --danny
192                 for character in api_char_list.characters:
193                     ui_char_list.append( ( character.name, fetchimg.portrait_filename( character.characterID, 64 ), uid) )
194             except eveapi.Error:
195                 ui_char_list.append(placeholder_chars)
196
197         return ui_char_list
198
199     def get_portrait(self, char_name, size):
200         """
201         Returns the relative path of the retrieved portrait
202         """
203         char_id = self.char_name2id(char_name)
204         return fetchimg.portrait_filename(char_id, size)
205
206     def get_skill_tree(self):
207         """
208         Returns an object from eveapi containing skill tree info
209         """
210         try:
211             tree = self.cached_api.eve.SkillTree()
212         except eveapi.Error:
213             return None
214         
215         return tree
216
217     def get_skill_in_training(self, uid, char_id):
218         """
219         Returns an object from eveapi containing information about the
220         current skill in training
221
222         """
223         try:
224             skill = self.get_auth(uid).character(char_id).SkillInTraining()
225         except eveapi.Error:
226             return None
227
228         return skill
229
230
231 if __name__ == "__main__":
232     app = mEveMon()
233     app.run()