comparison data_schema/util.py @ 8:2352d14ae261

Make the packagedata automatic subdir customizable
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 07 Jul 2023 00:32:30 +0200
parents 84dfd1a94926
children 0ef7141030ca
comparison
equal deleted inserted replaced
7:72f8dd2ce0ba 8:2352d14ae261
30 if "/../" in path: 30 if "/../" in path:
31 return False 31 return False
32 return True 32 return True
33 33
34 34
35 def get_data_stream(uri, basedir=None, basepackage=None): 35 def get_data_stream(uri, basedir=None,
36 basepackage=None, packagesubdir="packagedata"):
36 """ 37 """
37 38
38 "data:" URIs are resolved as Python package resources for packages 39 "data:" URIs are resolved as Python package resources for packages
39 `package`. by default this is the package where this module lives 40 `package`. by default this is the package where this module lives
40 in. 41 in.
41 42
42 "file:" URIs are resolved by prepending `basedir` to the URI path. 43 "file:" URIs are resolved by prepending `basedir` to the URI path.
43 44
44 "data:" URIs are resolve within "<basepackage>.packagedata". 45 "data:" URIs are resolve within "<basepackage>.<packagesubdir>".
45 46
46 The returned stream needs to be closes as usual. 47 The returned stream needs to be closes as usual.
47 48
48 """ 49 """
49 u = rfc3986.URIReference.from_string(uri).normalize() 50 u = rfc3986.URIReference.from_string(uri).normalize()
77 datapath_file = datapath_parts[-1] 78 datapath_file = datapath_parts[-1]
78 if datapath_dirs: 79 if datapath_dirs:
79 datapath_sep = '.' 80 datapath_sep = '.'
80 else: 81 else:
81 datapath_sep = '' 82 datapath_sep = ''
83 if packagesubdir:
84 psubdir = "." + packagesubdir
82 return il_resources.open_binary( 85 return il_resources.open_binary(
83 datapackage + '.packagedata' + datapath_sep 86 datapackage + psubdir + datapath_sep
84 + '.'.join(datapath_dirs), # noqa: E131 87 + '.'.join(datapath_dirs), # noqa: E131
85 datapath_file) 88 datapath_file)
86 else: 89 else:
90 if packagesubdir:
91 psubdir = packagesubdir + "/"
87 return pkg_resources.resource_stream( # noqa:E501 # pylint:disable=used-before-assignment 92 return pkg_resources.resource_stream( # noqa:E501 # pylint:disable=used-before-assignment
88 datapackage, "packagedata/" + datapath) 93 datapackage, psubdir + datapath)
89 else: 94 else:
90 raise ValueError( 95 raise ValueError(
91 "URI path for the `data' scheme must not be absolute") 96 "URI path for the `data' scheme must not be absolute")
92 elif u.scheme == "file": 97 elif u.scheme == "file":
93 if u.authority or u.query or u.fragment: 98 if u.authority or u.query or u.fragment: