summaryrefslogtreecommitdiff
path: root/helpers.py
diff options
context:
space:
mode:
authorRichard Wilbur <richard.wilbur@gmail.com>2014-06-02 20:58:47 -0600
committerRichard Wilbur <richard.wilbur@gmail.com>2014-06-02 20:58:47 -0600
commit6978da619c1c9dbee6554ffa70484e48b7f1e9d1 (patch)
tree55f02323fb2f16cd6328112f7be61e402b0f74c2 /helpers.py
parentb7b3d7f360c9f8c0d7315aae56bca63ad9f48411 (diff)
parentb7e626f15fd3059686b8925b4e854568519fd92c (diff)
downloadbzr-fastimport-trunk.tar.gz
Fix bzr-fastimport when used with newer versions of python-fastimport.(Jelmer Vernooij)HEADtrunk
Diffstat (limited to 'helpers.py')
-rw-r--r--helpers.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/helpers.py b/helpers.py
index fed4d68..6bba6fd 100644
--- a/helpers.py
+++ b/helpers.py
@@ -146,3 +146,53 @@ def mode_to_kind(mode):
return 'tree-reference', False
else:
raise AssertionError("invalid mode %o" % mode)
+
+
+def binary_stream(stream):
+ """Ensure a stream is binary on Windows.
+
+ :return: the stream
+ """
+ try:
+ import os
+ if os.name == 'nt':
+ fileno = getattr(stream, 'fileno', None)
+ if fileno:
+ no = fileno()
+ if no >= 0: # -1 means we're working as subprocess
+ import msvcrt
+ msvcrt.setmode(no, os.O_BINARY)
+ except ImportError:
+ pass
+ return stream
+
+
+def single_plural(n, single, plural):
+ """Return a single or plural form of a noun based on number."""
+ if n == 1:
+ return single
+ else:
+ return plural
+
+
+def invert_dictset(d):
+ """Invert a dictionary with keys matching a set of values, turned into lists."""
+ # Based on recipe from ASPN
+ result = {}
+ for k, c in d.iteritems():
+ for v in c:
+ keys = result.setdefault(v, [])
+ keys.append(k)
+ return result
+
+
+def invert_dict(d):
+ """Invert a dictionary with keys matching each value turned into a list."""
+ # Based on recipe from ASPN
+ result = {}
+ for k, v in d.iteritems():
+ keys = result.setdefault(v, [])
+ keys.append(k)
+ return result
+
+