File indexing completed on 2024-06-16 04:17:41

0001 """
0002 SPDX-FileCopyrightText: 2017 Eliakin Costa <eliakim170@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-2.0-or-later
0005 """
0006 from .document_scripter import document
0007 
0008 
0009 class DocumentController(object):
0010 
0011     def __init__(self):
0012         self._activeDocument = None
0013 
0014     @property
0015     def activeDocument(self):
0016         return self._activeDocument
0017 
0018     def openDocument(self, filePath):
0019         if filePath:
0020             newDocument = document.Document(filePath)
0021             newDocument.open()
0022             self._activeDocument = newDocument
0023             return newDocument
0024 
0025     def saveDocument(self, data, filePath, save_as=False):
0026         """
0027         data - data to be written
0028         filePath - file path to write data to
0029         save_as = boolean, is this call made from save_as functionality. If so, do not compare data
0030         against existing document before save.
0031         """
0032 
0033         if save_as or not self._activeDocument:
0034             self._activeDocument = document.Document(filePath)
0035 
0036         text = str(data)
0037         if save_as or not self._activeDocument.compare(text):
0038             # compare is not evaluated if save_as is True
0039             self._activeDocument.data = text
0040             self._activeDocument.save()
0041 
0042         return self._activeDocument
0043 
0044     def clearActiveDocument(self):
0045         self._activeDocument = None