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  import os.path 
 20   
 21  import wx 
 22   
 23  from timelinelib.plugin.plugins.exporters import EXPORTER 
 24  from timelinelib.plugin.pluginbase import PluginBase 
 25  from timelinelib.wxgui.dialogs.export.controller import CSV_FILE 
 26  from timelinelib.wxgui.dialogs.export.view import ExportDialog 
 27  from timelinelib.wxgui.utils import _ask_question 
 28  from timelinelib.wxgui.utils import WildcardHelper 
 29   
 30   
31 -class TimelineExporter(PluginBase):
32
33 - def service(self):
34 return EXPORTER
35
36 - def display_name(self):
37 return _("Export Timeline to File...")
38
39 - def wxid(self):
42
43 - def run(self, main_frame):
44 self.timeline = main_frame.timeline 45 dlg = ExportDialog(main_frame) 46 if dlg.ShowModal() == wx.ID_OK: 47 self.export_timeline(dlg, main_frame) 48 dlg.Destroy()
49
50 - def export_timeline(self, dlg, main_frame):
51 path, _ = get_path(main_frame) 52 if path is not None and overwrite_existing_path(main_frame, path): 53 if dlg.GetExportType() == CSV_FILE: 54 CsvExporter(self.timeline, path, dlg).export()
55 56
57 -class CsvExporter(object):
58
59 - def __init__(self, timeline, path, dlg):
60 self.path = path 61 self.timeline = timeline 62 self.text_encoding = dlg.GetTextEncoding() 63 self.encoding_error_strategy = dlg.GetTextEncodingErrorStrategy() 64 self.export_events = dlg.GetExportEvents() 65 self.export_categories = dlg.GetExportCategories() 66 self.event_fields = dlg.GetEventFields() 67 self.category_fields = dlg.GetCategoryFields()
68
69 - def export(self):
70 with open(self.path, "wb") as f: 71 self._write_events(f, self.event_fields) 72 self._write_categories(f, self.category_fields)
73
74 - def _write_events(self, f, event_fields):
75 if self.export_events: 76 self._write_label(f, _("Events")) 77 self._write_heading(f, event_fields) 78 self._write_events_fields(f, event_fields)
79
80 - def _write_categories(self, f, category_fields):
81 if self.export_categories: 82 self._write_label(f, _("Categories")) 83 self._write_heading(f, category_fields) 84 self._write_categories_fields(f, category_fields)
85
86 - def _write_label(self, f, text):
87 self._write_encoded_text(f, text) 88 self._write(f, "\n")
89
90 - def _write_heading(self, f, fields):
91 for field in fields: 92 self._write_encoded_text(f, field) 93 self._write(f, "\n")
94
95 - def _write_events_fields(self, f, event_fields):
96 for event in self.timeline.get_all_events(): 97 self._write_event(f, event, event_fields) 98 self._write(f, "\n")
99
100 - def _write_categories_fields(self, f, category_fields):
101 for category in self.timeline.get_categories(): 102 self._write_category(f, category, category_fields)
103
104 - def _write_event(self, f, event, event_fields):
105 if _("Text") in event_fields: 106 self._write_encoded_text(f, event.get_text()) 107 if _("Description") in event_fields: 108 self._write_encoded_text(f, self._get_event_description(event)) 109 if _("Start") in event_fields: 110 self._write_time_value(f, event.get_time_period().start_time) 111 if _("End") in event_fields: 112 self._write_time_value(f, event.get_time_period().end_time) 113 if _("Category") in event_fields: 114 self._write_encoded_text(f, self._get_event_category(event)) 115 if _("Fuzzy") in event_fields: 116 self._write_boolean_value(f, event.get_fuzzy()) 117 if _("Locked") in event_fields: 118 self._write_boolean_value(f, event.get_locked()) 119 if _("Ends Today") in event_fields: 120 self._write_boolean_value(f, event.get_ends_today()) 121 if _("Hyperlink") in event_fields: 122 self._write(f, "%s;" % event.get_hyperlink()) 123 if _("Progress") in event_fields: 124 self._write(f, "%s;" % event.get_progress()) 125 if _("Progress Color") in event_fields: 126 self._write_color_value(f, event.get_progress_color()) 127 if _("Done Color") in event_fields: 128 self._write_color_value(f, event.get_done_color()) 129 if _("Alert") in event_fields: 130 self._write(f, "%s;" % self._get_alert_string(event.get_alert())) 131 if _("Is Container") in event_fields: 132 self._write_boolean_value(f, event.is_container()) 133 if _("Is Subevent") in event_fields: 134 self._write_boolean_value(f, event.is_subevent()) 135 self._write(f, "\n")
136
137 - def _write_category(self, f, category, category_fields):
138 if _("Name") in category_fields: 139 self._write_encoded_text(f, category.get_name()) 140 if _("Color") in category_fields: 141 self._write_color_value(f, category.get_color()) 142 if _("Progress Color") in category_fields: 143 self._write_color_value(f, category.get_progress_color()) 144 if _("Done Color") in category_fields: 145 self._write_color_value(f, category.get_done_color()) 146 if _("Parent") in category_fields: 147 self._write_encoded_text(f, self._get_parent(category)) 148 self._write(f, "\n")
149
150 - def _get_event_description(self, event):
151 if event.get_description() is not None: 152 return event.get_description() 153 else: 154 return ""
155
156 - def _get_event_category(self, event):
157 if event.get_category() is not None: 158 return event.get_category().get_name() 159 else: 160 return ""
161
162 - def _get_parent(self, category):
163 if category._get_parent(): 164 return category._get_parent().get_name()
165
166 - def _get_category_parent(self, cat):
167 if cat._get_parent() is not None: 168 return cat._get_parent().get_name() 169 else: 170 return ""
171
172 - def _get_time_string(self, time_value):
173 return self.timeline.get_time_type().time_string(time_value)
174
175 - def _get_alert_string(self, alert):
176 if alert: 177 time, text = alert 178 return "%s %s" % (self._get_time_string(time), text) 179 else: 180 return ""
181
182 - def _write_encoded_text(self, f, text):
183 if text is not None: 184 text = text.replace('"', '""') 185 self._write(f, "\"%s\";" % text)
186
187 - def _write_time_value(self, f, time_value):
188 self._write(f, "%s;" % self.timeline.get_time_type().time_string(time_value))
189
190 - def _write_color_value(self, f, color):
191 self._write(f, "(%d, %d, %d);" % color)
192
193 - def _write_boolean_value(self, f, value):
194 self._write(f, "%s;" % value)
195
196 - def _write(self, f, text):
197 f.write(text.encode(self.text_encoding, self.encoding_error_strategy))
198 199
200 -def get_path(main_frame):
201 image_type = None 202 path = None 203 file_info = _("export files") 204 file_types = [("csv", "")] 205 images_wildcard_helper = WildcardHelper(file_info, file_types) 206 wildcard = images_wildcard_helper.wildcard_string() 207 dialog = wx.FileDialog(main_frame, message=_("Export"), wildcard=wildcard, style=wx.FD_SAVE) 208 if dialog.ShowModal() == wx.ID_OK: 209 path = images_wildcard_helper.get_path(dialog) 210 image_type = images_wildcard_helper.get_extension_data(path) 211 dialog.Destroy() 212 return path, image_type
213 214
215 -def overwrite_existing_path(main_frame, path):
216 if os.path.exists(path): 217 overwrite_question = _("File '%s' exists. Overwrite?") % path 218 return _ask_question(overwrite_question, main_frame) == wx.YES 219 return True
220