moth/teams.py

69 lines
1.4 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
import fcntl
2009-09-25 13:27:37 -06:00
import time
2009-10-01 12:17:03 -06:00
import config
2009-09-25 13:27:37 -06:00
import os
2009-09-02 14:25:31 -06:00
from urllib.parse import quote, unquote
2009-10-01 12:17:03 -06:00
house = config.get('global', 'house_team')
passwdfn = config.get('global', 'passwd')
team_colors = config.get('global', 'team_colors')
2009-09-25 13:27:37 -06:00
2009-09-29 15:36:25 -06:00
teams = {}
2009-09-25 13:27:37 -06:00
built = 0
def build_teams():
2009-09-25 13:27:37 -06:00
global teams, built
2009-09-29 15:36:25 -06:00
if not os.path.exists(passwdfn):
return
if os.path.getmtime(passwdfn) <= built:
2009-09-25 13:27:37 -06:00
return
teams = {}
try:
2009-09-25 13:27:37 -06:00
f = open(passwdfn)
for line in f:
2009-09-02 14:25:31 -06:00
line = line.strip()
team, passwd = [unquote(v) for v in line.strip().split('\t')]
2009-10-01 12:17:03 -06:00
color = team_colors.pop(0)
team_colors.append(color)
teams[team] = (passwd, color)
except IOError:
pass
2009-09-25 13:27:37 -06:00
built = time.time()
2009-09-02 14:25:31 -06:00
def validate(team):
2009-09-25 13:27:37 -06:00
build_teams()
2009-09-02 14:25:31 -06:00
def chkpasswd(team, passwd):
validate(team)
2009-10-01 12:17:03 -06:00
if teams.get(team, [None, None])[0] == passwd:
return True
else:
return False
def exists(team):
2009-09-02 14:25:31 -06:00
validate(team)
if team == house:
return True
return team in teams
def add(team, passwd):
2009-09-29 15:36:25 -06:00
f = open(passwdfn, 'a')
fcntl.lockf(f, fcntl.LOCK_EX)
f.seek(0, 2)
2009-09-02 14:25:31 -06:00
f.write('%s\t%s\n' % (quote(team), quote(passwd)))
2009-10-01 12:17:03 -06:00
def color(team):
t = teams.get(team)
if not t:
validate(team)
t = teams.get(team)
if not t:
return '888888'
return t[1]