from __future__ import division
-import numpy
+import numpy as np
class Noise(object):
"""A class that mimics audiolab.sndfile but generates noise instead of reading
else:
will_read = frames_to_read
self.seekpoint += will_read
- return numpy.random.random(will_read)*2 - 1
+ return np.random.random(will_read)*2 - 1
def path2uri(path):
return urlparse.urljoin('file:', urllib.pathname2url(path))
+def source_info(source):
+ import os.path
+
+ src_info = {'is_file': False,
+ 'uri': '',
+ 'pathname': ''}
+
+ if os.path.exists(source):
+ src_info['is_file'] = True
+ # get the absolute path
+ src_info['pathname'] = os.path.abspath(source)
+ # and make a uri of it
+ src_info['uri'] = path2uri(src_info['pathname'])
+ return src_info
+
+
def get_uri(source):
"""
Check a media source as a valid file or uri and return the proper uri
"""
import gst
- # Is this an valid URI source
- if gst.uri_is_valid(source):
+
+ src_info = source_info(source)
+
+ if src_info['is_file']: # Is this a file?
+ return get_uri(src_info['uri'])
+
+ elif gst.uri_is_valid(source): # Is this a valid URI source for Gstreamer
uri_protocol = gst.uri_get_protocol(source)
if gst.uri_protocol_is_supported(gst.URI_SRC, uri_protocol):
return source
else:
raise IOError('Invalid URI source for Gstreamer')
-
- # is this a file?
- import os.path
- if os.path.exists(source):
- # get the absolute path
- pathname = os.path.abspath(source)
- # and make a uri of it
- uri = path2uri(pathname)
-
- return get_uri(uri)
else:
- raise IOError('Failed getting uri for path %s: not such file or directoy' % source)
+ raise IOError('Failed getting uri for path %s: no such file' % source)
- return uri
def get_media_uri_info(uri):
return wrapper
+def get_sha1(source):
+ src_info = source_info(source)
+
+ if src_info['is_file']: # Is this a file?
+ return sha1sum_file(src_info['pathname'])
+ else: # Then it should be an url
+ return sha1sum_url(source)
+
+
+def sha1sum_file(filename):
+ '''
+ Return the secure hash digest with sha1 algorithm for a given file
+
+ >>> print sha1sum_file('../../tests/samples/guitar.wav')
+ 08301c3f9a8d60926f31e253825cc74263e52ad1
+ '''
+ import hashlib
+ import io
+
+ sha1 = hashlib.sha1()
+ chunk_size = sha1.block_size * io.DEFAULT_BUFFER_SIZE
+
+ with open(filename, 'rb') as f:
+ for chunk in iter(lambda: f.read(chunk_size), b''):
+ sha1.update(chunk)
+ return sha1.hexdigest()
+
+
+def sha1sum_url(url):
+ '''Return the secure hash digest with sha1 algorithm for a given url
+
+ >>> url = 'https://github.com/yomguy/timeside-samples/raw/master/samples/guitar.wav'
+ >>> print sha1sum_url(url)
+ 08301c3f9a8d60926f31e253825cc74263e52ad1
+ >>> uri = get_uri('../../tests/samples/guitar.wav')
+ >>> print sha1sum_url(uri)
+ 08301c3f9a8d60926f31e253825cc74263e52ad1
+
+ '''
+ import hashlib
+ import urllib
+ from contextlib import closing
+
+ sha1 = hashlib.sha1()
+ chunk_size = sha1.block_size * 8192
+
+ max_file_size = 10*1024*1024 # 10Mo limit in case of very large file
+
+ total_read = 0
+ with closing(urllib.urlopen(url)) as url_obj:
+ for chunk in iter(lambda: url_obj.read(chunk_size), b''):
+ sha1.update(chunk)
+ total_read += chunk_size
+ if total_read > max_file_size:
+ break
+
+ return sha1.hexdigest()
+
+
+def sha1sum_numpy(np_array):
+ '''
+ Return the secure hash digest with sha1 algorithm for a numpy array
+ '''
+ import hashlib
+ return hashlib.sha1(np_array.view(np.uint8)).hexdigest()
+
+
if __name__ == "__main__":
import doctest
doctest.testmod()