| Revision 436,
1.1 KB
checked in by jdalbey, 7 years ago
(diff) |
|
cad_watcher.py added exception handling for missing log file
|
| Line | |
|---|
| 1 | import json, time |
|---|
| 2 | from copy import deepcopy |
|---|
| 3 | |
|---|
| 4 | # CAD comment log Watcher |
|---|
| 5 | # Look for changes in the CAD comment log |
|---|
| 6 | # jdalbey 7/6/2019 |
|---|
| 7 | |
|---|
| 8 | lastLineNum = 0 |
|---|
| 9 | |
|---|
| 10 | # Utility functions |
|---|
| 11 | def isEmpty(cmsitem): |
|---|
| 12 | return cmsitem == ",,,,," |
|---|
| 13 | def isFull(cmsitem): |
|---|
| 14 | return not isEmpty(cmsitem) |
|---|
| 15 | |
|---|
| 16 | # Read the cms message file |
|---|
| 17 | def readFile(): |
|---|
| 18 | lines = [] |
|---|
| 19 | try: |
|---|
| 20 | text_file = open("../../CADcomments.log", "r") |
|---|
| 21 | except IOError: |
|---|
| 22 | print "Error: missing CADcomments.log file." |
|---|
| 23 | else: |
|---|
| 24 | lines = text_file.read().split('\n') |
|---|
| 25 | text_file.close() |
|---|
| 26 | return lines |
|---|
| 27 | |
|---|
| 28 | def setup(): |
|---|
| 29 | # nothing needed for setup |
|---|
| 30 | return |
|---|
| 31 | |
|---|
| 32 | # Retrieve new messages from CAD comment log |
|---|
| 33 | def getLogEntries(): |
|---|
| 34 | global lastLineNum |
|---|
| 35 | msgList = readFile() |
|---|
| 36 | currList = [] |
|---|
| 37 | currList = msgList[lastLineNum:] # new items since last file read |
|---|
| 38 | lastLineNum = len(msgList)-1 |
|---|
| 39 | return currList |
|---|
| 40 | |
|---|
| 41 | def main(): |
|---|
| 42 | setup() |
|---|
| 43 | # Loop Forever |
|---|
| 44 | while True: |
|---|
| 45 | # Look for changed messages |
|---|
| 46 | answer = getLogEntries() |
|---|
| 47 | # Output results |
|---|
| 48 | for item in answer: |
|---|
| 49 | print item |
|---|
| 50 | # wait |
|---|
| 51 | time.sleep(5) |
|---|
| 52 | |
|---|
| 53 | if __name__ == "__main__": |
|---|
| 54 | main() |
|---|
Note: See
TracBrowser
for help on using the repository browser.