2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

generate_auto_job: break out build_schema for refactor

This commit is contained in:
Jay Berkenbilt 2022-01-30 14:54:18 -05:00
parent 1355d95d08
commit d9e00e8e5b
2 changed files with 84 additions and 82 deletions

View File

@ -484,6 +484,86 @@ class Main:
self.json_init.append(
f'doSetup("{key}", bindSetup(&Handlers::{method}));')
def option_to_json_key(self, s):
return self.to_identifier(s, '', False)
def build_schema(self, j, s, flag, path, expected, options_seen):
if flag:
identifier = self.to_identifier(path, '', False)
self.json_decls.append(f'void begin{identifier}(JSON);')
self.json_decls.append(f'void end{identifier}();')
self.json_init.append(
f'beginDict("{flag}",'
f' bindJSON(&Handlers::begin{identifier}),'
f' bindBare(&Handlers::end{identifier})); // {path}')
for k, v in j.items():
is_trivial = False
if k in expected:
is_trivial = True
common_config = None
for t in expected[k]['tables']:
tdata = self.by_table[t]
if k in tdata['manual']:
is_trivial = False
if common_config is None:
common_config = tdata['config']
elif common_config != tdata['config']:
is_trivial = False
elif not (k.startswith('_') or isinstance(v, str)):
raise Exception(f'json: unknown key {k}')
if k.startswith('_'):
schema_key = k[1:]
else:
schema_key = re.sub(r'[^\.]+\.', '', k)
schema_key = self.option_to_json_key(schema_key)
schema_value = v
is_dict = False
if k in expected:
options_seen.add(re.sub('^_', '', k))
if v is None:
schema_value = re.sub(
r'--(\S+)',
lambda x: self.option_to_json_key(x.group(1)),
expected[k]['help'])
if (isinstance(v, dict)):
is_dict = True
schema_value = {}
self.build_schema(v, schema_value,
schema_key, f'{path}.{schema_key}',
expected, options_seen)
elif (isinstance(v, list)):
if len(v) != 1:
raise Exception('json contains array with length != 1')
if isinstance(v[0], dict):
is_dict = True
schema_value = [{}]
subpath = f'{path}.{schema_key}'
identifier = self.to_identifier(subpath, '', False)
self.json_decls.append(
f'void begin{identifier}Array(JSON);')
self.json_decls.append(
f'void end{identifier}Array();')
self.json_init.append(
f'beginArray("{flag}",'
f' bindJSON(&Handlers::begin{identifier}Array),'
f' bindBare(&Handlers::end{identifier}Array));'
f' // {subpath}[]')
self.build_schema(v[0], schema_value[0],
schema_key, subpath,
expected, options_seen)
self.json_init.append(
f'endContainer(); // {subpath}[]')
elif schema_value is None:
raise Exception(f'unknown schema value for {k}')
s[schema_key] = schema_value
if not is_dict:
if is_trivial:
self.handle_json_trivial(schema_key, expected[k])
else:
self.handle_json_manual(schema_key, path)
if flag:
self.json_init.append(f'endContainer(); // {path}')
def generate_schema(self, data):
# Check to make sure that every command-line option is
# represented either in data['json'] or data['no-json'].
@ -504,91 +584,13 @@ class Main:
expected[f'{t}.{k}'] = {**v}
options_seen = set(data['no-json'])
self.schema = {}
def option_to_json_key(s):
return self.to_identifier(s, '', False)
# Walk through the json information building the schema as we
# go. This verifies consistency between command-line options
# and the json section of the data and builds up a schema by
# populating with help information as available.
def build_schema(j, s, flag, path):
if flag:
identifier = self.to_identifier(path, '', False)
self.json_decls.append(f'void begin{identifier}(JSON);')
self.json_decls.append(f'void end{identifier}();')
self.json_init.append(
f'beginDict("{flag}",'
f' bindJSON(&Handlers::begin{identifier}),'
f' bindBare(&Handlers::end{identifier})); // {path}')
for k, v in j.items():
is_trivial = False
if k in expected:
is_trivial = True
common_config = None
for t in expected[k]['tables']:
tdata = self.by_table[t]
if k in tdata['manual']:
is_trivial = False
if common_config is None:
common_config = tdata['config']
elif common_config != tdata['config']:
is_trivial = False
elif not (k.startswith('_') or isinstance(v, str)):
raise Exception(f'json: unknown key {k}')
if k.startswith('_'):
schema_key = k[1:]
else:
schema_key = re.sub(r'[^\.]+\.', '', k)
schema_key = option_to_json_key(schema_key)
schema_value = v
is_dict = False
if k in expected:
options_seen.add(re.sub('^_', '', k))
if v is None:
schema_value = re.sub(
r'--(\S+)',
lambda x: option_to_json_key(x.group(1)),
expected[k]['help'])
if (isinstance(v, dict)):
is_dict = True
schema_value = {}
build_schema(v, schema_value,
schema_key, f'{path}.{schema_key}')
elif (isinstance(v, list)):
if len(v) != 1:
raise Exception('json contains array with length != 1')
if isinstance(v[0], dict):
is_dict = True
schema_value = [{}]
subpath = f'{path}.{schema_key}'
identifier = self.to_identifier(subpath, '', False)
self.json_decls.append(
f'void begin{identifier}Array(JSON);')
self.json_decls.append(
f'void end{identifier}Array();')
self.json_init.append(
f'beginArray("{flag}",'
f' bindJSON(&Handlers::begin{identifier}Array),'
f' bindBare(&Handlers::end{identifier}Array));'
f' // {subpath}[]')
build_schema(v[0], schema_value[0],
schema_key, subpath)
self.json_init.append(
f'endContainer(); // {subpath}[]')
elif schema_value is None:
raise Exception(f'unknown schema value for {k}')
s[schema_key] = schema_value
if not is_dict:
if is_trivial:
self.handle_json_trivial(schema_key, expected[k])
else:
self.handle_json_manual(schema_key, path)
if flag:
self.json_init.append(f'endContainer(); // {path}')
build_schema(data['json'], self.schema, '', '')
self.schema = {}
self.build_schema(data['json'], self.schema, '', '',
expected, options_seen)
if options_seen != set(expected.keys()):
raise Exception('missing from json: ' +
str(set(expected.keys()) - options_seen))

View File

@ -1,5 +1,5 @@
# Generated by generate_auto_job
generate_auto_job 68c39a9f6d1f10c1b9cf9af539036621589d6b0bbf3d05a2e1ddd1c9919d7383
generate_auto_job 8c88accfa988c8d6035c3e7e012bbde3da0c76fdfaba8e1460112bf344dc7b4f
include/qpdf/auto_job_c_att.hh 7ad43bb374c1370ef32ebdcdcb7b73a61d281f7f4e3f12755585872ab30fb60e
include/qpdf/auto_job_c_copy_att.hh 32275d03cdc69b703dd7e02ba0bbe15756e714e9ad185484773a6178dc09e1ee
include/qpdf/auto_job_c_enc.hh 72e138c7b96ed5aacdce78c1dec04b1c20d361faec4f8faf52f64c1d6be99265