Commit 3614c2e5 authored by Lucas Carvalho's avatar Lucas Carvalho

Following API definition.

The select method  must return the file from shacache.
And the download method should return the file content instead of path.
parent 123d209d
...@@ -18,6 +18,7 @@ import hashlib ...@@ -18,6 +18,7 @@ import hashlib
import httplib import httplib
import json import json
import os import os
import tempfile
from urllib import urlretrieve from urllib import urlretrieve
from urlparse import urlparse from urlparse import urlparse
...@@ -136,16 +137,22 @@ class NetworkcacheClient(object): ...@@ -136,16 +137,22 @@ class NetworkcacheClient(object):
return json.loads(data) return json.loads(data)
def download(self, path, sha512sum): def download(self, sha512sum):
''' Download the file. ''' Download the file.
It uses http GET request method. It uses http GET request method.
''' '''
sha_cache_url = os.path.join(self.shacache_url, sha512sum) sha_cache_url = os.path.join(self.shacache_url, sha512sum)
path, headers = urlretrieve(sha_cache_url, path) path, headers = urlretrieve(sha_cache_url, tempfile.mktemp())
return headers f = open(path)
try:
file_content = f.read()
finally:
f.close()
return file_content
def select(self, directory_key=None): def select(self, directory_key=None):
'''Download a file from shacache by selecting the entry in shadir ''' Download a file from shacache by selecting the entry in shadir
Raise DirectoryNotFound if multiple files are found. Raise DirectoryNotFound if multiple files are found.
''' '''
path_info = self.shadir_path path_info = self.shadir_path
...@@ -165,14 +172,12 @@ class NetworkcacheClient(object): ...@@ -165,14 +172,12 @@ class NetworkcacheClient(object):
data_list = json.loads(data) data_list = json.loads(data)
if len(data_list) > 1: if len(data_list) > 1:
raise MultipleFileFoundError('Too many entries for a given directory. ' \ raise DirectoryNotFound('Too many entries for a given directory. ' \
'Directory: %s. Entries: %s.' % (directory_key, str(data_list))) 'Directory: %s. Entries: %s.' % (directory_key, str(data_list)))
return data_list information_dict, signature = data_list[0]
sha512 = information_dict.get('sha512')
return self.download(sha512)
class MultipleFileFoundError(Exception):
pass
class DirectoryNotFound(Exception): class DirectoryNotFound(Exception):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment