4 from sfa.util.faults import InvalidRSpec, UnsupportedRSpecVersion
5 from sfa.rspecs.version import RSpecVersion
6 from sfa.util.sfalogging import logger
16 return "<VersionManager with {} flavours: [{}]>"\
17 .format(len(self.versions),
18 ", ".join([str(x) for x in self.versions]))
20 def load_versions(self):
21 path = os.path.dirname(os.path.abspath(__file__))
22 versions_path = path + os.sep + 'versions'
23 versions_module_path = 'sfa.rspecs.versions'
24 valid_module = lambda x: os.path.isfile(os.sep.join([versions_path, x])) \
25 and x.endswith('.py') and x != '__init__.py'
26 files = [f for f in os.listdir(versions_path) if valid_module(f)]
27 for filename in files:
28 basename = filename.split('.')[0]
29 module_path = versions_module_path + '.' + basename
30 module = __import__(module_path, fromlist=module_path)
31 for attr_name in dir(module):
32 attr = getattr(module, attr_name)
33 if hasattr(attr, 'version') and hasattr(attr, 'enabled') and attr.enabled == True:
34 self.versions.append(attr())
36 def _get_version(self, type, version_num=None, content_type=None):
38 for version in self.versions:
39 if type is None or type.lower() == version.type.lower():
40 if version_num is None or str(float(version_num)) == str(float(version.version)):
41 if content_type is None or content_type.lower() == version.content_type.lower() \
42 or version.content_type == '*':
44 # sounds like we should be glad with the first match,
48 raise UnsupportedRSpecVersion(
49 "[%s %s %s] is not suported here" % (type, version_num, content_type))
52 def get_version(self, version=None):
54 if isinstance(version, dict):
55 retval = self._get_version(version.get('type'), version.get(
56 'version'), version.get('content_type'))
57 elif isinstance(version, str):
58 version_parts = version.split(' ')
59 num_parts = len(version_parts)
60 type = version_parts[0]
64 version_num = version_parts[1]
66 content_type = version_parts[2]
67 retval = self._get_version(type, version_num, content_type)
68 elif isinstance(version, RSpecVersion):
71 retval = self.versions[0]
73 raise UnsupportedRSpecVersion(
74 "No such version: %s " % str(version))
78 def get_version_by_schema(self, schema):
80 for version in self.versions:
81 if schema == version.schema:
84 raise InvalidRSpec("Unkwnown RSpec schema: %s" % schema)
87 def show_by_string(self, string):
89 print(self.get_version(string))
90 except Exception as e:
93 def show_by_schema(self, string):
95 print(self.get_version_by_schema(string))
96 except Exception as e:
99 if __name__ == '__main__':
100 manager = VersionManager()
102 manager.show_by_string('sfa 1')
103 manager.show_by_string('protogeni 2')
104 manager.show_by_string('protogeni 2 advertisement')
105 manager.show_by_schema('http://www.protogeni.net/resources/rspec/2/ad.xsd')
106 manager.show_by_schema('http://sorch.netmode.ntua.gr/ws/RSpec/ad.xsd')