def save(config, data): """Save data and create backup, creating a new data file if necessary.""" create_dir(os.path.dirname(config['data_path'])) # atomically save by writing to temporary file and moving to destination try: temp = NamedTemporaryFile(delete=False) # Windows cannot reuse the same open file name temp.close() with open(temp.name, 'w', encoding='utf-8', errors='replace') as f: for path, weight in data.items(): f.write(unico('%s\t%s\n' % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print('Error saving autojump data (disk full?)' % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(temp.name, config['data_path']) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config['backup_path']) or \ (time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD): # noqa shutil.copy(config['data_path'], config['backup_path'])
def save(config, data): """Save data and create backup, creating a new data file if necessary.""" data_dir = os.path.dirname(config['data_path']) create_dir(data_dir) # atomically save by writing to temporary file and moving to destination try: temp = NamedTemporaryFile(delete=False, dir=data_dir) # Windows cannot reuse the same open file name temp.close() with open(temp.name, 'w', encoding='utf-8', errors='replace') as f: for path, weight in data.items(): f.write(unico('%s\t%s\n' % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print('Error saving autojump data (disk full?)' % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(temp.name, config['data_path']) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config['backup_path']) or \ (time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD): # noqa shutil.copy(config['data_path'], config['backup_path'])
def save(config, data): """Save data and create backup, creating a new data file if necessary.""" create_dir(os.path.dirname(config['data_path'])) # atomically save by writing to temporary file and moving to destination try: # write to temp file with open(config['tmp_path'], 'w', encoding='utf-8', errors='replace') as f: for path, weight in data.items(): f.write(unico("%s\t%s\n" % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print("Error saving autojump data (disk full?)" % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(config['tmp_path'], config['data_path']) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config['backup_path']) or \ (time() - os.path.getmtime(config['backup_path']) > BACKUP_THRESHOLD): shutil.copy(config['data_path'], config['backup_path'])
def save(config, data): """Save data and create backup, creating a new data file if necessary.""" data_dir = os.path.dirname(config["data_path"]) create_dir(data_dir) # atomically save by writing to temporary file and moving to destination try: temp = NamedTemporaryFile(delete=False, dir=data_dir) # Windows cannot reuse the same open file name temp.close() with open(temp.name, "w", encoding="utf-8", errors="replace") as f: for path, weight in data.items(): weight_with_age = weight * 0.999 slash_only_path = re.sub(re.escape(os.sep), "/", path) f.write(unico("%s\t%s\n" % (weight_with_age, slash_only_path))) f.flush() os.fsync(f) except IOError as ex: print("Error saving autojump data (disk full?)" % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(temp.name, config["data_path"]) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config["backup_path"]) or (time() - os.path.getmtime(config["backup_path"]) > BACKUP_THRESHOLD): # noqa shutil.copy(config["data_path"], config["backup_path"])
def save(config, data): """Save data and create backup, creating a new data file if necessary.""" create_dir(os.path.dirname(config["data_path"])) # atomically save by writing to temporary file and moving to destination try: # write to temp file with open(config["tmp_path"], "w", encoding="utf-8", errors="replace") as f: for path, weight in data.items(): f.write(unico("%s\t%s\n" % (weight, path))) f.flush() os.fsync(f) except IOError as ex: print("Error saving autojump data (disk full?)" % ex, file=sys.stderr) sys.exit(1) # move temp_file -> autojump.txt move_file(config["tmp_path"], config["data_path"]) # create backup file if it doesn't exist or is older than BACKUP_THRESHOLD if not os.path.exists(config["backup_path"]) or ( time() - os.path.getmtime(config["backup_path"]) > BACKUP_THRESHOLD ): shutil.copy(config["data_path"], config["backup_path"])
def test_unico(self): assert_equal(unico(b'blah'), u'blah') assert_equal(unico(b'日本語'), u'日本語') assert_equal(unico(u'でもおれは中国人だ。'), u'でもおれは中国人だ。')
def test_unico(): assert unico(str('blah')) == u('blah') assert unico(str('日本語')) == u('日本語') assert unico(u('でもおれは中国人だ。')) == u('でもおれは中国人だ。')