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
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
def list_duplicates(cats): from sire.printer import text_note from sire.helpers import cnr_parser cats = cnr_parser(cats) results = dbman.get_duplicates_in_categories(cats) if not results: return text_note(Misc.ERROR["nodupe"] % c(str(cats))) # each item returned by that last query up there only returns one of the duplicate items, # so get all matching titles and print their IDs and categories for item in results: print text_note("%s entries found for '%s':" % (c(str(item[2])), c(item[1]))) dupeids = dbexec(db, "SELECT id, cat FROM item WHERE title = '%s'" % item[1], None, False) for id in dupeids: print " ID '%s' in category '%s'" % (c(str(id[0])), c(id[1])) return
def format_category_out(category): from sire.helpers import config_value from sire.shared import opt cattitle = config_value("categories." + category) if not cattitle: text_error(ERROR["catdesc"] % c(category)) return if not opt.get("color"): return " %s ('%s')\n" % (cattitle, category) color = config_value("colors." + category) if not color: color = config_value("defval.color") if color in C: color = C[color] return " %s%s%s ('%s')%s" % (C["bold"], color, cattitle, category, C["default"])
def print_info(type, values): from sire.helpers import format_time_passed, config_value from sire.printer import text_info, c style = config_value("general.printstyle") if type is "delete": words = ("Deleted", "from", "red", True) elif type is "add": words = ("Added", "to", "green", True) elif type is "move": words = ("Moved", "from '%s' to" % c(values[4]), "green", True) # default value if not style or style == "0": text_info(text_color(words[0], words[2], words[3])) text_info("ID : %s" % c(values[0])) text_info("Title : %s" % c(values[1])) text_info("Category : %s" % c(values[2])) if values[3]: text_info("Age : %s" % format_time_passed(values[3])) print elif style == "1": text_info( "%s item with ID '%s' and title '%s' %s category '%s'" % (words[0], c(values[0]), c(str(values[1])), words[1], c(values[2])) ) if values[3]: text_info("It was in that category for %s" % format_time_passed(values[3])) elif style == "2": text_info("%s '%s' '%s' %s '%s'" % (words[0], c(values[0]), c(str(values[1])), words[1], c(values[2]))) if values[3]: text_info("Age was %s" % format_time_passed(values[3])) elif style == "3": text_info("%s '%s' '%s' %s '%s'" % (words[0], c(values[0]), c(str(values[1])), words[1], c(values[2]))) else: text_info("%s '%s'" % (words[0], c(str(values[1])))) return
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