| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 # | 46 # |
| 47 # InFile.load_from_path('file.in', {'arg': None, 'arg2': []}) | 47 # InFile.load_from_path('file.in', {'arg': None, 'arg2': []}) |
| 48 # | 48 # |
| 49 # Parsing produces an array of dictionaries: | 49 # Parsing produces an array of dictionaries: |
| 50 # [ { 'name' : 'name1', 'arg' :' value', arg2=['value2', 'value3'] } | 50 # [ { 'name' : 'name1', 'arg' :' value', arg2=['value2', 'value3'] } |
| 51 | 51 |
| 52 def _is_comment(line): | 52 def _is_comment(line): |
| 53 return line.startswith("//") or line.startswith("#") | 53 return line.startswith("//") or line.startswith("#") |
| 54 | 54 |
| 55 class InFile(object): | 55 class InFile(object): |
| 56 def __init__(self, lines, defaults, default_parameters): | 56 def __init__(self, lines, defaults, valid_values=None, default_parameters=No
ne): |
| 57 self.name_dictionaries = [] | 57 self.name_dictionaries = [] |
| 58 self.parameters = copy.deepcopy(default_parameters if default_parameters
else {}) | 58 self.parameters = copy.deepcopy(default_parameters if default_parameters
else {}) |
| 59 self._defaults = defaults | 59 self._defaults = defaults |
| 60 self._valid_values = copy.deepcopy(valid_values if valid_values else {}) |
| 60 self._parse(map(str.strip, lines)) | 61 self._parse(map(str.strip, lines)) |
| 61 | 62 |
| 62 @classmethod | 63 @classmethod |
| 63 def load_from_path(self, path, defaults, default_parameters): | 64 def load_from_path(self, path, defaults, valid_values, default_parameters): |
| 64 with open(os.path.abspath(path)) as in_file: | 65 with open(os.path.abspath(path)) as in_file: |
| 65 return InFile(in_file.readlines(), defaults, default_parameters) | 66 return InFile(in_file.readlines(), defaults, valid_values, default_p
arameters) |
| 66 | 67 |
| 67 def _is_sequence(self, arg): | 68 def _is_sequence(self, arg): |
| 68 return (not hasattr(arg, "strip") | 69 return (not hasattr(arg, "strip") |
| 69 and hasattr(arg, "__getitem__") | 70 and hasattr(arg, "__getitem__") |
| 70 or hasattr(arg, "__iter__")) | 71 or hasattr(arg, "__iter__")) |
| 71 | 72 |
| 72 def _parse(self, lines): | 73 def _parse(self, lines): |
| 73 parsing_parameters = True | 74 parsing_parameters = True |
| 74 for line in lines: | 75 for line in lines: |
| 75 if _is_comment(line): | 76 if _is_comment(line): |
| (...skipping 24 matching lines...) Expand all Loading... |
| 100 for arg_string in args_list: | 101 for arg_string in args_list: |
| 101 arg_string = arg_string.strip() | 102 arg_string = arg_string.strip() |
| 102 if not arg_string: # Ignore empty args | 103 if not arg_string: # Ignore empty args |
| 103 continue | 104 continue |
| 104 if '=' in arg_string: | 105 if '=' in arg_string: |
| 105 arg_name, arg_value = arg_string.split('=') | 106 arg_name, arg_value = arg_string.split('=') |
| 106 else: | 107 else: |
| 107 arg_name, arg_value = arg_string, True | 108 arg_name, arg_value = arg_string, True |
| 108 if arg_name not in self._defaults: | 109 if arg_name not in self._defaults: |
| 109 self._fatal("Unknown argument: '%s' in line:\n%s\nKnown argument
s: %s" % (arg_name, line, self._defaults.keys())) | 110 self._fatal("Unknown argument: '%s' in line:\n%s\nKnown argument
s: %s" % (arg_name, line, self._defaults.keys())) |
| 111 valid_values = self._valid_values.get(arg_name) |
| 112 if valid_values and arg_value not in valid_values: |
| 113 self._fatal("Unknown value: '%s' in line:\n%s\nKnown values: %s"
% (arg_value, line, valid_values)) |
| 110 if self._is_sequence(args[arg_name]): | 114 if self._is_sequence(args[arg_name]): |
| 111 args[arg_name].append(arg_value) | 115 args[arg_name].append(arg_value) |
| 112 else: | 116 else: |
| 113 args[arg_name] = arg_value | 117 args[arg_name] = arg_value |
| 114 return args | 118 return args |
| 115 | 119 |
| 116 def _fatal(self, message): | 120 def _fatal(self, message): |
| 117 # FIXME: This should probably raise instead of exit(1) | 121 # FIXME: This should probably raise instead of exit(1) |
| 118 print message | 122 print message |
| 119 exit(1) | 123 exit(1) |
| OLD | NEW |