/usr/share/uicilibris/url.py is in uicilibris 1.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# $Id$
#
# url.py is part of the package uicilibris
#
# (c) 2011 Georges Khaznadar (georgesk@ofset.org)
#
# This file is part of uicilibris.
# uicilibris is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# uicilibris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with uicilibris. If not, see <http://www.gnu.org/licenses/>.
"""
This file implements web access through a cache
"""
import urllib2, urllib, StringIO, time
class Cache:
"""
a class to implement an Internet cache in memory. Get the unique cache as
Cache.cache()
"""
theCache=None
def __init__(self):
"""
the constructor creates the first and only cache
"""
if Cache.theCache==None:
self.dict={}
Cache.theCache=self
else:
raise IndexError, "no more than one instantiation of Cache can exist"
def cache():
if Cache.theCache:
return Cache.theCache
else:
Cache.theCache=Cache()
return Cache.theCache
cache=staticmethod(cache)
def toCache(url, data, contents):
"""
@param url a valid URL
@param data a data/url-encoded strig, or None
@param contents a string
"""
Cache.theCache.dict[(url, data)]=contents
toCache=staticmethod(toCache)
def fromCache(url, data):
"""
@param url a valid URL
@param data a data/url-encoded strig, or None
@return a file-like objet giving access to the contents, or None
"""
if (url,data) in Cache.theCache.dict.keys():
return StringIO.StringIO(Cache.theCache.dict[(url, data)])
else:
return None
fromCache=staticmethod(fromCache)
def urlopen(url, data=None):
"""
@param url a valid URL
@param data are url-encoded data to post
@result a file-like object giving access to the URL's contents
"""
r=Cache.fromCache(url, data)
if r==None:
time.sleep(0.2) # do not fire too fast, to avoid greylisting.
headers = { 'User-Agent' : "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1",
'Pragma' : "no-cache",
'Referer' : "http://packages.debian.org/sid/uicilibris"
}
nbtries=8
finished=False
contentsOK=True
while not finished:
try:
r = urllib2.urlopen(urllib2.Request(url, data=data, headers=headers), timeout=1)
finished=True
r=StringIO.StringIO(r.read())
except Exception, e:
nbtries=nbtries-1
print "There was an error: %r, still %d tries" %(e, nbtries)
if nbtries==0:
finished=True
contentsOK=False
r=StringIO.StringIO("")
if contentsOK:
# cache the data if the url is open
nbtries=8
finished=False
while not finished:
try:
Cache.toCache(url, data, r.read())
r.seek(0)
finished=True
except Exception, e:
nbtries=nbtries-1
print "There was an error: %r, still %d tries" %(e, nbtries)
if nbtries==0:
finished=True
return r
def quote_plus(text):
"""
@param text
@return the same text with url_quoted encoding
"""
return urllib.quote_plus(text)
def urlencode(data):
"""
@param data a dictioanry of keys/values
@return a string in data/url-encoded format
"""
return urllib.urlencode(data)
dummy=Cache()
if __name__=="__main__":
dummy=Cache()
Cache.toCache("a","b","the contents")
r = Cache.fromCache("a", "b")
if r:
print r.read()
else:
print "noting cached"
r = Cache.fromCache("a", "c")
if r:
print r.read()
else:
print "noting cached"
|