From 70470156034c280d5a629dd74c2f2be1e4f5e943 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Sat, 3 Apr 2010 23:33:17 +0000 Subject: [PATCH] 3756: Cannot set new year value different than default (first January) svn: r15010 --- src/DateEdit.py | 5 ++- src/DateHandler/_DateDisplay.py | 9 ++++-- src/DateHandler/_DateParser.py | 34 ++++++++++++++------ src/gen/lib/date.py | 57 +++++++++++++++++++++++++++------ src/glade/dateedit.glade | 31 ++---------------- 5 files changed, 84 insertions(+), 52 deletions(-) diff --git a/src/DateEdit.py b/src/DateEdit.py index 07d3fc6f9..3e908f24b 100644 --- a/src/DateEdit.py +++ b/src/DateEdit.py @@ -186,7 +186,6 @@ class DateEditorDialog(ManagedWindow.ManagedWindow): """ Initiate and display the dialog. """ - ManagedWindow.ManagedWindow.__init__(self, uistate, track, self) # Create self.date as a copy of the given Date object. @@ -240,7 +239,7 @@ class DateEditorDialog(ManagedWindow.ManagedWindow): self.dual_dated = self.top.get_object('dualdated') self.new_year = self.top.get_object('newyear') - self.new_year.set_active(self.date.get_new_year()) + self.new_year.set_text(self.date.newyear_to_str()) # Disable second date controls if not compound date if not self.date.is_compound(): @@ -341,7 +340,7 @@ class DateEditorDialog(ManagedWindow.ManagedWindow): self.start_year.get_value_as_int(), self.dual_dated.get_active()) calendar = self.calendar_box.get_active() - newyear = self.new_year.get_active() + newyear = self.date.newyear_to_code(self.new_year.get_text()) return (quality, modifier, calendar, value, text, newyear) def switch_type(self, obj): diff --git a/src/DateHandler/_DateDisplay.py b/src/DateHandler/_DateDisplay.py index 12de801fc..124e1315e 100644 --- a/src/DateHandler/_DateDisplay.py +++ b/src/DateHandler/_DateDisplay.py @@ -139,12 +139,17 @@ class DateDisplay(object): Formats the extra items (calendar, newyear) for a date. """ scal = self.calendar[cal] - snewyear = self.newyear[newyear] + if isinstance(newyear, int) and newyear <= len(self.newyear): + snewyear = self.newyear[newyear] + elif isinstance(newyear, (list, tuple)): + snewyear = "%s-%s" % (newyear[0], newyear[1]) + else: + snewyear = "Err" retval = "" for item in [scal, snewyear]: if item: if retval: - retval += "," + retval += ", " retval += item if retval: return " (%s)" % retval diff --git a/src/DateHandler/_DateParser.py b/src/DateHandler/_DateParser.py index 1698544cd..f675e4a6d 100644 --- a/src/DateHandler/_DateParser.py +++ b/src/DateHandler/_DateParser.py @@ -317,20 +317,25 @@ class DateParser(object): self._imon_str = self.re_longest_first(self.islamic_to_int.keys()) self._smon_str = self.re_longest_first(self.swedish_to_int.keys()) self._cal_str = self.re_longest_first(self.calendar_to_int.keys()) - self._ny_str = self.re_longest_first(self.newyear_to_int.keys()) + self._ny_str = self.re_longest_first(self.newyear_to_int.keys()) # bce, calendar type and quality may be either at the end or at # the beginning of the given date string, therefore they will # be parsed from the middle and will be in match.group(2). - self._bce_re = re.compile("(.*)\s+%s( ?.*)" % self._bce_str) + self._bce_re = re.compile("(.*)\s+%s( ?.*)" % self._bce_str) + + self._cal = re.compile("(.*)\s+\(%s\)( ?.*)" % self._cal_str, + re.IGNORECASE) + self._calny = re.compile("(.*)\s+\(%s,\s*%s\)( ?.*)" % (self._cal_str, + self._ny_str), + re.IGNORECASE) + self._calny_iso = re.compile("(.*)\s+\(%s,\s*(\d{1,2}-\d{1,2})\)( ?.*)" % self._cal_str, + re.IGNORECASE) + + self._ny = re.compile("(.*)\s+\(%s\)( ?.*)" % self._ny_str, + re.IGNORECASE) + self._ny_iso = re.compile("(.*)\s+\((\d{1,2}-\d{1,2})\)( ?.*)") - self._cal = re.compile("(.*)\s+\(%s\)( ?.*)" % self._cal_str, - re.IGNORECASE) - self._calny = re.compile("(.*)\s+\(%s,%s\)( ?.*)" % (self._cal_str, - self._ny_str), - re.IGNORECASE) - self._ny = re.compile("(.*)\s+\(%s\)( ?.*)" % self._ny_str, - re.IGNORECASE) self._qual = re.compile("(.* ?)%s\s+(.+)" % self._qual_str, re.IGNORECASE) @@ -562,6 +567,12 @@ class DateParser(object): cal = self.calendar_to_int[match.group(2).lower()] newyear = self.newyear_to_int[match.group(3).lower()] text = match.group(1) + match.group(4) + else: + match = self._calny_iso.match(text) + if match: + cal = self.calendar_to_int[match.group(2).lower()] + newyear = tuple([int(s) for s in match.group(3).split("-")]) + text = match.group(1) + match.group(4) return (text, cal, newyear) def match_newyear(self, text, newyear): @@ -574,6 +585,11 @@ class DateParser(object): if match: newyear = self.newyear_to_int[match.group(2).lower()] text = match.group(1) + match.group(3) + else: + match = self._ny_iso.match(text) + if match: + newyear = tuple([int(s) for s in match.group(2).split("-")]) + text = match.group(1) + match.group(3) return (text, newyear) def match_quality(self, text, qual): diff --git a/src/gen/lib/date.py b/src/gen/lib/date.py index f310c7999..ed10e45a1 100644 --- a/src/gen/lib/date.py +++ b/src/gen/lib/date.py @@ -1012,16 +1012,7 @@ class Date(object): else: pref = "" - if self.newyear == Date.NEWYEAR_JAN1: - ny = "" - elif self.newyear == Date.NEWYEAR_MAR1: - ny = "Mar1" - elif self.newyear == Date.NEWYEAR_MAR25: - ny = "Mar25" - elif self.newyear == Date.NEWYEAR_SEP1: - ny = "Sep1" - else: - ny = "Err" + ny = self.newyear_str() if self.calendar != Date.CAL_GREGORIAN: if ny: @@ -1053,6 +1044,46 @@ class Date(object): self.dateval[Date._POS_DAY]) return "%s%s%s%s" % (qual, pref, val, cal) + def newyear_to_str(self): + """ + Return the string representation of the newyear. + """ + if self.newyear == Date.NEWYEAR_JAN1: + ny = "" + elif self.newyear == Date.NEWYEAR_MAR1: + ny = "Mar1" + elif self.newyear == Date.NEWYEAR_MAR25: + ny = "Mar25" + elif self.newyear == Date.NEWYEAR_SEP1: + ny = "Sep1" + elif isinstance(self.newyear, (list, tuple)): + ny = "%s-%s" % (self.newyear[0], self.newyear[1]) + else: + ny = "Err" + return ny + + def newyear_to_code(self, string): + """ + Return the code of a newyear string. + """ + string = string.strip().lower() + if string == "" or string == "jan1": + code = Date.NEWYEAR_JAN1 + elif string == "mar1": + code = Date.NEWYEAR_MAR1 + elif self.newyear == "mar25": + code = Date.NEWYEAR_MAR25 + elif self.newyear == "sep1": + code = Date.NEWYEAR_SEP1 + elif "-" in string: + try: + code = tuple([int(n) for n in string.split("-")]) + except: + code = 0 + else: + code = 0 + return code + def get_sort_value(self): """ Return the sort value of Date object. @@ -1429,6 +1460,8 @@ class Date(object): (DD, MM, YY, slash1, DD, MM, YY, slash2) text - A text string holding either the verbatim user input or a comment relating to the date. + newyear - The newyear code, or tuple representing (month, day) + of newyear day. The sort value is recalculated. """ @@ -1479,6 +1512,10 @@ class Date(object): split = (3, 25) elif ny == Date.NEWYEAR_SEP1: split = (9, 1) + elif isinstance(ny, (list, tuple)): + split = ny + else: + split = (0, 0) if (self.get_month(), self.get_day()) >= split: d1 = Date(self.get_year(), 1, 1) d1.set_calendar(self.calendar) diff --git a/src/glade/dateedit.glade b/src/glade/dateedit.glade index 822605587..7812240cd 100644 --- a/src/glade/dateedit.glade +++ b/src/glade/dateedit.glade @@ -119,16 +119,11 @@ - + True - newyear_model + Month-Day of first day of new year (e.g., "Jan1", "Mar1", "3-1", "3-25") - - - - 0 - - 3 @@ -599,26 +594,6 @@ - - - - - - - - January 1 - - - March 1 - - - March 25 - - - September 1 - - -