config: allow xdg .config folder fully deprecate old klipper_config and home folder config

config order of preference

1. printer_data/config
2. xdg .config/KlipperScreen
3. klipperscreen repo directory
This commit is contained in:
alfrix 2024-01-26 13:48:42 -03:00
parent 160e2ecd31
commit 9d0e4b841f
4 changed files with 35 additions and 36 deletions

View File

@ -2,6 +2,10 @@
Breaking changes will be listed here.
#### [2024_01_26]()
* Deprecated old ~/klipper_config folder users need to place the configfile
in ~/printer_data/config, ~/.config/KlipperScreen, or directly in the repo
#### [2023 10 08](https://github.com/KlipperScreen/KlipperScreen/commit/b6199a4f24beb02dc0f8956f60c88e3ba3468927)
* Deprecated [bed_screws] and [z_calibrate_position] sections those configs should go in [printer name]

View File

@ -235,10 +235,9 @@ enable: {{ 'MY_MACRO' in printer.gcode_macros.list }}
KlipperScreen will search for a configuration file in the following order:
1. _~/KlipperScreen.conf_
2. _${KlipperScreen_Directory}/KlipperScreen.conf_
3. _~/printer_data/config/KlipperScreen.conf_
4. _~/klipper_config/KlipperScreen.conf_
1. _~/printer_data/config/KlipperScreen.conf_
2. _~/.config/KlipperScreen/KlipperScreen.conf_
3. _${KlipperScreen_Directory}/KlipperScreen.conf_
If you need a custom location for the configuration file, you can add -c or --configfile to the systemd file and specify
the location of your configuration file.

View File

@ -22,6 +22,9 @@ SCREEN_BLANKING_OPTIONS = [
]
klipperscreendir = pathlib.Path(__file__).parent.resolve().parent
home = os.path.expanduser("~/")
klipper_config = os.path.join(home, "printer_data", "config")
xdg_config = os.path.join(home, ".config", "KlipperScreen")
class ConfigError(Exception):
@ -389,35 +392,27 @@ class KlipperScreenConfig:
saved_def.append(line[(len(self.do_not_edit_prefix) + 1):])
return ["\n".join(user_def), None if saved_def is None else "\n".join(saved_def)]
@staticmethod
def check_path_exists(base_dir, filename):
for name in (filename, filename.lower()):
full_path = os.path.join(base_dir, name)
if os.path.exists(full_path):
return full_path
return None
def get_config_file_location(self, file):
# Passed config (-c) by default is ~/KlipperScreen.conf
# Passed config (-c) by default is blank
logging.info(f"Passed config (-c): {file}")
if os.path.exists(file):
if file not in (".", "..") and os.path.exists(file):
return file
file = os.path.join(klipperscreendir, self.configfile_name)
if os.path.exists(file):
return file
file = os.path.join(klipperscreendir, self.configfile_name.lower())
if os.path.exists(file):
return file
# List of directories to search for the config file
directories = [klipper_config, xdg_config, klipperscreendir]
klipper_config = os.path.join(os.path.expanduser("~/"), "printer_data", "config")
file = os.path.join(klipper_config, self.configfile_name)
if os.path.exists(file):
return file
file = os.path.join(klipper_config, self.configfile_name.lower())
if os.path.exists(file):
return file
# OLD config folder
klipper_config = os.path.join(os.path.expanduser("~/"), "klipper_config")
file = os.path.join(klipper_config, self.configfile_name)
if os.path.exists(file):
return file
file = os.path.join(klipper_config, self.configfile_name.lower())
if os.path.exists(file):
return file
for directory in directories:
path = self.check_path_exists(directory, self.configfile_name)
if path:
return path
# fallback
return self.default_config_path
@ -517,17 +512,17 @@ class KlipperScreenConfig:
if self.config_path != self.default_config_path:
filepath = self.config_path
else:
filepath = os.path.expanduser("~/")
klipper_config = os.path.join(filepath, "printer_data", "config")
old_klipper_config = os.path.join(filepath, "klipper_config")
if os.path.exists(klipper_config):
filepath = os.path.join(klipper_config, self.configfile_name)
elif os.path.exists(old_klipper_config):
filepath = os.path.join(old_klipper_config, self.configfile_name)
else:
filepath = os.path.join(filepath, self.configfile_name)
try:
if not os.path.exists(xdg_config):
pathlib.Path(xdg_config).mkdir(parents=True, exist_ok=True)
filepath = os.path.join(xdg_config, self.configfile_name)
except Exception as e:
logging.error(e)
filepath = klipperscreendir
logging.info(f'Creating a new config file in {filepath}')
try:
with open(filepath, 'w') as file:
file.write(contents)

View File

@ -1097,7 +1097,8 @@ def main():
homedir = os.path.expanduser("~")
parser.add_argument(
"-c", "--configfile", default=os.path.join(homedir, "KlipperScreen.conf"), metavar='<configfile>',
"-c", "--configfile",
default="", metavar='<configfile>',
help="Location of KlipperScreen configuration file"
)
logdir = os.path.join(homedir, "printer_data", "logs")