Example #1
0
def id_exists(id):
    if not is_valid_id(id):
        text_error(Misc.ERROR['bad_id'] % c(id))
        sys.exit(1);

    cursor = dbexec("SELECT * FROM item WHERE id = '%s'" % str(id), None, False)
    if len(cursor) > 0:
        return True
    return False
Example #2
0
File: change.py Project: sovaa/sire
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
Example #3
0
File: dbman.py Project: sovaa/sire
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
Example #4
0
def get_title_from_id(id):
    if not is_valid_id(id):
        text_error(Misc.ERROR['bad_id'] % c(id))
        sys.exit(1);

    cursor = dbman.dbexec("SELECT title FROM item WHERE id = '%s'" % id, None, False)
    res = cursor
    if len(res) > 0:
        return res[0][0]
    text_error(Misc.ERROR["item"] % c(id)) 
    sys.exit(1)
Example #5
0
def parse_config(conffile):
    import os, ConfigParser, string
    if not os.path.isfile(conffile):
        f = file(conffile, 'w')
        f.close()
        text_error(Misc.ERROR["conf"])
        sys.exit(1)

    _config = {}
    cp = ConfigParser.ConfigParser()
    cp.read(conffile)

    for sec in cp.sections():
        name = string.lower(sec)
        for opt in cp.options(sec):
            _config[name + "." + string.lower(opt)] = string.strip(cp.get(sec, opt))
    return _config
Example #6
0
File: move.py Project: 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
Example #7
0
File: dbman.py Project: sovaa/sire
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
Example #8
0
File: dbman.py Project: sovaa/sire
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