Skip to content
Snippets Groups Projects

Resolve #2021 "04 16 branched from master ini file device"

Merged Taya Snijder requested to merge 2021-04-16-Branched_from_master-INI_file_device into master
Files
3
@@ -2,6 +2,8 @@ from util.comms_client import CommClient
import configparser
import numpy
__all__ = ["ini_client"]
numpy_to_ini_dict = {
numpy.int64: int,
@@ -30,9 +32,8 @@ import os
class ini_client(CommClient):
"""
This class provides an example implementation of a comms_client.
During initialisation, it creates a correctly shaped zero-filled value. On read, that value is returned, and on write it is modified.
this class provides an example implementation of a comms_client.
Durirng initialisation it creates a correctly shaped zero filled value. on read that value is returned and on write its modified.
"""
def start(self):
@@ -45,10 +46,6 @@ class ini_client(CommClient):
self.config = configparser.ConfigParser()
self.filename = filename
if not filename.endswith(".ini"):
filename = filename + ".ini"
super().__init__(fault_func, streams, try_interval)
# Explicitly connect
@@ -58,18 +55,10 @@ class ini_client(CommClient):
return
def connect(self):
files_path = [os.path.abspath(x) for x in os.listdir()]
self.streams.debug_stream(" %s", files_path)
try:
write_config(self.filename)
self.config_file = open(self.filename, "r")
except FileNotFoundError:
write_config(self.filename)
self.config_file = open(self.filename, "r")
self.config_file = open(self.filename, "r")
self.connected = True # set connected to true
return True # if succesfull, return true. otherwise return false
return True # if successful, return true. otherwise return false
def disconnect(self):
self.connected = False # always force a reconnect, regardless of a successful disconnect
@@ -84,16 +73,21 @@ class ini_client(CommClient):
example annotation may include:
- a file path and file line/location
- COM object path
Annotations:
name: Required, the name of the ini variable
section: Required, the section of the ini variable
"""
# as this is an example, just print the annotation
self.streams.debug_stream("annotation: {}".format(annotation))
name = annotation.get('name')
if name is None:
AssertionError("ini client requires a variable name to set/get")
ValueError("ini client requires a variable `name` in the annotation to set/get")
section = annotation.get('section')
if section is None:
AssertionError("requires a section to open")
ValueError("requires a `section` specified in the annotation to open")
return section, name
@@ -120,7 +114,7 @@ class ini_client(CommClient):
self.config.read_file(self.config_file)
value = self.config.get(section, name)
value = array(value, dtype)
value = data_handler(value, dtype)
if dim_y > 1:
# if data is an image, slice it according to the y dimensions
@@ -131,13 +125,8 @@ class ini_client(CommClient):
def write_function(value):
if type(value) is list:
write_value = ""
for i in value:
write_value = write_value + str(value) + ", "
write_value = ", ".join([str(v) for v in value])
# strip the extra ", " at the end
write_value = write_value[:-2]
else:
write_value = str(value)
@@ -166,73 +155,38 @@ class ini_client(CommClient):
# return the read/write functions
return read_function, write_function
def array(string, dtype):
array = []
def data_handler(string, dtype):
value = []
if dtype is numpy.bool_:
# Handle special case for Bools
for i in string.split(","):
i.replace("", "")
if "True" in i:
array.append(True)
elif "False" in i:
array.append(False)
i = i.strip(" ")
if "True" == i:
value.append(True)
elif "False" == i:
value.append(False)
else:
raise ValueError("String to bool failed. String is not True/False, but is: '{}'".format(i))
array = dtype(array)
value = dtype(value)
elif dtype is numpy.str_:
for i in string.split(","):
value = numpy.str_(i)
array.append(value)
val = numpy.str_(i)
value.append(val)
array = numpy.array(array)
value = numpy.array(value)
else:
# regular case, go through the separator
for i in string.split(","):
i.replace(" ", "")
value = dtype(i)
array.append(value)
i = i.replace(" ", "")
val = dtype(i)
value.append(val)
# convert values from buildin type to numpy type
array = dtype(array)
return array
def write_config(filename):
with open(filename, 'w') as configfile:
config = configparser.ConfigParser()
config['scalar'] = {}
config['scalar']['double_scalar_R'] = '1.2'
config['scalar']['double_scalar_RW'] = '3.4'
config['scalar']['bool_scalar_R'] = 'True'
config['scalar']['bool_scalar_RW'] = 'False'
config['scalar']['int_scalar_R'] = '5'
config['scalar']['int_scalar_RW'] = '6'
config['scalar']['str_scalar_R'] = 'this is'
config['scalar']['str_scalar_RW'] = 'a test'
config['spectrum'] = {}
config['spectrum']['double_spectrum_R'] = '1.2, 2.3, 3.4, 4.5'
config['spectrum']['double_spectrum_RW'] = '5.6, 6.7, 7.8, 9.0'
config['spectrum']['bool_spectrum_R'] = 'True, True, False, False'
config['spectrum']['bool_spectrum_RW'] = 'False, False, True, True'
config['spectrum']['int_spectrum_R'] = '1, 2, 3, 4'
config['spectrum']['int_spectrum_RW'] = '6, 7, 8, 9'
config['spectrum']['str_spectrum_R'] = '"a", "b", "c", "d"'
config['spectrum']['str_spectrum_RW'] = '"D", "E", "F", "G"'
config['image'] = {}
config['image']['double_image_R'] = '1.2, 2.3, 3.4, 4.5, 5.6, 6.7'
config['image']['double_image_RW'] = '5.6, 6.7, 7.8, 9.0, 1.2, 3.4'
config['image']['bool_image_R'] = 'True, True, False, False, True, False'
config['image']['bool_image_RW'] = 'False, False, True, True, False, Trie'
config['image']['int_image_R'] = '1, 2, 3, 4, 5, 6'
config['image']['int_image_RW'] = '6, 7, 8, 9, 10, 11'
config['image']['str_image_R'] = '"a", "b", "c", "d", "e", "f"'
config['image']['str_image_RW'] = '"D", "E", "F", "G", "H", "I"'
config.write(configfile)
value = dtype(value)
return value
Loading