MythTV  master
request.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # smolt - Fedora hardware profiler
3 #
4 # Copyright (C) 2011 Raymond Wagner <sebastian@pipping.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 # This sets up a factory for urllib2.Request objects, automatically
21 # providing the base url, user agent, and proxy information.
22 # The object returned is slightly modified, with a shortcut to urlopen.
23 
24 from builtins import object
25 
26 try:
27  import urllib2 as urllib
28 except ImportError:
29  import urllib.request as urllib
30 
31 try:
32  import urlparse as parse
33 except ImportError:
34  from urllib import parse
35 
36 class _Request( urllib.Request ):
37  timeout = None
38  def open(self):
39  if self.timeout:
40  return urllib.urlopen(self, None, self.timeout)
41  return urllib.urlopen(self)
42 
43 class _RequestFactory( object ):
44  def __init__(self, baseurl, user_agent, timeout, proxy):
45  self.base_url = baseurl
46  self.user_agent = user_agent
47  self.timeout = timeout
48  self.proxy = proxy
49 
50  def __call__(self, *args, **kwargs):
51  return self.new_request(*args, **kwargs)
52 
53  def new_request(self, url):
54  url = parse.urljoin(self.base_url, url)
55  req = _Request(url)
56  req.timeout = self.timeout
57  if self.proxy:
58  req.set_proxy(self.proxy, 'http')
59  if self.user_agent:
60  req.add_header('User-Agent', self.user_agent)
61  return req
62 
63 _request = None
64 
65 def ConnSetup(baseurl, user_agent=None, timeout=120, proxy=None):
66  global _request
67  if _request is None:
68  _request = _RequestFactory(baseurl, user_agent, timeout, proxy)
69 
70 def Request(url):
71  global _request
72  if _request is None:
73  raise Exception("Request Factory not yet spawned")
74  return _request(url)
hardwareprofile.request._RequestFactory.new_request
def new_request(self, url)
Definition: request.py:53
hardwareprofile.request._RequestFactory.timeout
timeout
Definition: request.py:47
hardwareprofile.request._request
_request
Definition: request.py:63
hardwareprofile.request._RequestFactory.__call__
def __call__(self, *args, **kwargs)
Definition: request.py:50
hardwareprofile.request._Request
Definition: request.py:36
hardwareprofile.request.ConnSetup
def ConnSetup(baseurl, user_agent=None, timeout=120, proxy=None)
Definition: request.py:65
hardwareprofile.request._RequestFactory.proxy
proxy
Definition: request.py:48
hardwareprofile.request._RequestFactory.__init__
def __init__(self, baseurl, user_agent, timeout, proxy)
Definition: request.py:44
hardwareprofile.request._RequestFactory
Definition: request.py:43
hardwareprofile.request._Request.timeout
timeout
Definition: request.py:37
hardwareprofile.request._RequestFactory.user_agent
user_agent
Definition: request.py:46
hardwareprofile.request.Request
def Request(url)
Definition: request.py:70
hardwareprofile.request._Request.open
def open(self)
Definition: request.py:38
hardwareprofile.request._RequestFactory.base_url
base_url
Definition: request.py:45