willb (owner)

Revisions

gist: 14500 Download_button fork
public
Public Clone URL: git://gist.github.com/14500.git
Embed All Files: show embed
addmint.py #
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# encoding: utf-8
"""
addmint.py
 
Adds Mint analytics to a static HTML page; requires BeautifulSoup.
 
(I use this for Aperture exports.)
 
Created by Will Benton on 2008-07-15.
Copyright (c) 2008 Aition Technologies, LLC and Will Benton. All rights reserved.
"""
 
import sys
import getopt
from BeautifulSoup import BeautifulSoup, Tag, Comment
 
help_message = '''
The help message goes here.
'''
# edit this next line or else pass the --url option
minturl = "http://YOURSERVER.com/mint/"
files = []
 
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
 
def process(filename):
global minturl
 
f = open(filename)
lines = f.read()
 
soup = BeautifulSoup(''.join(lines))
 
head = soup.head
mtag = Tag(soup, "script")
mtag["src"] = "%s/?js" % minturl
mtag["type"] = "text/javascript"
 
head.insert(0, mtag)
 
f.close()
 
f = open(filename, "w")
f.write(soup.prettify())
f.close()
 
def main(argv=None):
global minturl, files
 
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hu:v", ["help", "url="])
except getopt.error, msg:
raise Usage(msg)
 
# option processing
for option, value in opts:
if option == "-v":
verbose = True
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-u", "--url"):
minturl = value
 
for f in args:
process(f)
 
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
 
 
if __name__ == "__main__":
sys.exit(main())