Skip to content

Adds support for both size and empty #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 73 additions & 21 deletions gogs_client/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ def json_get(parsed_json, key):
return parsed_json[key]


class GogsUser(object):
class GogsEntity(object):
def __init__(self, json):
self._json = json

@property
def json(self):
return self._json

class GogsUser(GogsEntity):
"""
An immutable representation of a Gogs user
"""

def __init__(self, user_id, username, full_name, email, avatar_url):
def __init__(self, user_id, username, full_name, email, avatar_url, json={}):
super(GogsUser, self).__init__(json=json)
self._id = user_id
self._username = username
self._full_name = full_name
Expand All @@ -33,7 +42,7 @@ def from_json(parsed_json):
email = parsed_json.get("email", None)
avatar_url = parsed_json.get("avatar_url", None)
return GogsUser(user_id=user_id, username=username, full_name=full_name,
email=email, avatar_url=avatar_url)
email=email, avatar_url=avatar_url, json=parsed_json)

@property # named user_id to avoid conflict with built-in id
def user_id(self):
Expand Down Expand Up @@ -81,17 +90,22 @@ def avatar_url(self):
return self._avatar_url


class GogsRepo(object):
class GogsRepo(GogsEntity):
"""
An immutable representation of a Gogs repository
"""

def __init__(self, repo_id, owner, full_name, private, fork, urls, permissions):
def __init__(self, repo_id, owner, full_name, private, fork, default_branch,
empty, size, urls, permissions, json={}):
super(GogsRepo, self).__init__(json=json)
self._repo_id = repo_id
self._owner = owner
self._full_name = full_name
self._private = private
self._fork = fork
self._default_branch = default_branch
self._empty = empty
self._size = size
self._urls = urls
self._permissions = permissions

Expand All @@ -102,11 +116,15 @@ def from_json(parsed_json):
full_name = json_get(parsed_json, "full_name")
private = json_get(parsed_json, "private")
fork = json_get(parsed_json, "fork")
default_branch = json_get(parsed_json, "default_branch")
empty = parsed_json.get("empty", None)
size = parsed_json.get("size", None)
urls = GogsRepo.Urls(json_get(parsed_json, "html_url"), json_get(parsed_json, "clone_url"),
json_get(parsed_json, "ssh_url"))
permissions = GogsRepo.Permissions.from_json(json_get(parsed_json, "permissions"))
return GogsRepo(repo_id=repo_id, owner=owner, full_name=full_name, private=private, fork=fork,
urls=urls, permissions=permissions)
default_branch=default_branch, empty=empty, size=size, urls=urls,
permissions=permissions, json=parsed_json)

@property # named repo_id to avoid conflict with built-in id
def repo_id(self):
Expand Down Expand Up @@ -153,6 +171,33 @@ def fork(self):
"""
return self._fork

@property
def default_branch(self):
"""
The name of the default branch

:rtype: str
"""
return self._default_branch

@property
def empty(self):
"""
Whether the repository is empty

:rtype: bool
"""
return self._empty

@property
def size(self):
"""
Size of the repository in kilobytes

:rtype: int
"""
return self._size

@property
def urls(self):
"""
Expand Down Expand Up @@ -204,8 +249,9 @@ def ssh_url(self):
"""
return self._ssh_url

class Permissions(object):
def __init__(self, admin, push, pull):
class Permissions(GogsEntity):
def __init__(self, admin, push, pull, json={}):
super(GogsRepo.Permissions, self).__init__(json=json)
self._admin = admin
self._push = push
self._pull = pull
Expand All @@ -215,7 +261,7 @@ def from_json(parsed_json):
admin = parsed_json.get("admin", False)
push = parsed_json.get("push", False)
pull = parsed_json.get("pull", False)
return GogsRepo.Permissions(admin, push, pull)
return GogsRepo.Permissions(admin, push, pull, parsed_json)

@property
def admin(self):
Expand Down Expand Up @@ -244,8 +290,9 @@ def pull(self):
"""
return self._pull

class Hook(object):
def __init__(self, hook_id, hook_type, events, active, config):
class Hook(GogsEntity):
def __init__(self, hook_id, hook_type, events, active, config, json={}):
super(GogsRepo.Hook, self).__init__(json=json)
self._id = hook_id
self._type = hook_type
self._events = events
Expand All @@ -260,7 +307,7 @@ def from_json(parsed_json):
active = json_get(parsed_json, "active")
config = json_get(parsed_json, "config")
return GogsRepo.Hook(hook_id=hook_id, hook_type=hook_type, events=events, active=active,
config=config)
config=config, json=parsed_json)

@property # named hook_id to avoid conflict with built-in id
def hook_id(self):
Expand Down Expand Up @@ -307,8 +354,9 @@ def config(self):
"""
return self._config

class DeployKey(object):
def __init__(self, key_id, key, url, title, created_at, read_only):
class DeployKey(GogsEntity):
def __init__(self, key_id, key, url, title, created_at, read_only, json={}):
super(GogsRepo.DeployKey, self).__init__(json=json)
self._id = key_id
self._key = key
self._url = url
Expand All @@ -326,7 +374,8 @@ def from_json(parsed_json):
read_only = json_get(parsed_json, "read_only")

return GogsRepo.DeployKey(key_id=key_id, key=key, url=url,
title=title, created_at=created_at, read_only=read_only)
title=title, created_at=created_at,
read_only=read_only, json=parsed_json)

@property # named key_id to avoid conflict with built-in id
def key_id(self):
Expand Down Expand Up @@ -383,12 +432,13 @@ def read_only(self):
return self._read_only


class GogsOrg(object):
class GogsOrg(GogsEntity):
"""
An immutable representation of a Gogs Organization
"""

def __init__(self, org_id, username, full_name, avatar_url, description, website, location):
def __init__(self, org_id, username, full_name, avatar_url, description, website, location, json={}):
super(GogsOrg, self).__init__(json=json)
self._id = org_id
self._username = username
self._full_name = full_name
Expand All @@ -408,7 +458,7 @@ def from_json(parsed_json):
location = json_get(parsed_json, "location")
return GogsOrg(org_id=org_id, username=username, full_name=full_name,
avatar_url=avatar_url, description=description,
website=website, location=location)
website=website, location=location, json=parsed_json)

@property # named org_id to avoid conflict with built-in id
def org_id(self):
Expand Down Expand Up @@ -474,11 +524,12 @@ def location(self):
return self._location


class GogsTeam(object):
class GogsTeam(GogsEntity):
"""
An immutable representation of a Gogs organization team
"""
def __init__(self, team_id, name, description, permission):
def __init__(self, team_id, name, description, permission, json={}):
super(GogsTeam, self).__init__(json=json)
self._id = team_id
self._name = name
self._description = description
Expand All @@ -490,7 +541,8 @@ def from_json(parsed_json):
name = json_get(parsed_json, "name")
description = json_get(parsed_json, "description")
permission = json_get(parsed_json, "permission")
return GogsTeam(team_id=team_id, name=name, description=description, permission=permission)
return GogsTeam(team_id=team_id, name=name, description=description,
permission=permission, json=parsed_json)

@property # named team_id to avoid conflict with built-in id
def team_id(self):
Expand Down
12 changes: 12 additions & 0 deletions tests/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def setUp(self):
"full_name": "unknwon/Hello-World",
"private": false,
"fork": false,
"default_branch": "master",
"empty": false,
"size": 42,
"html_url": "http://localhost:3000/unknwon/Hello-World",
"clone_url": "http://localhost:3000/unknwon/hello-world.git",
"ssh_url": "jiahuachen@localhost:unknwon/hello-world.git",
Expand All @@ -47,6 +50,9 @@ def setUp(self):
"full_name": "unknwon/Hello-World",
"private": false,
"fork": false,
"default_branch": "master",
"empty": false,
"size": 42,
"html_url": "http://localhost:3000/unknwon/Hello-World",
"clone_url": "http://localhost:3000/unknwon/hello-world.git",
"ssh_url": "jiahuachen@localhost:unknwon/hello-world.git",
Expand All @@ -67,6 +73,9 @@ def setUp(self):
"full_name": "unknwon/Hello-World-Again",
"private": false,
"fork": false,
"default_branch": "master",
"empty": false,
"size": 42,
"html_url": "http://localhost:3000/unknwon/Hello-World-Again",
"clone_url": "http://localhost:3000/unknwon/hello-world-again.git",
"ssh_url": "jiahuachen@localhost:unknwon/hello-world-again.git",
Expand Down Expand Up @@ -578,6 +587,9 @@ def assert_repos_equal(self, repo, expected):
self.assertEqual(repo.full_name, expected.full_name)
self.assertEqual(repo.private, expected.private)
self.assertEqual(repo.fork, expected.fork)
self.assertEqual(repo.default_branch, expected.default_branch)
self.assertEqual(repo.size, expected.size)
self.assertEqual(repo.empty, expected.empty)
self.assertEqual(repo.urls.html_url, expected.urls.html_url)
self.assertEqual(repo.urls.clone_url, expected.urls.clone_url)
self.assertEqual(repo.urls.ssh_url, expected.urls.ssh_url)
Expand Down