mightyscape-1.2/extensions/fablabchemnitz/boxes.py/boxes/scripts/boxes2pot

151 lines
5.7 KiB
Plaintext
Raw Normal View History

2022-11-30 15:57:58 +01:00
#!/usr/bin/env python3
# Copyright (C) 2019 Florian Festi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2024-03-31 18:51:28 +02:00
from __future__ import annotations
2022-11-30 15:57:58 +01:00
import argparse
2024-03-31 18:51:28 +02:00
import os.path
import sys
from typing import Any
2022-11-30 15:57:58 +01:00
try:
import boxes.generators
except ImportError:
sys.path.append(os.path.dirname(__file__) + "/..")
import boxes.generators
2024-03-31 18:51:28 +02:00
from boxes import edges, lids
2022-11-30 15:57:58 +01:00
class DefaultParams(boxes.Boxes):
2024-03-31 18:51:28 +02:00
def __init__(self) -> None:
2022-11-30 15:57:58 +01:00
boxes.Boxes.__init__(self)
self.buildArgParser("x", "y", "h", "hi", "sx", "sy", "sh",
"bottom_edge", "top_edge", "outside", "nema_mount")
2024-03-31 18:51:28 +02:00
self.addSettingsArgs(edges.FingerJointSettings, finger=1.0, space=1.0)
2022-11-30 15:57:58 +01:00
self.addSettingsArgs(edges.StackableSettings)
self.addSettingsArgs(edges.HingeSettings)
2024-03-31 18:51:28 +02:00
self.addSettingsArgs(edges.SlideOnLidSettings)
2022-11-30 15:57:58 +01:00
self.addSettingsArgs(edges.ClickSettings)
self.addSettingsArgs(edges.FlexSettings)
2024-03-31 18:51:28 +02:00
self.addSettingsArgs(lids.LidSettings)
2022-11-30 15:57:58 +01:00
class Boxes2pot:
2024-03-31 18:51:28 +02:00
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}
2022-11-30 15:57:58 +01:00
self.groups = boxes.generators.ui_groups
self.groups_by_name = boxes.generators.ui_groups_by_name
self._readEdgeDescriptions()
def add(self, msg, comment=None, reference=None):
if not msg or msg in self.message_set:
return
self.message_set.add(msg)
self.messages.append((msg, comment, reference))
def filename_from_module(self, modulename):
modulenames = modulename.split(".")
return "/".join(modulenames) + ".py"
def _readEdgeDescriptions(self):
for char, descr in edges.getDescriptions().items():
self.add(descr, f"{char} edge description", "boxes/edges.py")
2024-03-31 18:51:28 +02:00
self.add(f"{char} {descr}", f"{char} edge description", "boxes/edges.py")
2022-11-30 15:57:58 +01:00
def addBoxParams(self, name, box, location=None):
for group in box.argparser._action_groups:
if not group._group_actions:
continue
self.add(group.title)
for a in group._group_actions:
if a.dest in ("input", "output"):
continue
if isinstance(a, argparse._HelpAction):
continue
prefix = getattr(group, "prefix", "")
name = a.option_strings[0].replace("-", "")
if prefix and name.startswith(prefix + '_'):
2024-03-31 18:51:28 +02:00
name = name[len(prefix) + 1:]
2022-11-30 15:57:58 +01:00
self.add(name, "parameter name for " + prefix, location)
else:
self.add(name, "parameter name", location)
if a.help:
self.add(a.help, "help for parameter " + name, location)
for c in a.choices or []:
if isinstance(c, (float, int)):
continue
self.add(c, "possible choice for " + name, location)
def readBoxes(self):
for group in self.groups:
location = "boxes/generators/__init__.py"
self.add(group.name, "name of generator group", location)
self.add(group.title, "title of group " + group.name, location)
self.add(group.description, "description of group " + group.name, location)
self.addBoxParams(None, DefaultParams())
for name, box in self.boxes.items():
location = self.filename_from_module(box.__module__)
self.add(name, "name of box generator", location)
if box.__doc__:
self.add(box.__doc__, "description of " + name, location)
if box.description:
2024-03-31 18:51:28 +02:00
self.add(box.description, "long description of " + name + " in markdown", location)
2022-11-30 15:57:58 +01:00
self.addBoxParams(name, box, location)
def writePOT(self, fn):
with open(fn, encoding="utf-8", mode="w") as f:
f.write(r"""msgid ""
msgstr ""
"Project-Id-Version: boxes.py VERSION\n"
"PO-Revision-Date: 2019-04-20 14:53+0200\n"
"Last-Translator: Florian Festi <florian@festi.info>\n"
"Language-Team: English\n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
""")
2024-03-31 18:51:28 +02:00
2022-11-30 15:57:58 +01:00
for msg, comment, reference in self.messages:
f.write("\n")
if comment:
f.write("#. %s\n" % comment)
if reference:
f.write("#: %s\n" % reference)
msg = msg.split("\n")
2024-03-31 18:51:28 +02:00
for i in range(len(msg) - 1):
2022-11-30 15:57:58 +01:00
msg[i] += "\\n"
f.write('msgid ')
for m in msg:
f.write(' "%s"\n' % m.replace('"', '\\"'))
f.write('msgstr ""\n')
2024-03-31 18:51:28 +02:00
def main() -> None:
2022-11-30 15:57:58 +01:00
if len(sys.argv) != 2:
2024-03-31 18:51:28 +02:00
print("Usage: boxes2pot TARGETPATH")
return
2022-11-30 15:57:58 +01:00
b = Boxes2pot()
b.readBoxes()
b.writePOT(sys.argv[1])
2024-03-31 18:51:28 +02:00
if __name__ == "__main__":
main()