This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
config.py
147 lines (118 loc) · 5.16 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
141
142
143
144
145
146
147
# --------------------------------------------------------------------------
# Copyright (c) 2015-2017 Digital Sapphire
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the
# following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
# ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
# --------------------------------------------------------------------------
from __future__ import unicode_literals
import logging
import os
from pyupdater import settings
from pyupdater.utils.storage import Storage
log = logging.getLogger(__name__)
class Config(dict):
def __init__(self, *args, **kwargs):
super(Config, self).__init__(*args, **kwargs)
self.__dict__ = self
self.__postinit__()
def __postinit__(self):
config_template = {
# If left None "PyUpdater App" will be used
'APP_NAME': settings.GENERIC_APP_NAME,
# path to place client config
'CLIENT_CONFIG_PATH': settings.DEFAULT_CLIENT_CONFIG,
# Company/Your name
'COMPANY_NAME': settings.GENERIC_APP_NAME,
'PLUGIN_CONFIGS': {},
# Support for patch updates
'UPDATE_PATCHES': True,
# Max retries for downloads
'MAX_DOWNLOAD_RETRIES': 3,
}
self.update(config_template)
def from_object(self, obj):
# Updates the values from the given object
# Args:
# obj (instance): Object with config attributes
# Objects are classes.
# Just the uppercase variables in that object are stored in the config.
# Example usage::
# from yourapplication import default_config
# app.config.from_object(default_config())
for key in dir(obj):
if key.isupper():
self[key] = getattr(obj, key)
# Loads & saves config file
class ConfigManager(object):
def __init__(self):
self.cwd = os.getcwd()
self.db = Storage()
self.config_key = settings.CONFIG_DB_KEY_APP_CONFIG
# Loads config from database (json file)
def load_config(self):
config_data = self.db.load(self.config_key)
if config_data is None:
config_data = {}
config = Config()
for k, v in config_data.items():
config[k] = v
config.DATA_DIR = os.getcwd()
return config
def get_app_name(self):
config = self.load_config()
return config.APP_NAME
# Saves config to database (json file)
def save_config(self, obj):
log.info('Saving Config')
self.db.save(self.config_key, obj)
log.info('Config saved')
self.write_config_py(obj)
log.info('Wrote client config')
# Writes client config to client_config.py
def write_config_py(self, obj):
keypack_data = self.db.load(settings.CONFIG_DB_KEY_KEYPACK)
if keypack_data is None:
log.debug('*** Keypack data is None ***')
public_key = None
else:
public_key = keypack_data['client']['offline_public']
filename = os.path.join(self.cwd, *obj.CLIENT_CONFIG_PATH)
attr_str_format = " {} = '{}'\n"
attr_format = " {} = {}\n"
log.debug('Writing client_config.py')
with open(filename, 'w') as f:
f.write('class ClientConfig(object):\n')
log.debug('Adding PUBLIC_KEY to client_config.py')
f.write(attr_str_format.format('PUBLIC_KEY', public_key))
if hasattr(obj, 'APP_NAME'):
log.debug('Adding APP_NAME to client_config.py')
f.write(attr_str_format.format('APP_NAME', obj.APP_NAME))
if hasattr(obj, 'COMPANY_NAME'):
log.debug('Adding COMPANY_NAME to client_config.py')
f.write(attr_str_format.format('COMPANY_NAME',
obj.COMPANY_NAME))
if hasattr(obj, 'UPDATE_URLS'):
log.debug('Adding UPDATE_URLS to client_config.py')
f.write(attr_format.format('UPDATE_URLS', obj.UPDATE_URLS))
if hasattr(obj, 'MAX_DOWNLOAD_RETRIES'):
log.debug('Adding MAX_DOWNLOAD_RETRIES to client_config.py')
f.write(attr_format.format('MAX_DOWNLOAD_RETRIES',
obj.MAX_DOWNLOAD_RETRIES))