Gnumed.timelinelib.db

Package Gnumed :: Package timelinelib :: Package db |
[frames] | no frames]

# Source Code for Package Gnumed.timelinelib.db

      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  import tempfile
     21
     22  from timelinelib.canvas.data.exceptions import TimelineIOError
     23  from timelinelib.canvas.data import Category
     24  from timelinelib.canvas.data import Event
     25  from timelinelib.canvas.data import TimePeriod
     26  from timelinelib.canvas.drawing.viewproperties import ViewProperties
     27
     28
     29 -def db_open(path, timetype=None):
     30      """
     31      Create timeline database that can read and write timeline data from and to
     32      persistent storage identified by path.
     33
     34      Throw a TimelineIOError exception if not able to read from the given path.
     35
     36      Valid values for path:
     37
     38        - special string ":tutorial:"
     39        - string with suffix .timeline
     40        - string with suffix .ics
     41        - string denoting a directory
     42      """
     43      if path == ":tutorial:":
     44          return open_gregorian_tutorial_timeline(path)
     45      elif path == ":numtutorial:":
     46          return open_numeric_tutorial_timeline(path)
     47      elif os.path.isdir(path):
     48          return open_directory_timeline(path)
     49      elif path.endswith(".timeline"):
     50          return db_open_timeline(path, timetype)
     51      elif path.endswith(".ics"):
     52          return db_open_ics(path)
     53      else:
     54          msg_template = (_("Unable to open timeline '%s'.") + "\n\n" +
     55                          _("Unknown format."))
     56          raise TimelineIOError(msg_template % path)
     57
     58
     59 -def open_gregorian_tutorial_timeline(path):
     60      from timelinelib.dataimport.tutorial import create_in_memory_gregorian_tutorial_db
     61      db = create_in_memory_gregorian_tutorial_db()
     62      db.path = path
     63      return db
     64
     65
     66 -def open_numeric_tutorial_timeline(path):
     67      from timelinelib.dataimport.tutorial import create_in_memory_numeric_tutorial_db
     68      db = create_in_memory_numeric_tutorial_db()
     69      db.path = path
     70      return db
     71
     72
     73 -def open_directory_timeline(path):
     74      from timelinelib.dataimport.dir import import_db_from_dir
     75      db = import_db_from_dir(path)
     76      db.path = path
     77      return db
     78
     79
     80 -def db_open_timeline(path, timetype=None):
     81      if (os.path.exists(path) and file_starts_with(path, "# Written by Timeline ")):
     82          raise TimelineIOError(_("You are trying to open an old file with a new version of timeline. Please install version 0.21.1 of timeline to convert it to the new format."))
     83      else:
     84          return db_open_newtype_timeline(path, timetype)
     85
     86
     87 -def db_open_newtype_timeline(path, timetype=None):
     88      if os.path.exists(path):
     89          from timelinelib.dataimport.timelinexml import import_db_from_timeline_xml
     90          db = import_db_from_timeline_xml(path)
     91          if dir_is_read_only(path):
     92              from timelinelib.wxgui.utils import display_warning_message
     93              db.set_readonly()
     94              display_warning_message(_("Since the directory of the Timeline file is not writable,\nthe timeline is opened in read-only mode"))
     95              return db
     96      else:
     97          from timelinelib.canvas.data.db import MemoryDB
     98          from timelinelib.calendar.gregorian.timetype import GregorianTimeType
     99          db = MemoryDB()
    100          if timetype is None:
    101              db.set_time_type(GregorianTimeType())
    102          else:
    103              db.set_time_type(timetype)
    104
    105      def save_callback():
    106          from timelinelib.dataexport.timelinexml import export_db_to_timeline_xml
    107          export_db_to_timeline_xml(db, path)
    108      db.register_save_callback(save_callback)
    109      db.set_should_lock(True)
    110      return db
    111
    112
    113 -def dir_is_read_only(path):
    114      try:
    115          testfile = tempfile.TemporaryFile(dir=os.path.dirname(os.path.abspath(path)))
    116      except:
    117          return True
    118      else:
    119          testfile.close()
    120          return False
    121
    122
    123 -def db_open_ics(path):
    124      try:
    125          import icalendar
    126          from timelinelib.wxgui.dialogs.importics.view import ImportIcsDialog
    127      except ImportError:
    128          raise TimelineIOError(_("Could not find iCalendar Python package. It is required for working with ICS files."))
    129      else:
    130          from timelinelib.dataimport.ics import import_db_from_ics
    131          return import_db_from_ics(path, ImportIcsDialog)
    132
    133
    134 -def file_starts_with(path, start):
    135      return read_first_line(path).startswith(start)
    136
    137
    138 -def read_first_line(path):
    139      try:
    140          f = open(path)
    141          try:
    142              line = f.readline()
    143              return line
    144          finally:
    145              f.close()
    146      except IOError:
    147          raise TimelineIOError("Unable to read data from '%s'." % path)
    148