Skip to content

Commit

Permalink
[whitespace] tools/xpcc*
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Oct 14, 2018
1 parent 36ec189 commit 40ab0bf
Show file tree
Hide file tree
Showing 29 changed files with 543 additions and 543 deletions.
66 changes: 33 additions & 33 deletions tools/xpcc_generator/builder/builder_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class BuilderException(Exception):
class Builder(object):
"""
Base class for any builder-class
Attributes:
self.xmlpath -- path to the xml-file
self.xmlfile -- xml-filename
self.tree -- evaluated xml tree, instance of Parser
self.time -- string containing the current date and time
self.options -- evaluated commandline arguments (see OptionParser)
self.include_paths -- paths that will be searched for include files
To access the elements from the xml tree use for example:
self.tree.components
self.tree.components.actions -- all actions
Expand All @@ -58,47 +58,47 @@ class Builder(object):
container 'PC'
self.tree.types
self.tree.events
You have to specify a VERSION attribute.
"""
def __init__(self):
optparser = OptionParser(
usage = "%prog [options] XML_FILE",
version = "%prog version: " + self.VERSION )

optparser.add_option(
"-o", "--outpath",
dest = "outpath",
default = None,
help = "Output path [optional]")

optparser.add_option(
"-t", "--template_path",
dest = "templatePath",
default = None,
help = "Template path [optional]")

optparser.add_option(
"-d", "--dtdpath",
dest = "dtdPath",
default = None,
help = "DTD path [optional]")

optparser.add_option(
"-I", "--include_path",
dest = "includePath",
default = None,
help = "Include Search Path [optional]")

self.setup(optparser)

(self.options, args) = optparser.parse_args()

if len(args) != 1:
raise BuilderException("XML_FILE: You have to specify a valid xml-file")
exit(1)

self.xmlfile = args[0]
self.xmlpath = os.path.dirname(os.path.abspath(self.xmlfile))

Expand All @@ -118,89 +118,89 @@ def __init__(self):
sys.stderr.write("Parsing Error: %s\n" % str(e))
print(traceback.format_exc())
sys.exit(1)

self.tree = parser.tree
self.globals = {
'time': time.strftime("%d %b %Y, %H:%M:%S", time.localtime()),
}

if self.options.templatePath == None:
self.templatePath = "templates/"
else:
self.templatePath = self.options.templatePath

def generate(self):
""" Generate the requested files """
raise BuilderException("You can not use this class directly!")

def setup(self, optparser):
"""
Setup the builder. Can be overwritten to customise the behavior.
Keywords arguments:
optparser -- OptionParser instance. Use add_option for
additional commandline arguments.
"""
pass

def write(self, filename, data):
"""
Write data utf-8 decoded to a file.
Contents of the file will be overwritten.
"""
# create the path if it doesn't exists
dir = os.path.dirname(filename)
if not os.path.isdir(dir):
os.mkdir(dir)

# write data
file = codecs.open(filename, 'w', 'utf8')
file.write(data)
file.close()

def read(self, filename):
"""
Read a utf-8 decoded file.
Provided for convenience.
"""
return codecs.open(filename, 'r', 'utf8').read()

def template(self, filename, filter=None):
""" Open a template file
Uses the jinja2 template engine. The following additional filters
are included:
modm.wordwrap(with) -- like the original filter, but with correct
handling of newlines
modm.indent(level) -- indent every line with \a level tabs
Keyword arguments:
filename -- Template file
filter -- dict with additional filters (see the section
filter -- dict with additional filters (see the section
'Custom Filters' in the Jinja2 documentation for details)
Example:
template = builder.template(file)
output = template.render(dict)
builder.write(outputfile, output)
"""
def filter_wordwrap(value, width=79):
return '\n\n'.join([textwrap.fill(str, width) for str in value.split('\n\n')])

def filter_indent(value, level=0, prefix=""):
return ('\n' + '\t' * level + prefix).join(value.split('\n'))

path = os.path.dirname(filename)
name = os.path.basename(filename)

if not os.path.isabs(filename):
relpath = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(relpath, path)

environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(path),
extensions=["jinja2.ext.loopcontrols"])
Expand All @@ -210,9 +210,9 @@ def filter_indent(value, level=0, prefix=""):
if filter:
environment.filters.update(filter)
template = environment.get_template(name, globals=self.globals)

return template

def run(self):
try:
self.generate()
Expand Down
14 changes: 7 additions & 7 deletions tools/xpcc_generator/builder/cpp_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class CppCommunicationBuilder(builder_base.Builder):
Generate the whole communication tree. The Output is a class named
Communication, which contains all concrete Components. They all contain
methods, which are corresponding to Actions of given Components.
A common call would be like:
$python3 java_communication.py --outpath source/rca/robot --package rca.robot robot.xml;
"""


VERSION = "0.1"

def setup(self, optparser):
optparser.add_option(
"--namespace",
Expand All @@ -63,17 +63,17 @@ def generate(self):
}
template = self.template('templates/cpp_communication.tpl',
filter = cppFilter)

# Bool has a special status because its primitive but user generated
# and the only not numerical type
components = self.tree.components

substitutions = {
'components': components,
'events': self.tree.events,
'namespace': namespace
}

file = os.path.join(self.options.outpath, 'communication.hpp')
self.write(file, template.render(substitutions))

Expand Down
10 changes: 5 additions & 5 deletions tools/xpcc_generator/builder/cpp_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import filter.cpp as filter

class IdentifierBuilder(builder_base.Builder):

VERSION = "0.1"

def setup(self, optparser):
Expand All @@ -46,13 +46,13 @@ def generate(self):
'enumElementStrong': filter.typeName,
'enumValue': filter.toHexValue,
}

template = self.template('templates/robot_identifier.tpl', filter=cppFilter)

components = []
for component in self.tree.components.iter(abstract=False):
components.append(component.flattened())

substitutions = {
'domains' : self.tree.domains,
'containers' : self.tree.containers,
Expand All @@ -61,7 +61,7 @@ def generate(self):
'events': self.tree.events,
'namespace': namespace
}

if os.path.splitext(self.options.outpath)[1] == '':
file = os.path.join(self.options.outpath, 'identifier.hpp')
else:
Expand Down
16 changes: 8 additions & 8 deletions tools/xpcc_generator/builder/cpp_packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def filter_constexpr_constructor(class_, default=True):

# -----------------------------------------------------------------------------
class TypeBuilder(builder_base.Builder):

VERSION = "0.1"

def setup(self, optparser):
optparser.add_option(
"--namespace",
Expand All @@ -121,7 +121,7 @@ def setup(self, optparser):
dest = "system_include_path",
default = None,
help = "Include directive for the source file")

def generate(self):
# check the commandline options
if self.options.outpath:
Expand All @@ -132,7 +132,7 @@ def generate(self):
header_path = self.options.header_path
else:
raise builder_base.BuilderException("You need to provide an output path!")

if self.options.system_include_path:
includeDirective = '<%s>' % os.path.join(self.options.system_include_path, 'packets.hpp')
elif self.options.quote_include_path:
Expand All @@ -156,10 +156,10 @@ def generate(self):
'generateInitializationList': filter_initialization_list,
'isConstexprConstructor': filter_constexpr_constructor
}

template_header = self.template('templates/robot_packets.hpp.tpl', filter=cppFilter)
template_source = self.template('templates/robot_packets.cpp.tpl', filter=cppFilter)

substitutions = {
'components': self.tree.components,
'actions': self.tree.components.actions,
Expand All @@ -168,10 +168,10 @@ def generate(self):
'includeDirective': includeDirective,
'namespace': namespace
}

file = os.path.join(header_path, 'packets.hpp')
self.write(file, template_header.render(substitutions) + "\n")

file = os.path.join(source_path, 'packets.cpp')
self.write(file, template_source.render(substitutions) + "\n")

Expand Down
20 changes: 10 additions & 10 deletions tools/xpcc_generator/builder/java_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ class JavaCommunicationBuilder(builder_base.Builder):
Generate the whole communication tree. The Output is a class named
Communication, which contains all concrete Components. They all contain
methods, which are corresponding to Actions of given Components.
A common call would be like:
$python3 java_communication.py --outpath source/rca/robot --package rca.robot robot.xml;
"""


VERSION = "0.1"

def setup(self, optparser):
optparser.add_option(
"--package",
dest = "package",
default = '',
help = "name of package")

def generate(self):
# check the commandline options
if not self.options.outpath:
raise builder_base.BuilderException("You need to provide an output path!")

javaFilter = {
'enumElement': filter.enumElement,
'typeName': filter.typeName,
Expand All @@ -53,19 +53,19 @@ def generate(self):
}
template = self.template('templates/java_communication.tpl',
filter = javaFilter)

# Bool has a special status because its primitive but user generated
# and the only not numerical type
components = self.tree.components

primitives = sorted(filter.PRIMITIVES.values())

substitutions = {
'package' : self.options.package,
'components': components,
'events': self.tree.events,
}

file = os.path.join(self.options.outpath, 'Communication.java')
self.write(file, template.render(substitutions))

Expand Down
Loading

0 comments on commit 40ab0bf

Please sign in to comment.