예제 #1
0
파일: test1.py 프로젝트: fraoustin/myterm
"""

import sys, traceback
import colorconsole, colorconsole.terminal
from myterm.parser import OptionParser
import myterm
import os

VERSION="0.1"
PROG="test1"
DESCRIPTION="""test of myterm.OptionParser"""
AUTHOR="Frederic Aoustin"

if __name__ == '__main__':
    usage = "usage: %prog [options]"
    parser = OptionParser(version="%s %s" % (PROG,VERSION), usage=usage)
    parser.description= DESCRIPTION
    parser.epilog = AUTHOR
    parser.add_option("-a","--arg",
        dest = "myarg",
        action = "store_true",
        help = "arg by default False",
        default = False)
    try:
        (options, args) = parser.parse_args()
        if options.myarg:
            parser.error("myarg is True, error ;-)")
        screen = myterm.SCREEN
        for color in colorconsole.terminal.colors:
            screen.set_color(colorconsole.terminal.colors[color], myterm.BACKGROUND_DEFAULT)
            print(colorconsole.terminal.colors[color], color)
예제 #2
0
def main():
    usage = USAGE
    parser = OptionParser(version="%s %s" % (PROG,VERSION), usage=usage)
    parser.description= DESCRIPTION
    parser.epilog = AUTHOR
    try:
        add_dynamic_option(parser)
        (options, args) = parser.parse_args()
        verb = args[0]
        if verb == 'ws':
            piwigo = Piwigo(url=options.url)
            if 'user' and 'password' in options.__dict__:
                piwigo.login(options.user, options.password)
            kw = purge_kw(options.__dict__,('user','password','url'))
            pp = pprint.PrettyPrinter(indent=4)
            pp.pprint(Ws(piwigo, options.method)(**kw))
            if piwigo.islogged:
                piwigo.logout()
        if verb == "download":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                if not os.path.isdir(options.dest):
                    os.makedirs(options.dest)        
                options.dest = os.path.abspath(options.dest)
                piwigo.iscategory(options.category)
                if options.category[-2:] == ' /' : options.category = options.category[:-2]
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,('user','password','url','dest','category','thread'))
                for img in piwigo.images(options.category, **kw):
                    run.add(piwigo.download, 
                            ["%s / %s" % (options.category, str(img)), "%s%s%s" % (options.dest, os.path.sep, str(img))],
                            kw)
            except Exception as e:
                ana.stop()
                raise e
            ana.stop()
            run.start()
            piwigo.logout()
            if run.error:
               parser.error(run.strerror) 
        if verb == "upload":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                piwigo.makedirs(options.category)
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
                for img in glob.glob(options.source):
                    run.add(piwigo.upload,
                            [os.path.abspath(img), options.category], 
                            kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
        if verb == "sync-up":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                options.source = os.path.abspath(options.source)
                if not os.path.isdir(options.source):
                    raise Exception("%s is not directory" % options.source)
                piwigo.iscategory(options.category)
                if len(options.category) and options.category[-1] != '/' and options.category[:-3] != ' / ':
                    options.category = options.category+ ' / '
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
                # local -> piwigo
                for root, dirnames, filenames in os.walk(options.source):
                    filtering = fnmatch.filter(filenames, options.extension.split(',')[0])
                    for ext in options.extension.split(',')[1:]:
                        filtering = filtering + fnmatch.filter(filenames, ext)
                    for filename in filtering:
                        pathrel = os.path.abspath(os.path.join(root, filename))[len(options.source)+1:]
                        pathabs = os.path.abspath(os.path.join(root, filename))
                        category = options.category + ' / '.join(pathrel.split(os.sep)[:-1])
                        if not piwigo.isimage(category + ' / ' + filename):
                            run.add(piwigo.makedirs,[category,], kw)
                            run.add(piwigo.upload,[pathabs, category], kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
        if verb == "sync-down":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                options.source = os.path.abspath(options.source)
                if not os.path.isdir(options.source):
                    raise Exception("%s is not directory" % options.source)
                piwigo.iscategory(options.category)
                if len(options.category) and options.category[-1] != '/' and options.category[:-3] != ' / ':
                    options.category = options.category+ ' / '
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,('user','password','url','source','category','thread'))
                # piwigo -> local
                for category, item in piwigo.plan.iteritems():
                    if options.category == category[0:len(options.category)]:
                        path = os.path.join(options.source, *category[len(options.category):].split(' / '))
                        if not os.path.exists(path):
                            os.makedirs(path)
                        for img in piwigo.images(category):
                            pathimg = os.path.join(path, img)
                            if not os.path.exists(pathimg):
                                run.add(piwigo.download, 
                                    ["%s / %s" % (category, str(img)), pathimg],
                                    kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
    except Exception as e:
        parser.error(e)
        sys.exit(1)
예제 #3
0
def main():
    usage = USAGE
    parser = OptionParser(version="%s %s" % (PROG, VERSION), usage=usage)
    parser.description = DESCRIPTION
    parser.epilog = AUTHOR
    try:
        add_dynamic_option(parser)
        (options, args) = parser.parse_args()
        verb = args[0]
        if verb == 'ws':
            piwigo = Piwigo(url=options.url)
            if 'user' and 'password' in options.__dict__:
                piwigo.login(options.user, options.password)
            kw = purge_kw(options.__dict__, ('user', 'password', 'url'))
            pp = pprint.PrettyPrinter(indent=4)
            pp.pprint(Ws(piwigo, options.method)(**kw))
            if piwigo.islogged:
                piwigo.logout()
        if verb == "download":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                if not os.path.isdir(options.dest):
                    os.makedirs(options.dest)
                options.dest = os.path.abspath(options.dest)
                piwigo.iscategory(options.category)
                if options.category[-2:] == ' /':
                    options.category = options.category[:-2]
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(
                    options.__dict__,
                    ('user', 'password', 'url', 'dest', 'category', 'thread'))
                for img in piwigo.images(options.category, **kw):
                    run.add(piwigo.download, [
                        "%s / %s" % (options.category, str(img)),
                        "%s%s%s" % (options.dest, os.path.sep, str(img))
                    ], kw)
            except Exception as e:
                ana.stop()
                raise e
            ana.stop()
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
        if verb == "upload":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                piwigo.makedirs(options.category)
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,
                              ('user', 'password', 'url', 'source', 'category',
                               'thread'))
                for img in glob.glob(options.source):
                    run.add(piwigo.upload,
                            [os.path.abspath(img), options.category], kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
        if verb == "sync-up":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                options.source = os.path.abspath(options.source)
                if not os.path.isdir(options.source):
                    raise Exception("%s is not directory" % options.source)
                piwigo.iscategory(options.category)
                if len(options.category) and options.category[
                        -1] != '/' and options.category[:-3] != ' / ':
                    options.category = options.category + ' / '
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,
                              ('user', 'password', 'url', 'source', 'category',
                               'thread'))
                # local -> piwigo
                for root, dirnames, filenames in os.walk(options.source):
                    filtering = fnmatch.filter(filenames,
                                               options.extension.split(',')[0])
                    for ext in options.extension.split(',')[1:]:
                        filtering = filtering + fnmatch.filter(filenames, ext)
                    for filename in filtering:
                        pathrel = os.path.abspath(os.path.join(
                            root, filename))[len(options.source) + 1:]
                        pathabs = os.path.abspath(os.path.join(root, filename))
                        category = options.category + ' / '.join(
                            pathrel.split(os.sep)[:-1])
                        if not piwigo.isimage(category + ' / ' + filename):
                            run.add(piwigo.makedirs, [
                                category,
                            ], kw)
                            run.add(piwigo.upload, [pathabs, category], kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
        if verb == "sync-down":
            ana = Analyse('Analyze')
            ana.start()
            try:
                piwigo = Piwigo(url=options.url)
                piwigo.login(options.user, options.password)
                # check
                options.source = os.path.abspath(options.source)
                if not os.path.isdir(options.source):
                    raise Exception("%s is not directory" % options.source)
                piwigo.iscategory(options.category)
                if len(options.category) and options.category[
                        -1] != '/' and options.category[:-3] != ' / ':
                    options.category = options.category + ' / '
                # treatment
                run = Run(verb, options.thread)
                kw = purge_kw(options.__dict__,
                              ('user', 'password', 'url', 'source', 'category',
                               'thread'))
                # piwigo -> local
                for category, item in piwigo.plan.iteritems():
                    if options.category == category[0:len(options.category)]:
                        path = os.path.join(
                            options.source,
                            *category[len(options.category):].split(' / '))
                        if not os.path.exists(path):
                            os.makedirs(path)
                        for img in piwigo.images(category):
                            pathimg = os.path.join(path, img)
                            if not os.path.exists(pathimg):
                                run.add(piwigo.download, [
                                    "%s / %s" % (category, str(img)), pathimg
                                ], kw)
                ana.stop()
            except Exception as e:
                ana.stop()
                raise e
            run.start()
            piwigo.logout()
            if run.error:
                parser.error(run.strerror)
    except Exception as e:
        parser.error(e)
        sys.exit(1)