objdictgen/eds_utils.py
author etisserant
Fri, 21 Sep 2007 17:50:20 +0200
changeset 279 9b69f2fabafe
parent 258 8f7725451453
child 299 506f9700b9fb
permissions -rw-r--r--
added config.py.in, filled by configure, so that python program using objdictedit can gat compilation params. Reomved old dead files.
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
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    26
from node import nosub, var, array, rec, plurivar, pluriarray, plurirec
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    27
from sets import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    28
from types import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    29
from time import *
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    30
import os,re
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    31
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    32
# Regular expression for finding index section names
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    33
index_model = re.compile('([0-9A-F]{1,4})')
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    34
# Regular expression for finding subindex section names
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    35
subindex_model = re.compile('([0-9A-F]{1,4})SUB([0-9A-F]{1,2})')
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    36
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    37
# Regular expression for finding NodeXPresent keynames
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    38
nodepresent_model = re.compile('NODE([0-9]{1,3})PRESENT')
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    39
# Regular expression for finding NodeXName keynames
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    40
nodename_model = re.compile('NODE([0-9]{1,3})NAME')
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    41
# Regular expression for finding NodeXDCFName keynames
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    42
nodedcfname_model = re.compile('NODE([0-9]{1,3})DCFNAME')
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    43
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    44
# Dictionary for quickly translate boolean into integer value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    45
BOOL_TRANSLATE = {True : "1", False : "0"}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    46
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    47
# Dictionary for quickly translate eds access value into canfestival access value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    48
ACCESS_TRANSLATE = {"ro" : "ro", "wo" : "wo", "rw" : "rw", "rwr" : "rw", "rww" : "rw", "const" : "ro"}
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    49
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    50
# Function for verifying data values
206
6787754b251b Bugs corrected on objdictedit and networkdict
greg
parents: 205
diff changeset
    51
is_integer = lambda x: type(x) in (IntType, LongType)
6787754b251b Bugs corrected on objdictedit and networkdict
greg
parents: 205
diff changeset
    52
is_string = lambda x: type(x) in (StringType, UnicodeType)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    53
is_boolean = lambda x: x in (0, 1)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    54
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    55
# Define checking of value for each attribute
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    56
ENTRY_ATTRIBUTES = {"SUBNUMBER" : is_integer, "PARAMETERNAME" : is_string, 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    57
                    "OBJECTTYPE" : lambda x: x in (7, 8, 9), "DATATYPE" : is_integer, 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    58
                    "LOWLIMIT" : is_integer, "HIGHLIMIT" : is_integer,
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    59
                    "ACCESSTYPE" : lambda x: x in ["ro","wo", "rw", "rwr", "rww", "const"],
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    60
                    "DEFAULTVALUE" : lambda x: True, "PDOMAPPING" : is_boolean,
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    61
                    "OBJFLAGS" : is_integer}
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
# Define entry parameters by entry ObjectType number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    64
ENTRY_TYPES = {7 : {"name" : " VAR",
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
    65
                    "require" : ["PARAMETERNAME", "DATATYPE", "ACCESSTYPE"],
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
    66
                    "optional" : ["OBJECTTYPE", "DEFAULTVALUE", "PDOMAPPING", "LOWLIMIT", "HIGHLIMIT", "OBJFLAGS"]},
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    67
               8 : {"name" : "n ARRAY",
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
    68
                    "require" : ["PARAMETERNAME", "OBJECTTYPE", "SUBNUMBER"],
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    69
                    "optional" : ["OBJFLAGS"]},
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    70
               9 : {"name" : " RECORD",
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
    71
                    "require" : ["PARAMETERNAME", "OBJECTTYPE", "SUBNUMBER"],
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    72
                    "optional" : ["OBJFLAGS"]}}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    73
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    74
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    75
# 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
    76
# and return the default value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    77
def GetDefaultValue(index, subIndex = None):
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    78
    infos = Node.GetEntryInfos(index)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    79
    if infos["struct"] & node.OD_MultipleSubindexes:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    80
        # First case entry is a record
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    81
        if infos["struct"] & node.OD_IdenticalSubindexes:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    82
            subentry_infos = Node.GetSubentryInfos(index, 1)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    83
        # Second case entry is an array
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    84
        else:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    85
            subentry_infos = Node.GetSubentryInfos(index, subIndex)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    86
        # If a default value is defined for this subindex, returns it
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    87
        if "default" in subentry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    88
            return subentry_infos["default"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    89
        # If not, returns the default value for the subindex type
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    90
        else:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    91
            return Node.GetTypeDefaultValue(subentry_infos["type"])
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    92
    # Third case entry is a var
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    93
    else:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
    94
        subentry_infos = Node.GetSubentryInfos(index, 0)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    95
        # If a default value is defined for this subindex, returns it
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    96
        if "default" in subentry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    97
            return subentry_infos["default"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    98
        # If not, returns the default value for the subindex type
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
    99
        else:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   100
            return Node.GetTypeDefaultValue(subentry_infos["type"])
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   101
    return None
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
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   104
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   105
#                               Parse file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   106
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   107
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   108
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   109
# 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
   110
# an EDS file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   111
SECTION_KEYNAMES = ["FILEINFO", "DEVICEINFO", "DUMMYUSAGE", "COMMENTS", 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   112
                    "MANDATORYOBJECTS", "OPTIONALOBJECTS", "MANUFACTUREROBJECTS"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   113
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   114
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   115
# Function that extract sections from a file and returns a dictionary of the informations
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   116
def ExtractSections(file):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   117
    return [(blocktuple[0],                # EntryName : Assignements dict
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   118
             blocktuple[-1].splitlines())  # all the lines
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   119
             for blocktuple in [           # Split the eds files into
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   120
             block.split("]", 1)              # (EntryName,Assignements) tuple
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   121
             for block in                  # for each blocks staring with '['
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   122
             ("\n"+file).split("\n[")]
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   123
             if blocktuple[0].isalnum()]   # if EntryName exists
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   124
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   125
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   126
# Function that parse an CPJ file and returns a dictionary of the informations
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   127
def ParseCPJFile(filepath):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   128
    networks = []
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   129
    # Read file text
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   130
    cpj_file = open(filepath,'r').read()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   131
    sections = ExtractSections(cpj_file)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   132
    # Parse assignments for each section
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   133
    for section_name, assignments in sections:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   134
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   135
        # Verify that section name is TOPOLOGY 
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   136
        if section_name.upper() in "TOPOLOGY":
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   137
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   138
            # Reset values for topology
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   139
            topology = {"Name" : "", "Nodes" : {}}
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   140
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   141
            for assignment in assignments:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   142
                # Escape any comment
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   143
                if assignment.startswith(";"):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   144
                    pass
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   145
                # Verify that line is a valid assignment
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   146
                elif assignment.find('=') > 0:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   147
                    # Split assignment into the two values keyname and value
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   148
                    keyname, value = assignment.split("=", 1)
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   149
                    
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   150
                    # keyname must be immediately followed by the "=" sign, so we
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   151
                    # verify that there is no whitespace into keyname
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   152
                    if keyname.isalnum():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   153
                        # value can be preceded and followed by whitespaces, so we escape them
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   154
                        value = value.strip()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   155
                
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   156
                        # First case, value starts with "0x", then it's an hexadecimal value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   157
                        if value.startswith("0x"):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   158
                            try:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   159
                                computed_value = int(value, 16)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   160
                            except:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   161
                                raise SyntaxError, "\"%s\" is not a valid value for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   162
                        elif value.isdigit():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   163
                            # Second case, value is a number and starts with "0", then it's an octal value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   164
                            if value.startswith("0"):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   165
                                computed_value = int(value, 8)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   166
                            # Third case, value is a number and don't start with "0", then it's a decimal value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   167
                            else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   168
                                computed_value = int(value)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   169
                        # In any other case, we keep string value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   170
                        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   171
                            computed_value = value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   172
                        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   173
                        # Search if the section name match any cpj expression
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   174
                        nodepresent_result = nodepresent_model.match(keyname.upper())
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   175
                        nodename_result = nodename_model.match(keyname.upper())
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   176
                        nodedcfname_result = nodedcfname_model.match(keyname.upper())
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   177
                        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   178
                        if keyname.upper() == "NETNAME":
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   179
                            if not is_string(computed_value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   180
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   181
                            topology["Name"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   182
                        elif keyname.upper() == "NODES":
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   183
                            if not is_integer(computed_value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   184
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   185
                            topology["Number"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   186
                        elif keyname.upper() == "EDSBASENAME":
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   187
                            if not is_string(computed_value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   188
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   189
                            topology["Path"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   190
                        elif nodepresent_result:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   191
                            if not is_boolean(computed_value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   192
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   193
                            nodeid = int(nodepresent_result.groups()[0])
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   194
                            if nodeid not in topology["Nodes"].keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   195
                                topology["Nodes"][nodeid] = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   196
                            topology["Nodes"][nodeid]["Present"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   197
                        elif nodename_result:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   198
                            if not is_string(value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   199
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   200
                            nodeid = int(nodename_result.groups()[0])
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   201
                            if nodeid not in topology["Nodes"].keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   202
                                topology["Nodes"][nodeid] = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   203
                            topology["Nodes"][nodeid]["Name"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   204
                        elif nodedcfname_result:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   205
                            if not is_string(computed_value):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   206
                                raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   207
                            nodeid = int(nodedcfname_result.groups()[0])
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   208
                            if nodeid not in topology["Nodes"].keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   209
                                topology["Nodes"][nodeid] = {}
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   210
                            topology["Nodes"][nodeid]["DCFName"] = computed_value
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   211
                        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   212
                            raise SyntaxError, "Keyname \"%s\" not recognised for section \"[%s]\""%(keyname, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   213
                        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   214
                # All lines that are not empty and are neither a comment neither not a valid assignment
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   215
                elif assignment.strip() != "":
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   216
                    raise SyntaxError, "\"%s\" is not a valid CPJ line"%assignment.strip()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   217
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   218
            if "Number" not in topology.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   219
                raise SyntaxError, "\"Nodes\" keyname in \"[%s]\" section is missing"%section_name
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   220
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   221
            if topology["Number"] != len(topology["Nodes"]):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   222
                raise SyntaxError, "\"Nodes\" value not corresponding to number of nodes defined"
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   223
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   224
            for nodeid, node in topology["Nodes"].items():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   225
                if "Present" not in node.keys():
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   226
                    raise SyntaxError, "\"Node%dPresent\" keyname in \"[%s]\" section is missing"%(nodeid, section_name)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   227
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   228
            networks.append(topology)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   229
            
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   230
        # In other case, there is a syntax problem into CPJ file
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   231
        else:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   232
            raise SyntaxError, "Section \"[%s]\" is unrecognized"%section_name
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   233
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   234
    return networks
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   235
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   236
# Function that parse an EDS file and returns a dictionary of the informations
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   237
def ParseEDSFile(filepath):
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   238
    eds_dict = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   239
    # Read file text
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   240
    eds_file = open(filepath,'r').read()
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   241
    sections = ExtractSections(eds_file)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   242
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   243
    # Parse assignments for each section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   244
    for section_name, assignments in sections:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   245
        # Reset values of entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   246
        values = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   247
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   248
        # Search if the section name match an index or subindex expression
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   249
        index_result = index_model.match(section_name.upper())
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   250
        subindex_result = subindex_model.match(section_name.upper())
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   251
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   252
        # Compilation of the EDS information dictionary
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
        is_entry = False
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   255
        # First case, section name is in SECTION_KEYNAMES 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   256
        if section_name.upper() in SECTION_KEYNAMES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   257
            # Verify that entry is not already defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   258
            if section_name.upper() not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   259
                eds_dict[section_name.upper()] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   260
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   261
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   262
        # Second case, section name is a subindex name 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   263
        elif subindex_result:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   264
            # Extract index and subindex number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   265
            index, subindex = [int(value, 16) for value in subindex_result.groups()]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   266
            # 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
   267
            # that will be updated later
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   268
            if index not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   269
                eds_dict[index] = {"subindexes" : {}}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   270
            if subindex not in eds_dict[index]["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   271
                eds_dict[index]["subindexes"][subindex] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   272
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   273
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   274
            is_entry = True
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   275
        # Third case, section name is an index name 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   276
        elif index_result:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   277
            # Extract index number
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   278
            index = int(index_result.groups()[0], 16)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   279
            # 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
   280
            if index not in eds_dict:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   281
                eds_dict[index] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   282
                eds_dict[index]["subindexes"] = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   283
            elif eds_dict[index].keys() == ["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   284
                values["subindexes"] = eds_dict[index]["subindexes"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   285
                eds_dict[index] = values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   286
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   287
                raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   288
            is_entry = True
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   289
        # 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
   290
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   291
            raise SyntaxError, "Section \"[%s]\" is unrecognized"%section_name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   292
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   293
        for assignment in assignments:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   294
            # Escape any comment
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   295
            if assignment.startswith(";"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   296
                pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   297
            # Verify that line is a valid assignment
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   298
            elif assignment.find('=') > 0:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   299
                # Split assignment into the two values keyname and value
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   300
                keyname, value = assignment.split("=", 1)
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   301
                
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   302
                # keyname must be immediately followed by the "=" sign, so we
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   303
                # verify that there is no whitespace into keyname
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   304
                if keyname.isalnum():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   305
                    # 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
   306
                    value = value.strip()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   307
                    # First case, value starts with "$NODEID", then it's a formula
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   308
                    if value.startswith("$NODEID"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   309
                        try:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   310
                            test = int(value.replace("$NODEID+", ""), 16)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   311
                            computed_value = value.replace("$NODEID", "self.ID")
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   312
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   313
                            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
   314
                    # 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
   315
                    elif value.startswith("0x"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   316
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   317
                            computed_value = int(value, 16)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   318
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   319
                            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
   320
                    elif value.isdigit():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   321
                        # 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
   322
                        if value.startswith("0"):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   323
                            computed_value = int(value, 8)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   324
                        # 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
   325
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   326
                            computed_value = int(value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   327
                    # In any other case, we keep string value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   328
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   329
                        computed_value = value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   330
                    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   331
                    # Add value to values dictionary
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   332
                    if computed_value != "":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   333
                        # If entry is an index or a subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   334
                        if is_entry:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   335
                            # Verify that keyname is a possible attribute
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   336
                            if keyname.upper() not in ENTRY_ATTRIBUTES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   337
                                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
   338
                            # Verify that value is valid
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   339
                            elif not ENTRY_ATTRIBUTES[keyname.upper()](computed_value):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   340
                                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
   341
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   342
                                values[keyname.upper()] = computed_value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   343
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   344
                            values[keyname.upper()] = computed_value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   345
            # 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
   346
            elif assignment.strip() != "":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   347
                raise SyntaxError, "\"%s\" is not a valid EDS line"%assignment.strip()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   348
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   349
        # If entry is an index or a subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   350
        if is_entry:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   351
            # Verify that entry has an ObjectType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   352
            if "OBJECTTYPE" in values.keys():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   353
                # Extract entry ObjectType
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   354
                objecttype = values["OBJECTTYPE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   355
            else:
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   356
                # Set ObjectType to VAR by default
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   357
                objecttype = 7
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   358
            # Extract parameters defined
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   359
            keys = Set(values.keys())
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   360
            keys.discard("subindexes")
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   361
            # Extract possible parameters and parameters required
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   362
            possible = Set(ENTRY_TYPES[objecttype]["require"] + ENTRY_TYPES[objecttype]["optional"])
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   363
            required = Set(ENTRY_TYPES[objecttype]["require"])
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   364
            # Verify that parameters defined contains all the parameters required
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   365
            if not keys.issuperset(required):
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   366
                missing = required.difference(keys)._data.keys()
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   367
                if len(missing) > 1:
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   368
                    attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in missing])
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   369
                else:
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   370
                    attributes = "Attribute \"%s\" is"%missing[0]
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   371
                raise SyntaxError, "Error on section \"[%s]\":\n%s required for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   372
            # Verify that parameters defined are all in the possible parameters
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   373
            if not keys.issubset(possible):
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   374
                unsupported = keys.difference(possible)._data.keys()
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   375
                if len(unsupported) > 1:
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   376
                    attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in unsupported])
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   377
                else:
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   378
                    attributes = "Attribute \"%s\" is"%unsupported[0]
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   379
                raise SyntaxError, "Error on section \"[%s]\":\n%s unsupported for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
227
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   380
            
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   381
            if "DEFAULTVALUE" in values:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   382
                try:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   383
                    if values["DATATYPE"] in (0x09, 0x0A, 0x0B, 0x0F):
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   384
                        values["DEFAULTVALUE"] = str(values["DEFAULTVALUE"])
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   385
                    elif values["DATATYPE"] in (0x08, 0x11):
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   386
                        values["DEFAULTVALUE"] = float(values["DEFAULTVALUE"])
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   387
                    elif values["DATATYPE"] == 0x01:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   388
                        values["DEFAULTVALUE"] = {0 : True, 1 : False}[values["DEFAULTVALUE"]]
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   389
                    else:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   390
                        if type(values["DEFAULTVALUE"]) != IntType and values["DEFAULTVALUE"].find("self.ID") == -1:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   391
                            raise
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   392
                except:
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   393
                    raise SyntaxError, "Error on section \"[%s]\":\nDefaultValue incompatible with DataType"%section_name
f76c64f66097 Bugs on cfile generation fixed
lbessard
parents: 226
diff changeset
   394
            
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   395
    return eds_dict
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
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   398
# Function that write an EDS file after generate it's content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   399
def WriteFile(filepath, content):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   400
    # Open file in write mode
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   401
    cfile = open(filepath,"w")
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   402
    # Write content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   403
    cfile.write(content)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   404
    # Close file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   405
    cfile.close()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   406
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   407
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   408
# 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
   409
def GenerateFileContent(filepath):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   410
    # Dictionary of each index contents
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   411
    indexContents = {}
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   412
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   413
    # Extract local time
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   414
    current_time = localtime()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   415
    # Extract node informations
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   416
    nodename, nodeid, nodetype, description = Manager.GetCurrentNodeInfos()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   417
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   418
    # Compiling lists of indexes defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   419
    entries = [idx for name, idx in Manager.GetCurrentValidIndexes(0, 0xFFFF)]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   420
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   421
    # Generate FileInfo section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   422
    fileContent = "[FileInfo]\n"
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   423
    fileContent += "FileName=%s\n"%os.path.split(filepath)[-1]
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   424
    fileContent += "FileVersion=1\n"
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   425
    fileContent += "FileRevision=1\n"
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   426
    fileContent += "EDSVersion=4.0\n"
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   427
    fileContent += "Description=%s\n"%description
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   428
    fileContent += "CreationTime=%s"%strftime("%I:%M", current_time)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   429
    # %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
   430
    if strftime("%I", current_time) == strftime("%H", current_time):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   431
        fileContent += "AM\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   432
    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   433
        fileContent += "PM\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   434
    fileContent += "CreationDate=%s\n"%strftime("%m-%d-%Y", current_time)
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   435
    fileContent += "CreatedBy=CANFestival\n"
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   436
    fileContent += "ModificationTime=%s"%strftime("%I:%M", current_time)
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   437
    # %p option of strftime seems not working, then generate AM/PM by hands
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   438
    if strftime("%I", current_time) == strftime("%H", current_time):
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   439
        fileContent += "AM\n"
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   440
    else:
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   441
        fileContent += "PM\n"
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   442
    fileContent += "ModificationDate=%s\n"%strftime("%m-%d-%Y", current_time)
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   443
    fileContent += "ModifiedBy=CANFestival\n"
182
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
    # Generate DeviceInfo section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   446
    fileContent += "\n[DeviceInfo]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   447
    fileContent += "VendorName=CANFestival\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   448
    # Use information typed by user in Identity entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   449
    fileContent += "VendorNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 1)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   450
    fileContent += "ProductName=%s\n"%nodename
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   451
    fileContent += "ProductNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 2)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   452
    fileContent += "RevisionNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 3)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   453
    # CANFestival support all baudrates as soon as driver choosen support them
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   454
    fileContent += "BaudRate_10=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   455
    fileContent += "BaudRate_20=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   456
    fileContent += "BaudRate_50=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   457
    fileContent += "BaudRate_125=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   458
    fileContent += "BaudRate_250=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   459
    fileContent += "BaudRate_500=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   460
    fileContent += "BaudRate_800=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   461
    fileContent += "BaudRate_1000=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   462
    # Select BootUp type from the informations given by user
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   463
    fileContent += "SimpleBootUpMaster=%s\n"%BOOL_TRANSLATE[nodetype == "master"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   464
    fileContent += "SimpleBootUpSlave=%s\n"%BOOL_TRANSLATE[nodetype == "slave"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   465
    # CANFestival characteristics
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   466
    fileContent += "Granularity=8\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   467
    fileContent += "DynamicChannelsSupported=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   468
    fileContent += "CompactPDO=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   469
    fileContent += "GroupMessaging=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   470
    # Calculate receive and tranmit PDO numbers with the entry available
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   471
    fileContent += "NrOfRXPDO=%d\n"%len([idx for idx in entries if 0x1400 <= idx <= 0x15FF])
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   472
    fileContent += "NrOfTXPDO=%d\n"%len([idx for idx in entries if 0x1800 <= idx <= 0x19FF])
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   473
    # 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
   474
    fileContent += "LSS_Supported=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   475
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   476
    # Generate Dummy Usage section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   477
    fileContent += "\n[DummyUsage]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   478
    fileContent += "Dummy0001=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   479
    fileContent += "Dummy0002=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   480
    fileContent += "Dummy0003=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   481
    fileContent += "Dummy0004=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   482
    fileContent += "Dummy0005=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   483
    fileContent += "Dummy0006=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   484
    fileContent += "Dummy0007=1\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   485
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   486
    # Generate Comments section
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   487
    fileContent += "\n[Comments]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   488
    fileContent += "Lines=0\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   489
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   490
    # List of entry by type (Mandatory, Optional or Manufacturer
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   491
    mandatories = []
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   492
    optionals = []
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   493
    manufacturers = []
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
    # 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
   496
    for entry in entries:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   497
        # Extract infos and values for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   498
        entry_infos = Manager.GetEntryInfos(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   499
        values = Manager.GetCurrentEntry(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   500
        # Define section name
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   501
        text = "\n[%X]\n"%entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   502
        # If there is only one value, it's a VAR entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   503
        if type(values) != ListType:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   504
            # Extract the informations of the first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   505
            subentry_infos = Manager.GetSubentryInfos(entry, 0)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   506
            # Generate EDS informations for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   507
            text += "ParameterName=%s\n"%subentry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   508
            text += "ObjectType=0x7\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   509
            text += "DataType=0x%4.4X\n"%subentry_infos["type"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   510
            text += "AccessType=%s\n"%subentry_infos["access"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   511
            text += "DefaultValue=%s\n"%values
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   512
            text += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   513
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   514
            # Generate EDS informations for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   515
            text += "ParameterName=%s\n"%entry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   516
            if entry_infos["struct"] & node.OD_IdenticalSubindexes:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   517
                text += "ObjectType=0x9\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   518
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   519
                text += "ObjectType=0x8\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   520
            
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   521
            # 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
   522
            subtext = ""
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   523
            # Reset number of subindex defined 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   524
            nb_subentry = 0
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   525
            for subentry, value in enumerate(values):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   526
                # Extract the informations of each subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   527
                subentry_infos = Manager.GetSubentryInfos(entry, subentry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   528
                # If entry is not for the compatibility, generate informations for subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   529
                if subentry_infos["name"] != "Compatibility Entry":
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   530
                    subtext += "\n[%Xsub%X]\n"%(entry, subentry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   531
                    subtext += "ParameterName=%s\n"%subentry_infos["name"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   532
                    subtext += "ObjectType=0x7\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   533
                    subtext += "DataType=0x%4.4X\n"%subentry_infos["type"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   534
                    subtext += "AccessType=%s\n"%subentry_infos["access"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   535
                    subtext += "DefaultValue=%s\n"%value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   536
                    subtext += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   537
                    # Increment number of subindex defined 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   538
                    nb_subentry += 1
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   539
            # Write number of subindex defined for the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   540
            text += "SubNumber=%d\n"%nb_subentry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   541
            # Write subindex definitions
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   542
            text += subtext
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   543
        
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   544
        # Then we add the entry in the right list
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
        # 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
   547
        if 0x2000 <= entry <= 0x5FFF:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   548
            manufacturers.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   549
        # Second case, entry is required, then it's a mandatory entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   550
        elif entry_infos["need"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   551
            mandatories.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   552
        # In any other case, it's an optional entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   553
        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   554
            optionals.append(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   555
        # Save text of the entry in the dictiionary of contents
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   556
        indexContents[entry] = text
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   557
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   558
    # Before generate File Content we sort the entry list
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   559
    manufacturers.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   560
    mandatories.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   561
    optionals.sort()
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   562
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   563
    # Generate Definition of mandatory objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   564
    fileContent += "\n[MandatoryObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   565
    fileContent += "SupportedObjects=%d\n"%len(mandatories)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   566
    for idx, entry in enumerate(mandatories):
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   567
        fileContent += "%d=0x%4.4X\n"%(idx + 1, entry)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   568
    # Write mandatory entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   569
    for entry in mandatories:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   570
        fileContent += indexContents[entry]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   571
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   572
    # Generate Definition of optional objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   573
    fileContent += "\n[OptionalObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   574
    fileContent += "SupportedObjects=%d\n"%len(optionals)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   575
    for idx, entry in enumerate(optionals):
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   576
        fileContent += "%d=0x%4.4X\n"%(idx + 1, entry)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   577
    # Write optional entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   578
    for entry in optionals:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   579
        fileContent += indexContents[entry]
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
    # Generate Definition of manufacturer objects
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   582
    fileContent += "\n[ManufacturerObjects]\n"
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   583
    fileContent += "SupportedObjects=%d\n"%len(manufacturers)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   584
    for idx, entry in enumerate(manufacturers):
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   585
        fileContent += "%d=0x%4.4X\n"%(idx + 1, entry)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   586
    # Write manufacturer entries
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   587
    for entry in manufacturers:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   588
        fileContent += indexContents[entry]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   589
    
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   590
    # Return File Content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   591
    return fileContent
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   592
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   593
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   594
# Function that generates EDS file from current node edited
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   595
def GenerateEDSFile(filepath, manager):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   596
    global Manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   597
    Manager = manager
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   598
    try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   599
        # Generate file content
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   600
        content = GenerateFileContent(filepath)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   601
        # Write file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   602
        WriteFile(filepath, content)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   603
        return None
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   604
    except ValueError, message:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   605
        return "Unable to generate EDS file\n%s"%message
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   606
    
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   607
# Function that generate the CPJ file content for the nodelist
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   608
def GenerateCPJContent(nodelist):
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   609
    nodes = nodelist.SlaveNodes.keys()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   610
    nodes.sort()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   611
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   612
    fileContent = "[TOPOLOGY]\n"
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   613
    fileContent += "NetName=%s\n"%nodelist.GetNetworkName()
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   614
    fileContent += "Nodes=0x%2.2X\n"%len(nodes)
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   615
    
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   616
    for nodeid in nodes:
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   617
        fileContent += "Node%dPresent=0x01\n"%nodeid
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   618
        fileContent += "Node%dName=%s\n"%(nodeid, nodelist.SlaveNodes[nodeid]["Name"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   619
        fileContent += "Node%dDCFName=%s\n"%(nodeid, nodelist.SlaveNodes[nodeid]["EDS"])
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   620
        
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   621
    fileContent += "EDSBaseName=eds\n"
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   622
    return fileContent
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   623
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   624
# Function that generates Node from an EDS file
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   625
def GenerateNode(filepath, nodeID = 0):
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   626
    global Node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   627
    # Create a new node
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   628
    Node = node.Node(id = nodeID)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   629
    try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   630
        # Parse file and extract dictionary of EDS entry
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   631
        eds_dict = ParseEDSFile(filepath)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   632
        # Extract Profile Number from Device Type entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   633
        ProfileNb = eds_dict[0x1000]["DEFAULTVALUE"] & 0x0000ffff
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   634
        # If profile is not DS-301 or DS-302
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   635
        if ProfileNb not in [301, 302]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   636
            # Compile Profile name and path to .prf file
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   637
            ProfileName = "DS-%d"%ProfileNb
258
8f7725451453 Some bugs fixed:
lbessard
parents: 227
diff changeset
   638
            ProfilePath = os.path.join(os.path.split(__file__)[0], "config/%s.prf"%ProfileName)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   639
            # Verify that profile is available
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   640
            if os.path.isfile(ProfilePath):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   641
                try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   642
                    # Load Profile
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   643
                    execfile(ProfilePath)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   644
                    Node.SetProfileName(ProfileName)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   645
                    Node.SetProfile(Mapping)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   646
                    Node.SetSpecificMenu(AddMenuEntries)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   647
                except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   648
                    pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   649
        # Read all entries in the EDS dictionary 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   650
        for entry, values in eds_dict.items():
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   651
            # All sections with a name in keynames are escaped
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   652
            if entry in SECTION_KEYNAMES:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   653
                pass
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   654
            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   655
                # Extract informations for the entry
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   656
                entry_infos = Node.GetEntryInfos(entry)
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   657
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   658
                # If no informations are available, then we write them
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   659
                if not entry_infos:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   660
                    # First case, entry is a VAR
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   661
                    if values["OBJECTTYPE"] == 7:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   662
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   663
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 1)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   664
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   665
                        Node.AddMappingEntry(entry, 0, values = {"name" : values["PARAMETERNAME"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   666
                                                                 "type" : values["DATATYPE"], 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   667
                                                                 "access" : ACCESS_TRANSLATE[values["ACCESSTYPE"]], 
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   668
                                                                 "pdo" : values.get("PDOMAPPING", 0) == 1})
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   669
                    # Second case, entry is an ARRAY
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   670
                    elif values["OBJECTTYPE"] == 8:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   671
                        # Extract maximum subindex number defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   672
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   673
                            max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   674
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   675
                            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
   676
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   677
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 3)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   678
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   679
                        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
   680
                        # Add mapping for other subindexes
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   681
                        for subindex in xrange(1, int(max_subindex) + 1):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   682
                            # if subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   683
                            if subindex in values["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   684
                                Node.AddMappingEntry(entry, subindex, values = {"name" : values["subindexes"][subindex]["PARAMETERNAME"], 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   685
                                                                                "type" : values["subindexes"][subindex]["DATATYPE"], 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   686
                                                                                "access" : ACCESS_TRANSLATE[values["subindexes"][subindex]["ACCESSTYPE"]], 
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   687
                                                                                "pdo" : values["subindexes"][subindex].get("PDOMAPPING", 0) == 1})
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   688
                            # if not, we add a mapping for compatibility 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   689
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   690
                                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
   691
                    # Third case, entry is an RECORD
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   692
                    elif values["OBJECTTYPE"] == 9:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   693
                        # Verify that the first subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   694
                        if 0 not in values["subindexes"]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   695
                            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
   696
                        # Add mapping for entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   697
                        Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 7)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   698
                        # Add mapping for first subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   699
                        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
   700
                        # Verify that second subindex is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   701
                        if 1 in values:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   702
                            Node.AddMappingEntry(entry, 1, values = {"name" : values["PARAMETERNAME"] + " %d[(sub)]", 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   703
                                                                     "type" : values["subindexes"][1]["DATATYPE"], 
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   704
                                                                     "access" : ACCESS_TRANSLATE[values["subindexes"][1]["ACCESSTYPE"]], 
226
abf63d732a84 Bug on EDS file parsing fixed
lbessard
parents: 206
diff changeset
   705
                                                                     "pdo" : values["subindexes"][1].get("PDOMAPPING", 0) == 1})
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   706
                        else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   707
                            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
   708
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   709
                # Define entry for the new node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   710
                
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   711
                # First case, entry is a VAR
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   712
                if values["OBJECTTYPE"] == 7:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   713
                    # Take default value if it is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   714
                    if "DEFAULTVALUE" in values:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   715
                        value = values["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   716
                    # Find default value for value type of the entry
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   717
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   718
                        value = GetDefaultValue(entry)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   719
                    Node.AddEntry(entry, 0, value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   720
                # Second case, entry is an ARRAY or a RECORD
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   721
                elif values["OBJECTTYPE"] in (8, 9):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   722
                    # Verify that "Subnumber" attribute is defined and has a valid value
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   723
                    if "SUBNUMBER" in values and values["SUBNUMBER"] > 0:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   724
                        # Extract maximum subindex number defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   725
                        try:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   726
                            max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   727
                        except:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   728
                            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
   729
                        # Define value for all subindexes except the first 
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   730
                        for subindex in xrange(1, int(max_subindex) + 1):
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   731
                            # Take default value if it is defined and entry is defined
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   732
                            if subindex in values["subindexes"] and "DEFAULTVALUE" in values["subindexes"][subindex]:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   733
                                value = values["subindexes"][subindex]["DEFAULTVALUE"]
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   734
                            # Find default value for value type of the subindex
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   735
                            else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   736
                                value = GetDefaultValue(entry, subindex)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   737
                            Node.AddEntry(entry, subindex, value)
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   738
                    else:
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   739
                        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
   740
        return Node
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   741
    except SyntaxError, message:
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   742
        return "Unable to import EDS file\n%s"%message
182
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   743
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   744
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   745
#                             Main Function
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   746
#-------------------------------------------------------------------------------
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   747
988f2b302aa6 Adding support for importing and exporting EDS files
lbessard
parents:
diff changeset
   748
if __name__ == '__main__':
205
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   749
    print ParseEDSFile("examples/PEAK MicroMod.eds")
dac0f9b4e3f8 Adding program for network management
lbessard
parents: 182
diff changeset
   750