Gnumed.timelinelib.plugin.plugins.exporters.timelineexporter

Package Gnumed :: Package timelinelib :: Package plugin :: Package plugins :: Package exporters :: Module timelineexporter |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.plugin.plugins.exporters.timelineexporter

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  from __future__ import with_statement
     20
     21
     22  import os.path
     23
     24  import wx
     25
     26  from timelinelib.plugin.factory import EXPORTER
     27  from timelinelib.plugin.pluginbase import PluginBase
     28  from timelinelib.wxgui.dialogs.export.controller import CSV_FILE
     29  from timelinelib.wxgui.dialogs.export.view import ExportDialog
     30  from timelinelib.wxgui.utils import _ask_question
     31  from timelinelib.wxgui.utils import WildcardHelper
     32
     33
     34 -class TimelineExporter(PluginBase):
     35
     36 -    def service(self):
     37          return EXPORTER
     38
     39 -    def display_name(self):
     40          return _("Export Timeline to File...")
     41
     42 -    def wxid(self):
     43          from timelinelib.wxgui.frames.mainframe.guicreator import ID_EXPORT
     44          return ID_EXPORT
     45
     46 -    def run(self, main_frame):
     47          self.timeline = main_frame.timeline
     48          dlg = ExportDialog(main_frame)
     49          if dlg.ShowModal() == wx.ID_OK:
     50              self.export_timeline(dlg, main_frame)
     51          dlg.Destroy()
     52
     53 -    def export_timeline(self, dlg, main_frame):
     54          path, _ = get_path(main_frame)
     55          if path is not None and overwrite_existing_path(main_frame, path):
     56              if dlg.GetExportType() == CSV_FILE:
     57                  CsvExporter(self.timeline, path, dlg).export()
     58
     59
     60 -class CsvExporter(object):
     61
     62 -    def __init__(self, timeline, path, dlg):
     63          self.path = path
     64          self.timeline = timeline
     65          self.text_encoding = dlg.GetTextEncoding()
     66          self.encoding_error_strategy = dlg.GetTextEncodingErrorStrategy()
     67          self.export_events = dlg.GetExportEvents()
     68          self.export_categories = dlg.GetExportCategories()
     69          self.event_fields = dlg.GetEventFields()
     70          self.category_fields = dlg.GetCategoryFields()
     71
     72 -    def export(self):
     73          with open(self.path, "w") as f:
     74              self._write_events(f, self.event_fields)
     75              self._write_categories(f, self.category_fields)
     76
     77 -    def _write_events(self, f, event_fields):
     78          if self.export_events:
     79              self._write_label(f, _("Events"))
     80              self._write_heading(f, event_fields)
     81              self._write_events_fields(f, event_fields)
     82
     83 -    def _write_categories(self, f, category_fields):
     84          if self.export_categories:
     85              self._write_label(f, _("Categories"))
     86              self._write_heading(f, category_fields)
     87              self._write_categories_fields(f, category_fields)
     88
     89 -    def _write_label(self, f, text):
     90          self._write_encoded_text(f, text)
     91          f.write("\n")
     92
     93 -    def _write_heading(self, f, fields):
     94          for field in fields:
     95              self._write_encoded_text(f, field)
     96          f.write("\n")
     97
     98 -    def _write_events_fields(self, f, event_fields):
     99          for event in self.timeline.get_all_events():
    100              self._write_event(f, event, event_fields)
    101          f.write("\n")
    102
    103 -    def _write_categories_fields(self, f, category_fields):
    104          for category in self.timeline.get_categories():
    105              self._write_category(f, category, category_fields)
    106
    107 -    def _write_event(self, f, event, event_fields):
    108          if _("Text") in event_fields:
    109              self._write_encoded_text(f, event.get_text())
    110          if _("Description") in event_fields:
    111              self._write_encoded_text(f, self._get_event_description(event))
    112          if _("Start") in event_fields:
    113              self._write_time_value(f, event.get_time_period().start_time)
    114          if _("End") in event_fields:
    115              self._write_time_value(f, event.get_time_period().end_time)
    116          if _("Category") in event_fields:
    117              self._write_encoded_text(f, self._get_event_category(event))
    118          if _("Fuzzy") in event_fields:
    119              self._write_boolean_value(f, event.get_fuzzy())
    120          if _("Locked") in event_fields:
    121              self._write_boolean_value(f, event.get_locked())
    122          if _("Ends Today") in event_fields:
    123              self._write_boolean_value(f, event.get_ends_today())
    124          if _("Hyperlink") in event_fields:
    125              f.write("%s;" % event.get_hyperlink())
    126          if _("Progress") in event_fields:
    127              f.write("%s;" % event.get_progress())
    128          if _("Progress Color") in event_fields:
    129              self._write_color_value(f, event.get_progress_color())
    130          if _("Done Color") in event_fields:
    131              self._write_color_value(f, event.get_done_color())
    132          if _("Alert") in event_fields:
    133              f.write("%s;" % self._get_alert_string(event.get_alert()))
    134          if _("Is Container") in event_fields:
    135              self._write_boolean_value(f, event.is_container())
    136          if _("Is Subevent") in event_fields:
    137              self._write_boolean_value(f, event.is_subevent())
    138          f.write("\n")
    139
    140 -    def _write_category(self, f, category, category_fields):
    141          if _("Name") in category_fields:
    142              self._write_encoded_text(f, category.get_name())
    143          if _("Color") in category_fields:
    144              self._write_color_value(f, category.get_color())
    145          if _("Progress Color") in category_fields:
    146              self._write_color_value(f, category.get_progress_color())
    147          if _("Done Color") in category_fields:
    148              self._write_color_value(f, category.get_done_color())
    149          if _("Parent") in category_fields:
    150              self._write_encoded_text(f, self._get_parent(category))
    151          f.write("\n")
    152
    153 -    def _get_event_description(self, event):
    154          if event.get_description() is not None:
    155              return event.get_description()
    156          else:
    157              return ""
    158
    159 -    def _get_event_category(self, event):
    160          if event.get_category() is not None:
    161              return event.get_category().get_name()
    162          else:
    163              return ""
    164
    165 -    def _get_parent(self, category):
    166          if category._get_parent():
    167              return category._get_parent().get_name()
    168
    169 -    def _get_category_parent(self, cat):
    170          if cat._get_parent() is not None:
    171              return self._encode_text(cat._get_parent().get_name())
    172          else:
    173              return ""
    174
    175 -    def _get_time_string(self, time_value):
    176          return self.timeline.get_time_type().time_string(time_value)
    177
    178 -    def _get_alert_string(self, alert):
    179          if alert:
    180              time, text = alert
    181              return "%s %s" % (self._get_time_string(time), self._encode_text(text))
    182          else:
    183              return ""
    184
    185 -    def _write_encoded_text(self, f, text):
    186          if text is not None:
    187              text = text.replace('"', '""')
    188          f.write("\"%s\";" % self._encode_text(text))
    189
    190 -    def _encode_text(self, text):
    191          if text is not None:
    192              return text.encode(self.text_encoding, self.encoding_error_strategy)
    193          else:
    194              return text
    195
    196 -    def _write_time_value(self, f, time_value):
    197          f.write("%s;" % self.timeline.get_time_type().time_string(time_value))
    198
    199 -    def _write_color_value(self, f, color):
    200          f.write("(%d, %d, %d);" % color)
    201
    202 -    def _write_boolean_value(self, f, value):
    203          f.write("%s;" % value)
    204
    205
    206 -def get_path(main_frame):
    207      image_type = None
    208      path = None
    209      file_info = _("export files")
    210      file_types = [("csv", "")]
    211      images_wildcard_helper = WildcardHelper(file_info, file_types)
    212      wildcard = images_wildcard_helper.wildcard_string()
    213      dialog = wx.FileDialog(main_frame, message=_("Export"), wildcard=wildcard, style=wx.FD_SAVE)
    214      if dialog.ShowModal() == wx.ID_OK:
    215          path = images_wildcard_helper.get_path(dialog)
    216          image_type = images_wildcard_helper.get_extension_data(path)
    217      dialog.Destroy()
    218      return path, image_type
    219
    220
    221 -def overwrite_existing_path(main_frame, path):
    222      if os.path.exists(path):
    223          overwrite_question = _("File '%s' exists. Overwrite?") % path
    224          return _ask_question(overwrite_question, main_frame) == wx.YES
    225      return True
    226