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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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._valid_values = copy.deepcopy(valid_values if valid_values else {}) |
61 self._parse(map(str.strip, lines)) | 61 self._parse(map(str.strip, lines)) |
62 | 62 |
63 @classmethod | 63 @classmethod |
64 def load_from_files(self, file_paths, defaults, valid_values, default_parame
ters): | 64 def load_from_files(self, file_paths, defaults, valid_values, default_parame
ters): |
65 lines = [] | 65 lines = [] |
66 for path in file_paths: | 66 for path in file_paths: |
| 67 assert path.endswith(".in") |
67 with open(os.path.abspath(path)) as in_file: | 68 with open(os.path.abspath(path)) as in_file: |
68 lines += in_file.readlines() | 69 lines += in_file.readlines() |
69 return InFile(lines, defaults, valid_values, default_parameters) | 70 return InFile(lines, defaults, valid_values, default_parameters) |
70 | 71 |
71 def _is_sequence(self, arg): | 72 def _is_sequence(self, arg): |
72 return (not hasattr(arg, "strip") | 73 return (not hasattr(arg, "strip") |
73 and hasattr(arg, "__getitem__") | 74 and hasattr(arg, "__getitem__") |
74 or hasattr(arg, "__iter__")) | 75 or hasattr(arg, "__iter__")) |
75 | 76 |
76 def _parse(self, lines): | 77 def _parse(self, lines): |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 if self._is_sequence(args[arg_name]): | 152 if self._is_sequence(args[arg_name]): |
152 args[arg_name].append(arg_value) | 153 args[arg_name].append(arg_value) |
153 else: | 154 else: |
154 args[arg_name] = arg_value | 155 args[arg_name] = arg_value |
155 return args | 156 return args |
156 | 157 |
157 def _fatal(self, message): | 158 def _fatal(self, message): |
158 # FIXME: This should probably raise instead of exit(1) | 159 # FIXME: This should probably raise instead of exit(1) |
159 print message | 160 print message |
160 exit(1) | 161 exit(1) |
OLD | NEW |