python3 - 2to3 + miscell obvious tweaks
[sfa.git] / sfa / server / api_versions.py
1 import os
2 from sfa.util.xml import XML
3 from sfa.util.config import Config
4
5
6 class ApiVersions:
7
8     required_fields = ['version', 'url']
9
10     template = """<api_versions>
11 <api_version name="" version="" url="" />
12 </api_versions>"""
13
14     def __init__(self, string=None, filename=None, create=False):
15         self.xml = None
16
17         if create:
18             self.create()
19         elif string:
20             self.load(string)
21         elif filename:
22             self.load(filename)
23         else:
24             # load the default file
25             c = Config()
26             api_versions_file = os.path.sep.join(
27                 [c.config_path, 'api_versions.xml'])
28             self.load(api_versions_file)
29
30     def create(self):
31         self.xml = XML(string=ApiVersions.template)
32
33     def load(self, source):
34         self.xml = XML(source)
35
36     def get_versions(self):
37         versions = {}
38         for value in list(self.xml.todict().values()):
39             if not value:
40                 continue
41             if isinstance(value, list):
42                 for item in value:
43                     if isinstance(item, dict) and \
44                        set(ApiVersions.required_fields).issubset(list(item.keys())) and \
45                        item['version'] != '' and item['url'] != '':
46                         versions[str(item['version'])] = item['url']
47         return versions