| 1 | import json, time, ConfigParser |
|---|
| 2 | from copy import deepcopy |
|---|
| 3 | |
|---|
| 4 | # CAD comment log Watcher |
|---|
| 5 | # Look for changes in the CAD comment log. |
|---|
| 6 | # The CADserver will append new comments to the log as they arrive |
|---|
| 7 | # from clients. We only need to keep track of the log length in order to |
|---|
| 8 | # determine if a new comment has been added. We will output the |
|---|
| 9 | # new messages that arrived during the last wait interval. |
|---|
| 10 | # jdalbey 7/6/2019 |
|---|
| 11 | |
|---|
| 12 | lastLineNum = 0 |
|---|
| 13 | |
|---|
| 14 | # Utility functions |
|---|
| 15 | def isEmpty(cmsitem): |
|---|
| 16 | return cmsitem == ",,,,," |
|---|
| 17 | def isFull(cmsitem): |
|---|
| 18 | return not isEmpty(cmsitem) |
|---|
| 19 | |
|---|
| 20 | # Read the cad comments log |
|---|
| 21 | def readFile(): |
|---|
| 22 | # get path to input file from configuration |
|---|
| 23 | config = ConfigParser.ConfigParser() |
|---|
| 24 | config.read('config/logging_service.cfg') |
|---|
| 25 | logfilepath = config.get('Paths', 'UnifiedLogPath') |
|---|
| 26 | |
|---|
| 27 | lines = [] |
|---|
| 28 | try: |
|---|
| 29 | text_file = open(logfilepath + "CADcomments.log", "r") |
|---|
| 30 | except IOError: |
|---|
| 31 | print "Error: missing "+logfilepath+"CADcomments.log file." |
|---|
| 32 | else: |
|---|
| 33 | lines = text_file.read().split('\n') |
|---|
| 34 | text_file.close() |
|---|
| 35 | return lines |
|---|
| 36 | |
|---|
| 37 | def setup(): |
|---|
| 38 | # nothing needed for setup |
|---|
| 39 | return |
|---|
| 40 | |
|---|
| 41 | # Retrieve new messages from CAD comment log |
|---|
| 42 | def getLogEntries(): |
|---|
| 43 | global lastLineNum |
|---|
| 44 | msgList = readFile() |
|---|
| 45 | currList = [] |
|---|
| 46 | currList = msgList[lastLineNum:] # new items since last file read |
|---|
| 47 | lastLineNum = len(msgList)-1 |
|---|
| 48 | return currList |
|---|
| 49 | |
|---|
| 50 | # Local main for unit testing |
|---|
| 51 | def main(): |
|---|
| 52 | setup() |
|---|
| 53 | # Loop Forever, checking every five seconds |
|---|
| 54 | while True: |
|---|
| 55 | # Look for new messages |
|---|
| 56 | answer = getLogEntries() |
|---|
| 57 | # Output results |
|---|
| 58 | for item in answer: |
|---|
| 59 | print item |
|---|
| 60 | # wait |
|---|
| 61 | time.sleep(5) |
|---|
| 62 | |
|---|
| 63 | if __name__ == "__main__": |
|---|
| 64 | main() |
|---|