python3 compat

This commit is contained in:
MaZderMind 2014-07-20 10:59:06 +02:00
parent bc4c29f8b6
commit bd079cc0cd

42
make.py
View file

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
import sys import sys
@ -9,7 +9,7 @@ import math
import time import time
import shutil import shutil
import errno import errno
import urllib2 import urllib
from lxml import etree from lxml import etree
from xml.sax.saxutils import escape as xmlescape from xml.sax.saxutils import escape as xmlescape
import cssutils import cssutils
@ -19,11 +19,11 @@ import threading
import multiprocessing import multiprocessing
from threading import Thread, Lock from threading import Thread, Lock
import subprocess import subprocess
from Queue import Queue from queue import Queue
# Project-Name # Project-Name
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "you must specify a project-name as first argument, eg. './make.py sotmeu14'" print("you must specify a project-name as first argument, eg. './make.py sotmeu14'")
sys.exit(1) sys.exit(1)
projectname = sys.argv[1].strip('/') projectname = sys.argv[1].strip('/')
@ -31,7 +31,7 @@ try:
sys.path.append(projectname) sys.path.append(projectname)
project = __import__(projectname) project = __import__(projectname)
except ImportError: except ImportError:
print "you must specify a project-name as first argument, eg. './make.py sotmeu14'. The supplied value '{0}' seems not to be a valid project (there is no '{0}/__init__.py').".format(projectname); print("you must specify a project-name as first argument, eg. './make.py sotmeu14'. The supplied value '{0}' seems not to be a valid project (there is no '{0}/__init__.py').".format(projectname))
sys.exit(1) sys.exit(1)
# Frames per second. Increasing this renders more frames, the avconf-statements would still need modifications # Frames per second. Increasing this renders more frames, the avconf-statements would still need modifications
@ -44,10 +44,6 @@ debug = ('--debug' in sys.argv)
# using --offline only skips the network fetching and use a local schedule.de.xml # using --offline only skips the network fetching and use a local schedule.de.xml
offline = ('--offline' in sys.argv) offline = ('--offline' in sys.argv)
# set charset of output-terminal
reload(sys)
sys.setdefaultencoding('utf-8')
# try to create all folders needed and skip, they already exist # try to create all folders needed and skip, they already exist
def ensurePathExists(path): def ensurePathExists(path):
try: try:
@ -67,7 +63,7 @@ cssutils.log.setLevel(logging.FATAL)
def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projectname, 'artwork')): def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projectname, 'artwork')):
# in debug mode we have no thread-worker which prints its progress # in debug mode we have no thread-worker which prints its progress
if debug: if debug:
print "generating {0} from {1}".format(outfile, infile) print("generating {0} from {1}".format(outfile, infile))
# make sure a .frames-directory exists in out workdir # make sure a .frames-directory exists in out workdir
ensurePathExists(os.path.join(workdir, '.frames')) ensurePathExists(os.path.join(workdir, '.frames'))
@ -78,7 +74,7 @@ def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projec
for key in parameters.keys(): for key in parameters.keys():
svgstr = svgstr.replace(key, xmlescape(str(parameters[key]))) svgstr = svgstr.replace(key, xmlescape(str(parameters[key])))
svg = etree.fromstring(svgstr) svg = etree.fromstring(svgstr.encode('utf-8'))
# find all images and force them to absolute file-urls # find all images and force them to absolute file-urls
namespaces = {'xlink': 'http://www.w3.org/1999/xlink', 'svg': 'http://www.w3.org/2000/svg'} namespaces = {'xlink': 'http://www.w3.org/1999/xlink', 'svg': 'http://www.w3.org/2000/svg'}
@ -93,7 +89,7 @@ def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projec
for frame in sequence(): for frame in sequence():
# print a line for each and every frame generated # print a line for each and every frame generated
if debug: if debug:
print "frameNr {0:2d} => {1}".format(frameNr, frame) print("frameNr {0:2d} => {1}".format(frameNr, frame))
# open the output-file (named ".gen.svg" in the workdir) # open the output-file (named ".gen.svg" in the workdir)
with open(os.path.join(workdir, '.gen.svg'), 'w') as fp: with open(os.path.join(workdir, '.gen.svg'), 'w') as fp:
@ -104,19 +100,19 @@ def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projec
for el in svg.findall(".//*[@id='"+id.replace("'", "\\'")+"']"): for el in svg.findall(".//*[@id='"+id.replace("'", "\\'")+"']"):
if type == 'style': if type == 'style':
style = cssutils.parseStyle( el.attrib['style'] if 'style' in el.attrib else '' ) style = cssutils.parseStyle( el.attrib['style'] if 'style' in el.attrib else '' )
style[key] = unicode(value) style[key] = str(value)
el.attrib['style'] = style.cssText el.attrib['style'] = style.cssText
elif type == 'attr': elif type == 'attr':
el.attrib[key] = value el.attrib[key] = value
# write the generated svg-text into the output-file # write the generated svg-text into the output-file
fp.write( etree.tostring(svg) ) fp.write( etree.tostring(svg, encoding='unicode') )
# invoke inkscape to convert the generated svg-file into a png inside the .frames-directory # invoke inkscape to convert the generated svg-file into a png inside the .frames-directory
errorReturn = subprocess.check_output('cd {0} && inkscape --export-png=.frames/{1:04d}.png .gen.svg 2>&1 >/dev/null'.format(workdir, frameNr), shell=True) errorReturn = subprocess.check_output('cd {0} && inkscape --export-png=.frames/{1:04d}.png .gen.svg 2>&1 >/dev/null'.format(workdir, frameNr), shell=True, universal_newlines=True)
if errorReturn != '': if errorReturn != '':
print "inkscape exitted with error\n"+errorReturn print("inkscape exitted with error\n"+errorReturn)
sys.exit(42) sys.exit(42)
# increment frame-number # increment frame-number
@ -133,7 +129,7 @@ def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projec
# as before, in non-debug-mode the thread-worker does all progress messages # as before, in non-debug-mode the thread-worker does all progress messages
if debug: if debug:
print "cleanup" print("cleanup")
# remove the .frames-dir with all frames in it # remove the .frames-dir with all frames in it
shutil.rmtree(os.path.join(workdir, '.frames')) shutil.rmtree(os.path.join(workdir, '.frames'))
@ -145,7 +141,7 @@ def render(infile, outfile, sequence, parameters={}, workdir=os.path.join(projec
# Download the Events-Schedule and parse all Events out of it. Yield a tupel for each Event # Download the Events-Schedule and parse all Events out of it. Yield a tupel for each Event
def events(): def events():
print "downloading pentabarf schedule" print("downloading pentabarf schedule")
# use --offline to skip networking # use --offline to skip networking
if offline: if offline:
@ -154,7 +150,7 @@ def events():
else: else:
# download the schedule # download the schedule
response = urllib2.urlopen(project.scheduleUrl) response = urllib.urlopen(project.scheduleUrl)
# read xml-source # read xml-source
xml = response.read() xml = response.read()
@ -215,7 +211,7 @@ project.easeLinear = easeLinear
# debug-mode selected by --debug switch # debug-mode selected by --debug switch
if debug: if debug:
print "!!! DEBUG MODE !!!" print("!!! DEBUG MODE !!!")
# call into project which calls render as needed # call into project which calls render as needed
project.debug() project.debug()
@ -233,7 +229,7 @@ project.tasks(tasks)
# one working thread per cpu # one working thread per cpu
num_worker_threads = multiprocessing.cpu_count() num_worker_threads = multiprocessing.cpu_count()
print "{0} tasks in queue, starting {1} worker threads".format(tasks.qsize(), num_worker_threads) print("{0} tasks in queue, starting {1} worker threads".format(tasks.qsize(), num_worker_threads))
# put a sentinel for each thread into the queue to signal the end # put a sentinel for each thread into the queue to signal the end
for _ in range(num_worker_threads): for _ in range(num_worker_threads):
@ -247,7 +243,7 @@ def tprint(str):
printLock.acquire() printLock.acquire()
# print thread-name and message # print thread-name and message
print threading.current_thread().name+': '+str print(threading.current_thread().name+': '+str)
# release lock # release lock
printLock.release() printLock.release()
@ -336,4 +332,4 @@ while True:
# sleep while the workers work # sleep while the workers work
time.sleep(1) time.sleep(1)
print "all worker threads ended" print("all worker threads ended")