From dd8076b16e4c04a510dc8fe8fe8655f5e350014f Mon Sep 17 00:00:00 2001 From: James French Date: Thu, 5 Aug 2010 13:56:01 +0800 Subject: [PATCH] Handles MIME encodings from FTP now too * Provided the remote end specifies what it is. * Default behaviour is to follow the RFC ie use remote if given or assume UTF-8 --- icalparse.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/icalparse.py b/icalparse.py index 47db225..293295c 100755 --- a/icalparse.py +++ b/icalparse.py @@ -125,22 +125,21 @@ def getContent(url='',stdin=False): 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 not parsedURL[0]: url = 'file://' + os.path.abspath(url) # If we've survived, use python's generic URL opening library to handle it import urllib2 try: res = urllib2.urlopen(url) content = res.read() + if 'content-type' in res.info(): ct = res.info()['content-type'] + else: ct = '' res.close() except (urllib2.URLError, OSError), e: sys.stderr.write('%s\n'%e) sys.exit(1) + + encoding = 'charset' in ct and ct.split(';')[-1].lower().split('=')[-1].strip() or '' return (content, encoding) @@ -154,6 +153,7 @@ def getHTTPContent(url='',cache='.httplib2-cache'): import urllib2 if not url: return ('','') + if not 'http' in parsedURL[0]: return ('','') if 'httplib2' in sys.modules: try: h = httplib2.Http('.httplib2-cache') -- 2.20.1