From 8a62293a47de958d493b4eb5d188eabf349a1a0b Mon Sep 17 00:00:00 2001 From: Rotzbua Date: Thu, 27 Apr 2023 23:45:06 +0200 Subject: [PATCH] Lint code --- scripts/boxes | 11 ++++++++--- scripts/boxes2inkscape | 44 ++++++++++++++++++++++-------------------- scripts/boxes2pot | 17 +++++++++------- scripts/boxes_proxy.py | 26 +++++++++++++------------ scripts/boxesserver | 8 ++++++-- 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/scripts/boxes b/scripts/boxes index a36a5f5..700d5a7 100755 --- a/scripts/boxes +++ b/scripts/boxes @@ -27,11 +27,12 @@ except ImportError: import boxes.generators -#__version__ = get_distribution('boxes').version + +# __version__ = get_distribution('boxes').version def main(): - #version = 'boxes.py v{}'.format(__version__) + # version = 'boxes.py v{}'.format(__version__) if len(sys.argv) > 1 and sys.argv[1].startswith("--id="): del sys.argv[1] @@ -45,9 +46,11 @@ def main(): name = name[12:] run_generator(name, sys.argv[2:]) + def print_usage(): print(__doc__) + def list_grouped_generators(): print('Available generators:') for group in generator_groups(): @@ -59,12 +62,14 @@ def list_grouped_generators(): for box in group.generators: print(' * {} - {}'.format(box.__name__, box.__doc__ or '')) + def get_translation(): try: return gettext.translation('boxes.py', localedir='locale') except OSError: return gettext.translation('boxes.py', fallback=True) - + + def run_generator(name, args): generators = generators_by_name() lower_name = name.lower() diff --git a/scripts/boxes2inkscape b/scripts/boxes2inkscape index 6ac91b2..badf529 100755 --- a/scripts/boxes2inkscape +++ b/scripts/boxes2inkscape @@ -25,9 +25,10 @@ except ImportError: sys.path.append(os.path.dirname(__file__) + "/..") import boxes.generators + class Boxes2INX: def __init__(self) -> None: - self.boxes = {b.__name__ : b() for b in boxes.generators.getAllBoxGenerators().values() if b.webinterface} + self.boxes = {b.__name__: b() for b in boxes.generators.getAllBoxGenerators().values() if b.webinterface} self.groups = boxes.generators.ui_groups self.groups_by_name = boxes.generators.ui_groups_by_name @@ -37,44 +38,44 @@ class Boxes2INX: def arg2inx(self, a, prefix): name = a.option_strings[0].replace("-", "") - + if isinstance(a, argparse._HelpAction): return "" - + viewname = name if prefix and name.startswith(prefix + '_'): - viewname = name[len(prefix)+1:] + viewname = name[len(prefix) + 1:] if (isinstance(a, argparse._StoreAction) and hasattr(a.type, "inx")): - return a.type.inx(name, viewname, a) #see boxes.__init__.py + return a.type.inx(name, viewname, a) # see boxes.__init__.py elif a.dest == "layout": return "" val = a.default.split("\n") input = """""" % \ - (name, max(len(l) for l in val)+10, len(val)+1, a.default) + (name, max(len(l) for l in val) + 10, len(val) + 1, a.default) elif a.choices: uniqueChoices = [] for e in a.choices: if e not in uniqueChoices: uniqueChoices.append(e) return (''' \n''' - % (name, viewname, quoteattr(a.help or viewname)) + - "".join(' \n' % (e, e) for e in uniqueChoices) + ' \n') + % (name, viewname, quoteattr(a.help or viewname)) + + "".join(' \n' % (e, e) for e in uniqueChoices) + ' \n') else: default = a.default if isinstance(a.type, boxes.BoolArg): t = '"bool"' default = str(a.default).lower() - + elif a.type is boxes.argparseSections: t = '"string"' - + else: - t = { int : '"int"', - float : '"float" precision="2"', - str : '"string"', - }.get(a.type, '"string"') - + t = {int: '"int"', + float: '"float" precision="2"', + str: '"string"', + }.get(a.type, '"string"') + if t == '"int"' or t == '"float" precision="2"': return ''' %s\n''' % (name, t, viewname, quoteattr(a.help or viewname), default) @@ -82,7 +83,7 @@ class Boxes2INX: return ''' %s\n''' % (name, t, viewname, quoteattr(a.help or viewname), default) def generator2inx(self, name, box): - result = [ """ + result = [""" %s info.festi.boxes.py.%s @@ -97,12 +98,12 @@ class Boxes2INX: if title.startswith("Settings for "): title = title[len("Settings for "):] if title.endswith(" Settings"): - title = title[:-len(" Settings")] - + title = title[:-len(" Settings")] + pageParams = [] for a in group._group_actions: if a.dest in ("input", "output", "format"): - continue + continue if self.arg2inx(a, prefix) != "": pageParams.append(self.arg2inx(a, prefix)) if len(pageParams) > 0: @@ -111,7 +112,7 @@ class Boxes2INX: """ % (groupid, title)) result.extend(pageParams) result.append(" ") - + groupid += 1 result.append(""" @@ -145,7 +146,8 @@ class Boxes2INX: continue self.writeINX(name, box, path) -if __name__=="__main__": + +if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: boxes2inkscape TARGETPATH") b = Boxes2INX() diff --git a/scripts/boxes2pot b/scripts/boxes2pot index 70f2a66..35e3a92 100755 --- a/scripts/boxes2pot +++ b/scripts/boxes2pot @@ -27,13 +27,14 @@ except ImportError: import boxes.generators from boxes import edges, lids + class DefaultParams(boxes.Boxes): def __init__(self) -> None: boxes.Boxes.__init__(self) self.buildArgParser("x", "y", "h", "hi", "sx", "sy", "sh", "bottom_edge", "top_edge", "outside", "nema_mount") - self.addSettingsArgs(edges.FingerJointSettings, finger=1.0,space=1.0) + self.addSettingsArgs(edges.FingerJointSettings, finger=1.0, space=1.0) self.addSettingsArgs(edges.StackableSettings) self.addSettingsArgs(edges.HingeSettings) self.addSettingsArgs(edges.SlideOnLidSettings) @@ -41,11 +42,12 @@ class DefaultParams(boxes.Boxes): self.addSettingsArgs(edges.FlexSettings) self.addSettingsArgs(lids.LidSettings) + class Boxes2pot: def __init__(self) -> None: self.messages: list[Any] = [] self.message_set: set[Any] = set() - self.boxes = {b.__name__ : b() for b in boxes.generators.getAllBoxGenerators().values() if b.webinterface} + self.boxes = {b.__name__: b() for b in boxes.generators.getAllBoxGenerators().values() if b.webinterface} self.groups = boxes.generators.ui_groups self.groups_by_name = boxes.generators.ui_groups_by_name self._readEdgeDescriptions() @@ -79,7 +81,7 @@ class Boxes2pot: prefix = getattr(group, "prefix", "") name = a.option_strings[0].replace("-", "") if prefix and name.startswith(prefix + '_'): - name = name[len(prefix)+1:] + name = name[len(prefix) + 1:] self.add(name, "parameter name for " + prefix, location) else: self.add(name, "parameter name", location) @@ -103,7 +105,7 @@ class Boxes2pot: if box.__doc__: self.add(box.__doc__, "description of " + name, location) if box.description: - self.add(box.description, "long description of "+ name + " in markdown", location) + self.add(box.description, "long description of " + name + " in markdown", location) self.addBoxParams(name, box, location) def writePOT(self, fn): @@ -120,7 +122,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" """) - + for msg, comment, reference in self.messages: f.write("\n") if comment: @@ -128,14 +130,15 @@ msgstr "" if reference: f.write("#: %s\n" % reference) msg = msg.split("\n") - for i in range(len(msg)-1): + for i in range(len(msg) - 1): msg[i] += "\\n" f.write('msgid ') for m in msg: f.write(' "%s"\n' % m.replace('"', '\\"')) f.write('msgstr ""\n') -if __name__=="__main__": + +if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: boxes2pot TARGETPATH") b = Boxes2pot() diff --git a/scripts/boxes_proxy.py b/scripts/boxes_proxy.py index dc05413..695d11d 100644 --- a/scripts/boxes_proxy.py +++ b/scripts/boxes_proxy.py @@ -19,39 +19,40 @@ from lxml import etree import tempfile from shlex import quote + class boxesPyWrapper(inkex.GenerateExtension): def add_arguments(self, pars): - args = sys.argv[1:] + args = sys.argv[1:] for arg in args: - key=arg.split("=")[0] + key = arg.split("=")[0] if key == "--id": continue if len(arg.split("=")) == 2: - value=arg.split("=")[1] + value = arg.split("=")[1] pars.add_argument(key, default=key) def generate(self): f, box_file = tempfile.mkstemp(".svg", "boxes.py-inkscape") - cmd = "boxes" #boxes.exe in this local dir (or if present in %PATH%), or boxes from $PATH in linux + cmd = "boxes" # boxes.exe in this local dir (or if present in %PATH%), or boxes from $PATH in linux for arg in vars(self.options): if arg in ( "output", "id", "ids", "selected_nodes", "input_file", "tab"): continue - #fix behaviour of "original" arg which does not correctly gets + # fix behaviour of "original" arg which does not correctly gets # interpreted if set to false if arg == "original" and str(getattr(self.options, arg)) == "false": continue cmd += f" --{arg} {quote(str(getattr(self.options, arg)))}" - cmd += f" --output {box_file} {box_file}" #we need to add box_file string twice in a row. Otherwise program executable throws an error + cmd += f" --output {box_file} {box_file}" # we need to add box_file string twice in a row. Otherwise program executable throws an error cmd = cmd.replace("boxes --generator", "boxes") - + # run boxes with the parameters provided with os.popen(cmd, "r") as boxes: result = boxes.read() - + # check output existence try: stream = open(box_file, 'r') @@ -59,18 +60,19 @@ class boxesPyWrapper(inkex.GenerateExtension): inkex.utils.debug("There was no " + box_file + " output generated. Cannot continue. Command was:") inkex.utils.debug(str(cmd)) exit(1) - + # write the generated SVG into Inkscape's canvas p = etree.XMLParser(huge_tree=True) doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True)) stream.close() if os.path.exists(box_file): - os.remove(box_file) #remove previously generated box file at the end too - + os.remove(box_file) # remove previously generated box file at the end too + group = inkex.Group(id="boxes.py") for element in doc.getroot(): group.append(element) return group - + + if __name__ == '__main__': boxesPyWrapper().run() diff --git a/scripts/boxesserver b/scripts/boxesserver index 3acca17..e5368bf 100755 --- a/scripts/boxesserver +++ b/scripts/boxesserver @@ -81,6 +81,7 @@ class FileChecker(threading.Thread): def stop(self) -> None: self._stopped = True + def filter_url(url, non_default_args): if len(url) == 0: return '' @@ -102,6 +103,7 @@ def filter_url(url, non_default_args): else: return f"{base}" + class ArgumentParserError(Exception): pass @@ -575,7 +577,7 @@ class BServer: """ - ) + ) self._cache[("Gallery", lang_name)] = [s.encode("utf-8") for s in result] return self._cache[("Gallery", lang_name)] @@ -654,7 +656,7 @@ class BServer: if render == "3": http_headers = [('Content-type', 'image/png')] http_headers.append(('X-Robots-Tag', 'noindex,nofollow')) - qr_format="png" + qr_format = "png" fn = (box.__class__.__name__) start_response(status, http_headers) qrcode = get_qrcode(box.metadata["url_short"], qr_format) @@ -671,6 +673,7 @@ class BServer: os.remove(box.output) return (l for l in result) + def get_qrcode(url, format): if url is None: url = "no url" @@ -679,6 +682,7 @@ def get_qrcode(url, format): img.save(image_bytes, format=format) return image_bytes.getvalue() + if __name__ == "__main__": parser = argparse.ArgumentParser()