1import nomad.api.exceptions
2
3from nomad.api.base import Requester
4
5
6class Regions(Requester):
7
8    """
9    https://www.nomadproject.io/docs/http/regions.html
10    """
11    ENDPOINT = "regions"
12
13    def __init__(self, **kwargs):
14        super(Regions, self).__init__(**kwargs)
15
16    def __str__(self):
17        return "{0}".format(self.__dict__)
18
19    def __repr__(self):
20        return "{0}".format(self.__dict__)
21
22    def __getattr__(self, item):
23        raise AttributeError
24
25    def __contains__(self, item):
26        try:
27            regions = self.get_regions()
28
29            for r in regions:
30                if r == item:
31                    return True
32            else:
33                return False
34        except nomad.api.exceptions.URLNotFoundNomadException:
35            return False
36
37    def __len__(self):
38        regions = self.get_regions()
39        return len(regions)
40
41    def __getitem__(self, item):
42        try:
43            regions = self.get_regions()
44
45            for r in regions:
46                if r == item:
47                    return r
48            else:
49                raise KeyError
50        except nomad.api.exceptions.URLNotFoundNomadException:
51            raise KeyError
52
53    def __iter__(self):
54        regions = self.get_regions()
55        return iter(regions)
56
57    def get_regions(self):
58        """ Returns the known region names.
59
60            https://www.nomadproject.io/docs/http/regions.html
61
62            returns: list
63            raises:
64              - nomad.api.exceptions.BaseNomadException
65              - nomad.api.exceptions.URLNotFoundNomadException
66        """
67        return self.request(method="get").json()
68