1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-01 18:29:16 +01:00

Merge branch 'parched/OP-1806_version-info_escape_strings' into next

This commit is contained in:
James Duley 2015-05-06 17:11:17 +12:00
commit 9923803e59

View File

@ -235,7 +235,16 @@ class Repo:
with open(json_path, 'w') as json_file: with open(json_path, 'w') as json_file:
json.dump(json_data, json_file) json.dump(json_data, json_file)
def file_from_template(tpl_name, out_name, dict): def escape_dict(dictionary):
"""Escapes dictionary values for C"""
# We need to escape the strings for C
for key in dictionary:
# Using json.dumps and removing the surounding quotes escapes for C
dictionary[key] = json.dumps(dictionary[key])[1:-1]
def file_from_template(tpl_name, out_name, dictionary):
"""Create or update file from template using dictionary """Create or update file from template using dictionary
This function reads the template, performs placeholder replacement This function reads the template, performs placeholder replacement
@ -269,7 +278,7 @@ def file_from_template(tpl_name, out_name, dict):
tf.close() tf.close()
# Replace placeholders using dictionary # Replace placeholders using dictionary
out = Template(tpl).substitute(dict) out = Template(tpl).substitute(dictionary)
# Check if output file already exists # Check if output file already exists
try: try:
@ -422,6 +431,10 @@ string given.
help='name of template file'); help='name of template file');
parser.add_option('--outfile', parser.add_option('--outfile',
help='name of output file'); help='name of output file');
parser.add_option('--escape', action="store_true",
help='do escape strings for C (default based on file ext)');
parser.add_option('--no-escape', action="store_false", dest="escape",
help='do not escape strings for C');
parser.add_option('--image', parser.add_option('--image',
help='name of image file for sha1 calculation'); help='name of image file for sha1 calculation');
parser.add_option('--type', default="", parser.add_option('--type', default="",
@ -477,6 +490,12 @@ string given.
if args.info: if args.info:
r.info() r.info()
files_to_escape = ['.c', '.cpp']
if (args.escape == None and args.outfile != None and
os.path.splitext(args.outfile)[1] in files_to_escape) or args.escape:
escape_dict(dictionary)
if args.format != None: if args.format != None:
print Template(args.format).substitute(dictionary) print Template(args.format).substitute(dictionary)