From d4cb697f14c036efe3fd4a09e49164ee53351741 Mon Sep 17 00:00:00 2001 From: Michael Stangl Date: Fri, 9 Oct 2026 08:53:55 +0200 Subject: [PATCH] =?UTF-8?q?Auf=20Python=203=20portiert,=20Django-Abh=C3=A4?= =?UTF-8?q?ngigkeit=20entfernt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- bin/piccreate | 2 +- libs/piccreate.py | 53 ++++++++++++++++++++++----------------------- libs/progressbar.py | 2 +- libs/svgconvert.py | 2 +- libs/utils.py | 28 +++++++++++++++--------- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/bin/piccreate b/bin/piccreate index b496816..6d93e13 100755 --- a/bin/piccreate +++ b/bin/piccreate @@ -2,4 +2,4 @@ . setenv.sh # Startet das Programm -python $PICCREATE_LIBS/piccreate.py "$@" +python3 $PICCREATE_LIBS/piccreate.py "$@" diff --git a/libs/piccreate.py b/libs/piccreate.py index 0d09be5..a8806f1 100644 --- a/libs/piccreate.py +++ b/libs/piccreate.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- __version__ = '0.0.1' @@ -9,9 +9,8 @@ __license__ = 'BSD' import optparse import os import re -import ConfigParser +import configparser from utils import CSVImport, CompleteEnding, get_translation -from django.utils.encoding import smart_str, smart_unicode from string import Template class PicError(Exception): @@ -47,7 +46,7 @@ def create_picture_frame( config ): font_path = os.path.expandvars(os.path.join('$PICCREATE_BACKGROUNDS', background )) params['background'] = os.path.normpath(font_path) svgbegin = svgbegin + Template( imagetpl ).substitute(params) - except ConfigParser.NoOptionError: + except configparser.NoOptionError: # dann eben kein Hintergrundbild pass svgend = '' @@ -60,18 +59,18 @@ def write_font_line( content, font, params ): wordsvg = w.GetSVG() # falls das Wort nun skaliert werden soll - if params.has_key('scale'): + if 'scale' in params: params['xscale'] = params['scale'] params['yscale'] = params['scale'] - if not params.has_key('xscale'): + if 'xscale' not in params: params['xscale'] = 1.0 - if not params.has_key('yscale'): + if 'yscale' not in params: params['yscale'] = 1.0 pic_width = float(params['width']) pic_height = float(params['height']) - if params.has_key('autoscale'): + if 'autoscale' in params: wordlength = w.calc_length() wordheight = w.calc_height() #print wordlength, wordheight @@ -87,7 +86,7 @@ def write_font_line( content, font, params ): scale_faktor = 1.0 # berechne immer den richtigen Startpunkt, je nach Bündigkeit - if params.has_key('align'): + if 'align' in params: xstart = float(params['x']) if params['align'] == 'left': x = xstart @@ -101,7 +100,7 @@ def write_font_line( content, font, params ): else: params['xstart'] = params['x'] - if params.has_key('yalign'): + if 'yalign' in params: ystart = float(params['y']) if params['yalign'] == 'top': y = 0.0 @@ -204,9 +203,9 @@ class Font: self.name = fontname self.path = font_path self.glyphs = dict() - fontconfig = ConfigParser.ConfigParser() + fontconfig = configparser.ConfigParser(interpolation=None, strict=False) config_path = os.path.join(font_path, fontname+'.cfg') - fontconfig.read(config_path) + fontconfig.read(config_path, encoding='utf-8') fontconfig.optionxform = str # lies alle Zeichen des Fonts ein @@ -214,8 +213,8 @@ class Font: # hol die Fontattribute wie Ausdehnung usw. content = dict(fontconfig.items(letter)) # lade das svg template dazu ein - glyph_path = os.path.join( font_path, smart_unicode(content['fontname'])) - f = open(glyph_path, 'r') + glyph_path = os.path.join( font_path, content['fontname']) + f = open(glyph_path, 'r', encoding='utf-8') svgstr = f.read() f.close() # und bau das Zeichen auf @@ -224,7 +223,7 @@ class Font: def __repr__(self): return repr(self.glyphs) def GlyphGet(self, glyphname): - return self.glyphs[smart_str(glyphname)] + return self.glyphs[glyphname] def init_fonts( config ): ''' 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)) # 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') - if not lineparams.has_key('height'): + if 'height' not in lineparams: lineparams['height'] = config.get('globals', 'height') - if not lineparams.has_key('x'): + if 'x' not in lineparams: lineparams['x'] = 0.0 - if not lineparams.has_key('y'): + if 'y' not in lineparams: lineparams['y'] = 0.0 picstr = write_font_line( content, font, lineparams ) @@ -305,9 +304,9 @@ def create_picture(config, fontinfos, data, pic_path): svgstr = svgstr + picend if options.dry: - print svgstr + print(svgstr) else: - f = open(pic_path, 'w') + f = open(pic_path, 'w', encoding='utf-8') f.write( svgstr ) f.close() @@ -326,17 +325,17 @@ def main( config ): 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'))) 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 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 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 - config = ConfigParser.RawConfigParser() - config.read(config_path) + config = configparser.RawConfigParser(strict=False) + config.read(config_path, encoding='utf-8') Trans = get_translation(config_path, 'translation') Rows = CSVImport(in_path, translate=Trans) @@ -344,7 +343,7 @@ def main( config ): fontinfos = init_fonts( config ) for row in Rows: filename = row.customerid - print filename + print(filename) content = row.__dict__ del content['firstrow'] del content['row'] diff --git a/libs/progressbar.py b/libs/progressbar.py index bf9c595..c86c1e8 100644 --- a/libs/progressbar.py +++ b/libs/progressbar.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- # A Python Library to create a Progress Bar. # Copyright (C) 2008 BJ Dierkes diff --git a/libs/svgconvert.py b/libs/svgconvert.py index 1afe517..d2cc2e7 100644 --- a/libs/svgconvert.py +++ b/libs/svgconvert.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3.2 +#!/usr/bin/env python3 # -*- coding: utf-8 -*- __version__ = "0.0.1" diff --git a/libs/utils.py b/libs/utils.py index fc6c6bb..c58f3ab 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -1,15 +1,23 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- -from django.utils.encoding import smart_unicode -from django.template.defaultfilters import slugify +import configparser import csv -import ConfigParser -import os.path +import os.path 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): - fh = open(csvfile, 'rb') + fh = open(csvfile, 'r', encoding='utf-8', newline='') if dialect is None: sniffer = csv.Sniffer() dialect = sniffer.sniff(fh.read(1024), delimiters=';,\t') @@ -24,7 +32,7 @@ class CSVRow(object): self.firstrow = field_refinition for i, field in enumerate(field_refinition): try: - setattr(self, field, smart_unicode(row[i])) + setattr(self, field, row[i]) except IndexError: setattr(self, field, None) @@ -50,7 +58,7 @@ class CSVImport(object): field = 'column_%d' % i if translate: if field in translate: - print field + ' -> '+ translate[field] + print(field + ' -> ' + translate[field]) field = translate[field] field = slugify(field.strip()).replace('-', '_') self.field_definition.append(field) @@ -71,8 +79,8 @@ class CSVImport(object): def get_translation(cfg_path, sec_name): """ lädt die Zuordnung zwischen einem .csv Eingabefile und den Attributen """ - config = ConfigParser.RawConfigParser() - config.read(cfg_path) + config = configparser.RawConfigParser(strict=False) + config.read(cfg_path, encoding='utf-8') trans = dict(config.items(sec_name)) return dict([[v,k] for k,v in trans.items()])