import configparser import socket import json from pydoc import locate IMAGES_FOLDER = '../../src/images' CONFIG_FILE = "../config/socket_config.ini" GRAMMAR_FILE = "../config/grammar.json" class Device(): """ Generic class for simulators. Initializes the sockets and the grammar, reading the CONFIG_FILE. Classes inheriting from it must call the __init__ function with their name (Telescope, CameraVIS, ...) """ def __init__(self, simul_name): config = configparser.ConfigParser() config.read(CONFIG_FILE) ip = config.get(simul_name, "ip") port = int(config.get(simul_name, "port")) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind((ip, port)) self.sock.listen(122) self.set_msgs = [] self.get_msgs = [] self.do_msgs = [] self.enums = {} with open(GRAMMAR_FILE) as grammar_file: grammar = json.load(grammar_file) enums = grammar["Enums"] for enum in enums : self.enums[enum] = enums[enum] if simul_name[:6] == "Camera": device = grammar["Camera"] for item in device["set"]: param_type = item["param_type"] if len(param_type) < 4 or param_type[:4] != "Enum": ''' This transforms 'int' string to type ''' param_type = locate(param_type) self.set_msgs.append((item["name"], item["nb_param"], param_type)) for item in device["get"]: self.get_msgs.append(item) for item in device["do"]: param_type = item["param_type"] if len(param_type) < 4 or param_type[:4] != "Enum": param_type = locate(param_type) self.do_msgs.append((item["name"], item["nb_param"], param_type)) device = grammar[simul_name] for item in device["set"]: param_type = item["param_type"] if len(param_type) < 4 or param_type[:4] != "Enum": param_type = locate(param_type) self.set_msgs.append((item["name"], item["nb_param"], param_type)) for item in device["get"]: self.get_msgs.append(item) for item in device["do"]: param_type = item["param_type"] if len(param_type) < 4 or param_type[:4] != "Enum": param_type = locate(param_type) self.do_msgs.append((item["name"], item["nb_param"], param_type))