Package entropy :: Package services :: Module skel

Source Code for Module entropy.services.skel

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