Skip to content

Commit 001e9bb

Browse files
committed
assets now get downloaded if missing, modified how config variables work
1 parent 4d13931 commit 001e9bb

11 files changed

+193
-161
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,5 @@ cython_debug/
153153

154154
output/
155155
.vscode/
156-
temp/
156+
mmt/
157157
config.ini

assets2/icon.ico

5.78 KB
Binary file not shown.

assets2/iconDisabled.png

15.3 KB
Loading

assets2/iconEnabled.png

12.5 KB
Loading

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def main():
5-
config.read_config()
5+
config.init_config()
66
tray.init_tray()
77

88

smct/config.py

+110-76
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,140 @@
11
import configparser
22
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"
2918

3019
_configparser = configparser.ConfigParser()
3120

3221

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+
3426
# 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)
3531
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)
4034
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)
4537

4638
# 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)
4941

5042

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]
5549

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)
5753

58-
# * pylint: disable=global-statement
59-
global START_WITH_WINDOWS_VALUE, MMT_PATH_VALUE, MONITOR_NAME_VALUE, MONITOR_SERIAL_VALUE, FIRST_START_VALUE
6054

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()
6858

69-
check_for_missing_files()
59+
_check_for_missing_files()
7060

71-
if START_WITH_WINDOWS_VALUE:
61+
if get_start_with_windows_value():
7262
registry.add_to_autostart()
7363
else:
7464
registry.remove_from_autostart()
7565

76-
if FIRST_START_VALUE:
66+
if get_first_start_with_windows_value():
7767
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)
8069

8170

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)
9274

9375

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:
101129
_configparser.write(configfile)
102-
# print("Setting config value: " + key + " to " + value)
103130

104131

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()

smct/multimonitortool.py

+46-33
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
import subprocess
2-
32
import pandas as pd
43
from smct import config, paths
54

6-
7-
def _get_monitor_df():
8-
_run_mmt_command("/scomma", paths.MMT_CSV_PATH)
9-
_monitor_df = []
10-
11-
data = pd.read_csv(paths.MMT_CSV_PATH)
12-
for index in range(len(data)):
13-
_monitor_df.append(data.iloc[index])
14-
return _monitor_df
5+
MMT_CSV_MONITOR_NAME = "Monitor Name"
6+
MMT_CSV_SERIAL_NUMBER = "Monitor Serial Number"
7+
MMT_CSV_NAME = "Name"
8+
MMT_CSV_ACTIVE = "Active"
9+
MMT_CSV_YES = "yes"
10+
MMT_CSV_NO = "no"
1511

1612

1713
def get_monitor_selection_list():
1814
_monitor_selection_list = []
19-
for _monitor in _get_monitor_df():
20-
_monitor_name = _monitor["Monitor Name"]
21-
_monitor_serial = _monitor["Monitor Serial Number"]
22-
_monitor_selection_list.append(f"{_monitor_name} | {_monitor_serial}")
15+
monitor_df = _get_monitor_df()
16+
# pylint: disable=unused-variable
17+
for index, row in monitor_df.iterrows():
18+
_monitor_id = row[MMT_CSV_NAME][-1]
19+
_monitor_name = row[MMT_CSV_MONITOR_NAME]
20+
_monitor_serial = row[MMT_CSV_SERIAL_NUMBER]
21+
_monitor_selection_list.append(
22+
f"{_monitor_id} | {_monitor_name} | {_monitor_serial}"
23+
)
24+
_monitor_selection_list.sort()
2325
return _monitor_selection_list
2426

2527

28+
def _get_monitor_df():
29+
_run_mmt_command("/scomma", paths.MMT_CSV_PATH)
30+
data = pd.read_csv(paths.MMT_CSV_PATH)
31+
return data
32+
33+
2634
def _run_mmt_command(command, destination):
2735
try:
2836
subprocess.run(
2937
[
30-
config.MMT_PATH_VALUE,
38+
config.get_mmt_path_value(),
3139
command,
3240
destination,
3341
],
3442
check=True,
3543
)
3644
except subprocess.CalledProcessError as error:
37-
print(f"{config.MMT_PATH_VALUE} {command} {destination} failed: {error}")
45+
print(f"{config.get_mmt_path_value} {command} {destination} failed: {error}")
3846

3947

4048
def save_mmt_config():
@@ -46,39 +54,44 @@ def enable_monitor():
4654

4755

4856
def disable_monitor():
49-
_run_mmt_command("/disable", get_selected_monitor_id())
57+
selected_monitor_id = get_selected_monitor_id()
58+
if selected_monitor_id:
59+
_run_mmt_command("/disable", selected_monitor_id)
5060

5161

5262
def enable_all_disabled_monitors():
53-
for _monitor_id in _get_all_disabled_monitor_ids():
63+
disabled_monitor_ids = _get_all_disabled_monitor_ids()
64+
for _monitor_id in disabled_monitor_ids:
5465
_run_mmt_command("/enable", _monitor_id)
5566

5667

5768
def get_selected_monitor_id():
58-
for monitor in _get_monitor_df():
69+
# pylint: disable=unused-variable
70+
for index, monitor in _get_monitor_df().iterrows():
5971
if (
60-
monitor["Monitor Name"] == config.MONITOR_NAME_VALUE
61-
and str(monitor["Monitor Serial Number"]) == config.MONITOR_SERIAL_VALUE
72+
monitor[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value()
73+
and str(monitor[MMT_CSV_SERIAL_NUMBER]) == config.get_monitor_serial_value()
6274
):
63-
_id = monitor["Name"]
64-
return _id[-1]
75+
return monitor[MMT_CSV_NAME][-1]
6576

6677

6778
def _get_all_disabled_monitor_ids():
68-
_monitor_id_list = []
69-
for monitor in _get_monitor_df():
70-
if monitor["Active"].upper() == "NO":
71-
_id = monitor["Name"]
72-
_monitor_id_list.append(_id[-1])
73-
return _monitor_id_list
79+
monitor_df = _get_monitor_df()
80+
disabled_monitor_ids = []
81+
# pylint: disable=unused-variable
82+
for index, monitor in monitor_df.iterrows():
83+
if monitor[MMT_CSV_ACTIVE].lower() == MMT_CSV_NO:
84+
disabled_monitor_ids.append(monitor[MMT_CSV_NAME][-1])
85+
return disabled_monitor_ids
7486

7587

7688
def is_selected_monitor_enabled():
77-
for monitor in _get_monitor_df():
89+
# pylint: disable=unused-variable
90+
for index, monitor in _get_monitor_df().iterrows():
7891
if (
79-
monitor["Monitor Name"] == config.MONITOR_NAME_VALUE
80-
and str(monitor["Monitor Serial Number"]) == config.MONITOR_SERIAL_VALUE
81-
and monitor["Active"].upper() == "YES"
92+
monitor[MMT_CSV_MONITOR_NAME] == config.get_monitor_name_value()
93+
and str(monitor[MMT_CSV_SERIAL_NUMBER]) == config.get_monitor_serial_value()
94+
and monitor[MMT_CSV_ACTIVE].lower() == MMT_CSV_YES
8295
):
8396
return True
8497
return False

smct/paths.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ def strip_package_name_from_path(path):
2424
BASE_PATH = get_base_path()
2525
EXE_PATH = BASE_PATH + "\\" + ui_strings.APP_NAME + ".exe"
2626

27-
CONFIG_PATH = BASE_PATH + r"\config.ini"
28-
ASSETS_DIR_PATH = BASE_PATH + r"\assets"
29-
TEMP_DIR_PATH = BASE_PATH + r"\temp"
30-
31-
MMT_CSV_PATH = TEMP_DIR_PATH + r"\MultiMonitorToolOutput.csv"
32-
MMT_CONFIG_PATH = TEMP_DIR_PATH + r"\MultiMonitorToolConfig"
33-
ASSETS_ICON_ENABLED_PATH = ASSETS_DIR_PATH + r"\iconEnabled.png"
34-
ASSETS_ICON_DISABLED_PATH = ASSETS_DIR_PATH + r"\iconDisabled.png"
35-
ASSETS_ICO_PATH = ASSETS_DIR_PATH + r"\icon.ico"
27+
CONFIG_PATH = BASE_PATH + "\\config.ini"
28+
ASSETS_DIR_PATH = BASE_PATH + "\\assets"
29+
MMT_DIR_PATH = BASE_PATH + "\\mmt"
30+
31+
MMT_CSV_PATH = MMT_DIR_PATH + "\\MultiMonitorToolOutput.csv"
32+
MMT_CONFIG_PATH = MMT_DIR_PATH + "\\MultiMonitorToolConfig"
33+
34+
ASSETS_ICON_ENABLED_PATH = ASSETS_DIR_PATH + "\\iconEnabled.png"
35+
ASSETS_ICON_DISABLED_PATH = ASSETS_DIR_PATH + "\\iconDisabled.png"
36+
ASSETS_ICO_PATH = ASSETS_DIR_PATH + "\\icon.ico"
37+
38+
ASSETS_BASE_URL = "https://raw.githubusercontent.com/wrecks-code/SimpleMonitorControlTray/main/assets/"

0 commit comments

Comments
 (0)