

# https://www.globalsino.com/ICs/
# Program function: monitor specific new files and execute the file

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import datetime as dt
import os

print('To stop the program, press Control_C.')

class OnMyWatch:
    # Directory on watch
    watchDirectory = r"C:\Users\yyliao\Downloads"
  
    def __init__(MyChecking):
        MyChecking.observer = Observer()
  
    def run(MyChecking):
        event_handler = Handler()
        MyChecking.observer.schedule(event_handler, MyChecking.watchDirectory, recursive = True)
        MyChecking.observer.start()
        try:
            while True:
                time.sleep(2)
        except:
            MyChecking.observer.stop()
            print("Observer Stopped")
  
        MyChecking.observer.join()  
  
class Handler(FileSystemEventHandler):  # Define program execution
    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None  
        elif event.event_type == 'created':
            # Event is created            
            path = event.src_path              
            # Split the path in root and ext pair
            root_ext = os.path.splitext(path)
            # The extension
            theExtension = root_ext[1]            
            print("There are uploaded new files")            
            if theExtension == ".dm4":
                print(event.src_path)                
            else:
                if theExtension == ".csv":
                    print(event.src_path)
                else:
                    if theExtension == ".pdf":
                        print(event.src_path)
                        print(os.path.dirname(event.src_path))
                        os.startfile(event.src_path)
                    else:
                         print("Not any of those extension\n")
        
  
if __name__ == '__main__':
    watch = OnMyWatch()
    watch.run()
