You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6 KiB

import os
import subprocess
import tempfile
# files i/o
def check_file(path, fail=True):
if fail:
if not os.path.exists(path):
raise IOError("File does not exist: {}.".format(path))
if not os.path.isfile(path):
raise IOError("{} is not a file.".format(path))
return True
else:
return os.path.exists(path) and os.path.isfile(path)
def check_directory(path, fail=True):
if fail:
if not os.path.exists(path):
raise IOError("File does not exist: {}.".format(path))
if not os.path.isdir(path):
raise IOError("{} is not a directory.".format(path))
return True
else:
return os.path.exists(path) and os.path.isdir(path)
def read_file(filepath):
check_file(filepath)
with open(filepath, 'r') as f:
s = f.read()
return s
def write_file(filepath, data):
check_directory(os.path.dirname(filepath))
with open(filepath, 'w') as f:
f.write(data)
# dealing with formatless content
def get_content(path):
"""Will be useful when we need to get content from url"""
return read_file(path)
def move_content(source, target, overwrite = False):
if source == target:
return
if not overwrite and os.path.exists(target):
raise IOError('target file exists')
shutil.move(source, target)
def copy_content(source, target, overwrite = False):
if source == target:
return
if not overwrite and os.path.exists(target):
raise IOError('target file exists')
shutil.copy(source, target)
# editor input
def editor_input(editor, initial="", suffix=None):
"""Use an editor to get input"""
if suffix is None:
suffix = '.tmp'
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file:
tfile_name = temp_file.name
temp_file.write(initial)
temp_file.flush()
cmd = editor.split() # this enable editor command with option, e.g. gvim -f
cmd.append(tfile_name)
subprocess.call(cmd)
with open(tfile_name) as temp_file:
content = temp_file.read()
os.remove(tfile_name)
return content
def edit_file(editor, path_to_file, temporary=True):
if temporary:
check_file(path_to_file, fail=True)
with open(path_to_file) as f:
content = f.read()
content = editor_input(editor, content)
with open(path_to_file, 'w') as f:
f.write(content)
else:
cmd = editor.split() # this enable editor command with option, e.g. gvim -f
cmd.append(path_to_file)
subprocess.call(cmd)