GithubHelp home page GithubHelp logo

python-directory-watcher's Introduction

PYTHON-DIRECTORY-WATCHER

Description:

Python-Directory-Watcher is a command line python app that monitors a specified directory for file created events,when the correct number of files get created in a directory an event is raised which runs a specified batch file.The app works well in the windows enviroments primary due to permission issues on other OS like linux or mac. The app uses a backend Sqlite3 database to store information about the directories that need to be monitored.

Example:

from peewee import *
from mywatchdog import *

db = SqliteDatabase('directories.db')

loopCount = 0

class Directory(Model):
  monitorDirectory = CharField(max_length=500,unique=True)
  batchFileDirectory = CharField(max_length=500)
  fileTrigger = IntegerField(null=False)
  
  class Meta:
    database = db


def manageInput(mystring):
  """This function takes a string and depending on the version of 
  python calls either raw_input or input function."""
  try:
    myInput = raw_input(mystring)
  except:
    myInput = input(mystring)
  return myInput      


def add_directories(my_dir,my_batchfile_path,trigger):
  """This function takes 3 arguments, The directory to monitor,the batchfilepath, 
  and the trigger( or the number of files that need 
  to be created before the batchfile runs)""" 

  try:
    Directory.create(monitorDirectory=my_dir
      ,batchFileDirectory=my_batchfile_path,fileTrigger=trigger)
    return " \n Directory added"
  except IntegrityError:
    directory = Directory.get(monitorDirectory=my_dir)
    directory.batchFile = my_batchfile_path
    directory.fileTrigger= trigger
    directory.save()
    return "\n Directory saved"


def list_directories():
  print("\n *** LIST OF DIRECTORIES BEING WATCHED \n")
  if Directory.select().count()==0:
    print("No directories being monitored !!")
  else:
    for dir in Directory.select():
      print("\n {} {} {}".format(dir.id,dir.monitorDirectory,dir.fileTrigger))


def del_directories(my_id):
    """This function takes an id or Primary key 
  and deletes that record from the database."""

  print("\n")
  try:
    directory = Directory.get(Directory.id == my_id)
    directory.delete_instance()
    print("\n")
    return "The Directory has been deleted! "
  except Directory.DoesNotExist:
    return "The directory you are trying to delete does not exist"  


  
         
         

def show_menu():

  mystring ="TO ADD A DIRECTORY [A] \n"
  mystring+="TO DELETE A DIRECTORY FROM THE QUEUE [D] \n"
  mystring+="TO START MONITORINING DIRECTORIES [M]\n"
  mystring+="TO LIST DIRECTORIES [L],\n"
  mystring+="TO QUIT [Q] \n"
  print(mystring)     



if __name__ == '__main__':
  db.connect()
  db.create_tables([Directory],safe=True)

  while True:
    if loopCount == 0:
      print(" \n WELCOME TO PY-DIRECTORY-WATCHER.\n")
    loopCount+=1  
    print("\n")
    show_menu()
    user_input = manageInput(">>  ")
    if user_input.lower()=="q":
      break;
    if user_input.lower()=="a":
      mydir = manageInput("PATH TO YOU DIRECTORY \n ")
      batchfilepath = manageInput("PATH TO THE BATCH FILE INCLUDING THE FILENAME AND EXTENTION \n") 
      fileTrigger = 0
      fileTrigger = int(manageInput("No of files for automation trigger ! >>  "))
      if mydir == "" or batchfilepath =="" or fileTrigger==0:
        print("PLEASE ENTER INFORMATION PROPERLY!!")
      else:
        print(add_directories(mydir,batchfilepath,fileTrigger))  
      continue
    if user_input.lower()=="l":
      list_directories() 
      continue 
    if user_input.lower()=="m":
      try:
        directory = Directory.select()   # getting all the directories in the database
        mywatcher = Watcher(directory)   # passing the returned select object to the Watcher class
        mywatcher.watch()
      except Exception as e:
        print(str(e))
      continue  
    if user_input.lower()=="d":
      id = 0
      id = int(manageInput("What is the Id of the directory >>  "))
      if id==0:
        print("You have not supplied a directory !")
      else:
        print(del_directories(id))       
    else:
      print(" \n SORRY DID NOT RECOGNISE YOUR INPUT >>")
      continue 

Motivation:

Professionally I work as a data programmer, the idea for this app came from the need to automate some of my jobs at work. I set a directory to be monitored by the app,when the data processing software exports data to a directory with the correct number of files a batch file gets called running another application which creates print ready pdf's.

Installation:

  1. Download the zip or clone the repo
  2. Install watchdog with the command pip install watchdog
  3. Install peewee pip install peewee

Reference:

  1. [WatchDog] (https://pypi.python.org/pypi/watchdog)
  2. [Peewee] (http://docs.peewee-orm.com/en/latest/)

python-directory-watcher's People

Contributors

squadran2003 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.