88from barcode .writer import ImageWriter
99import datetime
1010import re
11+ import random
12+ import string
1113
1214logger = 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
0 commit comments