Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhwanlazy committed Jun 24, 2023
0 parents commit 07f840f
Show file tree
Hide file tree
Showing 15 changed files with 835 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
build/
*.egg-info/
tags
12 changes: 12 additions & 0 deletions ps_trainer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__appname__ = 'ps_trainer'
__author__ = 'rick.choi'
__version__ = '0.1'

import os
from appdirs import AppDirs
__appdirs__ = AppDirs(__appname__, __author__, __version__)
__logdir__ = __appdirs__.user_log_dir
__logfile__ = os.path.join(__appdirs__.user_log_dir, 'ps_trainer.log')
__datadir__ = __appdirs__.user_data_dir
os.makedirs(__logdir__, exist_ok=True)
os.makedirs(__datadir__, exist_ok=True)
87 changes: 87 additions & 0 deletions ps_trainer/cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import shutil
from datetime import datetime, timezone
import logging
logger = logging.getLogger(__name__)

from .user import get_current_user
from .db import ProblemDB, HistoryDB, SubmissionDB
from .config import Config
from .utils import read_confirm
from . import trainer
from . import __logdir__, __datadir__

def start():
logger.debug('start() called')
config = Config()
hdb = HistoryDB()
pdb = ProblemDB()
sdb = SubmissionDB()
user = get_current_user(config)
user.initialize(pdb, hdb)
sdb.update(user.name)

last = trainer.check_unfinished_session(user, hdb)
remaining_time = 0
if last is not None:
logger.info(f'unfinished problem exist - {last.pid}')
remaining_time = config.time_limit - (datetime.now() - last.timestamp).seconds
if remaining_time <= 0:
logger.info('unfinished problem marked as timeout')
hdb.add_action(user.name, last.pid, 'timeout')
else:
problem = pdb.get_problem(last.pid)

if remaining_time > 0:
logger.info('resuming last session')
problem = pdb.get_problem(last.pid)
message = 'Resumed from your last session'
else:
remaining_time = config.time_limit
problem = trainer.select_problem(user, pdb, sdb)
logger.info(f'selected new problem - {problem.pid}')
message = ''
hdb.add_action(user.name, problem.pid, 'start')

result = trainer.start_session(user, problem, sdb, remaining_time, message)
if result in ['solve', 'giveup', 'timeout']:
rating_before = user.rating.rating
hdb.add_action(user.name, problem.pid, result)
user.update(problem, result == 'solve')
rating_after = user.rating.rating
logger.info(f'rating updated: {int(rating_before)} -> {int(rating_after)}')


def print_status():
logger.debug('print_status() called')
config = Config()
hdb = HistoryDB()
pdb = ProblemDB()
user = get_current_user(config)
user.initialize(pdb, hdb)
print(user)

def print_history():
logger.debug('print_history() called')
config = Config()
hdb = HistoryDB()
user = get_current_user(config)
print(hdb.get_history(user.name))

def reset():
logger.debug('reset called')
if read_confirm(f'All your progress will be lost.'):
logger.warning(f'removing {__logdir__}')
shutil.rmtree(__logdir__)
logger.warning(f'removing {__datadir__}')
shutil.rmtree(__datadir__)
else:
logger.debug('reset canceled')


def get_command_list():
return {
'start': start,
'status': print_status,
'history': print_history,
'reset': reset,
}
45 changes: 45 additions & 0 deletions ps_trainer/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from .db import DBTable

class Config(DBTable):
def __init__(self):
super().__init__('configs')

def __getitem__(self, key):
item = self.db.execute('SELECT value FROM config WHERE key = ?', (key,))
if item is None or len(item) == 0:
return None
return item[0][0]

def __setitem__(self, key, value):
self.db.execute('REPLACE INTO config (key, value) VALUES (?,?)', (key, value))
self.db.commit()

def __contains__(self, key):
return self[key] is not None

def keys(self):
for row in self.db.execute('SELECT key FROM config'):
yield row[0]

@property
def current_user(self):
return self['current_user']

@current_user.setter
def current_user(self, name):
self['current_user'] = name

@property
def time_limit(self):
if 'time_limit' not in self:
self['time_limit'] = 3600 # default to 1 hour
return int(self['time_limit'])

@time_limit.setter
def time_limit(self, tl):
self['time_limit'] = tl

def initialize(self):
self.db.execute("CREATE TABLE IF NOT EXISTS config (key TEXT UNIQUE, value TEXT)")
self.db.commit()

Loading

0 comments on commit 07f840f

Please sign in to comment.