|
1 | 1 | import configparser
|
2 | 2 | import os
|
3 |
| -import sys |
4 |
| - |
5 |
| -from smct import ( |
6 |
| - notification, |
7 |
| - paths, |
8 |
| - registry, |
9 |
| - ui, |
10 |
| - ui_strings, |
11 |
| -) |
12 |
| - |
13 |
| -ENCODING = "utf-8" |
14 |
| - |
15 |
| -# keys |
16 |
| -SETTINGS_SECTION = "Settings" |
17 |
| -MONITOR_NAME_KEY = "monitor_name" |
18 |
| -MONITOR_SERIAL_KEY = "monitor_serial" |
19 |
| -MMT_PATH_KEY = "multimonitortool_executable" |
20 |
| -START_WITH_WINDOWS_KEY = "start_with_windows" |
21 |
| -FIRT_START_KEY = "first_start" |
22 |
| - |
23 |
| -# values |
24 |
| -MMT_PATH_VALUE = "" |
25 |
| -MONITOR_NAME_VALUE = "" |
26 |
| -MONITOR_SERIAL_VALUE = "" |
27 |
| -START_WITH_WINDOWS_VALUE = False |
28 |
| -FIRST_START_VALUE = True |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from smct import paths, registry, ui |
| 7 | + |
| 8 | +# config.ini structure |
| 9 | +_ENCODING = "utf-8" |
| 10 | + |
| 11 | +_SETTINGS_SECTION = "Settings" |
| 12 | + |
| 13 | +_MONITOR_NAME_KEY = "monitor_name" |
| 14 | +_MONITOR_SERIAL_KEY = "monitor_serial" |
| 15 | +_MMT_PATH_KEY = "multimonitortool_executable" |
| 16 | +_START_WITH_WINDOWS_KEY = "start_with_windows" |
| 17 | +_FIRST_START_KEY = "first_start" |
29 | 18 |
|
30 | 19 | _configparser = configparser.ConfigParser()
|
31 | 20 |
|
32 | 21 |
|
33 |
| -def check_for_missing_files(): |
| 22 | +def _check_for_missing_files(): |
| 23 | + if not os.path.exists(paths.ASSETS_DIR_PATH): |
| 24 | + os.mkdir(paths.ASSETS_DIR_PATH) |
| 25 | + |
34 | 26 | # Check for Icons
|
| 27 | + # ! maybe send error message here? |
| 28 | + if not os.path.exists(paths.ASSETS_ICO_PATH): |
| 29 | + download_assets_file(os.path.basename(paths.ASSETS_ICO_PATH)) |
| 30 | + # sys.exit(1) |
35 | 31 | if not os.path.exists(paths.ASSETS_ICON_ENABLED_PATH):
|
36 |
| - notification.send_error( |
37 |
| - paths.ASSETS_ICON_ENABLED_PATH + ui_strings.FILE_NOT_FOUND |
38 |
| - ) |
39 |
| - sys.exit(1) |
| 32 | + download_assets_file(os.path.basename(paths.ASSETS_ICON_ENABLED_PATH)) |
| 33 | + # sys.exit(1) |
40 | 34 | if not os.path.exists(paths.ASSETS_ICON_DISABLED_PATH):
|
41 |
| - notification.send_error( |
42 |
| - paths.ASSETS_ICON_DISABLED_PATH + ui_strings.FILE_NOT_FOUND |
43 |
| - ) |
44 |
| - sys.exit(1) |
| 35 | + download_assets_file(os.path.basename(paths.ASSETS_ICON_DISABLED_PATH)) |
| 36 | + # sys.exit(1) |
45 | 37 |
|
46 | 38 | # Check for temp folder
|
47 |
| - if not os.path.exists(paths.TEMP_DIR_PATH): |
48 |
| - os.makedirs(paths.TEMP_DIR_PATH) |
| 39 | + if not os.path.exists(paths.MMT_DIR_PATH): |
| 40 | + os.makedirs(paths.MMT_DIR_PATH) |
49 | 41 |
|
50 | 42 |
|
51 |
| -def read_config(): |
52 |
| - # Check if config.ini file is present |
53 |
| - if not os.path.exists(paths.CONFIG_PATH): |
54 |
| - _create_default_config_file() |
| 43 | +def download_assets_file(image_name): |
| 44 | + image_url = paths.ASSETS_BASE_URL + image_name |
| 45 | + response = requests.get(image_url, stream=True, timeout=5) |
| 46 | + |
| 47 | + if response.status_code == 200: |
| 48 | + filename = response.url.split("/")[-1] |
55 | 49 |
|
56 |
| - _configparser.read(paths.CONFIG_PATH, encoding=ENCODING) |
| 50 | + with open(os.path.join(paths.ASSETS_DIR_PATH, filename), "wb") as f: |
| 51 | + for chunk in response.iter_content(1024): |
| 52 | + f.write(chunk) |
57 | 53 |
|
58 |
| - # * pylint: disable=global-statement |
59 |
| - global START_WITH_WINDOWS_VALUE, MMT_PATH_VALUE, MONITOR_NAME_VALUE, MONITOR_SERIAL_VALUE, FIRST_START_VALUE |
60 | 54 |
|
61 |
| - MMT_PATH_VALUE = _configparser.get(SETTINGS_SECTION, MMT_PATH_KEY) |
62 |
| - MONITOR_NAME_VALUE = _configparser.get(SETTINGS_SECTION, MONITOR_NAME_KEY) |
63 |
| - MONITOR_SERIAL_VALUE = _configparser.get(SETTINGS_SECTION, MONITOR_SERIAL_KEY) |
64 |
| - START_WITH_WINDOWS_VALUE = _configparser.getboolean( |
65 |
| - SETTINGS_SECTION, START_WITH_WINDOWS_KEY |
66 |
| - ) |
67 |
| - FIRST_START_VALUE = _configparser.getboolean(SETTINGS_SECTION, FIRT_START_KEY) |
| 55 | +def init_config(): |
| 56 | + if not os.path.exists(paths.CONFIG_PATH): |
| 57 | + _create_default_config_file() |
68 | 58 |
|
69 |
| - check_for_missing_files() |
| 59 | + _check_for_missing_files() |
70 | 60 |
|
71 |
| - if START_WITH_WINDOWS_VALUE: |
| 61 | + if get_start_with_windows_value(): |
72 | 62 | registry.add_to_autostart()
|
73 | 63 | else:
|
74 | 64 | registry.remove_from_autostart()
|
75 | 65 |
|
76 |
| - if FIRST_START_VALUE: |
| 66 | + if get_first_start_with_windows_value(): |
77 | 67 | ui.init_mmt_selection_frame()
|
78 |
| - FIRST_START_VALUE = False |
79 |
| - set_config_value(SETTINGS_SECTION, FIRT_START_KEY, FIRST_START_VALUE) |
| 68 | + set_first_start_value(False) |
80 | 69 |
|
81 | 70 |
|
82 |
| -def _create_default_config_file(): |
83 |
| - _configparser["Settings"] = { |
84 |
| - MONITOR_NAME_KEY: "Example Monitor", |
85 |
| - MONITOR_SERIAL_KEY: "12345", |
86 |
| - MMT_PATH_KEY: "C:/MultiMonitorTool.exe", |
87 |
| - START_WITH_WINDOWS_KEY: "no", |
88 |
| - FIRT_START_KEY: "yes", |
89 |
| - } |
90 |
| - with open(paths.CONFIG_PATH, "w", encoding=ENCODING) as _file_object: |
91 |
| - _configparser.write(_file_object) |
| 71 | +def get_mmt_path_value(): |
| 72 | + _read_from_config() |
| 73 | + return _configparser.get(_SETTINGS_SECTION, _MMT_PATH_KEY) |
92 | 74 |
|
93 | 75 |
|
94 |
| -def set_config_value(section, key, value): |
95 |
| - if isinstance(value, bool): |
96 |
| - value_str = "yes" if value else "no" |
97 |
| - else: |
98 |
| - value_str = str(value) |
99 |
| - _configparser[section][key] = value_str |
100 |
| - with open(paths.CONFIG_PATH, "w", encoding=ENCODING) as configfile: |
| 76 | +def set_mmt_path_value(_value): |
| 77 | + _configparser[_SETTINGS_SECTION][_MMT_PATH_KEY] = _value |
| 78 | + _write_to_config() |
| 79 | + |
| 80 | + |
| 81 | +def get_monitor_name_value(): |
| 82 | + _read_from_config() |
| 83 | + return _configparser.get(_SETTINGS_SECTION, _MONITOR_NAME_KEY) |
| 84 | + |
| 85 | + |
| 86 | +def set_monitor_name_value(_value): |
| 87 | + _configparser[_SETTINGS_SECTION][_MONITOR_NAME_KEY] = _value |
| 88 | + _write_to_config() |
| 89 | + |
| 90 | + |
| 91 | +def get_monitor_serial_value(): |
| 92 | + _read_from_config() |
| 93 | + return _configparser.get(_SETTINGS_SECTION, _MONITOR_SERIAL_KEY) |
| 94 | + |
| 95 | + |
| 96 | +def set_monitor_serial_value(_value): |
| 97 | + _configparser[_SETTINGS_SECTION][_MONITOR_SERIAL_KEY] = _value |
| 98 | + _write_to_config() |
| 99 | + |
| 100 | + |
| 101 | +def get_start_with_windows_value(): |
| 102 | + _read_from_config() |
| 103 | + return _configparser.getboolean(_SETTINGS_SECTION, _START_WITH_WINDOWS_KEY) |
| 104 | + |
| 105 | + |
| 106 | +def set_start_with_windows_value(_value): |
| 107 | + value_str = "yes" if _value else "no" |
| 108 | + _configparser[_SETTINGS_SECTION][_START_WITH_WINDOWS_KEY] = value_str |
| 109 | + _write_to_config() |
| 110 | + |
| 111 | + |
| 112 | +def get_first_start_with_windows_value(): |
| 113 | + _read_from_config() |
| 114 | + return _configparser.getboolean(_SETTINGS_SECTION, _FIRST_START_KEY) |
| 115 | + |
| 116 | + |
| 117 | +def set_first_start_value(_value): |
| 118 | + value_str = "yes" if _value else "no" |
| 119 | + _configparser[_SETTINGS_SECTION][_FIRST_START_KEY] = value_str |
| 120 | + _write_to_config() |
| 121 | + |
| 122 | + |
| 123 | +def _read_from_config(): |
| 124 | + _configparser.read(paths.CONFIG_PATH, encoding=_ENCODING) |
| 125 | + |
| 126 | + |
| 127 | +def _write_to_config(): |
| 128 | + with open(paths.CONFIG_PATH, "w", encoding=_ENCODING) as configfile: |
101 | 129 | _configparser.write(configfile)
|
102 |
| - # print("Setting config value: " + key + " to " + value) |
103 | 130 |
|
104 | 131 |
|
105 |
| -def get_config_value(section, key): |
106 |
| - return _configparser.get(section, key) |
| 132 | +def _create_default_config_file(): |
| 133 | + _configparser[_SETTINGS_SECTION] = { |
| 134 | + _MONITOR_NAME_KEY: "Example Monitor", |
| 135 | + _MONITOR_SERIAL_KEY: "12345", |
| 136 | + _MMT_PATH_KEY: "C:/MultiMonitorTool.exe", |
| 137 | + _START_WITH_WINDOWS_KEY: "no", |
| 138 | + _FIRST_START_KEY: "yes", |
| 139 | + } |
| 140 | + _write_to_config() |
0 commit comments