Loads content from file_path if file_path's extension is one of allowed ones (See ALLOWED_EXTS). Throws UnsupportedMetaException on disallowed filetypes. Throws ValueError on malformed meta. :param file_path: Absolute path to the file to load content from.
(self, file_path, expected_type=None)
| 231 | """ |
| 232 | |
| 233 | def load(self, file_path, expected_type=None): |
| 234 | """ |
| 235 | Loads content from file_path if file_path's extension |
| 236 | is one of allowed ones (See ALLOWED_EXTS). |
| 237 | |
| 238 | Throws UnsupportedMetaException on disallowed filetypes. |
| 239 | Throws ValueError on malformed meta. |
| 240 | |
| 241 | :param file_path: Absolute path to the file to load content from. |
| 242 | :type file_path: ``str`` |
| 243 | |
| 244 | :param expected_type: Expected type for the loaded and parsed content (optional). |
| 245 | :type expected_type: ``object`` |
| 246 | |
| 247 | :rtype: ``dict`` |
| 248 | """ |
| 249 | file_name, file_ext = os.path.splitext(file_path) |
| 250 | |
| 251 | if file_ext not in ALLOWED_EXTS: |
| 252 | raise Exception( |
| 253 | "Unsupported meta type %s, file %s. Allowed: %s" |
| 254 | % (file_ext, file_path, ALLOWED_EXTS) |
| 255 | ) |
| 256 | |
| 257 | result = self._load(PARSER_FUNCS[file_ext], file_path) |
| 258 | |
| 259 | if expected_type and not isinstance(result, expected_type): |
| 260 | actual_type = type(result).__name__ |
| 261 | error = 'Expected "%s", got "%s"' % (expected_type.__name__, actual_type) |
| 262 | raise ValueError(error) |
| 263 | |
| 264 | return result |
| 265 | |
| 266 | def _load(self, parser_func, file_path): |
| 267 | with open(file_path, "r", encoding="utf-8") as fd: |