-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathallconfig.py
More file actions
162 lines (142 loc) · 5.06 KB
/
allconfig.py
File metadata and controls
162 lines (142 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# AllConfig
#
# Utility for Klipper firmware to allow outputting the entire
# config to a file, as Klipper sees it.
# This can help debug cases where one section overrides another,
# Or with include statements.
#
# Copyright (C) 2025 Christopher Mattar (3dcoded)
# <info3dcoded@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
import textwrap
from pathlib import Path
import configparser
from io import StringIO
import glob
import os
import logging
INDENT = ' '*4
HEADER = """
# This file contains the config that Klipper actually sees.
# Note that this may be different that what is in your printer.cfg
# because there may be multiple sections overriding each other.
#
# Example:
# # In one file
# [extruder]
# step_pin: xxx
# dir_pin: xxx
# en_pin: !xxx
# rotation_distance: 40
# ...
#
# # In another file included into printer.cfg
# [extruder]
# rotation_distance: 32
#
# Klipper only sees what happens last, so if the config was loaded
# in the order shown in the previous example, Klipper would see:
#
# [extruder]
# step_pin: xxx
# dir_pin: xxx
# en_pin: !xxx
# rotation_distance: 32 # <-- NOTE the different rotation_distance
#
# This also takes into account SAVE_CONFIG.
# Example:
#
# #*# <---------------------- SAVE_CONFIG ---------------------->
# #*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
# #*#
# #*# [extruder]
# #*# pid_kp: 21.432
# #*# pid_ki: 1.856
# #*# pid_kd: 61.884
#
# In this example, including the previous [extruder] section,
# Klipper would see:
#
# [extruder]
# step_pin: xxx
# dir_pin: xxx
# en_pin: !xxx
# rotation_distance: 32
# pid_kp: 21.432
# pid_ki: 1.856
# pid_kd: 61.884
"""
class ConfigParser:
def __init__(self, printer):
global config_path
self.printer = printer
self.config_file = printer.start_args['config_file']
self.config_path = Path(os.path.dirname(self.config_file))
config_path = self.config_path
def read_config(self):
section_filenames = self._read_file(self.config_file)
return section_filenames
def _read_file(self, filename, visited=[], section_filenames={}):
path = self.config_path / filename
visited.append(path) # Keep a list of files previously included
with open(path, 'r') as file:
parser = configparser.RawConfigParser(
strict=False, inline_comment_prefixes=(';', '#'))
parser.read_file(file, filename)
for section in parser.sections():
if section in section_filenames:
if filename not in section_filenames[section]:
section_filenames[section].append(filename)
else:
section_filenames[section] = [filename]
file.seek(0)
for line in file.readlines():
mo = configparser.RawConfigParser.SECTCRE.match(line)
header = mo and mo.group('header')
# Handle [include xxx.cfg] sections
if header and header.startswith('include '):
include_spec = header[8:].strip()
include_path = str(path.parent / include_spec)
include_filenames = glob.glob(include_path, recursive=True)
for include_filename in include_filenames:
self._read_file(include_filename, visited, section_filenames)
visited.remove(path)
return section_filenames
class PrinterAllConfig:
def __init__(self, config):
self.printer = config.get_printer()
self.config = config
self.cfg_folder = Path(self.printer.start_args['config_file']).parent
self.output_path = Path(config.get('output', str(self.cfg_folder / 'allconfig.cfg')))
self.cfg_files = []
self.section_sources = {}
self.printer.register_event_handler('klippy:connect', self.handle_connect)
def handle_connect(self):
section_filenames = ConfigParser(self.printer).read_config()
cfg_root = str(self.cfg_folder) + '/'
for section, files in section_filenames.items():
section_filenames[section] = [
f[len(cfg_root):] if f.startswith(cfg_root) else f
for f in files
]
allconfig = ''+HEADER
for section in self.config.fileconfig.sections():
allconfig += f'[{section}]'
if section in section_filenames:
sources = section_filenames[section]
source_str = ', '.join(sources)
allconfig += f' # {source_str}'
allconfig += '\n'
for option, value in self.config.fileconfig.items(section):
if len(value.splitlines()) == 1:
allconfig += f'{option}: {value}\n'
else:
updated_value = textwrap.indent(value.strip(), INDENT)
allconfig += f'{option}:\n{updated_value}\n'
allconfig += '\n'
with open(self.output_path, 'w+') as file:
file.write(allconfig)
def load_config(config):
return PrinterAllConfig(config)