-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,12 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
# Jerome Dumonteil <[email protected]> | ||
"""Document class, root of the ODF document | ||
""" | ||
import os | ||
import posixpath | ||
from copy import deepcopy | ||
from mimetypes import guess_type | ||
from operator import itemgetter | ||
from pathlib import Path | ||
from typing import Union | ||
from uuid import uuid4 | ||
|
||
from .const import ( | ||
|
@@ -83,7 +83,14 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
|
||
AUTOMATIC_PREFIX = "odfdo_auto_" | ||
|
||
underline_lvl = ["=", "-", ":", "`", "'", '"', "~", "^", "_", "*", "+"] | ||
UNDERLINE_LVL = ["=", "-", ":", "`", "'", '"', "~", "^", "_", "*", "+"] | ||
|
||
|
||
def _underline_string(level: int, name: str) -> str: | ||
"""Underline string of the name.""" | ||
if level >= len(UNDERLINE_LVL): | ||
return "\n" | ||
return underline_lvl[level] * len(name) | ||
|
||
|
||
def _show_styles(element, level=0): | ||
|
@@ -95,10 +102,8 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
return None | ||
tag_name = element.tag | ||
output.append(tag_name) | ||
# Underline and Overline the name | ||
underline = underline_lvl[level] * len(tag_name) | ||
underline = underline if level < len(underline_lvl) else "\n" | ||
output.append(underline) | ||
# Underline the name | ||
output.append(_underline_string(level, tag_name)) | ||
# Add a separation between name and attributes | ||
output[-1] += "\n" | ||
attrs = [] | ||
|
@@ -146,7 +151,7 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
class Document: | ||
"""Abstraction of the ODF document.""" | ||
|
||
def __init__(self, target="text"): | ||
def __init__(self, target: Union[str, bytes, Path, Container, None] = "text"): | ||
# Cache of XML parts | ||
self.__xmlparts = {} | ||
# Cache of the body | ||
|
@@ -420,8 +425,9 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
return "\n".join(result) + "\n" | ||
|
||
def add_file(self, path_or_file): | ||
"""Insert a file from a path or a fike-like object in the container. | ||
Return the full path to reference it in the content. | ||
"""Insert a file from a path or a file-like object in the container. | ||
|
||
Return the full path to reference in the content. | ||
|
||
Arguments: | ||
|
||
|
@@ -442,6 +448,7 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
if posixpath.join("Pictures", name) in medias: | ||
name = f"{path.stem}_{uuid4()}{extension}" | ||
else: | ||
path = None | ||
name = getattr(path_or_file, "name", None) | ||
if not name: | ||
name = str(uuid4()) | ||
|
@@ -452,7 +459,7 @@ <h1 class="title">Module <code>odfdo.document</code></h1> | |
manifest.add_full_path("Pictures/") | ||
full_path = posixpath.join("Pictures", name) | ||
if path is None: | ||
self.container.set_part(full_path, handler.read()) | ||
self.container.set_part(full_path, path_or_file.read()) | ||
else: | ||
self.container.set_part(full_path, path.read_bytes()) | ||
manifest.add_full_path(full_path, media_type) | ||
|
@@ -865,7 +872,7 @@ <h2 class="section-title" id="header-classes">Classes</h2> | |
<dl> | ||
<dt id="odfdo.document.Document"><code class="flex name class"> | ||
<span>class <span class="ident">Document</span></span> | ||
<span>(</span><span>target='text')</span> | ||
<span>(</span><span>target: Union[str, bytes, pathlib.Path, <a title="odfdo.container.Container" href="container.html#odfdo.container.Container">Container</a>, ForwardRef(None)] = 'text')</span> | ||
</code></dt> | ||
<dd> | ||
<div class="desc"><p>Abstraction of the ODF document.</p></div> | ||
|
@@ -876,7 +883,7 @@ <h2 class="section-title" id="header-classes">Classes</h2> | |
<pre><code class="python">class Document: | ||
"""Abstraction of the ODF document.""" | ||
|
||
def __init__(self, target="text"): | ||
def __init__(self, target: Union[str, bytes, Path, Container, None] = "text"): | ||
# Cache of XML parts | ||
self.__xmlparts = {} | ||
# Cache of the body | ||
|
@@ -1150,8 +1157,9 @@ <h2 class="section-title" id="header-classes">Classes</h2> | |
return "\n".join(result) + "\n" | ||
|
||
def add_file(self, path_or_file): | ||
"""Insert a file from a path or a fike-like object in the container. | ||
Return the full path to reference it in the content. | ||
"""Insert a file from a path or a file-like object in the container. | ||
|
||
Return the full path to reference in the content. | ||
|
||
Arguments: | ||
|
||
|
@@ -1172,6 +1180,7 @@ <h2 class="section-title" id="header-classes">Classes</h2> | |
if posixpath.join("Pictures", name) in medias: | ||
name = f"{path.stem}_{uuid4()}{extension}" | ||
else: | ||
path = None | ||
name = getattr(path_or_file, "name", None) | ||
if not name: | ||
name = str(uuid4()) | ||
|
@@ -1182,7 +1191,7 @@ <h2 class="section-title" id="header-classes">Classes</h2> | |
manifest.add_full_path("Pictures/") | ||
full_path = posixpath.join("Pictures", name) | ||
if path is None: | ||
self.container.set_part(full_path, handler.read()) | ||
self.container.set_part(full_path, path_or_file.read()) | ||
else: | ||
self.container.set_part(full_path, path.read_bytes()) | ||
manifest.add_full_path(full_path, media_type) | ||
|
@@ -1685,8 +1694,8 @@ <h3>Methods</h3> | |
<span>def <span class="ident">add_file</span></span>(<span>self, path_or_file)</span> | ||
</code></dt> | ||
<dd> | ||
<div class="desc"><p>Insert a file from a path or a fike-like object in the container. | ||
Return the full path to reference it in the content.</p> | ||
<div class="desc"><p>Insert a file from a path or a file-like object in the container.</p> | ||
<p>Return the full path to reference in the content.</p> | ||
<h2 id="arguments">Arguments</h2> | ||
<p>path_or_file – str or Path or file-like</p> | ||
<p>Return: str (URI)</p></div> | ||
|
@@ -1695,8 +1704,9 @@ <h2 id="arguments">Arguments</h2> | |
<span>Expand source code</span> | ||
</summary> | ||
<pre><code class="python">def add_file(self, path_or_file): | ||
"""Insert a file from a path or a fike-like object in the container. | ||
Return the full path to reference it in the content. | ||
"""Insert a file from a path or a file-like object in the container. | ||
|
||
Return the full path to reference in the content. | ||
|
||
Arguments: | ||
|
||
|
@@ -1717,6 +1727,7 @@ <h2 id="arguments">Arguments</h2> | |
if posixpath.join("Pictures", name) in medias: | ||
name = f"{path.stem}_{uuid4()}{extension}" | ||
else: | ||
path = None | ||
name = getattr(path_or_file, "name", None) | ||
if not name: | ||
name = str(uuid4()) | ||
|
@@ -1727,7 +1738,7 @@ <h2 id="arguments">Arguments</h2> | |
manifest.add_full_path("Pictures/") | ||
full_path = posixpath.join("Pictures", name) | ||
if path is None: | ||
self.container.set_part(full_path, handler.read()) | ||
self.container.set_part(full_path, path_or_file.read()) | ||
else: | ||
self.container.set_part(full_path, path.read_bytes()) | ||
manifest.add_full_path(full_path, media_type) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters