-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.py
140 lines (104 loc) · 5.13 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
import logging
import os
import pathlib
import pprint
import typing
import click
import inquirer
import ipfshttpclient
import toml
from publish import ENV_NAME_CONFIG_PATH, exceptions, ENV_NAME_IPFS_HOST, ENV_NAME_IPFS_PORT, ENV_NAME_IPFS_MULTIADDR
logger = logging.getLogger('publish.config')
class Config:
DEFAULT_CONFIG_PATH = os.path.expanduser('~/.ipfs_publish.toml')
def __init__(self, path): # type: (pathlib.Path) -> None
if not path.exists():
raise exceptions.ConfigException('The config was not found on this path! {}'.format(path))
data = toml.load(path)
logger.debug(f'Loaded configuration:\n{pprint.pformat(data)}')
self.data, self.repos = self._load_data(data)
self.loaded_path = path
self._ipfs = None
def _load_data(self,
data): # type: (typing.Dict[str, typing.Any]) -> typing.Tuple[dict, typing.Dict[str, publishing.Repo]]
from publish import publishing
self._verify_data(data)
repos: typing.Dict[str, publishing.GenericRepo] = {}
for value in data.pop('repos', {}).values():
repo_class = publishing.get_repo_class(value['git_repo_url'])
repo = repo_class.from_toml_dict(value, self)
repos[repo.name] = repo
return data, repos
def _verify_data(self, data):
if not data.get('host') or not data.get('port'):
raise exceptions.ConfigException('\'host\' and \'port\' are required items in configuration file!')
def save(self):
data = json.loads(json.dumps(self.data))
data['repos'] = {}
for repo in self.repos.values():
data['repos'][repo.name] = repo.to_toml_dict()
with self.loaded_path.open('w') as f:
toml.dump(data, f)
def __getitem__(self, item):
return self.data.get(item) # TODO: [Q] Is this good idea? Return None instead of KeyError?
def __setitem__(self, key, value):
self.data[key] = value
@property
def webhook_base(self):
return 'http://{}{}'.format(self['host'], f':{self["port"]}' if self['port'] != 80 else '')
@property
def ipfs(self): # type: () -> ipfshttpclient.Client
if self._ipfs is None:
if self['ipfs'] is not None:
host = os.environ.get(ENV_NAME_IPFS_HOST) or self['ipfs'].get('host')
port = os.environ.get(ENV_NAME_IPFS_PORT) or self['ipfs'].get('port')
multiaddr = os.environ.get(ENV_NAME_IPFS_MULTIADDR) or self['ipfs'].get('multiaddr')
else:
multiaddr = os.environ.get(ENV_NAME_IPFS_MULTIADDR)
host = os.environ.get(ENV_NAME_IPFS_HOST)
port = os.environ.get(ENV_NAME_IPFS_PORT)
# Hack to allow cross-platform Docker to reference the Docker host's machine with $HOST_ADDR
if host and host.startswith('$'):
logger.info(f'Resolving host name from environment variable {host}')
host = os.environ[host[1:]]
if host == 'localhost':
host = '127.0.0.1'
if not multiaddr:
multiaddr = f'/ip4/{host}/tcp/{port}/http'
logger.info(f'Connecting and caching to IPFS host \'{multiaddr}\'')
self._ipfs = ipfshttpclient.connect(multiaddr)
return self._ipfs
@classmethod
def get_instance(cls, path=None): # type: (typing.Optional[pathlib.Path]) -> Config
"""
Method that resolves from where the config should be loaded.
:return:
"""
if hasattr(cls, '_instance'):
instance = cls._instance
if path is not None and instance.loaded_path != path:
logger.warning('Somebody is trying to load config with different path "{}", but we already have cached'
'instance with path "{}"'.format(path, instance.loaded_path))
return instance
if path is None:
if ENV_NAME_CONFIG_PATH in os.environ:
path = pathlib.Path(os.environ[ENV_NAME_CONFIG_PATH])
else:
path = pathlib.Path(cls.DEFAULT_CONFIG_PATH)
# Default config should exist, if not lets create it.
if not path.exists():
logger.info(f'Config on the path {path} was not found! Bootstrapping it there!')
cls.bootstrap(path)
logger.info('Loading and caching config from file: {}'.format(path))
cls._instance = cls(path)
return cls._instance
@classmethod
def bootstrap(cls, path):
click.echo('Welcome!\nLet\'s bootstrap some basic configuration:')
host = inquirer.shortcuts.text('Set web server\'s host', default='localhost')
port = int(inquirer.shortcuts.text('Set web server\'s port', default='8000', validate=lambda _, x: str(x).isdigit()))
ipfs_multiaddr = inquirer.shortcuts.text('Set IPFS\'s multiaddr', default='/ip4/127.0.0.1/tcp/5001/http')
with path.open('w') as f:
toml.dump({'host': host, 'port': port, 'ipfs': {'multiaddr': ipfs_multiaddr }}, f)
click.echo('Bootstrap successful! Let\'s continue with your original command.\n')