Much better unicode support
[frenchie/icalparse.git] / icalparse.py
index 5010d1d..6c3aac6 100755 (executable)
 # THE SOFTWARE.
 
 import sys
-import re
 import urlparse
 import os
 
-class InvalidICS(Exception): pass
-class notJoined(Exception): pass
 
-icalEntry = re.compile('^[A-Z\-]+:.*')
+class InvalidICS(Exception): pass
+class IncompleteICS(InvalidICS): pass
 
-def lineJoiner(oldcal):
+def lineJoiner(oldcal, encoding='utf-8'):
        '''Takes a string containing a calendar and returns an array of its lines'''
+       try:
+               oldcal = unicode(oldcal, encoding)
+               oldcal = oldcal.replace('\r\n ', '').replace('\r\n\t','')
+       except UnicodeDecodeError:
+               # This is probably a file with badly folded lines
+               oldcal = oldcal.replace('\r\n ', '').replace('\r\n\t','')
+               try: oldcal = unicode(oldcal, encoding)
+               except UnicodeDecodeError:
+                       sys.stderr.write('Malformed File')
+                       raise
+
+       if not oldcal[0:15] == 'BEGIN:VCALENDAR':
+               raise InvalidICS, "Does not appear to be a valid ICS file"
+
+       if not 'END:VCALENDAR' in oldcal[-15:-1]:
+               raise IncompleteICS, "File appears to be incomplete"
 
        if list(oldcal) == oldcal:
                oldcal = '\r\n'.join(oldcal)
 
-       oldcal.replace('\r\n ', '')
        return oldcal.split('\r\n')
 
 
@@ -49,22 +62,196 @@ def lineFolder(oldcal, length=75):
        cal = []
        sl = length - 1
 
-       for line in oldcal:
+       for uline in oldcal:
+               line = uline.encode('utf-8')
+
                # Line fits inside length, do nothing
-               if len(line.rstrip()) <= length:
+               if len(line) <= length:
                        cal.append(line)
+
                else:
-                       brokenline = [line[0:length] + '\r\n']
                        ll = length
-                       while ll < len(line.rstrip('\r\n')) + 1:
-                               brokenline.append(' ' + line[ll:sl+ll].rstrip('\r\n') + '\r\n')
-                               ll += sl
-                       cal += brokenline
+                       foldedline = []
+                       while uline:
+                               ufold = unicode(line[0:ll], 'utf-8', 'ignore')
+                               fold = ufold.encode('utf-8')
+                               uline = uline.replace(ufold,u'',1)
+                               line = uline.encode('utf-8')
+                               foldedline.append(fold)
+
+                               # Subsequent lines are shorter as they include a space
+                               ll = length - 1
+                       cal.append('\r\n '.join(foldedline))
 
        return cal
 
+
+def splitFields(cal):
+       '''Takes a list of lines in a calendar file and returns a list of tuples
+       as (key, value) pairs'''
+
+       ical = []
+
+       # Check that we got 2 items on every line
+       for line in [tuple(x.split(':',1)) for x in cal]:
+               if not len(line) == 2 and line[0]:
+                       raise InvalidICS, 'Unusual content line: %s'%line
+               elif line[0]:
+                       ical.append(line)
+
+       return ical
+
+
+def joinFields(ical):
+       '''Takes a list of tuples that make up a calendar file and returns it to a
+       list of lines'''
+
+       return [':'.join(x) for x in ical]
+
+
 def getContent(url='',stdin=False):
-       pass
+       '''Generic content retriever, DO NOT use this function in a CGI script as
+       it can read from the local disk (which you probably don't want it to).
+       '''
+
+       encoding = '' # If we don't populate this, the script will assume UTF-8
+
+       # Special case, if this is a HTTP url, return the data from it using
+       # the HTTP functions which attempt to play a bit nicer.
+       parsedURL = urlparse.urlparse(url)
+       if 'http' in parsedURL[0]: return getHTTPContent(url)
+
+       if stdin:
+               content = sys.stdin.read()
+               return (content, encoding)
+
+       if not parsedURL[0]:
+               try: content = open(os.path.abspath(url),'r').read()
+               except (IOError, OSError), e:
+                       sys.stderr.write('%s\n'%e)
+                       sys.exit(1)
+               return (content, encoding)
+
+       # If we've survived, use python's generic URL opening library to handle it
+       import urllib2
+       try:
+               res = urllib2.urlopen(url)
+               content = res.read()
+               res.close()
+       except (urllib2.URLError, OSError), e:
+               sys.stderr.write('%s\n'%e)
+               sys.exit(1)
+       return (content, encoding)
+
+
+def getHTTPContent(url='',cache='.httplib2-cache'):
+       '''This function attempts to play nice when retrieving content from HTTP
+       services. It's what you should use in a CGI script.'''
+
+       try:
+               import httplib2
+       except ImportError:
+               import urllib2
+
+       if not url: return ''
+
+       encoding = '' # If we don't populate this, the script will assume UTF-8
+
+       if 'httplib2' in sys.modules:
+               try: h = httplib2.Http('.httplib2-cache')
+               except OSError: h = httplib2.Http()
+       else: h = False
+
+       try:
+               if h:
+                       req = h.request(url)
+                       content = req[1]
+                       if 'content-type' in req[0]:
+                               for ct in req[0]['content-type'].split(';'):
+                                       ct = ct.lower()
+                                       if 'charset' in ct:
+                                               encoding = ct.split('=')[1].strip()
+               return (content, encoding)
+       except ValueError, e:
+               sys.stderr.write('%s\n'%e)
+               sys.exit(1)
+
+       try:
+               content = urllib2.urlopen(url).read()
+               return (content, encoding)
+       except (urllib2.URLError, OSError), e:
+               sys.stderr.write('%s\n'%e)
+               sys.exit(1)
+
+       return ('', '')
+
+
+def generateRules():
+       '''Attempts to load a series of rules into a list'''
+       try:
+               import parserrules
+       except ImportError:
+               return []
+
+       rules = [getattr(parserrules, rule) for rule in dir(parserrules) if callable(getattr(parserrules, rule))]
+       return rules
+
+
+def applyRules(ical, rules=[], verbose=False):
+       'Runs a series of rules on the lines in ical and mangles its output'
+
+       for rule in rules:
+               output = []
+               if rule.__doc__ and verbose:
+                       print(rule.__doc__)
+               for line in ical:
+                       try:
+                               out = rule(line[0],line[1])
+                       except TypeError, e:
+                               output.append(line)
+                               print(e)
+                               continue
+
+                       # Drop lines that are boolean False
+                       if not out and not out == None: continue
+
+                       # If the rule did something and is a tuple or a list we'll accept it
+                       # otherwise, pay no attention to the man behind the curtain
+                       try:
+                               if tuple(out) == out or list(out) == out and len(out) == 2:
+                                       output.append(tuple(out))
+                               else:
+                                       output.append(line)
+                       except TypeError, e:
+                               output.append(line)
+
+               ical = output
+
+       return ical
+
+
+def writeOutput(cal, outfile=''):
+       '''Takes a list of lines and outputs to the specified file'''
+
+       if not cal:
+               sys.stderr.write('Refusing to write out an empty file')
+               sys.exit(0)
+
+       if not outfile:
+               out = sys.stdout
+       else:
+               try:
+                       out = open(outfile, 'w')
+               except (IOError, OSError), e:
+                       sys.stderr.write('%s\n'%e)
+                       sys.exit(1)
+
+       if cal[-1]: cal.append('')
+
+       out.write('\r\n'.join(cal))
+
+       if not out == sys.stdout:
+               out.close()
 
 
 if __name__ == '__main__':
@@ -75,8 +262,13 @@ if __name__ == '__main__':
        parser = OptionParser('usage: %prog [options] url')
        parser.add_option('-s', '--stdin', action='store_true', dest='stdin',
                default=False, help='Take a calendar from standard input')
+       parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
+               default=False, help='Be verbose when rules are being applied')
        parser.add_option('-o', '--output', dest='outfile', default='',
                help='Specify output file (defaults to standard output)')
+       parser.add_option('-m','--encoding', dest='encoding', default='',
+               help='Specify a different character encoding'
+               '(ignored if the remote server also specifies one)')
 
        (options, args) = parser.parse_args()
 
@@ -88,43 +280,9 @@ if __name__ == '__main__':
        else:
                url = ''
 
-       # Work out what url parsers we're going to need based on what the user
-       # gave us on the command line - we do like files after all
-       parsedURL = urlparse.urlparse(url)
-       http = 'http' in parsedURL[0]
-
-       if not parsedURL[0]: u = False
-       else: u = True
-
-       if not options.stdin and http:
-               try:
-                       import httplib2
-               except ImportError:
-                       import urllib2
-
-       # Try and play nice with HTTP servers unless something goes wrong. We don't
-       # really care about this cache (A lot of ics files seem to be generated with
-       # php which hates caching with a passion).
-       h = False
-       if 'httplib2' in sys.modules:
-               try: h = httplib2.Http('.httplib2-cache')
-               except OSError: h = httplib2.Http()
-
-       # Load urllib2 if this is not a stdin
-       if not options.stdin and (not http or not 'httplib2' in sys.modules):
-               import urllib2
-
-       try:
-               content = u and (h and h.request(url)[1] or urllib2.urlopen(url).read())
-       except (ValueError, urllib2.URLError), e:
-               sys.stderr.write('%s\n'%e)
-               sys.exit(1)
-
-       if not u and not options.stdin:
-               try: content = open(os.path.abspath(url),'r').read()
-               except (IOError, OSError), e:
-                       sys.stderr.write('%s\n'%e)
-                       sys.exit(1)
-
-       if options.stdin:
-               content = sys.stdin.read()
+       (content, encoding) = getContent(url, options.stdin)
+       encoding = encoding or options.encoding or 'utf-8'
+       cal = lineJoiner(content, encoding)
+       ical = applyRules(splitFields(cal), generateRules(), options.verbose)
+       output = lineFolder(joinFields(ical))
+       writeOutput(output, options.outfile)

UCC git Repository :: git.ucc.asn.au