Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reload csv on update #321

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions boswatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import logging
import logging.handlers

import argparse # for parse the args
import ConfigParser # for parse the config file
import os # for log mkdir
import sys # for py version
import time # for time.sleep()
import subprocess # for starting rtl_fm and multimon-ng
import argparse # for parse the args
import ConfigParser # for parse the config file
import os # for log mkdir
import time # for time.sleep()
import subprocess # for starting rtl_fm and multimon-ng

## New for reloading csv
import pyinotify
import threading

from includes import globalVars # Global variables
from includes import MyTimedRotatingFileHandler # extension of TimedRotatingFileHandler
Expand Down Expand Up @@ -68,6 +71,42 @@
print "ERROR: cannot parsing the arguments"
exit(1)

#
# define a function for observing csv-files
#
def csv_watch(dir):
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE | pyinotify.IN_MODIFY
t = threading.currentThread()
logging.debug("CSV-Watch-Dir: %s", dir)

class EventHandler(pyinotify.ProcessEvent):
def process_IN_CREATE(self, event):
try:
logging.debug("Reloading csv...")
if globalVars.config.getboolean("FMS","idDescribed") or globalVars.config.getboolean("ZVEI","idDescribed") or globalVars.config.getboolean("POC","idDescribed"):
from includes import descriptionList
descriptionList.loadDescriptionLists()
except:
# It's an error, but we could work without that stuff...
logging.error("cannot reload description lists")
logging.debug("cannot reload description lists", exc_info=True)
def process_IN_MODIFY(self, event):
try:
logging.debug("Reloading csv...")
if globalVars.config.getboolean("FMS","idDescribed") or globalVars.config.getboolean("ZVEI","idDescribed") or globalVars.config.getboolean("POC","idDescribed"):
from includes import descriptionList
descriptionList.loadDescriptionLists()
except:
# It's an error, but we could work without that stuff...
logging.error("cannot reload description lists")
logging.debug("cannot reload description lists", exc_info=True)

handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(dir, mask, rec=True)

notifier.loop()

#
# Main program
Expand Down Expand Up @@ -158,14 +197,23 @@
logging.error("cannot clear Logfiles")
logging.debug("cannot clear Logfiles", exc_info=True)

#
# start a new oberserver.thread
#
try:
thread = threading.Thread(target = csv_watch, args = (globalVars.script_path+'/csv/',))
thread.daemon = True # start it as daemon to avoid trouble when exiting Boswatch
thread.start()
logging.debug("Thread for csv-watch started")
except:
logging.error("Unable to start thread to observe csv-directory.", exc_info=True)

#
# For debug display/log args
#
try:
logging.debug("SW Version: %s",globalVars.versionNr)
logging.debug("Branch: %s",globalVars.branch)
logging.debug("Build Date: %s",globalVars.buildDate)
logging.debug("Python Vers: %s",sys.version)
logging.debug("BOSWatch given arguments")
if args.test:
logging.debug(" - Test-Mode!")
Expand Down Expand Up @@ -225,9 +273,6 @@
configHandler.checkConfig("FMS")
configHandler.checkConfig("ZVEI")
configHandler.checkConfig("POC")
configHandler.checkConfig("Plugins")
configHandler.checkConfig("Filters")
#NMAHandler is outputed below
except:
# we couldn't work without config -> exit
logging.critical("cannot read config file")
Expand All @@ -248,7 +293,6 @@
logging.error("cannot set loglevel of fileHandler")
logging.debug("cannot set loglevel of fileHandler", exc_info=True)


# initialization was fine, continue with main program...

#
Expand Down Expand Up @@ -368,7 +412,7 @@
logging.info("Testdata: %s", testData.rstrip(' \t\n\r'))
from includes import decoder
decoder.decode(freqConverter.freqToHz(args.freq), testData)
#time.sleep(1)
time.sleep(5)
logging.debug("test finished")

except KeyboardInterrupt:
Expand Down