A parser for script-style input data.
Project description
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.
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 inputscriptparser-1.2.1.tar.gz.
File metadata
- Download URL: inputscriptparser-1.2.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.8.0-60-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
76aa00bf9c8518087243cebcde00ae6fad85bae93fa475dc58d7359987d6c628
|
|
| MD5 |
f3d8d0c630a75c6290f7fe11474c90f2
|
|
| BLAKE2b-256 |
9c56d3ca4b4ca5ab1c0660f8b5263bb0ebe35676157412465d1e023b63340af8
|
File details
Details for the file inputscriptparser-1.2.1-py3-none-any.whl.
File metadata
- Download URL: inputscriptparser-1.2.1-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Linux/6.8.0-60-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1344ed20b016e2336df7e37dd657b78b773e396896810951cf3a540de02ce1af
|
|
| MD5 |
2a99db43aa247f40c9e4eedac5503d2e
|
|
| BLAKE2b-256 |
3fdb5084058bac970b769cc0506c910257f82a299015ec38f6599f0b3e3c724a
|