printnode-community 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- printnode_community/__init__.py +13 -0
- printnode_community/accounts.py +228 -0
- printnode_community/auth.py +224 -0
- printnode_community/computers.py +317 -0
- printnode_community/gateway.py +105 -0
- printnode_community/model.py +279 -0
- printnode_community/util.py +11 -0
- printnode_community-0.3.0.dist-info/METADATA +546 -0
- printnode_community-0.3.0.dist-info/RECORD +11 -0
- printnode_community-0.3.0.dist-info/WHEEL +4 -0
- printnode_community-0.3.0.dist-info/licenses/LICENSE +22 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
from .model import Computer, Model, Printer, PrintJob, Scale, State
|
|
2
|
+
from future.types import newbytes
|
|
3
|
+
import json
|
|
4
|
+
import base64 as base_64
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Computers:
|
|
9
|
+
|
|
10
|
+
def __init__(self, auth, factory):
|
|
11
|
+
self._auth = auth
|
|
12
|
+
self._factory = factory
|
|
13
|
+
|
|
14
|
+
def get_computers(self, computer=None, limit=None, after=None, dir=None):
|
|
15
|
+
|
|
16
|
+
params = self._create_pagination_params(limit, after, dir)
|
|
17
|
+
|
|
18
|
+
if self._is_multi_query(computer):
|
|
19
|
+
url = '/computers'
|
|
20
|
+
if params is not None:
|
|
21
|
+
url = url + '?' + params
|
|
22
|
+
computers = self._factory.create_computers(self._auth.get(url))
|
|
23
|
+
if isinstance(computer, str):
|
|
24
|
+
return [c for c in computers if c.name == computer]
|
|
25
|
+
else:
|
|
26
|
+
return computers
|
|
27
|
+
else:
|
|
28
|
+
computer_id = self._get_computer_id(computer)
|
|
29
|
+
url = '/computers/{}'.format(computer_id)
|
|
30
|
+
if params is not None:
|
|
31
|
+
url = url + '?' + params
|
|
32
|
+
results = self._auth.get(url)
|
|
33
|
+
if len(results) == 0:
|
|
34
|
+
raise LookupError(
|
|
35
|
+
'computer not found with ID {}'.format(computer_id))
|
|
36
|
+
assert len(results) == 1
|
|
37
|
+
computers = self._factory.create_computers(results)
|
|
38
|
+
return computers[0]
|
|
39
|
+
|
|
40
|
+
def get_scales(self, computer, dev_name=None, dev_num=None):
|
|
41
|
+
url = '/computer/'+str(computer)+'/scales'
|
|
42
|
+
if dev_name is not None:
|
|
43
|
+
url = url+'/'+dev_name
|
|
44
|
+
if dev_num is not None:
|
|
45
|
+
url = url+'/'+str(dev_num)
|
|
46
|
+
if dev_num is not None and dev_name is None:
|
|
47
|
+
temp_str = 'Device num stated without name - nothing found.'
|
|
48
|
+
raise LookupError(temp_str)
|
|
49
|
+
scales = self._factory.create_scales(self._auth.get(url))
|
|
50
|
+
return scales
|
|
51
|
+
|
|
52
|
+
def get_states(self, pjob_set=None, limit=None, after=None, dir=None):
|
|
53
|
+
|
|
54
|
+
pjob_set = str(pjob_set)+"/" if pjob_set else ""
|
|
55
|
+
url = "/printjobs/"+pjob_set+"states"
|
|
56
|
+
params = self._create_pagination_params(limit, after, dir)
|
|
57
|
+
if params is not None:
|
|
58
|
+
url = url + '?' + params
|
|
59
|
+
states = self._factory.create_states_map(
|
|
60
|
+
self._auth.get(url))
|
|
61
|
+
|
|
62
|
+
return states
|
|
63
|
+
|
|
64
|
+
def get_printers(self, computer=None, printer=None, limit=None, after=None, dir=None):
|
|
65
|
+
"""queries API for printers.
|
|
66
|
+
the printer argument can be:
|
|
67
|
+
* id of the printer, in which case a single printer is returned
|
|
68
|
+
* name of the printer, in which case a single printer is returned
|
|
69
|
+
* unspecified in which case a list of all printers is returned
|
|
70
|
+
the computer argument can be:
|
|
71
|
+
* id of the computer in which case only printers that are attached
|
|
72
|
+
to that computer are returned
|
|
73
|
+
* unspecified in which case all printers attached to the account
|
|
74
|
+
are considered.
|
|
75
|
+
raises LookupError if querying for a specific printer
|
|
76
|
+
that doesn't exist
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
params = self._create_pagination_params(limit, after, dir)
|
|
80
|
+
|
|
81
|
+
if self._is_multi_query(printer):
|
|
82
|
+
computer_ids = ','.join(map(str, self._get_computer_ids(computer)))
|
|
83
|
+
url = '/computers/{}/printers'.format(computer_ids)
|
|
84
|
+
if params is not None:
|
|
85
|
+
url = url + '?' + params
|
|
86
|
+
printers = self._factory.create_printers(self._auth.get(url))
|
|
87
|
+
assert all(isinstance(p, Printer) for p in printers)
|
|
88
|
+
if isinstance(printer, str):
|
|
89
|
+
return [p for p in printers if p.name == printer]
|
|
90
|
+
else:
|
|
91
|
+
return printers
|
|
92
|
+
else:
|
|
93
|
+
printer_id = self._get_printer_id(printer)
|
|
94
|
+
url = '/printers/{}'.format(printer_id)
|
|
95
|
+
if params is not None:
|
|
96
|
+
url = url + '?' + params
|
|
97
|
+
printers = self._factory.create_printers(self._auth.get(url))
|
|
98
|
+
assert all(isinstance(p, Printer) for p in printers)
|
|
99
|
+
if len(printers) == 0:
|
|
100
|
+
raise LookupError('no printer with ID {}'.format(printer_id))
|
|
101
|
+
return printers[0]
|
|
102
|
+
|
|
103
|
+
def get_printjobs(self, computer=None, printer=None, printjob=None, limit=None, after=None, dir=None):
|
|
104
|
+
params = self._create_pagination_params(limit, after, dir)
|
|
105
|
+
|
|
106
|
+
if self._is_multi_query(printjob):
|
|
107
|
+
if computer is None and printer is None:
|
|
108
|
+
url = '/printjobs'
|
|
109
|
+
if params is not None:
|
|
110
|
+
url = url + '?' + params
|
|
111
|
+
printjobs = self._factory.create_printjobs(self._auth.get(url))
|
|
112
|
+
if isinstance(printjob, str):
|
|
113
|
+
return [pj for pj in printjobs if pj.title == printjob]
|
|
114
|
+
else:
|
|
115
|
+
return printjobs
|
|
116
|
+
else:
|
|
117
|
+
printers = self.get_printers(
|
|
118
|
+
computer=computer,
|
|
119
|
+
printer=printer)
|
|
120
|
+
if self._is_multi_query(printer):
|
|
121
|
+
if len(printers) == 0:
|
|
122
|
+
return []
|
|
123
|
+
else:
|
|
124
|
+
printers = [printers]
|
|
125
|
+
printer_ids = [p.id for p in printers]
|
|
126
|
+
printer_url = ','.join(map(str, printer_ids))
|
|
127
|
+
url = '/printers/{}/printjobs'.format(printer_url)
|
|
128
|
+
if params is not None:
|
|
129
|
+
url = url + '?' + params
|
|
130
|
+
printjobs = self._factory.create_printjobs(self._auth.get(url))
|
|
131
|
+
|
|
132
|
+
if isinstance(printjob, str):
|
|
133
|
+
return [pj for pj in printjobs if pj.title == printjob]
|
|
134
|
+
else:
|
|
135
|
+
return printjobs
|
|
136
|
+
else:
|
|
137
|
+
printjob_id = self._get_printjob_id(printjob)
|
|
138
|
+
results = self._auth.get('/printjobs/{}'.format(printjob_id))
|
|
139
|
+
printjobs = self._factory.create_printjobs(results)
|
|
140
|
+
if len(printjobs) == 0:
|
|
141
|
+
raise LookupError('no printjob with ID {}'.format(printjob_id))
|
|
142
|
+
return printjobs[0]
|
|
143
|
+
|
|
144
|
+
# could use the default printer if none is provided
|
|
145
|
+
def submit_printjob(
|
|
146
|
+
self,
|
|
147
|
+
computer=None,
|
|
148
|
+
printer=None,
|
|
149
|
+
job_type='pdf', # PDF|RAW
|
|
150
|
+
title='PrintJob',
|
|
151
|
+
qty=None,
|
|
152
|
+
options=None,
|
|
153
|
+
authentication=None,
|
|
154
|
+
uri=None,
|
|
155
|
+
base64=None,
|
|
156
|
+
binary=None):
|
|
157
|
+
printers = self.get_printers(computer=computer, printer=printer)
|
|
158
|
+
assert isinstance(printers, (list, Printer))
|
|
159
|
+
if isinstance(printers, list):
|
|
160
|
+
if len(printers) == 0:
|
|
161
|
+
raise LookupError('printer not found')
|
|
162
|
+
elif len(printers) > 1:
|
|
163
|
+
printer_ids = ','.join(str(p.id) for p in printers)
|
|
164
|
+
msg = 'multiple printers match destination: {}'.format(
|
|
165
|
+
printer_ids)
|
|
166
|
+
raise LookupError(msg)
|
|
167
|
+
printer_id = printers[0].id
|
|
168
|
+
else:
|
|
169
|
+
printer_id = printers.id
|
|
170
|
+
|
|
171
|
+
if job_type not in ['pdf', 'raw', 'binary']:
|
|
172
|
+
raise ValueError('only support job_type of pdf or raw')
|
|
173
|
+
if len([x for x in [uri, base64, binary] if x is not None]) != 1:
|
|
174
|
+
raise ValueError('one and only one of the following parameters '
|
|
175
|
+
'is needed: uri, base64, or binary')
|
|
176
|
+
|
|
177
|
+
if binary is not None:
|
|
178
|
+
if sys.version_info[0] < 3:
|
|
179
|
+
binary_bytes = newbytes(binary)
|
|
180
|
+
else:
|
|
181
|
+
if isinstance(binary, str):
|
|
182
|
+
binary_bytes = binary.encode('latin-1')
|
|
183
|
+
else:
|
|
184
|
+
binary_bytes = binary
|
|
185
|
+
base64 = base_64.b64encode(binary_bytes)
|
|
186
|
+
base64 = base64.decode('utf-8')
|
|
187
|
+
|
|
188
|
+
printjob_data = {
|
|
189
|
+
'printerId': printer_id,
|
|
190
|
+
'title': title,
|
|
191
|
+
'contentType': job_type + '_' + ('uri' if uri else 'base64'),
|
|
192
|
+
'content': uri or base64,
|
|
193
|
+
'source': 'PythonApiClient'}
|
|
194
|
+
|
|
195
|
+
if authentication is not None:
|
|
196
|
+
printjob_data.update({"authentication": authentication})
|
|
197
|
+
|
|
198
|
+
if options is not None:
|
|
199
|
+
printjob_data.update({"options": options})
|
|
200
|
+
|
|
201
|
+
if qty is not None:
|
|
202
|
+
printjob_data.update({"qty": qty})
|
|
203
|
+
|
|
204
|
+
printjob_id = self._auth.post('/printjobs', printjob_data)
|
|
205
|
+
|
|
206
|
+
return printjob_id
|
|
207
|
+
|
|
208
|
+
def _native(self, obj):
|
|
209
|
+
if hasattr(obj, '__native__'):
|
|
210
|
+
return obj.__native__()
|
|
211
|
+
else:
|
|
212
|
+
return obj
|
|
213
|
+
|
|
214
|
+
def _get_computer_ids(self, computer):
|
|
215
|
+
if isinstance(computer, int):
|
|
216
|
+
return [computer]
|
|
217
|
+
elif isinstance(computer, Computer):
|
|
218
|
+
return [computer.id]
|
|
219
|
+
elif computer is None or isinstance(computer, str):
|
|
220
|
+
computers = [
|
|
221
|
+
self._factory.create_computer(comp)
|
|
222
|
+
for comp in self._auth.get('/computers')]
|
|
223
|
+
if isinstance(computer, str):
|
|
224
|
+
computers = [
|
|
225
|
+
comp
|
|
226
|
+
for comp in computers
|
|
227
|
+
if comp.name == computer]
|
|
228
|
+
return [c.id for c in computers]
|
|
229
|
+
else:
|
|
230
|
+
raise TypeError('computer: "{}"'.format(type(computer)))
|
|
231
|
+
|
|
232
|
+
def _get_computer_id(self, computer):
|
|
233
|
+
return self._get_model_id(computer, Computer)
|
|
234
|
+
|
|
235
|
+
def _get_printer_id(self, printer):
|
|
236
|
+
return self._get_model_id(printer, Printer)
|
|
237
|
+
|
|
238
|
+
def _get_printjob_id(self, printjob):
|
|
239
|
+
return self._get_model_id(printjob, PrintJob)
|
|
240
|
+
|
|
241
|
+
def _get_model_id(self, model, model_type):
|
|
242
|
+
if isinstance(model, int):
|
|
243
|
+
return model
|
|
244
|
+
elif isinstance(model, model_type):
|
|
245
|
+
return model.id
|
|
246
|
+
else:
|
|
247
|
+
raise TypeError(str(type(model)))
|
|
248
|
+
|
|
249
|
+
def _get_printer_ids(self, printer):
|
|
250
|
+
if isinstance(printer, int):
|
|
251
|
+
return [printer]
|
|
252
|
+
elif isinstance(printer, Printer):
|
|
253
|
+
return [Printer.id]
|
|
254
|
+
elif printer is None or isinstance(printer, str):
|
|
255
|
+
printers = {
|
|
256
|
+
self._factory.create_printer(printer_dict)
|
|
257
|
+
for printer_dict in self._auth.get('/printer')}
|
|
258
|
+
if isinstance(printer, str):
|
|
259
|
+
printers = {
|
|
260
|
+
p for p in printers if p.name == printer}
|
|
261
|
+
return printers
|
|
262
|
+
else:
|
|
263
|
+
raise TypeError('printer: "{}"'.format(type(printer)))
|
|
264
|
+
|
|
265
|
+
def _is_multi_query(self, obj):
|
|
266
|
+
if obj is None:
|
|
267
|
+
return True
|
|
268
|
+
elif isinstance(obj, int):
|
|
269
|
+
return False
|
|
270
|
+
elif isinstance(obj, str):
|
|
271
|
+
return True
|
|
272
|
+
elif isinstance(obj, Model):
|
|
273
|
+
return False
|
|
274
|
+
else:
|
|
275
|
+
raise TypeError('type "{}" unsupported'.format(type(obj)))
|
|
276
|
+
|
|
277
|
+
def _get_printer_by_name(self, printer_name, computer_id=None):
|
|
278
|
+
assert isinstance(printer_name, str)
|
|
279
|
+
assert not computer_id or isinstance(computer_id, int)
|
|
280
|
+
printers = self.get_printers(computer=computer_id)
|
|
281
|
+
return [
|
|
282
|
+
printer
|
|
283
|
+
for printer in printers
|
|
284
|
+
if printer.name == printer_name]
|
|
285
|
+
|
|
286
|
+
def _create_pagination_params(self, limit, after, dir):
|
|
287
|
+
params = []
|
|
288
|
+
if limit is not None:
|
|
289
|
+
if isinstance(limit, int):
|
|
290
|
+
params.append('limit={}'.format(limit))
|
|
291
|
+
else:
|
|
292
|
+
raise TypeError('limit: "{}"'.format(type(limit)))
|
|
293
|
+
if after is not None:
|
|
294
|
+
if isinstance(after, int):
|
|
295
|
+
params.append('after={}'.format(after))
|
|
296
|
+
else:
|
|
297
|
+
raise TypeError('after: "{}"'.format(type(after)))
|
|
298
|
+
if dir is not None:
|
|
299
|
+
if dir == 'asc' or dir == 'desc':
|
|
300
|
+
params.append('dir={}'.format(dir))
|
|
301
|
+
else:
|
|
302
|
+
raise TypeError("dir must equal 'asc' or 'desc'")
|
|
303
|
+
if len(params) > 0:
|
|
304
|
+
return '&'.join(params)
|
|
305
|
+
|
|
306
|
+
class LookupFailedError(RuntimeError):
|
|
307
|
+
|
|
308
|
+
def __init__(self, obj_name, field, value):
|
|
309
|
+
msg = 'Failed to find a matching {} with {} of {}'.format(
|
|
310
|
+
obj_name,
|
|
311
|
+
field,
|
|
312
|
+
value)
|
|
313
|
+
super(RuntimeError, self).__init__(msg)
|
|
314
|
+
|
|
315
|
+
self.obj_name = obj_name
|
|
316
|
+
self.field = field
|
|
317
|
+
self.value = value
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
|
|
5
|
+
from .auth import Auth, Unauthorized
|
|
6
|
+
from .model import ModelFactory
|
|
7
|
+
from .accounts import Accounts
|
|
8
|
+
from .computers import Computers
|
|
9
|
+
from .util import camel_to_underscore
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Gateway:
|
|
13
|
+
|
|
14
|
+
URL = 'https://api.printnode.com'
|
|
15
|
+
|
|
16
|
+
def __init__(self, **kwargs):
|
|
17
|
+
url = kwargs.pop('url', self.URL)
|
|
18
|
+
self._auth = Auth(url=url, **kwargs)
|
|
19
|
+
self._factory = ModelFactory()
|
|
20
|
+
self._accounts = Accounts(self._auth, self._factory)
|
|
21
|
+
self._computers = Computers(self._auth, self._factory)
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def account(self):
|
|
25
|
+
server_account = self._auth.get('whoami')
|
|
26
|
+
return self._factory.create_account(server_account)
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def computers(self):
|
|
30
|
+
return self._computers.get_computers
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def printers(self):
|
|
34
|
+
return self._computers.get_printers
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def printjobs(self):
|
|
38
|
+
return self._computers.get_printjobs
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def scales(self):
|
|
42
|
+
return self._computers.get_scales
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def tag(self):
|
|
46
|
+
return self._accounts.get_tag
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def api_key(self):
|
|
50
|
+
return self._accounts.get_api_key
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def clientkey(self):
|
|
54
|
+
return self._accounts.get_clientkey
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def clients(self):
|
|
58
|
+
return self._accounts.get_clients
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def states(self):
|
|
62
|
+
return self._computers.get_states
|
|
63
|
+
|
|
64
|
+
def PrintJob(self, *args, **kwargs):
|
|
65
|
+
printjob_id = self._computers.submit_printjob(*args, **kwargs)
|
|
66
|
+
printjob = self.printjobs(printjob=printjob_id)
|
|
67
|
+
return printjob
|
|
68
|
+
|
|
69
|
+
def TestDataGenerate(self):
|
|
70
|
+
self._auth.get('test/data/generate')
|
|
71
|
+
|
|
72
|
+
def TestDataDelete(self):
|
|
73
|
+
self._auth.delete('test/data/generate')
|
|
74
|
+
|
|
75
|
+
def ModifyTag(self, tagname, tagvalue):
|
|
76
|
+
tag = self._accounts.modify_tag(tagname, tagvalue)
|
|
77
|
+
return tag
|
|
78
|
+
|
|
79
|
+
def DeleteTag(self, tagname):
|
|
80
|
+
tag = self._accounts.delete_tag(tagname)
|
|
81
|
+
return tag
|
|
82
|
+
|
|
83
|
+
def CreateAccount(self, **kwargs):
|
|
84
|
+
acc_post = self._accounts.create_account(**kwargs)
|
|
85
|
+
return acc_post
|
|
86
|
+
|
|
87
|
+
def ModifyAccount(self, **kwargs):
|
|
88
|
+
acc_id = self._accounts.modify_account(**kwargs)
|
|
89
|
+
return self._factory.create_account(acc_id)
|
|
90
|
+
|
|
91
|
+
def DeleteAccount(self, **kwargs):
|
|
92
|
+
boolean = self._accounts.delete_account(**kwargs)
|
|
93
|
+
return boolean
|
|
94
|
+
|
|
95
|
+
def DeleteApiKey(self, api_key):
|
|
96
|
+
key = self._accounts.delete_api_key(api_key)
|
|
97
|
+
return key
|
|
98
|
+
|
|
99
|
+
def CreateApiKey(self, api_key):
|
|
100
|
+
key = self._accounts.create_api_key(api_key)
|
|
101
|
+
return key
|
|
102
|
+
|
|
103
|
+
def ModifyClientDownloads(self, client_id, enabled):
|
|
104
|
+
clients = self._accounts.modify_client_downloads(client_id, enabled)
|
|
105
|
+
return clients
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
|
|
2
|
+
from collections import namedtuple
|
|
3
|
+
|
|
4
|
+
from .util import camel_to_underscore
|
|
5
|
+
|
|
6
|
+
def safe_tuple_populate(tuple_class, fields, defaults={}):
|
|
7
|
+
"""
|
|
8
|
+
Instantiate a named tuple using *fields in a "safe" way. I.e. extra keys
|
|
9
|
+
are ignored and missing keys are populated by defaults dict
|
|
10
|
+
|
|
11
|
+
tuple_class :namedtuple/model
|
|
12
|
+
fields :dict
|
|
13
|
+
defaults :dict
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
assert issubclass(tuple_class, Model)
|
|
17
|
+
assert isinstance(fields, dict)
|
|
18
|
+
assert isinstance(defaults, dict)
|
|
19
|
+
|
|
20
|
+
fk = set(fields.keys())
|
|
21
|
+
tk = set(tuple_class._fields)
|
|
22
|
+
|
|
23
|
+
# add in missing fields using defaults if present
|
|
24
|
+
for key in (tk - fk):
|
|
25
|
+
fields.setdefault(key, defaults.get(key))
|
|
26
|
+
|
|
27
|
+
# remove any extra keys
|
|
28
|
+
for key in (fk - tk):
|
|
29
|
+
fields.pop(key)
|
|
30
|
+
|
|
31
|
+
return tuple_class(**fields)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ModelFactory:
|
|
35
|
+
|
|
36
|
+
def create_account(self, account_dict):
|
|
37
|
+
fields = self._underscore_keys(account_dict)
|
|
38
|
+
fields.setdefault('api_keys', [])
|
|
39
|
+
fields.setdefault('permissions', [])
|
|
40
|
+
return safe_tuple_populate(Account, fields)
|
|
41
|
+
|
|
42
|
+
def create_clients(self, clients_dict):
|
|
43
|
+
return self._map(self.create_client, clients_dict)
|
|
44
|
+
|
|
45
|
+
def create_latest_download(self, client_dict):
|
|
46
|
+
fields = self._underscore_keys(client_dict)
|
|
47
|
+
return safe_tuple_populate(Download, fields)
|
|
48
|
+
|
|
49
|
+
def create_client(self, client_dict):
|
|
50
|
+
fields = self._underscore_keys(client_dict)
|
|
51
|
+
return safe_tuple_populate(Client, fields)
|
|
52
|
+
|
|
53
|
+
def create_scales(self, scales_dict):
|
|
54
|
+
return self._map(self.create_scale, scales_dict)
|
|
55
|
+
|
|
56
|
+
def create_scale(self, scale_dict):
|
|
57
|
+
fields = self._underscore_keys(scale_dict)
|
|
58
|
+
return safe_tuple_populate(Scale, fields)
|
|
59
|
+
|
|
60
|
+
def create_computers(self, computers_dict):
|
|
61
|
+
return self._map(self.create_computer, computers_dict)
|
|
62
|
+
|
|
63
|
+
def create_computer(self, computer_dict):
|
|
64
|
+
fields = self._underscore_keys(computer_dict)
|
|
65
|
+
del fields['jre']
|
|
66
|
+
self._rename_field(fields, 'inet_6', 'inet6')
|
|
67
|
+
return safe_tuple_populate(Computer, fields)
|
|
68
|
+
|
|
69
|
+
def create_printers(self, printers_dict):
|
|
70
|
+
return self._map(self.create_printer, printers_dict)
|
|
71
|
+
|
|
72
|
+
def create_printer(self, printer_dict):
|
|
73
|
+
fields = self._underscore_keys(printer_dict)
|
|
74
|
+
fields['computer'] = self.create_computer(fields['computer'])
|
|
75
|
+
if fields['capabilities']:
|
|
76
|
+
fields['capabilities'] = self.create_capabilities(
|
|
77
|
+
fields['capabilities'])
|
|
78
|
+
return safe_tuple_populate(Printer, fields)
|
|
79
|
+
|
|
80
|
+
def create_capabilities(self, capabilities_dict):
|
|
81
|
+
fields = self._underscore_keys(capabilities_dict)
|
|
82
|
+
return safe_tuple_populate(Capabilities, fields)
|
|
83
|
+
|
|
84
|
+
def create_printjobs(self, printjobs_dict):
|
|
85
|
+
return self._map(self.create_printjob, printjobs_dict)
|
|
86
|
+
|
|
87
|
+
def create_printjob(self, printjob_dict):
|
|
88
|
+
fields = self._underscore_keys(printjob_dict)
|
|
89
|
+
fields['printer'] = self.create_printer(fields['printer'])
|
|
90
|
+
fields.setdefault('content', None)
|
|
91
|
+
fields.setdefault('pages', None)
|
|
92
|
+
fields.setdefault('qty', 1)
|
|
93
|
+
fields.setdefault('options', {})
|
|
94
|
+
return safe_tuple_populate(PrintJob, fields)
|
|
95
|
+
|
|
96
|
+
def create_states_map(self, states_dict_list):
|
|
97
|
+
return self._map(self.create_states, states_dict_list)
|
|
98
|
+
|
|
99
|
+
def create_states(self, states_dict):
|
|
100
|
+
return self._map(self.create_state, states_dict)
|
|
101
|
+
|
|
102
|
+
def create_state(self, state_dict):
|
|
103
|
+
fields = self._underscore_keys(state_dict)
|
|
104
|
+
return safe_tuple_populate(State, fields)
|
|
105
|
+
|
|
106
|
+
def _underscore_keys(self, input_dict):
|
|
107
|
+
return {
|
|
108
|
+
camel_to_underscore(k): v
|
|
109
|
+
for k, v in input_dict.items()}
|
|
110
|
+
|
|
111
|
+
def _rename_field(self, obj_dict, old_name, new_name):
|
|
112
|
+
assert new_name not in obj_dict
|
|
113
|
+
obj_dict[new_name] = obj_dict[old_name]
|
|
114
|
+
del obj_dict[old_name]
|
|
115
|
+
|
|
116
|
+
def _map(self, f, iter):
|
|
117
|
+
return list(map(f, iter))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class Model:
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
Account = namedtuple('Account', [
|
|
125
|
+
'id',
|
|
126
|
+
'firstname',
|
|
127
|
+
'lastname',
|
|
128
|
+
'email',
|
|
129
|
+
'can_create_sub_accounts',
|
|
130
|
+
'creator_email',
|
|
131
|
+
'creator_ref',
|
|
132
|
+
'child_accounts',
|
|
133
|
+
'credits',
|
|
134
|
+
'num_computers',
|
|
135
|
+
'total_prints',
|
|
136
|
+
'versions',
|
|
137
|
+
'connected',
|
|
138
|
+
'tags',
|
|
139
|
+
'state',
|
|
140
|
+
'api_keys',
|
|
141
|
+
'permissions'])
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class Account(Account, Model):
|
|
145
|
+
pass
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
Computer = namedtuple('Computer', [
|
|
149
|
+
'id',
|
|
150
|
+
'name',
|
|
151
|
+
'inet',
|
|
152
|
+
'inet6',
|
|
153
|
+
'hostname',
|
|
154
|
+
'version',
|
|
155
|
+
'create_timestamp',
|
|
156
|
+
'state'])
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class Computer(Computer, Model):
|
|
160
|
+
pass
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
Printer = namedtuple('Printer', [
|
|
164
|
+
'id',
|
|
165
|
+
'computer',
|
|
166
|
+
'name',
|
|
167
|
+
'description',
|
|
168
|
+
'capabilities',
|
|
169
|
+
'default',
|
|
170
|
+
'create_timestamp',
|
|
171
|
+
'state'])
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class Printer(Printer, Model):
|
|
175
|
+
pass
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
PrintJob = namedtuple('PrintJob', [
|
|
179
|
+
'id',
|
|
180
|
+
'printer',
|
|
181
|
+
'title',
|
|
182
|
+
'content',
|
|
183
|
+
'content_type',
|
|
184
|
+
'pages',
|
|
185
|
+
'qty',
|
|
186
|
+
'options',
|
|
187
|
+
'source',
|
|
188
|
+
'expire_at',
|
|
189
|
+
'create_timestamp',
|
|
190
|
+
'state'])
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
class PrintJob(PrintJob, Model):
|
|
194
|
+
pass
|
|
195
|
+
|
|
196
|
+
Scale = namedtuple('Scale', [
|
|
197
|
+
'measurement',
|
|
198
|
+
'mass',
|
|
199
|
+
'product',
|
|
200
|
+
'computer_id',
|
|
201
|
+
'vendor_id',
|
|
202
|
+
'port',
|
|
203
|
+
'client_reported_create_timestamp',
|
|
204
|
+
'device_name',
|
|
205
|
+
'product_id',
|
|
206
|
+
'vendor',
|
|
207
|
+
'count',
|
|
208
|
+
'age_of_data',
|
|
209
|
+
'device_num',
|
|
210
|
+
'ntp_offset'])
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class Scale(Scale, Model):
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
Client = namedtuple('Client', [
|
|
217
|
+
'id',
|
|
218
|
+
'enabled',
|
|
219
|
+
'edition',
|
|
220
|
+
'version',
|
|
221
|
+
'os',
|
|
222
|
+
'filename',
|
|
223
|
+
'filesize',
|
|
224
|
+
'sha_1',
|
|
225
|
+
'release_timestamp',
|
|
226
|
+
'url'
|
|
227
|
+
])
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class Client(Client, Model):
|
|
231
|
+
pass
|
|
232
|
+
|
|
233
|
+
Download = namedtuple('Download', [
|
|
234
|
+
'edition',
|
|
235
|
+
'version',
|
|
236
|
+
'os',
|
|
237
|
+
'filename',
|
|
238
|
+
'filesize',
|
|
239
|
+
'sha_1',
|
|
240
|
+
'release_timestamp',
|
|
241
|
+
'url'
|
|
242
|
+
])
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class Download(Download, Model):
|
|
246
|
+
pass
|
|
247
|
+
|
|
248
|
+
State = namedtuple('State', [
|
|
249
|
+
'print_job_id',
|
|
250
|
+
'state',
|
|
251
|
+
'message',
|
|
252
|
+
'data',
|
|
253
|
+
'client_version',
|
|
254
|
+
'create_timestamp',
|
|
255
|
+
'age'
|
|
256
|
+
])
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
class State(State, Model):
|
|
260
|
+
pass
|
|
261
|
+
|
|
262
|
+
Capabilities = namedtuple('Capabilities', [
|
|
263
|
+
'bins',
|
|
264
|
+
'collate',
|
|
265
|
+
'copies',
|
|
266
|
+
'color',
|
|
267
|
+
'dpis',
|
|
268
|
+
'duplex',
|
|
269
|
+
'extent',
|
|
270
|
+
'medias',
|
|
271
|
+
'nup',
|
|
272
|
+
'papers',
|
|
273
|
+
'printrate',
|
|
274
|
+
'supports_custom_paper_size'
|
|
275
|
+
])
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class Capabilities(Capabilities, Model):
|
|
279
|
+
pass
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
def camel_to_underscore(text):
|
|
4
|
+
letters = list(text.strip())
|
|
5
|
+
indexes = [i for i,
|
|
6
|
+
l in enumerate(letters) if l.upper() == l and not re.match('_',l)]
|
|
7
|
+
for i in reversed(indexes):
|
|
8
|
+
letters[i] = letters[i].lower()
|
|
9
|
+
if i != 0:
|
|
10
|
+
letters.insert(i, '_')
|
|
11
|
+
return ''.join(letters)
|