~timo-jyrinki/ubuntu/trusty/pitivi/backport_utopic_fixes

« back to all changes in this revision

Viewing changes to pitivi/utils/misc.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2014-03-29 15:22:50 UTC
  • mto: (3.1.23 experimental)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20140329152250-flg9onx416bqf3e3
Tags: upstream-0.93
Import upstream version 0.93

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Pitivi video editor
2
2
#
3
 
#       utils.py
 
3
#       pitivi/utils/misc.py
4
4
#
5
5
# Copyright (c) 2005, Edward Hervey <bilboed@bilboed.com>
6
6
# Copyright (c) 2009, Alessandro Decina <alessandro.d@gmail.com>
27
27
from gi.repository import GObject
28
28
from gi.repository import Gst
29
29
from gi.repository import Gtk
30
 
from urllib import quote, unquote
31
 
from urlparse import urlsplit, urlunsplit, urlparse
 
30
from urllib import unquote
 
31
from urlparse import urlsplit, urlparse
 
32
import bisect
32
33
import hashlib
33
34
import os
34
35
import struct
60
61
    return (a <= b) and (b <= c)
61
62
 
62
63
 
63
 
def print_ns(time):
 
64
def format_ns(time):
 
65
    if time is None:
 
66
        return None
64
67
    if time == Gst.CLOCK_TIME_NONE:
65
68
        return "CLOCK_TIME_NONE"
66
69
 
98
101
    return False
99
102
 
100
103
 
101
 
def in_devel():
102
 
    """
103
 
    Returns True if the current Pitivi instance is run from a git checkout
104
 
    """
105
 
    try:
106
 
        # This code is the same as in the configure files
107
 
        root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
108
 
        return os.path.exists(os.path.join(root_dir, '.git'))
109
 
    except:
110
 
        return False
111
 
 
112
 
 
113
104
#------------------------------ URI helpers   --------------------------------#
114
105
def isWritable(path):
115
106
    """Check if the file/path is writable"""
124
115
        # open(path, "rw") will IOError if the file doesn't already exist.
125
116
        # Therefore, simply check the directory permissions instead:
126
117
        return os.access(os.path.dirname(path), os.W_OK)
127
 
    return True
128
118
 
129
119
 
130
120
def uri_is_valid(uri):
133
123
    Will also check if the size is valid (> 0).
134
124
 
135
125
    @param uri: The location to check
136
 
    @type uri: C{URI}
 
126
    @type uri: C{str}
137
127
    """
138
128
    return (Gst.uri_is_valid(uri) and
139
129
            Gst.uri_get_protocol(uri) == "file" and
144
134
    """ Check whether the given uri is reachable by GStreamer.
145
135
 
146
136
    @param uri: The location to check
147
 
    @type uri: C{URI}
 
137
    @type uri: C{str}
148
138
    @return: C{True} if the uri is reachable.
149
139
    @rtype: C{bool}
150
140
    """
160
150
    return sys.getfilesystemencoding() or "utf-8"
161
151
 
162
152
 
163
 
def path_from_uri(uri):
164
 
    """
165
 
    Return a human-readable path that can be used with python's os.path
166
 
    """
167
 
    foo = urlparse(uri)
168
 
    path = foo.netloc + foo.path
169
 
    return unquote(path)
 
153
def path_from_uri(raw_uri):
 
154
    """
 
155
    Return a path that can be used with Python's os.path.
 
156
    """
 
157
    uri = urlparse(raw_uri)
 
158
    assert uri.scheme == "file"
 
159
    return unquote(uri.path)
170
160
 
171
161
 
172
162
def filename_from_uri(uri):
258
248
 
259
249
def linkDynamic(element, target):
260
250
 
261
 
    def pad_added(bin, pad, target):
 
251
    def pad_added(unused_bin, pad, target):
262
252
        compatpad = target.get_compatible_pad(pad)
263
253
        if compatpad:
264
254
            pad.link_full(compatpad, Gst.PAD_LINK_CHECK_NOTHING)
337
327
 
338
328
 
339
329
def profile(func, profiler_filename="result.prof"):
340
 
    import os.path
341
330
    counter = 1
342
331
    output_filename = profiler_filename
343
332
    while os.path.exists(output_filename):
344
333
        output_filename = profiler_filename + str(counter)
345
334
        counter += 1
346
335
 
347
 
    def _wrapper(*args, **kwargs):
 
336
    def _wrapper(*unused_args, **kwargs):
348
337
        local_func = func
349
338
        cProfile.runctx("result = local_func(*args, **kwargs)", globals(), locals(),
350
339
                        filename=output_filename)
361
350
    return (input // interval) * interval
362
351
 
363
352
 
364
 
# Python re-implementation of binary search algorithm found here:
365
 
# http://en.wikipedia.org/wiki/Binary_search
366
 
#
367
 
# This is the iterative version without the early termination branch, which
368
 
# also tells us the element of A that are nearest to Value, if the element we
369
 
# want is not found. This is useful for implementing edge snaping in the UI,
370
 
# where we repeatedly search through a list of control points for the one
371
 
# closes to the cursor. Because we don't care whether the cursor position
372
 
# matches the list, this function returns the index of the lement closest to
373
 
# value in the array.
374
 
 
375
 
 
376
 
def binary_search(col, value):
377
 
    low = 0
378
 
    high = len(col)
379
 
    while (low < high):
380
 
        mid = (low + high) / 2
381
 
        if (col[mid] < value):
382
 
            low = mid + 1
383
 
        else:
384
 
            #can't be high = mid-1: here col[mid] >= value,
385
 
            #so high can't be < mid if col[mid] == value
386
 
            high = mid
387
 
    return low
 
353
def binary_search(elements, value):
 
354
    """Returns the index of the element closest to value.
 
355
 
 
356
    @param elements: A sorted list.
 
357
    """
 
358
    if not elements:
 
359
        return -1
 
360
    closest_index = bisect.bisect_left(elements, value, 0, len(elements) - 1)
 
361
    element = elements[closest_index]
 
362
    closest_distance = abs(element - value)
 
363
    if closest_distance == 0:
 
364
        return closest_index
 
365
    for index in (closest_index - 1,):
 
366
        if index < 0:
 
367
            continue
 
368
        distance = abs(elements[index] - value)
 
369
        if closest_distance > distance:
 
370
            closest_index = index
 
371
            closest_distance = distance
 
372
    return closest_index
388
373
 
389
374
 
390
375
def argmax(func, seq):