Skip to content

Instantly share code, notes, and snippets.

@madjar
Created March 15, 2012 09:27
Show Gist options
  • Save madjar/2043164 to your computer and use it in GitHub Desktop.
Save madjar/2043164 to your computer and use it in GitHub Desktop.
Automatically extract pseudo-sqlalchemy objects from the github api doc
import requests, re
result = requests.get('https://raw.github.com/github/developer.github.com/master/content/v3/events/types.md')
content = result.content
for event in re.findall('## [^#]*', content):
blocks = event.split('\n\n')
event_name = blocks[0][3:]
if 'Hook name' in blocks[1]:
description = ''
start = 2
else:
description = blocks[1]
start = 3
print 'class %s(object):\n """%s"""'%(event_name, description)
for att in blocks[start:]:
if not att or 'empty payload' in att:
continue
m = re.match('(.*)\n: \*\*(.*)\*\* - (.*)', att, re.DOTALL)
name, type, comment = m.groups()
name = name.replace(r'\_', '_')
comment = comment.replace('\n', '')
print ' %s = Column(%s) # %s'%(name, type, comment)
print
print
class CommitComment(object):
""""""
comment = Column(object) #The [comment](/v3/repos/commits/
class CreateEvent(object):
"""Represents a created repository, branch, or tag."""
ref_type = Column(string) #The object that was created: "repository", "branch", or"tag"
ref = Column(string) #The git ref (or `null` if only a repository was created).
master_branch = Column(string) #The name of the repository's master branch.
description = Column(string) #The repository's current description.
class DeleteEvent(object):
"""Represents a deleted branch or tag."""
ref_type = Column(string) #The object that was deleted: "branch" or "tag".
ref = Column(string) #The full git ref.
class DownloadEvent(object):
""""""
download = Column(object) #The [download](/v3/repos/downloads/) that was justcreated.
class FollowEvent(object):
""""""
target = Column(object) #The [user](/v3/users) that was just followed.
class ForkEvent(object):
""""""
forkee = Column(object) #The created [repository](/v3/repos/).
class ForkApplyEvent(object):
"""Triggered when a patch is applied in the Fork Queue."""
head = Column(string) #The branch name the patch is applied to.
before = Column(string) #SHA of the repo state before the patch.
after = Column(string) #SHA of the repo state after the patch.
class GistEvent(object):
""""""
action = Column(string) #The action that was performed: "create" or "update"
gist = Column(object) #The [gist](/v3/gists/) itself.
class GollumEvent(object):
""""""
pages = Column(array) #The pages that were updated.
pages[][page_name] = Column(string) #The name of the page.
pages[][title] = Column(string) #The current page title.
pages[][action] = Column(string) #The action that was performed on the page.
pages[][sha] = Column(string) #The latest commit SHA of the page.
pages[][html_url] = Column(string) #Points to the HTML wiki page.
class IssueCommentEvent(object):
""""""
action = Column(string) #The action that was performed on the comment.
issue = Column(object) #The [issue](/v3/issues/) the comment belongs to.
comment = Column(object) #The [comment](/v3/issues/comments/) itself.
class IssuesEvent(object):
""""""
action = Column(string) #The action that was performed: "opened", "closed", or"reopened".
issue = Column(string) #The [issue](/v3/issues) itself.
class MemberEvent(object):
"""Triggered when a user is added as a collaborator to a repository."""
member = Column(object) #The [user](/v3/users/) that was added.
action = Column(string) #The action that was performed: "added".
class PublicEvent(object):
"""This is triggered when a private repo is open sourced. Without a doubt: the
best GitHub event."""
class PullRequestEvent(object):
""""""
action = Column(string) #The action that was performed: "opened", "closed","synchronize", or "reopened".
number = Column(integer) #The pull request number.
pull_request = Column(object) #The [pull request](/v3/pulls) itself.
class PushEvent(object):
""""""
head = Column(string) #The SHA of the HEAD commit on the repository.
ref = Column(string) #The full Git ref that was pushed. Example:"refs/heads/master"
size = Column(integer) #The number of commits in the push.
commits = Column(array) #The list of pushed commits.
commits[][sha] = Column(string) #The SHA of the commit.
commits[][message] = Column(message) #The commit message.
commits[][author] = Column(object) #The git author of the commit.
commits[][author][name] = Column(string) #The git author's name.
commits[][author][email]
: **string** - The git author's email address.
commits[][url] = Column(url) #Points to the commit API resource.
class TeamAddEvent(object):
""""""
team = Column(object) #The [team](/v3/orgs/teams/) that was modified. Note:older events may not include this in the payload.
user = Column(object) #The [user](/v3/users/) that was added to this team.
repo = Column(object) #The [repository](/v3/repos/) that was added to this team.
class WatchEvent(object):
"""The event's actor is the watcher, and the event's repo is the watched
repository."""
action = Column(string) #The action that was performed.
[Finished]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment