Skip to content

Commit

Permalink
fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Forthoney committed Nov 25, 2023
1 parent 8ec13c7 commit 176ed73
Show file tree
Hide file tree
Showing 68 changed files with 1,533 additions and 704 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
from pash_annotations.datatypes.command_invocation_initial import (
CommandInvocationInitial,
)
from datatypes.input_output_info import InputOutputInfo
from datatypes.parallelizability_info import ParallelizabilityInfo
from pash_annotations.annotation_generation.datatypes.input_output_info import (
InputOutputInfo,
)
from pash_annotations.annotation_generation.datatypes.parallelizability_info import (
ParallelizabilityInfo,
)

### directory paths
ROOT_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../.."))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import List
from abc import ABC, abstractmethod

from datatypes.basic_datatypes import FlagOption
from datatypes.command_invocation_initial import CommandInvocationInitial
from pash_annotations.datatypes.basic_datatypes import FlagOption
from pash_annotations.datatypes.command_invocation_initial import (
CommandInvocationInitial,
)


class Generator_Interface(ABC):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorCol(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdin()
self.set_implicit_use_of_stdout()
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class InputOutputInfoGeneratorComm(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdout()
assert(self.get_operand_list_length() == 2) # needs two files to compare;
assert self.get_operand_list_length() == 2 # needs two files to compare;
self.all_operands_are_streaming_inputs()
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@


class InputOutputInfoGeneratorCustomSort(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
# copied from sort
if not self.does_flag_option_list_contain_at_least_one_of(["-o"]):
self.set_implicit_use_of_stdout()
if self.get_operand_list_length() == 0 and not self.does_flag_option_list_contain_at_least_one_of(["--files0-from"]):
if (
self.get_operand_list_length() == 0
and not self.does_flag_option_list_contain_at_least_one_of(
["--files0-from"]
)
):
self.set_implicit_use_of_stdin()
else:
self.all_operands_are_streaming_inputs()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorCut(InputOutputInfoGeneratorInterface):

# list_of_all_flags = ["-n", "--complement", "-s", "-z", "--help", "--version"]
# list_of_all_options = ["-b", "-c", "-d", "-f", "--output-delimiter", ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorDiff(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdout()
self.all_operands_are_streaming_inputs()
self.all_operands_are_streaming_inputs()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorHead(InputOutputInfoGeneratorInterface):

# list_of_all_flags = ["-q", "-v", "-z", "--help", "--version"]
# list_of_all_options = ["-c", "-n"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
from typing import List, Tuple, Union, Literal, Dict

from pash_annotations.config.definitions import INDICATORS_FOR_FILENAMES
from config.definitions import INDICATORS_FOR_FILENAMES

from pash_annotations.datatypes.BasicDatatypes import Flag, Option, WhichClassForArg
from pash_annotations.datatypes.AccessKind import AccessKind, get_access_from_string
from pash_annotations.datatypes.CommandInvocationInitial import CommandInvocationInitial
from pash_annotations.datatypes.CommandInvocationWithIO import CommandInvocationWithIO
from pash_annotations.datatypes.basic_datatypes import Flag, Option, WhichClassForArg
from pash_annotations.datatypes.access_kind import AccessKind, get_access_from_string
from pash_annotations.datatypes.command_invocation_initial import (
CommandInvocationInitial,
)
from pash_annotations.datatypes.command_invocation_with_io import (
CommandInvocationWithIO,
)

from pash_annotations.annotation_generation.annotation_generators.Generator_Interface import Generator_Interface
from pash_annotations.annotation_generation.annotation_generators.generator_interface import (
Generator_Interface,
)

from abc import ABC, abstractmethod

from pash_annotations.annotation_generation.datatypes.InputOutputInfo import InputOutputInfo
from pash_annotations.annotation_generation.datatypes.input_output_info import (
InputOutputInfo,
)

from pash_annotations.parser.util_parser import get_json_data


class InputOutputInfoGeneratorInterface(Generator_Interface, ABC):

# here, we only need to specify information about operands and implicitly used resources
# information about option arguments are provided by parsing infrastructure
# ASSUMPTION: No implicit information should be exploited since internal implementation may change


def __init__(self, cmd_invocation: CommandInvocationInitial) -> None:
super().__init__(cmd_invocation=cmd_invocation)
flagoption_list_typer: List[Union[Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
Tuple[Literal[WhichClassForArg.PLAINSTRING], None]]] \
= self.get_flagoption_list_typer_for_specific_list()
flagoption_list_typer: List[
Union[
Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
Tuple[Literal[WhichClassForArg.PLAINSTRING], None],
]
] = self.get_flagoption_list_typer_for_specific_list()
self.input_output_info: InputOutputInfo = InputOutputInfo(
flagoption_list_typer=flagoption_list_typer,
number_of_operands=len(cmd_invocation.operand_list)
)
flagoption_list_typer=flagoption_list_typer,
number_of_operands=len(cmd_invocation.operand_list),
)

@abstractmethod
def generate_info(self) -> None:
Expand All @@ -41,29 +50,57 @@ def generate_info(self) -> None:
def get_info(self) -> InputOutputInfo:
return self.input_output_info

def get_cmd_inv_with_io(self, cmd_inv: CommandInvocationInitial) -> CommandInvocationWithIO:
return self.input_output_info.apply_input_output_info_to_command_invocation(cmd_inv)

def get_flagoption_list_typer_for_specific_list(self) -> \
List[Union[Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
Tuple[Literal[WhichClassForArg.PLAINSTRING], None]]]:
dict_option_to_class_for_arg: Dict[str, Union[Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None]]] = self.get_dict_option_to_class_for_arg()
def get_cmd_inv_with_io(
self, cmd_inv: CommandInvocationInitial
) -> CommandInvocationWithIO:
return self.input_output_info.apply_input_output_info_to_command_invocation(
cmd_inv
)

def get_flagoption_list_typer_for_specific_list(
self,
) -> List[
Union[
Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
Tuple[Literal[WhichClassForArg.PLAINSTRING], None],
]
]:
dict_option_to_class_for_arg: Dict[
str,
Union[
Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
],
] = self.get_dict_option_to_class_for_arg()
flagoption_list_typer = []
for flagoption in self.cmd_inv.flag_option_list:
if isinstance(flagoption, Flag):
flagoption_list_typer.append((WhichClassForArg.PLAINSTRING, None))
elif isinstance(flagoption, Option):
flagoption_list_typer.append(dict_option_to_class_for_arg[flagoption.get_name()])
flagoption_list_typer.append(
dict_option_to_class_for_arg[flagoption.get_name()]
)
else:
raise Exception("neither Flag nor Option")
return flagoption_list_typer

def get_dict_option_to_class_for_arg(self) -> Dict[str, Union[Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None]]]:
dict_option_to_class_for_arg: Dict[str, Union[Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None]]] = dict()
def get_dict_option_to_class_for_arg(
self,
) -> Dict[
str,
Union[
Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
],
]:
dict_option_to_class_for_arg: Dict[
str,
Union[
Tuple[Literal[WhichClassForArg.FILESTD], AccessKind],
Tuple[Literal[WhichClassForArg.ARGSTRING], None],
],
] = dict()
json_data = get_json_data(self.cmd_inv.cmd_name)
for option_data in json_data["option"]:
option_name = option_data[0]
Expand All @@ -75,14 +112,25 @@ def get_dict_option_to_class_for_arg(self) -> Dict[str, Union[Tuple[Literal[Whic
access: AccessKind = get_access_from_string(option_arg_access_str)
if option_arg_type in INDICATORS_FOR_FILENAMES:
# for now, we do not allow to have '-' for stdin in option arguments
dict_option_to_class_for_arg[option_name] = (WhichClassForArg.FILESTD, access)
dict_option_to_class_for_arg[option_name] = (
WhichClassForArg.FILESTD,
access,
)
else:
dict_option_to_class_for_arg[option_name] = (WhichClassForArg.ARGSTRING, None)
dict_option_to_class_for_arg[option_name] = (
WhichClassForArg.ARGSTRING,
None,
)
else:
option_arg_type: str = option_arg_info
assert(not option_arg_type in INDICATORS_FOR_FILENAMES) # filenames need to declare access pattern, no default
assert (
not option_arg_type in INDICATORS_FOR_FILENAMES
) # filenames need to declare access pattern, no default
# access: AccessKind = AccessKind.make_config_input()
dict_option_to_class_for_arg[option_name] = (WhichClassForArg.ARGSTRING, None)
dict_option_to_class_for_arg[option_name] = (
WhichClassForArg.ARGSTRING,
None,
)
return dict_option_to_class_for_arg

## Library functions
Expand All @@ -99,7 +147,6 @@ def if_no_operands_given_stdin_implicitly_used(self) -> None:
if len(self.cmd_inv.operand_list) == 0:
self.set_implicit_use_of_stdin(True)


# forwarded to InputOutputInfo
# Assumption: streaming inputs are always filenames or stdin
# Assumption: (streaming) outputs are always filenames or stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@


class InputOutputInfoGeneratorRm(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.all_operands_are_other_outputs()
self.all_operands_are_other_outputs()
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorSed(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdin()
self.set_implicit_use_of_stdout()
Expand All @@ -11,4 +10,3 @@ def generate_info(self) -> None:
self.all_but_first_operand_is_streaming_input()
else:
self.all_operands_are_streaming_inputs()

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class InputOutputInfoGeneratorSeq(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdout()
self.set_all_operands_as_config_arg_type_string()
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@


class InputOutputInfoGeneratorSetDiff(InputOutputInfoGeneratorInterface):

def generate_info(self) -> None:
self.set_implicit_use_of_stdin()
self.set_implicit_use_of_stdout()
assert(self.get_operand_list_length() == 1)
self.set_first_operand_as_config_arg_type_filename_or_std_descriptor()
assert self.get_operand_list_length() == 1
self.set_first_operand_as_config_arg_type_filename_or_std_descriptor()
Loading

0 comments on commit 176ed73

Please sign in to comment.