-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.py
More file actions
162 lines (110 loc) · 4.59 KB
/
objects.py
File metadata and controls
162 lines (110 loc) · 4.59 KB
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
154
155
156
157
158
159
160
161
162
# This program 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 3 of the License, or
# (at your option) any later version.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
# import json
# from requests.compat import urljoin
# from internals import _get_sorter
class LEXContentObject(object):
"""Base class that represents actual LEX objects."""
def __init__(self,lex_session, json_dict=None, fetch=True):
self.lex_session= lex_session
self.has_fetched = self._populate(json_dict, fetch)
def __eq__(self, other):
return (isinstance(other, LEXContentObject) and self.name == other.name)
def __ne__(self, other):
return not (self == other)
def __getattr__(self, attr):
if not self.has_fetched:
self.has_fetched = self._populate(None, True)
return getattr(self, attr)
raise AttributeError('\'%s\' has no attribute \'%s\'' % (type(self),
attr))
def __str__(self):
strname = self.__unicode__()
strname = strname.encode('utf-8')
return strname
def __setattr__(self, name, value):
super(LEXContentObject, self).__setattr__(name, value)
def from_api_response(cls, lex_session, json_dict):
"""Return an instance of the appropriate class from the json_dict."""
return cls(lex_session, json_dict=json_dict)
def _populate(self, json_dict, fetch):
if json_dict is None:
json_dict = {}
for name, value in json_dict.iteritems():
setattr(self, name, value)
return bool(json_dict) or fetch
class Plugin(LEXContentObject):
"""A class for plugins uploaded to LEX"""
def __init__(self, lex_session, json_dict):
super(Plugin, self).__init__(lex_session, json_dict)
def __repr__(self):
return 'Plugin(name=\'{0}\', version=\'{1}\', author=\'{2}\')'.format(self.name,
self.version, self.author)
def __unicode__(self):
fullname = self.fullname.replace('\r\n', ' ')
return fullname
# @staticmethod
# def from_url(lex_session, url):
# pass
@property
def fullname(self):
"""Returns the object's fullname
The full name consists of the name like 'N.A.M' followed by
the version in parenthesis like '(v. 1.0)' followed by
the author such as 'by NAM Team'
"""
return '%s (v. %s) by %s' % (self.name, self.version, self.author)
def from_url(lex_session, url):
"""Request the URL and return a Plugin object.
Requires parsing HTML to get data."""
pass
# params = {}
# plugin_info = reddit_session, request_json(url, params=params)
class Category(LEXContentObject):
"""A class representing a LEX category.
These are somewhat fake as you can't query categories on LEX currently
but it is structured this way to make for more intuitive usage
such as c = l.get_category('maps').get_recent(limit=10).
"""
def __init__(self, lex_session, name):
super(Category, self).__init__(lex_session, None, False)
if name in lex_session.config.BROAD_CATEGORIES:
self.name = lex_session.config.BROAD_CATEGORIES[name]
else:
raise TypeError('`s` is not a valid category.')
self._url = lex_session.config['search']
self._params = {'broad_type': self.name}
def __str__(self):
return self.config.BROAD_CATEGORIES[name]
def __repr__(self):
return 'Category(name=`{0}`)'.format(self.name)
get_recent = _get_sorter('recent')
get_updated = _get_sorter('update')
get_popular = _get_sorter('popular')
class User(LEXContentObject):
"""A class representing a LEX user."""
def __init__(self, lex_session, json_dict, user_name=None, user_id=None):
super(User, self).__init__(lex_session, json_dict, False)
self._url = lex_session.config['search']
self._params = {'creator': self.id}
def __unicode__(self):
return self.user_name
get_uploaded = get_uploaded_by_recent = _get_sorter('recent')
get_uploaded_by_popular = _get_sorter('popular')
get_uploaded_by_updated = _get_sorter('update')
# get_uploaded = _get_user_plugins('uploaded')
# get_downloaded = _get_user_plugins('downloaded')
def _get_user_info(user_name=None, user_id=None):
"""Find user_name or user_id from LEX."""
if bool(user_name) == bool(user_id):
raise TypeError('One (and only one) of user_name or user_id is required!')
class LoggedInUser(User):
"""A class representing an authenticated LEX user."""