Skip to content

Commit 21935d3

Browse files
Addressing pycodestyle issues
Addressing styling issues in the learning_observer module
1 parent 436fe10 commit 21935d3

File tree

23 files changed

+99
-96
lines changed

23 files changed

+99
-96
lines changed

learning_observer/docs/conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
# add these directories to sys.path here. If the directory is relative to the
1111
# documentation root, use os.path.abspath to make it absolute, like shown here.
1212
#
13-
#import os
14-
#import sys
15-
#sys.path.insert(0, os.path.abspath('..'))
13+
# import os
14+
# import sys
15+
# sys.path.insert(0, os.path.abspath('..'))
1616

1717

1818
# -- Project information -----------------------------------------------------

learning_observer/learning_observer/auth/__init__.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,36 @@
7575
import learning_observer.prestartup
7676
import learning_observer.settings as settings
7777

78+
7879
@learning_observer.prestartup.register_startup_check
7980
def verify_auth_precheck():
80-
'''
81-
This is a pre-startup check to make sure that the auth system is configured
82-
correctly.
83-
'''
84-
# We need some auth
85-
if 'auth' not in settings.settings:
86-
raise learning_observer.prestartup.StartupCheck("Please configure auth")
87-
88-
# If we have Google oauth, we need it properly configured.
89-
# TODO: Confirm everything works with Google Oauth missing
90-
if 'google_oauth' in settings.settings['auth']:
91-
if 'web' not in settings.settings['auth']['google_oauth'] or \
92-
'client_secret' not in settings.settings['auth']['google_oauth']['web'] or \
93-
'project_id' not in settings.settings['auth']['google_oauth']['web'] or \
94-
'client_id' not in settings.settings['auth']['google_oauth']['web'] or \
95-
isinstance(settings.settings['auth']['google_oauth']['web']['client_secret'], dict) or \
96-
isinstance(settings.settings['auth']['google_oauth']['web']['project_id'], dict) or \
97-
isinstance(settings.settings['auth']['google_oauth']['web']['client_id'], dict):
98-
error = \
99-
"Please configure (or disable) Google oauth\n" + \
100-
"\n" + \
101-
"Go to:\n" + \
102-
" https://console.developers.google.com/ \n" + \
103-
"And set up an OAuth client for a web application. Make sure that configuration\n" + \
104-
"mirrors the one here.\n" + \
105-
"\n" + \
106-
"If you are not planning to use Google auth (which is the case for most dev\n" + \
107-
"settings), please disable Google authentication in creds.yaml"
108-
raise learning_observer.prestartup.StartupCheck(error)
81+
'''
82+
This is a pre-startup check to make sure that the auth system is configured
83+
correctly.
84+
'''
85+
# We need some auth
86+
if 'auth' not in settings.settings:
87+
raise learning_observer.prestartup.StartupCheck(
88+
"Please configure auth")
89+
90+
# If we have Google oauth, we need it properly configured.
91+
# TODO: Confirm everything works with Google Oauth missing
92+
if 'google_oauth' in settings.settings['auth']:
93+
if 'web' not in settings.settings['auth']['google_oauth'] or \
94+
'client_secret' not in settings.settings['auth']['google_oauth']['web'] or \
95+
'project_id' not in settings.settings['auth']['google_oauth']['web'] or \
96+
'client_id' not in settings.settings['auth']['google_oauth']['web'] or \
97+
isinstance(settings.settings['auth']['google_oauth']['web']['client_secret'], dict) or \
98+
isinstance(settings.settings['auth']['google_oauth']['web']['project_id'], dict) or \
99+
isinstance(settings.settings['auth']['google_oauth']['web']['client_id'], dict):
100+
error = \
101+
"Please configure (or disable) Google oauth\n" + \
102+
"\n" + \
103+
"Go to:\n" + \
104+
" https://console.developers.google.com/ \n" + \
105+
"And set up an OAuth client for a web application. Make sure that configuration\n" + \
106+
"mirrors the one here.\n" + \
107+
"\n" + \
108+
"If you are not planning to use Google auth (which is the case for most dev\n" + \
109+
"settings), please disable Google authentication in creds.yaml"
110+
raise learning_observer.prestartup.StartupCheck(error)

learning_observer/learning_observer/auth/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def basic_auth(request, headers, first_event, source):
120120
'''
121121
Authenticate with HTTP Basic through nginx.
122122
'''
123-
(username, password) = learning_observer.auth.http_basic.http_basic_extract_username_password(request)
123+
(username, password) = learning_observer.auth.http_basic.http_basic_extract_username_password(request)
124124
print(f"Authenticated as {username}")
125125
if username is None:
126126
# nginx shouldn't pass requests without

learning_observer/learning_observer/auth/handlers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ def name_to_email(name):
121121
name = names.get_full_name()
122122

123123
user_info = {
124-
"name": name,
125-
"picture": "/auth/default-avatar.svg",
126-
"authorized": True,
127-
"google_id": random.randint(10000, 99999),
128-
"email": name_to_email(name)
129-
}
124+
"name": name,
125+
"picture": "/auth/default-avatar.svg",
126+
"authorized": True,
127+
"google_id": random.randint(10000, 99999),
128+
"email": name_to_email(name)
129+
}
130130
await learning_observer.auth.utils.update_session_user_info(request, user_info)
131131
return user_info
132132

learning_observer/learning_observer/auth/http_basic.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,21 @@ async def password_auth_handler(request):
172172
def http_basic_startup_check():
173173
if http_auth_page_enabled() and http_auth_middleware_enabled():
174174
raise learning_observer.prestartup.StartupCheck(
175-
"Your HTTP Basic authentication is misconfigured.\n" +
176-
"\n" +
177-
"You want EITHER auth on every page, OR a login page,\n" +
178-
"not both. Having both setting may be a security risk.\n" +
179-
"Please fix this."
175+
"Your HTTP Basic authentication is misconfigured.\n"
176+
+ "\n"
177+
+ "You want EITHER auth on every page, OR a login page,\n"
178+
+ "not both. Having both setting may be a security risk.\n"
179+
+ "Please fix this."
180180
)
181181

182-
if ('http_basic' in learning_observer.settings.settings['auth']
182+
if (
183+
'http_basic' in learning_observer.settings.settings['auth']
183184
and learning_observer.settings.settings['auth']['http_basic'].get("delegate_nginx_auth", False)
184185
and learning_observer.settings.settings['auth']['http_basic'].get("password_file", False)
185-
):
186+
):
186187
raise learning_observer.prestartup.StartupCheck(
187-
"Your HTTP Basic authentication is misconfigured.\n" +
188-
"\n" +
189-
"You should EITHER rely on nginx for password authentication OR Learning Observer," +
190-
"not both."
188+
"Your HTTP Basic authentication is misconfigured.\n"
189+
+ "\n"
190+
+ "You should EITHER rely on nginx for password authentication OR Learning Observer,"
191+
+ "not both."
191192
)

learning_observer/learning_observer/auth/password.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async def password_auth_handler(request):
6262
data['password'].encode('utf-8'),
6363
user_data['password'].encode('utf-8')
6464
)
65-
except:
65+
except: # noqa: E722 TODO figure out which errors to catch
6666
debug_log("Error verifying password hash")
6767
debug_log("Hint: Try reinstalling / upgrading bcrypt")
6868
debug_log("For some reason, bcrypt tends to sometimes install incorrectly")

learning_observer/learning_observer/auth/social_sso.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
]
6060

6161

62-
6362
async def social_handler(request):
6463
"""Handles Google sign in.
6564

learning_observer/learning_observer/google.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ async def cleaner_handler(request):
231231
else:
232232
raise AttributeError(f"Invalid response type: {type(response)}")
233233
if name is not None:
234-
setattr(cleaner_handler, "__qualname__", name+"_handler")
234+
setattr(cleaner_handler, "__qualname__", name + "_handler")
235235

236236
return cleaner_handler
237237

@@ -365,7 +365,7 @@ def _force_text_length(text, length):
365365
>>> force_text_length("Hello", 13)
366366
>>> 'Hello '
367367
'''
368-
return text[:length] + " " * (length-len(text))
368+
return text[:length] + " " * (length - len(text))
369369

370370

371371
@register_cleaner("document", "doctext")
@@ -394,7 +394,7 @@ def extract_text_from_google_doc_json(
394394
flat = sum(elements, [])
395395
text_chunks = [f['textRun']['content'] for f in flat]
396396
if align:
397-
lengths = [f['endIndex']-f['startIndex'] for f in flat]
397+
lengths = [f['endIndex'] - f['startIndex'] for f in flat]
398398
text_chunks = [_force_text_length(chunk, length) for chunk, length in zip(text_chunks, lengths)]
399399
text = ''.join(text_chunks)
400400

learning_observer/learning_observer/kvs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def key_to_safe_filename(self, key):
231231
if self.subdirs:
232232
paths = key.split('/')
233233
# Add underscores to directories so they don't conflict with files
234-
for i in range(len(paths)-1):
234+
for i in range(len(paths) - 1):
235235
paths[i] = '_' + paths[i]
236236
safename = (os.sep).join(map(learning_observer.util.to_safe_filename, paths))
237237
else:

learning_observer/learning_observer/offline.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,28 @@ async def test_case():
303303
class StubApp():
304304
def __init__(self):
305305
self.loop = asyncio.get_event_loop()
306+
306307
def add_routes(self, *args, **kwargs):
307308
pass
308309

309310

310311
app = StubApp()
311312

313+
312314
class StubRequest():
315+
313316
def __init__(self):
314317
self.app = app
318+
315319
def __contains__(self, item):
316320
if item == 'auth_headers':
317321
return True
318322
return False
323+
319324
def __getitem__(self, item):
320325
return {}
321326

327+
322328
request = StubRequest()
323329

324330

0 commit comments

Comments
 (0)