Skip to content

Commit

Permalink
use importlib for importing modules safely
Browse files Browse the repository at this point in the history
  • Loading branch information
Forthoney committed Nov 25, 2023
1 parent 176ed73 commit ca57796
Show file tree
Hide file tree
Showing 56 changed files with 179 additions and 196 deletions.
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
125 changes: 54 additions & 71 deletions pash_annotations/annotation_generation/annotation_generation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import sys
import importlib
from typing import Optional

from collections import namedtuple
Expand All @@ -15,7 +15,6 @@
)

### directory paths
ROOT_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "../.."))
ANNOTATION_GENERATORS = "pash_annotations.annotation_generation.annotation_generators"
CMD_NAMES = [
"alt_bigrams_aux",
Expand Down Expand Up @@ -50,78 +49,62 @@
"wc",
]


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


IO_INFO_PREFIX = "input_output_info_generator"
PAR_INFO_PREFIX = "parallelizability_info_generator"

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

io_info_gen_file_module_names = [
FileModulePair(
f"{ANNOTATION_GENERATORS}.{IO_INFO_PREFIX}_{cmd}",
to_pascal_case(IO_INFO_PREFIX) + to_pascal_case(cmd),
)
for cmd in CMD_NAMES
]

par_info_gen_file_module_names = [
FileModulePair(
f"{ANNOTATION_GENERATORS}.{PAR_INFO_PREFIX}_{cmd}",
to_pascal_case(PAR_INFO_PREFIX) + to_pascal_case(cmd),
)
for cmd in CMD_NAMES
]

for filename, module in io_info_gen_file_module_names + par_info_gen_file_module_names:
import_str = "from " + filename + " import " + module
try:
exec(import_str)
except ModuleNotFoundError:
pass # it's fine if some do not exist, we catch that later


# cannot be merged due to types
def get_input_output_info_from_cmd_invocation(
cmd_invocation: CommandInvocationInitial,
) -> Optional[InputOutputInfo]:
# Get the Generator, info_generator_class_for_cmd_repr, info_generator_class_for_cmd_repr
try:
info_generator_class_for_cmd = str_to_class(
to_pascal_case(IO_INFO_PREFIX) + to_pascal_case(cmd_invocation.cmd_name)
)
# Initialize the info generator object
info_generator_object = info_generator_class_for_cmd(cmd_invocation)
# Generate info
info_generator_object.generate_info()
return info_generator_object.get_info()
except Exception as sth: # module does not exist
return None


def get_parallelizability_info_from_cmd_invocation(
cmd_invocation: CommandInvocationInitial,
) -> Optional[ParallelizabilityInfo]:
# Get the Generator
try:
info_generator_class_for_cmd = str_to_class(
to_pascal_case(PAR_INFO_PREFIX) + to_pascal_case(cmd_invocation.cmd_name)
)
# Initialize the info generator object
# TODO: be more rigorous and allow empty parallelization annotation: return ParallelizabilityInfo with [] as default
info_generator_object = info_generator_class_for_cmd(cmd_invocation)
# Generate info
info_generator_object.generate_info()
return info_generator_object.get_info()
except Exception: # module does not exist
return None


def str_to_class(classname):
return getattr(sys.modules[__name__], classname)
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(" ", "")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorAltBigramAuxReduce(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorAltBigramsAux(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorAwk(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorBigramsAux(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorCat(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorCol(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorComm(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorCustomSort(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorCustomTr(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorCut(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorDiff(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorGrep(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorHead(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Tuple, Union, Literal, Dict

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

from pash_annotations.datatypes.basic_datatypes import Flag, Option, WhichClassForArg
from pash_annotations.datatypes.access_kind import AccessKind, get_access_from_string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorMkfifo(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List
from pash_annotations.datatypes.basic_datatypes import FlagOption
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorMv(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorRm(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorSed(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorSeq(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorSetDiff(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorSort(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorTail(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorTee(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorTestOne(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorTestTwo(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorTr(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorUniq(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorWc(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from input_output_info_generator_interface import InputOutputInfoGeneratorInterface
from pash_annotations.annotation_generation.annotation_generators.input_output_info_generator_interface import InputOutputInfoGeneratorInterface


class InputOutputInfoGeneratorXargs(InputOutputInfoGeneratorInterface):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.aggregator_spec import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.AggregatorSpec import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.aggregator_spec import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.parallelizer import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.aggregator_spec import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pash_annotations.annotation_generation.datatypes.parallelizability_info_generator_interface import (
from pash_annotations.annotation_generation.annotation_generators.parallelizability_info_generator_interface import (
ParallelizabilityInfoGeneratorInterface,
)
from pash_annotations.annotation_generation.datatypes.parallelizability.aggregator_spec import (
Expand Down
Loading

0 comments on commit ca57796

Please sign in to comment.