Skip to content

Commit

Permalink
fix: single creation time
Browse files Browse the repository at this point in the history
remove trailing spaces of metadata cli and cli_short
  • Loading branch information
Rotzbua authored and florianfesti committed Apr 1, 2024
1 parent 17c1490 commit 841e15f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
32 changes: 18 additions & 14 deletions boxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import argparse
import copy
import datetime
import gettext
import math
import random
Expand Down Expand Up @@ -296,14 +297,15 @@ def __init__(self) -> None:
self.translations = gettext.NullTranslations()

self.metadata = {
"name" : self.__class__.__name__,
"short_description" : self.__doc__,
"description" : self.description,
"group" : self.ui_group,
"url" : "",
"url_short" : "",
"cli" : "",
"cli_short" : "",
"name": self.__class__.__name__,
"short_description": self.__doc__,
"description": self.description,
"group": self.ui_group,
"url": "",
"url_short": "",
"cli": "",
"cli_short": "",
"creation_date": datetime.datetime.now(),
}

# Dummy attribute for static analytic tools. Will be overwritten by `argparser` at runtime.
Expand Down Expand Up @@ -530,12 +532,13 @@ def parseArgs(self, args=None):
if args is None:
args = sys.argv[1:]

def cliquote(s):
def cliQuote(s: str) -> str:
s = s.replace('\r', '')
s = s.replace('\n', "\\n")
return quote(s)

self.metadata["cli"] = "boxes " + self.__class__.__name__ + " " + " ".join(cliquote(arg) for arg in args)
self.metadata["cli"] = "boxes " + self.__class__.__name__ + " " + " ".join(cliQuote(arg) for arg in args)
self.metadata["cli"] = self.metadata["cli"].strip()

for key, value in vars(self.argparser.parse_args(args=args)).items():
default = self.argparser.get_default(key)
Expand All @@ -546,15 +549,16 @@ def cliquote(s):
self.edgesettings[setting][key[len(setting)+1:]] = value
continue
setattr(self, key, value)
if (value != default):
if value != default:
self.non_default_args[key] = value

# Change file ending to format if not given explicitly
format = getattr(self, "format", "svg")
fileFormat = getattr(self, "format", "svg")
if getattr(self, 'output', None) == 'box.svg':
self.output = 'box.' + format.split("_")[0]
self.output = 'box.' + fileFormat.split("_")[0]

self.metadata["cli_short"] = "boxes " + self.__class__.__name__ + " " + " ".join(cliquote(arg) for arg in args if (arg.split("=")[0][2:] in self.non_default_args))
self.metadata["cli_short"] = "boxes " + self.__class__.__name__ + " " + " ".join(cliQuote(arg) for arg in args if (arg.split("=")[0][2:] in self.non_default_args))
self.metadata["cli_short"] = self.metadata["cli_short"].strip()

def addPart(self, part, name=None):
"""
Expand Down
35 changes: 13 additions & 22 deletions boxes/drawing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import codecs
import datetime
import io
import math
from typing import Any
Expand Down Expand Up @@ -427,24 +426,24 @@ def _addTag(self, parent, tag, text, first=False):
parent.insert(0, t)
return t

def _add_metadata(self, root):
def _add_metadata(self, root) -> None:
md = self.metadata

title = "{group} - {name}".format(**md)
creation_date: str = md["creation_date"].strftime("%Y-%m-%d %H:%M:%S")

# Add Inkscape style rdf meta data
root.set("xmlns:dc", "http://purl.org/dc/elements/1.1/")
root.set("xmlns:cc", "http://creativecommons.org/ns#")
root.set("xmlns:rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")

title = "{group} - {name}".format(**md)
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
root.set("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#")

m = self._addTag(root, "metadata", '\n', True)
r = ET.SubElement(m, 'rdf:RDF')
w = ET.SubElement(r, 'cc:Work')
w.text = '\n'

self._addTag(w, 'dc:title', title)
self._addTag(w, 'dc:date', date)
self._addTag(w, 'dc:date', creation_date)

if "url" in md and md["url"]:
self._addTag(w, 'dc:source', md["url"])
Expand All @@ -469,19 +468,11 @@ def _add_metadata(self, root):
self._addTag(root, "title", md["name"], True)

# Add XML comment
txt = """
{name} - {short_description}
""".format(**md)
txt = """\n{name} - {short_description}\n""".format(**md)
if md["description"]:
txt += """
{description}
""".format(**md)
txt += """
Created with Boxes.py (https://festi.info/boxes.py)
Creation date: {date}
""".format(date=date, **md)
txt += """\n\n{description}\n\n""".format(**md)
txt += """\nCreated with Boxes.py (https://festi.info/boxes.py)\n"""
txt += f"""Creation date: {creation_date}\n"""

txt += "Command line (remove spaces between dashes): %s\n" % md["cli_short"]

Expand Down Expand Up @@ -616,15 +607,15 @@ class PSSurface(Surface):
('monospaced', True, True) : 'Courier-BoldOblique',
}

def _metadata(self):
def _metadata(self) -> str:
md = self.metadata

desc = ""
desc += "%%Title: Boxes.py - {group} - {name}\n".format(**md)
desc += f'%%CreationDate: {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n'
desc += f'%%CreationDate: {md["creation_date"].strftime("%Y-%m-%d %H:%M:%S")}\n'
desc += f'%%Keywords: boxes.py, laser, laser cutter\n'
desc += f'%%Creator: {md.get("url") or md["cli"]}\n'
desc += "%%CreatedBy: Boxes.py (https://festi.info/boxes.py)\n"
desc += "%%CreatedBy: Boxes.py (https://festi.info/boxes.py)\n"
for line in (md["short_description"] or "").split("\n"):
desc += "%% %s\n" % line
desc += "%\n"
Expand Down

0 comments on commit 841e15f

Please sign in to comment.