blob: e85dd841e9defa68ef90574b23ab944d557caac8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#! /usr/bin/env python
# Copyright (c) 2007-2008 PediaPress GmbH
# See README.txt for additional licensing information.
"""implement http://meta.wikimedia.org/wiki/EasyTimeline
"""
import os
import tempfile
try:
from hashlib import md5
except ImportError:
from md5 import md5
def drawTimeline(script, basedir=None):
if isinstance(script, unicode):
script = script.encode('utf8')
if basedir is None:
basedir = os.path.join(tempfile.gettempdir(), "timeline-%s" % (os.getuid(),))
if not os.path.exists(basedir):
os.mkdir(basedir)
m=md5()
m.update(script)
ident = m.hexdigest()
pngfile = os.path.join(basedir, ident+'.png')
if os.path.exists(pngfile):
return pngfile
scriptfile = os.path.join(basedir, ident+'.txt')
open(scriptfile, 'w').write(script)
et = os.path.join(os.path.dirname(__file__), "EasyTimeline.pl")
err = os.system("perl %s -P /usr/bin/ploticus -T /tmp/ -i %s" % (et, scriptfile))
if err != 0:
return None
svgfile = os.path.join(basedir, ident+'.svg')
if os.path.exists(svgfile):
os.unlink(svgfile)
if os.path.exists(pngfile):
return pngfile
return None
|