File size: 3,260 Bytes
4cf88e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import docx

from src.domain.container import Container
from src.domain.paragraph import Paragraph
from src.domain.styles import Styles
import shutil
import os


class Doc:

    def __init__(self, path='', id_=None):

        self.xdoc = docx.Document(path)
        self.title = path.split('/')[-1]
        self.name = self.title.split('.')[0]
        self.id_ = id(self)
        self.path = path
        paragraphs = [Paragraph(xp, self.id_, i) for (i, xp) in enumerate(self.xdoc.paragraphs)]
        self.container = Container(paragraphs, father=self)
        self.styles = Styles(self.xdoc.styles)
        self.tasks = [c.get_fulltask(self.container.one_liner) for c in self.container.containers if c.task]

    def copy(self, new_doc_path):
        shutil.copyfile(self.path, new_doc_path)
        new_doc = Doc(new_doc_path)
        new_doc.save_as_docx(new_doc_path)
        return new_doc

    def clear(self):
        os.remove(self.path)

    def apply_template(self, template, add_front_pages=True):
        if add_front_pages:
            self.add_front_pages_from(template)
        log = self.styles.apply_from(template.styles)
        self.save_as_docx()
        return log

    def copy_one_style(self, src_style_name: str, dest_style_name: str, template):
        style_dest = template.styles.get_style_from_name(dest_style_name)
        src_style = self.styles.get_style_from_name(src_style_name)
        log = self.styles.copy_one_style(src_style, style_dest)
        return log

    def get_different_styles_with_template(self, template):
        different_styles = self.styles.get_different_styles(template.styles)
        return different_styles

    def save_as_docx(self, path: str = ''):
        path = path if path else self.path
        self.path = path
        self.xdoc.save(path)

    def add_front_pages_from(self, src_doc):
        src_paragraphs = [p for p in src_doc.xdoc.paragraphs]
        src_paragraphs.reverse()
        for p in src_paragraphs:
            self.xdoc.paragraphs[0].insert_paragraph_before(text=p.text, style=p.style)
        paragraphs = [Paragraph(xp, self.id_, i) for (i, xp) in enumerate(self.xdoc.paragraphs)]
        self.container = Container(paragraphs, father=self)

    def get_blocks(self):

        def from_list_to_str(index_list):
            index_str = str(index_list[0])
            for el in index_list[1:]:
                index_str += '.' + str(el)
            return index_str

        blocks = self.container.blocks
        for block in blocks:
            block.doc = self.title
            if block.level == 0:
                blocks.remove(block)
            block.index = from_list_to_str(block.index)
        return blocks


    @property
    def structure(self):

        return self.container.structure

    def replace_tasks(self, resolutions: [str]):
        if len(resolutions) == len(self.tasks):  # exception to be handled
            p_tasks = [p for p in self.get_paragraphs() if p.type == 'task']
            for p, r in zip(p_tasks, resolutions):
                p.set_text(r)
        else:
            print(f"résolutions : {len(resolutions)} != {len(self.tasks)} tasks")
        return self

    def get_paragraphs(self):
        return self.container.all_paragraphs