Пример #1
0
Файл: move.py Проект: sovaa/sire
def move(id, newcat):
    from sire.misc import Misc
    from sire.printer import c
    import sire.dbman as dbman
    import sire.helpers as helpers
    import sire.printer as printer
    import time, sys

    # User didn't specify what category to move to, try to use the default value.
    if newcat is None:
        # Try default for current category.
        newcat = helpers.config_value("move." + helpers.get_category_from_id(id))
        if not newcat:
            printer.text_error(Misc.ERROR["destdefcat"])
            sys.exit(1)

    if not helpers.config_value("categories." + newcat):
        printer.text_error(Misc.ERROR["nocat"] % c(newcat))
        sys.exit(1)

    newdate = str(int(time.time()))
    helpers.enforce_duplicate_policy(helpers.get_title_from_id(id), newcat)

    result = dbman.get_item_with_id(id)
    if len(result) == 0:
        printer.text_error(Misc.ERROR["item"] % c(id))
        sys.exit(1)

    dbman.update_category(str(id), newcat)
    dbman.update_date(str(id), newdate)
    title = result[0][1]
    times = result[0][3]

    printer.print_info("move", (id, printer.format_text_out(title), newcat, result[0][2], times))
    return
Пример #2
0
 def db_and_print(val, i):
     # sql friendly
     val = format_text_in(val)
     old = dbman.get_title_with_id(i)
     dbman.set_title_with_id(val, i)
     text_note("Changed item with ID '%s' from '%s' to '%s'." % (c(i), c(old), c(format_text_out(val))))
     return
Пример #3
0
def change(id, val_orig):
    from sire.printer import format_text_in, format_text_out, c, text_note, text_error, text_warning
    from sire.helpers import id_exists
    from sire.misc import Misc
    import sire.dbman as dbman
    import re, sys

    # internal, updates the db and prints the result
    def db_and_print(val, i):
        # sql friendly
        val = format_text_in(val)
        old = dbman.get_title_with_id(i)
        dbman.set_title_with_id(val, i)
        text_note("Changed item with ID '%s' from '%s' to '%s'." % (c(i), c(old), c(format_text_out(val))))
        return

    if val_orig is None:
        text_error(Misc.ERROR['destchg'])
        sys.exit(1)

    # user might have written something awefully wrong, so just in case
    dbman.db_backup()

    # Find all '%([0-9]+)' in 'val'.
    id = id.split(',')
    res = re.findall(r'\%\(([0-9]+|#)\)', format_text_out(val_orig))

    # removes all specified IDs from the job list that doesn't exists and warns about them
    rids = []
    for k, i in enumerate(id):
        if not id_exists(i):
            text_warning(Misc.ERROR['item'] % i)
            rids = [k] + rids
    for j in rids:
        del id[j]

    # f**k yeah, no search and replace needed, just do it
    if not res:
        for i in id:
            db_and_print(val_orig, i)
        return

    # so the user had some %(blabal) in their query; do some search and replace
    for i in id:
        val = val_orig
        # may be multiple %()
        for v in res:
            # this means "if %(#), do this"
            if not cmp(v, '#'):
                rval = dbman.get_title_with_id(i)
            # otherwise it's a %(<some ID>)
            else:
                rval = dbman.get_title_with_id(v)
            val = re.compile('\%\(' + v + '\)').sub(rval, val)
        db_and_print(val, i)

    return
Пример #4
0
Файл: list.py Проект: sovaa/sire
def list(cats, colw_score = 7, colw_id = 5):
    from sire.helpers import cnr_parser, config_value, sort, is_young
    from sire.printer import text_warning, text_note, table_head, format_category_out, format_text_out, bold
    alldbs = dbman.get_all_categories()
    cats = cnr_parser(cats)
    if not cats:
        cats = [config_value("defval.list")]

    if not cats:
        text_error(Misc.ERROR['deflist'])
        return

    # only care if it's set and not 0, and if newline is not... newline, dont show the table
    if config_value("general.showtable") and opt.get('newline') is '\n':
        colw_title = 0
        for cat in cats:
            dbsel = dbman.get_items_with_category(cat)
            for id, title, date, cat, score in dbsel:
                if len(title) > colw_title:
                    colw_title = len(title)
        table_head(opt.get('id'), opt.get('score'), [colw_title, colw_id, colw_score])

    output = ''
    for cat in cats:
        if cat not in alldbs:
            if config_value('warn.emptycat') in [None, 1]:
                text_warning(Misc.ERROR["emptycat"] % c(cat))
            continue

        # get all items in category 'cat' and sort them if sorting is specified
        dbsel = sort(dbman.get_items_with_category(cat))

        # (--no-category, -C) was used
        if opt.get('category'): 
            formcat = format_category_out(cat)
            if formcat:
                output += formcat + opt.get('newline')

        for id, title, date, cat, score in dbsel:
            # (--days-ago, -y) was used
            if not is_young(date):
                continue

            # Make titles aligned.
            id = str(id)
            sid = ' '*(colw_id - len(id))
            sscore = ' '*(colw_score - len(str(score)))

            # uuh, you probably don't want to touch the next few lines; it -works-, okay?
            l1 = ['1', None, False]
            l2 = ['0', False]
            gscore = config_value("general.showscore")
            if config_value("general.showid") is '1' and opt.get('id'):
                output += bold(id, opt.get('color')) + sid + ': '

            # break it down: gscore is the CONFIG value that tells us to PRINT scores or not,
            # opt.get('score') is the COMMAND LINE value that tells us to NOT PRINT (double negative)
            #
            # the command line have higher priority than the config
            # remember: 'command' here is double negative, so NO is YES, ignorance is bliss, war is..., sorry
            # config + command = 
            #   NO   +    NO   = YES
            #   NO   +    YES  = NO
            #   YES  +    NO   = YES (not necessary, since config already sais 'print ahead, dude'
            #   YES  +    YES  = NO
            if gscore in l1 and opt.get('score') in l1 or gscore in l2 and opt.get('score') in l2:
                output += bold(str(score), opt.get('color')) + sscore + ': '
            output += format_text_out(title) + opt.get('newline')

    output = output.strip()
    if len(output) is 0:
        return text_note(Misc.ERROR["itemnotfound"])
    if output[-1] == ',':
        output = output[:-1]

    print output
    return