MCPcopy Create free account
hub / github.com/cdgriffith/Box / box_from_file

Function box_from_file

box/from_file.py:108–133  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

106
107
108def 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
136def box_from_string(content: str, string_type: str = "json") -> Box | BoxList:

Callers 5

test_from_allMethod · 0.90
test_bad_fileMethod · 0.90
test_toon_filesMethod · 0.90
test_toon_unavailableMethod · 0.90

Calls 1

BoxErrorClass · 0.90

Tested by 5

test_from_allMethod · 0.72
test_bad_fileMethod · 0.72
test_toon_filesMethod · 0.72
test_toon_unavailableMethod · 0.72