Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd-config-for-lobster-tool-class #202

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

### 0.10.1-dev

* Introduced YAML-based configuration for `lobster-tool-class`, replacing individual command-line arguments.
* Added a `--config` argument to specify a YAML configuration file.
* Eliminated the need for direct command-line arguments like `--out`, `--single`, `--inputs`, and `--inputs-from-file`, simplifying user interaction.

* The title and placeholder for search box is renamed to `Filter` in
`lobster-html-report` tool.

Expand Down
59 changes: 26 additions & 33 deletions lobster/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from abc import ABCMeta, abstractmethod
from functools import partial
from typing import List, Union, Tuple

import yaml
from lobster.version import FULL_NAME, get_version
from lobster.errors import Message_Handler
from lobster.location import File_Reference
Expand Down Expand Up @@ -62,45 +62,43 @@ def __init__(self, name, description, extensions, official):
allow_abbrev = False)

self.g_common = self.ap.add_argument_group("common options")
self.g_debug = self.ap.add_argument_group("debug options")
self.g_tool = self.ap.add_argument_group("tool specific options")

self.g_common.add_argument(
"--out",
default = None,
help = "Write output to given file instead of stdout.")
"--config",
required=True,
help="Path to the YAML configuration file."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a detailed description for all the options that can go in the configuration file.

Comment on lines +69 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required=True,
help="Path to the YAML configuration file."
required = True,
help = "Path to the YAML configuration file."

)

self.g_common.add_argument(
"--inputs-from-file",
metavar = "FILE",
default = None,
help = ("Read input files or directories from this file."
" Each non-empty line is interpreted as one input."
" Supports comments starting with #."))

self.g_common.add_argument(
"inputs",
"--out",
default = None,
nargs = "*",
metavar = "FILE_OR_DIR",
help = ("List of files to process or directories to search"
" for relevant input files."))

self.g_common.add_argument(
"--traverse-bazel-dirs",
default = False,
action = "store_true",
help = ("Enter bazel-* directories, which are"
" excluded by default."))
help = "Write output to given file instead of stdout.")

self.add_argument = self.g_tool.add_argument

def load_yaml_config(self, config_path):
"""Loads configuration from a YAML file."""
if not os.path.isfile(config_path):
sys.exit(f"Error: Config file '{config_path}' not found.")
with open(config_path, "r", encoding="UTF-8") as f:
return yaml.safe_load(f) or {}

Comment on lines +80 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be added as utility function which then can be used by rest of the tools in future.

@get_version
def process_commandline_options(
self,
) -> Tuple[argparse.Namespace, List[Tuple[File_Reference, str]]]:
"""Processes all command line options"""

options = self.ap.parse_args()
config = self.load_yaml_config(options.config)

options.out = config.get("out")
options.inputs_from_file = config.get("inputs_from_file")
options.inputs = config.get("inputs", [])
options.traverse_bazel_dirs = config.get("traverse_bazel_dirs", False)
options.single = config.get("single", False)

work_list = self.process_common_options(options)
self.process_tool_options(options, work_list)
return options, work_list
Expand All @@ -121,7 +119,7 @@ def process_common_options(
# Assemble input requests
inputs = []
if options.inputs:
inputs += [(File_Reference("<cmdline>"), item)
inputs += [(File_Reference("<config>"), item)
for item in options.inputs]
if options.inputs_from_file:
if not os.path.isfile(options.inputs_from_file):
Expand All @@ -135,7 +133,7 @@ def process_common_options(
line_no),
line))
if not options.inputs and not options.inputs_from_file:
inputs.append((File_Reference("<cmdline>"), "."))
inputs.append((File_Reference("<config>"), "."))

# Sanity check and search directories
work_list = []
Expand Down Expand Up @@ -223,12 +221,6 @@ class LOBSTER_Per_File_Tool(LOBSTER_Tool):
def __init__(self, name, description, extensions, official=False):
super().__init__(name, description, extensions, official)
Comment on lines 221 to 222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this code and make official=False as keyword argument in LOBSTER_Tool (parent class).


self.g_debug.add_argument(
"--single",
default = False,
action = "store_true",
help = "Avoid use of multiprocessing.")

@classmethod
@abstractmethod
def process(
Expand All @@ -244,6 +236,7 @@ def execute(self):
ok = True
items = []
pfun = partial(self.process, options)

if options.single:
for file_name in work_list:
new_ok, new_items = pfun(file_name)
Expand Down
36 changes: 34 additions & 2 deletions packages/lobster-tool-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,38 @@ your own tool for your own files.

* `lobster-json`: Extract activities from JSON files

## Configuration via YAML

The tool uses a YAML configuration file to define the following common parameters:

1) inputs: A list of input file paths (can include directories).
2) out: The name of the output file where results will be stored.
3) inputs-from-file: A file containing paths to input files or directories.
4) single: A flag to avoid the use of multiprocessing. If true, multiprocessing will be skipped.

* YAML Configuration Example:

Below is an example of how you can define these parameters in the YAML configuration file:

```yaml
out: "output.lobster" # Output file where results will be written
inputs:
Copy link
Contributor

@mugdhadhole1 mugdhadhole1 Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add Inputs: example for directory as well?

- "file1.json"
- "file2.json"
inputs-from-file: "directory1/" # File containing a list of input files or directories
single: false # Set to true to avoid multiprocessing
```

* Command-Line Usage:

To run the tool with the specified YAML configuration file, use the following command:

```bash
lobster-json --config /path/to/config.yaml
```

Where /path/to/config.yaml is the path to your YAML configuration file.

## Usage

Some projects store their test vectors in JSON files. This tool can be
Expand Down Expand Up @@ -49,7 +81,7 @@ Here we have a list of three tests. You can configure the
$ lobster-json --name-attribute "name" \
--tag-attribute "tags" \
--justification-attribute "justification" \
FILENAME
--config "/path/to/config.yaml"
```

The name attribute is optional. If your test files do not contain
Expand Down Expand Up @@ -82,7 +114,7 @@ Then you can get to the data like so:
$ lobster-json --name-attribute "meta.name" \
--tag-attribute "meta.req" \
--justification-attribute "meta.just" \
FILENAME
--config "/path/to/config.yaml"
```

Finally, if your list of tests is nested more deeply in an object, you
Expand Down
7 changes: 3 additions & 4 deletions tests-integration/projects/basic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ software-requirements.lobster: potato.rsl potato.trlc
@lobster-trlc *.rsl *.trlc \
--out="software-requirements.lobster"

json.lobster: example.json
@lobster-json example.json \
--name-attribute "name" \
json.lobster:
@lobster-json --name-attribute "name" \
--tag-attribute "tags" \
--justification-attribute "justification" \
--out="json.lobster"
--config="config.yaml"
3 changes: 3 additions & 0 deletions tests-integration/projects/basic/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
out : json.lobster
inputs :
- example.json
7 changes: 3 additions & 4 deletions tests-integration/projects/filter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ requirements.lobster: example.rsl softreq_example.trlc sysreq_example.trlc
@lobster-trlc example.rsl softreq_example.trlc sysreq_example.trlc\
--out="requirements.lobster"

json.lobster: test_example.json
@lobster-json test_example.json \
--name-attribute "name" \
json.lobster:
@lobster-json --name-attribute "name" \
--tag-attribute "tags" \
--justification-attribute "justification" \
--out="json.lobster"
--config="config.yaml"
3 changes: 3 additions & 0 deletions tests-integration/projects/filter/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
out : json.lobster
inputs :
- test_example.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<cmdline>: lobster warning: not a .json file
<config>: lobster warning: not a .json file
lobster-json: wrote 2 items to output.lobster
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
--name-attribute=fruit
--tag-attribute=vegtable
--out=output.lobster
data.txt
data.json
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
out : output.lobster
inputs :
- data.txt
- data.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<cmdline>: lobster warning: not a .json file
<config>: lobster warning: not a .json file
lobster-json: wrote 1 items to output.lobster
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--name-attribute=fruit
--tag-attribute=vegtable
--out=output.lobster
data.txt
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
out : output.lobster
inputs :
- data.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<cmdline>: lobster error: file/does/not_exists.json is not a file or directory
<config>: lobster error: file/does/not_exists.json is not a file or directory
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--tag-attribute=pizza
file/does/not_exists.json
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inputs :
- file/does/not_exists.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<cmdline>: lobster error: file/does/not_exists.json is not a file or directory
<config>: lobster error: file/does/not_exists.json is not a file or directory
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--tag-attribute=soup
file_exists.json
file/does/not_exists.json
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs :
- file_exists.json
- file/does/not_exists.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--single
--tag-attribute=tags
--out=output.lobster
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
single : True
out : output.lobster
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
--single
--tag-attribute=tags
--out=output.lobster
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
single : True
out : output.lobster
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--single
--tag-attribute=tags
--name-attribute=name
--out=output.lobster
--config=config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
single : True
out : output.lobster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this file from this PR.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"commit": "main",
"file": "tests-system/lobster-online-report/rbt-valid-flow/valid-scenario/input/basic.py",
"line": 5,
"exec_commit_id": "47773e3ebff7ca2278723022bc98e990785c6458"
"exec_commit_id": "2ca85cc700a2a794853cbfd0baea304e24ea620e"
},
"name": "basic.trlc_reference",
"messages": [],
Expand All @@ -36,7 +36,7 @@
"commit": "main",
"file": "tests-system/lobster-online-report/rbt-valid-flow/valid-scenario/input/basic.py",
"line": 13,
"exec_commit_id": "47773e3ebff7ca2278723022bc98e990785c6458"
"exec_commit_id": "2ca85cc700a2a794853cbfd0baea304e24ea620e"
},
"name": "basic.Example.helper_function",
"messages": [],
Expand All @@ -55,7 +55,7 @@
"commit": "main",
"file": "tests-system/lobster-online-report/rbt-valid-flow/valid-scenario/input/basic.py",
"line": 17,
"exec_commit_id": "47773e3ebff7ca2278723022bc98e990785c6458"
"exec_commit_id": "2ca85cc700a2a794853cbfd0baea304e24ea620e"
},
"name": "basic.Example.nor",
"messages": [
Expand Down
Loading
Loading