Miki Tebeka avatar Miki Tebeka committed 09b26af

start of put

Comments (0)

Files changed (1)

webhdfs/__init__.py

 import requests
 import sys
+from os.path import isfile
 
 if sys.version_info[0] > 2:
     from urllib.parse import urlparse, urlencode
         if not query:
             raise WebHDFSError('need to specify at least one of user or group')
 
-        self._op('PUT', path, 'SETOWNER', is_json=False, **query)
+        self._op('PUT', path, 'SETOWNER', is_json=False, query=query)
 
     def read(self, path, offset=0, length=0, buffersize=0):
         # FIXME: Find a way to unite handling of optional parameters
         if buffersize:
             query['buffersize'] = str(buffersize)
 
-        return self._op('GET', path, 'OPEN', is_json=False, **query)
+        return self._op('GET', path, 'OPEN', is_json=False, query=query)
 
+    def put(self, local, path, overwrite=False, blocksize=0, replication=None,
+            permission=0, buffersize=0):
 
+        if not isfile(local):
+            raise WebHDFSError('put error: {} not found'.format(local))
 
-    def _call(self, method, url):
-        resp = requests.request(method, url, allow_redirects=False)
+        query = {'overwrite': 'true' if overwrite else 'false'}
+        if blocksize:
+            query['blocksize'] = str(blocksize)
+        if replication:
+            query['replication'] = str(replication)
+        if permission:
+            query['permission'] = '{:0o}'.format(permission)
+        if buffersize:
+            query['buffersize'] = str(buffersize)
+
+
+
+    def _call(self, method, url, opts):
+        resp = requests.request(method, url, allow_redirects=False, **opts)
         while True:
             if not resp.ok:
                 raise WebHDFSError(resp.reason)
 
         return resp
 
-    def _op(self, method, path, op, getters=None, is_json=True, **query):
+    def _op(self, method, path, op, getters=None, is_json=False, query=None,
+            opts=None):
+        opts = opts or {}
+
         url = '{}{}?op={}'.format(self.base_url, path, op)
 
         if self.user:
         if query:
             url += '&{}'.format(urlencode(query))
 
-        resp = self._call(method, url)
+        resp = self._call(method, url, opts)
         if not resp.ok:
             raise WebHDFSError
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.