Skip to content

Commit

Permalink
python: Improve yapf formatting (envoyproxy#15587)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax authored Mar 25, 2021
1 parent 54fc5dc commit 83d0d99
Show file tree
Hide file tree
Showing 68 changed files with 1,030 additions and 913 deletions.
5 changes: 3 additions & 2 deletions api/tools/generate_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def generate_listeners(listeners_pb_path, output_pb_path, output_json_path, frag

if __name__ == '__main__':
if len(sys.argv) < 4:
print('Usage: %s <path to listeners.pb> <output listeners.pb> <output '
'listeners.json> <filter config fragment paths>') % sys.argv[0]
print(
'Usage: %s <path to listeners.pb> <output listeners.pb> <output '
'listeners.json> <filter config fragment paths>') % sys.argv[0]
sys.exit(1)

generate_listeners(sys.argv[1], sys.argv[2], sys.argv[3], iter(sys.argv[4:]))
20 changes: 11 additions & 9 deletions ci/flaky_test/process_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def parse_and_print_test_suite_error(testsuite, log_path):
last_testcase = last_test_fullname.split('.')[1]

if error_msg != "":
return print_test_suite_error(last_testsuite, last_testcase, log_path, test_duration,
test_time, error_msg, test_output)
return print_test_suite_error(
last_testsuite, last_testcase, log_path, test_duration, test_time, error_msg,
test_output)

return ""

Expand All @@ -135,9 +136,9 @@ def parse_xml(file, visited):
for testcase in testsuite:
for failure_msg in testcase:
if (testcase.attrib['name'], testsuite.attrib['name']) not in visited:
ret += print_test_case_failure(testcase.attrib['name'],
testsuite.attrib['name'], failure_msg.text,
log_file_path)
ret += print_test_case_failure(
testcase.attrib['name'], testsuite.attrib['name'], failure_msg.text,
log_file_path)
visited.add((testcase.attrib['name'], testsuite.attrib['name']))
elif testsuite.attrib['errors'] != '0':
# If an unexpected error occurred, such as an exception or a timeout, the test suite was
Expand Down Expand Up @@ -174,8 +175,8 @@ def get_git_info(CI_TARGET):
ret += "Target: {}\n".format(CI_TARGET)

if os.getenv('SYSTEM_STAGEDISPLAYNAME') and os.getenv('SYSTEM_STAGEJOBNAME'):
ret += "Stage: {} {}\n".format(os.environ['SYSTEM_STAGEDISPLAYNAME'],
os.environ['SYSTEM_STAGEJOBNAME'])
ret += "Stage: {} {}\n".format(
os.environ['SYSTEM_STAGEDISPLAYNAME'], os.environ['SYSTEM_STAGEJOBNAME'])

if os.getenv('BUILD_REASON') == "PullRequest" and os.getenv(
'SYSTEM_PULLREQUEST_PULLREQUESTNUMBER'):
Expand Down Expand Up @@ -232,8 +233,9 @@ def get_git_info(CI_TARGET):
find_dir = "{}/**/**/**/**/bazel-testlogs/".format(os.environ['TEST_TMPDIR']).replace('\\', '/')
if CI_TARGET == "MacOS":
find_dir = '${TEST_TMPDIR}/'
os.system('sh -c "/usr/bin/find {} -name attempt_*.xml > ${{TMP_OUTPUT_PROCESS_XML}}"'.format(
find_dir))
os.system(
'sh -c "/usr/bin/find {} -name attempt_*.xml > ${{TMP_OUTPUT_PROCESS_XML}}"'.format(
find_dir))

# All output of find command should be either failed or flaky tests, as only then will
# a test be rerun and have an 'attempt_n.xml' file. problematic_tests holds a lookup
Expand Down
38 changes: 21 additions & 17 deletions configs/configgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@

def generate_config(template_path, template, output_file, **context):
""" Generate a final config file based on a template and some context. """
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path, followlinks=True),
undefined=jinja2.StrictUndefined)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_path, followlinks=True),
undefined=jinja2.StrictUndefined)
raw_output = env.get_template(template).render(**context)
with open(output_file, 'w') as fh:
fh.write(raw_output)
Expand All @@ -108,18 +109,20 @@ def generate_config(template_path, template, output_file, **context):

# Generate a demo config for the main front proxy. This sets up both HTTP and HTTPS listeners,
# as well as a listener for the double proxy to connect to via SSL client authentication.
generate_config(SCRIPT_DIR,
'envoy_front_proxy.template.yaml',
'{}/envoy_front_proxy.yaml'.format(OUT_DIR),
clusters=front_envoy_clusters,
tracing=tracing_enabled)
generate_config(
SCRIPT_DIR,
'envoy_front_proxy.template.yaml',
'{}/envoy_front_proxy.yaml'.format(OUT_DIR),
clusters=front_envoy_clusters,
tracing=tracing_enabled)

# Generate a demo config for the double proxy. This sets up both an HTTP and HTTPS listeners,
# and backhauls the traffic to the main front proxy.
generate_config(SCRIPT_DIR,
'envoy_double_proxy.template.yaml',
'{}/envoy_double_proxy.yaml'.format(OUT_DIR),
tracing=tracing_enabled)
generate_config(
SCRIPT_DIR,
'envoy_double_proxy.template.yaml',
'{}/envoy_double_proxy.yaml'.format(OUT_DIR),
tracing=tracing_enabled)

# Generate a demo config for the service to service (local) proxy. This sets up several different
# listeners:
Expand All @@ -129,12 +132,13 @@ def generate_config(template_path, template, output_file, **context):
# optional external service ports: built from external_virtual_hosts above. Each external host
# that Envoy proxies to listens on its own port.
# optional mongo ports: built from mongos_servers above.
generate_config(SCRIPT_DIR,
'envoy_service_to_service.template.yaml',
'{}/envoy_service_to_service.yaml'.format(OUT_DIR),
internal_virtual_hosts=service_to_service_envoy_clusters,
external_virtual_hosts=external_virtual_hosts,
mongos_servers=mongos_servers)
generate_config(
SCRIPT_DIR,
'envoy_service_to_service.template.yaml',
'{}/envoy_service_to_service.yaml'.format(OUT_DIR),
internal_virtual_hosts=service_to_service_envoy_clusters,
external_virtual_hosts=external_virtual_hosts,
mongos_servers=mongos_servers)

shutil.copy(os.path.join(SCRIPT_DIR, 'envoyproxy_io_proxy.yaml'), OUT_DIR)
shutil.copy(os.path.join(SCRIPT_DIR, 'encapsulate_in_http1_connect.yaml'), OUT_DIR)
Expand Down
5 changes: 3 additions & 2 deletions configs/example_configs_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def main():
errors = []
for arg in sys.argv[1:]:
try:
validate_fragment("envoy.config.bootstrap.v3.Bootstrap",
yaml.safe_load(pathlib.Path(arg).read_text()))
validate_fragment(
"envoy.config.bootstrap.v3.Bootstrap",
yaml.safe_load(pathlib.Path(arg).read_text()))
except (ParseError, KeyError) as e:
errors.append(arg)
print(f"\nERROR (validation failed): {arg}\n{e}\n\n")
Expand Down
6 changes: 2 additions & 4 deletions docs/_ext/validating_code_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ def run(self):
'bazel-bin/tools/config_validation/validate_fragment',
self.options.get('type-name'), '-s', '\n'.join(self.content)
]
completed = subprocess.run(args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8')
completed = subprocess.run(
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8')
if completed.returncode != 0:
raise ExtensionError(
"Failed config validation for type: '{0}' in: {1} line: {2}:\n {3}".format(
Expand Down
16 changes: 10 additions & 6 deletions examples/front-proxy/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

@app.route('/service/<service_number>')
def hello(service_number):
return ('Hello from behind Envoy (service {})! hostname: {} resolved'
'hostname: {}\n'.format(os.environ['SERVICE_NAME'], socket.gethostname(),
socket.gethostbyname(socket.gethostname())))
return (
'Hello from behind Envoy (service {})! hostname: {} resolved'
'hostname: {}\n'.format(
os.environ['SERVICE_NAME'], socket.gethostname(),
socket.gethostbyname(socket.gethostname())))


@app.route('/trace/<service_number>')
Expand All @@ -42,9 +44,11 @@ def trace(service_number):
if header in request.headers:
headers[header] = request.headers[header]
requests.get("http://localhost:9000/trace/2", headers=headers)
return ('Hello from behind Envoy (service {})! hostname: {} resolved'
'hostname: {}\n'.format(os.environ['SERVICE_NAME'], socket.gethostname(),
socket.gethostbyname(socket.gethostname())))
return (
'Hello from behind Envoy (service {})! hostname: {} resolved'
'hostname: {}\n'.format(
os.environ['SERVICE_NAME'], socket.gethostname(),
socket.gethostbyname(socket.gethostname())))


if __name__ == "__main__":
Expand Down
51 changes: 25 additions & 26 deletions source/extensions/filters/network/kafka/protocol/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def generate_main_code(type, main_header_file, resolver_cc_file, metrics_header_
fd.write(contents)


def generate_test_code(type, header_test_cc_file, codec_test_cc_file, utilities_cc_file,
input_files):
def generate_test_code(
type, header_test_cc_file, codec_test_cc_file, utilities_cc_file, input_files):
"""
Test code generator.
Expand Down Expand Up @@ -121,10 +121,8 @@ def parse_messages(self, input_files):
with open(input_file, 'r') as fd:
raw_contents = fd.read()
without_comments = re.sub(r'\s*//.*\n', '\n', raw_contents)
without_empty_newlines = re.sub(r'^\s*$',
'',
without_comments,
flags=re.MULTILINE)
without_empty_newlines = re.sub(
r'^\s*$', '', without_comments, flags=re.MULTILINE)
message_spec = json.loads(without_empty_newlines)
message = self.parse_top_level_element(message_spec)
messages.append(message)
Expand Down Expand Up @@ -167,13 +165,13 @@ def parse_top_level_element(self, spec):
common_struct_name = common_struct['name']
common_struct_versions = Statics.parse_version_string(
common_struct['versions'], versions[-1])
parsed_complex = self.parse_complex_type(common_struct_name, common_struct,
common_struct_versions)
parsed_complex = self.parse_complex_type(
common_struct_name, common_struct, common_struct_versions)
self.common_structs[parsed_complex.name] = parsed_complex

# Parse the type itself.
complex_type = self.parse_complex_type(self.currently_processed_message_type, spec,
versions)
complex_type = self.parse_complex_type(
self.currently_processed_message_type, spec, versions)
complex_type.register_flexible_versions(flexible_versions)

# Request / response types need to carry api key version.
Expand Down Expand Up @@ -223,8 +221,8 @@ def parse_field(self, field_spec, highest_possible_version):
if field_spec.get('tag') is not None:
return None

version_usage = Statics.parse_version_string(field_spec['versions'],
highest_possible_version)
version_usage = Statics.parse_version_string(
field_spec['versions'], highest_possible_version)
version_usage_as_nullable = Statics.parse_version_string(
field_spec['nullableVersions'],
highest_possible_version) if 'nullableVersions' in field_spec else range(-1)
Expand All @@ -244,8 +242,8 @@ def parse_type(self, type_name, field_spec, highest_possible_version):
if (type_name in Primitive.USABLE_PRIMITIVE_TYPE_NAMES):
return Primitive(type_name, field_spec.get('default'))
else:
versions = Statics.parse_version_string(field_spec['versions'],
highest_possible_version)
versions = Statics.parse_version_string(
field_spec['versions'], highest_possible_version)
return self.parse_complex_type(type_name, field_spec, versions)


Expand Down Expand Up @@ -382,8 +380,8 @@ def default_value(self):

def example_value_for_test(self, version):
if self.is_nullable():
return 'absl::make_optional<%s>(%s)' % (self.type.name,
self.type.example_value_for_test(version))
return 'absl::make_optional<%s>(%s)' % (
self.type.name, self.type.example_value_for_test(version))
else:
return str(self.type.example_value_for_test(version))

Expand Down Expand Up @@ -460,8 +458,8 @@ def has_flexible_handling(self):
return True

def example_value_for_test(self, version):
return 'std::vector<%s>{ %s }' % (self.underlying.name,
self.underlying.example_value_for_test(version))
return 'std::vector<%s>{ %s }' % (
self.underlying.name, self.underlying.example_value_for_test(version))

def is_printable(self):
return self.underlying.is_printable()
Expand Down Expand Up @@ -551,8 +549,8 @@ def compute_declaration_chain(self):

def deserializer_name_in_version(self, version, compact):
if compact and self.original_name in Primitive.KAFKA_TYPE_TO_COMPACT_DESERIALIZER.keys():
return Primitive.compute(self.original_name,
Primitive.KAFKA_TYPE_TO_COMPACT_DESERIALIZER)
return Primitive.compute(
self.original_name, Primitive.KAFKA_TYPE_TO_COMPACT_DESERIALIZER)
else:
return Primitive.compute(self.original_name, Primitive.KAFKA_TYPE_TO_DESERIALIZER)

Expand Down Expand Up @@ -600,8 +598,8 @@ def register_flexible_versions(self, flexible_versions):
for type in self.compute_declaration_chain():
type.flexible_versions = flexible_versions
if len(flexible_versions) > 0:
tagged_fields_field = FieldSpec('tagged_fields', Primitive('tagged_fields', None),
flexible_versions, [])
tagged_fields_field = FieldSpec(
'tagged_fields', Primitive('tagged_fields', None), flexible_versions, [])
type.fields.append(tagged_fields_field)

def compute_declaration_chain(self):
Expand Down Expand Up @@ -670,8 +668,8 @@ def compute_serialization_specs(self):
FieldSerializationSpec(field, non_flexible, 'computeSize', 'encode'))
if flexible:
result.append(
FieldSerializationSpec(field, flexible, 'computeCompactSize',
'encodeCompact'))
FieldSerializationSpec(
field, flexible, 'computeCompactSize', 'encodeCompact'))
else:
result.append(
FieldSerializationSpec(field, field.version_usage, 'computeSize', 'encode'))
Expand Down Expand Up @@ -712,6 +710,7 @@ def get_template(template):
import sys
# Templates are resolved relatively to main start script, due to main & test templates being
# stored in different directories.
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
searchpath=os.path.dirname(os.path.abspath(sys.argv[0]))))
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(
searchpath=os.path.dirname(os.path.abspath(sys.argv[0]))))
return env.get_template(template)
4 changes: 2 additions & 2 deletions source/extensions/filters/network/kafka/protocol/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def main():
resolver_cc_file = os.path.abspath(sys.argv[3])
metrics_h_file = os.path.abspath(sys.argv[4])
input_files = sys.argv[5:]
generator.generate_main_code(type, main_header_file, resolver_cc_file, metrics_h_file,
input_files)
generator.generate_main_code(
type, main_header_file, resolver_cc_file, metrics_h_file, input_files)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def get_template(template):
import sys
# Templates are resolved relatively to main start script, due to main & test templates being
# stored in different directories.
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
searchpath=os.path.dirname(os.path.abspath(sys.argv[0]))))
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(
searchpath=os.path.dirname(os.path.abspath(sys.argv[0]))))
return env.get_template(template)
Loading

0 comments on commit 83d0d99

Please sign in to comment.