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 determine_edits(edits): if edits is 0: if config_value("find.edits"): edits = int(config_value("find.edits")) else: edits = 18 # default if edits < 0: edits = 0 elif edits > 100: edits = 100 return edits
def db_valid_id(id): from sire.helpers import is_valid_id, format_text_out from sire.printer import text_warning if not is_valid_id(id): if config_value('warn.invalidid'): text_warning(Misc.ERROR["bad_id"] % format_text_out(id)) return False return True
def db_valid_category(cat): from sire.helpers import is_valid_category, format_text_out from sire.printer import text_warning if not is_valid_category(cat): if config_value('warn.invalidcat') in [None, 1]: text_warning(Misc.ERROR["bad_cat"] % format_text_out(cat)) return False return True
def get_db_location(type): from sire.helpers import config_value from sire.printer import text_warning dbloc = config_value("database.location") if not dbloc: text_warning(Misc.ERROR["dbloc"]) dbloc = Misc.DBLOCATION[type] return dbloc
def get_db_backup_location(type): from sire.helpers import config_value from sire.printer import text_warning dbbak = config_value("database.backup") if not dbbak: text_warning(Misc.ERROR["dbbackup"]) dbbak = Misc.DBBACKUP[type] return dbbak
def connect(): from sire.helpers import config_value from sire.printer import text_warning, text_error from sire.misc import Misc db = None type = get_db_type() if type == "sqlite": from pysqlite2 import dbapi2 as sqlite3 location = config_value("database.location") if not location: text_warning(Misc.ERROR["dbloc"]) location = "/etc/sire/db.sqlite" try: db = sqlite3.connect(location) db.text_factory = str except: text_error(Misc.ERROR["dbcon"]) sys.exit(1) elif type == "mysql": import MySQLdb host = config_value("database.host") username = config_value("database.user") password = config_value("database.pass") dbname = config_value("database.name") if not host: text_warning(Misc.ERROR["dbinfohost"]) host = "localhost" if not username: text_error(Misc.ERROR["dbinfouser"]) if not password: text_error(Misc.ERROR["dbinfopass"]) if not dbname: text_error(Misc.ERROR["dbinfoname"]) if None in [username, password, dbname]: sys.exit(1) try: db = MySQLdb.connect( host, config_value("database.user"), config_value("database.pass"), config_value("database.name") ) except: text_error(Misc.ERROR["dbcon"]) sys.exit(1) if not db: text_error(Misc.ERROR["dbcon"]) sys.exit(1) return db
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 get_db_type(): from sire.helpers import config_value from sire.printer import text_error type = config_value("database.type") if not type: text_error(Misc.ERROR["dbtype"]) sys.exit(1) if type not in Misc.DBLOCATION.keys(): text_error(Misc.ERROR["dbunknown"] % str(type)) sys.exit(1) return type
def db_backup(): import commands, sys, os from sire.printer import text_note, text_warning, text_error from sire.helpers import config_value text_note("Backing up database...") type = get_db_type() dbbak = get_db_backup_location(type) if type == "sqlite": dbloc = get_db_location(type) if os.path.exists(dbbak + '.gz'): os.remove(dbbak + '.gz') os.popen('cp %s %s' % (dbloc, dbbak)) os.popen('gzip -9 %s' % dbbak) elif type == "mysql": import MySQLdb host = config_value("database.host") username = config_value("database.user") password = config_value("database.pass") dbname = config_value("database.name") if not host: text_warning(Misc.ERROR["dbinfohost"]) host = "localhost" if not username: text_error(Misc.ERROR["dbinfouser"]) if not password: text_error(Misc.ERROR["dbinfopass"]) if not dbname: text_error(Misc.ERROR["dbinfoname"]) if None in [username, password, dbname]: sys.exit(1) os.popen('mysqldump -u %s -p%s %s | gzip -9 > %s.gz' % (username, password, dbname, dbbak)) text_note("Backup complete!") return
def db_restore(): import os from sire.helpers import pretend, config_value from sire.printer import text_note text_note("Restoring database from backup...") if pretend(): return text_note("Backup restored!") type = get_db_type() dbbak = get_db_backup_location(type) if type == "sqlite": dbloc = get_db_location(type) os.popen("gunzip -c %s > %s" % (dbbak, dbloc)) elif type == "mysql": username = config_value('database.user') password = config_value('database.pass') dbname = config_value('database.name') os.open("gunzip -c %s.gz | mysql -u %s -p%s %s" % (dbbak, username, password, dbname)) text_note("Backup restored!") 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 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