Commit d4b226f5 authored by Pedro Duarte's avatar Pedro Duarte
Browse files

init device translation module

parent 615beddd
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
import yaml


with open('config/config.yml') as cfg:
    config : dict = yaml.safe_load(cfg)

dict_name = config.get('name')
dict_default = config.get('default')


class DictHelper:
    def __init__(self, file):
        self.file = file

    def _read(self) -> dict:
        with open(self.file) as cfg:
            return yaml.safe_load(cfg)

    def get_object(self, name, plural=False) -> str:
        return self._read().get(name).get('plural' if plural else 'singular')
        
    def get_namespaces(self, name) -> str:
        return self._read().get(name)
        
    def get_path(self, name) -> str:
        path_details = self._read().get(name)

        path : str = path_details.get('path')
        namespace = path_details.get('namespace')

        if namespace is None:
            return path

        path_segments = path.split('/')
        required_namespaces = len([s for s in path_segments if len(s > 1)])

        if type(namespace) == list and len(namespace) != required_namespaces:
            raise Exception(f'Number of namespaces do not match the specified path : {path}')
        
        elif type(namespace) == str:
            namespace = [namespace for _ in range(required_namespaces)]

        ns_i = 0
        processed_segments = []
        for s in path_segments:
            if len(s) > 1:
                processed_segments.append(f'{namespace[ns_i]}:{s}')
                ns_i += 1
            else:
                processed_segments.append(s)


def get_dict_helper(dict_type = None) -> DictHelper:
    if dict_type is None: dict_type = dict_default
    return DictHelper(f'config/{dict_name}.{dict_type}.yml')
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
name: dict
default: oc
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
namespaces:
  interfaces: ipii
  interfaces_ip: ipiiip

objects:
  components:
    singular: interface
    plural: interfaces

paths:
  component_type:
    namespace: ipiie
    path: extended/state/hardware-type
 No newline at end of file
+13 −0
Original line number Diff line number Diff line
namespaces:
  interfaces: oci
  interfaces_ip: ociip

objects:
  components:
    singular: component
    plural: components

paths:
  component_type:
    namespace: ocpp
    path: port/breakout-mode/state/channel-speed
 No newline at end of file