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