diff src/memory/memory.py @ 592:e5a875a3311b

Fix pep8 support in src/memory.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
line wrap: on
line diff
--- a/src/memory/memory.py	Fri Jan 18 17:55:35 2013 +0100
+++ b/src/memory/memory.py	Fri Jan 18 17:55:35 2013 +0100
@@ -33,8 +33,8 @@
 from sat.memory.persistent import PersistentDict
 from sat.core import exceptions
 
-SAVEFILE_PARAM_XML="/param" #xml parameters template
-SAVEFILE_DATABASE="/sat.db"
+SAVEFILE_PARAM_XML = "/param"  # xml parameters template
+SAVEFILE_DATABASE = "/sat.db"
 
 
 class Params(object):
@@ -61,18 +61,20 @@
         </category>
     </individual>
     </params>
-    """ % {'category_connection': _("Connection"),
-           'label_NewAccount': _("Register new account"),
-           'label_autoconnect': _('Connect on frontend startup'),
-           'label_autodisconnect': _('Disconnect on frontend closure'),
-           'category_misc': _("Misc")
-          }
+    """ % {
+        'category_connection': _("Connection"),
+        'label_NewAccount': _("Register new account"),
+        'label_autoconnect': _('Connect on frontend startup'),
+        'label_autodisconnect': _('Disconnect on frontend closure'),
+        'category_misc': _("Misc")
+    }
 
     def load_default_params(self):
         self.dom = minidom.parseString(Params.default_xml.encode('utf-8'))
 
     def _mergeParams(self, source_node, dest_node):
         """Look for every node in source_node and recursively copy them to dest if they don't exists"""
+
         def getNodesMap(children):
             ret = {}
             for child in children:
@@ -109,9 +111,9 @@
         @param profile: profile to load (*must exist*)
         @param cache: if not None, will be used to store the value, as a short time cache
         @return: deferred triggered once params are loaded"""
-        if cache == None:
+        if cache is None:
             self.params[profile] = {}
-        return self.storage.loadIndParams(self.params[profile] if cache==None else cache, profile)
+        return self.storage.loadIndParams(self.params[profile] if cache is None else cache, profile)
 
     def purgeProfile(self, profile):
         """Remove cache data of a profile
@@ -121,9 +123,9 @@
         except KeyError:
             error(_("Trying to purge cache of a profile not in memory: [%s]") % profile)
 
-    def save_xml(self, file):
+    def save_xml(self, filename):
         """Save parameters template to xml file"""
-        with open(file, 'wb') as xml_file:
+        with open(filename, 'wb') as xml_file:
             xml_file.write(self.dom.toxml('utf-8'))
 
     def __init__(self, host, storage):
@@ -141,7 +143,7 @@
         @param profile: profile of the profile"""
         #FIXME: must be asynchronous and call the callback once the profile actually exists
         if self.storage.hasProfile(profile):
-            info (_('The profile [%s] already exists') % (profile,))
+            info(_('The profile [%s] already exists') % (profile, ))
             return True
         if not self.host.trigger.point("ProfileCreation", profile):
             return False
@@ -157,13 +159,12 @@
                         - CANCELED: profile creation canceled
         """
         if self.storage.hasProfile(profile):
-            info (_('The profile name already exists'))
+            info(_('The profile name already exists'))
             return defer.fail("CONFLICT")
         if not self.host.trigger.point("ProfileCreation", profile):
             return defer.fail("CANCEL")
         return self.storage.createProfile(profile)
 
-
     def deleteProfile(self, profile):
         """Delete an existing profile
         @param profile: name of the profile"""
@@ -183,14 +184,14 @@
                             @ALL@ for all profiles
                             @DEFAULT@ for default profile
         @return: requested profile name or None if it doesn't exist"""
-        if profile_key=='@DEFAULT@':
+        if profile_key == '@DEFAULT@':
             default = self.host.memory.memory_data.get('Profile_default')
             if not default:
-                info(_('No default profile, returning first one')) #TODO: manage real default profile
+                info(_('No default profile, returning first one'))  # TODO: manage real default profile
                 default = self.host.memory.memory_data['Profile_default'] = self.storage.getProfilesList()[0]
-            return default #FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
+            return default  # FIXME: temporary, must use real default value, and fallback to first one if it doesn't exists
         if not self.storage.hasProfile(profile_key):
-            info (_('Trying to access an unknown profile'))
+            info(_('Trying to access an unknown profile'))
             return ""
         return profile_key
 
@@ -218,7 +219,7 @@
                 if child.nodeName == '#text':
                     continue
                 node = self.__get_unique_node(tgt_parent, child.nodeName, child.getAttribute("name"))
-                if not node: #The node is new
+                if not node:  # The node is new
                     tgt_parent.appendChild(child)
                 else:
                     import_node(node, child)
@@ -227,10 +228,10 @@
 
     def __default_ok(self, value, name, category):
         #FIXME: gof: will not work with individual parameters
-        self.setParam(name, value, category) #FIXME: better to set param xml value ???
+        self.setParam(name, value, category)  # FIXME: better to set param xml value ???
 
     def __default_ko(self, failure, name, category):
-        error (_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category':category, 'name':name, 'reason':str(failure.value)})
+        error(_("Can't determine default value for [%(category)s/%(name)s]: %(reason)s") % {'category': category, 'name': name, 'reason': str(failure.value)})
 
     def setDefault(self, name, category, callback, errback=None):
         """Set default value of parameter
@@ -241,9 +242,9 @@
         @param errback: must manage the error with args failure, name, category
         """
         #TODO: send signal param update if value changed
-        node =  self.__getParamNode(name, category, '@ALL@')
+        node = self.__getParamNode(name, category, '@ALL@')
         if not node:
-            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
+            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
             return
         if node[1].getAttribute('default_cb') == 'yes':
             del node[1].attributes['default_cb']
@@ -257,15 +258,15 @@
         @param attr: name of the attribute to get (e.g.: 'value' or 'type')
         @param value: user defined value"""
         if attr == 'value':
-            value_to_use = value if value!=None else node.getAttribute(attr) #we use value (user defined) if it exist, else we use node's default value
+            value_to_use = value if value is not None else node.getAttribute(attr)  # we use value (user defined) if it exist, else we use node's default value
             if node.getAttribute('type') == 'bool':
-                return value_to_use.lower() not in ('false','0')
+                return value_to_use.lower() not in ('false', '0')
             return value_to_use
         return node.getAttribute(attr)
 
     def __type_to_string(self, result):
         """ convert result to string, according to its type """
-        if isinstance(result,bool):
+        if isinstance(result, bool):
             return "true" if result else "false"
         return result
 
@@ -284,14 +285,14 @@
         #FIXME: looks really dirty and buggy, need to be reviewed/refactored
         node = self.__getParamNode(name, category)
         if not node:
-            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
+            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
             raise exceptions.NotFound
 
         if node[0] == 'general':
             value = self.__getParam(None, category, name, 'general')
             return self.__getAttr(node[1], attr, value)
 
-        assert(node[0] == 'individual')
+        assert node[0] == 'individual'
 
         profile = self.getProfileName(profile_key)
         if not profile:
@@ -319,14 +320,14 @@
            @param profile: owner of the param (@ALL@ for everyone)"""
         node = self.__getParamNode(name, category)
         if not node:
-            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name':name, 'category':category})
+            error(_("Requested param [%(name)s] in category [%(category)s] doesn't exist !") % {'name': name, 'category': category})
             return None
 
         if node[0] == 'general':
             value = self.__getParam(None, category, name, 'general')
             return defer.succeed(self.__getAttr(node[1], attr, value))
 
-        assert(node[0] == 'individual')
+        assert node[0] == 'individual'
 
         profile = self.getProfileName(profile_key)
         if not profile:
@@ -353,16 +354,16 @@
         @return: param value or None if it doesn't exist
         """
         if _type == 'general':
-            if self.params_gen.has_key((category, name)):
+            if (category, name) in self.params_gen:
                 return self.params_gen[(category, name)]
-            return None  #This general param has the default value
+            return None  # This general param has the default value
         assert (_type == 'individual')
-        if self.params.has_key(profile):
-            cache = self.params[profile] # if profile is in main cache, we use it,
-                                         # ignoring the temporary cache
-        elif cache == None: #else we use the temporary cache if it exists, or raise an exception
+        if profile in self.params:
+            cache = self.params[profile]  # if profile is in main cache, we use it,
+                                          # ignoring the temporary cache
+        elif cache is None:  # else we use the temporary cache if it exists, or raise an exception
             raise exceptions.ProfileNotInCacheError
-        if not cache.has_key((category, name)):
+        if (category, name) not in cache:
             return None
         return cache[(category, name)]
 
@@ -372,21 +373,22 @@
         @param profile: profile name (not key !)
         @return: a deferred that fire a minidom.Document of the profile xml (cf warning above)
         """
-        def constructProfile(ignore,profile_cache):
+
+        def constructProfile(ignore, profile_cache):
             prof_xml = minidom.parseString('<params/>')
             cache = {}
 
             for type_node in self.dom.documentElement.childNodes:
-                if type_node.nodeName == 'general' or type_node.nodeName == 'individual':  #we use all params, general and individual
+                if type_node.nodeName == 'general' or type_node.nodeName == 'individual':  # we use all params, general and individual
                     for cat_node in type_node.childNodes:
                         if cat_node.nodeName == 'category':
                             category = cat_node.getAttribute('name')
-                            if not cache.has_key(category):
-                                cache[category] = dest_cat = cat_node.cloneNode(True) #we make a copy for the new xml
+                            if category not in cache:
+                                cache[category] = dest_cat = cat_node.cloneNode(True)  # we make a copy for the new xml
                                 new_node = True
                             else:
                                 dest_cat = cache[category]
-                                new_node = False #It's not a new node, we will merge information
+                                new_node = False  # It's not a new node, we will merge information
                             params = cat_node.getElementsByTagName("param")
                             dest_params = {}
                             for node in dest_cat.childNodes:
@@ -402,14 +404,13 @@
                                     dest_cat.appendChild(dest_params[name])
 
                                 profile_value = self.__getParam(profile, category, name, type_node.nodeName, cache=profile_cache)
-                                if profile_value!=None:  #there is a value for this profile, we must change the default
+                                if profile_value is not None:  # there is a value for this profile, we must change the default
                                     dest_params[name].setAttribute('value', profile_value)
                             if new_node:
                                 prof_xml.documentElement.appendChild(dest_cat)
             return prof_xml
 
-
-        if self.params.has_key(profile):
+        if profile in self.params:
             d = defer.succeed(None)
             profile_cache = self.params[profile]
         else:
@@ -426,7 +427,7 @@
             error(_("Asking params for inexistant profile"))
             return ""
         d = self.getParams(profile)
-        return d.addCallback(lambda param_xml:paramsXml2xmlUI(param_xml))
+        return d.addCallback(lambda param_xml: paramsXml2xmlUI(param_xml))
 
     def getParams(self, profile_key):
         """Construct xml for asked profile
@@ -464,7 +465,7 @@
         d = self.__constructProfileXml(profile)
         return d.addCallback(returnCategoryXml)
 
-    def __getParamNode(self, name, category, _type="@ALL@"): #FIXME: is _type useful ?
+    def __getParamNode(self, name, category, _type="@ALL@"):  # FIXME: is _type useful ?
         """Return a node from the param_xml
         @param name: name of the node
         @param category: category of the node
@@ -475,8 +476,8 @@
         @return: a tuple with the node type and the the node, or None if not found"""
 
         for type_node in self.dom.documentElement.childNodes:
-            if ( ((_type == "@ALL@" or _type == "@GENERAL@") and type_node.nodeName == 'general')
-            or ( (_type == "@ALL@" or _type == "@INDIVIDUAL@") and type_node.nodeName == 'individual') ):
+            if (((_type == "@ALL@" or _type == "@GENERAL@") and type_node.nodeName == 'general')
+                    or ((_type == "@ALL@" or _type == "@INDIVIDUAL@") and type_node.nodeName == 'individual')):
                 for node in type_node.getElementsByTagName('category'):
                     if node.getAttribute("name") == category:
                         params = node.getElementsByTagName("param")
@@ -487,7 +488,7 @@
 
     def getParamsCategories(self):
         """return the categories availables"""
-        categories=[]
+        categories = []
         for cat in self.dom.getElementsByTagName("category"):
             name = cat.getAttribute("name")
             if name not in categories:
@@ -497,15 +498,15 @@
     def setParam(self, name, value, category, profile_key='@NONE@'):
         """Set a parameter, return None if the parameter is not in param xml"""
         #TODO: use different behaviour depending of the data type (e.g. password encrypted)
-        if profile_key!="@NONE@":
+        if profile_key != "@NONE@":
             profile = self.getProfileName(profile_key)
             if not profile:
                 error(_('Trying to set parameter for an unknown profile'))
-                return #TODO: throw an error
+                return  # TODO: throw an error
 
         node = self.__getParamNode(name, category, '@ALL@')
         if not node:
-            error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category':category, 'name':name})
+            error(_('Requesting an unknown parameter (%(category)s/%(name)s)') % {'category': category, 'name': name})
             return
 
         if node[0] == 'general':
@@ -520,35 +521,36 @@
         assert (profile_key != "@NONE@")
 
         _type = node[1].getAttribute("type")
-        if _type=="button":
-            print "clique",node.toxml()
+        if _type == "button":
+            print "clique", node.toxml()
         else:
-            if self.host.isConnected(profile): #key can not exists if profile is not connected
+            if self.host.isConnected(profile):  # key can not exists if profile is not connected
                 self.params[profile][(category, name)] = value
             self.host.bridge.paramUpdate(name, value, category, profile)
             self.storage.setIndParam(category, name, value, profile)
 
+
 class Memory(object):
     """This class manage all persistent informations"""
 
     def __init__(self, host):
-        info (_("Memory manager init"))
+        info(_("Memory manager init"))
         self.initialized = defer.Deferred()
         self.host = host
-        self.entitiesCache={} #XXX: keep presence/last resource/other data in cache
+        self.entitiesCache = {}  # XXX: keep presence/last resource/other data in cache
                               #     /!\ an entity is not necessarily in roster
-        self.subscriptions={}
-        self.server_features={} #used to store discovery's informations
-        self.server_identities={}
+        self.subscriptions = {}
+        self.server_features = {}  # used to store discovery's informations
+        self.server_identities = {}
         self.config = self.parseMainConf()
         host.set_const('savefile_database', SAVEFILE_DATABASE)
-        database_file = os.path.expanduser(self.getConfig('','local_dir')+
-                                        self.host.get_const('savefile_database'))
+        database_file = os.path.expanduser(self.getConfig('', 'local_dir') +
+                                           self.host.get_const('savefile_database'))
         self.storage = SqliteStorage(database_file)
         PersistentDict.storage = self.storage
-        self.params=Params(host, self.storage)
+        self.params = Params(host, self.storage)
         self.loadFiles()
-        d = self.storage.initialized.addCallback(lambda ignore:self.load())
+        d = self.storage.initialized.addCallback(lambda ignore: self.load())
         self.memory_data = PersistentDict("memory")
         d.addCallback(lambda ignore: self.memory_data.load())
         d.chainDeferred(self.initialized)
@@ -559,7 +561,7 @@
         try:
             _config.read(map(os.path.expanduser, ['/etc/sat.conf', '~/sat.conf', '~/.sat.conf', 'sat.conf', '.sat.conf']))
         except:
-            error (_("Can't read main config !"))
+            error(_("Can't read main config !"))
 
         return _config
 
@@ -569,7 +571,7 @@
         @param name: name of the option
         """
         if not section:
-            section='DEFAULT'
+            section = 'DEFAULT'
         try:
             _value = self.config.get(section, name)
         except (NoOptionError, NoSectionError):
@@ -577,11 +579,10 @@
 
         return os.path.expanduser(_value) if name.endswith('_path') or name.endswith('_dir') else _value
 
-
     def loadFiles(self):
         """Load parameters and all memory things from file/db"""
-        param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
-                                        self.host.get_const('savefile_param_xml'))
+        param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') +
+                                            self.host.get_const('savefile_param_xml'))
 
         #parameters template
         if os.path.exists(param_file_xml):
@@ -589,13 +590,12 @@
                 self.params.load_xml(param_file_xml)
                 debug(_("params template loaded"))
             except Exception as e:
-                error (_("Can't load params template: %s") % (e,))
+                error(_("Can't load params template: %s") % (e, ))
                 self.params.load_default_params()
         else:
-            info (_("No params template, using default template"))
+            info(_("No params template, using default template"))
             self.params.load_default_params()
 
-
     def load(self):
         """Load parameters and all memory things from db"""
         #parameters data
@@ -622,12 +622,11 @@
         except KeyError:
             error(_("Trying to purge roster status cache for a profile not in memory: [%s]") % profile)
 
-
     def save(self):
         """Save parameters and all memory things to file/db"""
         #TODO: need to encrypt files (at least passwords !) and set permissions
-        param_file_xml = os.path.expanduser(self.getConfig('','local_dir')+
-                                        self.host.get_const('savefile_param_xml'))
+        param_file_xml = os.path.expanduser(self.getConfig('', 'local_dir') +
+                                            self.host.get_const('savefile_param_xml'))
 
         self.params.save_xml(param_file_xml)
         debug(_("params saved"))
@@ -635,7 +634,6 @@
     def getProfilesList(self):
         return self.storage.getProfilesList()
 
-
     def getProfileName(self, profile_key):
         """Return name of profile from keyword
         @param profile_key: can be the profile name or a keywork (like @DEFAULT@)
@@ -660,18 +658,18 @@
         return self.params.deleteProfile(name)
 
     def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile="@NONE@"):
-        assert(profile!="@NONE@")
+        assert profile != "@NONE@"
         return self.storage.addToHistory(from_jid, to_jid, message, _type, timestamp, profile)
 
     def getHistory(self, from_jid, to_jid, limit=0, between=True, profile="@NONE@"):
-        assert(profile != "@NONE@")
+        assert profile != "@NONE@"
         return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between, profile)
 
     def addServerFeature(self, feature, profile):
         """Add a feature discovered from server
         @param feature: string of the feature
         @param profile: which profile is using this server ?"""
-        if not self.server_features.has_key(profile):
+        if profile not in self.server_features:
             self.server_features[profile] = []
         self.server_features[profile].append(feature)
 
@@ -681,8 +679,8 @@
         @param profile: which profile is using this server ?"""
         if not profile in self.server_identities:
             self.server_identities[profile] = {}
-        if not self.server_identities[profile].has_key((category, _type)):
-            self.server_identities[profile][(category, _type)]=set()
+        if (category, _type) not in self.server_identities[profile]:
+            self.server_identities[profile][(category, _type)] = set()
         self.server_identities[profile][(category, _type)].add(entity)
 
     def getServerServiceEntities(self, category, _type, profile):
@@ -695,9 +693,9 @@
     def getServerServiceEntity(self, category, _type, profile):
         """Helper method to get first available entity for a service"""
         entities = self.getServerServiceEntities(category, _type, profile)
-        if entities == None:
-            warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category":category,
-                                                                                                                          "type":_type})
+        if entities is None:
+            warning(_("Entities (%(category)s/%(type)s) not available, maybe they haven't been asked to server yet ?") % {"category": category,
+                                                                                                                          "type": _type})
             return None
         else:
             return list(entities)[0] if entities else None
@@ -706,9 +704,9 @@
         """Tell if the server of the profile has the required feature"""
         profile = self.getProfileName(profile_key)
         if not profile:
-            error (_('Trying find server feature for a non-existant profile'))
+            error(_('Trying find server feature for a non-existant profile'))
             return
-        assert(self.server_features.has_key(profile))
+        assert profile in self.server_features
         return feature in self.server_features[profile]
 
     def getLastResource(self, contact, profile_key):
@@ -738,7 +736,7 @@
             if "presence" in self.entitiesCache[profile][entity]:
                 entities_presence[entity] = self.entitiesCache[profile][entity]["presence"]
 
-        debug ("Memory getPresenceStatus (%s)", entities_presence)
+        debug("Memory getPresenceStatus (%s)", entities_presence)
         return entities_presence
 
     def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key):
@@ -747,14 +745,14 @@
         if not profile:
             error(_('Trying to add presence status to a non-existant profile'))
             return
-        entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{})
+        entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(), {})
         resource = jid.parse(entity_jid.full())[2] or ''
         if resource:
             entity_data["last_resource"] = resource
         if not "last_resource" in entity_data:
             entity_data["last_resource"] = ''
 
-        entity_data.setdefault("presence",{})[resource] = (show, priority, statuses)
+        entity_data.setdefault("presence", {})[resource] = (show, priority, statuses)
 
     def updateEntityData(self, entity_jid, key, value, profile_key):
         """Set a misc data for an entity
@@ -768,9 +766,9 @@
             raise exceptions.UnknownProfileError(_('Trying to get entity data for a non-existant profile'))
         if not profile in self.entitiesCache:
             raise exceptions.ProfileNotInCacheError
-        entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(),{})
+        entity_data = self.entitiesCache[profile].setdefault(entity_jid.userhost(), {})
         entity_data[key] = value
-        if isinstance(value,basestring):
+        if isinstance(value, basestring):
             self.host.bridge.entityDataUpdated(entity_jid.userhost(), key, value, profile)
 
     def getEntityData(self, entity_jid, keys_list, profile_key):
@@ -810,21 +808,19 @@
         except KeyError:
             pass
 
-
-
     def addWaitingSub(self, _type, entity_jid, profile_key):
         """Called when a subcription request is received"""
         profile = self.getProfileName(profile_key)
-        assert(profile)
-        if not self.subscriptions.has_key(profile):
+        assert profile
+        if profile not in self.subscriptions:
             self.subscriptions[profile] = {}
         self.subscriptions[profile][entity_jid] = _type
 
     def delWaitingSub(self, entity_jid, profile_key):
         """Called when a subcription request is finished"""
         profile = self.getProfileName(profile_key)
-        assert(profile)
-        if self.subscriptions.has_key(profile) and self.subscriptions[profile].has_key(entity_jid):
+        assert profile
+        if profile in self.subscriptions and entity_jid in self.subscriptions[profile]:
             del self.subscriptions[profile][entity_jid]
 
     def getWaitingSub(self, profile_key):
@@ -833,7 +829,7 @@
         if not profile:
             error(_('Asking waiting subscriptions for a non-existant profile'))
             return {}
-        if not self.subscriptions.has_key(profile):
+        if profile not in self.subscriptions:
             return {}
 
         return self.subscriptions[profile]