objdictgen/eds_utils.py
author greg
Fri, 18 May 2007 13:41:50 +0200
changeset 202 9cfd9211616c
parent 182 988f2b302aa6
child 205 dac0f9b4e3f8
permissions -rw-r--r--
big-endian with concise dcf fixed
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     3
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     4
#This file is part of CanFestival, a library implementing CanOpen Stack. 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     5
#
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     6
#Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     7
#
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     8
#See COPYING file for copyrights details.
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
     9
#
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    10
#This library is free software; you can redistribute it and/or
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    11
#modify it under the terms of the GNU Lesser General Public
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    12
#License as published by the Free Software Foundation; either
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    13
#version 2.1 of the License, or (at your option) any later version.
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    14
#
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    15
#This library is distributed in the hope that it will be useful,
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    18
#Lesser General Public License for more details.
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    19
#
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    20
#You should have received a copy of the GNU Lesser General Public
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    21
#License along with this library; if not, write to the Free Software
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    22
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    23
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    24
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    25
import node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    26
from sets import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    27
from types import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    28
from time import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    29
import os,re
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    30
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    31
# Regular expression for finding index section names
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    32
index_model = re.compile('([0-9a-fA-F]{1,4})')
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    33
# Regular expression for finding subindex section names
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    34
subindex_model = re.compile('([0-9a-fA-F]{1,4})sub([0-9a-fA-F]{1,2})')
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    35
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    36
# Dictionary for quickly translate boolean into integer value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    37
BOOL_TRANSLATE = {True : "1", False : "0"}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    38
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    39
# Function for verifying data values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    40
is_integer = lambda x: type(x) == IntType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    41
is_string = lambda x: type(x) == StringType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    42
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    43
# Define checking of value for each attribute
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    44
ENTRY_ATTRIBUTES = {"SUBNUMBER" : is_integer, "PARAMETERNAME" : is_string, 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    45
                    "OBJECTTYPE" : lambda x: x in (7, 8, 9), "DATATYPE" : is_integer, 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    46
                    "LOWLIMIT" : is_integer, "HIGHLIMIT" : is_integer,
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    47
                    "ACCESSTYPE" : lambda x: x in ["ro","wo", "rw", "rwr", "rww", "const"],
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    48
                    "DEFAULTVALUE" : lambda x: True, "PDOMAPPING" : lambda x: x in (0, 1),
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    49
                    "OBJFLAGS" : is_integer}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    50
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    51
# Define entry parameters by entry ObjectType number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    52
ENTRY_TYPES = {7 : {"name" : " VAR",
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    53
                    "require" : ["PARAMETERNAME", "OBJECTTYPE", "DATATYPE", "ACCESSTYPE", "PDOMAPPING"],
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    54
                    "optional" : ["LOWLIMIT", "HIGHLIMIT", "DEFAULTVALUE", "OBJFLAGS"]},
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    55
               8 : {"name" : "n ARRAY",
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    56
                    "require" : ["SUBNUMBER", "PARAMETERNAME", "OBJECTTYPE"],
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    57
                    "optional" : ["OBJFLAGS"]},
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    58
               9 : {"name" : " RECORD",
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    59
                    "require" : ["SUBNUMBER", "PARAMETERNAME", "OBJECTTYPE"],
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    60
                    "optional" : ["OBJFLAGS"]}}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    61
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    62
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    63
# Function that search into Node Mappings the informations about an index or a subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    64
# and return the default value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    65
def GetDefaultValue(index, subIndex = None):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    66
    infos = Manager.GetEntryInfos(index, Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    67
    if infos["struct"] & node.OD_MultipleSubindexes:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    68
        # First case entry is a record
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    69
        if infos["struct"] & node.OD_IdenticalSubindexes:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    70
            subentry_infos = Manager.GetSubentryInfos(index, 1, Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    71
        # Second case entry is an array
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    72
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    73
            subentry_infos = Manager.GetSubentryInfos(index, subIndex, Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    74
        # If a default value is defined for this subindex, returns it
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    75
        if "default" in subentry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    76
            return subentry_infos["default"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    77
        # If not, returns the default value for the subindex type
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    78
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    79
            return Manager.GetTypeDefaultValue(subentry_infos["type"], Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    80
    # Third case entry is a var
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    81
    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    82
        subentry_infos = Manager.GetSubentryInfos(index, 0, Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    83
        # If a default value is defined for this subindex, returns it
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    84
        if "default" in subentry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    85
            return subentry_infos["default"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    86
        # If not, returns the default value for the subindex type
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    87
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    88
            return Manager.GetTypeDefaultValue(subentry_infos["type"], Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    89
    return None
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    90
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    91
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    92
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    93
#                               Parse file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    94
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    95
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    96
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    97
# List of section names that are not index and subindex and that we can meet in
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    98
# an EDS file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    99
SECTION_KEYNAMES = ["FILEINFO", "DEVICEINFO", "DUMMYUSAGE", "COMMENTS", 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   100
                    "MANDATORYOBJECTS", "OPTIONALOBJECTS", "MANUFACTUREROBJECTS"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   101
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   102
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   103
# Function that parse an EDS file and returns a dictionary of the informations
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   104
def ParseFile(filepath):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   105
    eds_dict = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   106
    # Read file text
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   107
    eds_file = open(filepath,'r').read()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   108
    sections = [(blocktuple[0],                # EntryName : Assignements dict
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   109
                 blocktuple[-1].splitlines())  # all the lines
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   110
                 for blocktuple in [           # Split the eds files into
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   111
                 block.split("]")              # (EntryName,Assignements) tuple
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   112
                 for block in                  # for each blocks staring with '['
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   113
                 eds_file.split("[")]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   114
                 if blocktuple[0].isalnum()]   # if EntryName exists
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   115
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   116
    # Parse assignments for each section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   117
    for section_name, assignments in sections:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   118
        # Reset values of entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   119
        values = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   120
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   121
        # Search if the section name match an index or subindex expression
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   122
        index_result = index_model.match(section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   123
        subindex_result = subindex_model.match(section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   124
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   125
        # Compilation of the EDS information dictionary
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   126
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   127
        is_entry = False
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   128
        # First case, section name is in SECTION_KEYNAMES 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   129
        if section_name.upper() in SECTION_KEYNAMES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   130
            # Verify that entry is not already defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   131
            if section_name.upper() not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   132
                eds_dict[section_name.upper()] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   133
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   134
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   135
        # Second case, section name is a subindex name 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   136
        elif subindex_result:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   137
            # Extract index and subindex number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   138
            index, subindex = [int(value, 16) for value in subindex_result.groups()]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   139
            # If index hasn't been referenced before, we add an entry into the dictionary
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   140
            # that will be updated later
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   141
            if index not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   142
                eds_dict[index] = {"subindexes" : {}}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   143
            if subindex not in eds_dict[index]["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   144
                eds_dict[index]["subindexes"][subindex] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   145
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   146
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   147
            is_entry = True
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   148
        # Third case, section name is an index name 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   149
        elif index_result:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   150
            # Extract index number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   151
            index = int(index_result.groups()[0], 16)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   152
            # If index hasn't been referenced before, we add an entry into the dictionary
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   153
            if index not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   154
                eds_dict[index] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   155
                eds_dict[index]["subindexes"] = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   156
            elif eds_dict[index].keys() == ["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   157
                values["subindexes"] = eds_dict[index]["subindexes"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   158
                eds_dict[index] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   159
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   160
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   161
            is_entry = True
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   162
        # In any other case, there is a syntax problem into EDS file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   163
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   164
            raise SyntaxError, "Section \"[%s]\" is unrecognized"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   165
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   166
        for assignment in assignments:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   167
            # Escape any comment
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   168
            if assignment.startswith(";"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   169
                pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   170
            # Verify that line is a valid assignment
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   171
            elif assignment.find('=') > 0:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   172
                # Split assignment into the two values keyname and value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   173
                try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   174
                    keyname, value = assignment.split("=")
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   175
                except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   176
                    raise SyntaxError, "\"%s\" is not a valid EDS line"%assignment.strip()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   177
                # keyname must be immediately followed by the "=" sign, so we
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   178
                # verify that there is no whitespace into keyname
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   179
                if keyname.isalnum():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   180
                    # value can be preceded and followed by whitespaces, so we escape them
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   181
                    value = value.strip()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   182
                    # First case, value starts with "$NODEID", then it's a formula
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   183
                    if value.startswith("$NODEID"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   184
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   185
                            computed_value = int(value.replace("$NODEID+", ""), 16)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   186
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   187
                            raise SyntaxError, "\"%s\" is not a valid formula for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   188
                    # Second case, value starts with "0x", then it's an hexadecimal value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   189
                    elif value.startswith("0x"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   190
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   191
                            computed_value = int(value, 16)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   192
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   193
                            raise SyntaxError, "\"%s\" is not a valid value for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   194
                    elif value.isdigit():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   195
                        # Third case, value is a number and starts with "0", then it's an octal value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   196
                        if value.startswith("0"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   197
                            computed_value = int(value, 8)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   198
                        # Forth case, value is a number and don't start with "0", then it's a decimal value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   199
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   200
                            computed_value = int(value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   201
                    # In any other case, we keep string value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   202
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   203
                        computed_value = value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   204
                    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   205
                    # Add value to values dictionary
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   206
                    if computed_value != "":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   207
                        # If entry is an index or a subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   208
                        if is_entry:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   209
                            # Verify that keyname is a possible attribute
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   210
                            if keyname.upper() not in ENTRY_ATTRIBUTES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   211
                                raise SyntaxError, "Keyname \"%s\" not recognised for section \"[%s]\""%(keyname, section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   212
                            # Verify that value is valid
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   213
                            elif not ENTRY_ATTRIBUTES[keyname.upper()](computed_value):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   214
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   215
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   216
                                values[keyname.upper()] = computed_value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   217
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   218
                            values[keyname.upper()] = computed_value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   219
            # All lines that are not empty and are neither a comment neither not a valid assignment
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   220
            elif assignment.strip() != "":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   221
                raise SyntaxError, "\"%s\" is not a valid EDS line"%assignment.strip()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   222
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   223
        # If entry is an index or a subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   224
        if is_entry:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   225
            # Verify that entry has an ObjectType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   226
            if "OBJECTTYPE" in values.keys():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   227
                # Extract entry ObjectType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   228
                objecttype = values["OBJECTTYPE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   229
                # Extract parameters defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   230
                keys = Set(values.keys())
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   231
                keys.discard("subindexes")
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   232
                # Extract possible parameters and parameters required
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   233
                possible = Set(ENTRY_TYPES[objecttype]["require"] + ENTRY_TYPES[objecttype]["optional"])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   234
                required = Set(ENTRY_TYPES[objecttype]["require"])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   235
                # Verify that parameters defined contains all the parameters required
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   236
                if not keys.issuperset(required):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   237
                    missing = required.difference(keys)._data.keys()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   238
                    if len(missing) > 1:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   239
                        attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in missing])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   240
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   241
                        attributes = "Attribute \"%s\" is"%missing[0]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   242
                    raise SyntaxError, "Error on section \"[%s]\":\n%s required for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   243
                # Verify that parameters defined are all in the possible parameters
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   244
                if not keys.issubset(possible):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   245
                    unsupported = keys.difference(possible)._data.keys()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   246
                    if len(unsupported) > 1:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   247
                        attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in unsupported])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   248
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   249
                        attributes = "Attribute \"%s\" is"%unsupported[0]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   250
                    raise SyntaxError, "Error on section \"[%s]\":\n%s unsupported for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   251
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   252
                raise SyntaxError, "Error on section \"[%s]\":\nAttribute OBJECTTYPE is required"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   253
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   254
    return eds_dict
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   255
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   256
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   257
# Function that write an EDS file after generate it's content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   258
def WriteFile(filepath, content):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   259
    # Open file in write mode
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   260
    cfile = open(filepath,"w")
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   261
    # Write content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   262
    cfile.write(content)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   263
    # Close file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   264
    cfile.close()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   265
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   266
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   267
# Function that generate the EDS file content for the current node in the manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   268
def GenerateFileContent(filepath):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   269
    # Dictionary of each index contents
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   270
    indexContents = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   271
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   272
    # Extract local time
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   273
    current_time = localtime()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   274
    # Extract node informations
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   275
    nodename, nodeid, nodetype, description = Manager.GetCurrentNodeInfos()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   276
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   277
    # Compiling lists of indexes defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   278
    entries = [idx for name, idx in Manager.GetCurrentValidIndexes(0, 0xFFFF)]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   279
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   280
    # Generate FileInfo section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   281
    fileContent = "[FileInfo]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   282
    fileContent += "CreatedBy=CANFestival\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   283
    fileContent += "Description=%s\n"%description
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   284
    fileContent += "CreationTime=%s"%strftime("%I:%M", current_time)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   285
    # %p option of strftime seems not working, then generate AM/PM by hands
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   286
    if strftime("%I", current_time) == strftime("%H", current_time):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   287
        fileContent += "AM\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   288
    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   289
        fileContent += "PM\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   290
    fileContent += "CreationDate=%s\n"%strftime("%m-%d-%Y", current_time)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   291
    fileContent += "FileName=%s\n"%os.path.split(filepath)[-1]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   292
    fileContent += "FileVersion=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   293
    fileContent += "FileRevision=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   294
    fileContent += "EDSVersion=3.0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   295
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   296
    # Generate DeviceInfo section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   297
    fileContent += "\n[DeviceInfo]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   298
    fileContent += "VendorName=CANFestival\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   299
    # Use information typed by user in Identity entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   300
    fileContent += "VendorNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 1)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   301
    fileContent += "ProductName=%s\n"%nodename
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   302
    fileContent += "ProductNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 2)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   303
    fileContent += "RevisionNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 3)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   304
    # CANFestival support all baudrates as soon as driver choosen support them
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   305
    fileContent += "BaudRate_10=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   306
    fileContent += "BaudRate_20=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   307
    fileContent += "BaudRate_50=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   308
    fileContent += "BaudRate_125=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   309
    fileContent += "BaudRate_250=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   310
    fileContent += "BaudRate_500=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   311
    fileContent += "BaudRate_800=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   312
    fileContent += "BaudRate_1000=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   313
    # Select BootUp type from the informations given by user
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   314
    fileContent += "SimpleBootUpMaster=%s\n"%BOOL_TRANSLATE[nodetype == "master"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   315
    fileContent += "SimpleBootUpSlave=%s\n"%BOOL_TRANSLATE[nodetype == "slave"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   316
    # CANFestival characteristics
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   317
    fileContent += "Granularity=8\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   318
    fileContent += "DynamicChannelsSupported=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   319
    fileContent += "CompactPDO=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   320
    fileContent += "GroupMessaging=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   321
    # Calculate receive and tranmit PDO numbers with the entry available
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   322
    fileContent += "NrOfRXPDO=%d\n"%len([idx for idx in entries if 0x1400 <= idx <= 0x15FF])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   323
    fileContent += "NrOfTXPDO=%d\n"%len([idx for idx in entries if 0x1400 <= idx <= 0x15FF])
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   324
    # LSS not supported as soon as DS-302 was not fully implemented
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   325
    fileContent += "LSS_Supported=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   326
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   327
    # Generate Dummy Usage section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   328
    fileContent += "\n[DummyUsage]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   329
    fileContent += "Dummy0001=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   330
    fileContent += "Dummy0002=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   331
    fileContent += "Dummy0003=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   332
    fileContent += "Dummy0004=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   333
    fileContent += "Dummy0005=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   334
    fileContent += "Dummy0006=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   335
    fileContent += "Dummy0007=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   336
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   337
    # Generate Comments section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   338
    fileContent += "\n[Comments]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   339
    fileContent += "Lines=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   340
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   341
    # List of entry by type (Mandatory, Optional or Manufacturer
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   342
    mandatories = []
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   343
    optionals = []
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   344
    manufacturers = []
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   345
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   346
    # For each entry, we generate the entry section or sections if there is subindexes
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   347
    for entry in entries:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   348
        # Extract infos and values for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   349
        entry_infos = Manager.GetEntryInfos(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   350
        values = Manager.GetCurrentEntry(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   351
        # Define section name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   352
        text = "\n[%X]\n"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   353
        # If there is only one value, it's a VAR entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   354
        if type(values) != ListType:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   355
            # Extract the informations of the first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   356
            subentry_infos = Manager.GetSubentryInfos(entry, 0)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   357
            # Generate EDS informations for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   358
            text += "ParameterName=%s\n"%subentry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   359
            text += "ObjectType=0x7\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   360
            text += "DataType=0x%4.4X\n"%subentry_infos["type"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   361
            text += "AccessType=%s\n"%subentry_infos["access"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   362
            text += "DefaultValue=%s\n"%values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   363
            text += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   364
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   365
            # Generate EDS informations for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   366
            text += "ParameterName=%s\n"%entry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   367
            if entry_infos["struct"] & node.OD_IdenticalSubindexes:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   368
                text += "ObjectType=0x9\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   369
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   370
                text += "ObjectType=0x8\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   371
            
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   372
            # Generate EDS informations for subindexes of the entry in a separate text
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   373
            subtext = ""
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   374
            # Reset number of subindex defined 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   375
            nb_subentry = 0
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   376
            for subentry, value in enumerate(values):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   377
                # Extract the informations of each subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   378
                subentry_infos = Manager.GetSubentryInfos(entry, subentry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   379
                # If entry is not for the compatibility, generate informations for subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   380
                if subentry_infos["name"] != "Compatibility Entry":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   381
                    subtext += "\n[%Xsub%X]\n"%(entry, subentry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   382
                    subtext += "ParameterName=%s\n"%subentry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   383
                    subtext += "ObjectType=0x7\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   384
                    subtext += "DataType=0x%4.4X\n"%subentry_infos["type"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   385
                    subtext += "AccessType=%s\n"%subentry_infos["access"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   386
                    subtext += "DefaultValue=%s\n"%value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   387
                    subtext += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   388
                    # Increment number of subindex defined 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   389
                    nb_subentry += 1
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   390
            # Write number of subindex defined for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   391
            text += "SubNumber=%d\n"%nb_subentry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   392
            # Write subindex definitions
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   393
            text += subtext
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   394
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   395
        # Then we add the entry in the right list
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   396
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   397
        # First case, entry is between 0x2000 and 0x5FFF, then it's a manufacturer entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   398
        if 0x2000 <= entry <= 0x5FFF:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   399
            manufacturers.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   400
        # Second case, entry is required, then it's a mandatory entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   401
        elif entry_infos["need"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   402
            mandatories.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   403
        # In any other case, it's an optional entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   404
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   405
            optionals.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   406
        # Save text of the entry in the dictiionary of contents
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   407
        indexContents[entry] = text
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   408
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   409
    # Before generate File Content we sort the entry list
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   410
    manufacturers.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   411
    mandatories.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   412
    optionals.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   413
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   414
    # Generate Definition of mandatory objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   415
    fileContent += "\n[MandatoryObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   416
    fileContent += "SupportedObjects=%d\n"%len(mandatories)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   417
    for idx, entry in enumerate(mandatories):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   418
        fileContent += "%d=0x%4.4X\n"%(idx, entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   419
    # Write mandatory entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   420
    for entry in mandatories:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   421
        fileContent += indexContents[entry]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   422
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   423
    # Generate Definition of optional objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   424
    fileContent += "\n[OptionalObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   425
    fileContent += "SupportedObjects=%d\n"%len(optionals)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   426
    for idx, entry in enumerate(optionals):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   427
        fileContent += "%d=0x%4.4X\n"%(idx, entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   428
    # Write optional entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   429
    for entry in optionals:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   430
        fileContent += indexContents[entry]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   431
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   432
    # Generate Definition of manufacturer objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   433
    fileContent += "\n[ManufacturerObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   434
    fileContent += "SupportedObjects=%d\n"%len(manufacturers)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   435
    for idx, entry in enumerate(manufacturers):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   436
        fileContent += "%d=0x%4.4X\n"%(idx, entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   437
    # Write manufacturer entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   438
    for entry in manufacturers:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   439
        fileContent += indexContents[entry]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   440
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   441
    # Return File Content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   442
    return fileContent
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   443
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   444
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   445
# Function that generates EDS file from current node edited
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   446
def GenerateEDSFile(filepath, manager):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   447
    global Manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   448
    Manager = manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   449
    try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   450
        # Generate file content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   451
        content = GenerateFileContent(filepath)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   452
        # Write file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   453
        WriteFile(filepath, content)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   454
        return None
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   455
    except ValueError, message:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   456
        return "Unable to generate EDS file\n%s"%message
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   457
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   458
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   459
# Function that generates Node from an EDS file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   460
def GenerateNode(filepath, manager, cwd):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   461
    global Node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   462
    global Manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   463
    Manager = manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   464
    # Create a new node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   465
    Node = node.Node()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   466
    try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   467
        # Parse file and extract dictionary of EDS entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   468
        eds_dict = ParseFile(filepath)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   469
        # Extract Profile Number from Device Type entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   470
        ProfileNb = eds_dict[0x1000]["DEFAULTVALUE"] & 0x0000ffff
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   471
        # If profile is not DS-301 or DS-302
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   472
        if ProfileNb not in [301, 302]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   473
            # Compile Profile name and path to .prf file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   474
            ProfileName = "DS-%d"%ProfileNb
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   475
            ProfilePath = os.path.join(cwd, "config/%s.prf"%ProfileName)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   476
            # Verify that profile is available
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   477
            if os.path.isfile(ProfilePath):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   478
                try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   479
                    # Load Profile
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   480
                    execfile(ProfilePath)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   481
                    Node.SetProfileName(ProfileName)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   482
                    Node.SetProfile(Mapping)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   483
                    Node.SetSpecificMenu(AddMenuEntries)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   484
                except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   485
                    pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   486
        # Read all entries in the EDS dictionary 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   487
        for entry, values in eds_dict.items():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   488
            # All sections with a name in keynames are escaped
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   489
            if entry in SECTION_KEYNAMES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   490
                pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   491
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   492
                # Extract informations for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   493
                entry_infos = Manager.GetEntryInfos(entry, Node)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   494
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   495
                # If no informations are available, then we write them
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   496
                if not entry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   497
                    # First case, entry is a VAR
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   498
                    if values["OBJECTTYPE"] == 7:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   499
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   500
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 1)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   501
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   502
                        Node.AddMappingEntry(entry, 0, values = {"name" : values["PARAMETERNAME"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   503
                                                                 "type" : values["DATATYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   504
                                                                 "access" : values["ACCESSTYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   505
                                                                 "pdo" : values["PDOMAPPING"] == 1})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   506
                    # Second case, entry is an ARRAY
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   507
                    elif values["OBJECTTYPE"] == 8:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   508
                        # Extract maximum subindex number defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   509
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   510
                            max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   511
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   512
                            raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for an ARRAY entry"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   513
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   514
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 3)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   515
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   516
                        Node.AddMappingEntry(entry, 0, values = {"name" : "Number of Entries", "type" : 0x05, "access" : "ro", "pdo" : False})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   517
                        # Add mapping for other subindexes
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   518
                        for subindex in xrange(1, int(max_subindex) + 1):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   519
                            # if subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   520
                            if subindex in values["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   521
                                Node.AddMappingEntry(entry, subindex, values = {"name" : values["subindexes"][subindex]["PARAMETERNAME"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   522
                                                                                "type" : values["subindexes"][subindex]["DATATYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   523
                                                                                "access" : values["subindexes"][subindex]["ACCESSTYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   524
                                                                                "pdo" : values["subindexes"][subindex]["PDOMAPPING"] == 1})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   525
                            # if not, we add a mapping for compatibility 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   526
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   527
                                Node.AddMappingEntry(entry, subindex, values = {"name" : "Compatibility Entry", "type" : 0x05, "access" : "rw", "pdo" : False})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   528
                    # Third case, entry is an RECORD
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   529
                    elif values["OBJECTTYPE"] == 9:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   530
                        # Verify that the first subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   531
                        if 0 not in values["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   532
                            raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for a RECORD entry"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   533
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   534
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 7)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   535
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   536
                        Node.AddMappingEntry(entry, 0, values = {"name" : "Number of Entries", "type" : 0x05, "access" : "ro", "pdo" : False})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   537
                        # Verify that second subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   538
                        if 1 in values:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   539
                            Node.AddMappingEntry(entry, 1, values = {"name" : values["PARAMETERNAME"] + " %d[(sub)]", 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   540
                                                                     "type" : values["subindexes"][1]["DATATYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   541
                                                                     "access" : values["subindexes"][1]["ACCESSTYPE"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   542
                                                                     "pdo" : values["subindexes"][1]["PDOMAPPING"] == 1})
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   543
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   544
                            raise SyntaxError, "Error on entry 0x%4.4X:\nA RECORD entry must have at least 2 subindexes"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   545
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   546
                # Define entry for the new node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   547
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   548
                # First case, entry is a VAR
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   549
                if values["OBJECTTYPE"] == 7:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   550
                    # Take default value if it is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   551
                    if "DEFAULTVALUE" in values:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   552
                        value = values["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   553
                    # Find default value for value type of the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   554
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   555
                        value = GetDefaultValue(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   556
                    Node.AddEntry(entry, 0, value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   557
                # Second case, entry is an ARRAY or a RECORD
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   558
                elif values["OBJECTTYPE"] in (8, 9):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   559
                    # Verify that "Subnumber" attribute is defined and has a valid value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   560
                    if "SUBNUMBER" in values and values["SUBNUMBER"] > 0:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   561
                        # Extract maximum subindex number defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   562
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   563
                            max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   564
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   565
                            raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for an ARRAY or a RECORD entry"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   566
                        # Define value for all subindexes except the first 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   567
                        for subindex in xrange(1, int(max_subindex) + 1):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   568
                            # Take default value if it is defined and entry is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   569
                            if subindex in values["subindexes"] and "DEFAULTVALUE" in values["subindexes"][subindex]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   570
                                value = values["subindexes"][subindex]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   571
                            # Find default value for value type of the subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   572
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   573
                                value = GetDefaultValue(entry, subindex)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   574
                            Node.AddEntry(entry, subindex, value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   575
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   576
                        raise SyntaxError, "Array or Record entry 0x%4.4X must have a \"SubNumber\" attribute"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   577
        return Node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   578
    except SyntaxError, message:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   579
        return "Unable to import EDS File\n%s"%message
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   580
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   581
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   582
#                             Main Function
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   583
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   584
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   585
if __name__ == '__main__':
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   586
    print ParseFile("examples/PEAK MicroMod.eds")
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   587