-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathusers.py
More file actions
167 lines (118 loc) · 5.09 KB
/
users.py
File metadata and controls
167 lines (118 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2012 Ask Solem <[email protected]>
# James Rowe <[email protected]>
# Sameer Al-Sakran <[email protected]>
# Stéphane Angel <[email protected]>
#
# This file is part of python-github2, and is made available under the 3-clause
# BSD license. See LICENSE for the full details.
try:
from urllib.parse import quote_plus # For Python 3
except ImportError:
from urllib import quote_plus # NOQA
from github2.core import (BaseData, GithubCommand, DateAttribute, Attribute,
enhanced_by_auth, requires_auth)
class Key(BaseData):
"""SSH key container."""
id = Attribute('The key id')
key = Attribute('The SSH key data')
title = Attribute('The title for the SSH key')
def __repr__(self):
return "<Key: %s>" % self.id
class User(BaseData):
"""GitHub user container."""
id = Attribute("The user id")
login = Attribute("The login username")
name = Attribute("The users full name")
company = Attribute("Name of the company the user is associated with")
location = Attribute("Location of the user")
email = Attribute("The users e-mail address")
blog = Attribute("The users blog")
following_count = Attribute("Number of other users the user is following")
followers_count = Attribute("Number of users following this user")
public_gist_count = Attribute(
"Number of active public gists owned by the user")
public_repo_count = Attribute(
"Number of active repositories owned by the user")
total_private_repo_count = Attribute("Number of private repositories")
collaborators = Attribute("Number of collaborators")
disk_usage = Attribute("Currently used disk space")
owned_private_repo_count = Attribute("Number of privately owned repos")
private_gist_count = Attribute(
"Number of private gists owned by the user")
plan = Attribute("Current active github plan")
created_at = DateAttribute("The date this user was registered",
format="user")
def is_authenticated(self):
"""Test for user authentication.
:return bool: ``True`` if user is authenticated
"""
return self.plan is not None
def __repr__(self):
return "<User: %s>" % self.login
class Users(GithubCommand):
"""GitHub API user functionality."""
domain = "user"
def search(self, query):
"""Search for users.
.. warning:
Returns at most 100 users
:param str query: term to search for
"""
return self.get_values("search", quote_plus(query), filter="users",
datatype=User)
def search_by_email(self, query):
"""Search for users by email address.
:param str query: email to search for
"""
return self.get_value("email", query, filter="user", datatype=User)
@enhanced_by_auth
def show(self, username):
"""Get information on GitHub user.
if ``username`` is ``None`` or an empty string information for the
currently authenticated user is returned.
"""
return self.get_value("show", username, filter="user", datatype=User)
def followers(self, username):
"""Get list of GitHub user's followers.
:param str username: GitHub user name
"""
return self.get_values("show", username, "followers", filter="users")
def following(self, username):
"""Get list of users a GitHub user is following.
:param str username: GitHub user name
"""
return self.get_values("show", username, "following", filter="users")
@requires_auth
def follow(self, other_user):
"""Follow a GitHub user.
:param str other_user: GitHub user name
"""
return self.get_values("follow", other_user, method="POST")
@requires_auth
def unfollow(self, other_user):
"""Unfollow a GitHub user.
:param str other_user: GitHub user name
"""
return self.get_values("unfollow", other_user, method="POST")
@requires_auth
def list_keys(self):
"""Get list of SSH keys for the authenticated user."""
return self.get_values('keys', filter='public_keys', datatype=Key)
@requires_auth
def add_key(self, key, title=''):
"""Add a SSH key for the authenticated user.
:param str key: SSH key identifier
:param str title: Optional title for the SSH key
"""
return self.get_values("key/add",
post_data={'key': key, 'title': title},
method="POST", filter='public_keys',
datatype=Key)
@requires_auth
def remove_key(self, key_id):
"""Remove a SSH key for the authenticated user.
:param int key_id: SSH key's GitHub identifier
"""
return self.get_values('key/remove', post_data={'id': str(key_id)},
filter='public_keys', datatype=Key)