Gnumed.timelinelib.config.dotfile

Package Gnumed :: Package timelinelib :: Package config :: Module dotfile |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.config.dotfile

      1  # Copyright (C) 2009, 2010, 2011  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  """
     20  Handle application configuration.
     21
     22  This module is global and can be used by all modules. Before accessing
     23  configurations, the read function should be called. To save the current
     24  configuration back to file, call the write method.
     25  """
     26
     27
     28  import sys
     29  from ConfigParser import ConfigParser
     30  from ConfigParser import DEFAULTSECT
     31  import os.path
     32
     33
     34  # Name used in ConfigParser
     35  WINDOW_WIDTH = "window_width"
     36  WINDOW_HEIGHT = "window_height"
     37  WINDOW_XPOS = "window xpos"
     38  WINDOW_YPOS = "window ypos"
     39  WINDOW_MAXIMIZED = "window_maximized"
     40  SHOW_SIDEBAR = "show_sidebar"
     41  SHOW_LEGEND = "show_legend"
     42  SIDEBAR_WIDTH = "sidebar_width"
     43  RECENT_FILES = "recent_files"
     44  OPEN_RECENT_AT_STARTUP = "open_recent_at_startup"
     45  BALLOON_ON_HOVER = "balloon_on_hover"
     46  WEEK_START = "week_start"
     47  USE_WIDE_DATE_RANGE = "use_wide_date_range"
     48  USE_INERTIAL_SCROLLING = "use_inertial_scrolling"
     49  DEFAULTS = {
     50      WINDOW_WIDTH: "900",
     51      WINDOW_HEIGHT: "500",
     52      WINDOW_XPOS: "-1",
     53      WINDOW_YPOS: "-1",
     54      WINDOW_MAXIMIZED: "False",
     55      SHOW_SIDEBAR: "True",
     56      SIDEBAR_WIDTH: "200",
     57      SHOW_LEGEND: "True",
     58      OPEN_RECENT_AT_STARTUP: "True",
     59      RECENT_FILES: "",
     60      BALLOON_ON_HOVER: "True",
     61      WEEK_START: "monday",
     62      USE_WIDE_DATE_RANGE: "False",
     63      USE_INERTIAL_SCROLLING : "False"
     64  }
     65  # Some settings
     66  MAX_NBR_OF_RECENT_FILES_SAVED = 5
     67  ENCODING = "utf-8"
     68
     69
     70 -def read_config(path):
     71      config = Config(path)
     72      config.read()
     73      return config
     74
     75
     76 -class Config(object):
     77      """
     78      Provide read and write access to application configuration settings.
     79
     80      Built as a wrapper around ConfigParser: Properties exist to read and write
     81      values but ConfigParser does the actual reading and writing of the
     82      configuration file.
     83      """
     84
     85 -    def __init__(self, path):
     86          self.path = path
     87          self.config_parser = ConfigParser(DEFAULTS)
     88
     89 -    def read(self):
     90          """Read settings from file specified in constructor."""
     91          self.config_parser.read(self.path)
     92
     93 -    def write(self):
     94          """
     95          Write settings to file specified in constructor and raise IOError if
     96          failed.
     97          """
     98          f = open(self.path, "w")
     99          try:
    100              self.config_parser.write(f)
    101          finally:
    102              f.close()
    103
    104 -    def get_window_size(self):
    105          return (self.config_parser.getint(DEFAULTSECT, WINDOW_WIDTH),
    106                  self.config_parser.getint(DEFAULTSECT, WINDOW_HEIGHT))
    107 -    def set_window_size(self, size):
    108          width, height = size
    109          self.config_parser.set(DEFAULTSECT, WINDOW_WIDTH, str(width))
    110          self.config_parser.set(DEFAULTSECT, WINDOW_HEIGHT, str(height))
    111      window_size = property(get_window_size, set_window_size)
    112
    113 -    def get_window_pos(self):
    114          width, height = self.get_window_size()
    115          # Make sure that some area of the window is visible on the screen
    116          # Some part of the titlebar must be visible
    117          xpos = max(-width + 100,
    118                     self.config_parser.getint(DEFAULTSECT, WINDOW_XPOS))
    119          # Titlebar must not be above the upper screen border
    120          ypos = max(0, self.config_parser.getint(DEFAULTSECT, WINDOW_YPOS))
    121          return (xpos, ypos)
    122
    123 -    def set_window_pos(self, pos):
    124          xpos, ypos = pos
    125          self.config_parser.set(DEFAULTSECT, WINDOW_XPOS, str(xpos))
    126          self.config_parser.set(DEFAULTSECT, WINDOW_YPOS, str(ypos))
    127      window_pos = property(get_window_pos, set_window_pos)
    128
    129 -    def get_window_maximized(self):
    130          return self.config_parser.getboolean(DEFAULTSECT, WINDOW_MAXIMIZED)
    131 -    def set_window_maximized(self, maximized):
    132          self.config_parser.set(DEFAULTSECT, WINDOW_MAXIMIZED, str(maximized))
    133      window_maximized = property(get_window_maximized, set_window_maximized)
    134
    139      show_sidebar = property(get_show_sidebar, set_show_sidebar)
    140
    141 -    def get_show_legend(self):
    142          return self.config_parser.getboolean(DEFAULTSECT, SHOW_LEGEND)
    143 -    def set_show_legend(self, show):
    144          self.config_parser.set(DEFAULTSECT, SHOW_LEGEND, str(show))
    145      show_legend = property(get_show_legend, set_show_legend)
    146
    151      sidebar_width = property(get_sidebar_width, set_sidebar_width)
    152
    153 -    def get_recently_opened(self):
    154          ro = self.config_parser.get(DEFAULTSECT, RECENT_FILES).decode(ENCODING).split(",")
    155          # Filter out empty elements: "".split(",") will return [""] but we want
    156          # the empty list
    157          ro_filtered = [x for x in ro if x]
    158          return ro_filtered
    159      recently_opened = property(get_recently_opened)
    160
    161 -    def append_recently_opened(self, path):
    162          if path in [":tutorial:"]:
    163              # Special timelines should not be saved
    164              return
    165          if isinstance(path, str):
    166              # This path might have come from the command line so we need to convert
    167              # it to unicode
    168              path = path.decode(sys.getfilesystemencoding())
    169          abs_path = os.path.abspath(path)
    170          current = self.recently_opened
    171          # Just keep one entry of the same path in the list
    172          if abs_path in current:
    173              current.remove(abs_path)
    174          current.insert(0, abs_path)
    175          self.config_parser.set(DEFAULTSECT, RECENT_FILES,
    176                (",".join(current[:MAX_NBR_OF_RECENT_FILES_SAVED])).encode(ENCODING))
    177
    178 -    def get_open_recent_at_startup(self):
    179          return self.config_parser.getboolean(DEFAULTSECT, OPEN_RECENT_AT_STARTUP)
    180 -    def set_open_recent_at_startup(self, open):
    181          self.config_parser.set(DEFAULTSECT, OPEN_RECENT_AT_STARTUP, str(open))
    182      open_recent_at_startup = property(get_open_recent_at_startup,
    183                                        set_open_recent_at_startup)
    184
    185 -    def get_balloon_on_hover(self):
    186          return self.config_parser.getboolean(DEFAULTSECT, BALLOON_ON_HOVER)
    187 -    def set_balloon_on_hover(self, balloon_on_hover):
    188          self.config_parser.set(DEFAULTSECT, BALLOON_ON_HOVER, str(balloon_on_hover))
    189      balloon_on_hover = property(get_balloon_on_hover, set_balloon_on_hover)
    190
    191 -    def get_week_start(self):
    192          return self.config_parser.get(DEFAULTSECT, WEEK_START)
    193 -    def set_week_start(self, week_start):
    194          if not week_start in ["monday", "sunday"]:
    195              raise ValueError("Invalid week start.")
    196          self.config_parser.set(DEFAULTSECT, WEEK_START, week_start)
    197      week_start = property(get_week_start, set_week_start)
    198
    199 -    def get_use_wide_date_range(self):
    200          return self.config_parser.getboolean(DEFAULTSECT, USE_WIDE_DATE_RANGE)
    201 -    def set_use_wide_date_range(self, value):
    202          self.config_parser.set(DEFAULTSECT, USE_WIDE_DATE_RANGE, str(value))
    203      use_wide_date_range = property(get_use_wide_date_range, set_use_wide_date_range)
    204
    205 -    def get_use_inertial_scrolling(self):
    206          return self.config_parser.getboolean(DEFAULTSECT, USE_INERTIAL_SCROLLING)
    207 -    def set_use_inertial_scrolling(self, value):
    208          self.config_parser.set(DEFAULTSECT, USE_INERTIAL_SCROLLING, str(value))
    209      use_inertial_scrolling = property(get_use_inertial_scrolling, set_use_inertial_scrolling)
    210