Kevin Veroneau avatar Kevin Veroneau committed fab8d21

Improved on BBFile object, now works like a file-object(mostly). Added more examples on how to use the API.

Comments (0)

Files changed (3)

 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
+    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})


+from api import API
+api = API("kveroneau", "**password**")
+change = api.get_changeset("kveroneau", "python-bitbucket", changeset="fa0d7c04593f")
+print change.files[0].read()
+# You can even do this:
+print api.get_file("kveroneau", "python-bitbucket", "examples/").read()
+# You are also able to fetch a specific revision/changeset:
+print api.get_file("kveroneau", "python-bitbucket", "", revision="298102b6aa12").read()


+from api import API
+import datetime
+api = API("kveroneau", "**password**")
+cs = api.get_changesets("kveroneau", "python-bitbucket", limit=10)
+for change in cs:
+  print "Revision/Node: %d:%s" % (change.revision, change.node)
+  # Since change.timestamp is a datetime object, we can use formatting on it.
+  print "Timestamp: %s" % datetime.datetime.strftime(change.timestamp, "%c")
+  print "Commit message:\n%s" % change.message
+  print "Affected files: %s" % len(change.files)
+  for f in change.files:
+    print f.filename
+  print "\n"
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
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.