From: Thierry Parmentelat Date: Tue, 1 Nov 2011 13:50:50 +0000 (+0100) Subject: defaultdict in sfa.util + minor/cosmetic X-Git-Tag: sfa-1.1-2~13 X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=commitdiff_plain;h=07fcf3cbd55e243bed442271a7557178a92559bf defaultdict in sfa.util + minor/cosmetic --- diff --git a/sfa/managers/aggregate_manager.py b/sfa/managers/aggregate_manager.py index 827b2d9e..702fe7d8 100644 --- a/sfa/managers/aggregate_manager.py +++ b/sfa/managers/aggregate_manager.py @@ -192,7 +192,7 @@ def CreateSliver(api, slice_xrn, creds, rspec_string, users, call_id): aggregate.prepare_interfaces({'node_id': aggregate.nodes.keys()}) slices.verify_slice_links(slice, rspec.version.get_link_requests(), aggregate) - # hanlde MyPLC peer association. + # handle MyPLC peer association. # only used by plc and ple. slices.handle_peer(site, slice, persons, peer) diff --git a/sfa/plc/aggregate.py b/sfa/plc/aggregate.py index f210705b..4cbeff70 100644 --- a/sfa/plc/aggregate.py +++ b/sfa/plc/aggregate.py @@ -1,9 +1,11 @@ #!/usr/bin/python from sfa.util.xrn import hrn_to_urn, urn_to_hrn from sfa.util.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename + from sfa.rspecs.rspec import RSpec from sfa.rspecs.elements.link import Link from sfa.rspecs.elements.interface import Interface + from sfa.managers.vini.topology import PhysicalLinks from sfa.rspecs.version_manager import VersionManager from sfa.plc.vlink import get_tc_rate diff --git a/sfa/plc/plccomponentapi.py b/sfa/plc/plccomponentapi.py index e09dbee5..d3264827 100644 --- a/sfa/plc/plccomponentapi.py +++ b/sfa/plc/plccomponentapi.py @@ -12,6 +12,13 @@ from sfa.server.sfaapi import SfaApi #################### class PlcComponentApi(SfaApi): + """ + This class is the type for the toplevel 'api' object + when running the component manager inside a planetlab node. + As such it runs an SFA-compliant interface and thus inherits SfaApi + However the fact that we run inside a planetlab nodes requires + some tweaks as compared with a service running in the infrastructure. + """ def __init__ (self, encoding="utf-8", methods='sfa.methods', config = "/etc/sfa/sfa_config.py", diff --git a/sfa/plc/plcsfaapi.py b/sfa/plc/plcsfaapi.py index 357a210a..842df318 100644 --- a/sfa/plc/plcsfaapi.py +++ b/sfa/plc/plcsfaapi.py @@ -3,52 +3,13 @@ import xmlrpclib from sfa.util.faults import MissingSfaInfo from sfa.util.sfalogging import logger from sfa.util.table import SfaTable +from sfa.util.defaultdict import defaultdict from sfa.util.xrn import hrn_to_urn from sfa.util.plxrn import slicename_to_hrn, hostname_to_hrn, hrn_to_pl_slicename, hrn_to_pl_login_base from sfa.server.sfaapi import SfaApi -#################### xxx should move into util/defaultdict -try: - from collections import defaultdict -except: - class defaultdict(dict): - def __init__(self, default_factory=None, *a, **kw): - if (default_factory is not None and - not hasattr(default_factory, '__call__')): - raise TypeError('first argument must be callable') - dict.__init__(self, *a, **kw) - self.default_factory = default_factory - def __getitem__(self, key): - try: - return dict.__getitem__(self, key) - except KeyError: - return self.__missing__(key) - def __missing__(self, key): - if self.default_factory is None: - raise KeyError(key) - self[key] = value = self.default_factory() - return value - def __reduce__(self): - if self.default_factory is None: - args = tuple() - else: - args = self.default_factory, - return type(self), args, None, None, self.items() - def copy(self): - return self.__copy__() - def __copy__(self): - return type(self)(self.default_factory, self) - def __deepcopy__(self, memo): - import copy - return type(self)(self.default_factory, - copy.deepcopy(self.items())) - def __repr__(self): - return 'defaultdict(%s, %s)' % (self.default_factory, - dict.__repr__(self)) -## end of http://code.activestate.com/recipes/523034/ }}} - def list_to_dict(recs, key): """ convert a list of dictionaries into a dictionary keyed on the diff --git a/sfa/server/sfaapi.py b/sfa/server/sfaapi.py index 242979cb..9d22e7f2 100644 --- a/sfa/server/sfaapi.py +++ b/sfa/server/sfaapi.py @@ -20,12 +20,18 @@ class SfaApi (XmlrpcApi): """ An SfaApi instance is a basic xmlrcp service augmented with the local cryptographic material and hrn - It also has the notion of neighbour sfa services - as defined in /etc/sfa/{aggregates,registries}.xml + + It also has the notion of its own interface (a string describing + whether we run a registry, aggregate or slicemgr) and has + the notion of neighbour sfa services as defined + in /etc/sfa/{aggregates,registries}.xml + Finally it contains a cache instance + It gets augmented by the generic layer with (*) an instance of manager (actually a manager module for now) (*) which in turn holds an instance of a testbed driver + For convenience api.manager.driver == api.driver """ def __init__ (self, encoding="utf-8", methods='sfa.methods', diff --git a/sfa/util/config.py b/sfa/util/config.py index cf2cca9b..14669160 100644 --- a/sfa/util/config.py +++ b/sfa/util/config.py @@ -76,7 +76,7 @@ class Config: except: pass except IOError, e: - raise IOError, "Could not find the configuration file: %s" % config_file + raise IOError, "Could not find or load the configuration file: %s" % config_file def get_trustedroots_dir(self): return self.config_path + os.sep + 'trusted_roots' diff --git a/sfa/util/defaultdict.py b/sfa/util/defaultdict.py new file mode 100644 index 00000000..e0dd1450 --- /dev/null +++ b/sfa/util/defaultdict.py @@ -0,0 +1,38 @@ +# from http://code.activestate.com/recipes/523034/ +try: + from collections import defaultdict +except: + class defaultdict(dict): + def __init__(self, default_factory=None, *a, **kw): + if (default_factory is not None and + not hasattr(default_factory, '__call__')): + raise TypeError('first argument must be callable') + dict.__init__(self, *a, **kw) + self.default_factory = default_factory + def __getitem__(self, key): + try: + return dict.__getitem__(self, key) + except KeyError: + return self.__missing__(key) + def __missing__(self, key): + if self.default_factory is None: + raise KeyError(key) + self[key] = value = self.default_factory() + return value + def __reduce__(self): + if self.default_factory is None: + args = tuple() + else: + args = self.default_factory, + return type(self), args, None, None, self.items() + def copy(self): + return self.__copy__() + def __copy__(self): + return type(self)(self.default_factory, self) + def __deepcopy__(self, memo): + import copy + return type(self)(self.default_factory, + copy.deepcopy(self.items())) + def __repr__(self): + return 'defaultdict(%s, %s)' % (self.default_factory, + dict.__repr__(self))