Skip to content

Commit 07d875d

Browse files
authored
Merge pull request #699 from binpash/future-formatter
Add black formatter to future branch
2 parents c875873 + f657d68 commit 07d875d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2537
-1326
lines changed

.github/workflows/black.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Lint
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- uses: psf/black@stable
11+
with:
12+
options: "--extend-exclude 'evaluations/'"

compiler/annotations_utils/util_cmd_invocations.py

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
from pash_annotations.datatypes.BasicDatatypes import Flag, ArgStringType, Operand
22
from pash_annotations.datatypes.BasicDatatypesWithIO import OptionWithIO
33
from pash_annotations.datatypes.CommandInvocationInitial import CommandInvocationInitial
4-
from pash_annotations.annotation_generation.datatypes.InputOutputInfo import InputOutputInfo
5-
from pash_annotations.annotation_generation.datatypes.ParallelizabilityInfo import ParallelizabilityInfo
6-
from pash_annotations.annotation_generation.datatypes.CommandProperties import CommandProperties
7-
from pash_annotations.annotation_generation.AnnotationGeneration import get_input_output_info_from_cmd_invocation, \
8-
get_parallelizability_info_from_cmd_invocation
9-
from pash_annotations.datatypes.CommandInvocationWithIOVars import CommandInvocationWithIOVars
4+
from pash_annotations.annotation_generation.datatypes.InputOutputInfo import (
5+
InputOutputInfo,
6+
)
7+
from pash_annotations.annotation_generation.datatypes.ParallelizabilityInfo import (
8+
ParallelizabilityInfo,
9+
)
10+
from pash_annotations.annotation_generation.datatypes.CommandProperties import (
11+
CommandProperties,
12+
)
13+
from pash_annotations.annotation_generation.AnnotationGeneration import (
14+
get_input_output_info_from_cmd_invocation,
15+
get_parallelizability_info_from_cmd_invocation,
16+
)
17+
from pash_annotations.datatypes.CommandInvocationWithIOVars import (
18+
CommandInvocationWithIOVars,
19+
)
1020

1121
from definitions.ir.arg import Arg
1222

1323
# for typing
1424
from pash_annotations.datatypes.CommandInvocationPrefix import CommandInvocationPrefix
1525

16-
from shell_ast.ast_util import string_to_argument, redir_stdout_to_file, redir_file_to_stdin, make_command
26+
from shell_ast.ast_util import (
27+
string_to_argument,
28+
redir_stdout_to_file,
29+
redir_file_to_stdin,
30+
make_command,
31+
)
32+
1733

1834
def get_command_invocation_prefix_from_dfg_node(dfg_node):
19-
return CommandInvocationPrefix(cmd_name = dfg_node.com_name,
20-
flag_option_list = dfg_node.flag_option_list,
21-
positional_config_list = dfg_node.positional_config_list)
35+
return CommandInvocationPrefix(
36+
cmd_name=dfg_node.com_name,
37+
flag_option_list=dfg_node.flag_option_list,
38+
positional_config_list=dfg_node.positional_config_list,
39+
)
40+
2241

2342
# TODO: ideally methods in the respective classes but requires refactoring of parsing infrastructure
2443
# TODO: isn't this `to_ast`?
@@ -48,55 +67,70 @@ def to_node_cmd_inv_with_io_vars(cmd_inv, edges, redirs, assignments):
4867
node = make_command(cmd_asts, redirections=new_redirs, assignments=assignments)
4968
return node
5069

70+
5171
def to_ast_flagoption(flagoption, edges):
5272
if isinstance(flagoption, Flag):
5373
return [string_to_argument(flagoption.get_name())]
54-
elif isinstance(flagoption, OptionWithIO): # retype to IOVar
74+
elif isinstance(flagoption, OptionWithIO): # retype to IOVar
5575
opt_name_ast = string_to_argument(flagoption.get_name())
5676
opt_arg_ast = translate_io_var_if_applicable(flagoption.get_arg(), edges)
5777
return [opt_name_ast, opt_arg_ast]
5878

79+
5980
def to_ast_operand(operand, edges):
6081
if isinstance(operand, Operand):
6182
return translate_io_var_if_applicable(operand.get_name(), edges)
6283
return translate_io_var_if_applicable(operand, edges)
6384

85+
6486
def translate_io_var_if_applicable(pot_io_var, edges):
6587
# TODO: this is currently a hack but eventually every possible type gets their own to_ast-function
6688
if isinstance(pot_io_var, int):
6789
return dereference_io_var(pot_io_var, edges)
6890
elif isinstance(pot_io_var, ArgStringType):
6991
return to_ast_arg_string_type(pot_io_var)
7092
elif isinstance(pot_io_var, CommandInvocationWithIOVars):
71-
assert(False)
93+
assert False
7294
# only happens as r-wrapped node
7395
return to_node_cmd_inv_with_io_vars(pot_io_var, edges, [], [])
7496
elif isinstance(pot_io_var, Arg):
7597
return pot_io_var.to_ast()
7698
else:
7799
raise Exception("Unhandled type for operand in to_ast!")
78100

101+
79102
def to_ast_arg_string_type(arg_string_type):
80-
return arg_string_type.get_name().arg_char_list # is of type Arg
103+
return arg_string_type.get_name().arg_char_list # is of type Arg
104+
81105

82106
# assumes io_var is an edge id
83107
def dereference_io_var(io_var, edges):
84108
fid, _, _ = edges[io_var]
85109
return fid.to_ast()
86110

87-
def get_input_output_info_from_cmd_invocation_util(cmd_invocationInitial : CommandInvocationInitial) -> InputOutputInfo:
111+
112+
def get_input_output_info_from_cmd_invocation_util(
113+
cmd_invocationInitial: CommandInvocationInitial,
114+
) -> InputOutputInfo:
88115
return get_input_output_info_from_cmd_invocation(cmd_invocationInitial)
89116

90-
def get_parallelizability_info_from_cmd_invocation_util(cmd_invocationInitial : CommandInvocationInitial) -> ParallelizabilityInfo:
117+
118+
def get_parallelizability_info_from_cmd_invocation_util(
119+
cmd_invocationInitial: CommandInvocationInitial,
120+
) -> ParallelizabilityInfo:
91121
return get_parallelizability_info_from_cmd_invocation(cmd_invocationInitial)
92122

123+
93124
def construct_property_container_from_list_of_properties(list_properties):
94125
return CommandProperties(dict(list_properties))
95126

127+
96128
# this function is needed to wrap a node in `r_wrap`
97-
def to_arg_from_cmd_inv_with_io_vars_without_streaming_inputs_or_outputs_for_wrapping(cmd_inv, edges):
129+
def to_arg_from_cmd_inv_with_io_vars_without_streaming_inputs_or_outputs_for_wrapping(
130+
cmd_inv, edges
131+
):
98132
# we already expand here
99-
whole_cmd = Arg.string_to_arg("\'")
133+
whole_cmd = Arg.string_to_arg("'")
100134
arg_cmd_name = Arg.string_to_arg(cmd_inv.cmd_name)
101135
arg_flagoptions = []
102136
for flagoption in cmd_inv.flag_option_list:
@@ -107,9 +141,10 @@ def to_arg_from_cmd_inv_with_io_vars_without_streaming_inputs_or_outputs_for_wra
107141
all_cmd_parts_arg.extend(arg_operands)
108142
for part in all_cmd_parts_arg:
109143
whole_cmd.concatenate(part)
110-
whole_cmd.concatenate(Arg.string_to_arg("\'"))
144+
whole_cmd.concatenate(Arg.string_to_arg("'"))
111145
return whole_cmd
112146

147+
113148
def to_arg_flagoption(flagoption, edges):
114149
if isinstance(flagoption, Flag):
115150
return [Arg.string_to_arg(flagoption.get_name())]
@@ -118,11 +153,13 @@ def to_arg_flagoption(flagoption, edges):
118153
opt_arg_arg = translate_io_var_to_arg_if_applicable(flagoption.get_arg(), edges)
119154
return [opt_name_arg, opt_arg_arg]
120155

156+
121157
def to_arg_operand(operand, edges):
122158
if isinstance(operand, Operand):
123159
return translate_io_var_to_arg_if_applicable(operand.get_name(), edges)
124160
return translate_io_var_to_arg_if_applicable(operand, edges)
125161

162+
126163
def translate_io_var_to_arg_if_applicable(pot_io_var, edges):
127164
if isinstance(pot_io_var, int):
128165
return Arg(dereference_io_var(pot_io_var, edges))
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
from util import log
22
from definitions.ir.resource import FileResource, Resource, FileDescriptorResource
3-
from pash_annotations.datatypes.BasicDatatypesWithIO import FileNameWithIOInfo, StdDescriptorWithIOInfo
3+
from pash_annotations.datatypes.BasicDatatypesWithIO import (
4+
FileNameWithIOInfo,
5+
StdDescriptorWithIOInfo,
6+
)
47

58

69
def resource_from_file_descriptor(file_descriptor) -> Resource:
710
if isinstance(file_descriptor, FileNameWithIOInfo):
811
arg = file_descriptor.get_name()
9-
log(f'filedes name: {file_descriptor.get_name()}')
10-
log(f'filedes name type: {type(file_descriptor.get_name())}')
11-
log(f'arg: {arg}')
12+
log(f"filedes name: {file_descriptor.get_name()}")
13+
log(f"filedes name type: {type(file_descriptor.get_name())}")
14+
log(f"arg: {arg}")
1215
return FileResource(file_descriptor.get_name())
1316
elif isinstance(file_descriptor, StdDescriptorWithIOInfo):
1417
resource = ("fd", file_descriptor.get_type().value)
1518
return FileDescriptorResource(resource)
1619
else:
17-
assert(False)
20+
assert False
1821
# unreachable

compiler/annotations_utils/util_parsing.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
from definitions.ir.arg import Arg
44

55
from pash_annotations.datatypes.CommandInvocationInitial import CommandInvocationInitial
6-
from pash_annotations.datatypes.BasicDatatypes import Option, ArgStringType, Flag, Operand
7-
from pash_annotations.parser.parser import parse, get_set_of_all_flags, get_dict_flag_to_primary_repr, get_set_of_all_options, \
8-
get_dict_option_to_primary_repr, are_all_individually_flags
6+
from pash_annotations.datatypes.BasicDatatypes import (
7+
Option,
8+
ArgStringType,
9+
Flag,
10+
Operand,
11+
)
12+
from pash_annotations.parser.parser import (
13+
parse,
14+
get_set_of_all_flags,
15+
get_dict_flag_to_primary_repr,
16+
get_set_of_all_options,
17+
get_dict_option_to_primary_repr,
18+
are_all_individually_flags,
19+
)
920
from pash_annotations.parser.util_parser import get_json_data
1021

1122

@@ -18,40 +29,51 @@ def merge_to_single_string_with_space(list_str):
1829
else:
1930
return " ".join(list_str)
2031

32+
2133
def get_command_invocation(command, options) -> CommandInvocationInitial:
2234
command_as_string: str = format_arg_chars(command)
23-
options_and_operands_as_string: str = merge_to_single_string_with_space([format_arg_chars(option) for option in options])
24-
command_invocation_as_string: str = f'{command_as_string} {options_and_operands_as_string}'
35+
options_and_operands_as_string: str = merge_to_single_string_with_space(
36+
[format_arg_chars(option) for option in options]
37+
)
38+
command_invocation_as_string: str = (
39+
f"{command_as_string} {options_and_operands_as_string}"
40+
)
2541
command_invocation: CommandInvocationInitial = parse(command_invocation_as_string)
2642
return command_invocation
2743

44+
2845
def get_ast_for_flagoption(flagoption):
2946
result = string_to_argument(flagoption.get_name())
3047
if isinstance(flagoption, Option):
3148
# TODO: add argument here as well but eventually also fid
3249
assert False
3350
return result
3451

52+
3553
def get_ast_for_argstringtype(arg):
3654
return string_to_argument(arg.get_name())
3755

56+
3857
# TODO: this is a hack to fix the wrong parsing of "
3958
def fix_parsing_newline(arg):
40-
if arg.get_name() == '\\n':
59+
if arg.get_name() == "\\n":
4160
return ArgStringType(r'"\n"')
4261
else:
4362
return arg
4463

4564

46-
def parse_arg_list_to_command_invocation(command, flags_options_operands) -> CommandInvocationInitial:
47-
65+
def parse_arg_list_to_command_invocation(
66+
command, flags_options_operands
67+
) -> CommandInvocationInitial:
4868
cmd_name = format_arg_chars(command)
4969
json_data = get_json_data(cmd_name)
5070

5171
set_of_all_flags: Set[str] = get_set_of_all_flags(json_data)
5272
dict_flag_to_primary_repr: dict[str, str] = get_dict_flag_to_primary_repr(json_data)
5373
set_of_all_options: Set[str] = get_set_of_all_options(json_data)
54-
dict_option_to_primary_repr: dict[str, str] = get_dict_option_to_primary_repr(json_data)
74+
dict_option_to_primary_repr: dict[str, str] = get_dict_option_to_primary_repr(
75+
json_data
76+
)
5577
# we keep the Arg for everything but flag and option names
5678

5779
# parse list of command invocation terms
@@ -61,20 +83,30 @@ def parse_arg_list_to_command_invocation(command, flags_options_operands) -> Com
6183
potential_flag_or_option_arg = flags_options_operands[i]
6284
potential_flag_or_option_name = format_arg_chars(potential_flag_or_option_arg)
6385
if potential_flag_or_option_name in set_of_all_flags:
64-
flag_name_as_string: str = dict_flag_to_primary_repr.get(potential_flag_or_option_name, potential_flag_or_option_name)
86+
flag_name_as_string: str = dict_flag_to_primary_repr.get(
87+
potential_flag_or_option_name, potential_flag_or_option_name
88+
)
6589
flag: Flag = Flag(flag_name_as_string)
6690
flag_option_list.append(flag)
67-
elif (potential_flag_or_option_name in set_of_all_options) and ((i+1) < len(flags_options_operands)):
68-
option_name_as_string: str = dict_option_to_primary_repr.get(potential_flag_or_option_name, potential_flag_or_option_name)
69-
option_arg_as_arg: Arg = Arg(flags_options_operands[i+1])
91+
elif (potential_flag_or_option_name in set_of_all_options) and (
92+
(i + 1) < len(flags_options_operands)
93+
):
94+
option_name_as_string: str = dict_option_to_primary_repr.get(
95+
potential_flag_or_option_name, potential_flag_or_option_name
96+
)
97+
option_arg_as_arg: Arg = Arg(flags_options_operands[i + 1])
7098
option = Option(option_name_as_string, option_arg_as_arg)
7199
flag_option_list.append(option)
72100
i += 1 # since we consumed another term for the argument
73-
elif potential_flag_or_option_name == "-": # switch to operand mode (interpreted as hyphen-stdin)
101+
elif (
102+
potential_flag_or_option_name == "-"
103+
): # switch to operand mode (interpreted as hyphen-stdin)
74104
break
75-
elif are_all_individually_flags(potential_flag_or_option_name, set_of_all_flags):
105+
elif are_all_individually_flags(
106+
potential_flag_or_option_name, set_of_all_flags
107+
):
76108
for split_el in list(potential_flag_or_option_name[1:]):
77-
flag: Flag = Flag(f'-{split_el}')
109+
flag: Flag = Flag(f"-{split_el}")
78110
flag_option_list.append(flag)
79111
else:
80112
break # next one is Operand, and we keep these in separate list
@@ -85,7 +117,9 @@ def parse_arg_list_to_command_invocation(command, flags_options_operands) -> Com
85117
# if parsed_elements_list[i] == '--':
86118
# i += 1
87119

88-
operand_list = [Operand(Arg(operand_arg)) for operand_arg in flags_options_operands[i:]]
120+
operand_list = [
121+
Operand(Arg(operand_arg)) for operand_arg in flags_options_operands[i:]
122+
]
89123
# log("type of operand_list[0].get_name()", type(operand_list[0].get_name())) can only be used if there are operands
90124

91125
return CommandInvocationInitial(cmd_name, flag_option_list, operand_list)

0 commit comments

Comments
 (0)