timApp.documentmodel package

Submodules

timApp.documentmodel.attributeparser module

class timApp.documentmodel.attributeparser.AttributeParser(string='')[source]

Bases: object

static attr_list_end_char()[source]
static attr_list_start_char()[source]
current_char()[source]
eat_whitespace()[source]
find_attr_list_start_char()[source]
get_attributes()[source]
get_char()[source]
has_chars()[source]
set_str(string)[source]
try_parse_attr_list_end_char()[source]
try_parse_class()[source]
try_parse_hash()[source]
try_parse_helper(char)[source]
try_parse_keyvaluepair()[source]
exception timApp.documentmodel.attributeparser.AttributeParserException[source]

Bases: Exception

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

clear()[source]
clear_refs()[source]
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]
delete_from_source()[source]
get_clipfilename() → str[source]
get_metafilename() → str[source]
get_parreffilename() → str[source]
get_reffilename() → str[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]
read(as_ref: typing.Union[bool, NoneType] = False, force_parrefs: typing.Union[bool, NoneType] = False) → typing.Union[typing.List[typing.Dict[str, str]], NoneType][source]
read_metadata() → typing.Dict[str, str][source]
write(pars: typing.List[typing.Dict[str, typing.Generic]])[source]
write_metadata(**kwargs)[source]
write_refs(pars: typing.List[documentmodel.docparagraph.DocParagraph], area_name: typing.Union[str, NoneType])[source]
Clipboard.clear_all()[source]
Clipboard.get(user_id: int)[source]
Clipboard.get_path()[source]

timApp.documentmodel.docparagraph module

class timApp.documentmodel.docparagraph.DocParagraph(doc, files_root: typing.Union[str, NoneType] = None)[source]

Bases: object

add_class(class_name)[source]
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'
dict() → typing.Dict[KT, VT][source]
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_attrs_str() → str[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_base_path() → str[source]
get_class_str() → str[source]
classmethod get_default_files_root()[source]
get_doc_id() → int[source]
get_exported_markdown() → str[source]
get_hash() → str[source]
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
get_id() → str[source]
classmethod get_latest(doc, par_id: str, files_root: typing.Union[str, NoneType] = None) → timApp.documentmodel.docparagraph.DocParagraph[source]
get_markdown() → str[source]
get_original() → timApp.documentmodel.docparagraph.DocParagraph[source]
get_path() → str[source]
get_properties(base_props: typing.Union[typing.Dict[KT, VT], NoneType] = None) → typing.Dict[KT, VT][source]
get_rd() → typing.Union[int, NoneType][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]
get_title() → typing.Union[str, NoneType][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).

has_class(class_name)[source]
has_headers() → bool[source]
html_dict() → typing.Dict[KT, VT][source]
is_area_reference() → bool[source]
is_different_from(par: timApp.documentmodel.docparagraph.DocParagraph) → bool[source]
is_dynamic() → bool[source]
is_multi_block() → bool[source]
is_par_reference() → bool[source]
is_plugin() → bool[source]
is_question() → bool[source]
is_reference() → bool[source]
is_setting() → bool[source]
is_translation() → bool[source]
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.

sanitize_html()[source]
set_attr(attr_name: str, attr_val: typing.Generic, dereference: bool = False)[source]
set_id(par_id: str)[source]
set_latest()[source]
set_original(orig: timApp.documentmodel.docparagraph.DocParagraph)[source]

timApp.documentmodel.docparagraphencoder module

class timApp.documentmodel.docparagraphencoder.DocParagraphEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

default(o)[source]

timApp.documentmodel.docsettings module

class timApp.documentmodel.docsettings.DocSettings[source]

Bases: object

auto_number_headings() → bool[source]
Return type:bool
auto_number_headings_key = 'auto_number_headings'
css()[source]
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.
get_dict() → dict[source]
Return type:dict
get_macro_delimiter() → str[source]
Return type:str
get_macros() → dict[source]
Return type:dict
get_source_document()[source]
Return type:int|None
global_plugin_attrs() → dict[source]
Return type:dict
global_plugin_attrs_key = 'global_plugin_attrs'
heading_format() → dict[source]
Return type:dict
heading_format_key = 'heading_format'
classmethod is_valid_paragraph(par)[source]
macro_delimiter_key = 'macro_delimiter'
macros_key = 'macros'
static parse_values(par)[source]
sanitize_macros()[source]
set_source_document()[source]
Parameters:source_docid (int|None) –
show_task_summary(default=False) → bool[source]
show_task_summary_key = 'show_task_summary'
source_document_key = 'source_document'
to_paragraph()[source]
Return type:DocParagraph

timApp.documentmodel.document module

class timApp.documentmodel.document.CacheIterator(i)[source]

Bases: object

class timApp.documentmodel.document.DocParagraphIter(doc: timApp.documentmodel.document.Document)[source]

Bases: object

close()[source]
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]
static add_index_entry(index_table, current_headers, header)[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]
add_setting(key: str, value) → None[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.

clear_mem_cache()[source]
create(ignore_exists: bool = False)[source]
default_files_root = 'tim_files'
delete_paragraph(par_id: str)[source]

Removes a paragraph from the document. :param par_id: Paragraph id to remove.

delete_section(area_start, area_end)[source]
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.

exists() → bool[source]
export_markdown(export_hashes: bool = False) → str[source]
export_section(par_id_start: str, par_id_end: str, export_hashes=False) → str[source]
find_insert_index(i2, old_ids)[source]
get_changelog(max_entries: int = 100) → typing.List[dict][source]
get_closest_paragraph_title(par_id: typing.Union[str, NoneType])[source]
classmethod get_default_files_root()[source]
get_document_path() → str[source]
get_id_version() → typing.Tuple[int, int, int][source]
get_index() → typing.List[typing.Tuple][source]
get_last_modified()[source]
get_last_par()[source]
get_latest_version()[source]
get_named_section(section_name: str)[source]
classmethod get_next_free_id(files_root: typing.Union[str, NoneType] = None) → int[source]

Gets the next free document id. :return:

get_original_document()[source]
get_paragraph(par_id: str) → documentmodel.docparagraph.DocParagraph[source]
get_paragraph_by_task(task_id_name)[source]
get_paragraphs() → typing.List[documentmodel.docparagraph.DocParagraph][source]
get_pars_till(par)[source]
get_previous_par(par, get_last_if_no_prev=False)[source]
get_referenced_document_ids(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → typing.Set[int][source]
get_reflist_filename(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → str[source]
get_refs_dir(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → str[source]
get_section(par_id_start: str, par_id_end: str) → typing.List[documentmodel.docparagraph.DocParagraph][source]
get_settings() → documentmodel.docsettings.DocSettings[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.

get_version_path(ver: typing.Union[typing.Tuple[int, int], NoneType] = None) → str[source]
getlogfilename() → str[source]
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]
insert_temporary_pars(pars, context_par)[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:

set_settings(settings: dict)[source]
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.
update_section()[source]

Updates a section of the document.

Parameters:
  • text – The text of the section.
  • par_id_first – The id of the paragraph that denotes the start of the section.
  • par_id_last – The id of the paragraph that denotes the end of the section.
classmethod version_exists()[source]

Checks if a document version exists. :param doc_id: Document id. :param doc_ver: Document version. :return: Boolean.

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.document.get_index_from_html_list(html_table) → typing.List[typing.Tuple][source]
Return type:List

timApp.documentmodel.documentparser module

class timApp.documentmodel.documentparser.DocReader(lines, i=0)[source]

Bases: object

Parameters:
  • lines (list[str]) –
  • i (int) –
get_line_and_advance()[source]
has_more_lines()[source]
peek_line()[source]
Return type:str
Returns:
class timApp.documentmodel.documentparser.DocumentParser(doc_text='')[source]

Bases: object

Splits documents into paragraphs.

add_missing_attributes(hash_func=<function hashfunc>, id_func=<function random_id>)[source]
eat_whitespace(doc)[source]
Return type:NoneType
extract_attrs(result, tokens)[source]
get_blocks(options: typing.Union[documentmodel.documentparseroptions.DocumentParserOptions, NoneType] = None)[source]
is_beginning_of_code_block(doc)[source]
is_beginning_of_header_block(doc)[source]
is_empty_line(doc)[source]
parse_normal_block(doc)[source]
try_parse_code_block(doc)[source]
Return type:dict
try_parse_header_block(doc)[source]
Return type:dict
Parameters:doc (DocReader) –
Returns:
validate_structure(id_validator_func=<function is_valid_id>, is_whole_document=True)[source]
exception timApp.documentmodel.documentparser.SplitterException[source]

Bases: Exception

exception timApp.documentmodel.documentparser.ValidationException[source]

Bases: Exception

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 single_paragraph() → timApp.documentmodel.documentparseroptions.DocumentParserOptions[source]
static whole_document() → timApp.documentmodel.documentparseroptions.DocumentParserOptions[source]

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

add_paragraph_obj()[source]
Parameters:p (DocParagraph) –
Return type:DocParagraph
add_ref_paragraph()[source]
cache_index()[source]
create(ignore_exists: bool = False)[source]
Parameters:ignore_exists (bool) –
delete_paragraph(par_id: str)[source]
Parameters:par_id (str) –
delete_section(area_start: str, area_end: str)[source]
Parameters:
  • area_start (str) –
  • area_end (str) –
exists() → bool[source]
Return type:bool
static get_diff(d1, d2)[source]
get_paragraph()[source]
Parameters:par_id (str) –
Return type:DocParagraph
get_settings()[source]
Return type:DocSettings
get_version()[source]
Return type:tuple(int,int)
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

insert_paragraph_obj()[source]
Parameters:
  • p (DocParagraph) –
  • insert_before_id (str|None) –
Return type:

DocParagraph

modify_paragraph()[source]
Parameters:
  • new_attrs (dict|None) –
  • new_properties (dict|None) –
  • par_id (str) –
  • new_text (str) –
Return type:

DocParagraph

modify_paragraph_obj()[source]
Parameters:
  • p (DocParagraph) –
  • par_id (str) –
Return type:

DocParagraph

classmethod remove()[source]
Parameters:
  • doc_id (int) –
  • files_root (str|None) –
update(text: str, original: str, strict_validation=True)[source]
Parameters:
  • text (str) –
  • original (str) –
update_section()[source]
Parameters:
  • text (str) –
  • par_id_first (str) –
  • par_id_last (str) –
Return type:

tuple(str,str)

timApp.documentmodel.documentwriter module

Defines the DocumentWriter class.

class timApp.documentmodel.documentwriter.DocumentWriter(pars, export_hashes=False, export_ids=True)[source]

Bases: object

Converts a sequence of document blocks to text.

attrs_to_str(attrs)[source]
get_text(options: typing.Union[documentmodel.documentparseroptions.DocumentParserOptions, NoneType] = None)[source]

Gets the full text for the document.

Returns:The full text of the document.

timApp.documentmodel.exceptions module

exception timApp.documentmodel.exceptions.DocExistsError(doc_id)[source]

Bases: Exception

timApp.documentmodel.randutils module

timApp.documentmodel.randutils.hashfunc(text, attrs=None)[source]
timApp.documentmodel.randutils.idchecksum(randid)[source]
timApp.documentmodel.randutils.is_valid_id(randid)[source]
timApp.documentmodel.randutils.random_id()[source]
timApp.documentmodel.randutils.random_jsonpar(par_id)[source]
timApp.documentmodel.randutils.random_paragraph()[source]
timApp.documentmodel.randutils.random_sentence()[source]
timApp.documentmodel.randutils.random_sentences()[source]
timApp.documentmodel.randutils.random_word(min_len=2, max_len=12)[source]

timApp.documentmodel.test_attributeparser module

class timApp.documentmodel.test_attributeparser.AttributeParserTest(methodName='runTest')[source]

Bases: unittest.case.TestCase

check_invalid(string)[source]
check_valid(string, expected, expected_index=0)[source]
test_broken()[source]
test_keyvalues()[source]
test_random()[source]
test_whitespace()[source]

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

class timApp.documentmodel.test_clipboard.ClipboardTest(methodName='runTest')[source]

Bases: timdbtest.TimDbTest

setUp()[source]
test_copy()[source]
test_empty()[source]
test_paste()[source]
test_persistence()[source]
test_readwrite()[source]
timApp.documentmodel.test_clipboard.test_paste_ref(self)[source]

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

class timApp.documentmodel.test_document.DocumentTest(methodName='runTest')[source]

Bases: timdbtest.TimDbTest

add_pars(d, num_docs)[source]
init_testdoc(doc_id=None)[source]
test_addparagraph()[source]
test_delparagraph()[source]
test_document_create()[source]
test_document_remove()[source]
test_get_html()[source]
test_import()[source]
test_insertparagraph()[source]
test_iterator()[source]
test_macros()[source]
test_modify()[source]
test_update()[source]
test_update_section()[source]

timApp.documentmodel.test_documentparser module

class timApp.documentmodel.test_documentparser.DocumentParserTest(methodName='runTest')[source]

Bases: unittest.case.TestCase

test_parsing()[source]
test_validation()[source]

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

class timApp.documentmodel.test_references.RefTest(methodName='runTest')[source]

Bases: timdbtest.TimDbTest

assert_dict_issubset(a, b)[source]
dict_issubset(a, b)[source]
dict_merge(a, b)[source]
doc_create(db, doc_name, doc_group)[source]
init_testdb()[source]
test_circular()[source]
test_editparagraph_cite()[source]
test_editparagraph_citearea()[source]
test_editparagraph_translate()[source]
test_editparagraph_translatearea()[source]
test_simpleref()[source]
test_transitive()[source]
test_translation()[source]

Module contents