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:
co-authored by
Claude Opus 5.5
parent
dc8e5230e2
commit
d4cb697f14
+26
-27
@@ -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 = '</svg>'
|
||||
@@ -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']
|
||||
|
||||
Reference in New Issue
Block a user