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

Snakecase #18

Open
wants to merge 5 commits 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For the time being, there are two sets of annotation generators:

## Adding an annotation

- Add the command in the dictionary in (https://github.com/binpash/annotations/blob/main/pash_annotations/annotation_generation/AnnotationGeneration.py#L13)
- Add the command in the dictionary in (https://github.com/binpash/annotations/blob/main/pash_annotations/annotation_generation/AnnotationGenerator().py#L13)
- Add a json file with the command flags in (https://github.com/binpash/annotations/tree/main/pash_annotations/parser/command_flag_option_info/data). This could be used to generate a first version of it: (https://github.com/binpash/annotations/blob/main/pash_annotations/parser/command_flag_option_info/manpage-to-json.sh).
- Add an `InputOutputInfoGeneratorXXX.py` in (https://github.com/binpash/annotations/tree/main/pash_annotations/annotation_generation/annotation_generators)
- (Optionally) add a `ParallelizabilityInfoGeneratorXXX.py` in (https://github.com/binpash/annotations/tree/main/pash_annotations/annotation_generation/annotation_generators)
Expand Down
56 changes: 39 additions & 17 deletions pash_annotations/annotation_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@
from typing import Optional

# create parser
from annotation_generation.AnnotationGeneration import get_input_output_info_from_cmd_invocation, \
get_parallelizability_info_from_cmd_invocation
from pash_annotations.annotation_generation.annotation_generation import (
get_input_output_info_from_cmd_invocation,
get_parallelizability_info_from_cmd_invocation,
)

from annotation_generation.datatypes.InputOutputInfo import InputOutputInfo
from annotation_generation.datatypes.ParallelizabilityInfo import ParallelizabilityInfo
from datatypes.CommandInvocationInitial import CommandInvocationInitial
from parser.parser import parse
from pash_annotations.annotation_generation.datatypes.input_output_info import InputOutputInfo
from pash_annotations.annotation_generation.datatypes.parallelizability_info import ParallelizabilityInfo
from pash_annotations.datatypes.command_invocation_initial import CommandInvocationInitial
from pash_annotations.parser.parser import parse

parser = argparse.ArgumentParser()

# add arguments to the parser
parser.add_argument('--command_invocation', metavar='STRING', type=str, required=True,
help='specifies the command invocation to check (enclosed by \")')
parser.add_argument('--save_to', metavar='FILE', type=str, default=None,
help='store output in file (relative to where the script is called from); '
'will not overwrite existing files but then print instead')
parser.add_argument(
"--command_invocation",
metavar="STRING",
type=str,
required=True,
help='specifies the command invocation to check (enclosed by ")',
)
parser.add_argument(
"--save_to",
metavar="FILE",
type=str,
default=None,
help="store output in file (relative to where the script is called from); "
"will not overwrite existing files but then print instead",
)

# parse the arguments
options = parser.parse_args()

script_path = __file__
split_path = script_path.rpartition('/')
split_path = script_path.rpartition("/")
script_prefix = "".join(split_path[:2])

where_to_save = options.save_to
Expand All @@ -38,7 +50,9 @@
except IOError:
shall_we_write_to_file = False
if not shall_we_write_to_file:
print("There exists a file on the provided path so the result will be output (only). ")
print(
"There exists a file on the provided path so the result will be output (only). "
)


result = ""
Expand All @@ -49,18 +63,26 @@
result += str(command_invocation) + "\n"

result += ">>> INPUT-OUTPUT INFORMATION (applied to command invocation if possible): \n"
io_info: Optional[InputOutputInfo] = get_input_output_info_from_cmd_invocation(command_invocation)
io_info: Optional[InputOutputInfo] = get_input_output_info_from_cmd_invocation(
command_invocation
)
if io_info is None:
result += f"Information not provided so considered side-effectful."
elif io_info.has_other_outputs():
result += f"Provided command has outputs other than streaming."
else:
command_invocation_with_io = io_info.apply_input_output_info_to_command_invocation(command_invocation)
command_invocation_with_io = io_info.apply_input_output_info_to_command_invocation(
command_invocation
)
result += str(command_invocation_with_io)
result += "\n"
para_info: Optional[ParallelizabilityInfo] = get_parallelizability_info_from_cmd_invocation(command_invocation)
para_info: Optional[
ParallelizabilityInfo
] = get_parallelizability_info_from_cmd_invocation(command_invocation)
if para_info is None:
para_info = ParallelizabilityInfo() # defaults to no parallelizer's and all properties False
para_info = (
ParallelizabilityInfo()
) # defaults to no parallelizer's and all properties False
result += ">>> PARALLELIZABILITY INFORMATION: \n"
# TODO: change representation when we move commutativity into parallelizers
result += str(para_info)
Expand Down
98 changes: 0 additions & 98 deletions pash_annotations/annotation_generation/AnnotationGeneration.py

This file was deleted.

110 changes: 110 additions & 0 deletions pash_annotations/annotation_generation/annotation_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import os
import importlib
from typing import Optional

from collections import namedtuple

from pash_annotations.datatypes.command_invocation_initial import (
CommandInvocationInitial,
)
from pash_annotations.annotation_generation.datatypes.input_output_info import (
InputOutputInfo,
)
from pash_annotations.annotation_generation.datatypes.parallelizability_info import (
ParallelizabilityInfo,
)

### directory paths
ANNOTATION_GENERATORS = "pash_annotations.annotation_generation.annotation_generators"
CMD_NAMES = [
"alt_bigrams_aux",
"alt_bigram_aux_reduce",
"awk",
"bigrams_aux",
"bigram_aux_map",
"bigram_aux_reduce",
"cat",
"col",
"comm",
"custom_sort",
"custom_tr",
"cut",
"diff",
"grep",
"head",
"mkfifo",
"mv",
"rm",
"sed",
"set_diff",
"seq",
"sort",
"tail",
"tee",
"test_one",
"test_two",
"tr",
"uniq",
"xarg",
"wc",
]

IO_INFO_PREFIX = "input_output_info_generator"
PAR_INFO_PREFIX = "parallelizability_info_generator"

FileModulePair = namedtuple("FileModulePair", ["file", "module"])


class AnnotationGenerator:
# cannot be merged due to types
def get_input_output_info_from_cmd_invocation(
self,
cmd_invocation: CommandInvocationInitial,
) -> Optional[InputOutputInfo]:
try:
# Get the Generator, info_generator_class_for_cmd_repr, info_generator_class_for_cmd_repr
cmd = cmd_invocation.cmd_name
module = importlib.import_module(
f"{ANNOTATION_GENERATORS}.{IO_INFO_PREFIX}_{cmd}"
)
info_gen_class = getattr(
module, self.to_pascal_case(IO_INFO_PREFIX) + self.to_pascal_case(cmd)
)
# Initialize the info generator object
info_generator_object = info_gen_class(cmd_invocation)
# Generate info
info_generator_object.generate_info()
return info_generator_object.get_info()
except:
return None

def get_parallelizability_info_from_cmd_invocation(
self,
cmd_invocation: CommandInvocationInitial,
) -> Optional[ParallelizabilityInfo]:
# Get the Generator
try:
cmd = cmd_invocation.cmd_name
module = importlib.import_module(
f"{ANNOTATION_GENERATORS}.{PAR_INFO_PREFIX}_{cmd}"
)
print(module)
info_gen_class = getattr(
module, self.to_pascal_case(PAR_INFO_PREFIX) + self.to_pascal_case(cmd)
)
# Initialize the info generator object
info_generator_object = info_gen_class(cmd_invocation)
# Initialize the info generator object
# TODO: be more rigorous and allow empty parallelization annotation: return ParallelizabilityInfo with [] as default
# Generate info
info_generator_object.generate_info()
return info_generator_object.get_info()
except:
return None

@staticmethod
def to_pascal_case(string: str) -> str:
"""
Turns a snake_case string to PascalCase
"""
return string.replace("_", " ").title().replace(" ", "")

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading