Source

python-bitbucket / api.py

Diff from to

File api.py

 
 class BBFile(object):
     # Additional functionality will be added to this class in the future.  For now it is a placeholder class.
-    def __init__(self, data):
+    def __init__(self, api, username, repository, rev, data):
+        self.url = 'repositories/%s/%s/raw/%s/%s' % (username, repository, rev, data['file'])
         # Python variables "type" and "file" are reserved, and incompatible with the BB API ones.
         # Any suggestions on other names are welcome, but I believe "action" and "filename" suit it well.
         self.action = data['type']
         self.filename = data['file']
+        self.api = api
+        self._handler = None
     
     def __repr__(self):
         return "<BBFile: %s>" % self.filename
+    
+    def __getattr__(self, name):
+        if name == 'handler':
+	    if self._handler == None:
+	        self._handler = self.api.get_raw(self.url)
+	    return self._handler
+	raise AttributeError
+
+    def read(self, size=-1):
+        return self.handler.read(size)
+    
+    def readline(self, size=-1):
+        return self.handler.readline(size)
 
 class Changeset(object):
-    def __init__(self, data):
+    def __init__(self, api, username, repository, data):
         for i in data:
 	    if i is not ['files', 'timestamp']:
 	        setattr(self, i, data[i])
 	self.timestamp = datetime.datetime.strptime(data['timestamp'], "%Y-%m-%d %H:%M:%S")
 	self.files = []
 	for f in data['files']:
-	    self.files.append(BBFile(f))
+	    self.files.append(BBFile(api, username, repository, data['node'], f))
 
     def __repr__(self):
         return "<Changeset: %s>" % self.node
             opener = urllib2.build_opener(cookie_handler)
         return opener
 
-    def _request(self, url, data=None):
+    def _raw_request(self, url, data=None):
         query_url = self.api_url + url
         if data:
 	  data = urlencode(data)
 	except urllib2.HTTPError, e:
 	    print e.headers
 	    raise e
-	return json.load(handler)
+	return handler
+
+    def _request(self, url, data=None):
+	return json.load(self._raw_request(url, data))
 
     def post(self, url, data):
         return self._request(url, data)
     def get(self, url):
         return self._request(url, None)
     
+    def get_raw(self, url):
+        return self._raw_request(url, None)
+    
     def get_issues(self, username, repository):
         json = self.get('repositories/%s/%s/issues/' % (username, repository))
         issues = []
         json = self.get('repositories/%s/%s/changesets?limit=%s' % (username, repository, limit))
         changesets = []
         for i in json['changesets']:
-	    changesets.append(Changeset(i))
+	    changesets.append(Changeset(self, username, repository, i))
 	return changesets
 
     def get_changeset(self, username, repository, changeset):
         json = self.get('repositories/%s/%s/changesets/%s' % (username, repository, changeset))
-        return Changeset(json)
+        return Changeset(self, username, repository, json)
+
+    def get_file(self, username, repository, filename, revision="tip"):
+        return BBFile(self, username, repository, revision, {'type':'source', 'file':filename})