GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

src.pyejabberd.GetRoster   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 13
Duplicated Lines 0 %
Metric Value
wmc 4
dl 0
loc 13
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform_response() 0 9 4
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3
4
from .core.arguments import StringArgument
5
from .core.definitions import API
6
from .core.serializers import StringSerializer
7
from .muc import muc_room_options_serializers
8
from .muc.arguments import MUCRoomArgument, AffiliationArgument
9
from .muc.enums import MUCRoomOption
10
11
from .errors import UserAlreadyRegisteredError
12
from pyejabberd.muc.enums import Affiliation
13
from .utils import format_password_hash_sha
14
15
16
class Echo(API):
17
    method = 'echothisnew'
18
    arguments = [StringArgument('sentence')]
19
20
    def transform_response(self, api, arguments, response):
21
        return response.get('repeated')
22
23
24
class RegisteredUsers(API):
25
    method = 'registered_users'
26
    arguments = [StringArgument('host')]
27
28
    def transform_response(self, api, arguments, response):
29
        return response.get('users', [])
30
31
32
class Register(API):
33
    method = 'register'
34
    arguments = [StringArgument('user'), StringArgument('host'), StringArgument('password')]
35
36
    def validate_response(self, api, arguments, response):
37
        if response.get('res') == 1:
38
            username = arguments.get('user')
39
            raise UserAlreadyRegisteredError('User with username %s already exists' % username)
40
41
    def transform_response(self, api, arguments, response):
42
        return response.get('res') == 0
43
44
45
class UnRegister(API):
46
    method = 'unregister'
47
    arguments = [StringArgument('user'), StringArgument('host')]
48
49
    def transform_response(self, api, arguments, response):
50
        return response.get('res') == 0
51
52
53
class ChangePassword(API):
54
    method = 'change_password'
55
    arguments = [StringArgument('user'), StringArgument('host'), StringArgument('newpass')]
56
57
    def transform_response(self, api, arguments, response):
58
        return response.get('res') == 0
59
60
61
class CheckPasswordHash(API):
62
    method = 'check_password_hash'
63
    arguments = [StringArgument('user'), StringArgument('host'), StringArgument('passwordhash'),
64
                 StringArgument('hashmethod')]
65
66
    def transform_arguments(self, **kwargs):
67
        passwordhash = format_password_hash_sha(password=kwargs.pop('password'))
68
        kwargs.update({
69
            'passwordhash': passwordhash,
70
            'hashmethod': 'sha'
71
        })
72
        return kwargs
73
74
    def transform_response(self, api, arguments, response):
75
        return response.get('res') == 0
76
77
78
class SetNickname(API):
79
    method = 'set_nickname'
80
    arguments = [StringArgument('user'), StringArgument('host'), StringArgument('nickname')]
81
82
    def transform_response(self, api, arguments, response):
83
        return response.get('res') == 0
84
85
class ConnectedUsers(API):
86
    method = 'connected_users'
87
    arguments = []
88
89
    def transform_response(self, api, arguments, response):
90
        connected_users = response.get('connected_users', [])
91
92
        return [user["sessions"] for user in connected_users]
93
94
class ConnectedUsersInfo(API):
95
    method = 'connected_users_info'
96
    arguments = []
97
98
    def transform_response(self, api, arguments, response):
99
        connected_users_info = response.get('connected_users_info', [])
100
101
        return [user["sessions"] for user in connected_users_info]
102
103
class ConnectedUsersNumber(API):
104
    method = 'connected_users_number'
105
    arguments = []
106
107
    def transform_response(self, api, arguments, response):
108
        return response.get('num_sessions')
109
110
class UserSessionInfo(API):
111
    method = 'user_sessions_info'
112
    arguments = [StringArgument('user'), StringArgument('host')]
113
114
    def transform_response(self, api, arguments, response):
115
        sessions_info = response.get('sessions_info', [])
116
        return [
117
            dict((k, v) for property_k_v in session["session"] for k, v in property_k_v.items())
118
            for session in sessions_info
119
        ]
120
121
class MucOnlineRooms(API):
122
    method = 'muc_online_rooms'
123
    arguments = [StringArgument('host')]
124
125
    def transform_response(self, api, arguments, response):
126
        return [result_dict.get('room') for result_dict in response.get('rooms', {})]
127
128
129
class CreateRoom(API):
130
    method = 'create_room'
131
    arguments = [StringArgument('name'), StringArgument('service'), StringArgument('host')]
132
133
    def transform_response(self, api, arguments, response):
134
        return response.get('res') == 0
135
136
137
class DestroyRoom(API):
138
    method = 'destroy_room'
139
    arguments = [StringArgument('name'), StringArgument('service'), StringArgument('host')]
140
141
    def transform_response(self, api, arguments, response):
142
        return response.get('res') == 0
143
144
145
class GetRoomOptions(API):
146
    method = 'get_room_options'
147
    arguments = [StringArgument('name'), StringArgument('service')]
148
149
    def transform_response(self, api, arguments, response):
150
        result = {}
151
        for option_dict in response.get('options', []):
152
            option = option_dict.get('option')
153
            if option is None:
154
                raise ValueError('Unexpected option in response: ' % str(option_dict))
155
            name_dict, value_dict = option
156
            result[name_dict['name']] = value_dict['value']
157
        return result
158
159
160
class ChangeRoomOption(API):
161
    method = 'change_room_option'
162
    arguments = [StringArgument('name'), StringArgument('service'), MUCRoomArgument('option'), StringArgument('value')]
163
164
    def transform_arguments(self, **kwargs):
165
        option = kwargs.get('option')
166
        assert isinstance(option, MUCRoomOption)
167
        serializer_class = muc_room_options_serializers.get(option, StringSerializer)
168
        kwargs['value'] = serializer_class().to_api(kwargs['value'])
169
        return kwargs
170
171
    def transform_response(self, api, arguments, response):
172
        return response.get('res') == 0
173
174
175
class SetRoomAffiliation(API):
176
    method = 'set_room_affiliation'
177
    arguments = [StringArgument('name'), StringArgument('service'), StringArgument('jid'),
178
                 AffiliationArgument('affiliation')]
179
180
    def transform_response(self, api, arguments, response):
181
        return response.get('res') == 0
182
183
184
class GetRoomAffiliations(API):
185
    method = 'get_room_affiliations'
186
    arguments = [StringArgument('name'), StringArgument('service')]
187
188
    def transform_response(self, api, arguments, response):
189
        affiliations = response.get('affiliations', [])
190
        return [{
191
            'username': subdict['affiliation'][0]['username'],
192
            'domain': subdict['affiliation'][1]['domain'],
193
            'affiliation': Affiliation.get_by_name(subdict['affiliation'][2]['affiliation']),
194
            'reason': subdict['affiliation'][3]['reason'],
195
        } for subdict in affiliations]
196
197
198
class AddRosterItem(API):
199
    method = 'add_rosteritem'
200
    arguments = [StringArgument('localuser'), StringArgument('localserver'),
201
                 StringArgument('user'), StringArgument('server'),
202
                 StringArgument('nick'), StringArgument('group'), StringArgument('subs')]
203
204
    def transform_response(self, api, arguments, response):
205
        return response.get('res') == 0
206
207
208
class DeleteRosterItem(API):
209
    method = 'delete_rosteritem'
210
    arguments = [StringArgument('localuser'), StringArgument('localserver'),
211
                 StringArgument('user'), StringArgument('server')]
212
213
    def transform_response(self, api, arguments, response):
214
        return response.get('res') == 0
215
216
217
class GetRoster(API):
218
    method = 'get_roster'
219
    arguments = [StringArgument('user'), StringArgument('host')]
220
221
    def transform_response(self, api, arguments, response):
222
        roster = []
223
        for contact in response.get('contacts', []):
224
            contact_details = {}
225
            for parameter in contact['contact']:
226
                for key, value in parameter.items():
227
                    contact_details[key] = value
228
            roster.append(contact_details)
229
        return roster
230