from collections import namedtuple
from os import listdir
from os.path import isfile, join
from datetime import datetime
import time, threading, logging
from watchdog.observers import Observer

import config
from parser_xml import parse_standing_from_file
from models.standings import TotalStandings
from fs_event_handler import FSEventHandler

StandingPath = namedtuple("StandingPath", "fname standing")

def parse_file(file_name):
    return parse_standing_from_file(config.XML_DIR + file_name)

def get_files(mypath):
    """Get list of files in samples directory"""
    return [f for f in listdir(mypath) if isfile(join(mypath, f))]

logger = logging.getLogger(__name__)

class DataRepository:

    def __init__(self):
        self.__set_data()
        self.__init_watchers()
        self.__init_timer()

    def __set_data(self):
        self.standings, self.total_standing = self.read_data_from_files()
        self.last_update = str(datetime.now())

    def read_data_from_files(self):

        files = filter(lambda fname: fname.endswith(".xml"), get_files(config.XML_DIR))
        standings = list(map(lambda fname: StandingPath(fname, parse_file(fname)), files))
        total_standing = TotalStandings(list(map(lambda s: s.standing, standings)))
        return standings, total_standing

    def get_contests_files(self):
        """returns list of { name: 'Contest 1', path: 'contest1.xml' }"""
        files = map(lambda t: {"path": t.fname, "name": t.standing.contest.name}, self.standings)
        return sorted(files, key=lambda t: t["name"])

    def get_total_standings(self):
        return self.total_standing

    def get_all_standings(self):
        return list(map(lambda t: t.standing, self.standings))

    def get_contest_standing(self, file_name):
        return next(t.standing for t in self.standings if t.fname == file_name)

    def __init_watchers(self):
        event_handler = FSEventHandler(self.__set_data)
        self.observer = Observer()
        self.observer.schedule(event_handler, config.XML_DIR)
        self.observer.start()

    def __init_timer(self):
        try:
            self.__set_data()
        except Exception as e:
            logger.error(e)
        threading.Timer(config.AUTO_REFRESH_SECONDS, self.__init_timer).start()

@@ -124,14 +124,8 @@
 port
- time,
  thr
@@ -140,16 +140,16 @@
 logging%0A
+
 from wat
@@ -715,34 +715,32 @@
 ):%0A        self.
-__
 set_data()%0A     
@@ -802,18 +802,16 @@
     def 
-__
 set_data
@@ -810,32 +810,49 @@
 set_data(self):%0A
+        try:%0A    
         self.sta
@@ -913,24 +913,28 @@
 s()%0A        
+  
+  
 self.last_up
@@ -960,16 +960,76 @@
 .now())%0A
+        except Exception as e:%0A            logger.error(e)%0A%0A
 %0A    def
@@ -1991,26 +1991,24 @@
 andler(self.
-__
 set_data)%0A  
@@ -2161,33 +2161,16 @@
 (self):%0A
-        try:%0A    
         
@@ -2174,18 +2174,16 @@
    self.
-__
 set_data
@@ -2189,67 +2189,8 @@
 a()%0A
-        except Exception as e:%0A            logger.error(e)%0A
     

Update repository