Skip to content

Commit a8eb5d0

Browse files
committed
linting and CI fixes
1 parent dad5560 commit a8eb5d0

File tree

10 files changed

+70
-86
lines changed

10 files changed

+70
-86
lines changed

sogs/model/clientmanager.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,20 @@ class ClientManager:
7979
queue - BotQueue object
8080
"""
8181

82-
def __init__(
83-
self,
84-
*,
85-
id: Optional[int] = None,
86-
) -> None:
82+
def __init__(self, *, id: Optional[int] = None) -> None:
8783
self.id = id
8884
self.filter = False
8985
self.bqueue = BotQueue()
9086
self.clients = []
9187

92-
9388
def bqempty(self):
9489
return not self.bqueue._empty()
95-
9690

9791
def register_client(self, conn_id, cid, authlevel, bot, priority):
9892
if not bot:
9993
# add client to self.clients
10094
return
101-
95+
10296
if not priority:
10397
# if no priority is given, lowest priority is assigned
10498
priority = self.qsize()
@@ -108,19 +102,16 @@ def register_client(self, conn_id, cid, authlevel, bot, priority):
108102
priority += 1
109103
self.bqueue._put(PriorityTuple(priority, bot))
110104

111-
112105
def deregister_client(self, cid, bot):
113106
if not bot:
114107
# remove client from clients list
115108
return
116-
109+
117110
# remove bot from bot queue
118-
119111

120112
def peek(self, priority: int):
121113
return self.bqueue._peek(priority)
122114

123-
124115
def check_permission_for(
125116
self,
126117
room: Room,

sogs/model/room.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ def _refresh(self, *, id=None, token=None, row=None, perms=False):
154154
if perms or not hasattr(self, '_perm_cache'):
155155
self._perm_cache = {}
156156

157-
158157
def __str__(self):
159158
"""Returns `Room[token]` when converted to a str"""
160159
return f"Room[{self.token}]"

sogs/mule.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def setup_omq(self, omq: OMQ):
111111
omq.mule_conn = omq.connect_inproc(on_success=None, on_failure=self.inproc_fail)
112112

113113

114-
115114
"""
116115
TOFIX: oxenmq calls should pass as oxenmq.Message not as JSON
117116

sogs/omq.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import oxenmq, queue
55
from oxenc import bt_serialize, bt_deserialize
66

7-
from mule import log_exceptions
8-
from routes import omq_auth
7+
from .mule import log_exceptions
8+
from .routes import omq_auth
99
from . import crypto, config
1010
from .postfork import postfork
1111
from .model.clientmanager import ClientManager
@@ -36,7 +36,7 @@ def __init__(self):
3636
if uwsgi.mule_id() != 0:
3737
uwsgi.opt['mule'].setup_omq(self)
3838
return
39-
39+
4040
uwsgi.register_signal(123, 'internal', self.handle_proxied_omq_req)
4141

4242
from .web import app # Imported here to avoid circular import
@@ -52,57 +52,49 @@ def __init__(self):
5252
global omq_global
5353
omq_global = self
5454

55-
5655
@log_exceptions
5756
def subreq_response(self):
5857
pass
5958

60-
6159
@log_exceptions
6260
def handle_proxied_omq_req(self):
63-
id, subreq_body = self.send_mule(
64-
command='get_next_request',
65-
prefix='internal'
66-
)
61+
id, subreq_body = self.send_mule(command='get_next_request', prefix='internal')
6762

6863
'''
6964
7065
Handle omq subrequest
7166
7267
'''
7368

74-
return
69+
return
7570

7671
@log_exceptions
7772
def get_next_request(self):
7873
subreq_body = self.subreq_queue.get()
7974
id = list(subreq_body.keys())[0]
8075
return id, subreq_body[id]
8176

82-
8377
@log_exceptions
8478
def register_client(self, msg: oxenmq.Message):
8579
cid, authlevel, bot, priority = bt_deserialize(msg.data()[0])
8680
conn_id = msg.conn()
8781
self.client_map[conn_id] = cid
8882
self.manager.register_client(msg)
8983

90-
9184
@log_exceptions
9285
def deregister_client(self, msg: oxenmq.Message):
9386
cid, bot = bt_deserialize(msg.data()[0])
9487
self.client_map.pop(cid)
9588
self.manager.deregister_client(cid, bot)
9689

97-
9890
def send_mule(self, command, *args, prefix="worker."):
9991
"""
10092
Sends a command to the mule from a worker (or possibly from the mule itself). The command will
10193
be prefixed with "worker." (unless overridden).
10294
10395
Any args will be bt-serialized and send as message parts.
10496
"""
105-
97+
10698
if prefix:
10799
command = prefix + command
108100

sogs/routes/clients.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@omq_auth.first_request
2323
def register(cid):
2424
"""
25-
Registers a client with SOGS OMQ instance. In this context, "client" refers to any entity
25+
Registers a client with SOGS OMQ instance. In this context, "client" refers to any entity
2626
seeking to create an authenticated OMQ connection. This may be, but is not limited to,
2727
a user or a bot
2828
@@ -44,12 +44,11 @@ def register(cid):
4444
"""
4545

4646
req = request.json
47-
bot = utils.get_int_param('bot') # will set bot == 1 if key "bot" has value True
47+
bot = utils.get_int_param('bot') # will set bot == 1 if key "bot" has value True
4848
authlevel = req.get('authlevel')
4949
priority = req.get('priority')
50-
51-
client = (bot is 1)[register_client(cid, authlevel),
52-
register_bot(cid, authlevel, priority)]
50+
51+
client = (bot is 1)[register_client(cid, authlevel), register_bot(cid, authlevel, priority)]
5352

5453
return client
5554

@@ -78,7 +77,7 @@ def register_bot(cid, authlevel, priority):
7877
authlevel=authlevel,
7978
bot=1,
8079
priority=priority,
81-
prefix='handler'
80+
prefix='handler',
8281
)
8382

8483
return client
@@ -106,7 +105,7 @@ def register_client(cid, authlevel):
106105
authlevel=authlevel,
107106
bot=0,
108107
priority=None,
109-
prefix='handler'
108+
prefix='handler',
110109
)
111110

112111
return client
@@ -125,7 +124,7 @@ def unregister(cid):
125124
126125
- 'bot' (bool) : is bot or not
127126
"""
128-
127+
129128
bot = utils.get_int_param('bot')
130129

131130
client = (bot)[unregister_client(cid), unregister_bot(cid)]
@@ -137,25 +136,15 @@ def unregister(cid):
137136
@clients.delete("/client/registered/client/<cid>")
138137
def unregister_client(cid):
139138

140-
client = omq_global.send_mule(
141-
command='unregister_client',
142-
cid=cid,
143-
bot=0,
144-
prefix='handler'
145-
)
146-
139+
client = omq_global.send_mule(command='unregister_client', cid=cid, bot=0, prefix='handler')
140+
147141
return client
148142

149143

150144
@clients.post("/bot/deregistered/bot/<cid>")
151145
@clients.delete("/bot/registered/bot/<cid>")
152146
def unregister_bot(cid):
153-
154-
client = omq_global.send_mule(
155-
command='unregister_bot',
156-
cid=cid,
157-
bot=1,
158-
prefix='handler'
159-
)
147+
148+
client = omq_global.send_mule(command='unregister_bot', cid=cid, bot=1, prefix='handler')
160149

161150
return client

sogs/routes/messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
messages = Blueprint('messages', __name__)
1111
blueprints_global['messages'] = messages
1212

13+
1314
def qs_reactors():
1415
return utils.get_int_param('reactors', 4, min=0, max=20, truncate=True)
1516

@@ -314,6 +315,7 @@ def message_single(room: Room, msg_id):
314315
- add some decorator to this s.t. it routes it to the correct OMQ endpoint
315316
"""
316317

318+
317319
@messages.post("/room/<Room:room>/message")
318320
@auth.user_required
319321
def post_message(room: Room):

sogs/routes/omq_auth.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
omq = Blueprint('endpoint', __name__)
2222

23+
2324
def endpoint(f):
24-
"""
25+
"""
2526
Default endpoint for omq routes to pass requests to; constructs flask HTTP request
2627
2728
Message (request) components:
2829
2930
"blueprint" - the flask blueprint
30-
"query" - the request query
31+
"query" - the request query
3132
"pubkey" - pk of client making request
3233
"params" - a json value to dump as the the query parameters
3334
@@ -52,64 +53,68 @@ def abort_request(code, msg, warn=True):
5253

5354

5455
def require_client():
55-
"""
56-
Requires that an authenticated client was found in the OMQ instance; aborts with
57-
UNAUTHORIZED if the request has no client
56+
"""
57+
Requires that an authenticated client was found in the OMQ instance; aborts with
58+
UNAUTHORIZED if the request has no client
5859
"""
5960
if g.client_id is None:
6061
abort_request(http.UNAUTHORIZED, 'OMQ client authentication required')
6162

6263

6364
def client_required(f):
64-
"""
65-
Decorator for an endpoint that requires a client; this calls require_client() at the
66-
beginning of the request to abort the request as UNAUTHORIZED if the client has not been
67-
previously authenticated
65+
"""
66+
Decorator for an endpoint that requires a client; this calls require_client() at the
67+
beginning of the request to abort the request as UNAUTHORIZED if the client has not been
68+
previously authenticated
6869
"""
6970

7071
@wraps(f)
7172
def required_client_wrapper(*args, **kwargs):
7273
require_client()
7374
return f(*args, **kwargs)
74-
75+
7576
return required_client_wrapper
7677

7778

7879
def require_authlevel(admin=True):
7980
require_client()
80-
if g.client_authlevel is not oxenmq.Authlevel.admin if admin else g.client_authlevel is not oxenmq.Authlevel.basic:
81+
if (
82+
g.client_authlevel is not oxenmq.Authlevel.admin
83+
if admin
84+
else g.client_authlevel is not oxenmq.Authlevel.basic
85+
):
8186
abort_request(
82-
http.FORBIDDEN,
83-
f"This endpoint requires oxenmq.Authlevel.{'admin' if admin else 'basic'} permissions"
87+
http.FORBIDDEN,
88+
f"This endpoint requires oxenmq.Authlevel.{'admin' if admin else 'basic'} permissions",
8489
)
8590

8691

8792
def basic_required(f):
88-
""" Decorator for an endpoint that requires a client has basic OMQ authorization """
93+
"""Decorator for an endpoint that requires a client has basic OMQ authorization"""
8994

9095
@wraps(f)
9196
def required_basic_wrapper(*args, **kwargs):
9297
require_authlevel(admin=False)
9398
return f(*args, **kwargs)
94-
99+
95100
return required_basic_wrapper
96101

97102

98103
def admin_required(f):
99-
""" Decorator for an endpoint that requires a client has admin OMQ authorization """
104+
"""Decorator for an endpoint that requires a client has admin OMQ authorization"""
100105

101106
@wraps(f)
102107
def required_admin_wrapper(*args, **kwargs):
103108
require_authlevel(admin=True)
104109
return f(*args, **kwargs)
105-
110+
106111
return required_admin_wrapper
107112

108113

109114
def first_request(f):
110-
""" Decorator for an endpoint that will be the very first request for a given client. This
115+
"""Decorator for an endpoint that will be the very first request for a given client. This
111116
will ensure that the client is then registered for any subsequent requests.
112-
117+
113118
This function will typically take the folling parameters:
114119
- cid : unique client ID to be attributed
115120
- authlevel (oxenmq)
@@ -119,7 +124,7 @@ def first_request(f):
119124
def first_request_wrapper(*args, cid, authlevel, **kwargs):
120125
handle_omq_registration(cid, authlevel)
121126
return f(*args, cid=cid, authlevel=authlevel, **kwargs)
122-
127+
123128
return first_request_wrapper
124129

125130

@@ -128,9 +133,11 @@ def handle_omq_registration(sid, authlevel):
128133
Registers client with OMQ instance before its very first request
129134
"""
130135
if hasattr(g, 'client_id') and hasattr(g, 'client_authlevel') and not g.client_reauth:
131-
app.logger.warning(f"Client {g.client_id} already registered for {g.client_authlevel} access")
136+
app.logger.warning(
137+
f"Client {g.client_id} already registered for {g.client_authlevel} access"
138+
)
132139
return
133-
140+
134141
"""
135142
Here goes ye olde OMQ registration logic. We need to decide what identification will
136143
be used to verify every connected client s.t. that information persists for all subsequent
@@ -147,11 +154,13 @@ def verify_omq_auth():
147154
"""
148155
Verifies OMQ authentication before each request
149156
"""
150-
157+
151158
# If there is already a g.o_id, then this is NOT the first request made by this client, unless
152159
# g.client_reauth has been specifically set
153160
if hasattr(g, 'client_id') and hasattr(g, 'client_authlevel') and not g.client_reauth:
154-
app.logger.debug(f"Client {g.client_id} already authenticated for {g.client_authlevel} access")
161+
app.logger.debug(
162+
f"Client {g.client_id} already authenticated for {g.client_authlevel} access"
163+
)
155164
return
156165

157166

0 commit comments

Comments
 (0)