printrun-src/printrun/utils.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rw-r--r--

updated and added new files for printrun

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 import os
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import gettext
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import datetime
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import subprocess
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import shlex
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
23 import locale
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
26 DATADIR = os.path.join(sys.prefix, 'share')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
27
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
28
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
29 def set_utf8_locale():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
30 """Make sure we read/write all text files in UTF-8"""
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
31 lang, encoding = locale.getlocale()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
32 if encoding != 'UTF-8':
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
33 locale.setlocale(locale.LC_CTYPE, (lang, 'UTF-8'))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
34
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 # Set up Internationalization using gettext
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 # searching for installed locales on /usr/share; uses relative folder if not
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 # found (windows)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 def install_locale(domain):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
39 shared_locale_dir = os.path.join(DATADIR, 'locale')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
40 if os.path.exists(shared_locale_dir):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
41 gettext.install(domain, shared_locale_dir)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 else:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
43 gettext.install(domain, './locale')
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 class LogFormatter(logging.Formatter):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 def __init__(self, format_default, format_info):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 super(LogFormatter, self).__init__(format_info)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 self.format_default = format_default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 self.format_info = format_info
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 def format(self, record):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 if record.levelno == logging.INFO:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 self._fmt = self.format_info
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self._fmt = self.format_default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 return super(LogFormatter, self).format(record)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 def setup_logging(out, filepath = None, reset_handlers = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 logger = logging.getLogger()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 logger.setLevel(logging.INFO)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 if reset_handlers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 logger.handlers = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 logging_handler = logging.StreamHandler(out)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 logging_handler.setFormatter(formatter)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 logger.addHandler(logging_handler)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 if filepath:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 if os.path.isdir(filepath):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 filepath = os.path.join(filepath, "printrun.log")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 formatter = LogFormatter("%(asctime)s - [%(levelname)s] %(message)s", "%(asctime)s - %(message)s")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 logging_handler = logging.FileHandler(filepath)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 logging_handler.setFormatter(formatter)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 logger.addHandler(logging_handler)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 def iconfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 if hasattr(sys, "frozen") and sys.frozen == "windows_exe":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 return sys.executable
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 return pixmapfile(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 def imagefile(filename):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
82 shared_pronterface_images_dir = os.path.join(DATADIR, 'pronterface/images')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
83 candidate = os.path.join(shared_pronterface_images_dir, filename)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
84 if os.path.exists(candidate):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
85 return candidate
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 local_candidate = os.path.join(os.path.dirname(sys.argv[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 "images", filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 if os.path.exists(local_candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return local_candidate
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
90 frozen_candidate=os.path.join(getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))),"images",filename)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
91 if os.path.exists(frozen_candidate):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
92 return frozen_candidate
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 return os.path.join("images", filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 def lookup_file(filename, prefixes):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 local_candidate = os.path.join(os.path.dirname(sys.argv[0]), filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 if os.path.exists(local_candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 return local_candidate
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
100 if getattr(sys,"frozen",False): prefixes+=[getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))),]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 for prefix in prefixes:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 candidate = os.path.join(prefix, filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 if os.path.exists(candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 return candidate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 return filename
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 def pixmapfile(filename):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
108 shared_pixmaps_dir = os.path.join(DATADIR, 'pixmaps')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
109 return lookup_file(filename, [shared_pixmaps_dir])
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 def sharedfile(filename):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
112 shared_pronterface_dir = os.path.join(DATADIR, 'pronterface')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
113 return lookup_file(filename, [shared_pronterface_dir])
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 def configfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 return lookup_file(filename, [os.path.expanduser("~/.printrun/"), ])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 def decode_utf8(s):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 s = s.decode("utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 return s
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 def format_time(timestamp):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 return datetime.datetime.fromtimestamp(timestamp).strftime("%H:%M:%S")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 def format_duration(delta):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 return str(datetime.timedelta(seconds = int(delta)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 def prepare_command(command, replaces = None):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
132 command = shlex.split(command.replace("\\", "\\\\"))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 if replaces:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 replaces["$python"] = sys.executable
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 for pattern, rep in replaces.items():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 command = [bit.replace(pattern, rep) for bit in command]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 return command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
139 def run_command(command, replaces = None, stdout = subprocess.STDOUT, stderr = subprocess.STDOUT, blocking = False, universal_newlines = False):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 command = prepare_command(command, replaces)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 if blocking:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
142 return subprocess.call(command, universal_newlines = universal_newlines)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 else:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
144 return subprocess.Popen(command, stderr = stderr, stdout = stdout, universal_newlines = universal_newlines)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 def get_command_output(command, replaces):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 p = run_command(command, replaces,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 stdout = subprocess.PIPE, stderr = subprocess.STDOUT,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
149 blocking = False, universal_newlines = True)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 return p.stdout.read()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 def dosify(name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 return os.path.split(name)[1].split(".")[0][:8] + ".g"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
155 class RemainingTimeEstimator:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 drift = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 def __init__(self, gcode):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 self.drift = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 self.previous_layers_estimate = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 self.current_layer_estimate = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 self.current_layer_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 self.gcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 self.remaining_layers_estimate = sum(layer.duration for layer in gcode.all_layers)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 if len(gcode) > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 self.update_layer(0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 def update_layer(self, layer, printtime):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 self.previous_layers_estimate += self.current_layer_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 if self.previous_layers_estimate > 1. and printtime > 1.:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 self.drift = printtime / self.previous_layers_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 self.current_layer_estimate = self.gcode.all_layers[layer].duration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 self.current_layer_lines = len(self.gcode.all_layers[layer])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 self.remaining_layers_estimate -= self.current_layer_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 self.last_idx = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 self.last_estimate = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 def __call__(self, idx, printtime):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 if not self.current_layer_lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 return (0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 if idx == self.last_idx:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 return self.last_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 layer, line = self.gcode.idxs(idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 layer_progress = (1 - (float(line + 1) / self.current_layer_lines))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 remaining = layer_progress * self.current_layer_estimate + self.remaining_layers_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 estimate = self.drift * remaining
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 total = estimate + printtime
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 self.last_idx = idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 self.last_estimate = (estimate, total)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 return self.last_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 def parse_build_dimensions(bdim):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 # a string containing up to six numbers delimited by almost anything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 # first 0-3 numbers specify the build volume, no sign, always positive
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 # remaining 0-3 numbers specify the coordinates of the "southwest" corner of the build platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 # "XXX,YYY"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 # "XXXxYYY+xxx-yyy"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 # "XXX,YYY,ZZZ+xxx+yyy-zzz"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 # etc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 bdl = re.findall("([-+]?[0-9]*\.?[0-9]*)", bdim)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 defaults = [200, 200, 100, 0, 0, 0, 0, 0, 0]
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
204 bdl = [b for b in bdl if b]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 bdl_float = [float(value) if value else defaults[i] for i, value in enumerate(bdl)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 if len(bdl_float) < len(defaults):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 bdl_float += [defaults[i] for i in range(len(bdl_float), len(defaults))]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 for i in range(3): # Check for nonpositive dimensions for build volume
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 if bdl_float[i] <= 0: bdl_float[i] = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 return bdl_float
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 def get_home_pos(build_dimensions):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 return build_dimensions[6:9] if len(build_dimensions) >= 9 else None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 def hexcolor_to_float(color, components):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 color = color[1:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 numel = len(color)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
218 ndigits = numel // components
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 div = 16 ** ndigits - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 return tuple(round(float(int(color[i:i + ndigits], 16)) / div, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 for i in range(0, numel, ndigits))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 def check_rgb_color(color):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 if len(color[1:]) % 3 != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 ex = ValueError(_("Color must be specified as #RGB"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 ex.from_validator = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 raise ex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 def check_rgba_color(color):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 if len(color[1:]) % 4 != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 ex = ValueError(_("Color must be specified as #RGBA"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 ex.from_validator = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 raise ex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 tempreport_exp = re.compile("([TB]\d*):([-+]?\d*\.?\d*)(?: ?\/)?([-+]?\d*\.?\d*)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 def parse_temperature_report(report):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 matches = tempreport_exp.findall(report)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 return dict((m[0], (m[1], m[2])) for m in matches)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
239
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
240 def compile_file(filename):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
241 with open(filename) as f:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
242 return compile(f.read(), filename, 'exec')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
243
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
244 def read_history_from(filename):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
245 history=[]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
246 if os.path.exists(filename):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
247 _hf=open(filename,encoding="utf-8")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
248 for i in _hf:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
249 history.append(i.rstrip())
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
250 return history
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
251
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
252 def write_history_to(filename, hist):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
253 _hf=open(filename,"w",encoding="utf-8")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
254 for i in hist:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
255 _hf.write(i+"\n")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
256 _hf.close()

mercurial