from pathlib import Path
import contextlib
import os
from datetime import timedelta, datetime

class local_dir_remote:
    def __init__(self, config):
        self.config = config
        self.path = config["path"]

    def list_dir(self):
        path = Path(self.path)
        file_list = []
        delta = timedelta(minutes=2)
        for file_demo in list(path.glob('*')):
            mtime = datetime.fromtimestamp(os.stat(file_demo).st_mtime)
            if mtime < datetime.now() - delta:
                file_list.append(file_demo)
        return file_list

    def delete_local(self, pathfile):
        with contextlib.suppress(FileNotFoundError):
            if str(pathfile).endswith('.dem'):
                os.remove(pathfile)
                print('removed demo: ', pathfile)