Attachment 'error_notice_for_1.0rc4.patch'
Download 1 --- ../MoinGraphViz-1.0rc4/wiki/data/plugin/parser/MoinGraphViz/main.py 2009-05-22 12:22:34.000000000 +0800
2 +++ wiki/data/plugin/parser/MoinGraphViz/main.py 2010-10-27 17:10:58.236666657 +0800
3 @@ -46,6 +46,12 @@
4
5 #UID = 'BB962F5E-DB8E-424C-8E4D-D2B53286D6F3'
6
7 +class GraphvizRenderError(Exception):
8 + pass
9 +
10 +class GraphvizSyntaxError(GraphvizRenderError):
11 + pass
12 +
13 class Parser:
14 """
15 MoinMoin GraphViz parser.
16 @@ -62,14 +68,21 @@
17 self.renderer = Renderer(tool, targetdir=p.getPagePath('attachments'), encoding=config.charset)
18
19 def format(self, formatter):
20 - w = self.request.write
21 - ##w('<div style="border:3px ridge gray; padding:5px; width:95%; overflow:auto">')
22 - s = self.renderer.render(self.raw)
23 - fmt = moinVersion >= (1,6) and '{{attachment:%s}}' or 'attachment:%s'
24 + append_text = ''
25 + try:
26 + s = self.renderer.render(self.raw)
27 + except GraphvizSyntaxError, e:
28 + s = e.imagefilename
29 + #eliminate source path in the error message, which annoys users
30 + append_text = "{{{%s}}}" % str(e).replace(e.dotfilename, "")
31 + except GraphvizRenderError, e:
32 + self.request.write("<strong class=\"error\">GraphViz: </strong><pre>%s</pre>" % e)
33 + return
34 + fmt = '{{attachment:%s}}' if moinVersion >= (1,6) else 'attachment:%s'
35 + fmt += append_text
36 s = wiki2html(self.request, fmt % os.path.basename(s))
37 - if DEBUG: print '[TRACE] attachment URL:', s
38 - w(s)
39 - ##w('</div>')
40 + if DEBUG: print '[TRACE] attachment html:', s
41 + self.request.write(s)
42
43
44 def parseArguments(s):
45 @@ -105,6 +118,7 @@
46 import sys
47 import sha
48 import string
49 +from subprocess import Popen, PIPE
50
51 try: __file = __file__
52 except NameError: __file = sys.argv[0]
53 @@ -160,6 +174,7 @@
54 imagefilename = 'graphviz-%s-%s.%s' % (gname, uid, self.format)
55 imagefilename = os.path.join(self.targetdir, imagefilename)
56 ##if DEBUG: print '[TRACE] imagefilename:', imagefilename
57 +
58 if not os.path.isfile(imagefilename):
59 if DEBUG: print '[TRACE] creating graph image:', imagefilename
60 dotfilename = '%s-%s.%s' % ('graph', uid, 'graphviz.txt')
61 @@ -167,6 +182,15 @@
62 fwrite(dotfilename, script)
63 try:
64 renderGraphImage(self.toolpath, self.format, imagefilename, dotfilename)
65 + except GraphvizRenderError, e:
66 + if os.path.exists(imagefilename):
67 + #imagefilename exists means nothing terriblely happeded,
68 + #just little cases about dot syntax.
69 + if DEBUG: print "[TRACE] Syntax Error"
70 + e = GraphvizSyntaxError(e)
71 + e.imagefilename = imagefilename
72 + e.dotfilename = dotfilename
73 + raise e
74 finally:
75 os.remove(dotfilename)
76 return imagefilename
77 @@ -176,20 +200,23 @@
78 ##return string.Template(script.strip()).safe_substitute(vars(Snippets)) # for syntax $name
79 v = vars(Snippets)
80 s = string.Template(script.strip() % v).safe_substitute(v) # for both syntaxes
81 +
82 # the following is a hint from an anonimous user, the purpose of which I do not quite
83 # understand yet. it should fix a "unicode problem", but it is unclear what the problem
84 # actually is and how it occurs. I'll take the patch in just in case, in the hope that
85 # it really fixes something and someone can explain what that something is. -- ZI, 2008-04-20
86 - ##return unicode(s).encode(self.encoding)
87 - return s
88 + return unicode(s).encode(self.encoding)
89 + #return s
90
91 def hashFor(self, content):
92 return sha.new(content).hexdigest()
93
94 def graphName(script):
95 ##m = re.match(r'^(?:\n|\s)*(?:di)?graph\s*(\w*)', script) # allows spaces but no comments at beginning of script
96 - m = re.search(r'^(?:di)?graph\s*(\w*)', script, re.M)
97 - assert m, 'Could not derive graph name from graph script. Check the syntax, please!'
98 + m = re.search(r'^(?:di)?graph\s+(\w*)', script, re.M)
99 + if m is None:
100 + raise GraphvizRenderError( \
101 + "Could not derive graph name from graph script. Check the syntax, please!")
102 return m.group(1)
103
104 def renderGraphImage(tool, format, imagefilename, dotfilename):
105 @@ -220,17 +247,15 @@
106
107 def oscmd(cmd):
108 '''instead of simply calling os.system(cmd)
109 - capture stderr and raise os.error if exit code != 0
110 + capture stderr and raise GraphvizRenderError if exit code != 0
111 '''
112 - err = 'Unknown OS error'
113 - w, r, e = os.popen3(cmd)
114 - try:
115 - err = e.read()
116 - out = r.read()
117 - ##return out, err
118 - finally:
119 - for f in w, r, e: xc = f.close()
120 - if xc: raise os.error, err
121 + p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True,
122 + bufsize=1024, close_fds=True)
123 + stdo, stde = p.communicate()
124 +
125 + if p.returncode != 0:
126 + raise GraphvizRenderError("%s\n%s" % (stdo, stde))
127 +
128
129 ######################################################################
130 ##
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.