diff options
author | Hrishi Hiraskar | 2016-09-08 17:58:49 +0530 |
---|---|---|
committer | GitHub | 2016-09-08 17:58:49 +0530 |
commit | 9ec9152b636591ca4d9bf85e1fc0322c57bb9fd7 (patch) | |
tree | 54b1f7177039752cad790921e7e6beddde1fe8b8 | |
parent | b688887d0371e270fb42d40449b0d2fb56819e22 (diff) | |
download | xcos-on-web-9ec9152b636591ca4d9bf85e1fc0322c57bb9fd7.tar.gz xcos-on-web-9ec9152b636591ca4d9bf85e1fc0322c57bb9fd7.tar.bz2 xcos-on-web-9ec9152b636591ca4d9bf85e1fc0322c57bb9fd7.zip |
Update SendLog.py
-rwxr-xr-x | testing/SendLog/SendLog.py | 82 |
1 files changed, 67 insertions, 15 deletions
diff --git a/testing/SendLog/SendLog.py b/testing/SendLog/SendLog.py index b96f24e..71f62c8 100755 --- a/testing/SendLog/SendLog.py +++ b/testing/SendLog/SendLog.py @@ -1,5 +1,5 @@ ## Hrishi Hiraskar -## 31 August 2016 +## 8 September 2016 import gevent import time @@ -11,24 +11,77 @@ monkey.patch_all() app = Flask(__name__) +# Delay time to look for new line (in s) +LOOK_DELAY = 0.01 + +def get_line(file): + # Function to get a new line from file + line = file.readline() + if not line: + # If no new line is found + # Wait for some time and look again + gevent.sleep(LOOK_DELAY) + return get_line(file) + else: + return line + +def parse_line(line): + # Function to parse the line + # Returns fig ID if new fig created + # -1 if current fig end + # -2 otherwise + linewords = line.split(' ') + if linewords.count("Initialization") == 1: + # New fig created + # Get fig id + figID = int(linewords[-1]) + return figID + elif linewords.count("Ending") == 1: + # Current fig end + return -1 + else: + # Current fig coordinates + return -2 + def event_stream(): + # List to store fig IDs + figList = [] + # Log file directory + log_dir = "../../bin/" + # Log file name + log_name = "scilab-log-0.txt" # Open the log file - log_dir = "../../bin/" # Log file directory - log_name = "scilab-log-0.txt" # Log file name logfile = open(log_dir + log_name, "r") - # Seek file pointer to the end of file + # Seek the file pointer to the end of file logfile.seek(0,2) + figID = 0 + # This loop is to get the first fig ID + while len(figList) == 0: + line = get_line(logfile) + # Check if there is new fig + r = parse_line(line) + if r >= 0: + # Add fig ID to list + figID = r + figList.append(figID) + break # Start sending log - LOOK_DELAY = 0.01 # Delay time to look for new line (in s) - while 1: - line = logfile.readline() - if not line: - # If no new line is found - # Wait for some time and continue - gevent.sleep(LOOK_DELAY) - continue - print(line) - yield "event: log\ndata: "+line+"\n\n"; + while len(figList) > 0: + line = get_line(logfile) + # Parse the line to check state + r = parse_line(line) + # Check if there is new fig + if r >= 0: + # Add fig ID to list + figID = r + figList.append(figID) + elif r == -1: + # End of fig + # Remove fig ID from list + figList.remove(figID) + else: + yield "event: log\ndata: "+line+"\n\n"; + # Finished Sending Log @app.route('/SendLog') def sse_request(): @@ -43,4 +96,3 @@ if __name__ == '__main__': # Set server address 127.0.0.1:8001/ http_server = WSGIServer(('127.0.0.1', 8001), app) http_server.serve_forever() - |