| 1 |
#!/usr/local/bin/python |
| 2 |
|
| 3 |
# Ralph Gauges <[email protected]> |
| 4 |
|
| 5 |
## YOU NEED AT LEAST python version 1.5 TO RUN THIS SCRIPT !! |
| 6 |
|
| 7 |
## Todo List: - complete the predefined classes (what about ?Bat etc. ?) (hopefully already done) |
| 8 |
## - store comments from model files to write them back out with the model |
| 9 |
## - add methods to use the model class to represent a java swing jtree |
| 10 |
## - add COORD modifier and @ ???? |
| 11 |
|
| 12 |
import string ## imports a module for string operations |
| 13 |
import sys ## imports a module to get command line arguments |
| 14 |
|
| 15 |
|
| 16 |
class Model: ## This class represents the model as a tree |
| 17 |
## Systemmodels is a long string that hold the textual representation of the classes and datatypes defined in sysclasses.c |
| 18 |
## This might be necessary for the validation. |
| 19 |
## At least ?Colour and ?View are used in some models |
| 20 |
|
| 21 |
Systemmodels=["?Model","?Text","?Comment","?Keyword","?Class","?Tag","SourceCode","?Include","?Session","?Display","?Table","#Table_definition","?Colour","?UserSession","?Jade","?View"] |
| 22 |
|
| 23 |
|
| 24 |
CharacterArrays=["?LongText","?DNA","?Peptide","?BasePosition","?BaseQuality","?URL","?BaseCall","?KeySet","?MatchTable"] ## some character arrays of ACEDB. |
| 25 |
## they are needed for the validation process |
| 26 |
|
| 27 |
BasicDataTypes=["Int","Text","Float","DateType"] |
| 28 |
## those are needed to find out if an entry is a label or a datatype |
| 29 |
|
| 30 |
def __init__(self,FileContent): ## Constructor |
| 31 |
self.Classes=[] ## A List that holds all the classes |
| 32 |
self.DefinedDataTypes=[] ## A list that holds the user defined datatypes |
| 33 |
Blocks=self.MakeBlocks(FileContent) ## Divides the content of the file into blocks which correspond to the individual classes |
| 34 |
for Block in Blocks: |
| 35 |
if (Block[0][0]== "?"): ## if it starts with a '?', it is a class |
| 36 |
self.Classes.append(ClassType(self,Block,self.BasicDataTypes)) ## add a new class instance to Classes |
| 37 |
else: |
| 38 |
if (Block[0][0]=="#"): ## if it starts with a '#' it is a datatype |
| 39 |
self.DefinedDataTypes.append(UserDefinedType(self,Block,self.BasicDataTypes)) ## add a new datatype to the user defined datatypes |
| 40 |
else: ## if it doesn't start with '?' or '#' than it is an error |
| 41 |
print "This Datatype is unknown: "+string.split(Block[0])[0] |
| 42 |
print "Please check your model." |
| 43 |
sys.exit(0) ## exit the program |
| 44 |
|
| 45 |
def MakeBlocks(self,ModelText): |
| 46 |
ClassBlocks=[] |
| 47 |
ClassBlock=[] |
| 48 |
Text=string.join(ModelText,"") |
| 49 |
while (string.find(Text,"/*") != -1): ## removes multi line comments |
| 50 |
x=string.find(Text,"/*") |
| 51 |
y=string.find(Text,"*/") |
| 52 |
if (x > y): ## since it doesn't work for all cases print error message and exit |
| 53 |
print "There is some problem with the comments." |
| 54 |
print "Please remove all nestet comments." |
| 55 |
sys.exit(0) |
| 56 |
else: |
| 57 |
Text=Text[:x]+Text[y+2:] |
| 58 |
ModelText=string.split(Text,"\n") |
| 59 |
NumOfClasses=0 |
| 60 |
for Line in ModelText: |
| 61 |
x=string.find(Line,"//") |
| 62 |
if (x != -1): |
| 63 |
Line=Line[:x] ## removes one line comment |
| 64 |
Comment=Line[x:] |
| 65 |
if (len(string.split(Line)) > 0): |
| 66 |
Line=string.expandtabs(Line) |
| 67 |
if (Line[0] not in [" ","\t"]): ## if the line starts with anything else than ' ' or '\t' a new class starts |
| 68 |
NumOfClasses=NumOfClasses+1 |
| 69 |
if (NumOfClasses !=1): ## if it is not the first class |
| 70 |
ClassBlocks.append(ClassBlock) ## add the class |
| 71 |
ClassBlock=[Line] ## this Line belongs to the next class |
| 72 |
else: |
| 73 |
ClassBlock.append(Line) ## it is the first time a `?' or `#' is encountered, so there is nothing to add yet |
| 74 |
ClassBlocks.append(ClassBlock) ## We have to add the last class block before we return to the main program |
| 75 |
return ClassBlocks ## this return a list of Textblocks that correspond to classes |
| 76 |
|
| 77 |
def __str__(self): ## this represents the model if you say 'print 'instance of Model' |
| 78 |
String="" |
| 79 |
for Class in self.Classes: |
| 80 |
String=String+str(Class) |
| 81 |
for UDataType in self.DefinedDataTypes: |
| 82 |
String=String+str(UDataType) |
| 83 |
return String |
| 84 |
|
| 85 |
def __repr__(self): ## similar to __str__ |
| 86 |
return self.__str__() |
| 87 |
|
| 88 |
|
| 89 |
def toString(self): ## actualy not used, but might come handy when interfacing to java |
| 90 |
return self |
| 91 |
|
| 92 |
def Validate(self): ## this routine checks the model |
| 93 |
ERROR="" |
| 94 |
ValidIdentifiers=[] |
| 95 |
CLASSDICT=self.MakeClassDict() ## this makes a dictionary of all the defined classnames and pointers to the corresponding class in the model |
| 96 |
for CLASS in self.Classes: ## let every class validate itself |
| 97 |
ERROR=ERROR+CLASS.Validate(self.BasicDataTypes,CLASSDICT,self.CharacterArrays+self.Systemmodels) |
| 98 |
for DATATYPE in self.DefinedDataTypes: ## let every datatype validate itself |
| 99 |
ERROR=ERROR+DATATYPE.Validate(self.BasicDataTypes,CLASSDICT,self.CharacterArrays+self.Systemmodels) |
| 100 |
return ERROR |
| 101 |
|
| 102 |
def MakeClassDict(self): ## this makes a dictionary of all classnames and pointers to the corresponding classes |
| 103 |
## with this it is possible to get a classinstance with the name of the class |
| 104 |
## {classname:pointer_to_class} |
| 105 |
CLASSDICT={} |
| 106 |
for CLASS in self.Classes+self.DefinedDataTypes: |
| 107 |
CLASSDICT[CLASS.GetName()]=CLASS |
| 108 |
return CLASSDICT |
| 109 |
|
| 110 |
def GetPath(self): |
| 111 |
return "" |
| 112 |
|
| 113 |
class ClassType: ## this class represents a model class |
| 114 |
def __init__(self,Parent,ClassBlock,BasicDataTypes): ## Constructor |
| 115 |
self.Parts=[] ## Parts is a list that stores the branches (labels) and leafes (datatypes) |
| 116 |
self.SetParent(Parent) ## stores the parent node |
| 117 |
self.SetName(string.split(ClassBlock[0])[0]) ## sets the name of the class |
| 118 |
NextLevel=self.GetNextLevelBlocks(ClassBlock) ## makes the next level of hirearchy |
| 119 |
## this just makes textblocks of all the entries that belong to the next level in the tree |
| 120 |
for Object in NextLevel: ## for each textblock try to make an object and add it to the list |
| 121 |
## the entries are created as lists of lists |
| 122 |
## each line in the modelfile corresponds to a list |
| 123 |
## lists that correspond to datatypes will have more entries in one list |
| 124 |
## list corresponding to labels have only one entry |
| 125 |
## [ |
| 126 |
## [label1], |
| 127 |
## [label2], |
| 128 |
## [datatype1, datatype2, datatype3...], |
| 129 |
## [label4] |
| 130 |
## ] |
| 131 |
|
| 132 |
O=self.MakeObject(Object,BasicDataTypes) |
| 133 |
if (O): |
| 134 |
self.AddObject(O) |
| 135 |
else: ## if no object could be made, print an error and exit |
| 136 |
print "This block does not represent a valid entry:" |
| 137 |
print Object |
| 138 |
sys.exit(0) |
| 139 |
|
| 140 |
def GetNextLevelBlocks(self,Block): ## makes textblocks corresponding to the next level in the tree |
| 141 |
BLOCKS=[] |
| 142 |
TEMP=string.split(string.strip(Block[0])) ## split first line into parts |
| 143 |
if (len(TEMP) > 1): ## if object has parts |
| 144 |
INDENT=string.find(Block[0]," "+TEMP[1])+1 |
| 145 |
TEMP2=[] |
| 146 |
for LINE in Block: |
| 147 |
if (LINE[INDENT-1] not in [" ","\t"]): ## if the character before the current indentation level is a non whitespace, then we have a problem with the indentation |
| 148 |
print self.GetPath()+": There seems to be a problem with the indentation.\n\n" |
| 149 |
sys.exit(0) |
| 150 |
else: |
| 151 |
LINE=LINE[INDENT:] ## shorten each line according to the indentation level |
| 152 |
if (LINE[0] != " "): ## if the line doesn't start with a space, we have the start of a new object |
| 153 |
if (TEMP2 != []): ## is it the first one ?, if not |
| 154 |
BLOCKS.append(TEMP2) ## add the current object to the list |
| 155 |
TEMP2=[LINE] |
| 156 |
else: |
| 157 |
TEMP2.append(LINE) ## starts with a space? So it is part of the current object |
| 158 |
BLOCKS.append(TEMP2) ## don't forget to add the last one to the list |
| 159 |
else: ## if object doesn't have parts, it can only one line long |
| 160 |
## otherwise there is a problem with the indentation |
| 161 |
if (len(Block)>1): |
| 162 |
print self.GetPath()+": There seems to be a problem with the indentation.\n\n" |
| 163 |
sys.exit(0) |
| 164 |
return BLOCKS |
| 165 |
|
| 166 |
def __repr__(self): ## similar to __str__ |
| 167 |
return self |
| 168 |
|
| 169 |
def __str__(self): ## returns a string representation of the class |
| 170 |
STRING="" |
| 171 |
if (len(self.Parts) > 0): |
| 172 |
for PART in self.Parts: ## let every entry represent itself |
| 173 |
for P in PART: |
| 174 |
STRING=STRING+str(P)+" " |
| 175 |
STRING=STRING[:-1] |
| 176 |
LINES=string.split(STRING,"\n") |
| 177 |
LINES=LINES[:-1] |
| 178 |
LINES[0]=self.GetName()+"\t"+LINES[0] ## add the name of this instance to the beginning of the first line |
| 179 |
INDENT=(len(self.GetName())/8)+1 ## Indentation of the other lines |
| 180 |
for X in range(1,len(LINES)): |
| 181 |
LINES[X]=INDENT*"\t"+LINES[X] ## indent all the other lines |
| 182 |
STRING=string.join(LINES,"\n") |
| 183 |
STRING=STRING+"\n" |
| 184 |
else: |
| 185 |
STRING=self.GetName()+"\n" ## if the instance doesn't have entries, return just the name |
| 186 |
return STRING |
| 187 |
|
| 188 |
def SetParent(self,Parent): ## sets the parent node |
| 189 |
self.Parent=Parent |
| 190 |
|
| 191 |
def GetParent(self): ## return the parent node |
| 192 |
return self.Parent |
| 193 |
|
| 194 |
def SetParts(self,Parts): ## not used, but sets Parts |
| 195 |
self.Parts=Parts |
| 196 |
|
| 197 |
def GetParts(self): |
| 198 |
return self.Parts ## returns all entries |
| 199 |
|
| 200 |
def HasLabel(self,LabelName): ## checks wether the class has a specified label |
| 201 |
LABEL="" |
| 202 |
for PART in self.Parts: |
| 203 |
if (string.upper(PART[0].GetName()) == string.upper(LabelName)): ## if the class has the label |
| 204 |
LABEL=PART[0].GetPath() ## return the Path to the label |
| 205 |
break |
| 206 |
if (LABEL == ""): ## if the class doesn't contain the label directly, check further down |
| 207 |
for PART in self.Parts: |
| 208 |
LABEL=PART[0].HasLabel(LabelName) |
| 209 |
if (LABEL != ""): |
| 210 |
break |
| 211 |
return LABEL ## returns an empty string if the label wasn't found |
| 212 |
|
| 213 |
def Validate(self,BasicDataTypes,ClassDict,CharacterArrays): ## checks the class |
| 214 |
ERROR="" |
| 215 |
for PART in self.Parts: ## let every entry check itself |
| 216 |
for P in PART: |
| 217 |
ERROR=ERROR+P.Validate(BasicDataTypes,ClassDict,CharacterArrays) |
| 218 |
if (self.GetName()[0] != "?"): ## if the name doesn't start with an `?`, it's not a class |
| 219 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": Classes have to start with an '?'.\n" |
| 220 |
if (string.upper(self.GetName()) in BasicDataTypes+["UNIQUE","XREF","REPEAT"]): |
| 221 |
## if the name corrsponds to a reserved keyword, than print an error message |
| 222 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": The name "+self.GetName()+" is not allowed for a class.\n" |
| 223 |
if (("#"+string.upper(self.GetName()[1:])) in map(string.upper,ClassDict.keys()+CharacterArrays)): ## checks wether there already exists a defined datatype with the same name |
| 224 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": There already exists a defined datatype with this name.\n" |
| 225 |
if (len(self.Parts) < 1): ## if the class is empty |
| 226 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": Classes must have labels. They can not be empty.\n" |
| 227 |
return ERROR |
| 228 |
|
| 229 |
def SetName(self,Name): |
| 230 |
self.Name=Name |
| 231 |
|
| 232 |
def GetName(self): |
| 233 |
return self.Name |
| 234 |
|
| 235 |
def GetPath(self): ## returns the path of the instance ?Classname -> Label1 -> Label11 -> Datatype111 |
| 236 |
PATH="" |
| 237 |
if (hasattr(self,"Parent")): |
| 238 |
PATH=self.Parent.GetPath() |
| 239 |
if (PATH==""): |
| 240 |
PATH=self.Name |
| 241 |
else: |
| 242 |
PATH=PATH+" -> "+self.Name |
| 243 |
return PATH |
| 244 |
|
| 245 |
def AddObject(self,Object): ## just adds an entry |
| 246 |
self.Parts.append(Object) |
| 247 |
|
| 248 |
def RemoveObject(self,Object): ## removes an entry |
| 249 |
self.Parts.remove(Object) |
| 250 |
|
| 251 |
def MakeDataPackages(self,Object,BasicDataTypes): ## if the object is a line with several datatypes, it has to be divided into the individual datypes |
| 252 |
## this makes lists of the datatypes and their modifiers |
| 253 |
BITS=string.split(Object[0]) |
| 254 |
PACKAGES=[] |
| 255 |
LAST=-1 |
| 256 |
for X in range(0,len(BITS)): |
| 257 |
if ((string.upper(BITS[X]) in map(string.upper,BasicDataTypes)) or (BITS[X][0] in ["#","?"])): |
| 258 |
if (LAST != -1): |
| 259 |
if (string.upper(BITS[X-1]) == "UNIQUE"): |
| 260 |
PACKAGE=BITS[LAST:X-1] |
| 261 |
PACKAGES.append(PACKAGE) |
| 262 |
else: |
| 263 |
PACKAGE=BITS[LAST:X] |
| 264 |
PACKAGES.append(PACKAGE) |
| 265 |
LAST=X |
| 266 |
else: |
| 267 |
LAST=0 |
| 268 |
if (string.upper(BITS[LAST-1]) =="UNIQUE"): |
| 269 |
PACKAGES.append(BITS[LAST-1:]) |
| 270 |
else: |
| 271 |
PACKAGES.append(BITS[LAST:]) |
| 272 |
return PACKAGES |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
def MakeObject(self,Object,BasicDataTypes): ## this routine checks wether an object is a label or a datatype or a 'UNIQUE list' |
| 277 |
IDENTIFIER = string.split(Object[0])[0] |
| 278 |
OBJECT=[] |
| 279 |
if ((string.upper(IDENTIFIER) in map(string.upper,BasicDataTypes)) or (IDENTIFIER[0] in ["#","?"])): |
| 280 |
PACKAGES=self.MakeDataPackages(Object,BasicDataTypes) |
| 281 |
for PACKAGE in PACKAGES: |
| 282 |
OBJECT.append(DataType(self,PACKAGE,BasicDataTypes)) |
| 283 |
else: |
| 284 |
if (string.upper(IDENTIFIER) == "UNIQUE"): |
| 285 |
if ((len(Object) == 1) and ((string.upper(string.split(Object[0])[1]) in map(string.upper,BasicDataTypes)) or (string.split(Object[0])[1][0] in ["#","?"]))): |
| 286 |
PACKAGES=self.MakeDataPackages(Object,BasicDataTypes) |
| 287 |
OBJECT=[] |
| 288 |
for PACKAGE in PACKAGES: |
| 289 |
OBJECT.append(DataType(self,PACKAGE,BasicDataTypes)) |
| 290 |
else: |
| 291 |
OBJECT.append(ExclusiveListType(self,Object,BasicDataTypes)) |
| 292 |
else: |
| 293 |
if (string.upper(IDENTIFIER) in ["XREF","REPEAT"]): |
| 294 |
print self.GetPath() |
| 295 |
print "Fatal error. This entry can not have a modifier "+IDENTIFIER |
| 296 |
sys.exit(0) |
| 297 |
else: |
| 298 |
OBJECT.append(LabelType(self,Object,BasicDataTypes)) |
| 299 |
return OBJECT |
| 300 |
|
| 301 |
def GetNumParts(self): ## returns the number of entries |
| 302 |
return len(self.Parts) |
| 303 |
|
| 304 |
def GetLenPart(self,INDEX): ## returns the length of an entry |
| 305 |
## for labels this is 1 |
| 306 |
## for lines with datatypes, this corresponds to the number of datatypes on the line |
| 307 |
if (self.GetNumParts() >= INDEX): |
| 308 |
RESULT=len(self.Parts[INDEX]) |
| 309 |
else: |
| 310 |
RESULT=-1 |
| 311 |
return RESULT |
| 312 |
|
| 313 |
def FindElement(self,Element): ## this searches for an instance of a label or datatype in Parts and returns its index |
| 314 |
RESULT=(-1,-1) |
| 315 |
for PART in self.Parts: |
| 316 |
if (Element in PART): |
| 317 |
RESULT=(self.Parts.index(PART),PART.index(Element)) |
| 318 |
return RESULT |
| 319 |
|
| 320 |
|
| 321 |
class LabelType(ClassType): ## this class represents a label |
| 322 |
## it inherits everything from the ClassType class |
| 323 |
## only the validation method is overwritten |
| 324 |
def Validate(self,BasicDataTypes,ClassDict,CharacterArrays): ## validates the label |
| 325 |
ERROR="" |
| 326 |
for PART in self.Parts: ## let all entries check themselves |
| 327 |
for P in PART: |
| 328 |
ERROR=ERROR+P.Validate(BasicDataTypes,ClassDict,CharacterArrays) |
| 329 |
if ((self.GetName()[0] in ["#","?"]) or (string.upper(self.GetName()) in map(string.upper,BasicDataTypes)+["UNIQUE","XREF","REPEAT"])): |
| 330 |
## if the name starts with `#` or `?` or corresponds to a reserved keyword, than print an error message |
| 331 |
ERROR=ERROR+"ERROR "+self.GetPath()+": This name is not allowed for a label.\n\n" |
| 332 |
return ERROR |
| 333 |
|
| 334 |
class ExclusiveListType(ClassType): ## this class represents a UNIQUE list of labels |
| 335 |
## UNIQUE datatypes are just datatypes with the modifier UNIQUE |
| 336 |
## this class also inherits everything from ClassType and overwrites the validation method |
| 337 |
def Validate(self,BasicDataTypes,ClassDict,CharacterArrays): |
| 338 |
ERROR="" |
| 339 |
for PART in self.Parts: |
| 340 |
if (len(PART) > 1): ## if the entry has more than one entry, it can not be a label |
| 341 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": UNIQUE lists can only have labels not datatypes.\n" |
| 342 |
for P in PART: |
| 343 |
ERROR=ERROR+P.Validate(BasicDataTypes,ClassDict,CharacterArrays) |
| 344 |
if (string.upper(self.GetName()) != "UNIQUE"): ## actualy the UNIQUE list is just a label that has the name UNIQUE |
| 345 |
## if it doesn't have that name, it's an error |
| 346 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": UNIQUE lists must have name 'UNIQUE'.\n" |
| 347 |
if (len(self.Parts) < 1): ## UNIQUE lists can not be empty |
| 348 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": UNIQUE lists must have labels. They can not be empty.\n" |
| 349 |
return ERROR |
| 350 |
|
| 351 |
class UserDefinedType(ClassType): ## this class represents the user defined datatypes |
| 352 |
## it inherits everything from ClassType and overwrites the validation method |
| 353 |
def Validate(self,BasicDataTypes,ClassDict,CharacterArrays): |
| 354 |
ERROR="" |
| 355 |
for PART in self.Parts: ## let every entry check itself |
| 356 |
for P in PART: |
| 357 |
ERROR=ERROR+P.Validate(BasicDataTypes,ClassDict,CharacterArrays) |
| 358 |
if (self.GetName()[0] != "#"): ## per definition user defined datatypes have to start with an '#' |
| 359 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": User defined datatypes have to start with an '#'.\n" |
| 360 |
if (len(self.Parts) < 1): ## an empty user defined datatype doesn't make sense |
| 361 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": User defined datatypes not be empty.\n" |
| 362 |
if ((string.upper(self.GetName()[1:]) in map(string.upper,BasicDataTypes)+["UNIQUE","XREF","REPEAT"])): |
| 363 |
## if the name corrsponds to a reserved keyword, than print an error message |
| 364 |
ERROR=ERROR+"ERROR "+self.GetPath()+": This name is not allowed for a user defined datatype.\n\n" |
| 365 |
if ("?"+string.upper(self.GetName()[1:]) in map(string.upper,ClassDict.keys()+CharacterArrays)): ## checks wether there already exists a class with the same name |
| 366 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": There already exists a class with this name.\n" |
| 367 |
return ERROR |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
class DataType: ## this class represents a datatype |
| 372 |
def __init__(self,Parent,Object,BasicDataTypes): ## Constructor |
| 373 |
self.Modifiers=[] ## this lists hold the modifiers |
| 374 |
self.SetParent(Parent) ## sets the parent node |
| 375 |
for P in Object: ## an initialisation a datatype object gets a list of string that represents the individual parts of the datatype, like modifiers, name etc. |
| 376 |
## this list is not ordered |
| 377 |
if (string.upper(P) == "UNIQUE"): |
| 378 |
self.AddModifier(UNIQUE_Modifier(self,P)) |
| 379 |
if ((string.upper(P) in map(string.upper,BasicDataTypes)) or (P[0] in ["#","?"])): |
| 380 |
self.SetName(P) |
| 381 |
if (string.upper(P) == "XREF"): |
| 382 |
self.AddModifier(XREF_Modifier(self,P,Object[Object.index(P)+1])) |
| 383 |
if (string.upper(P) == "REPEAT"): |
| 384 |
self.AddModifier(REPEAT_Modifier(self,P)) |
| 385 |
|
| 386 |
|
| 387 |
def SetParent(self,Parent): ## sets the parent node |
| 388 |
self.Parent=Parent |
| 389 |
|
| 390 |
def GetParent(self): ## returns the parent node |
| 391 |
return self.Parent |
| 392 |
|
| 393 |
def Validate(self,BasicDataTypes,ClassDict,CharacterArrays): ## checks the datatype |
| 394 |
ERROR="" |
| 395 |
if ((string.upper(self.GetName()) not in map(string.upper,BasicDataTypes)) and (self.GetName()[0] not in ["?","#"])): |
| 396 |
## the name has to start with `?' or `#` or be a basic data type |
| 397 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": This datatype is not valid.\n" |
| 398 |
else: |
| 399 |
if (self.GetName()[0] in ["#","?"]): ## if it is a class, it must be defined in the model |
| 400 |
if ((string.upper(self.GetName()) not in map(string.upper,ClassDict.keys()+CharacterArrays)) and ("?"+string.upper(self.GetName()[1:]) not in map(string.upper,ClassDict.keys()+CharacterArrays))): |
| 401 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": This class or datatype was never defined.\n" |
| 402 |
else: ## check if the classname has consistens spelling concerning upper and lower case letters |
| 403 |
if ((self.GetName() not in ClassDict.keys()+CharacterArrays) and ("?"+self.GetName()[1:] not in ClassDict.keys()+CharacterArrays)): |
| 404 |
ERROR=ERROR+"WARNING! "+self.GetPath()+": There is an inconsistency in the spelling of a class or datatype concerning upper and lower case usage.\n" |
| 405 |
else: ## check if the basic datatype has consistent usage of upper and lower case letters |
| 406 |
if (self.GetName() not in BasicDataTypes): |
| 407 |
ERROR=ERROR+"WARNING! "+self.GetPath()+": There is an inconsistency in the spelling of a datatype concerning upper and lower case usage.\n" |
| 408 |
if (len(self.Modifiers) > 3): ## a datatype can at most have 3 modifiers |
| 409 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": A datatype can at most have 3 modifieres (UNIQUE xxx XREF yyy REPEAT).\n" |
| 410 |
else: |
| 411 |
XREF_count=0 |
| 412 |
UNIQUE_count=0 |
| 413 |
REPEAT_count=0 |
| 414 |
for Modif in self.Modifiers: |
| 415 |
if (isinstance(Modif,UNIQUE_Modifier)): |
| 416 |
UNIQUE_count=UNIQUE_count+1 |
| 417 |
else: |
| 418 |
if (isinstance(Modif,XREF_Modifier)): |
| 419 |
ERR=Modif.Validate(BasicDataTypes) |
| 420 |
if (ERR != ""): |
| 421 |
ERROR=ERROR+"ERROR in "+self.GetPath()+" "+Modif.__str__()+": "+ERR |
| 422 |
|
| 423 |
if (self.GetName()[0] != "?"): ## only labels in classinstance can be referenced |
| 424 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": Only classinstances can have references.\n" |
| 425 |
if (ClassDict.has_key(self.GetName())): |
| 426 |
CLASS=ClassDict[self.GetName()] |
| 427 |
if (CLASS.HasLabel(Modif.GetRef()) == ""): ## checks wether the reference exists |
| 428 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": The label '"+Modif.GetRef()+"' is not defined in class " +self.GetName()+".\n" |
| 429 |
else: |
| 430 |
LABELNAME=string.split(CLASS.HasLabel(Modif.GetRef()))[-1] |
| 431 |
if (LABELNAME != Modif.GetRef()): |
| 432 |
ERROR=ERROR+"WARNING! There is an inconsistency in the spelling of the label '"+CLASS.HasLabel(Modif.GetRef())+"'. In the reference that belongs to '"+self.GetPath()+"' it is spelled '"+Modif.GetRef()+"'.\n" |
| 433 |
|
| 434 |
XREF_count=XREF_count+1 |
| 435 |
else: |
| 436 |
if (isinstance(Modif,REPEAT_Modifier)): |
| 437 |
REPEAT_count=REPEAT_count+1 |
| 438 |
if (self.IsLast() !=1): ## only the last datatype can have modifier REPEAT |
| 439 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": Only the last datatype in a line can have a REPEAT modifier.\n" |
| 440 |
if ((REPEAT_count > 1) or (XREF_count > 1) or (UNIQUE_count > 1)): ## each modifier can only appear once |
| 441 |
ERROR=ERROR+"ERROR in "+self.GetPath()+": Each modifier can only be used once for a datatype.\n" |
| 442 |
return ERROR |
| 443 |
|
| 444 |
|
| 445 |
def AddModifier(self,Modif): ## adds a modifier |
| 446 |
self.Modifiers.append(Modif) |
| 447 |
|
| 448 |
def RemoveModifier(self,ModName): ## removes a modifier |
| 449 |
for Modif in self.Modifiers: |
| 450 |
if (Modif.GetName() == ModName): |
| 451 |
self.Modifiers.remove(Modif) |
| 452 |
|
| 453 |
|
| 454 |
def SetName(self,Name): ## sets the name |
| 455 |
self.Name=Name |
| 456 |
|
| 457 |
def GetName(self): ## returns the name |
| 458 |
return self.Name |
| 459 |
|
| 460 |
def GetPath(self): ## returns the path to the object |
| 461 |
PATH="" |
| 462 |
if (hasattr(self,"Parent")): |
| 463 |
PATH=self.Parent.GetPath() |
| 464 |
if (PATH==""): |
| 465 |
PATH=self.Name |
| 466 |
else: |
| 467 |
PATH=PATH+" -> "+self.Name |
| 468 |
return PATH |
| 469 |
|
| 470 |
|
| 471 |
def __str__(self): ## returns a string representation of the object |
| 472 |
STRING=self.GetName() |
| 473 |
for Modif in self.Modifiers: |
| 474 |
if (isinstance(Modif,UNIQUE_Modifier)): |
| 475 |
STRING=Modif.__str__()+" "+STRING |
| 476 |
else: |
| 477 |
STRING=STRING+" "+Modif.__str__() |
| 478 |
if (self.IsLast()==1): |
| 479 |
STRING=STRING+"\n" |
| 480 |
return STRING |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
def __repr__(self): ## similar to __str__ |
| 485 |
return self |
| 486 |
|
| 487 |
def IsLast(self): ## checks wether the datatype is the last one in a line |
| 488 |
RESULT=0 |
| 489 |
POSITION=self.Parent.FindElement(self) |
| 490 |
if ((POSITION[1]+1) == self.Parent.GetLenPart(POSITION[0])): |
| 491 |
RESULT=1 |
| 492 |
return RESULT |
| 493 |
|
| 494 |
|
| 495 |
def HasLabel(self,LabelName): ## this is needed to ensure that the recursive method for class and labels works |
| 496 |
return "" |
| 497 |
|
| 498 |
|
| 499 |
class Modifier: ## abstract modifier class |
| 500 |
def __init__(self,Parent,Name): |
| 501 |
self.SetParent(Parent) |
| 502 |
self.SetName(Name) |
| 503 |
|
| 504 |
def __str__(self): |
| 505 |
return self.GetName() |
| 506 |
|
| 507 |
def __repr__(self): |
| 508 |
return self |
| 509 |
|
| 510 |
def SetName(self,Name): |
| 511 |
self.Name=Name |
| 512 |
|
| 513 |
def SetParent(self,Parent): |
| 514 |
self.Parent=Parent |
| 515 |
|
| 516 |
def GetName(self): |
| 517 |
return self.Name |
| 518 |
|
| 519 |
def GetParent(self): |
| 520 |
return self.Parent |
| 521 |
|
| 522 |
def Validate(self,BasicDataTypes): |
| 523 |
return "" |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
class XREF_Modifier(Modifier): ## inherits from Modifier and adds some extra functinality |
| 528 |
def __init__(self,Parent,Name,Reference): |
| 529 |
Modifier.__init__(self,Parent,Name) |
| 530 |
self.SetRef(Reference) |
| 531 |
|
| 532 |
def __str__(self): |
| 533 |
return Modifier.__str__(self)+" "+self.GetRef() |
| 534 |
|
| 535 |
def SetRef(self,Ref): |
| 536 |
self.Reference=Ref |
| 537 |
|
| 538 |
def GetRef(self): |
| 539 |
return self.Reference |
| 540 |
|
| 541 |
def Validate(self,BasicDataTypes): |
| 542 |
Error = "" |
| 543 |
if ((self.GetRef()[0] in ["#","?"]) or (string.upper(self.GetRef()) in map(string.upper,BasicDataTypes))): |
| 544 |
Error="A reference can not be a class or datatype, it must be a label.\n" |
| 545 |
return Error |
| 546 |
|
| 547 |
|
| 548 |
class REPEAT_Modifier(Modifier): ## same as Modifier just needed for convienence |
| 549 |
pass |
| 550 |
|
| 551 |
|
| 552 |
class UNIQUE_Modifier(Modifier): ## same as Modifier |
| 553 |
pass |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if __name__=="__main__": ## this method is the main method |
| 561 |
## it is executed when the file is called from the command line |
| 562 |
## it is not executed when the file is imported as a module |
| 563 |
if (len(sys.argv) > 2): |
| 564 |
print "usage ParseModel.py 'modelfile'" |
| 565 |
|
| 566 |
else: |
| 567 |
File=sys.argv[1] ## first command line argument is the filename |
| 568 |
Text=open(File,'r').readlines() ## read all the text from the file |
| 569 |
M=Model(Text) ## creates an instance M of the Model |
| 570 |
print M.Validate() ## prints the reulst of the validation process for the whole model |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|