MythTV  master
distros/mythtv_data/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 future import standard_library
25 standard_library.install_aliases()
26 from builtins import object
27 import urllib.request, urllib.error, urllib.parse
28 import urllib.parse
29 
30 class _Request( urllib.request.Request ):
31  timeout = None
32  def open(self):
33  if self.timeout:
34  return urllib.request.urlopen(self, None, self.timeout)
35  return urllib.request.urlopen(self)
36 
37 class _RequestFactory( object ):
38  def __init__(self, baseurl, user_agent, timeout, proxy):
39  self.base_url = baseurl
40  self.user_agent = user_agent
41  self.timeout = timeout
42  self.proxy = proxy
43 
44  def __call__(self, *args, **kwargs):
45  return self.new_request(*args, **kwargs)
46 
47  def new_request(self, url):
48  url = urllib.parse.urljoin(self.base_url, url)
49  req = _Request(url)
50  req.timeout = self.timeout
51  if self.proxy:
52  req.set_proxy(self.proxy, 'http')
53  if self.user_agent:
54  req.add_header('User-Agent', self.user_agent)
55  return req
56 
57 _request = None
58 
59 def ConnSetup(baseurl, user_agent=None, timeout=120, proxy=None):
60  global _request
61  if _request is None:
62  _request = _RequestFactory(baseurl, user_agent, timeout, proxy)
63 
64 def Request(url=None):
65  global _request
66  if _request is None:
67  raise Exception("Request Factory not yet spawned")
68  if url:
69  return _request(url)
70  return _request.base_url
hardwareprofile.distros.mythtv_data.request._RequestFactory.timeout
timeout
Definition: distros/mythtv_data/request.py:41
hardwareprofile.distros.mythtv_data.request._RequestFactory.user_agent
user_agent
Definition: distros/mythtv_data/request.py:40
hardwareprofile.distros.mythtv_data.request._RequestFactory
Definition: distros/mythtv_data/request.py:37
hardwareprofile.distros.mythtv_data.request._RequestFactory.proxy
proxy
Definition: distros/mythtv_data/request.py:42
hardwareprofile.distros.mythtv_data.request._request
_request
Definition: distros/mythtv_data/request.py:57
hardwareprofile.distros.mythtv_data.request._RequestFactory.__call__
def __call__(self, *args, **kwargs)
Definition: distros/mythtv_data/request.py:44
hardwareprofile.distros.mythtv_data.request._Request
Definition: distros/mythtv_data/request.py:30
hardwareprofile.distros.mythtv_data.request._RequestFactory.new_request
def new_request(self, url)
Definition: distros/mythtv_data/request.py:47
hardwareprofile.distros.mythtv_data.request.ConnSetup
def ConnSetup(baseurl, user_agent=None, timeout=120, proxy=None)
Definition: distros/mythtv_data/request.py:59
hardwareprofile.distros.mythtv_data.request.Request
def Request(url=None)
Definition: distros/mythtv_data/request.py:64
hardwareprofile.distros.mythtv_data.request._Request.open
def open(self)
Definition: distros/mythtv_data/request.py:32
hardwareprofile.distros.mythtv_data.request._RequestFactory.__init__
def __init__(self, baseurl, user_agent, timeout, proxy)
Definition: distros/mythtv_data/request.py:38
hardwareprofile.distros.mythtv_data.request._Request.timeout
timeout
Definition: distros/mythtv_data/request.py:31
hardwareprofile.distros.mythtv_data.request._RequestFactory.base_url
base_url
Definition: distros/mythtv_data/request.py:39