5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 from cgi import parse_header
30 def getContent(url='',stdin=False):
31 '''Generic content retriever, DO NOT use this function in a CGI script as
32 it can read from the local disk (which you probably don't want it to).
35 encoding = '' # If we don't populate this, the script will assume UTF-8
37 # Special case, if this is a HTTP url, return the data from it using
38 # the HTTP functions which attempt to play a bit nicer.
39 parsedURL = urlparse.urlparse(url)
40 if 'http' in parsedURL[0]: return getHTTPContent(url)
43 content = sys.stdin.read()
44 return (content, encoding)
46 if not parsedURL[0]: url = 'file://' + os.path.abspath(url)
48 # If we've survived, use python's generic URL opening library to handle it
51 res = urllib2.urlopen(url)
53 ct = res.info().getplist()
55 except (urllib2.URLError, OSError), e:
56 sys.stderr.write('%s\n'%e)
60 if 'charset' in param:
61 encoding = param.split('=')[1]
64 return (content, encoding)
67 def getHTTPContent(url='',cache='.httplib2-cache'):
68 '''This function attempts to play nice when retrieving content from HTTP
69 services. It's what you should use in a CGI script.'''
76 if not url: return ('','')
78 if not 'http' in urlparse.urlparse(url)[0]: return ('','')
80 if 'httplib2' in sys.modules:
81 try: h = httplib2.Http('.httplib2-cache')
82 except OSError: h = httplib2.Http()
89 sys.stderr.write('%s\n'%e)
93 if 'content-type' in resp:
94 ct = 'Content-Type: %s'%req[0]['content-type']
96 if 'charset' in ct[1]: encoding = ct[1]['charset']
104 req = urllib2.urlopen(url)
105 except urllib2.URLError, e:
106 sys.stderr.write('%s\n'%e)
110 ct = req.info().getplist()
112 if 'charset' in param:
113 encoding = param.split('=')[1]
116 return (content, encoding)
120 '''Attempts to load a series of rules into a list'''
126 rules = [getattr(parserrules, rule) for rule in dir(parserrules) if callable(getattr(parserrules, rule))]
130 def applyRules(cal, rules=[], verbose=False):
131 'Runs a series of rules on the lines in ical and mangles its output'
138 def writeOutput(cal, outfile=''):
139 '''Takes a list of lines and outputs to the specified file'''
142 sys.stderr.write('Refusing to write out an empty file')
149 out = open(outfile, 'w')
150 except (IOError, OSError), e:
151 sys.stderr.write('%s\n'%e)
156 if not out == sys.stdout:
159 if __name__ == '__main__':
160 from optparse import OptionParser
161 # If the user passed us a 'stdin' argument, we'll go with that,
162 # otherwise we'll try for a url opener
164 parser = OptionParser('usage: %prog [options] url')
165 parser.add_option('-s', '--stdin', action='store_true', dest='stdin',
166 default=False, help='Take a calendar from standard input')
167 parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
168 default=False, help='Be verbose when rules are being applied')
169 parser.add_option('-o', '--output', dest='outfile', default='',
170 help='Specify output file (defaults to standard output)')
171 parser.add_option('-m','--encoding', dest='encoding', default='',
172 help='Specify a different character encoding'
173 '(ignored if the remote server also specifies one)')
175 (options, args) = parser.parse_args()
177 if not args and not options.stdin:
180 elif not options.stdin:
185 (content, encoding) = getContent(url, options.stdin)
186 encoding = encoding or options.encoding or 'utf-8'
188 cal = vobject.readOne(unicode(content, encoding))
189 cal = applyRules(cal, generateRules(), options.verbose)
191 writeOutput(cal, options.outfile)