Loads the provided file and tries to parse it into a Box or BoxList object as appropriate. :param file: Location of file :param encoding: File encoding :param errors: How to handle encoding errors :param file_type: manually specify file type: json, toml or yaml :return: Box
(
file: str | PathLike,
file_type: str | None = None,
encoding: str = "utf-8",
errors: str = "strict",
**kwargs,
)
| 106 | |
| 107 | |
| 108 | def box_from_file( |
| 109 | file: str | PathLike, |
| 110 | file_type: str | None = None, |
| 111 | encoding: str = "utf-8", |
| 112 | errors: str = "strict", |
| 113 | **kwargs, |
| 114 | ) -> Box | BoxList: |
| 115 | """ |
| 116 | Loads the provided file and tries to parse it into a Box or BoxList object as appropriate. |
| 117 | |
| 118 | :param file: Location of file |
| 119 | :param encoding: File encoding |
| 120 | :param errors: How to handle encoding errors |
| 121 | :param file_type: manually specify file type: json, toml or yaml |
| 122 | :return: Box or BoxList |
| 123 | """ |
| 124 | |
| 125 | if not isinstance(file, Path): |
| 126 | file = Path(file) |
| 127 | if not file.exists(): |
| 128 | raise BoxError(f'file "{file}" does not exist') |
| 129 | file_type = file_type or file.suffix |
| 130 | file_type = file_type.lower().lstrip(".") |
| 131 | if file_type.lower() in converters: |
| 132 | return converters[file_type.lower()](file, encoding, errors, **kwargs) # type: ignore |
| 133 | raise BoxError(f'"{file_type}" is an unknown type. Please use either csv, toon, toml, msgpack, yaml or json') |
| 134 | |
| 135 | |
| 136 | def box_from_string(content: str, string_type: str = "json") -> Box | BoxList: |