changeset 828:016f11e5f189

utils: add {save,load}_string since this is a fairly common idiom
author Yonggang Luo <luoyonggang@gmail.com>
date Fri, 30 Sep 2011 21:35:36 +0800
parents 076ded084655
children 5061640fe5bc
files hgsubversion/util.py
diffstat 1 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/util.py
+++ b/hgsubversion/util.py
@@ -99,6 +99,29 @@ def normalize_url(url):
         url = '%s#%s' % (url, checkout)
     return url
 
+
+def load_string(file_path, default=None, limit=1024):
+    if not os.path.exists(file_path):
+        return default
+    try:
+        f = open(file_path, 'r')
+        ret = f.read(limit)
+        f.close()
+    except:
+        return default
+    if ret == '':
+        return default
+    return ret
+
+
+def save_string(file_path, string):
+    if string is None:
+        string = ""
+    f = open(file_path, 'wb')
+    f.write(str(string))
+    f.close()
+
+
 # TODO remove when we drop 1.3 support
 def progress(ui, *args, **kwargs):
     if getattr(ui, 'progress', False):