MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / add

Method add

src/__init__.py:2000–2103  ·  view source on GitHub ↗

Add a sub-archive. Args: content: The content to be added. May be one of: `str` - must be path of directory or file. `bytes`, `bytearray`, `io.BytesIO` - raw data. `zipfile.Zipfile`.

( self, content, path=None)

Source from the content-addressed store, hash-verified

1998 mupdf.fz_mount_multi_archive( self.this, sub, path)
1999
2000 def add( self, content, path=None):
2001 '''
2002 Add a sub-archive.
2003
2004 Args:
2005 content:
2006 The content to be added. May be one of:
2007 `str` - must be path of directory or file.
2008 `bytes`, `bytearray`, `io.BytesIO` - raw data.
2009 `zipfile.Zipfile`.
2010 `tarfile.TarFile`.
2011 `pymupdf.Archive`.
2012 A two-item tuple `(data, name)`.
2013 List or tuple (but not tuple with length 2) of the above.
2014 path: (str) a "virtual" path name, under which the elements
2015 of content can be retrieved. Use it to e.g. cope with
2016 duplicate element names.
2017 '''
2018 def is_binary_data(x):
2019 return isinstance(x, (bytes, bytearray, io.BytesIO))
2020
2021 def make_subarch(entries, mount, fmt):
2022 subarch = dict(fmt=fmt, entries=entries, path=mount)
2023 if fmt != "tree" or self._subarchives == []:
2024 self._subarchives.append(subarch)
2025 else:
2026 ltree = self._subarchives[-1]
2027 if ltree["fmt"] != "tree" or ltree["path"] != subarch["path"]:
2028 self._subarchives.append(subarch)
2029 else:
2030 ltree["entries"].extend(subarch["entries"])
2031 self._subarchives[-1] = ltree
2032
2033 if isinstance(content, pathlib.Path):
2034 content = str(content)
2035
2036 if isinstance(content, str):
2037 if os.path.isdir(content):
2038 self._add_dir(content, path)
2039 return make_subarch(os.listdir(content), path, 'dir')
2040 elif os.path.isfile(content):
2041 assert isinstance(path, str) and path != '', \
2042 f'Need name for binary content, but {path=}.'
2043 with io.open(content, 'rb') as f:
2044 ff = f.read()
2045 self._add_treeitem(ff, path)
2046 return make_subarch([path], None, 'tree')
2047 else:
2048 raise ValueError(f'Not a file or directory: {content!r}')
2049
2050 elif is_binary_data(content):
2051 assert isinstance(path, str) and path != '' \
2052 f'Need name for binary content, but {path=}.'
2053 self._add_treeitem(content, path)
2054 return make_subarch([path], None, 'tree')
2055
2056 elif isinstance(content, zipfile.ZipFile):
2057 filename = getattr(content, "filename", None)

Callers 15

__init__Method · 0.95
test_4746Function · 0.95
extract_objectsFunction · 0.45
process_blocksFunction · 0.45
build_subsetMethod · 0.45
repl_fontnamesMethod · 0.45
subset_fontsMethod · 0.45
appendMethod · 0.45
css_for_pymupdf_fontFunction · 0.45
cells_to_tablesFunction · 0.45
cibuildwheelFunction · 0.45
extract_objectsFunction · 0.45

Calls 5

_add_dirMethod · 0.95
_add_treeitemMethod · 0.95
_add_ziptarmemoryMethod · 0.95
_add_ziptarfileMethod · 0.95
_add_archMethod · 0.95

Tested by 2

test_4746Function · 0.76
cibuildwheelFunction · 0.36