11import configparser
22import os
3-
43import requests
5-
64from smct import paths , ui
7- from smct .logger import log
5+ from smct .logger import log , INFO , ERROR
86
9- # config.ini structure
107ENCODING = "utf-8"
11-
12- _SETTINGS_SECTION = "Settings"
13-
14- _MONITOR_NAME_KEY = "monitor_name"
15- _MONITOR_SERIAL_KEY = "monitor_serial"
16- _MMT_PATH_KEY = "multimonitortool_executable"
17- _START_WITH_WINDOWS_KEY = "start_with_windows"
18- _FIRST_START_KEY = "first_start"
19-
8+ _SECTION_SETTINGS = "Settings"
9+ KEY_MONITOR_NAME = "monitor_name"
10+ KEY_MONITOR_SERIAL = "monitor_serial"
11+ KEY_MMT_PATH = "multimonitortool_executable"
12+ KEY_START_WITH_WINDOWS = "start_with_windows"
13+ KEY_FIRST_START = "first_start"
14+
15+ _DEFAULT_CONFIG = {
16+ KEY_MONITOR_NAME : "Example Monitor" ,
17+ KEY_MONITOR_SERIAL : "12345" ,
18+ KEY_MMT_PATH : "C:/MultiMonitorTool.exe" ,
19+ KEY_START_WITH_WINDOWS : "no" ,
20+ KEY_FIRST_START : "yes" ,
21+ }
2022_configparser = configparser .ConfigParser ()
2123
2224
2325def _check_for_missing_files ():
24- if not os .path .exists (paths .CONFIG_PATH ):
25- _create_default_config_file ()
26- log (f"Creating { paths .CONFIG_FILE_NAME } " )
27-
28- if not os .path .exists (paths .ASSETS_DIR_PATH ):
29- os .mkdir (paths .ASSETS_DIR_PATH )
30- log (f"Creating { paths .ASSETS_DIR_NAME } folder" )
31-
32- # Check for Icons
33- if not os .path .exists (paths .ASSETS_ICO_PATH ):
34- log (f"Downloading { paths .ASSETS_BASE_URL } { paths .ASSETS_ICO_NAME } " )
35- download_assets_file (paths .ASSETS_ICO_NAME )
36- # sys.exit(1)
37- if not os .path .exists (paths .ASSETS_ICON_ENABLED_PATH ):
38- log (f"Downloading { paths .ASSETS_BASE_URL } { paths .ASSETS_ICON_ENABLED_NAME } " )
39- download_assets_file (paths .ASSETS_ICON_ENABLED_NAME )
40- # sys.exit(1)
41- if not os .path .exists (paths .ASSETS_ICON_DISABLED_PATH ):
42- log (f"Downloading { paths .ASSETS_BASE_URL } { paths .ASSETS_ICON_DISABLED_NAME } " )
43- download_assets_file (paths .ASSETS_ICON_DISABLED_NAME )
44- # sys.exit(1)
45-
46- # Check for temp folder
47- if not os .path .exists (paths .MMT_DIR_PATH ):
48- os .makedirs (paths .MMT_DIR_PATH )
49- log (f"Creating { paths .MMT_DIR_NAME } folder" )
26+ paths_actions = {
27+ paths .CONFIG_PATH : _create_default_config_file ,
28+ paths .ASSETS_DIR_PATH : lambda : os .mkdir (paths .ASSETS_DIR_PATH ),
29+ paths .ASSETS_ICO_PATH : lambda : download_assets_file (paths .ASSETS_ICO_NAME ),
30+ paths .ASSETS_ICON_ENABLED_PATH : lambda : download_assets_file (
31+ paths .ASSETS_ICON_ENABLED_NAME
32+ ),
33+ paths .ASSETS_ICON_DISABLED_PATH : lambda : download_assets_file (
34+ paths .ASSETS_ICON_DISABLED_NAME
35+ ),
36+ }
37+ for path , action in paths_actions .items ():
38+ if not os .path .exists (path ):
39+ action ()
40+ log (INFO , f"Creating or downloading { os .path .basename (path )} " )
5041
5142
5243def download_assets_file (image_name ):
53- image_url = paths .ASSETS_BASE_URL + image_name
44+ image_url = os . path . join ( paths .ASSETS_BASE_URL , image_name )
5445 response = requests .get (image_url , stream = True , timeout = 5 )
55-
5646 if response .status_code == 200 :
57- filename = response .url .split ("/" )[- 1 ]
58-
59- with open (os .path .join (paths .ASSETS_DIR_PATH , filename ), "wb" ) as f :
47+ with open (os .path .join (paths .ASSETS_DIR_PATH , image_name ), "wb" ) as f :
6048 for chunk in response .iter_content (1024 ):
6149 f .write (chunk )
6250 else :
63- log (f"Error occurred while downloading { image_name } : { response .status_code } " )
51+ log (
52+ ERROR ,
53+ f"Error occurred while downloading { image_name } : { response .status_code } " ,
54+ )
6455
6556
6657def init_config ():
6758 _check_for_missing_files ()
68-
69- if get_first_start_value ():
59+ if get_value (KEY_FIRST_START ):
7060 ui .init_root_window ()
71- set_first_start_value (False )
72-
73-
74- def get_mmt_path_value ():
75- _read_from_config ()
76- return _configparser .get (_SETTINGS_SECTION , _MMT_PATH_KEY )
77-
78-
79- def set_mmt_path_value (_value ):
80- log (f"Config.ini - Setting mmt_path to { _value } " )
81- _configparser [_SETTINGS_SECTION ][_MMT_PATH_KEY ] = _value
82- _write_to_config ()
83-
84-
85- def get_monitor_name_value ():
86- _read_from_config ()
87- return _configparser .get (_SETTINGS_SECTION , _MONITOR_NAME_KEY )
88-
61+ set_value (KEY_FIRST_START , False )
8962
90- def set_monitor_name_value (_value ):
91- log (f"Config.ini - Setting monitor_name to { _value } " )
92- _configparser [_SETTINGS_SECTION ][_MONITOR_NAME_KEY ] = _value
93- _write_to_config ()
9463
95-
96- def get_monitor_serial_value ():
64+ def get_value (key ):
9765 _read_from_config ()
98- return _configparser .get (_SETTINGS_SECTION , _MONITOR_SERIAL_KEY )
99-
66+ value = _configparser .get (_SECTION_SETTINGS , key )
67+ return value . lower () == "yes" if value . lower () in [ "yes" , "no" ] else value
10068
101- def set_monitor_serial_value (_value ):
102- log (f"Config.ini - Setting monitor_serial to { _value } " )
103- _configparser [_SETTINGS_SECTION ][_MONITOR_SERIAL_KEY ] = _value
104- _write_to_config ()
105-
106-
107- def get_start_with_windows_value ():
108- _read_from_config ()
109- return _configparser .getboolean (_SETTINGS_SECTION , _START_WITH_WINDOWS_KEY )
110-
111-
112- def set_start_with_windows_value (_value ):
113- value_str = "yes" if _value else "no"
114- log (f"Config.ini - Setting start_with_windows to { value_str } " )
115- _configparser [_SETTINGS_SECTION ][_START_WITH_WINDOWS_KEY ] = value_str
116- _write_to_config ()
117-
118-
119- def get_first_start_value ():
120- _read_from_config ()
121- return _configparser .getboolean (_SETTINGS_SECTION , _FIRST_START_KEY )
12269
70+ def set_value (key , value ):
71+ if isinstance (value , bool ):
72+ value_str = "yes" if value else "no"
73+ else :
74+ value_str = str (value )
12375
124- def set_first_start_value (_value ):
125- value_str = "yes" if _value else "no"
126- log (f"Config.ini - Setting first_start to { value_str } " )
127- _configparser [_SETTINGS_SECTION ][_FIRST_START_KEY ] = value_str
76+ log (INFO , f"{ paths .CONFIG_FILE_NAME } - Setting { key } to { value_str } " )
77+ _configparser [_SECTION_SETTINGS ][key ] = value_str
12878 _write_to_config ()
12979
13080
@@ -138,11 +88,5 @@ def _write_to_config():
13888
13989
14090def _create_default_config_file ():
141- _configparser [_SETTINGS_SECTION ] = {
142- _MONITOR_NAME_KEY : "Example Monitor" ,
143- _MONITOR_SERIAL_KEY : "12345" ,
144- _MMT_PATH_KEY : "C:/MultiMonitorTool.exe" ,
145- _START_WITH_WINDOWS_KEY : "no" ,
146- _FIRST_START_KEY : "yes" ,
147- }
91+ _configparser [_SECTION_SETTINGS ] = _DEFAULT_CONFIG
14892 _write_to_config ()
0 commit comments