[SEBA-412] Automated reformat of Python code
Passes of modernize, autopep8, black, then check with flake8
flake8 + manual fixes:
lib/xos-config
lib/xos-kafka
lib/xos-util
xos/coreapi
xos/api
xos/xos_client
Change-Id: Ib23cf84cb13beb3c6381fa0d79594dc9131dc815
diff --git a/lib/xos-genx/xosgenx/generator.py b/lib/xos-genx/xosgenx/generator.py
index 3355fb5..3e650be 100644
--- a/lib/xos-genx/xosgenx/generator.py
+++ b/lib/xos-genx/xosgenx/generator.py
@@ -1,4 +1,3 @@
-
# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +13,7 @@
# limitations under the License.
+from __future__ import print_function
import plyxproto.parser as plyxproto
import jinja2
import os
@@ -23,9 +23,10 @@
import yaml
from colorama import Fore
-loader = jinja2.PackageLoader(__name__, 'templates')
+loader = jinja2.PackageLoader(__name__, "templates")
env = jinja2.Environment(loader=loader)
+
class XOSProcessorArgs:
""" Helper class for use cases that want to call XOSProcessor directly, rather than executing xosgenx from the
command line.
@@ -40,9 +41,13 @@
default_dest_extension = None
default_target = None
default_checkers = None
- default_verbosity = 0 # Higher numbers = more verbosity, lower numbers = less verbosity
- default_include_models = [] # If neither include_models nor include_apps is specified, then all models will
- default_include_apps = [] # be included.
+ default_verbosity = (
+ 0
+ ) # Higher numbers = more verbosity, lower numbers = less verbosity
+ default_include_models = (
+ []
+ ) # If neither include_models nor include_apps is specified, then all models will
+ default_include_apps = [] # be included.
def __init__(self, **kwargs):
# set defaults
@@ -60,14 +65,14 @@
self.include_apps = XOSProcessorArgs.default_include_apps
# override defaults with kwargs
- for (k,v) in kwargs.items():
+ for (k, v) in kwargs.items():
setattr(self, k, v)
-class XOSProcessor:
+class XOSProcessor:
@staticmethod
def _read_input_from_files(files):
- input = ''
+ input = ""
for fname in files:
with open(fname) as infile:
input += infile.read()
@@ -75,7 +80,7 @@
@staticmethod
def _attach_parser(ast, args):
- if hasattr(args, 'rev') and args.rev:
+ if hasattr(args, "rev") and args.rev:
v = Proto2XProto()
ast.accept(v)
@@ -86,7 +91,9 @@
@staticmethod
def _get_template(target):
if not os.path.isabs(target):
- return os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/targets/' + target)
+ return os.path.abspath(
+ os.path.dirname(os.path.realpath(__file__)) + "/targets/" + target
+ )
return target
@staticmethod
@@ -94,10 +101,10 @@
# NOTE this method can be used in the jinja template
def file_exists2(name):
if attic is not None:
- path = attic + '/' + name
+ path = attic + "/" + name
else:
path = name
- return (os.path.exists(path))
+ return os.path.exists(path)
return file_exists2
@@ -106,60 +113,65 @@
# NOTE this method can be used in the jinja template
def include_file2(name):
if attic is not None:
- path = attic + '/' + name
+ path = attic + "/" + name
else:
path = name
return open(path).read()
+
return include_file2
@staticmethod
def _load_jinja2_extensions(os_template_env, attic):
- os_template_env.globals['include_file'] = XOSProcessor._include_file(attic) # Generates a function
- os_template_env.globals['file_exists'] = XOSProcessor._file_exists(attic) # Generates a function
+ os_template_env.globals["include_file"] = XOSProcessor._include_file(
+ attic
+ ) # Generates a function
+ os_template_env.globals["file_exists"] = XOSProcessor._file_exists(
+ attic
+ ) # Generates a function
- os_template_env.filters['yaml'] = yaml.dump
+ os_template_env.filters["yaml"] = yaml.dump
for f in dir(jinja2_extensions):
- if f.startswith('xproto'):
+ if f.startswith("xproto"):
os_template_env.globals[f] = getattr(jinja2_extensions, f)
return os_template_env
@staticmethod
def _add_context(args):
- if not hasattr(args, 'kv') or not args.kv:
+ if not hasattr(args, "kv") or not args.kv:
return
try:
context = {}
- for s in args.kv.split(','):
- k, val = s.split(':')
+ for s in args.kv.split(","):
+ k, val = s.split(":")
context[k] = val
return context
- except Exception, e:
- print e.message
+ except Exception as e:
+ print(e.message)
@staticmethod
def _write_single_file(rendered, dir, dest_file, quiet):
file_name = "%s/%s" % (dir, dest_file)
- file = open(file_name, 'w')
+ file = open(file_name, "w")
file.write(rendered)
file.close()
- if quiet == False:
- print "Saved: %s" % file_name
+ if not quiet:
+ print("Saved: %s" % file_name)
@staticmethod
def _write_file_per_model(rendered, dir, suffix, quiet):
for m in rendered:
file_name = "%s/%s%s" % (dir, m.lower(), suffix)
if not rendered[m]:
- if quiet == False:
- print "Not saving %s as it is empty" % file_name
+ if not quiet:
+ print("Not saving %s as it is empty" % file_name)
else:
- file = open(file_name, 'w')
+ file = open(file_name, "w")
file.write(rendered[m])
file.close()
- if quiet == False:
- print "Saved: %s" % file_name
+ if not quiet:
+ print("Saved: %s" % file_name)
@staticmethod
def _write_split_target(rendered, dir, quiet):
@@ -167,21 +179,21 @@
lines = rendered.splitlines()
current_buffer = []
for l in lines:
- if (l.startswith('+++')):
+ if l.startswith("+++"):
if dir:
- path = dir + '/' + l[4:].lower()
+ path = dir + "/" + l[4:].lower()
- fil = open(path, 'w')
- buf = '\n'.join(current_buffer)
+ fil = open(path, "w")
+ buf = "\n".join(current_buffer)
obuf = buf
fil.write(obuf)
fil.close()
- if quiet == False:
- print "Save file to: %s" % path
+ if not quiet:
+ print("Save file to: %s" % path)
current_buffer = []
else:
@@ -189,50 +201,55 @@
@staticmethod
def _find_message_by_model_name(messages, model):
- return next((x for x in messages if x['name'] == model), None)
+ return next((x for x in messages if x["name"] == model), None)
@staticmethod
def _find_last_nonempty_line(text, pointer):
ne_pointer = pointer
found = False
- while ne_pointer!=0 and not found:
- ne_pointer = text[:(ne_pointer-1)].rfind('\n')
- if ne_pointer<0: ne_pointer = 0
- if text[ne_pointer-1]!='\n':
+ while ne_pointer != 0 and not found:
+ ne_pointer = text[: (ne_pointer - 1)].rfind("\n")
+ if ne_pointer < 0:
+ ne_pointer = 0
+ if text[ne_pointer - 1] != "\n":
found = True
return ne_pointer
@staticmethod
- def process(args, operator = None):
+ def process(args, operator=None):
# Setting defaults
- if not hasattr(args, 'attic'):
+ if not hasattr(args, "attic"):
args.attic = None
- if not hasattr(args, 'write_to_file'):
+ if not hasattr(args, "write_to_file"):
args.write_to_file = None
- if not hasattr(args, 'dest_file'):
+ if not hasattr(args, "dest_file"):
args.dest_file = None
- if not hasattr(args, 'dest_extension'):
+ if not hasattr(args, "dest_extension"):
args.dest_extension = None
- if not hasattr(args, 'output'):
+ if not hasattr(args, "output"):
args.output = None
- if not hasattr(args, 'quiet'):
+ if not hasattr(args, "quiet"):
args.quiet = True
# Validating
- if args.write_to_file == 'single' and args.dest_file is None:
- raise Exception("[XosGenX] write_to_file option is specified as 'single' but no dest_file is provided")
- if args.write_to_file == 'model' and (args.dest_extension is None):
- raise Exception("[XosGenX] write_to_file option is specified as 'model' but no dest_extension is provided")
+ if args.write_to_file == "single" and args.dest_file is None:
+ raise Exception(
+ "[XosGenX] write_to_file option is specified as 'single' but no dest_file is provided"
+ )
+ if args.write_to_file == "model" and (args.dest_extension is None):
+ raise Exception(
+ "[XosGenX] write_to_file option is specified as 'model' but no dest_extension is provided"
+ )
if args.output is not None and not os.path.isabs(args.output):
raise Exception("[XosGenX] The output dir must be an absolute path!")
if args.output is not None and not os.path.isdir(args.output):
raise Exception("[XosGenX] The output dir must be a directory!")
- if hasattr(args, 'files'):
+ if hasattr(args, "files"):
inputs = XOSProcessor._read_input_from_files(args.files)
- elif hasattr(args, 'inputs'):
+ elif hasattr(args, "inputs"):
inputs = args.inputs
else:
raise Exception("[XosGenX] No inputs provided!")
@@ -243,28 +260,29 @@
else:
template_path = operator
-
[template_folder, template_name] = os.path.split(template_path)
os_template_loader = jinja2.FileSystemLoader(searchpath=[template_folder])
os_template_env = jinja2.Environment(loader=os_template_loader)
- os_template_env = XOSProcessor._load_jinja2_extensions(os_template_env, args.attic)
+ os_template_env = XOSProcessor._load_jinja2_extensions(
+ os_template_env, args.attic
+ )
template = os_template_env.get_template(template_name)
context = XOSProcessor._add_context(args)
parser = plyxproto.ProtobufAnalyzer()
try:
ast = parser.parse_string(inputs, debug=0)
- except plyxproto.ParsingError, e:
+ except plyxproto.ParsingError as e:
line, start, end = e.error_range
ptr = XOSProcessor._find_last_nonempty_line(inputs, start)
if start == 0:
- beginning = ''
+ beginning = ""
else:
- beginning = inputs[ptr:start-1]
+ beginning = inputs[ptr: start - 1]
- line_end_char = inputs[start+end:].find('\n')
+ line_end_char = inputs[start + end:].find("\n")
line_end = inputs[line_end_char]
if e.message:
@@ -272,11 +290,16 @@
else:
error = "xproto parsing error"
- print error + "\n" + Fore.YELLOW + "Line %d:"%line + Fore.WHITE
- print beginning + Fore.YELLOW + inputs[start-1:start+end] + Fore.WHITE + line_end
+ print(error + "\n" + Fore.YELLOW + "Line %d:" % line + Fore.WHITE)
+ print(
+ beginning
+ + Fore.YELLOW
+ + inputs[start - 1: start + end]
+ + Fore.WHITE
+ + line_end
+ )
exit(1)
-
v = XOSProcessor._attach_parser(ast, args)
if args.include_models or args.include_apps:
@@ -300,38 +323,42 @@
messages = [XOSProcessor._find_message_by_model_name(v.messages, model)]
rendered[model] = template.render(
- {"proto":
- {
- 'message_table': models,
- 'messages': messages,
- 'policies': v.policies,
- 'message_names': [m['name'] for m in v.messages]
+ {
+ "proto": {
+ "message_table": models,
+ "messages": messages,
+ "policies": v.policies,
+ "message_names": [m["name"] for m in v.messages],
},
"context": context,
- "options": v.options
+ "options": v.options,
}
)
- if (str(v.options.get("legacy", "false")).strip('"').lower() == "true"):
+ if str(v.options.get("legacy", "false")).strip('"').lower() == "true":
suffix = "_decl." + args.dest_extension
else:
suffix = "." + args.dest_extension
- XOSProcessor._write_file_per_model(rendered, args.output, suffix, args.quiet)
+ XOSProcessor._write_file_per_model(
+ rendered, args.output, suffix, args.quiet
+ )
else:
rendered = template.render(
- {"proto":
- {
- 'message_table': v.models,
- 'messages': v.messages,
- 'policies': v.policies,
- 'message_names': [m['name'] for m in v.messages]
+ {
+ "proto": {
+ "message_table": v.models,
+ "messages": v.messages,
+ "policies": v.policies,
+ "message_names": [m["name"] for m in v.messages],
},
"context": context,
- "options": v.options
+ "options": v.options,
}
)
if args.output is not None and args.write_to_file == "target":
XOSProcessor._write_split_target(rendered, args.output, args.quiet)
elif args.output is not None and args.write_to_file == "single":
- XOSProcessor._write_single_file(rendered, args.output, args.dest_file, args.quiet)
+ XOSProcessor._write_single_file(
+ rendered, args.output, args.dest_file, args.quiet
+ )
return rendered