|
19 | 19 | DEFAULT_BUCKET_PATTERN = 'lambda-cron-{environment}' |
20 | 20 |
|
21 | 21 |
|
22 | | -def get_package_root_directory(): |
23 | | - return os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../')) |
24 | | - |
25 | | - |
26 | | -def get_cli_config_file_path(): |
| 22 | +def get_default_cli_config_file_path(): |
27 | 23 | return os.path.expanduser('~/.lambda-cron.yml') |
28 | 24 |
|
29 | 25 |
|
30 | | -def get_jsonschema_file_path(): |
31 | | - return os.path.join(get_package_root_directory(), 'schema.json') |
| 26 | +class LambdaCronConfigFileNotFound(Exception): |
| 27 | + pass |
32 | 28 |
|
33 | 29 |
|
34 | | -def load_config(): |
35 | | - if os.path.exists(get_cli_config_file_path()): |
36 | | - with open(get_cli_config_file_path(), 'r') as config_file: |
| 30 | +def load_config(file_path): |
| 31 | + if os.path.exists(file_path): |
| 32 | + with open(file_path, 'r') as config_file: |
37 | 33 | return yaml.load(config_file) |
38 | | - return False |
| 34 | + raise LambdaCronConfigFileNotFound('Config file not found: {}'.format(file_path)) |
39 | 35 |
|
40 | 36 |
|
41 | | -class CliConfig: |
| 37 | +class CliConfigParser: |
42 | 38 |
|
43 | | - def __init__(self, environment): |
44 | | - self.environment = environment |
45 | | - self.bucket = DEFAULT_BUCKET_PATTERN.format(environment=self.environment) |
46 | | - self.enabled = True |
47 | | - self.alarm_enabled = False |
48 | | - self.alarm_email = '' |
49 | | - self.hours = 1 |
50 | | - self.minutes = False |
| 39 | + DEFAULT_ENABLED = True |
| 40 | + DEFAULT_ALARM_ENABLED = False |
| 41 | + DEFAULT_ALARM_EMAIL = '' |
51 | 42 |
|
52 | | - self.config = load_config() |
53 | | - self.set_bucket() |
54 | | - self.set_enabled() |
55 | | - self.set_alarm() |
56 | | - self.set_frequency() |
| 43 | + def __init__(self, config_file_path): |
| 44 | + self.file_config = load_config(config_file_path) |
| 45 | + self.environment = None |
57 | 46 |
|
58 | | - def is_environment_in_config_file(self): |
59 | | - return self.config and (self.environment in self.config) |
| 47 | + def get_config(self, environment): |
| 48 | + self.environment = environment |
| 49 | + cli_config = CliConfig(self.environment) |
| 50 | + cli_config.bucket = self._get_bucket() |
| 51 | + cli_config.enabled = self._get_enabled() |
| 52 | + cli_config.alarm_enabled, cli_config.alarm_email = self._get_alarm() |
| 53 | + self._set_frequency(cli_config) |
| 54 | + return cli_config |
60 | 55 |
|
61 | | - def is_custom_options(self, option): |
62 | | - return self.is_environment_in_config_file() and (option in self.config[self.environment]) |
| 56 | + def _is_environment_in_config_file(self): |
| 57 | + return self.file_config and (self.environment in self.file_config) |
63 | 58 |
|
64 | | - def is_global_option(self, option): |
65 | | - return self.config and ('all' in self.config) and (option in self.config['all']) |
| 59 | + def _is_custom_options(self, option): |
| 60 | + return self._is_environment_in_config_file() and (option in self.file_config[self.environment]) |
66 | 61 |
|
67 | | - def get_config_option(self, option): |
| 62 | + def _is_global_option(self, option): |
| 63 | + return self.file_config and ('all' in self.file_config) and (option in self.file_config['all']) |
| 64 | + |
| 65 | + def _get_config_option(self, option): |
68 | 66 | option_value = None |
69 | | - if self.is_custom_options(option): |
70 | | - option_value = self.config[self.environment][option] |
71 | | - elif self.is_global_option(option): |
72 | | - option_value = self.config['all'][option] |
| 67 | + if self._is_custom_options(option): |
| 68 | + option_value = self.file_config[self.environment][option] |
| 69 | + elif self._is_global_option(option): |
| 70 | + option_value = self.file_config['all'][option] |
73 | 71 | return option_value |
74 | 72 |
|
75 | | - def set_alarm(self): |
76 | | - alarm_config = self.get_config_option('alarm') |
| 73 | + def _get_alarm(self): |
| 74 | + alarm_config = self._get_config_option('alarm') |
77 | 75 |
|
78 | 76 | if not alarm_config: |
79 | | - return |
| 77 | + return self.DEFAULT_ALARM_ENABLED, self.DEFAULT_ALARM_EMAIL |
80 | 78 |
|
81 | | - self.alarm_enabled = alarm_config['enabled'] |
82 | | - if not isinstance(self.alarm_enabled, bool): |
| 79 | + alarm_enabled = alarm_config['enabled'] |
| 80 | + if not isinstance(alarm_enabled, bool): |
83 | 81 | raise Exception("Settings for 'alarm.enabled' must be a bool value") |
84 | | - if self.alarm_enabled: |
| 82 | + alarm_email = self.DEFAULT_ALARM_EMAIL |
| 83 | + if alarm_enabled: |
85 | 84 | if 'email' not in alarm_config: |
86 | 85 | raise Exception("Email must be provided when alarm is enabled") |
87 | | - self.alarm_email = alarm_config['email'] |
88 | | - |
89 | | - def set_bucket(self): |
90 | | - if self.is_custom_options('bucket'): |
91 | | - self.bucket = self.config[self.environment]['bucket'] |
92 | | - elif self.is_global_option('bucket'): |
93 | | - if '{environment}' in self.config['all']['bucket']: |
94 | | - self.bucket = self.config['all']['bucket'].format(environment=self.environment) |
| 86 | + alarm_email = alarm_config['email'] |
| 87 | + |
| 88 | + return alarm_enabled, alarm_email |
| 89 | + |
| 90 | + def _get_bucket(self): |
| 91 | + if self._is_custom_options('bucket'): |
| 92 | + return self.file_config[self.environment]['bucket'] |
| 93 | + elif self._is_global_option('bucket'): |
| 94 | + if '{environment}' in self.file_config['all']['bucket']: |
| 95 | + return self.file_config['all']['bucket'].format(environment=self.environment) |
95 | 96 | else: |
96 | | - self.bucket = self.config['all']['bucket'] + "-{}".format(self.environment) |
| 97 | + return self.file_config['all']['bucket'] + "-{}".format(self.environment) |
| 98 | + else: |
| 99 | + return DEFAULT_BUCKET_PATTERN.format(environment=self.environment) |
97 | 100 |
|
98 | | - def get_time_key_value(self, config_every): |
| 101 | + @staticmethod |
| 102 | + def _get_time_key_value(config_every): |
99 | 103 | time_key = None |
100 | 104 | if 'hours' in config_every: |
101 | 105 | time_key = 'hours' |
102 | 106 |
|
103 | 107 | if 'minutes' in config_every: |
104 | 108 | if time_key is not None: |
105 | | - raise Exception("Only one of 'hours' or 'minutes' must be used for the frequency ('every'). Both are not allowed.") |
| 109 | + raise Exception( |
| 110 | + "Only one of 'hours' or 'minutes' must be used for the frequency ('every'). Both are not allowed.") |
106 | 111 | time_key = 'minutes' |
107 | 112 |
|
108 | 113 | if time_key is None: |
109 | 114 | raise Exception("Invalid time key used for frequency ('every'). Allowed keys: 'hours' or 'minutes'") |
110 | 115 |
|
111 | 116 | return time_key, int(config_every[time_key]) |
112 | 117 |
|
113 | | - def set_frequency(self): |
114 | | - config_every = self.get_config_option('every') |
| 118 | + def _set_frequency(self, cli_config): |
| 119 | + config_every = self._get_config_option('every') |
115 | 120 | if not config_every: |
116 | 121 | return |
117 | 122 |
|
118 | | - time_key, time_value = self.get_time_key_value(config_every) |
119 | | - self.hours = False |
120 | | - self.minutes = False |
121 | | - setattr(self, time_key, time_value) |
| 123 | + cli_config.hours = cli_config.minutes = False |
| 124 | + time_key, time_value = self._get_time_key_value(config_every) |
| 125 | + setattr(cli_config, time_key, time_value) |
122 | 126 |
|
123 | | - def set_enabled(self): |
124 | | - enabled_default_value = self.enabled |
125 | | - self.enabled = self.get_config_option('enabled') |
126 | | - if self.enabled is None: |
127 | | - self.enabled = enabled_default_value |
| 127 | + def _get_enabled(self): |
| 128 | + enabled = self._get_config_option('enabled') |
| 129 | + if enabled is None: |
| 130 | + return self.DEFAULT_ENABLED |
128 | 131 |
|
129 | | - if not isinstance(self.enabled, bool): |
| 132 | + if not isinstance(enabled, bool): |
130 | 133 | raise Exception("Settings for 'enabled' must be a bool value") |
| 134 | + |
| 135 | + return enabled |
| 136 | + |
| 137 | + |
| 138 | +class CliConfig: |
| 139 | + |
| 140 | + def __init__(self, environment): |
| 141 | + self.environment = environment |
| 142 | + self.bucket = DEFAULT_BUCKET_PATTERN.format(environment=self.environment) |
| 143 | + self.enabled = True |
| 144 | + self.alarm_enabled = False |
| 145 | + self.alarm_email = '' |
| 146 | + self.hours = 1 |
| 147 | + self.minutes = False |
0 commit comments