##// END OF EJS Templates
highlight: only pygmentize for HTML mimetypes...
Rocco Rutte -
r6987:d09e813b default
parent child Browse files
Show More
@@ -1,50 +1,61 b''
1 """a mercurial extension for syntax highlighting in hgweb
1 """a mercurial extension for syntax highlighting in hgweb
2
2
3 It depends on the pygments syntax highlighting library:
3 It depends on the pygments syntax highlighting library:
4 http://pygments.org/
4 http://pygments.org/
5
5
6 To enable the extension add this to hgrc:
6 To enable the extension add this to hgrc:
7
7
8 [extensions]
8 [extensions]
9 hgext.highlight =
9 hgext.highlight =
10
10
11 There is a single configuration option:
11 There is a single configuration option:
12
12
13 [web]
13 [web]
14 pygments_style = <style>
14 pygments_style = <style>
15
15
16 The default is 'colorful'.
16 The default is 'colorful'.
17
17
18 -- Adam Hupp <adam@hupp.org>
18 -- Adam Hupp <adam@hupp.org>
19 """
19 """
20
20
21 import highlight
21 import highlight
22 from mercurial.hgweb import webcommands, webutil, common
22 from mercurial.hgweb import webcommands, webutil, common
23
23
24 web_filerevision = webcommands._filerevision
24 web_filerevision = webcommands._filerevision
25 web_annotate = webcommands.annotate
25 web_annotate = webcommands.annotate
26
26
27 def filerevision_highlight(web, tmpl, fctx):
27 def filerevision_highlight(web, tmpl, fctx):
28 style = web.config('web', 'pygments_style', 'colorful')
28 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
29 highlight.pygmentize('fileline', fctx, style, tmpl)
29 # only pygmentize for mimetype containing 'html' so we both match
30 # 'text/html' and possibly 'application/xhtml+xml' in the future
31 # so that we don't have to touch the extension when the mimetype
32 # for a template changes; also hgweb optimizes the case that a
33 # raw file is sent using rawfile() and doesn't call us, so we
34 # can't clash with the file's content-type here in case we
35 # pygmentize a html file
36 if 'html' in mt:
37 style = web.config('web', 'pygments_style', 'colorful')
38 highlight.pygmentize('fileline', fctx, style, tmpl)
30 return web_filerevision(web, tmpl, fctx)
39 return web_filerevision(web, tmpl, fctx)
31
40
32 def annotate_highlight(web, req, tmpl):
41 def annotate_highlight(web, req, tmpl):
33 fctx = webutil.filectx(web.repo, req)
42 mt = ''.join(tmpl('mimetype', encoding=web.encoding))
34 style = web.config('web', 'pygments_style', 'colorful')
43 if 'html' in mt:
35 highlight.pygmentize('annotateline', fctx, style, tmpl)
44 fctx = webutil.filectx(web.repo, req)
45 style = web.config('web', 'pygments_style', 'colorful')
46 highlight.pygmentize('annotateline', fctx, style, tmpl)
36 return web_annotate(web, req, tmpl)
47 return web_annotate(web, req, tmpl)
37
48
38 def generate_css(web, req, tmpl):
49 def generate_css(web, req, tmpl):
39 pg_style = web.config('web', 'pygments_style', 'colorful')
50 pg_style = web.config('web', 'pygments_style', 'colorful')
40 fmter = highlight.HtmlFormatter(style = pg_style)
51 fmter = highlight.HtmlFormatter(style = pg_style)
41 req.respond(common.HTTP_OK, 'text/css')
52 req.respond(common.HTTP_OK, 'text/css')
42 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
53 return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
43
54
44
55
45 # monkeypatch in the new version
56 # monkeypatch in the new version
46
57
47 webcommands._filerevision = filerevision_highlight
58 webcommands._filerevision = filerevision_highlight
48 webcommands.annotate = annotate_highlight
59 webcommands.annotate = annotate_highlight
49 webcommands.highlightcss = generate_css
60 webcommands.highlightcss = generate_css
50 webcommands.__all__.append('highlightcss')
61 webcommands.__all__.append('highlightcss')
@@ -1,55 +1,82 b''
1 #!/bin/sh
1 #!/bin/sh
2
2
3 "$TESTDIR/hghave" pygments || exit 80
3 "$TESTDIR/hghave" pygments || exit 80
4
4
5 cat <<EOF >> $HGRCPATH
5 cat <<EOF >> $HGRCPATH
6 [extensions]
6 [extensions]
7 hgext.highlight =
7 hgext.highlight =
8 [web]
8 [web]
9 pygments_style = friendly
9 pygments_style = friendly
10 EOF
10 EOF
11
11
12 hg init test
12 hg init test
13 cd test
13 cd test
14 cp $TESTDIR/get-with-headers.py ./
14 cp $TESTDIR/get-with-headers.py ./
15 hg ci -Ama
15 hg ci -Ama
16
16
17 echo % hg serve
17 echo % hg serve
18 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
18 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
19 cat hg.pid >> $DAEMON_PIDS
19 cat hg.pid >> $DAEMON_PIDS
20
20
21 echo % hgweb filerevision
21 echo % hgweb filerevision, html
22 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py') \
22 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py') \
23 | sed "s/[0-9]* years ago/long ago/g"
23 | sed "s/[0-9]* years ago/long ago/g"
24
24
25 echo % hgweb fileannotate
25 echo % hgweb fileannotate, html
26 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py') \
26 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py') \
27 | sed "s/[0-9]* years ago/long ago/g"
27 | sed "s/[0-9]* years ago/long ago/g"
28
28
29 echo % hgweb fileannotate, raw
30 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py?style=raw') \
31 | sed "s/test@//" > a
32
33 echo "200 Script output follows" > b
34 echo "" >> b
35 echo "" >> b
36 hg annotate "get-with-headers.py" >> b
37 echo "" >> b
38 echo "" >> b
39 echo "" >> b
40 echo "" >> b
41
42 diff -u b a
43
44 echo
45 echo % hgweb filerevision, raw
46 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py?style=raw') \
47 > a
48
49 echo "200 Script output follows" > b
50 echo "" >> b
51 hg cat get-with-headers.py >> b
52
53 diff -u b a
54
55 echo
29 echo % hgweb highlightcss friendly
56 echo % hgweb highlightcss friendly
30 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
57 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
31 head -n 4 out
58 head -n 4 out
32 rm out
59 rm out
33
60
34 echo % errors encountered
61 echo % errors encountered
35 cat errors.log
62 cat errors.log
36 kill `cat hg.pid`
63 kill `cat hg.pid`
37
64
38 # Change the pygments style
65 # Change the pygments style
39 cat > .hg/hgrc <<EOF
66 cat > .hg/hgrc <<EOF
40 [web]
67 [web]
41 pygments_style = fruity
68 pygments_style = fruity
42 EOF
69 EOF
43
70
44 echo % hg serve again
71 echo % hg serve again
45 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
72 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
46 cat hg.pid >> $DAEMON_PIDS
73 cat hg.pid >> $DAEMON_PIDS
47
74
48 echo % hgweb highlightcss fruity
75 echo % hgweb highlightcss fruity
49 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
76 "$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss' > out
50 head -n 4 out
77 head -n 4 out
51 rm out
78 rm out
52
79
53 echo % errors encountered
80 echo % errors encountered
54 cat errors.log
81 cat errors.log
55
82
@@ -1,145 +1,149 b''
1 adding get-with-headers.py
1 adding get-with-headers.py
2 % hg serve
2 % hg serve
3 % hgweb filerevision
3 % hgweb filerevision, html
4 200 Script output follows
4 200 Script output follows
5
5
6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7 <html>
7 <html>
8 <head>
8 <head>
9 <link rel="icon" href="/static/hgicon.png" type="image/png">
9 <link rel="icon" href="/static/hgicon.png" type="image/png">
10 <meta name="robots" content="index, nofollow" />
10 <meta name="robots" content="index, nofollow" />
11 <link rel="stylesheet" href="/static/style.css" type="text/css" />
11 <link rel="stylesheet" href="/static/style.css" type="text/css" />
12
12
13 <link rel="stylesheet" href="/highlightcss" type="text/css" />
13 <link rel="stylesheet" href="/highlightcss" type="text/css" />
14 <title>test:get-with-headers.py</title>
14 <title>test:get-with-headers.py</title>
15 </head>
15 </head>
16 <body>
16 <body>
17
17
18 <div class="buttons">
18 <div class="buttons">
19 <a href="/log/0">changelog</a>
19 <a href="/log/0">changelog</a>
20 <a href="/shortlog/0">shortlog</a>
20 <a href="/shortlog/0">shortlog</a>
21 <a href="/graph">graph</a>
21 <a href="/graph">graph</a>
22 <a href="/tags">tags</a>
22 <a href="/tags">tags</a>
23 <a href="/rev/79ee608ca36d">changeset</a>
23 <a href="/rev/79ee608ca36d">changeset</a>
24 <a href="/file/79ee608ca36d/">files</a>
24 <a href="/file/79ee608ca36d/">files</a>
25 <a href="/log/79ee608ca36d/get-with-headers.py">revisions</a>
25 <a href="/log/79ee608ca36d/get-with-headers.py">revisions</a>
26 <a href="/annotate/79ee608ca36d/get-with-headers.py">annotate</a>
26 <a href="/annotate/79ee608ca36d/get-with-headers.py">annotate</a>
27 <a href="/raw-file/79ee608ca36d/get-with-headers.py">raw</a>
27 <a href="/raw-file/79ee608ca36d/get-with-headers.py">raw</a>
28 </div>
28 </div>
29
29
30 <h2>get-with-headers.py</h2>
30 <h2>get-with-headers.py</h2>
31
31
32 <table>
32 <table>
33 <tr>
33 <tr>
34 <td class="metatag">changeset 0:</td>
34 <td class="metatag">changeset 0:</td>
35 <td><a href="/rev/79ee608ca36d">79ee608ca36d</a></td></tr>
35 <td><a href="/rev/79ee608ca36d">79ee608ca36d</a></td></tr>
36
36
37
37
38 <tr>
38 <tr>
39 <td class="metatag">author:</td>
39 <td class="metatag">author:</td>
40 <td>&#116;&#101;&#115;&#116;</td></tr>
40 <td>&#116;&#101;&#115;&#116;</td></tr>
41 <tr>
41 <tr>
42 <td class="metatag">date:</td>
42 <td class="metatag">date:</td>
43 <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td></tr>
43 <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td></tr>
44 <tr>
44 <tr>
45 <td class="metatag">permissions:</td>
45 <td class="metatag">permissions:</td>
46 <td>-rwxr-xr-x</td></tr>
46 <td>-rwxr-xr-x</td></tr>
47 <tr>
47 <tr>
48 <td class="metatag">description:</td>
48 <td class="metatag">description:</td>
49 <td>a</td>
49 <td>a</td>
50 </tr>
50 </tr>
51 </table>
51 </table>
52
52
53 <pre>
53 <pre>
54 <div class="parity0"><a class="lineno" href="#l1" id="l1"> 1</a><span class="c">#!/usr/bin/env python</span></div><div class="parity1"><a class="lineno" href="#l2" id="l2"> 2</a></div><div class="parity0"><a class="lineno" href="#l3" id="l3"> 3</a><span class="n">__doc__</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;This does HTTP get requests given a host:port and path and returns</span></div><div class="parity1"><a class="lineno" href="#l4" id="l4"> 4</a><span class="s">a subset of the headers plus the body of the result.&quot;&quot;&quot;</span></div><div class="parity0"><a class="lineno" href="#l5" id="l5"> 5</a></div><div class="parity1"><a class="lineno" href="#l6" id="l6"> 6</a><span class="k">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></div><div class="parity0"><a class="lineno" href="#l7" id="l7"> 7</a><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></div><div class="parity1"><a class="lineno" href="#l8" id="l8"> 8</a><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></div><div class="parity0"><a class="lineno" href="#l9" id="l9"> 9</a><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;GET&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></div><div class="parity1"><a class="lineno" href="#l10" id="l10"> 10</a><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></div><div class="parity0"><a class="lineno" href="#l11" id="l11"> 11</a><span class="k">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></div><div class="parity1"><a class="lineno" href="#l12" id="l12"> 12</a><span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l13" id="l13"> 13</a> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></div><div class="parity1"><a class="lineno" href="#l14" id="l14"> 14</a> <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></div><div class="parity0"><a class="lineno" href="#l15" id="l15"> 15</a><span class="k">print</span></div><div class="parity1"><a class="lineno" href="#l16" id="l16"> 16</a><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></div><div class="parity0"><a class="lineno" href="#l17" id="l17"> 17</a></div><div class="parity1"><a class="lineno" href="#l18" id="l18"> 18</a><span class="k">if</span> <span class="mf">200</span> <span class="o">&lt;=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">&lt;=</span> <span class="mf">299</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l19" id="l19"> 19</a> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></div><div class="parity1"><a class="lineno" href="#l20" id="l20"> 20</a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></div>
54 <div class="parity0"><a class="lineno" href="#l1" id="l1"> 1</a><span class="c">#!/usr/bin/env python</span></div><div class="parity1"><a class="lineno" href="#l2" id="l2"> 2</a></div><div class="parity0"><a class="lineno" href="#l3" id="l3"> 3</a><span class="n">__doc__</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;This does HTTP get requests given a host:port and path and returns</span></div><div class="parity1"><a class="lineno" href="#l4" id="l4"> 4</a><span class="s">a subset of the headers plus the body of the result.&quot;&quot;&quot;</span></div><div class="parity0"><a class="lineno" href="#l5" id="l5"> 5</a></div><div class="parity1"><a class="lineno" href="#l6" id="l6"> 6</a><span class="k">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></div><div class="parity0"><a class="lineno" href="#l7" id="l7"> 7</a><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></div><div class="parity1"><a class="lineno" href="#l8" id="l8"> 8</a><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></div><div class="parity0"><a class="lineno" href="#l9" id="l9"> 9</a><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;GET&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></div><div class="parity1"><a class="lineno" href="#l10" id="l10"> 10</a><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></div><div class="parity0"><a class="lineno" href="#l11" id="l11"> 11</a><span class="k">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></div><div class="parity1"><a class="lineno" href="#l12" id="l12"> 12</a><span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l13" id="l13"> 13</a> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></div><div class="parity1"><a class="lineno" href="#l14" id="l14"> 14</a> <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></div><div class="parity0"><a class="lineno" href="#l15" id="l15"> 15</a><span class="k">print</span></div><div class="parity1"><a class="lineno" href="#l16" id="l16"> 16</a><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></div><div class="parity0"><a class="lineno" href="#l17" id="l17"> 17</a></div><div class="parity1"><a class="lineno" href="#l18" id="l18"> 18</a><span class="k">if</span> <span class="mf">200</span> <span class="o">&lt;=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">&lt;=</span> <span class="mf">299</span><span class="p">:</span></div><div class="parity0"><a class="lineno" href="#l19" id="l19"> 19</a> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></div><div class="parity1"><a class="lineno" href="#l20" id="l20"> 20</a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></div>
55 </pre>
55 </pre>
56
56
57
57
58 <div class="logo">
58 <div class="logo">
59 <a href="http://www.selenic.com/mercurial/">
59 <a href="http://www.selenic.com/mercurial/">
60 <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a>
60 <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a>
61 </div>
61 </div>
62
62
63 </body>
63 </body>
64 </html>
64 </html>
65
65
66 % hgweb fileannotate
66 % hgweb fileannotate, html
67 200 Script output follows
67 200 Script output follows
68
68
69 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
69 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
70 <html>
70 <html>
71 <head>
71 <head>
72 <link rel="icon" href="/static/hgicon.png" type="image/png">
72 <link rel="icon" href="/static/hgicon.png" type="image/png">
73 <meta name="robots" content="index, nofollow" />
73 <meta name="robots" content="index, nofollow" />
74 <link rel="stylesheet" href="/static/style.css" type="text/css" />
74 <link rel="stylesheet" href="/static/style.css" type="text/css" />
75
75
76 <link rel="stylesheet" href="/highlightcss" type="text/css" />
76 <link rel="stylesheet" href="/highlightcss" type="text/css" />
77 <title>test: get-with-headers.py annotate</title>
77 <title>test: get-with-headers.py annotate</title>
78 </head>
78 </head>
79 <body>
79 <body>
80
80
81 <div class="buttons">
81 <div class="buttons">
82 <a href="/log/0">changelog</a>
82 <a href="/log/0">changelog</a>
83 <a href="/shortlog/0">shortlog</a>
83 <a href="/shortlog/0">shortlog</a>
84 <a href="/graph">graph</a>
84 <a href="/graph">graph</a>
85 <a href="/tags">tags</a>
85 <a href="/tags">tags</a>
86 <a href="/rev/79ee608ca36d">changeset</a>
86 <a href="/rev/79ee608ca36d">changeset</a>
87 <a href="/file/79ee608ca36d/">files</a>
87 <a href="/file/79ee608ca36d/">files</a>
88 <a href="/file/79ee608ca36d/get-with-headers.py">file</a>
88 <a href="/file/79ee608ca36d/get-with-headers.py">file</a>
89 <a href="/log/79ee608ca36d/get-with-headers.py">revisions</a>
89 <a href="/log/79ee608ca36d/get-with-headers.py">revisions</a>
90 <a href="/raw-annotate/79ee608ca36d/get-with-headers.py">raw</a>
90 <a href="/raw-annotate/79ee608ca36d/get-with-headers.py">raw</a>
91 </div>
91 </div>
92
92
93 <h2>Annotate get-with-headers.py</h2>
93 <h2>Annotate get-with-headers.py</h2>
94
94
95 <table>
95 <table>
96 <tr>
96 <tr>
97 <td class="metatag">changeset 0:</td>
97 <td class="metatag">changeset 0:</td>
98 <td><a href="/rev/79ee608ca36d">79ee608ca36d</a></td></tr>
98 <td><a href="/rev/79ee608ca36d">79ee608ca36d</a></td></tr>
99
99
100
100
101 <tr>
101 <tr>
102 <td class="metatag">author:</td>
102 <td class="metatag">author:</td>
103 <td>&#116;&#101;&#115;&#116;</td></tr>
103 <td>&#116;&#101;&#115;&#116;</td></tr>
104 <tr>
104 <tr>
105 <td class="metatag">date:</td>
105 <td class="metatag">date:</td>
106 <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td>
106 <td>Thu Jan 01 00:00:00 1970 +0000 (long ago)</td>
107 </tr>
107 </tr>
108 <tr>
108 <tr>
109 <td class="metatag">permissions:</td>
109 <td class="metatag">permissions:</td>
110 <td>-rwxr-xr-x</td>
110 <td>-rwxr-xr-x</td>
111 </tr>
111 </tr>
112 <tr>
112 <tr>
113 <td class="metatag">description:</td>
113 <td class="metatag">description:</td>
114 <td>a</td>
114 <td>a</td>
115 </tr>
115 </tr>
116 </table>
116 </table>
117
117
118 <br/>
118 <br/>
119
119
120 <table cellspacing="0" cellpadding="0">
120 <table cellspacing="0" cellpadding="0">
121 <tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l1" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l1" id="l1"> 1</a></td><td><pre><span class="c">#!/usr/bin/env python</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l2" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l2" id="l2"> 2</a></td><td><pre></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l3" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l3" id="l3"> 3</a></td><td><pre><span class="n">__doc__</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;This does HTTP get requests given a host:port and path and returns</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l4" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l4" id="l4"> 4</a></td><td><pre><span class="s">a subset of the headers plus the body of the result.&quot;&quot;&quot;</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l5" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l5" id="l5"> 5</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l6" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l6" id="l6"> 6</a></td><td><pre><span class="k">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l7" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l7" id="l7"> 7</a></td><td><pre><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l8" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l8" id="l8"> 8</a></td><td><pre><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l9" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l9" id="l9"> 9</a></td><td><pre><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;GET&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l10" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l10" id="l10"> 10</a></td><td><pre><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l11" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l11" id="l11"> 11</a></td><td><pre><span class="k">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l12" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l12" id="l12"> 12</a></td><td><pre><span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l13" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l13" id="l13"> 13</a></td><td><pre> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l14" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l14" id="l14"> 14</a></td><td><pre> <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l15" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l15" id="l15"> 15</a></td><td><pre><span class="k">print</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l16" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l16" id="l16"> 16</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l17" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l17" id="l17"> 17</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l18" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l18" id="l18"> 18</a></td><td><pre><span class="k">if</span> <span class="mf">200</span> <span class="o">&lt;=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">&lt;=</span> <span class="mf">299</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l19" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l19" id="l19"> 19</a></td><td><pre> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l20" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l20" id="l20"> 20</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></pre></td></tr>
121 <tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l1" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l1" id="l1"> 1</a></td><td><pre><span class="c">#!/usr/bin/env python</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l2" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l2" id="l2"> 2</a></td><td><pre></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l3" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l3" id="l3"> 3</a></td><td><pre><span class="n">__doc__</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;This does HTTP get requests given a host:port and path and returns</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l4" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l4" id="l4"> 4</a></td><td><pre><span class="s">a subset of the headers plus the body of the result.&quot;&quot;&quot;</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l5" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l5" id="l5"> 5</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l6" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l6" id="l6"> 6</a></td><td><pre><span class="k">import</span> <span class="nn">httplib</span><span class="o">,</span> <span class="nn">sys</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l7" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l7" id="l7"> 7</a></td><td><pre><span class="n">headers</span> <span class="o">=</span> <span class="p">[</span><span class="n">h</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">3</span><span class="p">:]]</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l8" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l8" id="l8"> 8</a></td><td><pre><span class="n">conn</span> <span class="o">=</span> <span class="n">httplib</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l9" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l9" id="l9"> 9</a></td><td><pre><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s">&quot;GET&quot;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">2</span><span class="p">])</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l10" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l10" id="l10"> 10</a></td><td><pre><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l11" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l11" id="l11"> 11</a></td><td><pre><span class="k">print</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l12" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l12" id="l12"> 12</a></td><td><pre><span class="k">for</span> <span class="n">h</span> <span class="ow">in</span> <span class="n">headers</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l13" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l13" id="l13"> 13</a></td><td><pre> <span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l14" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l14" id="l14"> 14</a></td><td><pre> <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">getheader</span><span class="p">(</span><span class="n">h</span><span class="p">))</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l15" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l15" id="l15"> 15</a></td><td><pre><span class="k">print</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l16" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l16" id="l16"> 16</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">())</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l17" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l17" id="l17"> 17</a></td><td><pre></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l18" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l18" id="l18"> 18</a></td><td><pre><span class="k">if</span> <span class="mf">200</span> <span class="o">&lt;=</span> <span class="n">response</span><span class="o">.</span><span class="n">status</span> <span class="o">&lt;=</span> <span class="mf">299</span><span class="p">:</span></pre></td></tr><tr class="parity0"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l19" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l19" id="l19"> 19</a></td><td><pre> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">0</span><span class="p">)</span></pre></td></tr><tr class="parity1"><td class="annotate"><a href="/annotate/79ee608ca36d/get-with-headers.py#l20" title="79ee608ca36d: a">test@0</a></td><td><a class="lineno" href="#l20" id="l20"> 20</a></td><td><pre><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mf">1</span><span class="p">)</span></pre></td></tr>
122 </table>
122 </table>
123
123
124
124
125 <div class="logo">
125 <div class="logo">
126 <a href="http://www.selenic.com/mercurial/">
126 <a href="http://www.selenic.com/mercurial/">
127 <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a>
127 <img src="/static/hglogo.png" width=75 height=90 border=0 alt="mercurial"></a>
128 </div>
128 </div>
129
129
130 </body>
130 </body>
131 </html>
131 </html>
132
132
133 % hgweb fileannotate, raw
134
135 % hgweb filerevision, raw
136
133 % hgweb highlightcss friendly
137 % hgweb highlightcss friendly
134 200 Script output follows
138 200 Script output follows
135
139
136 /* pygments_style = friendly */
140 /* pygments_style = friendly */
137
141
138 % errors encountered
142 % errors encountered
139 % hg serve again
143 % hg serve again
140 % hgweb highlightcss fruity
144 % hgweb highlightcss fruity
141 200 Script output follows
145 200 Script output follows
142
146
143 /* pygments_style = fruity */
147 /* pygments_style = fruity */
144
148
145 % errors encountered
149 % errors encountered
General Comments 0
You need to be logged in to leave comments. Login now