comparison planemo/lib/python3.7/site-packages/galaxy/util/facts.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """Return various facts for string formatting.
2 """
3 import socket
4 from collections import MutableMapping
5
6 from six import string_types
7
8
9 class Facts(MutableMapping):
10 """A dict-like object that evaluates values at access time."""
11
12 def __init__(self, config=None, **kwargs):
13 config = config or {}
14 self.__dict__ = {}
15 self.__set_defaults(config)
16 self.__set_config(config)
17 self.__dict__.update(dict(**kwargs))
18
19 def __set_defaults(self, config):
20 # config here may be a Galaxy config object, or it may just be a dict
21 defaults = {
22 'server_name': lambda: config.get('base_server_name', 'main'),
23 'server_id': None,
24 'instance_id': None,
25 'pool_name': None,
26 'fqdn': lambda: socket.getfqdn(),
27 'hostname': lambda: socket.gethostname().split('.', 1)[0],
28 }
29 self.__dict__.update(defaults)
30
31 def __set_config(self, config):
32 if config is not None:
33 for name in dir(config):
34 if not name.startswith('_') and isinstance(getattr(config, name), string_types):
35 self.__dict__['config_' + name] = lambda name=name: getattr(config, name)
36
37 def __getitem__(self, key):
38 item = self.__dict__.__getitem__(key)
39 if callable(item):
40 return item()
41 else:
42 return item
43
44 # Other methods pass through to the corresponding dict methods
45
46 def __setitem__(self, key, value):
47 return self.__dict__.__setitem__(key, value)
48
49 def __delitem__(self, key):
50 return self.__dict__.__delitem__(key)
51
52 def __iter__(self):
53 return self.__dict__.__iter__()
54
55 def __len__(self):
56 return self.__dict__.__len__()
57
58 def __str__(self):
59 return self.__dict__.__str__()
60
61 def __repr__(self):
62 return self.__dict__.__repr__()
63
64
65 def get_facts(config=None, **kwargs):
66 return Facts(config=config, **kwargs)