Module dataurl
[hide private]
[frames] | no frames]

Source Code for Module dataurl

 1  #============================================================================  
 2  #Name        : dataurl.py  
 3  #Part of     : Helium  
 4   
 5  #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
 6  #All rights reserved. 
 7  #This component and the accompanying materials are made available 
 8  #under the terms of the License "Eclipse Public License v1.0" 
 9  #which accompanies this distribution, and is available 
10  #at the URL "http://www.eclipse.org/legal/epl-v10.html". 
11  # 
12  #Initial Contributors: 
13  #Nokia Corporation - initial contribution. 
14  # 
15  #Contributors: 
16  # 
17  #Description: 
18  #=============================================================================== 
19   
20  """ This module implements method to create dataurl """ 
21  import mimetypes 
22  import base64 
23  import urllib 
24   
25 -def from_url(url):
26 """ This function returns a data url using content pointed by url. """ 27 (mimetype, encoding) = mimetypes.guess_type(url) 28 if mimetype == None: 29 return url 30 if encoding != None: 31 encoding = "charset=%s;" % encoding 32 else: 33 encoding = "" 34 data = urllib.urlopen(url).read() 35 return "data:%s;%sbase64,%s" % (mimetype, encoding, base64.encodestring(data).replace("\n",""))
36