1# -*- coding: utf-8 -*-
2#
3# Gramps - a GTK+/GNOME based genealogy program
4#
5# Copyright (C) 2004-2006  Donald N. Allingham
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21
22"""
23Arabic-specific classes for parsing and displaying dates.
24"""
25
26#-------------------------------------------------------------------------
27#
28# Python modules
29#
30#-------------------------------------------------------------------------
31import re
32
33#-------------------------------------------------------------------------
34#
35# Gramps modules
36#
37#-------------------------------------------------------------------------
38from ..lib.date import Date
39from ._dateparser import DateParser
40from ._datedisplay import DateDisplay
41from ._datehandler import register_datehandler
42from ..const import ARABIC_COMMA
43
44#-------------------------------------------------------------------------
45#
46# Arabic parser class
47#
48#-------------------------------------------------------------------------
49class DateParserAR(DateParser):
50    """
51    Convert a text string into a Date object. If the date cannot be
52    converted, the text string is assigned.
53    """
54
55    # modifiers before the date
56    modifier_to_int = {
57        'قبل' : Date.MOD_BEFORE,
58        'قبل'    : Date.MOD_BEFORE,
59        'قبل.'   : Date.MOD_BEFORE,
60        'بعد'  : Date.MOD_AFTER,
61        'بعد'    : Date.MOD_AFTER,
62        'بعد.'   : Date.MOD_AFTER,
63        'حوالي'  : Date.MOD_ABOUT,
64        'حوالي.'   : Date.MOD_ABOUT,
65        'حوالي'    : Date.MOD_ABOUT,
66        'حوالي'  : Date.MOD_ABOUT,
67        'حوالي.'     : Date.MOD_ABOUT,
68        'حوالي' : Date.MOD_ABOUT,
69        }
70
71    islamic_to_int = {
72        "محرّم"           : 1,  "محرّم الحرام"  : 1,
73        "صفر"              : 2,  "ربيع الأول"      : 3,
74        "ربيع 1"             : 3,  "ربيع الأخير"      : 4,
75        "ربيع الثاني"     : 4,  "ربيع الثاني"    : 4,
76        "ربيع الثاني"     : 4,  "ربيع الثاني"    : 4,
77        "ربيع 2"           : 4,  "جمادى الأولى"       : 5,
78        "جمادى الأول"   : 5,  "جمادى 1"          : 5,
79        "جمادى الثانية"     : 6,  "جمادى الأخير"   : 6,
80        "جمادى الثاني"  : 6,  "جمادى 2"         : 5,
81        "رجب"              : 7,  "شعبان"            : 8,
82        "شعبان"           : 8,  "رمضان"            : 9,
83        "رمضان"           : 9,  "شوال"            : 10,
84        "ذو القعدة"        : 11, "ذو القعدة"          : 11,
85        "ذو القعدة"      : 11, "ذو الحجة"        : 12,
86        "ذو الحجة"          : 12, "ذو الحجة"      : 12,
87        }
88
89    bce = ["قبل الميلاد", "قبل الميلاد", "قبل الميلاد", "قبل الميلاد", "قبل الميلاد", "قبل الميلاد" ]
90
91    calendar_to_int = {
92        'غريغوري'        : Date.CAL_GREGORIAN,
93        'غريغوري'                : Date.CAL_GREGORIAN,
94        'يوليوسي'           : Date.CAL_JULIAN,
95        'يوليوسي'                : Date.CAL_JULIAN,
96        'عبري'           : Date.CAL_HEBREW,
97        'عبري'                : Date.CAL_HEBREW,
98        'إسلامي'          : Date.CAL_ISLAMIC,
99        'إسلامي'                : Date.CAL_ISLAMIC,
100        'فرنسي'           : Date.CAL_FRENCH,
101        'فرنسي جمهوري': Date.CAL_FRENCH,
102        'فرنسي'                : Date.CAL_FRENCH,
103        'فارسي'          : Date.CAL_PERSIAN,
104        'فارسي'                : Date.CAL_PERSIAN,
105        'سويدي'          : Date.CAL_SWEDISH,
106        'سويدي'                : Date.CAL_SWEDISH,
107        }
108
109    quality_to_int = {
110        'متوقع'  : Date.QUAL_ESTIMATED,
111        'متوقع.'       : Date.QUAL_ESTIMATED,
112        'متوقع'        : Date.QUAL_ESTIMATED,
113        'محسوب.'      : Date.QUAL_CALCULATED,
114        'محسوب'       : Date.QUAL_CALCULATED,
115        'محسوب' : Date.QUAL_CALCULATED,
116        }
117
118    def init_strings(self):
119        """
120        This method compiles regular expression strings for matching dates.
121        """
122        DateParser.init_strings(self)
123        _span_1 = ['من']
124        _span_2 = ['إلى']
125        _range_1 = ['بين']
126        _range_2 = ['و']
127        self._span = re.compile(
128            r"(%s)\s+(?P<start>.+)\s+(%s)\s+(?P<stop>.+)" %
129            ('|'.join(_span_1), '|'.join(_span_2)), re.IGNORECASE)
130        self._range = re.compile(
131            r"(%s)\s+(?P<start>.+)\s+(%s)\s+(?P<stop>.+)" %
132            ('|'.join(_range_1), '|'.join(_range_2)), re.IGNORECASE)
133
134#-------------------------------------------------------------------------
135#
136# Arabic display
137#
138#-------------------------------------------------------------------------
139class DateDisplayAR(DateDisplay):
140    """
141    Arabic language date display class.
142    """
143    # this is used to display the 12 gregorian months
144    long_months = ( "", "كانون الثاني", "شباط", "آذار", "نيسان", "أيار",
145                    "حزيران", "تموز", "آب", "أيلول", "تشرين الأول",
146                    "تشرين الثاني", "كانون الأول" )
147
148    short_months = ( "", "كانون2", "شباط", "آذار", "نيسان", "أيار", "حزيران",
149                     "تموز", "آب", "أيلول", "تشرين1", "تشرين2", "كانون1" )
150
151    islamic = (
152        "", "محرّم", "صفر", "ربيع الأول", "ربيع الثاني",
153        "جمادى الأولى", "جمادى الثانية", "رجب", "شعبان",
154        "رمضان", "شوال", "ذو القعدة", "ذو الحجة"
155        )
156
157    formats = (
158        "YYYY-MM-DD (قياسي)", "عددي", "شهر يوم, سنة",
159        "شهر يوم, سنة", "يوم شهر سنة", "يوم شهر سنة"
160        )
161        # this must agree with DateDisplayEn's "formats" definition
162        # (since no locale-specific _display_gregorian exists, here)
163
164    calendar = (
165        "", "يوليوسي", "عبري", "فرنسي",
166        "فارسي", "إسلامي", "سويدي"
167        )
168
169    _mod_str = ("", "قبل ", "بعد ", "حوالي ", "", "", "")
170
171    _qual_str = ("", "متوقع ", "محسوب ")
172
173    _bce_str = "%s قبل الميلاد."
174
175    def display(self, date):
176        """
177        Return a text string representing the date.
178        """
179        mod = date.get_modifier()
180        cal = date.get_calendar()
181        qual = date.get_quality()
182        start = date.get_start_date()
183        newyear = date.get_new_year()
184
185        qual_str = self._qual_str[qual]
186
187        if mod == Date.MOD_TEXTONLY:
188            return date.get_text()
189        elif start == Date.EMPTY:
190            return ""
191        elif mod == Date.MOD_SPAN:
192            d1 = self.display_cal[cal](start)
193            d2 = self.display_cal[cal](date.get_stop_date())
194            scal = self.format_extras(cal, newyear)
195            return "%s%s %s %s %s%s" % (qual_str, 'من', d1, 'إلى', d2, scal)
196        elif mod == Date.MOD_RANGE:
197            d1 = self.display_cal[cal](start)
198            d2 = self.display_cal[cal](date.get_stop_date())
199            scal = self.format_extras(cal, newyear)
200            return "%s%s %s %s %s%s" % (qual_str, 'بين', d1, 'و', d2, scal)
201        else:
202            text = self.display_cal[date.get_calendar()](start)
203            scal = self.format_extras(cal, newyear)
204            return "%s%s%s%s" % (qual_str, self._mod_str[mod], text, scal)
205
206    def dd_dformat01(self, date_val):
207        """
208        numerical -- for Arabic dates
209        """
210        value = DateDisplay.dd_dformat01(self, date_val)
211        return value.replace(',', ARABIC_COMMA)
212
213    def dd_dformat02(self, date_val, inflect, long_months):
214        """
215        month_name day, year -- for Arabic dates
216        """
217        value = DateDisplay.dd_dformat02(self, date_val, inflect, long_months)
218        return value.replace(',', ARABIC_COMMA)
219
220    def dd_dformat03(self, date_val, inflect, short_months):
221        """
222        month_abbreviation day, year -- for Arabic dates
223        """
224        value = DateDisplay.dd_dformat03(self, date_val, inflect, short_months)
225        return value.replace(',', ARABIC_COMMA)
226
227#-------------------------------------------------------------------------
228#
229# Register classes
230#
231#-------------------------------------------------------------------------
232register_datehandler(
233    ('ar_EG', 'ar_AR', 'ar', 'Arabic', 'arabic', ('%d %b, %Y',)),
234    DateParserAR, DateDisplayAR)
235