Example #1
0
def save_data(data, _id, path, do_pickle=True, silent=False):
    """ Save data to a diskfile """
    if not silent:
        logging.debug("Saving data for %s in %s", _id, path)
    path = os.path.join(path, _id)

    try:
        _f = open(path, 'wb')
        if do_pickle:
            if cfg.use_pickle():
                pickler = pickle.Pickler(_f, 2)
            else:
                pickler = cPickle.Pickler(_f, 2)
            pickler.dump(data)
            _f.flush()
            _f.close()
            pickler.clear_memo()
            del pickler
        else:
            _f.write(data)
            _f.flush()
            _f.close()
    except:
        logging.error(Ta('Saving %s failed'), path)
        logging.info("Traceback: ", exc_info=True)
Example #2
0
def load_data(_id, path, remove=True, do_pickle=True, silent=False):
    """ Read data from disk file """
    path = os.path.join(path, _id)

    if not os.path.exists(path):
        logging.info("%s missing", path)
        return None

    if not silent:
        logging.debug("Loading data for %s from %s", _id, path)

    try:
        _f = open(path, 'rb')
        if do_pickle:
            if cfg.use_pickle():
                data = pickle.load(_f)
            else:
                data = cPickle.load(_f)
        else:
            data = _f.read()
        _f.close()

        if remove:
            os.remove(path)
    except:
        logging.error(Ta('Loading %s failed'), path)
        logging.info("Traceback: ", exc_info=True)
        return None

    return data
Example #3
0
def save_data(data, _id, path, do_pickle=True, silent=False):
    """ Save data to a diskfile """
    if not silent:
        logging.debug('[%s] Saving data for %s in %s', misc.caller_name(), _id,
                      path)
    path = os.path.join(path, _id)

    # We try 3 times, to avoid any dict or access problems
    for t in xrange(3):
        try:
            with open(path, 'wb') as data_file:
                if do_pickle:
                    if cfg.use_pickle():
                        pickle.dump(data, data_file)
                    else:
                        cPickle.dump(data, data_file)
                else:
                    data_file.write(data)
            break
        except:
            if silent:
                # This can happen, probably a removed folder
                pass
            elif t == 2:
                logging.error(T('Saving %s failed'), path)
                logging.info("Traceback: ", exc_info=True)
            else:
                # Wait a tiny bit before trying again
                time.sleep(0.1)
Example #4
0
def load_data(_id, path, remove=True, do_pickle=True, silent=False):
    """ Read data from disk file """
    path = os.path.join(path, _id)

    if not os.path.exists(path):
        logging.info("[%s] %s missing", misc.caller_name(), path)
        return None

    if not silent:
        logging.debug("[%s] Loading data for %s from %s", misc.caller_name(),
                      _id, path)

    try:
        with open(path, 'rb') as data_file:
            if do_pickle:
                if cfg.use_pickle():
                    data = pickle.load(data_file)
                else:
                    data = cPickle.load(data_file)
            else:
                data = data_file.read()

        if remove:
            misc.remove_file(path)
    except:
        logging.error(T('Loading %s failed'), path)
        logging.info("Traceback: ", exc_info=True)
        return None

    return data
Example #5
0
def load_admin(_id, remove=False, silent=False):
    """ Read data in admin folder in specified format """
    path = os.path.join(cfg.admin_dir.get_path(), _id)
    logging.info("Loading data for %s from %s", _id, path)

    if not os.path.exists(path):
        logging.info("%s missing", path)
        return None

    try:
        with open(path, 'rb') as data_file:
            if cfg.use_pickle():
                data = pickle.load(data_file)
            else:
                data = cPickle.load(data_file)
        if remove:
            os.remove(path)
    except:
        if not silent:
            excepterror = str(sys.exc_info()[0])
            logging.error(T('Loading %s failed with error %s'), path, excepterror)
            logging.info("Traceback: ", exc_info=True)
        return None

    return data
Example #6
0
def load_admin(_id, remove=False, silent=False):
    """ Read data in admin folder in specified format """
    path = os.path.join(cfg.admin_dir.get_path(), _id)
    logging.debug("[%s] Loading data for %s from %s", misc.caller_name(), _id,
                  path)

    if not os.path.exists(path):
        logging.info("[%s] %s missing", misc.caller_name(), path)
        return None

    try:
        with open(path, 'rb') as data_file:
            if cfg.use_pickle():
                data = pickle.load(data_file)
            else:
                data = cPickle.load(data_file)
        if remove:
            misc.remove_file(path)
    except:
        if not silent:
            excepterror = str(sys.exc_info()[0])
            logging.error(T('Loading %s failed with error %s'), path,
                          excepterror)
            logging.info("Traceback: ", exc_info=True)
        return None

    return data
Example #7
0
def load_data(_id, path, remove=True, do_pickle=True, silent=False):
    """ Read data from disk file """
    path = os.path.join(path, _id)

    if not os.path.exists(path):
        logging.info("%s missing", path)
        return None

    if not silent:
        logging.debug("Loading data for %s from %s", _id, path)

    try:
        with open(path, 'rb') as data_file:
            if do_pickle:
                if cfg.use_pickle():
                    data = pickle.load(data_file)
                else:
                    data = cPickle.load(data_file)
            else:
                data = data_file.read()

        if remove:
            os.remove(path)
    except:
        logging.error(T('Loading %s failed'), path)
        logging.info("Traceback: ", exc_info=True)
        return None

    return data
Example #8
0
def save_data(data, _id, path, do_pickle=True, silent=False):
    """ Save data to a diskfile """
    if not silent:
        logging.debug("Saving data for %s in %s", _id, path)
    path = os.path.join(path, _id)

    # We try 3 times, to avoid any dict or access problems
    for t in xrange(3):
        try:
            with open(path, 'wb') as data_file:
                if do_pickle:
                    if cfg.use_pickle():
                        cPickle.dump(data, data_file)
                    else:
                        pickle.dump(data, data_file)
                else:
                    data_file.write(data)
            break
        except:
            if t == 2:
                logging.error(T('Saving %s failed'), path)
                logging.info("Traceback: ", exc_info=True)
            else:
                # Wait a tiny bit before trying again
                time.sleep(0.1)
Example #9
0
def save_data(data, _id, path, do_pickle=True, silent=False):
    """ Save data to a diskfile """
    if not silent:
        logging.debug("Saving data for %s in %s", _id, path)
    path = os.path.join(path, _id)

    try:
        _f = open(path, "wb")
        if do_pickle:
            if cfg.use_pickle():
                pickler = pickle.Pickler(_f, 2)
            else:
                pickler = cPickle.Pickler(_f, 2)
            pickler.dump(data)
            _f.flush()
            _f.close()
            pickler.clear_memo()
            del pickler
        else:
            _f.write(data)
            _f.flush()
            _f.close()
    except:
        logging.error(T("Saving %s failed"), path)
        logging.info("Traceback: ", exc_info=True)
Example #10
0
def save_admin(data, _id):
    """ Save data in admin folder in specified format """
    path = os.path.join(cfg.admin_dir.get_path(), _id)
    logging.info("Saving data for %s in %s", _id, path)

    # We try 3 times, to avoid any dict or access problems
    for t in xrange(3):
        try:
            with open(path, 'wb') as data_file:
                if cfg.use_pickle():
                    data = pickle.dump(data, data_file)
                else:
                    data = cPickle.dump(data, data_file)
            break
        except:
            if t == 2:
                logging.error(T('Saving %s failed'), path)
                logging.info("Traceback: ", exc_info=True)
            else:
                # Wait a tiny bit before trying again
                time.sleep(0.1)
Example #11
0
def save_admin(data, _id):
    """ Save data in admin folder in specified format """
    path = os.path.join(cfg.admin_dir.get_path(), _id)
    logging.info("Saving data for %s in %s", _id, path)

    # We try 3 times, to avoid any dict or access problems
    for t in xrange(3):
        try:
            with open(path, 'wb') as data_file:
                if cfg.use_pickle():
                    data = pickle.dump(data, data_file)
                else:
                    data = cPickle.dump(data, data_file)
            break
        except:
            if t == 2:
                logging.error(T('Saving %s failed'), path)
                logging.info("Traceback: ", exc_info=True)
            else:
                # Wait a tiny bit before trying again
                time.sleep(0.1)