Debian Codegraph @

yarg/parse.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (The MIT License)
4 #
5 # Copyright (c) 2014 Kura
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the 'Software'), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 from datetime import datetime
26 import xml.etree.ElementTree
27
28 import requests
29
30 from .exceptions import HTTPError
31
32
33 def _get(pypi_server):
34 """
35 Query the PyPI RSS feed and return a list
36 of XML items.
37 """
38 response = requests.get(pypi_server)
39 if response.status_code >= 300:
40 raise HTTPError(status_code=response.status_code,
41 reason=response.reason)
42 if hasattr(response.content, 'decode'):
43 tree = xml.etree.ElementTree.fromstring(response.content.decode())
44 else:
45 tree = xml.etree.ElementTree.fromstring(response.content)
46 channel = tree.find('channel')
47 return channel.findall('item')
48
49
50 def newest_packages(
51 pypi_server="https://pypi.python.org/pypi?%3Aaction=packages_rss"):
52 """
53 Constructs a request to the PyPI server and returns a list of
54 :class:`yarg.parse.Package`.
55
56 :param pypi_server: (option) URL to the PyPI server.
57
58 >>> import yarg
59 >>> yarg.newest_packages()
60 [<Package yarg>, <Package gray>, <Package ragy>]
61 """
62 items = _get(pypi_server)
63 i = []
64 for item in items:
65 i_dict = {'name': item[0].text.split()[0],
66 'url': item[1].text,
67 'description': item[3].text,
68 'date': item[4].text}
69 i.append(Package(i_dict))
70 return i
71
72
73 def latest_updated_packages(
74 pypi_server="https://pypi.python.org/pypi?%3Aaction=rss"):
75 """
76 Constructs a request to the PyPI server and returns a list of
77 :class:`yarg.parse.Package`.
78
79 :param pypi_server: (option) URL to the PyPI server.
80
81 >>> import yarg
82 >>> yarg.latest_updated_packages()
83 [<Package yarg>, <Package gray>, <Package ragy>]
84 """
85 items = _get(pypi_server)
86 i = []
87 for item in items:
88 name, version = item[0].text.split()
89 i_dict = {'name': name,
90 'version': version,
91 'url': item[1].text,
92 'description': item[2].text,
93 'date': item[3].text}
94 i.append(Package(i_dict))
95 return i
96
97
98 class Package(object):
99 """
100 A PyPI package generated from the RSS feed information.
101
102 :param pypi_dict: A dictionary retrieved from the PyPI server.
103 """
104
105 def __init__(self, pypi_dict):
106 self._content = pypi_dict
107
108 def __repr__(self):
109 return "<Package {0}>".format(self.name)
110
111 @property
112 def name(self):
113 """
114 >>> package = yarg.newest_packages()[0]
115 >>> package.name
116 'yarg'
117 >>> package = yarg.latest_updated_packages()[0]
118 >>> package.name
119 'yarg'
120 """
121 return self._content['name']
122
123 @property
124 def version(self):
125 """
126 >>> package = yarg.newest_packages()[0]
127 >>> package.name
128 'yarg'
129 >>> package = yarg.latest_updated_packages()[0]
130 >>> package.name
131 'yarg'
132 """
133 if 'version' not in self._content:
134 return None
135 return self._content['version']
136
137 @property
138 def url(self):
139 """
140 This is only available for :meth:`yarg.latest_updated_packages`, for
141 :meth:`yarg.newest_packages` will return `None`
142
143 >>> package = yarg.latest_updated_packages()[0]
144 >>> package.url
145 'http://pypi.python.org/pypi/yarg'
146 """
147 return self._content['url']
148
149 @property
150 def date(self):
151 """
152 >>> package = yarg.newest_packages()[0]
153 >>> package.date
154 datetime.datetime(2014, 8, 9, 8, 40, 20)
155 >>> package = yarg.latest_updated_packages()[0]
156 >>> package.date
157 datetime.datetime(2014, 8, 9, 8, 40, 20)
158 """
159 return datetime.strptime(self._content['date'],
160 "%d %b %Y %H:%M:%S %Z")
161
162 @property
163 def description(self):
164 """
165 >>> package = yarg.newest_packages()[0]
166 >>> package.description
167 'Some random summary stuff'
168 >>> package = yarg.latest_updated_packages()[0]
169 >>> package.description
170 'Some random summary stuff'
171 """
172 return self._content['description']