inputscriptparser
A parser for script-style input data.
"script-style" means assembling the internal representation of the input data by executing commands one at a time. A close example is the Dockerfile.
Install
inputscriptparser can be installed with pip:
$ pip install inputscriptparser
Or add it to your project using the package manager:
$ poetry add inputscriptparser
Usage
This parser converts text input into a list of tuples consisting of commands and their arguments.
from inputscriptparser import Parser
input_data = '''CMD1 "example data"
CMD2 1 2 3
NO-ARG-CMD
'''
parser = Parser()
script = parser.parse(input_data)
In the example above, the script parsing the input would be:
[
('CMD1', ['example data']),
('CMD2', [1.0, 2.0, 3.0]),
('NO-ARG-CMD', [])
]
The interpreter that executes each command must be provided by the user.
Grammar
The grammar is as follows:
- A
scriptrepresenting the entire input data consists of one or morestatement. - A
statementconsists of acommandand zero or moreargument. - The
commandand eachargumentare separated by whitespace characters. - The
commandalways begins at the beginning of a line. If a line begins with a space character, it is considered a continuation of the previous line. - Lines from
//to the end of the line are ignored as comments. Blank lines and lines containing only whitespace are also ignored.
Command
The command consists of latin uppercase letters, digits, and -; and must begin with a uppercase letter.
Argument values
The following types can be used as argument values:
- number
- string
- boolean
- keyword
Numbers do not distinguish between integers and real numbers. All numbers are real numbers.
Strings are enclosed in " (double quotation marks) ".
Boolean can be lowercase words that can be considered True or False.
true/falseyes/noon/off
Keywords consist of Latin uppercase letters and digits and always begin with an uppercase letter. They are similar to commands, but are distinguished by the fact that they do not appear at the beginning of a line.
Interpreter
The inputscriptparser.Interpreter class is the base class of the interpreters.
To implement an interpreter, extend inputscriptparser.Interpreter and implement methods corresponding to each command. The correspondence between commands and methods is bound by the following rules:
- Convert uppercase to lowercase
- Replace
-with_ - Prefix
_to the beginning
In short, the NO-ARG-CMD command corresponds to the _no_arg_cmd method.
The input data interpreter for the first example is as follows:
from inputscriptparser import Interpreter
class ExampleInterpreter(Interpreter):
def _cmd1(self, args):
self.state['CMD1'] = args[0]
def _cmd2(self, args):
self.state['CMD2'] = sum(args)
def _no_arg_cmd(self, args):
self.state['NO-ARG-CMD'] = 'This is a no-argument-command.'
Create an instance of the interpreter and call the run method.
interpreter = ExampleInterpreter({})
state = interpreter.run(script)
for k, v in state.items():
print(f'{k} = {v}')
The argument of the constructor is an object (here an empty dict) to store the state. It can be accessed from inside the interpreter as self.state.
The state object after executing the script is obtained as the return value of the run method.
And the following output is obtained:
CMD1 = example data
CMD2 = 6.0
NO-ARG-CMD = This is a no-argument-command.
CLI tool ispshow
The CLI tool ispshow parses input data and displays the results (commands and arguments) in an easy-to-read format.
$ ispshow examples/inputscript.dat
License
MIT license.
Metadata
Release files for inputscriptparser 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| inputscriptparser-1.2.1.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| inputscriptparser-1.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / inputscriptparser-1.2.1.tar.gz
| Download URL | inputscriptparser-1.2.1.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
76aa00bf9c8518087243cebcde00ae6fad85bae93fa475dc58d7359987d6c628
|
|
BLAKE2b-256 checksum How to use checksums |
9c56d3ca4b4ca5ab1c0660f8b5263bb0ebe35676157412465d1e023b63340af8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.3 Linux/6.8.0-60-generic
|
Release files / inputscriptparser-1.2.1-py3-none-any.whl
| Download URL | inputscriptparser-1.2.1-py3-none-any.whl |
|---|---|
| Size | 7.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1344ed20b016e2336df7e37dd657b78b773e396896810951cf3a540de02ce1af
|
|
BLAKE2b-256 checksum How to use checksums |
3fdb5084058bac970b769cc0506c910257f82a299015ec38f6599f0b3e3c724a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.3 Linux/6.8.0-60-generic
|