~unit193/bzr-fastimport/deletion-fixes

« back to all changes in this revision

Viewing changes to helpers.py

  • Committer: Richard Wilbur
  • Date: 2014-06-03 02:58:47 UTC
  • mfrom: (359.1.1 trunk)
  • Revision ID: richard.wilbur@gmail.com-20140603025847-jotiggy437v5jx9a
Fix bzr-fastimport when used with newer versions of python-fastimport.(Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
146
146
        return 'tree-reference', False
147
147
    else:
148
148
        raise AssertionError("invalid mode %o" % mode)
 
149
 
 
150
 
 
151
def binary_stream(stream):
 
152
    """Ensure a stream is binary on Windows.
 
153
 
 
154
    :return: the stream
 
155
    """
 
156
    try:
 
157
        import os
 
158
        if os.name == 'nt':
 
159
            fileno = getattr(stream, 'fileno', None)
 
160
            if fileno:
 
161
                no = fileno()
 
162
                if no >= 0:     # -1 means we're working as subprocess
 
163
                    import msvcrt
 
164
                    msvcrt.setmode(no, os.O_BINARY)
 
165
    except ImportError:
 
166
        pass
 
167
    return stream
 
168
 
 
169
 
 
170
def single_plural(n, single, plural):
 
171
    """Return a single or plural form of a noun based on number."""
 
172
    if n == 1:
 
173
        return single
 
174
    else:
 
175
        return plural
 
176
 
 
177
 
 
178
def invert_dictset(d):
 
179
    """Invert a dictionary with keys matching a set of values, turned into lists."""
 
180
    # Based on recipe from ASPN
 
181
    result = {}
 
182
    for k, c in d.iteritems():
 
183
        for v in c:
 
184
            keys = result.setdefault(v, [])
 
185
            keys.append(k)
 
186
    return result
 
187
 
 
188
 
 
189
def invert_dict(d):
 
190
    """Invert a dictionary with keys matching each value turned into a list."""
 
191
    # Based on recipe from ASPN
 
192
    result = {}
 
193
    for k, v in d.iteritems():
 
194
        keys = result.setdefault(v, [])
 
195
        keys.append(k)
 
196
    return result
 
197
 
 
198