Weftspace is a Python library designed to provide the maximum possible level of support for Endless Sky datafile syntax by providing not only IO functionality but also support in handling and converting parsed data.
Reason this release was yanked:
Pre-release testing version, not intended to be used publicly
Project description
Weftspace
Weftspace was originally written in Java, but it is now also available in Python. The Python library is slightly smaller than the Java one, but it provides all the same functionalities.
Installation and Usage
The Weftspace Python library can be installed using pip:
pip install weftspace
You can find the full project details on PyPI.
Parser
The most common use case of Weftspace is to read data from a file into a node tree. This is accomplished by using the DataReader class.
Although the DataReader class does have several potentially useful methods, in the vast majority of cases a variation on the following three lines is all you need.
root_node: DataNode = DataNode.create_root_node() # Creates a generic root node that you'll access your parsed nodes from later
reader: DataReader = DataReader("path/to/file", root_node) # Constructs a DataReader
reader.parse() # Parses every line in the file, writing its contents as children of root_node, and automatically handling exceptions
These lines should turn your file of ES-formatted data into a node tree, ready for use!
Options
DataReader currently has the following options that can be used as *args when parsing:
IGNORE_NODE_FLAGS: Treats the keywordsaddandremoveas node names rather than flags.
Working with the Node Tree
Now that you've parsed your data, it should end up written to a (sometimes enormous) tree, with a single root node. Keep track of that root node, because it's how you access the rest of the tree!
The tree itself is made up of a whole bunch of DataNodes, each with three major properties: a name, a list of arguments, and a list of child nodes. Additionally, nodes include a reference to their parent node (if they aren't the root of a tree), and a special Flag that usually isn't all that important. Any nodes loaded using DataReader will also contain information about where they were loaded from for debug purposes.
Suppose you have the following lines in a datafile:
"some node"
description `This node is a cool node.`
attributes "short" "helpful"
When parsed, this would produce three nodes:
- A node named
some nodewith no arguments and two children (descriptionandattributes). - A node named
descriptionwith one argument (This node is a cool node.) and no children. - A node named
attributeswith two arguments (shortandhelpful) and no children.
Both the arguments and children of any given node are presented in a list, and the DataNode class contains several convenience methods to with each list.
Building Objects from Nodes
Let's face it: you probably don't want a node tree. You want to turn the nodes into objects. And you probably don't want to handle a billion exceptions that might arise if the data doesn't conform to the expected pattern. For this reason, I put together the Builder class, which allows you to convert a DataNode argument into any of several common data types, given a node, the number of the argument to build, and a "context" string that is usually the same across all instances of a class and is best defined as a constant at the top of the class you're writing a constructor for.
Here's an example of how you could convert a node to an object:
CONTEXT: Final[str] = "node-based object"
def __init__(self, node: DataNode):
self.name = Builder.build_string(node, 0, CONTEXT)
for child in node.children:
match child.name:
case "description": # Normal string
self.description = Builder.build_string(child, 0, CONTEXT)
case "mass": # Non-negative integer
self.mass = Builder.build_spec_int(child, 0, CONTEXT, Builder.IntType.NATURAL)
case "random number for fun": # A float
self.thingy = Builder.build_float(child, 0, CONTEXT)
case "position": # From "position" x y
self.x = Builder.build_int(child, 0, CONTEXT)
self.y = Builder.build_int(child, 1, CONTEXT)
case _:
pass
Writing Data
Occasionally, you may find that you need to write data to a file. This can be accomplished using the DataWriter class. This can be done almost as simply as parsing, as follows:
writer: DataWriter = new DataWriter("path/to/file") # Constructs a DataWriter for the file you want to write to.
writer.open() # Opens the DataWriter so you can append to the file
writer.write(some_node) # Writes the node to the end of the file
writer.close() # Don't forget to do this, or you may have memory leaks!
Currently, it is not possible to overwrite or insert data using DataWriter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file weftspace-0.3.8.tar.gz.
File metadata
- Download URL: weftspace-0.3.8.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e02f05ea6367b0bca69b14f980f84284f0d4c0b1b7cadf7f77f8d9a6d8013f1
|
|
| MD5 |
7c547e2dbd024ee4a591ce5267a4bcb9
|
|
| BLAKE2b-256 |
5f9808a407f097bd9ef6fbc41e2a377e23d7cffea66eb74097eb2edaef0a054f
|
Provenance
The following attestation bundles were made for weftspace-0.3.8.tar.gz:
Publisher:
release.yml on mOctave/weftspace
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weftspace-0.3.8.tar.gz -
Subject digest:
2e02f05ea6367b0bca69b14f980f84284f0d4c0b1b7cadf7f77f8d9a6d8013f1 - Sigstore transparency entry: 315928797
- Sigstore integration time:
-
Permalink:
mOctave/weftspace@3d7d26816889ded41252684b34c25bd2c0c7864c -
Branch / Tag:
refs/tags/v0.3.8 - Owner: https://github.com/mOctave
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3d7d26816889ded41252684b34c25bd2c0c7864c -
Trigger Event:
release
-
Statement type:
File details
Details for the file weftspace-0.3.8-py3-none-any.whl.
File metadata
- Download URL: weftspace-0.3.8-py3-none-any.whl
- Upload date:
- Size: 27.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d57d8ce3d0dba72d56655bdbcc030e5781e560b8d26e2420a6bc364f0d87456
|
|
| MD5 |
10b4d1fdb1ca9dc84186d2fcc9f1d55d
|
|
| BLAKE2b-256 |
e9fce4b40d46e96968961456a849b22eea9248577cb7949ec2828ae99e30f1c0
|
Provenance
The following attestation bundles were made for weftspace-0.3.8-py3-none-any.whl:
Publisher:
release.yml on mOctave/weftspace
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weftspace-0.3.8-py3-none-any.whl -
Subject digest:
5d57d8ce3d0dba72d56655bdbcc030e5781e560b8d26e2420a6bc364f0d87456 - Sigstore transparency entry: 315928808
- Sigstore integration time:
-
Permalink:
mOctave/weftspace@3d7d26816889ded41252684b34c25bd2c0c7864c -
Branch / Tag:
refs/tags/v0.3.8 - Owner: https://github.com/mOctave
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3d7d26816889ded41252684b34c25bd2c0c7864c -
Trigger Event:
release
-
Statement type: