A R Karthick | a974c21 | 2017-07-17 19:47:12 -0700 | [diff] [blame^] | 1 | from webserver import app |
| 2 | from flask import request, jsonify |
| 3 | import httplib |
| 4 | import json |
| 5 | import os |
| 6 | import sys |
| 7 | |
| 8 | class CordTesterWebConfig(object): |
| 9 | our_path = os.path.dirname(os.path.realpath(__file__)) |
| 10 | test_base = os.path.realpath(os.path.join(our_path, '..', '..')) |
| 11 | |
| 12 | def __init__(self, test_case): |
| 13 | self.test_case = test_case |
| 14 | self.test_path = None |
| 15 | self.test_config = None |
| 16 | test_path = os.path.join(self.test_base, self.test_case) |
| 17 | if os.path.isdir(test_path): |
| 18 | self.test_path = test_path |
| 19 | self.test_config = os.path.join(self.test_path, '{}Test.json'.format(self.test_case)) |
| 20 | |
| 21 | def update(self, config): |
| 22 | cur_config = {} |
| 23 | if self.test_config: |
| 24 | if os.access(self.test_config, os.F_OK): |
| 25 | with open(self.test_config, 'r') as f: |
| 26 | cur_config = json.load(f) |
| 27 | os.rename(self.test_config, '{}.save'.format(self.test_config)) |
| 28 | for k, v in config.iteritems(): |
| 29 | cur_config[k] = v |
| 30 | with open(self.test_config, 'w') as f: |
| 31 | json.dump(cur_config, f, indent = 4) |
| 32 | return True |
| 33 | return False |
| 34 | |
| 35 | def restore(self): |
| 36 | if self.test_config: |
| 37 | if os.access(self.test_config, os.F_OK): |
| 38 | restore_file = '{}.save'.format(self.test_config) |
| 39 | if os.access(restore_file, os.F_OK): |
| 40 | os.rename(restore_file, self.test_config) |
| 41 | return True |
| 42 | return False |
| 43 | |
| 44 | def get(self): |
| 45 | cur_config = {} |
| 46 | if self.test_config: |
| 47 | if os.access(self.test_config, os.F_OK): |
| 48 | with open(self.test_config) as f: |
| 49 | cur_config = json.load(f) |
| 50 | return cur_config |
| 51 | |
| 52 | @app.route('/') |
| 53 | @app.route('/index') |
| 54 | def index(): |
| 55 | return 'Welcome to Cord Tester configuration page' |
| 56 | |
| 57 | @app.route('/get') |
| 58 | def get(): |
| 59 | data = request.get_json(force = True) |
| 60 | test_case = data.get('test_case', None) |
| 61 | if test_case: |
| 62 | cordWeb = CordTesterWebConfig(test_case) |
| 63 | config = cordWeb.get() |
| 64 | return jsonify(config) |
| 65 | return ('', httplib.NOT_FOUND) |
| 66 | |
| 67 | @app.route('/update', methods = ['POST']) |
| 68 | def update(): |
| 69 | data = request.get_json(force = True) |
| 70 | test_case = data.get('test_case', None) |
| 71 | config = data.get('config', None) |
| 72 | response = ('', httplib.NOT_FOUND) |
| 73 | if test_case: |
| 74 | cordWeb = CordTesterWebConfig(test_case) |
| 75 | status = cordWeb.update(config) |
| 76 | if status: |
| 77 | response = ('', httplib.OK) |
| 78 | |
| 79 | return response |
| 80 | |
| 81 | @app.route('/restore', methods = ['POST']) |
| 82 | def restore(): |
| 83 | data = request.get_json(force = True) |
| 84 | test_case = data.get('test_case', None) |
| 85 | response = ('', httplib.NOT_FOUND) |
| 86 | if test_case: |
| 87 | cordWeb = CordTesterWebConfig(test_case) |
| 88 | status = cordWeb.restore() |
| 89 | if status: |
| 90 | response = ('', httplib.OK) |
| 91 | return response |