def text_error(text): from sire.shared import opt if opt.get("verbose") is 0: return text = format_text_out(text) if opt.get("color"): print "%s%sError: %s%s" % (C["bold"], C["red"], C["default"], text) else: print "Error: " + text return
def text_warning(text): from sire.shared import opt if opt.get("verbose") is 0: return text = format_text_out(text) if opt.get("color"): print "%s%sWarning: %s%s" % (C["bold"], C["yellow"], C["default"], text) else: print "Warning: " + text return
def text_note(text): from sire.shared import opt text = format_text_out(text) if opt.get("color"): print "%s%sNote: %s%s" % (C["bold"], C["green"], C["default"], text) else: print "Note: " + text return
def title_exists(cat, title): from sire.helpers import format_text_in if not db_valid_category(cat): return False cursor = dbexec("SELECT title FROM item WHERE title = '%s' AND cat = '%s' AND profile = '%s'" % (format_text_in(title), format_text_in(cat), opt.get('profile')), None, False) if len(cursor) > 0: return True return False
def enforce_duplicate_policy(name, category): from shared import opt import sys dup = get_duplicate_policy(category) if dup == '0' and item_exists(name): existing_category = get_category_from_title(name) existing_duplicate = get_duplicate_policy(existing_category) if existing_duplicate not in ["1", "2"]: text_warning("Item already exists in category '%s'. Use (--force, -f) to add anyway." % c(existing_category)) if not opt.get('force'): sys.exit(1) # need to check both if dup == '1' and title_exists(category, name): text_warning(Misc.ERROR["itemexists"] % c(category)) if not opt.get('force'): sys.exit(1) return
def approxsearch(edits, search_strings, destinations): from sire.helpers import text_warning, format_text_out from sire.printer import format_category_out from sire.shared import opt from sire.misc import Misc show_categories = config_value('find.showcats') == '1' and opt.get('category') is not False edits = determine_edits(edits) results = search(edits, search_strings, destinations) sort_results(results, show_categories) if not results: text_warning("No matches found!") return previous_category = None for result in results: item_id, ratio, title, category = result[0], str(result[2]) + '%', result[1], result[3] spacer_id = ' '*(5 - len(str(item_id))) spacer_ratio = ' '*(6 - len(ratio)) if show_categories: if should_show_category_header(previous_category, category): if previous_category is not None: print("") previous_category = category print(format_category_out(category)) print_columns() # Showing ID when listing is optional. if config_value("general.showid") == '0' or opt.get('id') is False: print(format_text_out(title)) else: print("%s%s%s %s | %s%s | %s" % (Misc.C['bold'], item_id, Misc.C['default'], spacer_id, ratio, spacer_ratio, format_text_out(title)))
def get_duplicates_from_categories(cats): from sire.misc import Misc # not categories specified, find in all if cats is None: return dbexec("SELECT id, title, COUNT(title) FROM item GROUP BY title HAVING (COUNT(title) > 1)", None, False) sqlcat = "WHERE profile = '%s' AND (" % opt.get('profile') # set up the WHERE sql thingie for cat in cats: # no dropping tables here kiddo if not db_valid_category(cat): continue sqlcat += "cat = '%s' OR " % cat # probably don't want the last ' OR ' anyway sqlcat = sqlcat[:-4] + ')' # fire up the main laseeer return dbexec("SELECT id, title, COUNT(title) FROM item %s GROUP BY title HAVING (COUNT(title) > 1)" % sqlcat, None, False)
def sort(items): # Sorting is optional. sortmethod = opt.get('sort') if not sortmethod: sortmethod = config_value("defval.sort") if sortmethod is None: return items if sortmethod == "title": items = sorted(items, key=lambda (k,v,a,b,c): (v.lower(),int(k),a,b,c)) elif sortmethod == "id": items = sorted(items, key=lambda (k,v,a,b,c): (int(k),v,a,b,c)) elif sortmethod == "time": items = sorted(items, key=lambda (k,v,a,b,c): (a,int(k),v,b,c)) elif sortmethod == "score": items = sorted(items, key=lambda (k,v,a,b,c): (int(c),int(k),v,a,b)) return items
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 set_title_with_id(title, id): from sire.helpers import format_text_in if not db_valid_id(id): return False dbexec("UPDATE item SET title = '%s' WHERE id = '%s' AND profile = '%s'" % (format_text_in(title), id, opt.get('profile')), None, True)
def delete(id): if not db_valid_id(id): return False return dbexec("DELETE FROM item WHERE id = '%s' AND profile = '%s'" % (id, opt.get('profile')), None, True)
def update_date(id, date): if not db_valid_id(id): return False return dbexec("UPDATE item SET date = '%s' WHERE id = '%s' AND profile = '%s'" % (date, id, opt.get('profile')), None, True)
def update_category(id, cat): from sire.helpers import format_text_in if not db_valid_id(id) or not db_valid_category(cat): return False return dbexec("UPDATE item SET cat = '%s' WHERE id = '%s' AND profile = '%s'" % (format_text_in(cat), id, opt.get('profile')), None, True)
def get_title_with_id(id): from sire.helpers import format_text_out if not db_valid_id(id): return None return format_text_out(dbexec("SELECT title FROM item WHERE id = '%s' AND profile = '%s'" % (id, opt.get('profile')), None, False)[0][0])
def add(title, category): from sire.helpers import format_text_in if not db_valid_category(category): return False dbexec("INSERT INTO item (title, cat, profile) VALUES ('%s', '%s', '%s')" % (format_text_in(title), format_text_in(category), opt.get('profile')), None, True) return True
def get_item_with_id(id): if not db_valid_id(id): return None return dbexec("SELECT id, title, date, cat, score FROM item WHERE id = '%s' AND profile = '%s'" % (id, opt.get('profile')), None, False)
def get_items_with_category(cat): if not db_valid_category(cat): return None return dbexec("SELECT id, title, date, cat, score FROM item WHERE cat = '%s' AND profile = '%s'" % (cat, opt.get('profile')), None, False)
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
def pretend(): return opt.get('pretend')
def is_young(date): if opt.get('daysago') is 0: return True if int(time.time()) - int(date) > opt.get('daysago')*24*3600: return False return True
def c(text): from sire.shared import opt if opt.get("color"): return C["g"] + str(text) + C["d"] return text