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

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

      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 ConfigParser import DEFAULTSECT
     20  from ConfigParser import SafeConfigParser
     21
     22  from timelinelib.general.observer import Observable
     23
     24
     25 -class Config(Observable):
     26
     27 -    def __init__(self, item_dicts):
     28          Observable.__init__(self)
     29          self._config_parser = SafeConfigParser()
     30          self._build(item_dicts)
     31
     32 -    def read(self, path):
     33          self._config_parser.read(path)
     34
     35 -    def write(self, path):
     36          with open(path, "wb") as f:
     37              self._config_parser.write(f)
     38
     39 -    def _build(self, item_dicts):
     40          for item_dict in item_dicts:
     41              self._build_item(Item(item_dict))
     42
     43 -    def _build_item(self, item):
     44          def getter():
     45              return item.get_decoder()(self._config_parser.get(
     46                  item.get_section(),
     47                  item.get_config_name()
     48              ))
     49
     50          def setter(value):
     51              self._config_parser.set(
     52                  item.get_section(),
     53                  item.get_config_name(),
     54                  item.get_encoder()(value)
     55              )
     56              self._notify()
     57          setattr(self, "get_%s" % item.get_name(), getter)
     58          setattr(self, "set_%s" % item.get_name(), setter)
     59          setter(item.get_default())
     60
     61
     62 -class Item(object):
     63
     64 -    def __init__(self, item_dict):
     65          self._item_dict = item_dict
     66
     67 -    def get_name(self):
     68          return self._item_dict["name"]
     69
     70 -    def get_config_name(self):
     71          return self._item_dict.get("config_name", self.get_name())
     72
     73 -    def get_section(self):
     74          return DEFAULTSECT
     75
     76 -    def get_default(self):
     77          return self._item_dict["default"]
     78
     79 -    def get_encoder(self):
     80          return {
     81              "text": self._text_to_string,
     82              "integer": str,
     83              "boolean": str,
     84          }[self._get_data_type()]
     85
     86 -    def get_decoder(self):
     87          return {
     88              "text": self._string_to_text,
     89              "integer": int,
     90              "boolean": self._string_to_bool,
     91          }[self._get_data_type()]
     92
     93 -    def _get_data_type(self):
     94          return self._item_dict.get("data_type", "text")
     95
     96 -    def _text_to_string(self, text):
     97          if isinstance(text, unicode):
     98              return text.encode("utf-8")
     99          else:
    100              return text
    101
    102 -    def _string_to_text(self, string):
    103          return string.decode("utf-8")
    104
    105 -    def _string_to_bool(self, string):
    106          return {
    107              "true": True,
    108              "false": False,
    109          }[string.lower()]
    110