Exemplo n.º 1
0
def check_parsed_options(options):
    """set (remaining) default values and check validity of various option combinations"""
    if options.min_size is None:
        options.min_size = min(0.333,
                               options.max_size) if options.placement in [
                                   'min', 'max', 'random'
                               ] else min(0.05, options.max_size)
    if options.min_size > options.max_size:
        raise lib.Abort(
            "calmagick: --min-size should not be greater than --max-size")
    if options.sample is not None and not options.range:
        raise lib.Abort("calmagick: --sample requested without --range")
    if options.outfile is not None and options.range:
        raise lib.Abort(
            "calmagick: you cannot specify both --outfile and --range options")
    if options.sample is not None and options.shuffle:
        raise lib.Abort(
            "calmagick: you cannot specify both --shuffle and --sample options"
        )
    if options.shuffle:
        options.sample = 0
    if options.sample is None:
        if options.prefix == 'auto':
            options.prefix = 'no?'  # dirty, isn't it? :)
    else:
        if options.prefix == 'auto': options.prefix = 'yes'
    if options.jobs < 1: options.jobs = 1
Exemplo n.º 2
0
def main_program():
    """this is the main program routine

    Parses options, and calls C{compose_calendar()} the appropriate number of times,
    possibly by multiple threads (if requested by user)
    """
    parser = get_parser()

    magick_args = parse_magick_args()
    sys.argv,argv2 = lib.extract_parser_args(sys.argv,parser,2)
    (options,args) = parser.parse_args()
    check_parsed_options(options)

    if len(args) < 1:
        parser.print_help()
        sys.exit(0)

    if not os.path.isdir(options.outdir):
        # this way we get an exception if outdir exists and is a normal file
        os.mkdir(options.outdir)

    if options.range:
        flist = sorted(glob.glob(args[0]))
        mrange = parse_range(options.range,hint=len(flist))
        if options.verbose: print "Composing %d photos..." % len(mrange)
        if options.sample is not None:
            flist = random.sample(flist, options.sample if options.sample else min(len(mrange),len(flist)))
        nf = len(flist)
        if nf > 0:
            if len(mrange) > nf and options.prefix == 'no?': options.prefix = 'yes'
            if options.jobs > 1:
                q = Queue.Queue()
                ev = threading.Event()
                for i in range(options.jobs):
                     t = threading.Thread(target=range_worker,args=(q,ev,i))
                     t.daemon = True
                     t.start()

            cache = get_cache(nf, len(mrange));
            for i in range(len(mrange)):
                img = flist[i % nf]
                m,y = mrange[i]
                prefix = '' if options.prefix.startswith('no') else '%04d-%02d_' % (y,m)
                outimg = get_outfile(img,options.outdir,prefix,options.format)
                args = (img, outimg, options, [str(m), str(y)] + argv2, magick_args,
                        (i+1,len(mrange)), cache)
                if options.jobs > 1: q.put(args)
                else: compose_calendar(*args)

            if options.jobs > 1: q.join()
    else:
        img = args[0]
        if not os.path.isfile(img):
            raise lib.Abort("calmagick: input image '%s' does not exist" % img)
        outimg = get_outfile(img,options.outdir,'',options.format,options.outfile)
        compose_calendar(img, outimg, options, argv2, magick_args)
Exemplo n.º 3
0
def get_outfile(infile, outdir, base_prefix, format, hint=None):
    """get output file name taking into account output directory, format and prefix, avoiding overwriting the input file

    @rtype: str
    """
    if hint:
        outfile = hint
    else:
        head,tail = os.path.split(infile)
        base,ext = os.path.splitext(tail)
        if format: ext = '.' + format
        outfile = os.path.join(outdir,base_prefix+base+ext)
    if os.path.exists(outfile) and os.path.samefile(infile, outfile):
        if hint: raise lib.Abort("calmagick: --outfile same as input, aborting")
        outfile = os.path.join(outdir,base_prefix+base+'_calmagick'+ext)
    return outfile
Exemplo n.º 4
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)
Exemplo n.º 5
0
def main_program(imageName, month, isRange):
    """this is the main program routine

    Parses options, and calls C{compose_calendar()} the appropriate number of times,
    possibly by multiple threads (if requested by user)
    """
    parser = get_parser()

    magick_args = parse_magick_args()
    sys.argv, argv2 = lib.extract_parser_args(sys.argv, parser, 2)
    (options, args) = parser.parse_args()
    check_parsed_options(options)

    # if len(args) < 1:
    #     parser.print_help()
    #     sys.exit(0)
    #
    # if not os.path.isdir(options.outdir):
    #     # this way we get an exception if outdir exists and is a normal file
    #     os.mkdir(options.outdir)

    if isRange:
        flist = [imageName]
        #contains the image name
        mrange = [(month, 2020)]
        #use the current month given into the function to determine which
        #month to draw
        if options.verbose: print "Composing %d photos..." % len(mrange)
        if options.sample is not None:
            flist = random.sample(
                flist, options.sample if options.sample else min(
                    len(mrange), len(flist)))
        nf = len(flist)
        if nf > 0:
            if len(mrange) > nf and options.prefix == 'no?':
                options.prefix = 'yes'
            if options.jobs > 1:
                q = Queue.Queue()
                ev = threading.Event()
                for i in range(options.jobs):
                    t = threading.Thread(target=range_worker, args=(q, ev, i))
                    t.daemon = True
                    t.start()

            cache = get_cache(nf, len(mrange))
            for i in range(len(mrange)):
                img = flist[i % nf]
                m, y = mrange[i]
                prefix = '' if options.prefix.startswith(
                    'no') else '%04d-%02d_' % (y, m)
                #outimg = get_outfile(img,options.outdir,prefix,options.format)
                outimg = "HistoryCalendar/HistoryCalendar" + str(
                    month) + ".jpg"
                args = (img, outimg, options, [str(m), str(y)], magick_args,
                        (i + 1, len(mrange)), cache)
                if options.jobs > 1: q.put(args)
                else: compose_calendar(*args)

            if options.jobs > 1: q.join()
    else:
        img = imageName
        if not os.path.isfile(img):
            raise lib.Abort("calmagick: input image '%s' does not exist" % img)
        #outimg = get_outfile(img,options.outdir,'',options.format,options.outfile)
        outimg = "HistoryCalendar/HistoryCalendar" + str(month) + ".jpg"
        compose_calendar(img, outimg, options, argv2, magick_args, month)
Exemplo n.º 6
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()