Example #1
0
def parse_range(s,hint=None):
    """returns list of (I{Month,Year}) tuples for a given range

    @param s: range string in format I{Month1-Month2/Year} or I{Month:Span/Year}
    @param hint: span value to be used, when M{Span=0}
    @rtype: [(int,int),...]
    @return: list of (I{Month,Year}) tuples for every month specified
    """
    if '/' in s:
        t = s.split('/')
        month,span = lib.parse_month_range(t[0])
        if hint and span == 0: span = hint
        year = lib.parse_year(t[1])
        margs = []
        for m in xrange(span):
            margs += [(month,year)]
            month += 1
            if month > 12: month = 1; year += 1
        return margs
    else:
        raise lib.Abort("calmagick: invalid range format '%s'" % options.range)
Example #2
0
def main_program():
    parser = get_parser()

    sys.argv, argv2 = lib.extract_parser_args(sys.argv, parser)
    (options, args) = parser.parse_args()

    list_and_exit = False
    if options.list_languages:
        for x in plugin_list("lang"):
            print(x[0], end=' ')
        print()
        list_and_exit = True
    if options.list_styles:
        for x in plugin_list("style"):
            print(x[0], end=' ')
        print()
        list_and_exit = True
    if options.list_geometries:
        for x in plugin_list("geom"):
            print(x[0], end=' ')
        print()
        list_and_exit = True
    if options.list_layouts:
        for x in plugin_list("layouts"):
            print(x[0], end=' ')
        print()
        list_and_exit = True
    if list_and_exit: return

    plugin_paths = get_plugin_paths()
    Language = import_plugin(plugin_paths, "lang", "language", "languages",
                             "--list-languages", options.lang)
    Style = import_plugin(plugin_paths, "style", "style", "styles",
                          "--list-styles", options.style)
    Geometry = import_plugin(plugin_paths, "geom", "geometry", "geometries",
                             "--list-geometries", options.geom)
    Layout = import_plugin(plugin_paths, "layouts", "layout", "layouts",
                           "--list-layouts", options.layout)

    for x in argv2:
        if '=' in x: x = x[0:x.find('=')]
        if not Layout.parser.has_option(x):
            parser.error(
                "invalid option %s; use --help (-h) or --layout-help (-?) to see available options"
                % x)

    (loptions, largs) = Layout.parser.parse_args(argv2)

    if options.layouthelp:
        #print "Help for layout:", options.layout
        Layout.parser.print_help()
        return

    if options.examples:
        print_examples()
        return

    # we can put it separately together with Layout; but we load Layout *after* lang,style,geom
    if len(args) < 1 or len(args) > 3:
        parser.print_help()
        return

    #if (len(args[-1]) == 4 and args[-1].isdigit()):
    #    print "WARNING: file name '%s' looks like a year, writing anyway..." % args[-1]

    # the usual "beware of exec()" crap applies here... but come on,
    # this is a SCRIPTING language, you can always hack the source code!!!
    if options.lang_assign:
        for x in options.lang_assign:
            exec("Language." + x)
    if options.style_assign:
        for x in options.style_assign:
            exec("Style." + x)
    if options.geom_assign:
        for x in options.geom_assign:
            exec("Geometry." + x)

    calendar.long_month_name = Language.long_month_name
    calendar.long_day_name = Language.long_day_name
    calendar.short_month_name = Language.short_month_name
    calendar.short_day_name = Language.short_day_name

    if len(args) == 1:
        Year = time.localtime()[0]
        Month, MonthSpan = 1, 12
        Outfile = args[0]
    elif len(args) == 2:
        Year = lib.parse_year(args[0])
        Month, MonthSpan = 1, 12
        Outfile = args[1]
    elif len(args) == 3:
        Month, MonthSpan = lib.parse_month_range(args[0])
        Year = lib.parse_year(args[1])
        Outfile = args[2]

    if MonthSpan == 0:
        raise lib.Abort("callirhoe: empty calendar requested, aborting")

    Geometry.landscape = options.landscape
    xcairo.XDPI = options.dpi
    Geometry.pagespec = options.paper
    Geometry.border = options.border

    hprovider = holiday.HolidayProvider(Style.dom, Style.dom_weekend,
                                        Style.dom_holiday,
                                        Style.dom_weekend_holiday,
                                        Style.dom_multi,
                                        Style.dom_weekend_multi,
                                        options.multiday_holidays)
    dataprovider = data.DataProvider(Style.dom)

    if options.holidays:
        for f in options.holidays:
            hprovider.load_holiday_file(f)

    if options.ics_file:
        for f in options.ics_file:
            dataprovider.load_data_file(f)

    if options.long_daynames:
        Language.day_name = Language.long_day_name
    else:
        Language.day_name = Language.short_day_name

    if options.short_monthnames:
        Language.month_name = Language.short_month_name
    else:
        Language.month_name = Language.long_month_name

    renderer = Layout.CalendarRenderer(Outfile, Year, Month, MonthSpan,
                                       (Style, Geometry, Language), hprovider,
                                       dataprovider, lib._version, loptions)
    renderer.render()