Skip to content

Instantly share code, notes, and snippets.

@pdxjohnny
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdxjohnny/a92009a68d8217256867 to your computer and use it in GitHub Desktop.
Save pdxjohnny/a92009a68d8217256867 to your computer and use it in GitHub Desktop.
PyFileSystem save to tmp file before remote write
"""
sanitize is a colletion of database functions to
abtract file storage and other actions
"""
import sys
import uuid
from fs.opener import fsopendir
# Custom modules
import extra.config as config
FS = config.section("general")["file_system"]
TMP = config.section("general")["tmp_system"]
BASE_DIR = config.section("general")["base_dir"]
def create_and_open(file_path, fs_system=FS, **kwargs):
file_system = fsopendir(fs_system)
if file_path[0] == '/':
file_path = file_path[1:]
if not file_path.split('/')[0] == BASE_DIR:
file_path = BASE_DIR + '/' + file_path
file_dirs = '/' + '/'.join(file_path.split('/')[:-1])
if not file_system.isdir(file_dirs):
file_system.makedir(file_dirs, recursive=True)
return file_system.open(file_path, **kwargs)
def path(file_contents=False):
file_name = unicode(uuid.uuid4())
if file_contents:
file_handle = open_file(file_name, mode="wb")
file_handle.write(file_contents)
file_handle.close()
return file_name
def contents(file_path, file_obj=False):
file_handle = open_file(file_path, mode="rb")
lines = ""
for line in file_handle:
if file_obj:
file_obj.write(line)
else:
lines += line
file_handle.close()
return lines
class open_file(object):
def __init__(self, file_path, **kwargs):
self.path = file_path
self.kwargs = kwargs
if "w" in self.kwargs["mode"]:
self.tmp_file = create_and_open(self.path, fs_system=TMP, \
**self.kwargs)
else:
self.final_file = create_and_open(self.path, fs_system=FS, \
**self.kwargs)
def write(self, data):
return self.tmp_file.write(data)
def __iter__(self):
return iter(self.final_file)
def read(self):
return self.final_file.read()
def readline(self):
return self.final_file.readline()
def close(self):
if "w" in self.kwargs["mode"]:
self.tmp_file.close()
self.tmp_file = create_and_open(self.path, fs_system=TMP, \
mode="rb")
self.final_file = create_and_open(self.path, fs_system=FS, \
mode="wb")
for line in self.tmp_file:
self.final_file.write(line)
self.tmp_file.close()
self.final_file.close()
else:
self.final_file.close()
return True
def main():
file_path = path()
log_file = open_file(file_path, mode="wb")
lines_iterator = iter(sys.stdin.readline, b"")
for line in lines_iterator:
log_file.write(line)
log_file.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment