Skip to main content

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:

  1. A script representing the entire input data consists of one or more statement.
  2. A statement consists of a command and zero or more argument.
  3. The command and each argument are separated by whitespace characters.
  4. The command always begins at the beginning of a line. If a line begins with a space character, it is considered a continuation of the previous line.
  5. 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 / false
  • yes / no
  • on / 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:

  1. Convert uppercase to lowercase
  2. Replace - with _
  3. 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)

Source distribution for inputscriptparser 1.2.1
File Size Uploaded
inputscriptparser-1.2.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for inputscriptparser 1.2.1
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page