aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJauhien Piatlicki (jauhien) <piatlicki@gmail.com>2013-07-01 22:14:03 +0200
committerJauhien Piatlicki (jauhien) <piatlicki@gmail.com>2013-07-01 22:14:03 +0200
commit6faf8d5ab5dc16f193acc1d344bf473e1eaa4808 (patch)
treef372b03cb4937457f8462b7b773ca464d5c809cb /g_sorcery/fileutils.py
parentexecutable and the main module added (diff)
downloadg-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.tar.gz
g-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.tar.bz2
g-sorcery-6faf8d5ab5dc16f193acc1d344bf473e1eaa4808.zip
g_sorcery/fileutils: file utilities moved to separate module
Diffstat (limited to 'g_sorcery/fileutils.py')
-rw-r--r--g_sorcery/fileutils.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/g_sorcery/fileutils.py b/g_sorcery/fileutils.py
new file mode 100644
index 0000000..1920495
--- /dev/null
+++ b/g_sorcery/fileutils.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+ fileutils.py
+ ~~~~~~~~~~~~
+
+ file utilities
+
+ :copyright: (c) 2013 by Jauhien Piatlicki
+ :license: GPL-2, see LICENSE for more details.
+"""
+
+import json, os, shutil
+
+from .exceptions import FileJSONError
+
+class FileJSON:
+ def __init__(self, directory, name, mandatories):
+ """
+ Initialize
+
+ mandatories -- list of mandatory keys
+ """
+ self.directory = os.path.abspath(directory)
+ self.name = name
+ self.path = os.path.join(directory, name)
+ self.mandatories = mandatories
+
+ def read(self):
+ if not os.path.exists(self.directory):
+ os.makedirs(self.directory)
+ content = {}
+ if not os.path.isfile(self.path):
+ for key in self.mandatories:
+ content[key] = ""
+ with open(self.path, 'w') as f:
+ json.dump(content, f, indent=2, sort_keys=True)
+ else:
+ with open(self.path, 'r') as f:
+ content = json.load(f)
+ for key in self.mandatories:
+ if not key in content:
+ raise FileJSONError('lack of mandatory key: ' + key)
+ return content
+
+ def write(self, content):
+ for key in self.mandatories:
+ if not key in content:
+ raise FileJSONError('lack of mandatory key: ' + key)
+ if not os.path.exists(self.directory):
+ os.makedirs(self.directory)
+ with open(self.path, 'w') as f:
+ json.dump(content, f, indent=2, sort_keys=True)
+
+
+def hash_file(name, hasher, blocksize=65536):
+ with open(name, 'rb') as f:
+ buf = f.read(blocksize)
+ while len(buf) > 0:
+ hasher.update(buf)
+ buf = f.read(blocksize)
+ return hasher.hexdigest()
+
+def copy_all(src, dst):
+ for f_name in os.listdir(src):
+ src_name = os.path.join(src, f_name)
+ dst_name = os.path.join(dst, f_name)
+ if os.path.isdir(src_name):
+ shutil.copytree(src_name, dst_name)
+ else:
+ shutil.copy2(src_name, dst_name)