MeasKit/generator.py
2025-11-21 21:00:41 +01:00

89 lines
3.9 KiB
Python

import os
import re
functionBuffer = []
writeBuffer = []
with open(f'equipment/multimeter/md_sourcefiles/3458A.md') as file:
current_block = ''
command_block = ''
for line in file:
line = line.lstrip(' ')
line = line.replace('\n', '')
# check if descriptor block
try:
sub_block = line.split(' ', 1)[0]
if sub_block == '#':
command_block = line.split(' ', 1)[1]
writeBuffer.append(line)
if 'LIBRARY COMMANDS' in command_block:
current_block = 'FUNCTION'
if 'SETTEABLE PARAMETERS' in command_block:
current_block = 'SETTER'
if 'GETTEABLE PARAMETERS' in command_block:
current_block = 'GETTER'
if 'GETSETTEABLE PARAMETERS' in command_block:
current_block = 'GETSETTER'
# if sub_block a getter/setter make it
if sub_block == '-':
functionBuffer.append({"NAME":line.split(' ', 1)[1],"TYPE":current_block})
# if sub_block make a function/parameter
if sub_block == '1.':
functionBuffer[-1].update({"DESCRIPTION":line.split(' ', 1)[1]})
if sub_block == '2.':
functionBuffer[-1].update({"DEVICE_COMMAND":line.split(' ', 1)[1]})
if sub_block == '3.':
functionBuffer[-1].update({"FUNCTION_CALL":line.split(' ', 1)[1]})
except:
pass
writeBuffer = ''
for functionality in functionBuffer:
tmpFunc = ''
# check what kind of function is needed
print(functionality)
try:
tmp = functionality["NAME"]
tmp_cmd_name = tmp.lstrip(':')
tmp_cmd_name = tmp_cmd_name.replace(':', '_')
tmp_cmd_name = re.sub(r'[^a-zA-Z0-9_]', '', tmp_cmd_name)
if functionality["TYPE"] == "FUNCTION":
tmpFunc += (f'\tdef out = {tmp_cmd_name}:\n')
if functionality["TYPE"] == "SETTER":
tmpFunc += (f'\tdef {tmp_cmd_name}(self, input = None):\n')
if functionality["TYPE"] == "GETTER":
tmpFunc += (f'\tdef {tmp_cmd_name}(self):\n')
if functionality["TYPE"] == "GETSETTER":
tmpFunc += (f'\tdef {tmp_cmd_name}(self, input = None):\n')
# add description
if "DESCRIPTION" in functionality:
tmp = functionality["DESCRIPTION"]
tmpFunc += (f'\t\t\'\'\'{tmp}\'\'\'\n')
# add command if not function
if not "DEVICE_COMMAND" in functionality:
tmp = functionality["NAME"]
tmpFunc += (f'\t\tself.pyvisa_instrument_object.write(f\'{tmp} ' + '{str(input)}\')\n')
else:
tmp = functionality["DEVICE_COMMAND"]
tmpFunc += (f'\t\t{tmp}\n')
# add return to getters
if functionality["TYPE"] == "GETTER":
tmpFunc += (f'\t\treturn self.pyvisa_instrument_object.read()\n')
if functionality["TYPE"] == "GETSETTER":
tmpFunc += (f'\t\treturn self.pyvisa_instrument_object.read()\n')
writeBuffer += (tmpFunc+'\n')
except:
pass
with open("demofile.py", "a") as f:
# add base constructor stuff
f.write('import pyvisa\n\n')
f.write('class DS1054Z(object):\n')
f.write('\tdef __init__(self, pyvisa_instrument_address, device_handle):\n')
f.write('\t\t# initiate correct device class and pass pyvisa handle\n')
f.write('\t\tself.pyvisa_instrument_address = pyvisa_instrument_address\n')
f.write('\t\tself.pyvisa_instrument_manager = pyvisa.ResourceManager()\n')
f.write('\t\tself.pyvisa_instrument_object = self.pyvisa_instrument_manager.open_resource(pyvisa_instrument_address)\n')
f.write('\t\tself.pyvisa_instrument_object = self.pyvisa_instrument_object\n')
f.write('\t\tself.device_handle = device_handle\n')
f.write('\t\tprint(f"[MeasKit] started control class instance for {device_handle} at {pyvisa_instrument_address}.")\n')
f.write(writeBuffer)