Commit d9e00e8e5bcad720c73d45c790857907898d3591

Authored by Jay Berkenbilt
1 parent 1355d95d

generate_auto_job: break out build_schema for refactor

Showing 2 changed files with 84 additions and 82 deletions
generate_auto_job
@@ -484,6 +484,86 @@ class Main: @@ -484,6 +484,86 @@ class Main:
484 self.json_init.append( 484 self.json_init.append(
485 f'doSetup("{key}", bindSetup(&Handlers::{method}));') 485 f'doSetup("{key}", bindSetup(&Handlers::{method}));')
486 486
  487 + def option_to_json_key(self, s):
  488 + return self.to_identifier(s, '', False)
  489 +
  490 + def build_schema(self, j, s, flag, path, expected, options_seen):
  491 + if flag:
  492 + identifier = self.to_identifier(path, '', False)
  493 + self.json_decls.append(f'void begin{identifier}(JSON);')
  494 + self.json_decls.append(f'void end{identifier}();')
  495 + self.json_init.append(
  496 + f'beginDict("{flag}",'
  497 + f' bindJSON(&Handlers::begin{identifier}),'
  498 + f' bindBare(&Handlers::end{identifier})); // {path}')
  499 + for k, v in j.items():
  500 + is_trivial = False
  501 + if k in expected:
  502 + is_trivial = True
  503 + common_config = None
  504 + for t in expected[k]['tables']:
  505 + tdata = self.by_table[t]
  506 + if k in tdata['manual']:
  507 + is_trivial = False
  508 + if common_config is None:
  509 + common_config = tdata['config']
  510 + elif common_config != tdata['config']:
  511 + is_trivial = False
  512 + elif not (k.startswith('_') or isinstance(v, str)):
  513 + raise Exception(f'json: unknown key {k}')
  514 + if k.startswith('_'):
  515 + schema_key = k[1:]
  516 + else:
  517 + schema_key = re.sub(r'[^\.]+\.', '', k)
  518 + schema_key = self.option_to_json_key(schema_key)
  519 + schema_value = v
  520 + is_dict = False
  521 + if k in expected:
  522 + options_seen.add(re.sub('^_', '', k))
  523 + if v is None:
  524 + schema_value = re.sub(
  525 + r'--(\S+)',
  526 + lambda x: self.option_to_json_key(x.group(1)),
  527 + expected[k]['help'])
  528 + if (isinstance(v, dict)):
  529 + is_dict = True
  530 + schema_value = {}
  531 + self.build_schema(v, schema_value,
  532 + schema_key, f'{path}.{schema_key}',
  533 + expected, options_seen)
  534 + elif (isinstance(v, list)):
  535 + if len(v) != 1:
  536 + raise Exception('json contains array with length != 1')
  537 + if isinstance(v[0], dict):
  538 + is_dict = True
  539 + schema_value = [{}]
  540 + subpath = f'{path}.{schema_key}'
  541 + identifier = self.to_identifier(subpath, '', False)
  542 + self.json_decls.append(
  543 + f'void begin{identifier}Array(JSON);')
  544 + self.json_decls.append(
  545 + f'void end{identifier}Array();')
  546 + self.json_init.append(
  547 + f'beginArray("{flag}",'
  548 + f' bindJSON(&Handlers::begin{identifier}Array),'
  549 + f' bindBare(&Handlers::end{identifier}Array));'
  550 + f' // {subpath}[]')
  551 + self.build_schema(v[0], schema_value[0],
  552 + schema_key, subpath,
  553 + expected, options_seen)
  554 + self.json_init.append(
  555 + f'endContainer(); // {subpath}[]')
  556 + elif schema_value is None:
  557 + raise Exception(f'unknown schema value for {k}')
  558 + s[schema_key] = schema_value
  559 + if not is_dict:
  560 + if is_trivial:
  561 + self.handle_json_trivial(schema_key, expected[k])
  562 + else:
  563 + self.handle_json_manual(schema_key, path)
  564 + if flag:
  565 + self.json_init.append(f'endContainer(); // {path}')
  566 +
487 def generate_schema(self, data): 567 def generate_schema(self, data):
488 # Check to make sure that every command-line option is 568 # Check to make sure that every command-line option is
489 # represented either in data['json'] or data['no-json']. 569 # represented either in data['json'] or data['no-json'].
@@ -504,91 +584,13 @@ class Main: @@ -504,91 +584,13 @@ class Main:
504 expected[f'{t}.{k}'] = {**v} 584 expected[f'{t}.{k}'] = {**v}
505 options_seen = set(data['no-json']) 585 options_seen = set(data['no-json'])
506 586
507 - self.schema = {}  
508 -  
509 - def option_to_json_key(s):  
510 - return self.to_identifier(s, '', False)  
511 -  
512 # Walk through the json information building the schema as we 587 # Walk through the json information building the schema as we
513 # go. This verifies consistency between command-line options 588 # go. This verifies consistency between command-line options
514 # and the json section of the data and builds up a schema by 589 # and the json section of the data and builds up a schema by
515 # populating with help information as available. 590 # populating with help information as available.
516 - def build_schema(j, s, flag, path):  
517 - if flag:  
518 - identifier = self.to_identifier(path, '', False)  
519 - self.json_decls.append(f'void begin{identifier}(JSON);')  
520 - self.json_decls.append(f'void end{identifier}();')  
521 - self.json_init.append(  
522 - f'beginDict("{flag}",'  
523 - f' bindJSON(&Handlers::begin{identifier}),'  
524 - f' bindBare(&Handlers::end{identifier})); // {path}')  
525 - for k, v in j.items():  
526 - is_trivial = False  
527 - if k in expected:  
528 - is_trivial = True  
529 - common_config = None  
530 - for t in expected[k]['tables']:  
531 - tdata = self.by_table[t]  
532 - if k in tdata['manual']:  
533 - is_trivial = False  
534 - if common_config is None:  
535 - common_config = tdata['config']  
536 - elif common_config != tdata['config']:  
537 - is_trivial = False  
538 - elif not (k.startswith('_') or isinstance(v, str)):  
539 - raise Exception(f'json: unknown key {k}')  
540 - if k.startswith('_'):  
541 - schema_key = k[1:]  
542 - else:  
543 - schema_key = re.sub(r'[^\.]+\.', '', k)  
544 - schema_key = option_to_json_key(schema_key)  
545 - schema_value = v  
546 - is_dict = False  
547 - if k in expected:  
548 - options_seen.add(re.sub('^_', '', k))  
549 - if v is None:  
550 - schema_value = re.sub(  
551 - r'--(\S+)',  
552 - lambda x: option_to_json_key(x.group(1)),  
553 - expected[k]['help'])  
554 - if (isinstance(v, dict)):  
555 - is_dict = True  
556 - schema_value = {}  
557 - build_schema(v, schema_value,  
558 - schema_key, f'{path}.{schema_key}')  
559 - elif (isinstance(v, list)):  
560 - if len(v) != 1:  
561 - raise Exception('json contains array with length != 1')  
562 - if isinstance(v[0], dict):  
563 - is_dict = True  
564 - schema_value = [{}]  
565 - subpath = f'{path}.{schema_key}'  
566 - identifier = self.to_identifier(subpath, '', False)  
567 - self.json_decls.append(  
568 - f'void begin{identifier}Array(JSON);')  
569 - self.json_decls.append(  
570 - f'void end{identifier}Array();')  
571 - self.json_init.append(  
572 - f'beginArray("{flag}",'  
573 - f' bindJSON(&Handlers::begin{identifier}Array),'  
574 - f' bindBare(&Handlers::end{identifier}Array));'  
575 - f' // {subpath}[]')  
576 - build_schema(v[0], schema_value[0],  
577 - schema_key, subpath)  
578 - self.json_init.append(  
579 - f'endContainer(); // {subpath}[]')  
580 - elif schema_value is None:  
581 - raise Exception(f'unknown schema value for {k}')  
582 - s[schema_key] = schema_value  
583 - if not is_dict:  
584 - if is_trivial:  
585 - self.handle_json_trivial(schema_key, expected[k])  
586 - else:  
587 - self.handle_json_manual(schema_key, path)  
588 - if flag:  
589 - self.json_init.append(f'endContainer(); // {path}')  
590 -  
591 - build_schema(data['json'], self.schema, '', '') 591 + self.schema = {}
  592 + self.build_schema(data['json'], self.schema, '', '',
  593 + expected, options_seen)
592 if options_seen != set(expected.keys()): 594 if options_seen != set(expected.keys()):
593 raise Exception('missing from json: ' + 595 raise Exception('missing from json: ' +
594 str(set(expected.keys()) - options_seen)) 596 str(set(expected.keys()) - options_seen))
job.sums
1 # Generated by generate_auto_job 1 # Generated by generate_auto_job
2 -generate_auto_job 68c39a9f6d1f10c1b9cf9af539036621589d6b0bbf3d05a2e1ddd1c9919d7383 2 +generate_auto_job 8c88accfa988c8d6035c3e7e012bbde3da0c76fdfaba8e1460112bf344dc7b4f
3 include/qpdf/auto_job_c_att.hh 7ad43bb374c1370ef32ebdcdcb7b73a61d281f7f4e3f12755585872ab30fb60e 3 include/qpdf/auto_job_c_att.hh 7ad43bb374c1370ef32ebdcdcb7b73a61d281f7f4e3f12755585872ab30fb60e
4 include/qpdf/auto_job_c_copy_att.hh 32275d03cdc69b703dd7e02ba0bbe15756e714e9ad185484773a6178dc09e1ee 4 include/qpdf/auto_job_c_copy_att.hh 32275d03cdc69b703dd7e02ba0bbe15756e714e9ad185484773a6178dc09e1ee
5 include/qpdf/auto_job_c_enc.hh 72e138c7b96ed5aacdce78c1dec04b1c20d361faec4f8faf52f64c1d6be99265 5 include/qpdf/auto_job_c_enc.hh 72e138c7b96ed5aacdce78c1dec04b1c20d361faec4f8faf52f64c1d6be99265