Auf Python 3 portiert, Django-Abhängigkeit entfernt

- piccreate.py, utils.py: print, configparser, has_key -> in, UTF-8 I/O
- smart_str/smart_unicode entfernt, eigenes slugify() in utils.py
- configparser mit strict=False (doppelte [+] Sektion in den Font-cfgs)
- Shebangs und bin/piccreate auf python3

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michael Stangl
2026-10-09 08:53:55 +02:00
co-authored by Claude Opus 5.5
parent dc8e5230e2
commit d4cb697f14
5 changed files with 47 additions and 40 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
. setenv.sh . setenv.sh
# Startet das Programm # Startet das Programm
python $PICCREATE_LIBS/piccreate.py "$@" python3 $PICCREATE_LIBS/piccreate.py "$@"
+26 -27
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__version__ = '0.0.1' __version__ = '0.0.1'
@@ -9,9 +9,8 @@ __license__ = 'BSD'
import optparse import optparse
import os import os
import re import re
import ConfigParser import configparser
from utils import CSVImport, CompleteEnding, get_translation from utils import CSVImport, CompleteEnding, get_translation
from django.utils.encoding import smart_str, smart_unicode
from string import Template from string import Template
class PicError(Exception): class PicError(Exception):
@@ -47,7 +46,7 @@ def create_picture_frame( config ):
font_path = os.path.expandvars(os.path.join('$PICCREATE_BACKGROUNDS', background )) font_path = os.path.expandvars(os.path.join('$PICCREATE_BACKGROUNDS', background ))
params['background'] = os.path.normpath(font_path) params['background'] = os.path.normpath(font_path)
svgbegin = svgbegin + Template( imagetpl ).substitute(params) svgbegin = svgbegin + Template( imagetpl ).substitute(params)
except ConfigParser.NoOptionError: except configparser.NoOptionError:
# dann eben kein Hintergrundbild # dann eben kein Hintergrundbild
pass pass
svgend = '</svg>' svgend = '</svg>'
@@ -60,18 +59,18 @@ def write_font_line( content, font, params ):
wordsvg = w.GetSVG() wordsvg = w.GetSVG()
# falls das Wort nun skaliert werden soll # falls das Wort nun skaliert werden soll
if params.has_key('scale'): if 'scale' in params:
params['xscale'] = params['scale'] params['xscale'] = params['scale']
params['yscale'] = params['scale'] params['yscale'] = params['scale']
if not params.has_key('xscale'): if 'xscale' not in params:
params['xscale'] = 1.0 params['xscale'] = 1.0
if not params.has_key('yscale'): if 'yscale' not in params:
params['yscale'] = 1.0 params['yscale'] = 1.0
pic_width = float(params['width']) pic_width = float(params['width'])
pic_height = float(params['height']) pic_height = float(params['height'])
if params.has_key('autoscale'): if 'autoscale' in params:
wordlength = w.calc_length() wordlength = w.calc_length()
wordheight = w.calc_height() wordheight = w.calc_height()
#print wordlength, wordheight #print wordlength, wordheight
@@ -87,7 +86,7 @@ def write_font_line( content, font, params ):
scale_faktor = 1.0 scale_faktor = 1.0
# berechne immer den richtigen Startpunkt, je nach Bündigkeit # berechne immer den richtigen Startpunkt, je nach Bündigkeit
if params.has_key('align'): if 'align' in params:
xstart = float(params['x']) xstart = float(params['x'])
if params['align'] == 'left': if params['align'] == 'left':
x = xstart x = xstart
@@ -101,7 +100,7 @@ def write_font_line( content, font, params ):
else: else:
params['xstart'] = params['x'] params['xstart'] = params['x']
if params.has_key('yalign'): if 'yalign' in params:
ystart = float(params['y']) ystart = float(params['y'])
if params['yalign'] == 'top': if params['yalign'] == 'top':
y = 0.0 y = 0.0
@@ -204,9 +203,9 @@ class Font:
self.name = fontname self.name = fontname
self.path = font_path self.path = font_path
self.glyphs = dict() self.glyphs = dict()
fontconfig = ConfigParser.ConfigParser() fontconfig = configparser.ConfigParser(interpolation=None, strict=False)
config_path = os.path.join(font_path, fontname+'.cfg') config_path = os.path.join(font_path, fontname+'.cfg')
fontconfig.read(config_path) fontconfig.read(config_path, encoding='utf-8')
fontconfig.optionxform = str fontconfig.optionxform = str
# lies alle Zeichen des Fonts ein # lies alle Zeichen des Fonts ein
@@ -214,8 +213,8 @@ class Font:
# hol die Fontattribute wie Ausdehnung usw. # hol die Fontattribute wie Ausdehnung usw.
content = dict(fontconfig.items(letter)) content = dict(fontconfig.items(letter))
# lade das svg template dazu ein # lade das svg template dazu ein
glyph_path = os.path.join( font_path, smart_unicode(content['fontname'])) glyph_path = os.path.join( font_path, content['fontname'])
f = open(glyph_path, 'r') f = open(glyph_path, 'r', encoding='utf-8')
svgstr = f.read() svgstr = f.read()
f.close() f.close()
# und bau das Zeichen auf # und bau das Zeichen auf
@@ -224,7 +223,7 @@ class Font:
def __repr__(self): def __repr__(self):
return repr(self.glyphs) return repr(self.glyphs)
def GlyphGet(self, glyphname): def GlyphGet(self, glyphname):
return self.glyphs[smart_str(glyphname)] return self.glyphs[glyphname]
def init_fonts( config ): def init_fonts( config ):
''' lädt alle benötigten Fonts für den Auftrag in den Speicher ''' lädt alle benötigten Fonts für den Auftrag in den Speicher
@@ -287,13 +286,13 @@ def create_picture(config, fontinfos, data, pic_path):
lineparams = dict(config.items(linename)) lineparams = dict(config.items(linename))
# falls nix angegeben, dann aus hol defaults aus globalen daten # falls nix angegeben, dann aus hol defaults aus globalen daten
if not lineparams.has_key('width'): if 'width' not in lineparams:
lineparams['width'] = config.get('globals', 'width') lineparams['width'] = config.get('globals', 'width')
if not lineparams.has_key('height'): if 'height' not in lineparams:
lineparams['height'] = config.get('globals', 'height') lineparams['height'] = config.get('globals', 'height')
if not lineparams.has_key('x'): if 'x' not in lineparams:
lineparams['x'] = 0.0 lineparams['x'] = 0.0
if not lineparams.has_key('y'): if 'y' not in lineparams:
lineparams['y'] = 0.0 lineparams['y'] = 0.0
picstr = write_font_line( content, font, lineparams ) picstr = write_font_line( content, font, lineparams )
@@ -305,9 +304,9 @@ def create_picture(config, fontinfos, data, pic_path):
svgstr = svgstr + picend svgstr = svgstr + picend
if options.dry: if options.dry:
print svgstr print(svgstr)
else: else:
f = open(pic_path, 'w') f = open(pic_path, 'w', encoding='utf-8')
f.write( svgstr ) f.write( svgstr )
f.close() f.close()
@@ -326,17 +325,17 @@ def main( config ):
config_path = os.path.expandvars(os.path.join('$PICCREATE_CFG', CompleteEnding( config, 'cfg'))) config_path = os.path.expandvars(os.path.join('$PICCREATE_CFG', CompleteEnding( config, 'cfg')))
in_path = os.path.expandvars(os.path.join('$PICCREATE_IN', CompleteEnding( infile, 'csv'))) in_path = os.path.expandvars(os.path.join('$PICCREATE_IN', CompleteEnding( infile, 'csv')))
if not os.path.exists(config_path): if not os.path.exists(config_path):
print '\"'+ config_path +'\" does not exist. Please give the name of a config file!' print('\"'+ config_path +'\" does not exist. Please give the name of a config file!')
return None return None
if not os.path.exists(in_path): if not os.path.exists(in_path):
print '\"'+ in_path +'\" does not exist. Please give the name to a input file!' print('\"'+ in_path +'\" does not exist. Please give the name to a input file!')
return None return None
if not os.path.exists(out_path): if not os.path.exists(out_path):
print '\"'+ out_path +'\" does not exist. Please give the path to the directory for the pictures!' print('\"'+ out_path +'\" does not exist. Please give the path to the directory for the pictures!')
return None return None
config = ConfigParser.RawConfigParser() config = configparser.RawConfigParser(strict=False)
config.read(config_path) config.read(config_path, encoding='utf-8')
Trans = get_translation(config_path, 'translation') Trans = get_translation(config_path, 'translation')
Rows = CSVImport(in_path, translate=Trans) Rows = CSVImport(in_path, translate=Trans)
@@ -344,7 +343,7 @@ def main( config ):
fontinfos = init_fonts( config ) fontinfos = init_fonts( config )
for row in Rows: for row in Rows:
filename = row.customerid filename = row.customerid
print filename print(filename)
content = row.__dict__ content = row.__dict__
del content['firstrow'] del content['firstrow']
del content['row'] del content['row']
+1 -1
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# A Python Library to create a Progress Bar. # A Python Library to create a Progress Bar.
# Copyright (C) 2008 BJ Dierkes <wdierkes@5dollarwhitebox.org> # Copyright (C) 2008 BJ Dierkes <wdierkes@5dollarwhitebox.org>
+1 -1
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env python3.2 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
__version__ = "0.0.1" __version__ = "0.0.1"
+18 -10
View File
@@ -1,15 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.utils.encoding import smart_unicode import configparser
from django.template.defaultfilters import slugify
import csv import csv
import ConfigParser import os.path
import os.path
import re import re
import unicodedata
def slugify(value):
''' wandelt einen String in einen ASCII Bezeichner um (wie Djangos slugify)
'''
value = unicodedata.normalize('NFKD', str(value))
value = value.encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return re.sub(r'[-\s]+', '-', value)
def csv_open(csvfile, dialect=None): def csv_open(csvfile, dialect=None):
fh = open(csvfile, 'rb') fh = open(csvfile, 'r', encoding='utf-8', newline='')
if dialect is None: if dialect is None:
sniffer = csv.Sniffer() sniffer = csv.Sniffer()
dialect = sniffer.sniff(fh.read(1024), delimiters=';,\t') dialect = sniffer.sniff(fh.read(1024), delimiters=';,\t')
@@ -24,7 +32,7 @@ class CSVRow(object):
self.firstrow = field_refinition self.firstrow = field_refinition
for i, field in enumerate(field_refinition): for i, field in enumerate(field_refinition):
try: try:
setattr(self, field, smart_unicode(row[i])) setattr(self, field, row[i])
except IndexError: except IndexError:
setattr(self, field, None) setattr(self, field, None)
@@ -50,7 +58,7 @@ class CSVImport(object):
field = 'column_%d' % i field = 'column_%d' % i
if translate: if translate:
if field in translate: if field in translate:
print field + ' -> '+ translate[field] print(field + ' -> ' + translate[field])
field = translate[field] field = translate[field]
field = slugify(field.strip()).replace('-', '_') field = slugify(field.strip()).replace('-', '_')
self.field_definition.append(field) self.field_definition.append(field)
@@ -71,8 +79,8 @@ class CSVImport(object):
def get_translation(cfg_path, sec_name): def get_translation(cfg_path, sec_name):
""" lädt die Zuordnung zwischen einem .csv Eingabefile und den """ lädt die Zuordnung zwischen einem .csv Eingabefile und den
Attributen """ Attributen """
config = ConfigParser.RawConfigParser() config = configparser.RawConfigParser(strict=False)
config.read(cfg_path) config.read(cfg_path, encoding='utf-8')
trans = dict(config.items(sec_name)) trans = dict(config.items(sec_name))
return dict([[v,k] for k,v in trans.items()]) return dict([[v,k] for k,v in trans.items()])