Made the code more robust and universal.
This commit is contained in:
56
software/test_software/modbus_definition_file_reader.py
Normal file
56
software/test_software/modbus_definition_file_reader.py
Normal file
@@ -0,0 +1,56 @@
|
||||
""""
|
||||
ModBus definition file reader routines
|
||||
Copyright (C) 2025 M.T. Konstapel
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
"""
|
||||
import yaml
|
||||
from yaml.loader import SafeLoader
|
||||
|
||||
class definition_file_reader:
|
||||
|
||||
# initiate class: define name configuration files
|
||||
def __init__(self, definition_file):
|
||||
self.definition_file = definition_file
|
||||
|
||||
def read_settings(self):
|
||||
|
||||
if self.read_definition_file() == 0:
|
||||
return 0
|
||||
|
||||
if self.test_definition_file() == 0:
|
||||
return 0
|
||||
|
||||
return 1
|
||||
|
||||
def read_definition_file (self):
|
||||
try:
|
||||
with open(self.definition_file) as f:
|
||||
self.definition_file_data = yaml.load(f, Loader=SafeLoader)
|
||||
except:
|
||||
print ("Definition file ./" + self.definition_file + " not found or syntax error in file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
# Test if all settings are pressent
|
||||
def test_definition_file(self):
|
||||
# Test is all expected settings are present
|
||||
try:
|
||||
tmp = self.definition_file_data['devices']
|
||||
except:
|
||||
print ("Error in the ModBus definition file.")
|
||||
return 0
|
||||
else:
|
||||
return 1
|
Reference in New Issue
Block a user