-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
|
@@ -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." | ||||||||||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
) | ||||||||||
|
||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
|
@@ -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): | ||||||||||
|
@@ -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 = [] | ||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this code and make |
||||||||||
|
||||||||||
self.g_debug.add_argument( | ||||||||||
"--single", | ||||||||||
default = False, | ||||||||||
action = "store_true", | ||||||||||
help = "Avoid use of multiprocessing.") | ||||||||||
|
||||||||||
@classmethod | ||||||||||
@abstractmethod | ||||||||||
def process( | ||||||||||
|
@@ -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) | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add |
||
- "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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
out : json.lobster | ||
inputs : | ||
- example.json |
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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete this file from this PR. |
There was a problem hiding this comment.
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.