printrun-src/printrun/utils.py

Fri, 03 Jun 2016 10:05:45 +0200

author
mbayer
date
Fri, 03 Jun 2016 10:05:45 +0200
changeset 17
d93a721621bd
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

remove pyc files

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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 # Set up Internationalization using gettext
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 # searching for installed locales on /usr/share; uses relative folder if not
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 # found (windows)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 def install_locale(domain):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 if os.path.exists('/usr/share/pronterface/locale'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 gettext.install(domain, '/usr/share/pronterface/locale', unicode = 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 elif os.path.exists('/usr/local/share/pronterface/locale'):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 gettext.install(domain, '/usr/local/share/pronterface/locale',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 unicode = 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 gettext.install(domain, './locale', unicode = 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 class LogFormatter(logging.Formatter):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 def __init__(self, format_default, format_info):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 super(LogFormatter, self).__init__(format_info)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 self.format_default = format_default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 self.format_info = format_info
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 def format(self, record):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 if record.levelno == logging.INFO:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 self._fmt = self.format_info
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 self._fmt = self.format_default
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 return super(LogFormatter, self).format(record)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 def setup_logging(out, filepath = None, reset_handlers = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 logger = logging.getLogger()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 logger.setLevel(logging.INFO)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 if reset_handlers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 logger.handlers = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 formatter = LogFormatter("[%(levelname)s] %(message)s", "%(message)s")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 logging_handler = logging.StreamHandler(out)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 logging_handler.setFormatter(formatter)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 logger.addHandler(logging_handler)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 if filepath:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 if os.path.isdir(filepath):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 filepath = os.path.join(filepath, "printrun.log")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 formatter = LogFormatter("%(asctime)s - [%(levelname)s] %(message)s", "%(asctime)s - %(message)s")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 logging_handler = logging.FileHandler(filepath)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 logging_handler.setFormatter(formatter)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 logger.addHandler(logging_handler)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 def iconfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 if hasattr(sys, "frozen") and sys.frozen == "windows_exe":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 return sys.executable
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 return pixmapfile(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 def imagefile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 for prefix in ['/usr/local/share/pronterface/images',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 '/usr/share/pronterface/images']:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 candidate = os.path.join(prefix, filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 if os.path.exists(candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 return candidate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 local_candidate = os.path.join(os.path.dirname(sys.argv[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 "images", filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 if os.path.exists(local_candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 return local_candidate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 return os.path.join("images", filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 def lookup_file(filename, prefixes):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 local_candidate = os.path.join(os.path.dirname(sys.argv[0]), 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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 for prefix in prefixes:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 candidate = os.path.join(prefix, filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 if os.path.exists(candidate):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 return candidate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 return filename
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 def pixmapfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 return lookup_file(filename, ['/usr/local/share/pixmaps',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 '/usr/share/pixmaps'])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 def sharedfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 return lookup_file(filename, ['/usr/local/share/pronterface',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 '/usr/share/pronterface'])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 def configfile(filename):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 return lookup_file(filename, [os.path.expanduser("~/.printrun/"), ])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 def decode_utf8(s):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 s = s.decode("utf-8")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 except:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 return s
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 def format_time(timestamp):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 return datetime.datetime.fromtimestamp(timestamp).strftime("%H:%M:%S")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 def format_duration(delta):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 return str(datetime.timedelta(seconds = int(delta)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 def prepare_command(command, replaces = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 command = shlex.split(command.replace("\\", "\\\\").encode())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 if replaces:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 replaces["$python"] = sys.executable
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 for pattern, rep in replaces.items():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 command = [bit.replace(pattern, rep) for bit in command]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 command = [bit.encode() for bit in command]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 return command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 def run_command(command, replaces = None, stdout = subprocess.STDOUT, stderr = subprocess.STDOUT, blocking = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 command = prepare_command(command, replaces)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 if blocking:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 return subprocess.call(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 return subprocess.Popen(command, stderr = stderr, stdout = stdout)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 def get_command_output(command, replaces):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 p = run_command(command, replaces,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 stdout = subprocess.PIPE, stderr = subprocess.STDOUT,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 blocking = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 return p.stdout.read()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 def dosify(name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 return os.path.split(name)[1].split(".")[0][:8] + ".g"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 class RemainingTimeEstimator(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 drift = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 def __init__(self, gcode):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 self.drift = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.previous_layers_estimate = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 self.current_layer_estimate = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 self.current_layer_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 self.gcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 self.remaining_layers_estimate = sum(layer.duration for layer in gcode.all_layers)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 if len(gcode) > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 self.update_layer(0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 def update_layer(self, layer, printtime):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 self.previous_layers_estimate += self.current_layer_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 if self.previous_layers_estimate > 1. and printtime > 1.:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 self.drift = printtime / self.previous_layers_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 self.current_layer_estimate = self.gcode.all_layers[layer].duration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 self.current_layer_lines = len(self.gcode.all_layers[layer])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 self.remaining_layers_estimate -= self.current_layer_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 self.last_idx = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 self.last_estimate = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 def __call__(self, idx, printtime):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 if not self.current_layer_lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 return (0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 if idx == self.last_idx:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 return self.last_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 layer, line = self.gcode.idxs(idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 layer_progress = (1 - (float(line + 1) / self.current_layer_lines))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 remaining = layer_progress * self.current_layer_estimate + self.remaining_layers_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 estimate = self.drift * remaining
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 total = estimate + printtime
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 self.last_idx = idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 self.last_estimate = (estimate, total)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 return self.last_estimate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 def parse_build_dimensions(bdim):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 # a string containing up to six numbers delimited by almost anything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 # first 0-3 numbers specify the build volume, no sign, always positive
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 # remaining 0-3 numbers specify the coordinates of the "southwest" corner of the build platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 # "XXX,YYY"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 # "XXXxYYY+xxx-yyy"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 # "XXX,YYY,ZZZ+xxx+yyy-zzz"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 # etc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 bdl = re.findall("([-+]?[0-9]*\.?[0-9]*)", bdim)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 defaults = [200, 200, 100, 0, 0, 0, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 bdl = filter(None, bdl)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 bdl_float = [float(value) if value else defaults[i] for i, value in enumerate(bdl)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 if len(bdl_float) < len(defaults):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 bdl_float += [defaults[i] for i in range(len(bdl_float), len(defaults))]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 for i in range(3): # Check for nonpositive dimensions for build volume
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 if bdl_float[i] <= 0: bdl_float[i] = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 return bdl_float
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 def get_home_pos(build_dimensions):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 return build_dimensions[6:9] if len(build_dimensions) >= 9 else None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 def hexcolor_to_float(color, components):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 color = color[1:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 numel = len(color)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 ndigits = numel / components
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 div = 16 ** ndigits - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 return tuple(round(float(int(color[i:i + ndigits], 16)) / div, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 for i in range(0, numel, ndigits))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 def check_rgb_color(color):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 if len(color[1:]) % 3 != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 ex = ValueError(_("Color must be specified as #RGB"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 ex.from_validator = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 raise ex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 def check_rgba_color(color):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 if len(color[1:]) % 4 != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 ex = ValueError(_("Color must be specified as #RGBA"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 ex.from_validator = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 raise ex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 tempreport_exp = re.compile("([TB]\d*):([-+]?\d*\.?\d*)(?: ?\/)?([-+]?\d*\.?\d*)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 def parse_temperature_report(report):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 matches = tempreport_exp.findall(report)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 return dict((m[0], (m[1], m[2])) for m in matches)

mercurial