Skip to content

Commit 85f903d

Browse files
committed
folder organization for protocols
1 parent 66bc871 commit 85f903d

24 files changed

Lines changed: 35 additions & 4 deletions

classes/protocol_settings.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import csv
22
from dataclasses import dataclass
33
from enum import Enum
4+
import glob
45
from typing import Union
56
from defs.common import strtoint
67
import itertools
@@ -300,7 +301,12 @@ def load__json(self, file : str = '', settings_dir : str = ''):
300301
if not file:
301302
file = self.protocol + '.json'
302303

303-
path = settings_dir + '/' + file
304+
path = self.find_protocol_file(file, settings_dir)
305+
306+
#if path does not exist; nothing to load. skip.
307+
if not path:
308+
print("ERROR: '"+file+"' not found")
309+
return
304310

305311
with open(path) as f:
306312
self.codes = json.loads(f.read())
@@ -618,6 +624,23 @@ def calculate_registry_ranges(self, map : list[registry_map_entry], max_register
618624
ranges.append((min(registers), max(registers)-min(registers)+1)) ## APPENDING A TUPLE!
619625

620626
return ranges
627+
def find_protocol_file(self, file : str, base_dir : str = '' ) -> str:
628+
629+
path = base_dir + '/' + file
630+
if os.path.exists(path):
631+
return path
632+
633+
suffix = file.split('_', 1)[0]
634+
635+
path = base_dir + '/' + suffix +'/' + file
636+
if os.path.exists(path):
637+
return path
638+
639+
#find file by name, recurisvely. last resort
640+
search_pattern = os.path.join(base_dir, '**', file)
641+
matches = glob.glob(search_pattern, recursive=True)
642+
return matches[0] if matches else None
643+
621644

622645
def load_registry_map(self, registry_type : Registry_Type, file : str = '', settings_dir : str = ''):
623646
if not settings_dir:
@@ -629,10 +652,10 @@ def load_registry_map(self, registry_type : Registry_Type, file : str = '', sett
629652
else:
630653
file = self.protocol + '.'+registry_type.name.lower()+'_registry_map.csv'
631654

632-
path = settings_dir + '/' + file
655+
path = self.find_protocol_file(file, settings_dir)
633656

634657
#if path does not exist; nothing to load. skip.
635-
if not os.path.exists(path):
658+
if not path:
636659
return
637660

638661
self.registry_map[registry_type] = self.load__registry(path, registry_type)
File renamed without changes.
File renamed without changes.
File renamed without changes.

protocols/growatt_2020_v1.24.holding_registry_map.csv renamed to protocols/growatt/growatt_2020_v1.24.holding_registry_map.csv

File renamed without changes.

protocols/growatt_2020_v1.24.input_registry_map.csv renamed to protocols/growatt/growatt_2020_v1.24.input_registry_map.csv

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)