Skip to content

Commit

Permalink
Use black to reformat code
Browse files Browse the repository at this point in the history
this reformats the code using `black --exclude=worker/requests .`

fixes official-stockfish#634

as with any tool, not all is perfect, but things are pretty consistent.
I propose to apply this patch (or execute the above black command) as part of the next worker change.
  • Loading branch information
vondele committed Aug 27, 2020
1 parent 79fc723 commit 1f4b690
Show file tree
Hide file tree
Showing 35 changed files with 5,903 additions and 5,073 deletions.
171 changes: 88 additions & 83 deletions fishtest/fishtest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,88 +10,93 @@
from fishtest.rundb import RunDb
from fishtest import helpers


def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
""" This function returns a Pyramid WSGI application.
"""
session_factory = UnencryptedCookieSessionFactoryConfig('fishtest')
config = Configurator(settings=settings,
session_factory=session_factory,
root_factory='fishtest.models.RootFactory')
config.include('pyramid_mako')
config.set_default_csrf_options(require_csrf=False)

rundb = RunDb()

def add_rundb(event):
event.request.rundb = rundb
event.request.userdb = rundb.userdb
event.request.actiondb = rundb.actiondb

def add_renderer_globals(event):
event['h'] = helpers

config.add_subscriber(add_rundb, NewRequest)
config.add_subscriber(add_renderer_globals, BeforeRender)

# Authentication
def group_finder(username, request):
return request.userdb.get_user_groups(username)

with open(os.path.expanduser('~/fishtest.secret'), 'r') as f:
secret = f.read()
config.set_authentication_policy(
AuthTktAuthenticationPolicy(
secret, callback=group_finder, hashalg='sha512', http_only=True))
config.set_authorization_policy(ACLAuthorizationPolicy())

config.add_static_view('html', 'static/html', cache_max_age=3600)
config.add_static_view('css', 'static/css', cache_max_age=3600)
config.add_static_view('js', 'static/js', cache_max_age=3600)
config.add_static_view('img', 'static/img', cache_max_age=3600)

config.add_route('home', '/')
config.add_route('login', '/login')
config.add_route('nn_upload', '/upload')
config.add_route('logout', '/logout')
config.add_route('signup', '/signup')
config.add_route('user', '/user/{username}')
config.add_route('profile', '/user')
config.add_route('pending', '/pending')
config.add_route('users', '/users')
config.add_route('users_monthly', '/users/monthly')
config.add_route('actions', '/actions')
config.add_route('nns', '/nns')

config.add_route('tests', '/tests')
config.add_route('tests_machines', '/tests/machines')
config.add_route('tests_finished', '/tests/finished')
config.add_route('tests_run', '/tests/run')
config.add_route('tests_view', '/tests/view/{id}')
config.add_route('tests_view_spsa_history', '/tests/view/{id}/spsa_history')
config.add_route('tests_user', '/tests/user/{username}')
config.add_route('tests_stats', '/tests/stats/{id}')

# Tests - actions
config.add_route('tests_modify', '/tests/modify')
config.add_route('tests_delete', '/tests/delete')
config.add_route('tests_stop', '/tests/stop')
config.add_route('tests_approve', '/tests/approve')
config.add_route('tests_purge', '/tests/purge')

# API
config.add_route('api_request_task', '/api/request_task')
config.add_route('api_update_task', '/api/update_task')
config.add_route('api_failed_task', '/api/failed_task')
config.add_route('api_stop_run', '/api/stop_run')
config.add_route('api_request_version', '/api/request_version')
config.add_route('api_request_spsa', '/api/request_spsa')
config.add_route('api_active_runs', '/api/active_runs')
config.add_route('api_get_run', '/api/get_run/{id}')
config.add_route('api_upload_pgn', '/api/upload_pgn')
config.add_route('api_download_pgn', '/api/pgn/{id}')
config.add_route('api_download_pgn_100', '/api/pgn_100/{skip}')
config.add_route('api_download_nn', '/api/nn/{id}')
config.add_route('api_get_elo', '/api/get_elo/{id}')

config.scan()
return config.make_wsgi_app()
session_factory = UnencryptedCookieSessionFactoryConfig("fishtest")
config = Configurator(
settings=settings,
session_factory=session_factory,
root_factory="fishtest.models.RootFactory",
)
config.include("pyramid_mako")
config.set_default_csrf_options(require_csrf=False)

rundb = RunDb()

def add_rundb(event):
event.request.rundb = rundb
event.request.userdb = rundb.userdb
event.request.actiondb = rundb.actiondb

def add_renderer_globals(event):
event["h"] = helpers

config.add_subscriber(add_rundb, NewRequest)
config.add_subscriber(add_renderer_globals, BeforeRender)

# Authentication
def group_finder(username, request):
return request.userdb.get_user_groups(username)

with open(os.path.expanduser("~/fishtest.secret"), "r") as f:
secret = f.read()
config.set_authentication_policy(
AuthTktAuthenticationPolicy(
secret, callback=group_finder, hashalg="sha512", http_only=True
)
)
config.set_authorization_policy(ACLAuthorizationPolicy())

config.add_static_view("html", "static/html", cache_max_age=3600)
config.add_static_view("css", "static/css", cache_max_age=3600)
config.add_static_view("js", "static/js", cache_max_age=3600)
config.add_static_view("img", "static/img", cache_max_age=3600)

config.add_route("home", "/")
config.add_route("login", "/login")
config.add_route("nn_upload", "/upload")
config.add_route("logout", "/logout")
config.add_route("signup", "/signup")
config.add_route("user", "/user/{username}")
config.add_route("profile", "/user")
config.add_route("pending", "/pending")
config.add_route("users", "/users")
config.add_route("users_monthly", "/users/monthly")
config.add_route("actions", "/actions")
config.add_route("nns", "/nns")

config.add_route("tests", "/tests")
config.add_route("tests_machines", "/tests/machines")
config.add_route("tests_finished", "/tests/finished")
config.add_route("tests_run", "/tests/run")
config.add_route("tests_view", "/tests/view/{id}")
config.add_route("tests_view_spsa_history", "/tests/view/{id}/spsa_history")
config.add_route("tests_user", "/tests/user/{username}")
config.add_route("tests_stats", "/tests/stats/{id}")

# Tests - actions
config.add_route("tests_modify", "/tests/modify")
config.add_route("tests_delete", "/tests/delete")
config.add_route("tests_stop", "/tests/stop")
config.add_route("tests_approve", "/tests/approve")
config.add_route("tests_purge", "/tests/purge")

# API
config.add_route("api_request_task", "/api/request_task")
config.add_route("api_update_task", "/api/update_task")
config.add_route("api_failed_task", "/api/failed_task")
config.add_route("api_stop_run", "/api/stop_run")
config.add_route("api_request_version", "/api/request_version")
config.add_route("api_request_spsa", "/api/request_spsa")
config.add_route("api_active_runs", "/api/active_runs")
config.add_route("api_get_run", "/api/get_run/{id}")
config.add_route("api_upload_pgn", "/api/upload_pgn")
config.add_route("api_download_pgn", "/api/pgn/{id}")
config.add_route("api_download_pgn_100", "/api/pgn_100/{skip}")
config.add_route("api_download_nn", "/api/nn/{id}")
config.add_route("api_get_elo", "/api/get_elo/{id}")

config.scan()
return config.make_wsgi_app()
99 changes: 50 additions & 49 deletions fishtest/fishtest/actiondb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,53 @@


class ActionDb:
def __init__(self, db):
self.db = db
self.actions = self.db['actions']

def get_actions(self, max_num, action=None, username=None):
q = {}
if action:
q['action'] = action
else:
q['action'] = {"$ne": 'update_stats'}
if username:
q['username'] = username
return self.actions.find(q, sort=[('_id', DESCENDING)], limit=max_num)

def update_stats(self):
self._new_action('fishtest.system', 'update_stats', '')

def new_run(self, username, run):
self._new_action(username, 'new_run', run)

def upload_nn(self, username, network):
self._new_action(username, 'upload_nn', network)

def modify_run(self, username, before, after):
self._new_action(username, 'modify_run',
{'before': before, 'after': after})

def delete_run(self, username, run):
self._new_action(username, 'delete_run', run)

def stop_run(self, username, run):
self._new_action(username, 'stop_run', run)

def approve_run(self, username, run):
self._new_action(username, 'approve_run', run)

def purge_run(self, username, run):
self._new_action(username, 'purge_run', run)

def block_user(self, username, data):
self._new_action(username, 'block_user', data)

def _new_action(self, username, action, data):
self.actions.insert_one({
'username': username,
'action': action,
'data': data,
'time': datetime.utcnow(),
})
def __init__(self, db):
self.db = db
self.actions = self.db["actions"]

def get_actions(self, max_num, action=None, username=None):
q = {}
if action:
q["action"] = action
else:
q["action"] = {"$ne": "update_stats"}
if username:
q["username"] = username
return self.actions.find(q, sort=[("_id", DESCENDING)], limit=max_num)

def update_stats(self):
self._new_action("fishtest.system", "update_stats", "")

def new_run(self, username, run):
self._new_action(username, "new_run", run)

def upload_nn(self, username, network):
self._new_action(username, "upload_nn", network)

def modify_run(self, username, before, after):
self._new_action(username, "modify_run", {"before": before, "after": after})

def delete_run(self, username, run):
self._new_action(username, "delete_run", run)

def stop_run(self, username, run):
self._new_action(username, "stop_run", run)

def approve_run(self, username, run):
self._new_action(username, "approve_run", run)

def purge_run(self, username, run):
self._new_action(username, "purge_run", run)

def block_user(self, username, data):
self._new_action(username, "block_user", data)

def _new_action(self, username, action, data):
self.actions.insert_one(
{
"username": username,
"action": action,
"data": data,
"time": datetime.utcnow(),
}
)
Loading

0 comments on commit 1f4b690

Please sign in to comment.