timApp.documentmodel package¶
Submodules¶
timApp.documentmodel.attributeparser module¶
timApp.documentmodel.clipboard module¶
-
class
timApp.documentmodel.clipboard.
Clipboard
(files_root: str)[source]¶ Bases:
object
-
class
UserClipboard
(parent: timApp.documentmodel.clipboard.Clipboard, user_id: int)[source]¶ Bases:
object
-
copy_pars
(doc: documentmodel.document.Document, par_start: str, par_end: str, area_name: typing.Union[str, NoneType] = None, ref_doc: typing.Union[documentmodel.document.Document, NoneType] = None, disable_ref: bool = False) → typing.List[documentmodel.docparagraph.DocParagraph][source]¶
-
cut_pars
(doc: documentmodel.document.Document, par_start: str, par_end: str, area_name: typing.Union[str, NoneType] = None) → typing.List[documentmodel.docparagraph.DocParagraph][source]¶
-
paste_after
(doc: documentmodel.document.Document, par_id: str, as_ref: bool = False) → typing.List[documentmodel.docparagraph.DocParagraph][source]¶
-
paste_before
(doc: documentmodel.document.Document, par_id: str, as_ref: bool = False) → typing.List[documentmodel.docparagraph.DocParagraph][source]¶
-
-
class
timApp.documentmodel.docparagraph module¶
-
class
timApp.documentmodel.docparagraph.
DocParagraph
(doc, files_root: typing.Union[str, NoneType] = None)[source]¶ Bases:
object
-
classmethod
create
(doc, par_id: typing.Union[str, NoneType] = None, md: str = '', t: typing.Union[str, NoneType] = None, html: typing.Union[str, NoneType] = None, attrs: typing.Union[typing.Dict[KT, VT], NoneType] = None, props: typing.Union[typing.Dict[KT, VT], NoneType] = None, files_root: typing.Union[str, NoneType] = None) → timApp.documentmodel.docparagraph.DocParagraph[source]¶
-
classmethod
create_area_reference
(doc, area_name: str, r: typing.Union[str, NoneType] = None, add_rd: typing.Union[bool, NoneType] = True, files_root: typing.Union[str, NoneType] = None)[source]¶
-
create_reference
(doc, r: typing.Union[str, NoneType] = None, add_rd: bool = True) → timApp.documentmodel.docparagraph.DocParagraph[source]¶
-
default_files_root
= 'tim_files'¶
-
classmethod
from_dict
(doc, d: typing.Dict[KT, VT], files_root: typing.Union[str, NoneType] = None) → timApp.documentmodel.docparagraph.DocParagraph[source]¶
-
classmethod
get
(doc, par_id: str, t: str, files_root: typing.Union[str, NoneType] = None) → timApp.documentmodel.docparagraph.DocParagraph[source]¶
-
get_attr
(attr_name: str, default_value: typing.Generic = None, dereference: bool = False) → typing.Generic[source]¶
-
get_attrs
(base_attrs: typing.Union[typing.Dict[KT, VT], NoneType] = None) → typing.Dict[KT, VT][source]¶
-
get_auto_macro_values
(macros, macro_delim, auto_macro_cache, heading_cache)[source]¶ Gets the auto macros values for the current paragraph. Auto macros include things like current heading/table/figure numbers.
Parameters: - heading_cache – A cache object to store headings into. The key is paragraph id and value is a list of headings in that paragraph.
- macros – Macros to apply for the paragraph.
- auto_macro_cache – The cache object from which to retrieve and store the auto macro data.
- macro_delim – Delimiter for macros.
Returns: Auto macro values as a dict.
Returns: A dict(str, dict(int,int)) containing the auto macro information.
-
get_html
(from_preview: bool = True) → str[source]¶ Gets the html for the paragraph. :param from_preview: Whether this is called from a preview window or not.
If True, previous paragraphs are preloaded too and the result is not cached. Safer, but slower. Set explicitly False if you know what you’re doing.Returns: html string
-
classmethod
get_latest
(doc, par_id: str, files_root: typing.Union[str, NoneType] = None) → timApp.documentmodel.docparagraph.DocParagraph[source]¶
-
get_properties
(base_props: typing.Union[typing.Dict[KT, VT], NoneType] = None) → typing.Dict[KT, VT][source]¶
-
get_referenced_pars
(edit_window: bool = False, set_html: bool = True, source_doc: bool = None, tr_get_one: bool = True, cycle: typing.Union[typing.List[typing.Tuple[int, str]], NoneType] = None)[source]¶
-
classmethod
get_unloaded_pars
(pars, settings, auto_macro_cache, heading_cache, clear_cache=False)[source]¶ Finds out which of the given paragraphs need to be preloaded again.
Parameters: - pars – The list of paragraphs to be processed.
- settings – The settings for the document.
- auto_macro_cache – The cache object from which to retrieve and store the auto macro data.
- heading_cache – A cache object to store headings into. The key is paragraph id and value is a list of headings in that paragraph.
- clear_cache – Whether all caches should be refreshed.
Returns: A 5-tuple of the form: (paragraph, hash of the auto macro values, auto macros, so far used headings, old HTML).
-
classmethod
preload_htmls
(pars: typing.List[typing.DocParagraph], settings, clear_cache: bool = False, context_par: typing.Union[typing.DocParagraph, NoneType] = None, persist: bool = True)[source]¶ Loads the HTML for each paragraph in the given list. :param context_par: The context paragraph. Required only for previewing for now. :param persist: Whether the result of preloading should be saved to disk. :param clear_cache: Whether all caches should be refreshed. :param settings: The document settings. :param pars: Paragraphs to preload. :return: A list of paragraphs whose HTML changed as the result of preloading.
-
classmethod
timApp.documentmodel.docparagraphencoder module¶
timApp.documentmodel.docsettings module¶
-
class
timApp.documentmodel.docsettings.
DocSettings
[source]¶ Bases:
object
-
auto_number_headings_key
= 'auto_number_headings'¶
-
css_key
= 'css'¶
-
classmethod
from_paragraph
(par)[source]¶ Constructs DocSettings from the given DocParagraph.
Parameters: par (DocParagraph) – The DocParagraph to extract settings from. Returns: The DocSettings object.
-
global_plugin_attrs_key
= 'global_plugin_attrs'¶
-
heading_format_key
= 'heading_format'¶
-
macro_delimiter_key
= 'macro_delimiter'¶
-
macros_key
= 'macros'¶
-
show_task_summary_key
= 'show_task_summary'¶
-
source_document_key
= 'source_document'¶
-
timApp.documentmodel.document module¶
-
class
timApp.documentmodel.document.
DocParagraphIter
(doc: timApp.documentmodel.document.Document)[source]¶ Bases:
object
-
class
timApp.documentmodel.document.
Document
(doc_id: typing.Union[int, NoneType] = None, files_root=None, modifier_group_id: typing.Union[int, NoneType] = 0)[source]¶ Bases:
object
-
add_area_ref_paragraph
(src_doc: timApp.documentmodel.document.Document, src_area_name: str, text: typing.Union[str, NoneType] = None, attrs: typing.Union[dict, NoneType] = None, properties: typing.Union[dict, NoneType] = None) → documentmodel.docparagraph.DocParagraph[source]¶
-
add_paragraph
(text: str, par_id: typing.Union[str, NoneType] = None, attrs: typing.Union[dict, NoneType] = None, properties: typing.Union[dict, NoneType] = None) → documentmodel.docparagraph.DocParagraph[source]¶ Appends a new paragraph into the document. :param par_id: The id of the paragraph or None if it should be autogenerated. :param attrs: The attributes for the paragraph. :param text: New paragraph text. :return: The new paragraph object.
-
add_paragraph_obj
(p: documentmodel.docparagraph.DocParagraph) → documentmodel.docparagraph.DocParagraph[source]¶ Appends a new paragraph into the document. :param p: Paragraph to be added. :return: The same paragraph object, or None if could not add.
-
add_ref_paragraph
(src_par: documentmodel.docparagraph.DocParagraph, text: typing.Union[str, NoneType] = None, attrs: typing.Union[dict, NoneType] = None, properties: typing.Union[dict, NoneType] = None) → documentmodel.docparagraph.DocParagraph[source]¶
-
calculate_referenced_document_ids
(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → typing.Set[int][source]¶ Gets all the document ids that are referenced from this document recursively. :return: The set of the document ids.
-
default_files_root
= 'tim_files'¶
-
delete_paragraph
(par_id: str)[source]¶ Removes a paragraph from the document. :param par_id: Paragraph id to remove.
-
classmethod
doc_exists
(doc_id: int, files_root: typing.Union[str, NoneType] = None) → bool[source]¶ Checks if a document id exists. :param doc_id: Document id. :return: Boolean.
-
classmethod
get_next_free_id
(files_root: typing.Union[str, NoneType] = None) → int[source]¶ Gets the next free document id. :return:
-
get_referenced_document_ids
(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → typing.Set[int][source]¶
-
get_section
(par_id_start: str, par_id_end: str) → typing.List[documentmodel.docparagraph.DocParagraph][source]¶
-
get_version
() → typing.Tuple[int, int][source]¶ Gets the latest version of the document as a major-minor tuple. :return: Latest version, or (-1, 0) if there isn’t yet one.
-
has_paragraph
(par_id: str) → bool[source]¶ Checks if the document has the given paragraph. :param par_id: The paragraph id. :return: Boolean.
-
insert_paragraph
(text: str, insert_before_id: typing.Union[str, NoneType], attrs: typing.Union[dict, NoneType] = None, properties: typing.Union[dict, NoneType] = None, par_id: typing.Union[str, NoneType] = None) → documentmodel.docparagraph.DocParagraph[source]¶ Inserts a paragraph before a given paragraph id. :param par_id: The id of the new paragraph or None if it should be autogenerated. :param attrs: The attributes for the paragraph. :param text: New paragraph text. :param insert_before_id: Id of the paragraph to insert before, or None if last. :return: The inserted paragraph object.
-
insert_paragraph_obj
(p: documentmodel.docparagraph.DocParagraph, insert_before_id: typing.Union[str, NoneType]) → documentmodel.docparagraph.DocParagraph[source]¶
-
load_pars
()[source]¶ Loads the paragraphs from disk to memory so that subsequent iterations for the Document are faster.
-
modify_paragraph
(par_id: str, new_text: str, new_attrs: typing.Union[dict, NoneType] = None, new_properties: typing.Union[dict, NoneType] = None) → documentmodel.docparagraph.DocParagraph[source]¶ Modifies the text of the given paragraph. :param par_id: Paragraph id. :param new_text: New text. :return: The new paragraph object.
-
modify_paragraph_obj
(par_id: str, p: documentmodel.docparagraph.DocParagraph) → documentmodel.docparagraph.DocParagraph[source]¶
-
classmethod
remove
(doc_id: int, files_root: typing.Union[str, NoneType] = None, ignore_exists=False)[source]¶ Removes the whole document. :param doc_id: Document id to remove. :return:
-
update
(text: str, original: str, strict_validation=True)[source]¶ Replaces the document’s contents with the specified text.
Parameters: - text – The new text for the document.
- original – The original text for the document.
- strict_validation – Whether to use stricter validation rules for areas etc.
-
-
timApp.documentmodel.document.
dereference_pars
(pars, edit_window: bool = False, source_doc: typing.Union[timApp.documentmodel.document.Document, NoneType] = None)[source]¶ Resolves references in the given paragraphs.
Parameters: - pars (list[DocParagraph]) – The DocParagraphs to be processed.
- edit_window – Calling from edit window or not.
- source_doc – Default document for referencing.
timApp.documentmodel.documentparser module¶
-
class
timApp.documentmodel.documentparser.
DocReader
(lines, i=0)[source]¶ Bases:
object
Parameters: - lines (list[str]) –
- i (int) –
-
class
timApp.documentmodel.documentparser.
DocumentParser
(doc_text='')[source]¶ Bases:
object
Splits documents into paragraphs.
-
exception
timApp.documentmodel.documentparser.
ValidationWarning
[source]¶ Bases:
timApp.documentmodel.documentparser.ValidationException
timApp.documentmodel.documentparseroptions module¶
-
class
timApp.documentmodel.documentparseroptions.
DocumentParserOptions
(break_on_empty_line=False, break_on_code_block=True, break_on_header=True, break_on_normal=True)[source]¶ Bases:
object
-
static
break_on_empty_lines
() → timApp.documentmodel.documentparseroptions.DocumentParserOptions[source]¶
-
static
timApp.documentmodel.documentversion module¶
-
class
timApp.documentmodel.documentversion.
DocumentVersion
[source]¶ Bases:
documentmodel.document.Document
-
add_paragraph
()[source]¶ Parameters: - text (
str
) – - properties (
dict|None
) – - attrs (
dict|None
) – - par_id (
str|None
) –
Return type: DocParagraph
- text (
-
delete_section
(area_start: str, area_end: str)[source]¶ Parameters: - area_start (
str
) – - area_end (
str
) –
- area_start (
-
has_paragraph
(par_id: str) → bool[source]¶ - Checks if the document has the given paragraph.
Parameters: par_id ( str
) – The paragraph id.Returns: Boolean. Return type: bool
-
insert_paragraph
()[source]¶ Parameters: - text (
str
) – - properties (
dict|None
) – - insert_before_id (
str|None
) – - attrs (
dict|None
) – - par_id (
str|None
) –
Return type: DocParagraph
- text (
-
insert_paragraph_obj
()[source]¶ Parameters: - p (
DocParagraph
) – - insert_before_id (
str|None
) –
Return type: DocParagraph
- p (
-
modify_paragraph
()[source]¶ Parameters: - new_attrs (
dict|None
) – - new_properties (
dict|None
) – - par_id (
str
) – - new_text (
str
) –
Return type: DocParagraph
- new_attrs (
-
modify_paragraph_obj
()[source]¶ Parameters: - p (
DocParagraph
) – - par_id (
str
) –
Return type: DocParagraph
- p (
-
timApp.documentmodel.documentwriter module¶
Defines the DocumentWriter class.
timApp.documentmodel.exceptions module¶
timApp.documentmodel.randutils module¶
timApp.documentmodel.test_attributeparser module¶
timApp.documentmodel.test_clipboard module¶
Unit tests for Clipboard class.
Run from parent directory with command: python3 -m unittest dumboclient filemodehelper documentmodel/test_clipboard.py
timApp.documentmodel.test_document module¶
Unit tests for Document class.
Run from parent directory with command: python3 -m unittest dumboclient filemodehelper documentmodel/test_document.py
timApp.documentmodel.test_documentparser module¶
timApp.documentmodel.test_references module¶
Unit tests for testing paragraph referencing.
Run from parent directory with command: python3 -m unittest dumboclient filemodehelper documentmodel/test_references.py