-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource.py
599 lines (484 loc) · 17.9 KB
/
resource.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import io
import os
import six
import json
import warnings
import cchardet
import requests
from copy import deepcopy
from tableschema import Table, Storage
from six.moves.urllib.parse import urljoin
from six.moves.urllib.request import urlopen
from .profile import Profile
from . import exceptions
from . import helpers
from . import config
# Module API
class Resource(object):
# Public
def __init__(self, descriptor={}, base_path=None, strict=False, storage=None,
# Internal
package=None, **options):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Get base path
if base_path is None:
base_path = helpers.get_descriptor_base_path(descriptor)
# Instantiate storage
if storage and not isinstance(storage, Storage):
storage = Storage.connect(storage, **options)
# Process descriptor
descriptor = helpers.retrieve_descriptor(descriptor)
descriptor = helpers.dereference_resource_descriptor(descriptor, base_path)
# Handle deprecated resource.path.url
if descriptor.get('url'):
warnings.warn(
'Resource property "url: <url>" is deprecated. '
'Please use "path: <url>" instead.',
UserWarning)
descriptor['path'] = descriptor['url']
del descriptor['url']
# Set attributes
self.__current_descriptor = deepcopy(descriptor)
self.__next_descriptor = deepcopy(descriptor)
self.__base_path = base_path
self.__package = package
self.__storage = storage
self.__relations = None
self.__strict = strict
self.__table = None
self.__errors = []
self.__table_options = options
# Build resource
self.__build()
@property
def package(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__package
@property
def valid(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return not bool(self.__errors)
@property
def errors(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__errors
@property
def profile(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__profile
@property
def descriptor(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Never use self.descriptor inside self class (!!!)
return self.__next_descriptor
@property
def group(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__current_descriptor.get('group')
@property
def name(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__current_descriptor.get('name')
@property
def inline(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__source_inspection.get('inline', False)
@property
def local(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__source_inspection.get('local', False)
@property
def remote(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__source_inspection.get('remote', False)
@property
def multipart(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__source_inspection.get('multipart', False)
@property
def tabular(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
if self.__current_descriptor.get('profile') == 'tabular-data-resource':
return True
if not self.__strict:
if self.__current_descriptor.get('format') in config.TABULAR_FORMATS:
return True
if self.__source_inspection.get('tabular', False):
return True
return False
@property
def source(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
return self.__source_inspection.get('source')
@property
def headers(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
if not self.tabular:
return None
return self.__get_table().headers
@property
def schema(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
if not self.tabular:
return None
return self.__get_table().schema
def iter(self, relations=False, **options):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Error for non tabular
if not self.tabular:
message = 'Methods iter/read are not supported for non tabular data'
raise exceptions.DataPackageException(message)
# Get relations
if relations:
relations = self.__get_relations()
return self.__get_table().iter(relations=relations, **options)
def read(self, relations=False, **options):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Error for non tabular
if not self.tabular:
message = 'Methods iter/read are not supported for non tabular data'
raise exceptions.DataPackageException(message)
# Get relations
if relations:
relations = self.__get_relations()
return self.__get_table().read(relations=relations, **options)
def check_relations(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
self.read(relations=True)
return True
def raw_iter(self, stream=False):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Error for inline
if self.inline:
message = 'Methods raw_iter/raw_read are not supported for inline data'
raise exceptions.DataPackageException(message)
# Get filelike
if self.multipart:
filelike = _MultipartSource(self.source, remote=self.remote)
elif self.remote:
if self.__table_options.get('http_session'):
http_session = self.__table_options['http_session']
else:
http_session = requests.Session()
http_session.headers = config.HTTP_HEADERS
res = http_session.get(self.source, stream=True)
filelike = res.raw
else:
filelike = io.open(self.source, 'rb')
return filelike
def raw_read(self):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
contents = b''
with self.raw_iter() as filelike:
for chunk in filelike:
contents += chunk
return contents
def infer(self, **options):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
descriptor = deepcopy(self.__current_descriptor)
# Blank -> Stop
if self.__source_inspection.get('blank'):
return descriptor
# Name
if not descriptor.get('name'):
descriptor['name'] = self.__source_inspection['name']
# Only for non inline/storage
if not self.inline and not self.__storage:
# Format
if not descriptor.get('format'):
descriptor['format'] = self.__source_inspection['format']
# Mediatype
if not descriptor.get('mediatype'):
descriptor['mediatype'] = 'text/%s' % descriptor['format']
# Encoding
if not descriptor.get('encoding'):
contents = b''
with self.raw_iter(stream=True) as stream:
for chunk in stream:
contents += chunk
if len(contents) > 1000: break
encoding = cchardet.detect(contents)['encoding']
if encoding is not None:
encoding = encoding.lower()
descriptor['encoding'] = 'utf-8' if encoding == 'ascii' else encoding
# Schema
if not descriptor.get('schema'):
if self.tabular:
descriptor['schema'] = self.__get_table().infer(**options)
# Profile
if descriptor.get('profile') == config.DEFAULT_RESOURCE_PROFILE:
if self.tabular:
descriptor['profile'] = 'tabular-data-resource'
# Save descriptor
self.__current_descriptor = descriptor
self.__build()
return descriptor
def commit(self, strict=None):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
if strict is not None:
self.__strict = strict
elif self.__current_descriptor == self.__next_descriptor:
return False
self.__current_descriptor = deepcopy(self.__next_descriptor)
self.__table = None
self.__build()
return True
def save(self, target, storage=None, **options):
"""https://github.com/frictionlessdata/datapackage-py#resource
"""
# Save resource to storage
if storage is not None:
if self.tabular:
self.infer()
storage.create(target, self.schema.descriptor, force=True)
storage.write(target, self.iter())
# Save descriptor to json
else:
mode = 'w'
encoding = 'utf-8'
if six.PY2:
mode = 'wb'
encoding = None
helpers.ensure_dir(target)
with io.open(target, mode=mode, encoding=encoding) as file:
json.dump(self.__current_descriptor, file, indent=4)
# Private
def __build(self):
# Process descriptor
expand = helpers.expand_resource_descriptor
self.__current_descriptor = expand(self.__current_descriptor)
self.__next_descriptor = deepcopy(self.__current_descriptor)
# Inspect source
self.__source_inspection = _inspect_source(
self.__current_descriptor.get('data'),
self.__current_descriptor.get('path'),
self.__base_path,
self.__storage)
# Instantiate profile
self.__profile = Profile(self.__current_descriptor.get('profile'))
# Validate descriptor
try:
self.__profile.validate(self.__current_descriptor)
self.__errors = []
except exceptions.ValidationError as exception:
self.__errors = exception.errors
if self.__strict:
raise exception
def __get_table(self):
if not self.__table:
# Non tabular -> None
if not self.tabular:
return None
# Get source/schema
source = self.source
if self.multipart:
source = _MultipartSource(self.source, remote=self.remote)
schema = self.descriptor.get('schema')
# Storage resource
if self.__storage is not None:
self.__table = Table(source, schema=schema, storage=self.__storage)
# General resource
else:
options = self.__table_options
descriptor = self.__current_descriptor
options['format'] = descriptor.get('format', 'csv')
if descriptor.get('data'):
options['format'] = 'inline'
if descriptor.get('encoding'):
options['encoding'] = descriptor['encoding']
if descriptor.get('compression'):
options['compression'] = descriptor['compression']
options['skip_rows'] = descriptor.get(
'skipRows', options.get('skip_rows', []))
dialect = descriptor.get('dialect')
if dialect:
if not dialect.get('header', config.DEFAULT_DIALECT['header']):
fields = descriptor.get('schema', {}).get('fields', [])
options['headers'] = [field['name'] for field in fields] or None
for key in _DIALECT_KEYS:
if key in dialect:
options[key.lower()] = dialect[key]
self.__table = Table(source, schema=schema, **options)
return self.__table
def __get_relations(self):
if not self.__relations:
# Prepare resources
resources = {}
if self.__get_table() and self.__get_table().schema:
for fk in self.__get_table().schema.foreign_keys:
resources.setdefault(fk['reference']['resource'], [])
for field in fk['reference']['fields']:
resources[fk['reference']['resource']].append(field)
# Fill relations
self.__relations = {}
for resource, fields in resources.items():
if resource and not self.__package:
continue
self.__relations.setdefault(resource, [])
data = self.__package.get_resource(resource) if resource else self
if data.tabular:
self.__relations[resource] = data.read(keyed=True)
return self.__relations
# Deprecated
@property
def table(self):
"""Return resource table
"""
# Deprecate
warnings.warn(
'Property "resource.table" is deprecated. '
'Please use "resource.iter/read" directly.',
UserWarning)
return self.__get_table()
@property
def data(self):
"""Return resource data
"""
# Deprecate
warnings.warn(
'Property "resource.data" is deprecated. '
'Please use "resource.read(keyed=True)" instead.',
UserWarning)
return self.read(keyed=True)
# Internal
_DIALECT_KEYS = [
'delimiter',
'doubleQuote',
'lineTerminator',
'quoteChar',
'escapeChar',
'skipInitialSpace',
]
def _inspect_source(data, path, base_path, storage):
inspection = {}
# Normalize path
if path and not isinstance(path, list):
path = [path]
# Blank
if not data and not path:
inspection['source'] = None
inspection['blank'] = True
# Storage
elif storage is not None:
inspection['name'] = path[0]
inspection['source'] = path[0]
inspection['tabular'] = True
# Inline
elif data is not None:
inspection['name'] = 'inline'
inspection['source'] = data
inspection['inline'] = True
inspection['tabular'] = isinstance(data, list)
# Local/Remote
elif len(path) == 1:
# Remote
if path[0].startswith('http') or path[0].startswith('s3'):
inspection['source'] = path[0]
inspection['remote'] = True
elif base_path and base_path.startswith('http'):
norm_base_path = base_path if base_path.endswith('/') else base_path + '/'
inspection['source'] = urljoin(norm_base_path, path[0])
inspection['remote'] = True
# Local
else:
# Path is not safe
if not helpers.is_safe_path(path[0]):
raise exceptions.DataPackageException(
'Local path "%s" is not safe' % path[0])
# Not base path
if not base_path:
raise exceptions.DataPackageException(
'Local path "%s" requires base path' % path[0])
inspection['source'] = os.path.join(base_path, path[0])
inspection['local'] = True
# Inspect
filename = os.path.basename(path[0])
inspection['format'] = os.path.splitext(filename)[1][1:]
inspection['name'] = os.path.splitext(filename)[0]
inspection['tabular'] = inspection['format'] in config.TABULAR_FORMATS
# Multipart Local/Remote
elif len(path) > 1:
inspections = list(map(lambda item: _inspect_source(None, item, base_path, None), path))
inspection.update(inspections[0])
inspection['source'] = list(map(lambda item: item['source'], inspections))
inspection['multipart'] = True
return inspection
class _MultipartSource(object):
# Public
def __init__(self, source, remote=False):
self.__source = source
self.__remote = remote
self.__rows = self.__iter_rows()
def __enter__(self):
pass
def __exit__(self):
pass
def __iter__(self):
return self.__rows
@property
def closed(self):
return False
def readable(self):
return True
def seekable(self):
return True
def writable(self):
return False
def close(self):
pass
def flush(self):
pass
def read1(self, size):
return self.read(size)
def seek(self, offset):
assert offset == 0
self.__rows = self.__iter_rows()
def read(self, size):
res = b''
while True:
try:
res += next(self.__rows)
except StopIteration:
break
if len(res) > size:
break
return res
# Private
def __iter_rows(self):
streams = []
if self.__remote:
streams = [urlopen(chunk) for chunk in self.__source]
else:
streams = [io.open(chunk, 'rb') for chunk in self.__source]
for stream in streams:
for row in stream:
if not row.endswith(b'\n'):
row += b'\n'
yield row