Interface
interface
Package providing an interface to data sources following the repository pattern.
Example
A XPlangGML 6.0 file can be loaded like this:
Datasource is the value object every
repository stores: it classifies a datasource, retrieves a remote one and identifies
its format, and Datasource.repo
builds the matching repository. It can also be used on its own, for instance to read
a WFS response:
Datasource(source, *, allow_remote=None, format=None, for_write=False)
Where a datasource is, what it holds, and which repository reads it.
The value object every repository stores. Construction classifies and validates but never reads: a path is opened, and a remote URL retrieved, only when an attribute first needs the bytes.
Usage example
# read a local file
collection = Datasource("plan.gml").repo().get_all()
# read a WFS response, which carries no usable file extension
ds = Datasource(wfs_url, allow_remote=True)
ds.format # -> "gml", identified from the retrieved content
ds.repo_class # -> <class GMLRepository>
collection = ds.repo().get_all()
# write to a GeoPackage; a database is always addressed by connection URL
Datasource("gpkg:///out.gpkg").repo().save_all(collection)
# name the format instead of identifying it from the content
Datasource("out.dat", format="gml").repo().save_all(collection)
Attributes:
| Name | Type | Description |
|---|---|---|
raw |
The datasource exactly as it was handed over. |
|
kind |
Kind
|
Where the datasource's bytes are. |
allow_remote |
Whether retrieval was permitted; |
|
for_write |
Whether this is a write target, and so classified by name. |
Classifies and validates a datasource, without reading it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
A file path as a string or |
required |
allow_remote
|
bool | None
|
Whether an |
None
|
format
|
RepoType | None
|
The repository type, when it is known. Skips identifying it. |
None
|
for_write
|
bool
|
Whether this datasource is a write target, whose
|
False
|
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
The datasource names a location this package refuses to open, or is remote while remote access is disabled. |
Source code in xplan_tools/interface/datasource.py
buffer
cached
property
The datasource's bytes, when they are in memory rather than on disk.
Retrieves a remote datasource on first access. A path is left for the repository
to open, so a large file is never read into memory here. A stream that cannot be
rewound is drained into a BytesIO once, so that identifying the format does not
consume it and leave every later reader with nothing.
Raises:
| Type | Description |
|---|---|
RemoteFetchError
|
A remote datasource could not be retrieved. |
format
cached
property
Which repository reads this datasource, identified from its content.
A write target (for_write) is
identified from its name instead: whatever is at the path now is about to be
replaced, so it says nothing about what is being written. Reading it would refuse
to overwrite a file holding a document this package does not read.
Raises:
| Type | Description |
|---|---|
ForbiddenDoctypeError
|
The datasource is an XML document declaring a DTD. |
UnsupportedRootElementError
|
The datasource is XML, but opens with an element this package does not read. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
is_remote
property
Whether the datasource is an http(s) URL.
repo_class
property
The repository class that reads this datasource.
Imported on demand, one module at a time: those modules import this one in turn,
so the import cannot be made at module scope. One at a time rather than all four
because DBRepository drags in Alembic, which a GML file has no use for.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource could not be identified. |
root_info
cached
property
The root element of an XML datasource, read without parsing the rest of it.
Seeded by format when the
content sniff already read it, so a GML datasource is preflighted once however
it was reached.
Raises:
| Type | Description |
|---|---|
ForbiddenDoctypeError
|
The datasource is an XML document declaring a DTD. |
UnsupportedRootElementError
|
The datasource opens with an element this package does not read. |
XMLParseError
|
The datasource does not hold well-formed XML. |
source
property
What a file-based repository reads: the buffer if there is one, else the path.
DBRepository reads
uri instead.
uri
cached
property
The datasource as a typed location, or None for a file-like object.
A sqlalchemy.URL masks its password, which is why this rather than
raw is what gets logged. An
http(s) URL is split by the standard library rather than validated by a URL
model: classification has already accepted the scheme, and a stricter parse here
would raise an error no caller of this package expects.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The connection URL could not be parsed. The URL itself is left out of the message, since a password cannot be masked in a string that did not parse. |
open()
Yields the datasource as a binary stream positioned at the start.
A path is opened and closed here; a buffer is rewound and put back where it was found, so a caller's stream comes back as it was handed over.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is a database connection, which holds no stream of bytes. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
OSError
|
The path could not be opened. |
Yields:
| Type | Description |
|---|---|
IO[bytes]
|
A binary stream over the datasource. |
Source code in xplan_tools/interface/datasource.py
read_bytes()
Returns the whole datasource, retrieving a remote one on the way.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is a database connection. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
OSError
|
The path could not be opened. |
Returns:
| Type | Description |
|---|---|
bytes
|
The datasource's bytes. |
Source code in xplan_tools/interface/datasource.py
repo()
Builds the repository that reads this datasource.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource could not be identified. |
Returns:
| Type | Description |
|---|---|
BaseRepository
|
An instance of the matching repository class. |
Source code in xplan_tools/interface/datasource.py
write_bytes(data)
Writes data to the datasource, encoding for a text buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The serialized document. |
required |
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is not something this package writes to - a database connection, which its repository writes through SQLAlchemy, or a remote URL, which is never written back. |
OSError
|
The path could not be written. |
Returns:
| Type | Description |
|---|---|
int
|
The number of bytes, or characters for a text buffer, written. |
Source code in xplan_tools/interface/datasource.py
fetch_remote(url, *, allow_remote=None, timeout=None, max_bytes=None)
Retrieves a remote datasource into a buffer.
The buffer can be handed straight to a repository. Retrieval happens here, rather
than inside lxml, so that the parser needs no network access of its own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
An |
required |
allow_remote
|
bool | None
|
Whether remote retrieval is permitted; defaults to the
|
None
|
timeout
|
float | None
|
Seconds to wait; defaults to the |
None
|
max_bytes
|
int | None
|
Largest response body accepted; defaults to the
|
None
|
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
Remote retrieval is disabled, or the URL does not
name an |
RemoteFetchError
|
The request failed, or the response exceeded |
Returns:
| Type | Description |
|---|---|
BytesIO
|
The response body, positioned at the start. |
Source code in xplan_tools/interface/datasource.py
repo_factory(datasource='', repo_type=None, allow_remote=None)
Factory method for Repositories.
Deprecated: use Datasource instead,
which this delegates to: Datasource(datasource, allow_remote=...).repo().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasource
|
str
|
Name of the input source or output file. |
''
|
repo_type
|
Literal['gml', 'jsonfg', 'shape', 'db'] | None
|
Allows to explicitly select a Repository. |
None
|
allow_remote
|
bool | None
|
Whether an |
None
|
Raises:
| Type | Description |
|---|---|
DatasourceError
|
raises error for unknown/unspecified datasource |
UnsupportedDatasourceError
|
raises error for a datasource this package refuses to open, or a remote one while remote access is disabled |
RemoteFetchError
|
raises error when a remote datasource could not be retrieved |
Returns:
| Name | Type | Description |
|---|---|---|
BaseRepository |
BaseRepository
|
instance of repository class for manipulating a collection of plan features |
Source code in xplan_tools/interface/__init__.py
sniff_datasource(source, *, suffix='')
Identifies which repository a datasource should be read with.
Content decides: an XML document by its root element, a GeoPackage or shapefile by
its magic number, JSON-FG by its first byte. So a URL with no usable file extension -
a WFS GetFeature request, say - is identified from what it served. The extension
is the fallback, and is all an output target that does not exist yet can offer.
A shapefile is the .shp itself, never the directory holding it: GDAL exposes each
.shp in a directory as a layer and
ShapeRepository reads only the first
one, so naming the file is what makes the choice deterministic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | BytesIO
|
A file path, or a buffer holding the datasource. |
required |
suffix
|
str
|
File extension to fall back on, e.g. |
''
|
Raises:
| Type | Description |
|---|---|
ForbiddenDoctypeError
|
The datasource is an XML document declaring a DTD. |
UnsupportedRootElementError
|
The datasource is XML, but opens with an element this package does not read. |
Returns:
| Type | Description |
|---|---|
RepoType | None
|
The repository type, or |
Source code in xplan_tools/interface/datasource.py
Datasource(source, *, allow_remote=None, format=None, for_write=False)
Where a datasource is, what it holds, and which repository reads it.
The value object every repository stores. Construction classifies and validates but never reads: a path is opened, and a remote URL retrieved, only when an attribute first needs the bytes.
Usage example
# read a local file
collection = Datasource("plan.gml").repo().get_all()
# read a WFS response, which carries no usable file extension
ds = Datasource(wfs_url, allow_remote=True)
ds.format # -> "gml", identified from the retrieved content
ds.repo_class # -> <class GMLRepository>
collection = ds.repo().get_all()
# write to a GeoPackage; a database is always addressed by connection URL
Datasource("gpkg:///out.gpkg").repo().save_all(collection)
# name the format instead of identifying it from the content
Datasource("out.dat", format="gml").repo().save_all(collection)
Attributes:
| Name | Type | Description |
|---|---|---|
raw |
The datasource exactly as it was handed over. |
|
kind |
Kind
|
Where the datasource's bytes are. |
allow_remote |
Whether retrieval was permitted; |
|
for_write |
Whether this is a write target, and so classified by name. |
Classifies and validates a datasource, without reading it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Any
|
A file path as a string or |
required |
allow_remote
|
bool | None
|
Whether an |
None
|
format
|
RepoType | None
|
The repository type, when it is known. Skips identifying it. |
None
|
for_write
|
bool
|
Whether this datasource is a write target, whose
|
False
|
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
The datasource names a location this package refuses to open, or is remote while remote access is disabled. |
Source code in xplan_tools/interface/datasource.py
buffer
cached
property
The datasource's bytes, when they are in memory rather than on disk.
Retrieves a remote datasource on first access. A path is left for the repository
to open, so a large file is never read into memory here. A stream that cannot be
rewound is drained into a BytesIO once, so that identifying the format does not
consume it and leave every later reader with nothing.
Raises:
| Type | Description |
|---|---|
RemoteFetchError
|
A remote datasource could not be retrieved. |
format
cached
property
Which repository reads this datasource, identified from its content.
A write target (for_write) is
identified from its name instead: whatever is at the path now is about to be
replaced, so it says nothing about what is being written. Reading it would refuse
to overwrite a file holding a document this package does not read.
Raises:
| Type | Description |
|---|---|
ForbiddenDoctypeError
|
The datasource is an XML document declaring a DTD. |
UnsupportedRootElementError
|
The datasource is XML, but opens with an element this package does not read. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
is_remote
property
Whether the datasource is an http(s) URL.
repo_class
property
The repository class that reads this datasource.
Imported on demand, one module at a time: those modules import this one in turn,
so the import cannot be made at module scope. One at a time rather than all four
because DBRepository drags in Alembic, which a GML file has no use for.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource could not be identified. |
root_info
cached
property
The root element of an XML datasource, read without parsing the rest of it.
Seeded by format when the
content sniff already read it, so a GML datasource is preflighted once however
it was reached.
Raises:
| Type | Description |
|---|---|
ForbiddenDoctypeError
|
The datasource is an XML document declaring a DTD. |
UnsupportedRootElementError
|
The datasource opens with an element this package does not read. |
XMLParseError
|
The datasource does not hold well-formed XML. |
source
property
What a file-based repository reads: the buffer if there is one, else the path.
DBRepository reads
uri instead.
uri
cached
property
The datasource as a typed location, or None for a file-like object.
A sqlalchemy.URL masks its password, which is why this rather than
raw is what gets logged. An
http(s) URL is split by the standard library rather than validated by a URL
model: classification has already accepted the scheme, and a stricter parse here
would raise an error no caller of this package expects.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The connection URL could not be parsed. The URL itself is left out of the message, since a password cannot be masked in a string that did not parse. |
open()
Yields the datasource as a binary stream positioned at the start.
A path is opened and closed here; a buffer is rewound and put back where it was found, so a caller's stream comes back as it was handed over.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is a database connection, which holds no stream of bytes. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
OSError
|
The path could not be opened. |
Yields:
| Type | Description |
|---|---|
IO[bytes]
|
A binary stream over the datasource. |
Source code in xplan_tools/interface/datasource.py
read_bytes()
Returns the whole datasource, retrieving a remote one on the way.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is a database connection. |
RemoteFetchError
|
A remote datasource could not be retrieved. |
OSError
|
The path could not be opened. |
Returns:
| Type | Description |
|---|---|
bytes
|
The datasource's bytes. |
Source code in xplan_tools/interface/datasource.py
repo()
Builds the repository that reads this datasource.
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource could not be identified. |
Returns:
| Type | Description |
|---|---|
BaseRepository
|
An instance of the matching repository class. |
Source code in xplan_tools/interface/datasource.py
write_bytes(data)
Writes data to the datasource, encoding for a text buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The serialized document. |
required |
Raises:
| Type | Description |
|---|---|
DatasourceError
|
The datasource is not something this package writes to - a database connection, which its repository writes through SQLAlchemy, or a remote URL, which is never written back. |
OSError
|
The path could not be written. |
Returns:
| Type | Description |
|---|---|
int
|
The number of bytes, or characters for a text buffer, written. |
Source code in xplan_tools/interface/datasource.py
BaseRepository(datasource)
Bases: Protocol
Protocol defining the common interface for all repository implementations.
Initialize the repository from a datasource.
Source code in xplan_tools/interface/base.py
delete(obj_id, *, session=None)
delete_plan_by_id(plan_id, *, session=None)
get(obj_id, *, session=None)
get_all()
get_plan_by_id(plan_id, *, session=None)
patch(obj_id, partial_obj, *, session=None)
save(obj, *, session=None)
save_all(features, *, session=None)
GMLRepository(datasource)
Repository class for loading from and writing to GML files or file-like objects.
Given a plan, either xplan or INSPIRE PLU, the data is saved as GML with according namespaces and structure. Reading data from datasource and retrieving the data version is currently only supported for xplan data.
Initializes the GML Repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasource
|
str | BytesIO | StringIO | Datasource
|
A file path as a String, a file-like object or a
|
required |
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
The datasource is one this package refuses to open, or is remote while remote access is disabled. |
Source code in xplan_tools/interface/gml.py
content
cached
property
The parsed XML tree.
root_info
property
The root element's name, namespace bindings and xsi:schemaLocation.
Read by streaming to the first element only, so a document this package cannot read is rejected before the full parse. Identifying the datasource's format already read it, so this is normally the cached result of that one read.
get_all(always_generate_ids=False, **kwargs)
Retrieves a Feature Collection to the datasource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
always_generate_ids
|
bool
|
Generate new Feature IDs even if GML IDs can be parsed to UUIDs. |
False
|
**kwargs
|
dict
|
Read options. |
{}
|
Source code in xplan_tools/interface/gml.py
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 | |
save_all(features, **kwargs)
Saves a Feature Collection to the datasource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
BaseCollection
|
A BaseCollection instance. |
required |
**kwargs
|
dict
|
Not used in this repository. |
{}
|
Raises:
| Type | Description |
|---|---|
AppschemaNotFoundError
|
The collection's application schema has no GML encoding in this package. |
Source code in xplan_tools/interface/gml.py
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 | |
JsonFGRepository(datasource)
Repository class for loading from and writing to JSON-FG files or file-like objects.
Initializes the JSON-FG Repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasource
|
str | IO | Datasource
|
A file path as a String, a file-like object or a
|
required |
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
The datasource is one this package refuses to open, or is remote while remote access is disabled. |
Source code in xplan_tools/interface/jsonfg.py
content
property
The JSON data as a dict.
Raises:
| Type | Description |
|---|---|
JsonFGParseError
|
The datasource does not hold readable JSON. A truncated response is reported here rather than surfacing later as a missing schema link. |
DatasourceError
|
The datasource holds no readable stream of bytes. |
get_all(**kwargs)
Retrieves a Feature Collection to the datasource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
dict
|
Read options. |
{}
|
Source code in xplan_tools/interface/jsonfg.py
save_all(features, **kwargs)
Saves a Feature Collection to the datasource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
BaseCollection
|
A BaseCollection instance. |
required |
**kwargs
|
dict
|
Keyword arguments to pass on to |
{}
|
Source code in xplan_tools/interface/jsonfg.py
DBRepository(datasource)
Repository class for loading from and writing to databases.
Initializes the DB Repository.
During initialization, a connection is established and the existence of required tables is tested. If an alembic revision is found, automatic migration is executed for PostgreSQL DBs. For other DBs, an Exception is raised if the revision does not correspond to the current model. If no revision and tables are found, they are automatically created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasource
|
str | URL | Datasource
|
A connection uri, a |
required |
Source code in xplan_tools/interface/db.py
create_tables()
Creates coretable and related/spatial tables in the database.
Source code in xplan_tools/interface/db.py
delete_tables()
Deletes coretable and related/spatial tables from the database.
Source code in xplan_tools/interface/db.py
get_session(session=None)
Yield a managed session.
Source code in xplan_tools/interface/db.py
AsyncDBRepository(datasource)
Bases: DBRepository
Async PostgreSQL-backed repository that reuses synchronous migrations.
Source code in xplan_tools/interface/db.py
get_session(session=None)
async
Yield a managed session.
Source code in xplan_tools/interface/db.py
ShapeRepository(datasource)
Repository class for collecting plans from shape files.
Given a shape file conforming to the format described here (Appendix 2), plan data is read to XPlanung classes. Only reading is supported.
Initializes the Shape Repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datasource
|
str | Datasource
|
A shapefile - the |
required |
Raises:
| Type | Description |
|---|---|
UnsupportedDatasourceError
|
The datasource is one this package refuses to
open. GDAL reads the datasource here, so a path that names nowhere on
this filesystem - a |
DatasourceError
|
The datasource is not a path. GDAL opens a shapefile by name, alongside its sidecars, so a buffer or a URL cannot be one. |
Source code in xplan_tools/interface/shape.py
get_all(**kwargs)
Retrieves a Feature Collection from the datasource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Read options. |
{}
|
Source code in xplan_tools/interface/shape.py
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 | |