PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gramps-3.3.2/src/DateHandler/_Date_pl.py

#
Python | 315 lines | 247 code | 13 blank | 55 comment | 0 complexity | b9decf5704dd27c570e3d83b31de8ea5 MD5 | raw file
Possible License(s): GPL-2.0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Gramps - a GTK+/GNOME based genealogy program
  4. #
  5. # Copyright (C) 2004-2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # Polish version 2007 by Piotr Czubaszek
  22. # Updated in 2010 by ?ukasz Rymarczyk
  23. """
  24. Polish-specific classes for parsing and displaying dates.
  25. """
  26. #-------------------------------------------------------------------------
  27. #
  28. # Python modules
  29. #
  30. #-------------------------------------------------------------------------
  31. import re
  32. #-------------------------------------------------------------------------
  33. #
  34. # GRAMPS modules
  35. #
  36. #-------------------------------------------------------------------------
  37. from gen.lib import Date
  38. from _DateParser import DateParser
  39. from _DateDisplay import DateDisplay
  40. from _DateHandler import register_datehandler
  41. #-------------------------------------------------------------------------
  42. #
  43. # Polish parser
  44. #
  45. #-------------------------------------------------------------------------
  46. class DateParserPL(DateParser):
  47. month_to_int = DateParser.month_to_int
  48. month_to_int[u"stycze?"] = 1
  49. month_to_int[u"sty"] = 1
  50. month_to_int[u"I"] = 1
  51. month_to_int[u"luty"] = 2
  52. month_to_int[u"lut"] = 2
  53. month_to_int[u"II"] = 2
  54. month_to_int[u"marzec"] = 3
  55. month_to_int[u"mar"] = 3
  56. month_to_int[u"III"] = 3
  57. month_to_int[u"kwiecie?"] = 4
  58. month_to_int[u"kwi"] = 4
  59. month_to_int[u"IV"] = 4
  60. month_to_int[u"maj"] = 5
  61. month_to_int[u"V"] = 5
  62. month_to_int[u"czerwiec"] = 6
  63. month_to_int[u"cze"] = 6
  64. month_to_int[u"VI"] = 6
  65. month_to_int[u"lipiec"] = 7
  66. month_to_int[u"lip"] = 7
  67. month_to_int[u"VII"] = 7
  68. month_to_int[u"sierpie?"] = 8
  69. month_to_int[u"sie"] = 8
  70. month_to_int[u"VIII"] = 8
  71. month_to_int[u"wrzesie?"] = 9
  72. month_to_int[u"wrz"] = 9
  73. month_to_int[u"IX"] = 9
  74. month_to_int[u"pa?dziernik"] = 10
  75. month_to_int[u"pa?"] = 10
  76. month_to_int[u"X"] = 10
  77. month_to_int[u"listopad"] = 11
  78. month_to_int[u"lis"] = 11
  79. month_to_int[u"XI"] = 11
  80. month_to_int[u"grudzie?"] = 12
  81. month_to_int[u"gru"] = 12
  82. month_to_int[u"XII"] = 12
  83. # Alternative forms: declined nouns
  84. month_to_int[u"stycznia"] = 1
  85. month_to_int[u"lutego"] = 2
  86. month_to_int[u"marca"] = 3
  87. month_to_int[u"kwietnia"] = 4
  88. month_to_int[u"maja"] = 5
  89. month_to_int[u"czerwca"] = 6
  90. month_to_int[u"lipca"] = 7
  91. month_to_int[u"sierpnia"] = 8
  92. month_to_int[u"wrze?nia"] = 9
  93. month_to_int[u"pa?dziernika"] = 10
  94. month_to_int[u"listopada"] = 11
  95. month_to_int[u"grudnia"] = 12
  96. # Alternative forms: nouns without polish accent letters
  97. # (misspellings sometimes used in emails)
  98. month_to_int[u"styczen"] = 1
  99. month_to_int[u"kwiecien"] = 4
  100. month_to_int[u"sierpien"] = 8
  101. month_to_int[u"wrzesien"] = 9
  102. month_to_int[u"pazdziernik"] = 10
  103. month_to_int[u"grudzien"] = 12
  104. month_to_int[u"wrzesnia"] = 9
  105. month_to_int[u"pazdziernika"] = 10
  106. month_to_int[u"paz"] = 10
  107. modifier_to_int = {
  108. u'przed' : Date.MOD_BEFORE,
  109. u'po' : Date.MOD_AFTER,
  110. u'oko?o' : Date.MOD_ABOUT,
  111. u'ok.' : Date.MOD_ABOUT,
  112. u'circa' : Date.MOD_ABOUT,
  113. u'ca.' : Date.MOD_ABOUT,
  114. # Alternative forms: misspellings sometimes used in emails
  115. u'okolo' : Date.MOD_ABOUT,
  116. u'ok' : Date.MOD_ABOUT,
  117. }
  118. calendar_to_int = {
  119. u'gregoria?ski' : Date.CAL_GREGORIAN,
  120. u'greg.' : Date.CAL_GREGORIAN,
  121. u'julia?ski' : Date.CAL_JULIAN,
  122. u'jul.' : Date.CAL_JULIAN,
  123. u'hebrajski' : Date.CAL_HEBREW,
  124. u'hebr.' : Date.CAL_HEBREW,
  125. u'islamski' : Date.CAL_ISLAMIC,
  126. u'isl.' : Date.CAL_ISLAMIC,
  127. u'francuski republika?ski': Date.CAL_FRENCH,
  128. u'franc.' : Date.CAL_FRENCH,
  129. u'perski' : Date.CAL_PERSIAN,
  130. u'szwedzki' : Date.CAL_SWEDISH,
  131. u's' : Date.CAL_SWEDISH,
  132. # Alternative forms: nouns without polish accent letters
  133. # (misspellings sometimes used in emails)
  134. u'gregorianski' : Date.CAL_GREGORIAN,
  135. u'julianski' : Date.CAL_JULIAN,
  136. u'francuski republikanski': Date.CAL_FRENCH,
  137. }
  138. quality_to_int = {
  139. u'szacowany' : Date.QUAL_ESTIMATED,
  140. u'szac.' : Date.QUAL_ESTIMATED,
  141. u'obliczony' : Date.QUAL_CALCULATED,
  142. u'obl.' : Date.QUAL_CALCULATED,
  143. }
  144. bce = ["przed nasz? er?", "przed Chrystusem",
  145. "p.n.e."] + DateParser.bce
  146. def init_strings(self):
  147. DateParser.init_strings(self)
  148. self._span = re.compile("(od)\s+(?P<start>.+)\s+(do)\s+(?P<stop>.+)", re.IGNORECASE)
  149. # Also handle a common mistakes
  150. self._range = re.compile(u"((?:po)?mi(?:?|e)dzy)\s+(?P<start>.+)\s+(a)\s+(?P<stop>.+)", re.IGNORECASE)
  151. self._text2 = re.compile('(\d+)?.?\s+?%s\s*((\d+)(/\d+)?)?' % self._mon_str,
  152. re.IGNORECASE)
  153. self._jtext2 = re.compile('(\d+)?.?\s+?%s\s*((\d+)(/\d+)?)?' % self._jmon_str,
  154. re.IGNORECASE)
  155. #-------------------------------------------------------------------------
  156. #
  157. # Polish display
  158. #
  159. #-------------------------------------------------------------------------
  160. class DateDisplayPL(DateDisplay):
  161. """
  162. Polish language date display class.
  163. """
  164. long_months = ( u"", u"Stycze?", u"Luty", u"Marzec", u"Kwiecie?", u"Maj",
  165. u"Czerwiec", u"Lipiec", u"Sierpie?", u"Wrzesie?", u"Pa?dziernik",
  166. u"Listopad", u"Grudzie?" )
  167. short_months = ( u"", u"Sty", u"Lut", u"Mar", u"Kwi", u"Maj", u"Cze",
  168. u"Lip", u"Sie", u"Wrz", u"Pa?", u"Lis", u"Gru" )
  169. calendar = (
  170. "", u"julia?ski", u"hebrajski",
  171. u"francuski republika?ski", u"perski", u"islamski",
  172. u"swedish"
  173. )
  174. _mod_str = ("", u"przed ", u"po ", u"ok. ", "", "", "")
  175. _qual_str = ("", u"szacowany ", u"obliczony ")
  176. _bce_str = "%s p.n.e."
  177. formats = (
  178. "RRRR-MM-DD (ISO)", "Numeryczny", "Miesi?c Dzie?, Rok",
  179. "Dzie?.Miesi?c.Rok", "Dzie? Miesi?c Rok", "Dzie? MieRzym Rok"
  180. )
  181. roman_months = (
  182. "",
  183. "I",
  184. "II",
  185. "III",
  186. "IV",
  187. "V",
  188. "VI",
  189. "VII",
  190. "VIII",
  191. "IX",
  192. "X",
  193. "XI",
  194. "XII"
  195. )
  196. def _display_gregorian(self, date_val):
  197. year = self._slash_year(date_val[2], date_val[3])
  198. if self.format == 0:
  199. return self.display_iso(date_val)
  200. elif self.format == 1:
  201. if date_val[3]:
  202. return self.display_iso(date_val)
  203. else:
  204. if date_val[0] == date_val[1] == 0:
  205. value = str(date_val[2])
  206. else:
  207. value = self._tformat.replace('%m', str(date_val[0]))
  208. value = value.replace('%d', str(date_val[1]))
  209. value = value.replace('%y', str(date_val[2]))
  210. elif self.format == 2:
  211. # Month Day, Year
  212. if date_val[0] == 0:
  213. if date_val[1] == 0:
  214. value = year
  215. else:
  216. value = "%s %s" % (self.long_months[date_val[1]], year)
  217. else:
  218. value = "%s %d, %s" % (self.long_months[date_val[1]],
  219. date_val[0], year)
  220. elif self.format == 3:
  221. # Day. Month. Year
  222. if date_val[0] == 0:
  223. if date_val[1] == 0:
  224. value = year
  225. else:
  226. value = "%d.%s" % (date_val[1], year)
  227. else:
  228. value = "%d.%d.%s" % (date_val[0], date_val[1], year)
  229. elif self.format == 4:
  230. # Day Month Year
  231. if date_val[0] == 0:
  232. if date_val[1] == 0:
  233. value = year
  234. else:
  235. value = "%s %s" % (self.long_months[date_val[1]], year)
  236. else:
  237. value = "%d %s %s" % (date_val[0],
  238. self.long_months[date_val[1]], year)
  239. else:
  240. # Day RomanMon Year
  241. if date_val[0] == 0:
  242. if date_val[1] == 0:
  243. value = year
  244. else:
  245. value = "%s %s" % (self.roman_months[date_val[1]], year)
  246. else:
  247. value = "%d %s %s" % (date_val[0],
  248. self.roman_months[date_val[1]], year)
  249. if date_val[2] < 0:
  250. return self._bce_str % value
  251. else:
  252. return value
  253. def display(self, date):
  254. """
  255. Return a text string representing the date.
  256. """
  257. mod = date.get_modifier()
  258. cal = date.get_calendar()
  259. qual = date.get_quality()
  260. start = date.get_start_date()
  261. newyear = date.get_new_year()
  262. qual_str = self._qual_str[qual]
  263. if mod == Date.MOD_TEXTONLY:
  264. return date.get_text()
  265. elif start == Date.EMPTY:
  266. return ""
  267. elif mod == Date.MOD_SPAN:
  268. d1 = self.display_cal[cal](start)
  269. d2 = self.display_cal[cal](date.get_stop_date())
  270. scal = self.format_extras(cal, newyear)
  271. return "%s%s %s %s %s%s" % (qual_str, u'od', d1, u'do', d2,
  272. scal)
  273. elif mod == Date.MOD_RANGE:
  274. d1 = self.display_cal[cal](start)
  275. d2 = self.display_cal[cal](date.get_stop_date())
  276. scal = self.format_extras(cal, newyear)
  277. return "%s%s %s %s %s%s" % (qual_str, u'mi?dzy', d1, u'a', d2,
  278. scal)
  279. else:
  280. text = self.display_cal[date.get_calendar()](start)
  281. scal = self.format_extras(cal, newyear)
  282. return "%s%s%s%s" % (qual_str, self._mod_str[mod], text,
  283. scal)
  284. #-------------------------------------------------------------------------
  285. #
  286. # Register classes
  287. #
  288. #-------------------------------------------------------------------------
  289. register_datehandler(('pl_PL','polish','Polish_Poland','pl'),
  290. DateParserPL, DateDisplayPL)