Commits

Yonggang Luo committed 016f11e

utils: add {save,load}_string since this is a fairly common idiom

Comments (0)

Files changed (1)

hgsubversion/util.py

         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):