Commit aead0f51 authored by Lucas Carvalho's avatar Lucas Carvalho

Removed useless file.

It has been moved to slapos.tool.libnetworkcache.
parent fdfe33ca
##############################################################################
#
# Copyright (c) 2010 ViFiB SARL and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import httplib
import os
class NetworkcacheClient(object):
'''
NetworkcacheClient is a wrapper for httplib.
It must implement all the required methods to use the Networkcache HTTP
Server.
- put(key, file)
- get(key)
- delete(key)
'''
def __init__(self, networkcache_url):
# XXX (lucas): Is it required to check if networkcache_url is a valid URL?
self.networkcache_url = networkcache_url
def _start(self):
self.connection = httplib.HTTPConnection(self.networkcache_url)
def _close(self):
self.connection.close()
def put(self, key, file_path):
'''
Upload the file to the server.
It uses http PUT resquest method.
'''
if not os.path.exists(file_path):
raise ValueError, 'File does not exists. %s' % file_path
f = open(file_path, 'rb')
try:
file_content = f.read()
except:
f.close()
path_info = '/%s' % key
self._start()
try:
self.connection.request('PUT', path_info, file_content)
result = self.connection.getresponse()
finally:
self._close()
return result
def get(self, key, file_path):
'''
Download the file.
It uses http GET request method.
'''
path_info = '/%s' % key
self._start()
try:
self.connection.request('GET', path_info)
result = self.connection.getresponse()
finally:
self._close()
# save file
if os.path.isdir(file_path):
file_path = os.path.join(file_path, key)
f = open(file_path, 'w+')
try:
f.write(result.read())
except:
f.close()
return result, file_path
def delete(self, key):
'''
Delete the file.
It uses http DELETE request method.
'''
path_info = '/%s' % key
self._start()
try:
self.connection.request('DELETE', path_info)
result = self.connection.getresponse()
finally:
self._close()
return result.read(), result.getheaders()
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