Package entropy :: Package services :: Module skel

Source Code for Module entropy.services.skel

  1  # -*- coding: utf-8 -*- 
  2  ''' 
  3      # DESCRIPTION: 
  4      # Entropy Object Oriented Interface 
  5   
  6      Copyright (C) 2007-2009 Fabio Erculiani 
  7   
  8      This program is free software; you can redistribute it and/or modify 
  9      it under the terms of the GNU General Public License as published by 
 10      the Free Software Foundation; either version 2 of the License, or 
 11      (at your option) any later version. 
 12   
 13      This program is distributed in the hope that it will be useful, 
 14      but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16      GNU General Public License for more details. 
 17   
 18      You should have received a copy of the GNU General Public License 
 19      along with this program; if not, write to the Free Software 
 20      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 21  ''' 
 22   
 23  from entropy.exceptions import * 
 24  from entropy.i18n import _ 
 25   
26 -class SocketCommands:
27
28 - def __str__(self):
29 return self.inst_name
30 31 inst_name = 'command-skel'
32 - def __init__(self, HostInterface, inst_name = 'command-skel'):
33 self.HostInterface = HostInterface 34 self.no_acked_commands = [] 35 self.termination_commands = [] 36 self.initialization_commands = [] 37 self.login_pass_commands = [] 38 self.no_session_commands = [] 39 self.raw_commands = [] 40 self.config_commands = [] 41 self.valid_commands = set() 42 self.inst_name = inst_name
43
44 - def register( 45 self, 46 valid_commands, 47 no_acked_commands, 48 termination_commands, 49 initialization_commands, 50 login_pass_commads, 51 no_session_commands, 52 raw_commands, 53 config_commands 54 ):
55 valid_commands.update(self.valid_commands) 56 no_acked_commands.extend(self.no_acked_commands) 57 termination_commands.extend(self.termination_commands) 58 initialization_commands.extend(self.initialization_commands) 59 login_pass_commads.extend(self.login_pass_commands) 60 no_session_commands.extend(self.no_session_commands) 61 raw_commands.extend(self.raw_commands) 62 config_commands.extend(self.config_commands)
63
64 -class SocketAuthenticator:
65
66 - def __init__(self, HostInterface):
67 self.HostInterface = HostInterface 68 self.session = None
69
70 - def set_session(self, session):
71 self.session = session
72
73 -class RemoteDatabase:
74
75 - def __init__(self):
76 self.dbconn = None 77 self.cursor = None 78 self.plain_cursor = None 79 self.connection_data = {} 80 try: 81 import MySQLdb, _mysql_exceptions 82 from MySQLdb.constants import FIELD_TYPE 83 from MySQLdb.converters import conversions 84 except ImportError: 85 raise LibraryNotFound('LibraryNotFound: dev-python/mysql-python not found') 86 self.mysql = MySQLdb 87 self.mysql_exceptions = _mysql_exceptions 88 self.FIELD_TYPE = FIELD_TYPE 89 self.conversion_dict = conversions.copy() 90 self.conversion_dict[self.FIELD_TYPE.DECIMAL] = int 91 self.conversion_dict[self.FIELD_TYPE.LONG] = int 92 self.conversion_dict[self.FIELD_TYPE.FLOAT] = float 93 self.conversion_dict[self.FIELD_TYPE.NEWDECIMAL] = float
94
95 - def check_connection(self):
96 if self.dbconn == None: 97 raise ConnectionError('ConnectionError: %s' % (_("not connected to database"),)) 98 self._check_needed_reconnect()
99
100 - def _check_needed_reconnect(self):
101 if self.dbconn == None: 102 return 103 try: 104 self.dbconn.ping() 105 except self.mysql_exceptions.OperationalError, e: 106 if e[0] != 2006: 107 raise 108 else: 109 self.connect() 110 return True 111 return False
112
114 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
115
116 - def set_connection_data(self, data):
117 self.connection_data = data.copy() 118 if not self.connection_data.has_key('converters') and self.conversion_dict: 119 self.connection_data['converters'] = self.conversion_dict.copy()
120
121 - def check_connection_data(self):
122 if not self.connection_data: 123 raise PermissionDenied('ConnectionError: %s' % (_("no connection data"),))
124
125 - def connect(self):
126 kwargs = {} 127 keys = [ 128 ('host',"hostname"), 129 ('user',"username"), 130 ('passwd',"password"), 131 ('db',"dbname"), 132 ('port',"port"), 133 ('conv',"converters"), # mysql type converter dict 134 ] 135 for ckey, dkey in keys: 136 if not self.connection_data.has_key(dkey): 137 continue 138 kwargs[ckey] = self.connection_data.get(dkey) 139 140 try: 141 self.dbconn = self.mysql.connect(**kwargs) 142 except self.mysql_exceptions.OperationalError, e: 143 raise ConnectionError('ConnectionError: %s' % (e,)) 144 self.plain_cursor = self.dbconn.cursor() 145 self.cursor = self.mysql.cursors.DictCursor(self.dbconn) 146 return True
147
148 - def disconnect(self):
149 self.check_connection() 150 if hasattr(self.cursor,'close'): 151 self.cursor.close() 152 if hasattr(self.dbconn,'close'): 153 self.dbconn.close() 154 self.dbconn = None 155 self.cursor = None 156 self.plain_cursor = None 157 self.connection_data.clear() 158 return True
159
160 - def commit(self):
161 self.check_connection() 162 return self.dbconn.commit()
163
164 - def execute_script(self, myscript):
165 pty = None 166 for line in myscript.split(";"): 167 line = line.strip() 168 if not line: 169 continue 170 pty = self.cursor.execute(line) 171 return pty
172
173 - def execute_query(self, *args):
174 return self.cursor.execute(*args)
175
176 - def execute_many(self, query, myiter):
177 return self.cursor.executemany(query, myiter)
178
179 - def fetchone(self):
180 return self.cursor.fetchone()
181
182 - def fetchall(self):
183 return self.cursor.fetchall()
184
185 - def fetchmany(self, *args, **kwargs):
186 return self.cursor.fetchmany(*args,**kwargs)
187
188 - def lastrowid(self):
189 return self.cursor.lastrowid
190
191 - def table_exists(self, table):
192 self.check_connection() 193 self.cursor.execute("show tables like %s", (table,)) 194 rslt = self.cursor.fetchone() 195 if rslt: 196 return True 197 return False
198
199 - def column_in_table_exists(self, table, column):
200 t_ex = self.table_exists(table) 201 if not t_ex: 202 return False 203 self.cursor.execute("show columns from "+table) 204 data = self.cursor.fetchall() 205 for row in data: 206 if row['Field'] == column: 207 return True 208 return False
209
210 - def fetchall2set(self, item):
211 mycontent = set() 212 for x in item: 213 mycontent |= set(x) 214 return mycontent
215
216 - def fetchall2list(self, item):
217 content = [] 218 for x in item: 219 content += list(x) 220 return content
221
222 - def fetchone2list(self, item):
223 return list(item)
224
225 - def fetchone2set(self, item):
226 return set(item)
227
228 - def _generate_sql(self, action, table, data, where = ''):
229 sql = u'' 230 keys = sorted(data.keys()) 231 if action == "update": 232 sql += 'UPDATE %s SET ' % (self.dbconn.escape_string(table),) 233 keys_data = [] 234 for key in keys: 235 keys_data.append("%s = '%s'" % ( 236 self.dbconn.escape_string(key), 237 self.dbconn.escape_string(unicode(data[key]).encode('utf-8')).decode('utf-8') 238 ) 239 ) 240 sql += ', '.join(keys_data) 241 sql += ' WHERE %s' % (where,) 242 elif action == "insert": 243 sql = u'INSERT INTO %s (%s) VALUES (%s)' % ( 244 self.dbconn.escape_string(table), 245 u', '.join([self.dbconn.escape_string(x) for x in keys]), 246 u', '.join([u"'"+self.dbconn.escape_string(unicode(data[x]).encode('utf-8')).decode('utf-8')+"'" for x in keys]) 247 ) 248 return sql
249
250 -class Authenticator:
251
252 - def __init__(self):
253 self.login_data = {} 254 self.logged_in = False
255
256 - def check_login(self):
257 if not self.logged_in: 258 raise PermissionDenied('PermissionDenied: %s' % (_("not logged in"),))
259
260 - def set_login_data(self, data):
261 self.login_data = data.copy()
262
263 - def check_login_data(self):
264 if not self.login_data: 265 raise PermissionDenied('PermissionDenied: %s' % (_("no login data"),))
266
267 - def check_logged_in(self):
268 if not self.is_logged_in(): 269 raise PermissionDenied('PermissionDenied: %s' % (_("not logged in"),))
270
272 raise NotImplementedError('NotImplementedError: %s' % (_('method not implemented'),))
273
274 - def check_connection(self):
275 pass
276
277 - def login(self):
278 self.check_connection() 279 self.check_login_data() 280 self._raise_not_implemented_error() 281 self.logged_in = True 282 return True
283
284 - def logout(self):
285 self.check_connection() 286 self.check_login_data() 287 self._raise_not_implemented_error() 288 return True
289
290 - def is_developer(self):
291 self.check_connection() 292 self.check_login_data() 293 self.check_logged_in() 294 self._raise_not_implemented_error() 295 return True
296
297 - def is_administrator(self):
298 self.check_connection() 299 self.check_login_data() 300 self.check_logged_in() 301 self._raise_not_implemented_error() 302 return True
303
304 - def is_moderator(self):
305 self.check_connection() 306 self.check_login_data() 307 self.check_logged_in() 308 self._raise_not_implemented_error() 309 return True
310
311 - def is_user(self):
312 self.check_connection() 313 self.check_login_data() 314 self.check_logged_in() 315 self._raise_not_implemented_error() 316 return True
317
318 - def is_user_banned(self, user):
319 self.check_connection() 320 self._raise_not_implemented_error() 321 return False
322
323 - def is_in_group(self, group):
324 self.check_connection() 325 self.check_login_data() 326 self.check_logged_in() 327 self._raise_not_implemented_error() 328 return True
329
330 - def get_user_groups(self):
331 self.check_connection() 332 self.check_login_data() 333 self.check_logged_in() 334 self._raise_not_implemented_error() 335 return {}
336
337 - def get_user_group(self):
338 self.check_connection() 339 self.check_login_data() 340 self.check_logged_in() 341 self._raise_not_implemented_error() 342 return -1
343
344 - def get_user_id(self):
345 self.check_connection() 346 self.check_login_data() 347 self.check_logged_in() 348 self._raise_not_implemented_error() 349 return -1
350
351 - def is_logged_in(self):
352 return self.logged_in
353
354 - def get_permission_data(self):
355 self.check_connection() 356 self.check_login_data() 357 self.check_logged_in() 358 self._raise_not_implemented_error() 359 return {}
360
361 - def get_user_data(self):
362 self.check_connection() 363 self.check_login_data() 364 self.check_logged_in() 365 self._raise_not_implemented_error() 366 return {}
367
368 - def get_username(self):
369 self.check_connection() 370 self.check_login_data() 371 self.check_logged_in() 372 self._raise_not_implemented_error() 373 return {}
374