Skip to content

Commit 10cd412

Browse files
committed
Add template {{random[:len]}} with optional length specifier
Signed-off-by: DL6ER <[email protected]>
1 parent 58a0e52 commit 10cd412

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ Additional printer support comes from [matmair/brother_ql-inventree](https://git
9696
- Dockerized
9797
- Devcontainer for ease of development/contributing
9898

99+
### Supported templates
100+
101+
- `{{counter}}` — Inserts the current counter value (automatically increments when printing multiple labels at the same time).
102+
- `{{datetime:<format>}}` — Inserts the current date and time, e.g. `%H:%M:%S %d.%m.%Y` (see [strftime](https://strftime.org/)).
103+
- `{{uuid}}` — Inserts a random UUID (Universally Unique Identifier).
104+
- `{{short-uuid}}` — Inserts a shortened version of a UUID.
105+
- `{{env:var}}` — Inserts the value of the environment variable `var`.
106+
- `{{random[:<len>]}}` — Inserts a random string of optional length `len` (defaulting to 64).
107+
99108
## Docker Compose
100109

101110
You may also use the example [`docker-compose.yml`](./docker-compose.yml) file provided in this repository to quickly get started with Docker Compose:

app/labeldesigner/label.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from barcode.writer import ImageWriter
99
import datetime
1010
import re
11+
import random
12+
import string
1113

1214
logger = logging.getLogger(__name__)
1315

@@ -175,6 +177,9 @@ def process_templates(self):
175177
# {{counter}} by an incrementing counter
176178
self.text = self.input_text.copy()
177179
for line in self.text:
180+
if len(line['text']) > 500:
181+
logger.warning("Text line is very long (> 500 characters), this may lead to long processing times.")
182+
178183
# Replace {{counter}} with current counter value
179184
line['text'] = line['text'].replace("{{counter}}", str(self._counter))
180185

@@ -186,9 +191,7 @@ def datetime_replacer(match):
186191
else:
187192
now = datetime.datetime.now()
188193
return now.strftime(fmt)
189-
# Performance issue mitigation
190-
if len(line['text']) < 100:
191-
line['text'] = re.sub(r"\{\{datetime:([^}]+)\}\}", datetime_replacer, line['text'])
194+
line['text'] = re.sub(r"\{\{datetime:([^}]+)\}\}", datetime_replacer, line['text'])
192195

193196
# Replace {{uuid}} with a new UUID
194197
if "{{uuid}}" in line['text']:
@@ -202,9 +205,13 @@ def datetime_replacer(match):
202205
def env_replacer(match):
203206
var_name = match.group(1)
204207
return os.getenv(var_name, "")
205-
# Performance issue mitigation
206-
if len(line['text']) < 100:
207-
line['text'] = re.sub(r"\{\{env:([^}]+)\}\}", env_replacer, line['text'])
208+
line['text'] = re.sub(r"\{\{env:([^}]+)\}\}", env_replacer, line['text'])
209+
210+
# Replace {{random[:len]}} with random string of optional length <len>
211+
def random_replacer(match):
212+
length = int(match.group(1)) if match.group(1) else 64
213+
return ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=length))
214+
line['text'] = re.sub(r"\{\{random(?:\:(\d+))?\}\}", random_replacer, line['text'])
208215

209216
# Increment counter
210217
self._counter += 1

app/labeldesigner/templates/labeldesigner.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ <h6>Additional Font Options:</h6>
360360
</li>
361361
<li><code>{{'{{uuid'}}}}</code> — Inserts a random UUID (Universally Unique Identifier).</li>
362362
<li><code>{{'{{short-uuid'}}}}</code> — Inserts a shortened version of a UUID.</li>
363-
<li><code>{{'{{env:var}}'}}</code> — Inserts the value of the environment variable <code>var</code>.
364-
</li>
363+
<li><code>{{'{{env:var}}'}}</code> — Inserts the value of the environment variable <code>var</code>.</li>
364+
<li><code>{{'{{random[:len]}}'}}</code> — Inserts a random string of optional length <code>len</code> (defaulting to 64).</li>
365365
</ul>
366366
<span class="text-muted">Templates are replaced at print time.</span>
367367
</div>

0 commit comments

Comments
 (0)