Gnumed.timelinelib.config.dateformatparser

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

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

      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 timelinelib.calendar.gregorian.dateformatter import GregorianDateFormatter
     20
     21
     22  REGION_START_POS = 0
     23  REGION_LENGTH = 1
     24  REGION_SEPARATOR = 2
     25  REGION_TYPE = 3
     26  YEAR = GregorianDateFormatter.YEAR
     27  MONTH = GregorianDateFormatter.MONTH
     28  DAY = GregorianDateFormatter.DAY
     29  ERROR_TEXT = _("""\
     30  Invalid Date Format:
     31
     32  The format should contain
     33      one year placeholder  = yyyy
     34      one month placeholder = mmm or mm
     35      one day placeholder   = dd
     36      two placeholders for separators between year, month and day
     37
     38  Separators can't contain the letters y, m or d
     39
     40  Example of valid formats:
     41      yyyy-mm-dd
     42      dd/mm/yyyy
     43      mmm/dd-yyyy
     44          """)
     45
     46
     47 -class DateFormatParser(object):
     48
     49 -    def get_error_text(self):
     50          return ERROR_TEXT
     51
     52 -    def is_valid(self, date_format):
     53          try:
     54              self.parse(date_format)
     55              return True
     56          except ValueError:
     57              return False
     58
     59 -    def parse(self, date_format):
     60          self.regions = self._to_regions(date_format)
     61          return self
     62
     63 -    def get_separators(self):
     64          return self.regions[1][REGION_SEPARATOR], self.regions[2][REGION_SEPARATOR]
     65
     66 -    def get_region_order(self):
     67          return (self._get_region_type_index(YEAR), self._get_region_type_index(MONTH), self._get_region_type_index(DAY))
     68
     69 -    def use_abbreviated_month_names(self):
     70          for i in range(len(self.regions)):
     71              if self.regions[i][REGION_TYPE] == MONTH:
     72                  return self.regions[i][REGION_LENGTH] == 3
     73          return False
     74
     75 -    def _get_region_type_index(self, region_type):
     76          for i in range(len(self.regions)):
     77              if self.regions[i][REGION_TYPE] == region_type:
     78                  return i
     79
     80 -    def _to_regions(self, date_format):
     81          fmt = date_format.lower()
     82          self._assert_leading_char_is_a_date_placeholder(date_format)
     83          self._assert_trailing_char_is_a_date_placeholder(date_format)
     84          fmt, regions = self._get_regions(fmt, date_format)
     85          self._get_separators_placeholders(fmt, regions)
     86          return regions
     87
     88 -    def _assert_leading_char_is_a_date_placeholder(self, date_format):
     89          if date_format[0] not in ("y", "m", "d"):
     90              raise ValueError()
     91
     92 -    def _assert_trailing_char_is_a_date_placeholder(self, date_format):
     93          if date_format[-1] not in ("y", "m", "d"):
     94              raise ValueError()
     95
     96 -    def _get_separators_placeholders(self, fmt, regions):
     97          separator1_length = regions[1][REGION_START_POS] - regions[0][REGION_LENGTH]
     98          regions[1][REGION_SEPARATOR] = fmt[:separator1_length]
     99          regions[2][REGION_SEPARATOR] = fmt[separator1_length:]
    100
    101 -    def _get_regions(self, fmt, date_format):
    102          def getKey(item):
    103              return item[0]
    104          fmt, region0 = self._get_and_remove_year_region(fmt, date_format)
    105          fmt, region1 = self._get_and_remove_month_region(fmt, date_format)
    106          fmt, region2 = self._get_and_remove_day_region(fmt, date_format)
    107          regions = [region0, region1, region2]
    108          if "y" in fmt or "m" in fmt or "d" in fmt:
    109              raise ValueError()
    110          regions.sort(key=getKey)
    111          return fmt, regions
    112
    113 -    def _get_and_remove_year_region(self, fmt, date_format):
    114          return self._get_and_remove_region(fmt, date_format, "yyyy", YEAR)
    115
    116 -    def _get_and_remove_month_region(self, fmt, date_format):
    117          if "mmm" in fmt:
    118              return self._get_and_remove_region(fmt, date_format, "mmm", MONTH)
    119          elif "mm" in fmt:
    120              return self._get_and_remove_region(fmt, date_format, "mm", MONTH)
    121          else:
    122              raise ValueError()
    123
    124 -    def _get_and_remove_day_region(self, fmt, date_format):
    125          return self._get_and_remove_region(fmt, date_format, "dd", DAY)
    126
    127 -    def _get_and_remove_region(self, fmt, date_format, placeholder, region_type):
    128          if placeholder in fmt:
    129              fmt = fmt.replace(placeholder, "", 1)
    130              index = date_format.lower().index(placeholder)
    131              return fmt, [index, len(placeholder), "", region_type]
    132          else:
    133              raise ValueError()
    134