Kevin Veroneau avatar Kevin Veroneau committed e5df90d

Added new get_repository/get_repositories functions to list all available repos. Also added example on usage.

Comments (0)

Files changed (2)

 from urllib import urlencode
 import datetime
 
+class Repository(object):
+    name = None
+    scm = "hg"
+    is_private = False
+    def __init__(self, api, username, repository, data=None):
+        self.api = api
+        self.url = 'repositories/%s/%s/' % (username, repository)
+        if data is not None:
+	    for i in data:
+	        if i not in ['last_updated', 'created_on']:
+	            setattr(self, i, data[i])
+	        else:
+		    setattr(self, i, datetime.datetime.strptime(data[i], "%Y-%m-%d %H:%M:%S"))
+
+    def __repr__(self):
+        return "<Repository: %s>" % self.name
+
+    def get_issues(self):
+        if self.has_issues:
+	    return self.api.get_issues(self.owner, self.slug)
+
+    def get_changesets(self, limit=15):
+        return self.api.get_changesets(self.owner, self.slug, limit)
+
+    def get_changeset(self, changeset):
+        return self.api.get_changesets(self.owner, self.slug, changeset)
+    
+    def get_file(self, filename, revision="tip"):
+        return self.api.get_file(self.owner, self.slug, filename, revision)
+    
+    def new_issue(self):
+        return self.api.new_issue(self.owner, self.slug)
+
 class BBFile(object):
     # Additional functionality will be added to this class in the future.  For now it is a placeholder class.
     def __init__(self, api, username, repository, rev, data):
 class Changeset(object):
     def __init__(self, api, username, repository, data):
         for i in data:
-	    if i is not ['files', 'timestamp']:
+	    if i not in ['files', 'timestamp']:
 	        setattr(self, i, data[i])
 	self.timestamp = datetime.datetime.strptime(data['timestamp'], "%Y-%m-%d %H:%M:%S")
 	self.files = []
 	    self.component = data['metadata']['component']
 	    self.milestone = data['metadata']['milestone']
 	    self.version = data['metadata']['version']
-	    self.responsible = data['responsible']['username']
+	    try:
+	        self.responsible = data['responsible']['username']
+	    except KeyError:
+	        self.responsible = None
 	    self.priority = data['priority']
 	    self.status = data['status']
 	    self.kind = data['metadata']['kind']
 
     def get_file(self, username, repository, filename, revision="tip"):
         return BBFile(self, username, repository, revision, {'type':'source', 'file':filename})
+
+    def get_repository(self, username, repository):
+        json = self.get('repositories/%s/%s/' % (username, repository))
+        return Repository(self, username, repository, json)
+
+    def get_repositories(self, username=None):
+        repos = []
+        if username == None:
+	    json = self.get('user/repositories/')
+	    for repo in json:
+	        repos.append(self.get_repository(repo['owner'], repo['slug']))
+	else:
+	    repo_list = self.get('users/%s/' % username)['repositories']
+	    for repo in repo_list:
+	        repos.append(self.get_repository(username, repo['slug']))
+	return repos

examples/get_repository.py

+from api import API
+import datetime
+
+api = API("kveroneau", "**password**")
+repos = api.get_repositories()
+
+for repo in repos:
+  print "Name: %s" % repo.name
+  print "Owner: %s" % repo.owner
+  print "Website: %s" % repo.website
+  print "Description: %s" % repo.description
+  print "Created on: %s" % datetime.datetime.strftime(repo.created_on, "%c")
+  print "Language: %s" % repo.language
+  print "SCM: %s" % repo.scm
+  for issue in repo.get_issues():
+    # Yes, this works too!
+    print "Issue title: %s" % issue.title
+    print "Issue priority: %s" % issue.priority
+    print "Issue content:\n%s\n\n" % issue.content
+  for change in repo.get_changesets(limit=5):
+    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 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.