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