Beispiel #1
0
def parse_args():
    parser = argparse.ArgumentParser(
        description='Holydate, %s ancient orthodox calendar.' % VERSION,
        epilog='Report bugs and wishes to <*****@*****.**>',
        prog='holydate',
        formatter_class=lambda prog: argparse.HelpFormatter(
            prog, max_help_position=79))

    calendar_group = parser.add_argument_group('calendar')
    paschalion_group = parser.add_argument_group('paschalion')
    search_group = parser.add_argument_group('search')

    parser.add_argument(
        'translit', help='transliterate calendar output', nargs='?')
    parser.add_argument(
        '-c', '--calendar', dest='calendar', type=check_calendar,
        help='julian or gregorian calendar system',
        action='store')
    parser.add_argument(
        '-v', '--version',
        action='version', version='Holydate version is %s' % VERSION)
    calendar_group.add_argument(
        '-d', '--date', dest='date', action='store', type=isodate,
        help='display a calendar for gregorian date input yyyy-mm-dd')
    calendar_group.add_argument(
        '-t', '--today', dest='today', action='store_true',
        help='display a calendar for gregorian date today')

    paschalion_group.add_argument(
        '-y', '--year', dest='year', action='store', type=isoyear,
        help='year for visulal paschalion')
    paschalion_group.add_argument(
        '-ch', '--chronology', dest='paschalion_collection',
        action='append_const', const='get_year',
        help='display information about the current year')
    paschalion_group.add_argument(
        '-p', '--pascha', dest='paschalion_collection',
        action='append_const', const='get_resurrection_day',
        help='display date of orthodoxy pascha')
    paschalion_group.add_argument(
        '-ff', '--fast-free-weeks', dest='paschalion_collection',
        action='append_const', const='get_fast_free_weeks',
        help='display fast free weeks for whole year')
    paschalion_group.add_argument(
        '-f', '--fasts', dest='paschalion_collection',
        action='append_const', const='get_fasts',
        help='display fasts for whole year')
    paschalion_group.add_argument(
        '-mv', '--movable-feasts', dest='paschalion_collection',
        action='append_const', const='get_movable_feasts',
        help='display movable feasts for whole year')
    paschalion_group.add_argument(
        '-mn', '--minor-fixed-feasts', dest='paschalion_collection',
        action='append_const', const='get_minor_fixed_feasts',
        help='display minor fixed feasts for whole year')
    paschalion_group.add_argument(
        '-mj', '--major-fixed-feasts', dest='paschalion_collection',
        action='append_const', const='get_major_fixed_feasts',
        help='display major fixed feasts for whole year')
    paschalion_group.add_argument(
        '-ss', '--soul-saturdays', dest='paschalion_collection',
        action='append_const', const='get_soul_saturdays',
        help='display a dead-offices for whole year')
    paschalion_group.add_argument(
        '--paschalion', dest='paschalion', action='store_true',
        help='display full visual paschalion suit for whole year')

    search_group.add_argument(
        '-s', '--search', dest='string', action='store', type=str,
        help=('search saints names, movable '
              'and fixed feasts in orthodoxy Menology'))

    results = parser.parse_args()
    today = datetime.today()
    year = results.year.year if results.year else today.year
    cal_system = results.calendar or 'gregorian'

    if results.date:
        date = (results.date.day, results.date.month, results.date.year)
    else:
        date = (today.day, today.month, today.year)
        if cal_system == 'julian':
            date = convert_gregorian_to_julian(*date)
    paschalion = VisualPaschalion(year, calendar=cal_system)

    def _paschalion_suite(methods):
        """Construct full paschalion"""

        _out = map(lambda x: getattr(
            paschalion, x).__call__(), methods)
        _out_covered = [u'\n']
        for item in _out:
            result = item.get('result')
            result.insert(0, u"{bold}%s{br}{end}" % item.get('headline'))
            _out_covered.append(u"\n".join(map(_cover, result)) + u'\n\n')
        _out_covered.append(u'\n')
        _out = u"".join(_out_covered)
        return _out.format(**DICT_FORMAT)

    output = u""
    if results.today or results.date:
        output = calendar(*date, cal_system=cal_system)
    elif results.paschalion_collection:
        output = _paschalion_suite(results.paschalion_collection)
    elif results.paschalion:
        _methods = [
            'get_year', 'get_resurrection_day',
            'get_fast_free_weeks', 'get_fasts',
            'get_movable_feasts', 'get_major_fixed_feasts',
            'get_minor_fixed_feasts', 'get_soul_saturdays'
        ]
        out = _paschalion_suite(_methods)
        output = u"".join([
            u'\n',
            _cover(u'{underline}Зрячая Пасхалия на %d год{end}' % year),
            u'\n\n',
            out, ])
        output = output.format(**DICT_FORMAT)
    elif results.string:
        output = search_constructor(results.string)
    else:
        parser.print_help()
    print translit_cal(output) if results.translit else output
Beispiel #2
0
def search_feasts(search_string):
    """
    Search saints and feasts in year menology.
    :param search_string: search input string (e.g. saint or feast name).
    :return list, witch contains gregorian & julian dates and search result.
    """
    if len(search_string) < 3:
        return dict(count=0, result=[])

    # TODO: Fix endings in the words: Андрей, Андриан
    endings = re.compile(
        ur"(ый|ой|ая|ое|ые|ому|а|ем|о|ов|у|е|й|" ur"ого|ал|ему|и|ство|ых|ох|ия|ий|ь|я|он|ют|ат|ин|ея)$", re.U
    )

    search_string_clean = re.sub(endings, u"", search_string)
    pattern = re.compile(ur"(\b%s\w+\b)" % search_string_clean, re.I | re.U)

    if len(search_string) <= 5:
        endings = re.compile(ur"(а|ы|у|и|я)$", re.U)

        search_string_clean = re.sub(endings, u"", search_string)
        pattern = re.compile(ur"(\b%s[а-я]{0,2}\b)" % search_string_clean, re.I | re.U)

    date = datetime.date.today()
    year = convert_gregorian_to_julian(date.day, date.month, date.year)[2]
    year_menology = menology
    cal_out = []

    # List of weekday names.
    for month in range(1, 13):
        days_in_month = calendar.monthrange(year, month)[1]
        for day in range(1, days_in_month + 1):
            cal = AncientCalendar(day, month, year, calendar="julian")
            weekdayname = cal.get_daily_feast()
            cal_out.extend([[list((day, month, year)), weekdayname]])

    # Construct year menology with movable feasts.
    for item in cal_out:
        year_menology[item[0][1]][item[0][0]]["weekday"] = item[1]

    d = year_menology
    out = []

    # Search string in menology.
    for _month, value in d.iteritems():
        for _day, value1 in d[_month].iteritems():
            for key2, content in d[_month][_day].iteritems():

                if re.search(pattern, unicode(content)):
                    _date = (_day, _month, year)
                    out.extend(
                        [
                            [
                                list(convert_julian_day_to_julian_am(convert_julian_to_julian_day(*_date))),
                                list(convert_julian_to_gregorian(*_date)),
                                clean_string(content),
                            ]
                        ]
                    )

    # Highliting search_string.
    start_str = u"{highlight_start}"
    end_str = u"{highlight_end}"

    pattern_highlite = re.compile(ur"(\b%s\w*\b)" % search_string_clean, re.I | re.U)

    tr = lambda m: start_str + m.group() + end_str

    result = [[t[0], t[1], pattern_highlite.sub(tr, t[2])] for t in out]
    # Count of serach results.
    count = len([re.findall(pattern, item[2]) for item in out])

    return dict(count=count, result=result)