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
103
Returns True if the current Pitivi instance is run from a git checkout
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'))
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)
130
120
def uri_is_valid(uri):
160
150
return sys.getfilesystemencoding() or "utf-8"
163
def path_from_uri(uri):
165
Return a human-readable path that can be used with python's os.path
168
path = foo.netloc + foo.path
153
def path_from_uri(raw_uri):
155
Return a path that can be used with Python's os.path.
157
uri = urlparse(raw_uri)
158
assert uri.scheme == "file"
159
return unquote(uri.path)
172
162
def filename_from_uri(uri):
259
249
def linkDynamic(element, target):
261
def pad_added(bin, pad, target):
251
def pad_added(unused_bin, pad, target):
262
252
compatpad = target.get_compatible_pad(pad)
264
254
pad.link_full(compatpad, Gst.PAD_LINK_CHECK_NOTHING)
339
329
def profile(func, profiler_filename="result.prof"):
342
331
output_filename = profiler_filename
343
332
while os.path.exists(output_filename):
344
333
output_filename = profiler_filename + str(counter)
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
364
# Python re-implementation of binary search algorithm found here:
365
# http://en.wikipedia.org/wiki/Binary_search
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.
376
def binary_search(col, value):
380
mid = (low + high) / 2
381
if (col[mid] < value):
384
#can't be high = mid-1: here col[mid] >= value,
385
#so high can't be < mid if col[mid] == value
353
def binary_search(elements, value):
354
"""Returns the index of the element closest to value.
356
@param elements: A sorted list.
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:
365
for index in (closest_index - 1,):
368
distance = abs(elements[index] - value)
369
if closest_distance > distance:
370
closest_index = index
371
closest_distance = distance
390
375
def argmax(func, seq):