| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 ''' | 6 ''' |
| 7 Checks a policy_templates.json file for conformity to its syntax specification. | 7 Checks a policy_templates.json file for conformity to its syntax specification. |
| 8 ''' | 8 ''' |
| 9 | 9 |
| 10 import json | 10 import json |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 (if the key is not present): | 95 (if the key is not present): |
| 96 Error: |container_name| must have a |value_type| named |key|. | 96 Error: |container_name| must have a |value_type| named |key|. |
| 97 Offending snippet: |offending| (if specified; defaults to |container|) | 97 Offending snippet: |offending| (if specified; defaults to |container|) |
| 98 (if the value does not have the required type): | 98 (if the value does not have the required type): |
| 99 Error: Value of |key| must be a |value_type|. | 99 Error: Value of |key| must be a |value_type|. |
| 100 Offending snippet: |container[key]| | 100 Offending snippet: |container[key]| |
| 101 | 101 |
| 102 Returns: |container[key]| if the key is present, None otherwise. | 102 Returns: |container[key]| if the key is present, None otherwise. |
| 103 ''' | 103 ''' |
| 104 if identifier is None: | 104 if identifier is None: |
| 105 identifier = container.get('name') | 105 try: |
| 106 identifier = container.get('name') |
| 107 except: |
| 108 self._Error('Cannot access container name of "%s".' % container_name) |
| 109 return None |
| 106 if container_name is None: | 110 if container_name is None: |
| 107 container_name = parent_element | 111 container_name = parent_element |
| 108 if offending == '__CONTAINER__': | 112 if offending == '__CONTAINER__': |
| 109 offending = container | 113 offending = container |
| 110 if key not in container: | 114 if key not in container: |
| 111 if optional: | 115 if optional: |
| 112 return | 116 return |
| 113 else: | 117 else: |
| 114 self._Error('%s must have a %s "%s".' % | 118 self._Error('%s must have a %s "%s".' % |
| 115 (container_name.title(), value_type.__name__, key), | 119 (container_name.title(), value_type.__name__, key), |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 with open(filename, 'w') as f: | 433 with open(filename, 'w') as f: |
| 430 f.writelines(fixed_lines) | 434 f.writelines(fixed_lines) |
| 431 | 435 |
| 432 def Main(self, filename, options): | 436 def Main(self, filename, options): |
| 433 try: | 437 try: |
| 434 with open(filename) as f: | 438 with open(filename) as f: |
| 435 data = eval(f.read()) | 439 data = eval(f.read()) |
| 436 except: | 440 except: |
| 437 import traceback | 441 import traceback |
| 438 traceback.print_exc(file=sys.stdout) | 442 traceback.print_exc(file=sys.stdout) |
| 439 self._Error('Invalid JSON syntax.') | 443 self._Error('Invalid Python/JSON syntax.') |
| 440 return | 444 return 1 |
| 441 if data == None: | 445 if data == None: |
| 442 self._Error('Invalid JSON syntax.') | 446 self._Error('Invalid Python/JSON syntax.') |
| 443 return | 447 return 1 |
| 444 self.options = options | 448 self.options = options |
| 445 | 449 |
| 446 # First part: check JSON structure. | 450 # First part: check JSON structure. |
| 447 | 451 |
| 448 # Check (non-policy-specific) message definitions. | 452 # Check (non-policy-specific) message definitions. |
| 449 messages = self._CheckContains(data, 'messages', dict, | 453 messages = self._CheckContains(data, 'messages', dict, |
| 450 parent_element=None, | 454 parent_element=None, |
| 451 container_name='The root element', | 455 container_name='The root element', |
| 452 offending=None) | 456 offending=None) |
| 453 if messages is not None: | 457 if messages is not None: |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 if filename is None: | 503 if filename is None: |
| 500 if len(args) != 2: | 504 if len(args) != 2: |
| 501 parser.print_help() | 505 parser.print_help() |
| 502 sys.exit(1) | 506 sys.exit(1) |
| 503 filename = args[1] | 507 filename = args[1] |
| 504 return self.Main(filename, options) | 508 return self.Main(filename, options) |
| 505 | 509 |
| 506 | 510 |
| 507 if __name__ == '__main__': | 511 if __name__ == '__main__': |
| 508 sys.exit(PolicyTemplateChecker().Run(sys.argv)) | 512 sys.exit(PolicyTemplateChecker().Run(sys.argv)) |
| OLD | NEW |