# -*- coding: utf-8 -*-
from setuphelpers import *
import chardet
import subprocess
import time
def install():
# Initializing variables
bin_name = glob.glob("Greenshot-INSTALLER-*-RELEASE.exe")[0]
basedir_conf_file = makepath(basedir, "install.inf")
try:
# Installing the package
install_exe_if_needed(
bin_name,
silentflags="/LOADINF=install.inf /VERYSILENT /NORESTART",
key="Greenshot_is1",
min_version=control.get_software_version(),
timeout=150,
)
except:
# Fix Windows 11 unexpected timeout
time.sleep(10)
# Installing the package
install_exe_if_needed(
bin_name,
silentflags="/LOADINF=install.inf /VERYSILENT /NORESTART",
key="Greenshot_is1",
min_version=control.get_software_version(),
timeout=300,
)
def session_setup():
user_conf_dir = makepath(application_data, "Greenshot")
user_conf_path = makepath(user_conf_dir, "Greenshot.ini")
print(f"Updating: {user_conf_path}")
if not isdir(user_conf_dir):
mkdirs(user_conf_dir)
config_dict = {
"UpdateCheckInterval": "UpdateCheckInterval=0",
"Language": "Language=" + get_language(full_locale=True, separator="-"), # fr-FR
"Destinations": "Destinations=Editor",
"CaptureMousepointer": "CaptureMousepointer=False",
}
user_screenshots_dir = makepath(user_home_directory, "Pictures", "Screenshots")
if isdir(user_screenshots_dir):
config_dict.update({"OutputFilePath": "OutputFilePath=" + user_screenshots_dir})
base_file_str = r"""; Greenshot core configuration
[Core]
; The language in IETF format (e.g. en-US)
Language=fr-FR
; Is this the first time launch?
IsFirstLaunch=False
; Which destinations? Possible options (more might be added by plugins) are: Editor, FileDefault, FileWithDialog, Clipboard, Printer, EMail, Picker
Destinations=Editor
; Should the mouse be captured?
CaptureMousepointer=False
; How many days between every update check? (0=no checks)
UpdateCheckInterval=0
; Output file path.
OutputFilePath=C:\Users\username\Desktop
; If the target file already exists True will make Greenshot always overwrite and False will display a 'Save-As' dialog.
OutputFileAllowOverwrite=True
"""
# Editting user_conf
# killalltasks(control.get_impacted_process_list())
killalltasks(["Greenshot"])
edit_config_raw_file(user_conf_path, config_dict, base_file_str, encoding="UTF-8-SIG")
subprocess.Popen([makepath(programfiles, "Greenshot", "Greenshot.exe")])
def edit_config_raw_file(conf_path, config_dict, base_file_str=None, encoding=None):
r"""
Edits a configuration file at the given path by replacing or adding key-value pairs from the provided dictionary.
This function allows you to edit INI file with an incorrect format, flat file, dirty JSON, etc.
Args:
conf_path (str): The path to the configuration file.
config_dict (dict): A dictionary containing key-value pairs to be updated or added in the configuration file.
The key is strictly the string at the beginning of the line you are trying to edit.
The value is the new replacing line.
(Note: take care of spaces and tabs (\t). but line return (\n) will be added automatically.)
base_file_str (str, optional): The base file content to be used if the configuration file doesn't exist. Defaults to None.
(Note: The values from the `config_dict` do not need to be included in `base_file_str` explicitly,
as they will be added automatically during the editing process.)
(Make sure to check and define the encoding in this case.)
encoding (str, optional): The encoding of the configuration file. If not specified, the function attempts to detect it. Defaults to None.
Returns:
dict: for key the previous line and value the new line of edited lines
>>> edit_config_raw_file(user_conf_path, config_dict, base_file_str, encoding="UTF-8-SIG")
dict({'Language=en-US\n': 'Language=fr-FR\n', 'Destinations=Picker\n': 'Destinations=Editor\n', 'CaptureMousepointer=True\n': 'CaptureMousepointer=False\n', 'OutputFilePath=C:\\Us...n\\Desktop\n': 'OutputFilePath=C:\\Us...reenshots\n', 'UpdateCheckInterval=14\n': 'UpdateCheckInterval=0\n'})
.. versionadded:: 2.5
"""
result_dict = {}
if not encoding and os.path.isfile(conf_path):
encoding = detect_file_encoding(conf_path)
else:
encoding = "UTF-8"
if not os.path.isfile(conf_path):
if not base_file_str:
error("You did not provide a base_file_str, so the file needs to be present")
with open(conf_path, "w", encoding=encoding) as f:
f.write(base_file_str)
new_lines = []
with open(conf_path, "r", encoding=encoding) as f:
for line in f:
line_edited = False
old_line = line
line = line.rsplit("\n", 1)[0]
for key, value in config_dict.items():
if line.startswith(key):
line = value
line_edited = True
break
new_line = line + "\n"
new_lines.append(new_line)
if line_edited:
result_dict.update({old_line: new_line})
with open(conf_path, "w", encoding=encoding) as f:
f.writelines(new_lines)
return result_dict
def detect_file_encoding(file_path):
"""Detect the encoding of a file."""
with open(file_path, "rb") as file:
raw_data = file.read()
result = chardet.detect(raw_data)
return result["encoding"]