Lint code

This commit is contained in:
Rotzbua 2023-04-27 23:45:06 +02:00 committed by Florian Festi
parent 86a8bf481d
commit 8a62293a47
5 changed files with 61 additions and 45 deletions

View File

@ -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()

View File

@ -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 = """<textarea name="%s" cols="%s" rows="%s">%s</textarea>""" % \
(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 (''' <param name="%s" type="optiongroup" appearance="combo" gui-text="%s" gui-description=%s>\n'''
% (name, viewname, quoteattr(a.help or viewname)) +
"".join(' <option value="%s">%s</option>\n' % (e, e) for e in uniqueChoices) + ' </param>\n')
% (name, viewname, quoteattr(a.help or viewname)) +
"".join(' <option value="%s">%s</option>\n' % (e, e) for e in uniqueChoices) + ' </param>\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 ''' <param name="%s" type=%s max="9999" gui-text="%s" gui-description=%s>%s</param>\n''' % (name, t, viewname, quoteattr(a.help or viewname), default)
@ -82,7 +83,7 @@ class Boxes2INX:
return ''' <param name="%s" type=%s gui-text="%s" gui-description=%s>%s</param>\n''' % (name, t, viewname, quoteattr(a.help or viewname), default)
def generator2inx(self, name, box):
result = [ """<?xml version="1.0" encoding="UTF-8"?>
result = ["""<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
<name>%s</name>
<id>info.festi.boxes.py.%s</id>
@ -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(" </page>")
groupid += 1
result.append("""
<page name="tab_%s" gui-text="Example">
@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -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:
</body>
</html>
"""
)
)
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()