Provides access to pack URI components such as the baseURI and the filename slice. Behaves as |str| otherwise.
| 10 | |
| 11 | |
| 12 | class PackURI(str): |
| 13 | """Provides access to pack URI components such as the baseURI and the filename slice. |
| 14 | |
| 15 | Behaves as |str| otherwise. |
| 16 | """ |
| 17 | |
| 18 | _filename_re = re.compile("([a-zA-Z]+)([1-9][0-9]*)?") |
| 19 | |
| 20 | def __new__(cls, pack_uri_str: str): |
| 21 | if pack_uri_str[0] != "/": |
| 22 | tmpl = "PackURI must begin with slash, got '%s'" |
| 23 | raise ValueError(tmpl % pack_uri_str) |
| 24 | return str.__new__(cls, pack_uri_str) |
| 25 | |
| 26 | @staticmethod |
| 27 | def from_rel_ref(baseURI: str, relative_ref: str) -> PackURI: |
| 28 | """The absolute PackURI formed by translating `relative_ref` onto `baseURI`.""" |
| 29 | joined_uri = posixpath.join(baseURI, relative_ref) |
| 30 | abs_uri = posixpath.abspath(joined_uri) |
| 31 | return PackURI(abs_uri) |
| 32 | |
| 33 | @property |
| 34 | def baseURI(self) -> str: |
| 35 | """The base URI of this pack URI, the directory portion, roughly speaking. |
| 36 | |
| 37 | E.g. ``'/ppt/slides'`` for ``'/ppt/slides/slide1.xml'``. For the package pseudo- |
| 38 | partname '/', baseURI is '/'. |
| 39 | """ |
| 40 | return posixpath.split(self)[0] |
| 41 | |
| 42 | @property |
| 43 | def ext(self) -> str: |
| 44 | """The extension portion of this pack URI, e.g. ``'xml'`` for ``'/word/document.xml'``. |
| 45 | |
| 46 | Note the period is not included. |
| 47 | """ |
| 48 | # raw_ext is either empty string or starts with period, e.g. '.xml' |
| 49 | raw_ext = posixpath.splitext(self)[1] |
| 50 | return raw_ext[1:] if raw_ext.startswith(".") else raw_ext |
| 51 | |
| 52 | @property |
| 53 | def filename(self): |
| 54 | """The "filename" portion of this pack URI, e.g. ``'slide1.xml'`` for |
| 55 | ``'/ppt/slides/slide1.xml'``. |
| 56 | |
| 57 | For the package pseudo-partname '/', filename is ''. |
| 58 | """ |
| 59 | return posixpath.split(self)[1] |
| 60 | |
| 61 | @property |
| 62 | def idx(self): |
| 63 | """Return partname index as integer for tuple partname or None for singleton |
| 64 | partname, e.g. ``21`` for ``'/ppt/slides/slide21.xml'`` and |None| for |
| 65 | ``'/ppt/presentation.xml'``.""" |
| 66 | filename = self.filename |
| 67 | if not filename: |
| 68 | return None |
| 69 | name_part = posixpath.splitext(filename)[0] # filename w/ext removed |
no outgoing calls
searching dependent graphs…