Commits

Miki Tebeka  committed 47608aa

chmod

  • Participants
  • Parent commits 0a9f2b0

Comments (0)

Files changed (1)

File webhdfs/__init__.py

 import sys
 
 if sys.version_info[0] > 2:
-    from urllib.parse import urlparse
+    from urllib.parse import urlparse, urlencode
 else:
     from urlparse import urlparse
+    from urllib import urlencode
 
 HOST, PORT = 'localhost', 50070
 
     pass
 
 class WebHDFS(object):
-    def __init__(self, host=HOST, port=PORT):
+    def __init__(self, host=HOST, port=PORT, **kw):
         self.host, self.port = host, port
         self.base_url = self._gen_base(self.host, self.port)
+        self.user = kw.get('user')
 
     def listdir(self, path):
         return self._op('GET', path, 'LISTSTATUS', ['FileStatuses', 'FileStatus'])
     def checksum(self, path):
         return self._op('GET', path, 'GETFILECHECKSUM', ['FileChecksum'])
 
+    def home(self):
+        return self._op('GET', '/', 'GETHOMEDIRECTORY')
+
+    def chmod(self, path, mode):
+        return self._op('PUT', path, 'SETPERMISSION',
+                        permission='{:o}'.format(mode))
+
     def _call(self, method, url):
         resp = requests.request(method, url, allow_redirects=False)
         while True:
 
         return resp
 
-    def _op(self, method, path, op, getters=None):
+    def _op(self, method, path, op, getters=None, **query):
         url = '{}{}?op={}'.format(self.base_url, path, op)
 
+        if self.user:
+            url += '&user.name={}'.format(self.user)
+
+        if query:
+            url += '&{}'.format(urlencode(query))
+
         resp = self._call(method, url)
         if not resp.ok:
             raise WebHDFSError
 
 
-        reply = resp.json()
+        reply = resp.json() if resp.content else {}
         for key in (getters or []):
             reply = reply[key]