From 168d8bcffb2d1986b59a5b099ffc8ccf9874b9ee Mon Sep 17 00:00:00 2001 From: anstarword Date: Sun, 10 Apr 2016 14:42:10 +0800 Subject: [PATCH 1/8] add save_all(path) def save_all(workbook, path=None) for saving all references in xmind file except Revisions --- xmind/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/xmind/__init__.py b/xmind/__init__.py index 996de85..0f3392a 100644 --- a/xmind/__init__.py +++ b/xmind/__init__.py @@ -35,6 +35,13 @@ def save(workbook, path=None): saver = WorkbookSaver(workbook) saver.save(path) +def save_all(workbook, path=None): + """ Save workbook to given path with all reference in xmind zip file except Revisions. + If path not given, then will save to path that set to workbook. + + """ + saver = WorkbookSaver(workbook) + saver.save_all(path) def main(): pass From 15f1a534b954c03a9a165b46b6cb08232d370c5f Mon Sep 17 00:00:00 2001 From: anstarword Date: Sun, 10 Apr 2016 14:44:12 +0800 Subject: [PATCH 2/8] add save_all(path) to saver def save_all(workbook, path=None) for saving all the reference for xmind file except Revisions --- xmind/core/saver.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/xmind/core/saver.py b/xmind/core/saver.py index 2546ab2..d3fb0a8 100644 --- a/xmind/core/saver.py +++ b/xmind/core/saver.py @@ -9,6 +9,7 @@ :license: """ +import os __author__ = "aiqi@xmind.net " @@ -57,6 +58,66 @@ def save(self, path=None): f=utils.compress(path) f.write(content, const.CONTENT_XML) + def _get_reference(self, old_path): + reference_dir = utils.temp_dir() + + old_file_name, old_ext = utils.split_ext(old_path) + + if old_ext != const.XMIND_EXT: + raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT) + + myzip = utils.extract(old_path) + with myzip as input_stream: + for name in input_stream.namelist(): + print(name) + if name == const.CONTENT_XML: + continue + if const.REVISIONS_DIR in name: + continue + + target_file = utils.get_abs_path(utils.join_path(reference_dir,name)) + if not os.path.exists(os.path.dirname(target_file)): + os.makedirs(os.path.dirname(target_file)) + f_handle=open(target_file,"xb") + f_handle.write(myzip.read(name)) + f_handle.close() + + return reference_dir + + def save_all(self, path=None): + """ + Save the workbook to the given path with all references except Revisions for saving space. + If the path is not given, then will save to the path set in workbook. + """ + old_path = self._workbook.get_path() + path = path or self._workbook.get_path() + + if not path: + raise Exception("Please specify a filename for the XMind file") + + path = utils.get_abs_path(path) + old_path = utils.get_abs_path(old_path) + + file_name, ext = utils.split_ext(path) + old_file_name, old_ext = utils.split_ext(old_path) + + if ext != const.XMIND_EXT: + raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT) + + if old_ext != const.XMIND_EXT: + raise Exception("XMind filenames require a '%s' extension" % const.XMIND_EXT) + + content = self._get_content() + reference_dir = self._get_reference(old_path) + + f = utils.compress(path) + f.write(content, const.CONTENT_XML) + + len = reference_dir.__len__() + for dirpath, dirnames, filenames in os.walk(reference_dir): + for filename in filenames: + f.write(utils.join_path(dirpath, filename), utils.join_path(dirpath[len+1:]+os.sep, filename)) + f.close() def main(): pass From 9175b953f8ef92f25d74246bcc2538ff3bafa4f7 Mon Sep 17 00:00:00 2001 From: yan-an Date: Mon, 11 Apr 2016 16:46:22 +0800 Subject: [PATCH 3/8] Add AxelVoitier's pull request for support label --- xmind/core/const.py | 2 ++ xmind/core/labels.py | 42 ++++++++++++++++++++++++++++++++++++++++++ xmind/core/topic.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 xmind/core/labels.py diff --git a/xmind/core/const.py b/xmind/core/const.py index a9b27d1..c13463a 100644 --- a/xmind/core/const.py +++ b/xmind/core/const.py @@ -38,6 +38,8 @@ TAG_POSITION = "position" TAG_CHILDREN = "children" TAG_NOTES = "notes" +TAG_LABELS = "labels" +TAG_LABEL = "label" TAG_RELATIONSHIP = "relationship" TAG_RELATIONSHIPS = "relationships" TAG_MARKERREFS = "marker-refs" diff --git a/xmind/core/labels.py b/xmind/core/labels.py new file mode 100644 index 0000000..a7da803 --- /dev/null +++ b/xmind/core/labels.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- + +""" + xmind.core.labels + ~~~~~~~~~~~~~~~~ + :copyright: + :license: +""" + +__author__ = "axel.voitier@gmail.com " + +from . import const + +from .mixin import TopicMixinElement + + +class LabelsElement(TopicMixinElement): + TAG_NAME = const.TAG_LABELS + + def __init__(self, node=None, ownerTopic=None): + super(LabelsElement, self).__init__(node, ownerTopic) + + +class LabelElement(TopicMixinElement): + TAG_NAME = const.TAG_LABEL + + def __init__(self, node=None, ownerTopic=None): + super(LabelElement, self).__init__(node, ownerTopic) + + def getLabel(self): + return self.getTextContent() + + def setLabel(self, label_text): + self.setTextContent(label_text) + + +def main(): + pass + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/xmind/core/topic.py b/xmind/core/topic.py index 9b011cd..28e69fb 100644 --- a/xmind/core/topic.py +++ b/xmind/core/topic.py @@ -21,6 +21,7 @@ from .markerref import MarkerRefElement from .markerref import MarkerRefsElement from .markerref import MarkerId +from .labels import LabelsElement, LabelElement from .. import utils @@ -53,6 +54,9 @@ def _get_title(self): def _get_markerrefs(self): return self.getFirstChildNodeByTagName(const.TAG_MARKERREFS) + def _get_labels(self): + return self.getFirstChildNodeByTagName(const.TAG_LABELS) + def _get_position(self): return self.getFirstChildNodeByTagName(const.TAG_POSITION) @@ -135,6 +139,31 @@ def addMarker(self, markerId): tmp.appendChild(mre) return mre + def getLabels(self): + labels_ = self._get_labels() + if not labels_: + return None + tmp = LabelsElement(labels_, self.getOwnerWorkbook()) + labels = tmp.getChildNodesByTagName(const.TAG_LABEL) + label_list = [] + if labels: + for i in labels: + label_list.append(LabelElement(i, self.getOwnerWorkbook())) + return label_list + + def addLabel(self, label_text): + labels_ = self._get_labels() + if not labels_: + tmp = LabelsElement(None, self.getOwnerWorkbook()) + self.appendChild(tmp) + else: + tmp = LabelsElement(labels_, self.getOwnerWorkbook()) + + label = LabelElement(None, self.getOwnerWorkbook()) + label.setLabel(label_text) + tmp.appendChild(label) + return label + def setFolded(self): self.setAttribute(const.ATTR_BRANCH, const.VAL_FOLDED) From de83393eedcdfbdcd485c9b3afc152541e7be5c3 Mon Sep 17 00:00:00 2001 From: yan-an Date: Mon, 11 Apr 2016 16:50:30 +0800 Subject: [PATCH 4/8] remove print in saver --- xmind/core/saver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmind/core/saver.py b/xmind/core/saver.py index d3fb0a8..2ab747c 100644 --- a/xmind/core/saver.py +++ b/xmind/core/saver.py @@ -69,7 +69,7 @@ def _get_reference(self, old_path): myzip = utils.extract(old_path) with myzip as input_stream: for name in input_stream.namelist(): - print(name) + if name == const.CONTENT_XML: continue if const.REVISIONS_DIR in name: From dcc6755ab42e6e2990ab383fe12e72bd5c76376e Mon Sep 17 00:00:00 2001 From: yan-an Date: Mon, 11 Apr 2016 16:51:46 +0800 Subject: [PATCH 5/8] update text.data = data.decode("utf8") to text.data = data for python3 --- xmind/core/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xmind/core/__init__.py b/xmind/core/__init__.py index 80d689e..fc8e7c6 100644 --- a/xmind/core/__init__.py +++ b/xmind/core/__init__.py @@ -308,7 +308,8 @@ def setTextContent(self, data): self._node.removeChild(node) text = DOM.Text() - text.data = data.decode("utf8") + #text.data = data.decode("utf8") + text.data = data self._node.appendChild(text) From 03680c0b5010718bd7daff153b603a1ceedc85e2 Mon Sep 17 00:00:00 2001 From: anstarword Date: Tue, 12 Apr 2016 09:37:25 +0800 Subject: [PATCH 6/8] Revert "update text.data = data.decode("utf8") to text.data = data for python3" This reverts commit dcc6755ab42e6e2990ab383fe12e72bd5c76376e. --- xmind/core/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/xmind/core/__init__.py b/xmind/core/__init__.py index fc8e7c6..80d689e 100644 --- a/xmind/core/__init__.py +++ b/xmind/core/__init__.py @@ -308,8 +308,7 @@ def setTextContent(self, data): self._node.removeChild(node) text = DOM.Text() - #text.data = data.decode("utf8") - text.data = data + text.data = data.decode("utf8") self._node.appendChild(text) From 1e739cf7ba3223ebe2efea58c1285e185e345075 Mon Sep 17 00:00:00 2001 From: anstarword Date: Tue, 12 Apr 2016 09:37:33 +0800 Subject: [PATCH 7/8] Revert "remove print in saver" This reverts commit de83393eedcdfbdcd485c9b3afc152541e7be5c3. --- xmind/core/saver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmind/core/saver.py b/xmind/core/saver.py index 2ab747c..d3fb0a8 100644 --- a/xmind/core/saver.py +++ b/xmind/core/saver.py @@ -69,7 +69,7 @@ def _get_reference(self, old_path): myzip = utils.extract(old_path) with myzip as input_stream: for name in input_stream.namelist(): - + print(name) if name == const.CONTENT_XML: continue if const.REVISIONS_DIR in name: From 322a667c106471577ff4a5b0c7447c58d580d4bb Mon Sep 17 00:00:00 2001 From: anstarword Date: Tue, 12 Apr 2016 09:37:47 +0800 Subject: [PATCH 8/8] Revert "Add AxelVoitier's pull request for support label" This reverts commit 9175b953f8ef92f25d74246bcc2538ff3bafa4f7. --- xmind/core/const.py | 2 -- xmind/core/labels.py | 42 ------------------------------------------ xmind/core/topic.py | 29 ----------------------------- 3 files changed, 73 deletions(-) delete mode 100644 xmind/core/labels.py diff --git a/xmind/core/const.py b/xmind/core/const.py index c13463a..a9b27d1 100644 --- a/xmind/core/const.py +++ b/xmind/core/const.py @@ -38,8 +38,6 @@ TAG_POSITION = "position" TAG_CHILDREN = "children" TAG_NOTES = "notes" -TAG_LABELS = "labels" -TAG_LABEL = "label" TAG_RELATIONSHIP = "relationship" TAG_RELATIONSHIPS = "relationships" TAG_MARKERREFS = "marker-refs" diff --git a/xmind/core/labels.py b/xmind/core/labels.py deleted file mode 100644 index a7da803..0000000 --- a/xmind/core/labels.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python -#-*- coding: utf-8 -*- - -""" - xmind.core.labels - ~~~~~~~~~~~~~~~~ - :copyright: - :license: -""" - -__author__ = "axel.voitier@gmail.com " - -from . import const - -from .mixin import TopicMixinElement - - -class LabelsElement(TopicMixinElement): - TAG_NAME = const.TAG_LABELS - - def __init__(self, node=None, ownerTopic=None): - super(LabelsElement, self).__init__(node, ownerTopic) - - -class LabelElement(TopicMixinElement): - TAG_NAME = const.TAG_LABEL - - def __init__(self, node=None, ownerTopic=None): - super(LabelElement, self).__init__(node, ownerTopic) - - def getLabel(self): - return self.getTextContent() - - def setLabel(self, label_text): - self.setTextContent(label_text) - - -def main(): - pass - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/xmind/core/topic.py b/xmind/core/topic.py index 28e69fb..9b011cd 100644 --- a/xmind/core/topic.py +++ b/xmind/core/topic.py @@ -21,7 +21,6 @@ from .markerref import MarkerRefElement from .markerref import MarkerRefsElement from .markerref import MarkerId -from .labels import LabelsElement, LabelElement from .. import utils @@ -54,9 +53,6 @@ def _get_title(self): def _get_markerrefs(self): return self.getFirstChildNodeByTagName(const.TAG_MARKERREFS) - def _get_labels(self): - return self.getFirstChildNodeByTagName(const.TAG_LABELS) - def _get_position(self): return self.getFirstChildNodeByTagName(const.TAG_POSITION) @@ -139,31 +135,6 @@ def addMarker(self, markerId): tmp.appendChild(mre) return mre - def getLabels(self): - labels_ = self._get_labels() - if not labels_: - return None - tmp = LabelsElement(labels_, self.getOwnerWorkbook()) - labels = tmp.getChildNodesByTagName(const.TAG_LABEL) - label_list = [] - if labels: - for i in labels: - label_list.append(LabelElement(i, self.getOwnerWorkbook())) - return label_list - - def addLabel(self, label_text): - labels_ = self._get_labels() - if not labels_: - tmp = LabelsElement(None, self.getOwnerWorkbook()) - self.appendChild(tmp) - else: - tmp = LabelsElement(labels_, self.getOwnerWorkbook()) - - label = LabelElement(None, self.getOwnerWorkbook()) - label.setLabel(label_text) - tmp.appendChild(label) - return label - def setFolded(self): self.setAttribute(const.ATTR_BRANCH, const.VAL_FOLDED)