Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

[tools] Add Python 3 support to xml parser #261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions tools/system_design/xmlparser/action.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import utils
import xml_utils
from . import utils
from . import xml_utils

from parser_exception import ParserException
from .parser_exception import ParserException

class Action(object):

Expand All @@ -31,10 +31,20 @@ def __get_type(self, node, name, tree):
raise ParserException("Type '%s' is not defined. Used by Action '%s')" % (type, self.name))

return type

def __cmp__(self, other):
return cmp(self.id, other.id) or cmp(self.name, other.name)

return 1 - self.__eq__(other) - 2 * self.__lt__(other)

def __lt__(self, other):
if self.id == other.id:
return self.name < other.name
if self.id is None:
return other.id is not None
return other.id is not None and self.id < other.id

def __eq__(self, other):
return self.id == other.id and self.name == other.name

def update(self, top):
assert self.name == top.name
for key, value in self.__dict__.items():
Expand Down
30 changes: 21 additions & 9 deletions tools/system_design/xmlparser/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import copy

import utils
import action
import xml_utils
from parser_exception import ParserException
from . import utils
from . import action
from . import xml_utils
from .parser_exception import ParserException

class ComponentDictionary(utils.SingleAssignDictionary):

Expand Down Expand Up @@ -111,12 +111,14 @@ def iter(self, abstract=False):
"""
class ComponentIterator:
def __init__(self, list, abstract):
self.list = list
self.list.sort()
self.list = sorted(list)
self.abstract = abstract

def __iter__(self):
return self

def __next__(self):
return self.next()

def next(self):
try:
Expand Down Expand Up @@ -282,10 +284,20 @@ def flattened(self):
raise ParserException("No id defined for the non abstract component '%s'!" % flat.name)

self.__flattened = flat

def __cmp__(self, other):
return cmp(self.id, other.id) or cmp(self.name, other.name)

return 1 - self.__eq__(other) - 2 * self.__lt__(other)

def __lt__(self, other):
if self.id == other.id:
return self.name < other.name
if self.id is None:
return other.id is not None
return other.id is not None and self.id < other.id

def __eq__(self, other):
return self.id == other.id and self.name == other.name

def dump(self):
"""
Print a nice UML-like box of content of the component
Expand Down
20 changes: 15 additions & 5 deletions tools/system_design/xmlparser/container.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import utils
import xml_utils
from component import EventContainer
from . import utils
from . import xml_utils
from .component import EventContainer

from parser_exception import ParserException
from .parser_exception import ParserException

class Container:
""" Representation of a container which bundles components.
Expand Down Expand Up @@ -98,7 +98,17 @@ def updateIndex(self):
self.indexReady = True

def __cmp__(self, other):
return cmp(self.name.lower(), other.name.lower()) or cmp(self.id, other.id)
return 1 - self.__eq__(other) - 2 * self.__lt__(other)

def __lt__(self, other):
if self.id == other.id:
return self.name < other.name
if self.id is None:
return other.id is not None
return other.id is not None and self.id < other.id

def __eq__(self, other):
return self.id == other.id and self.name == other.name

def dump(self):
str = "%s : container\n" % self.__str__()
Expand Down
6 changes: 3 additions & 3 deletions tools/system_design/xmlparser/domain.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import utils
import xml_utils
from . import utils
from . import xml_utils

from parser_exception import ParserException
from .parser_exception import ParserException

class Domain:
""" Representation of a domain ids.
Expand Down
22 changes: 16 additions & 6 deletions tools/system_design/xmlparser/event.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import utils
import xml_utils
from . import utils
from . import xml_utils

from parser_exception import ParserException
from .parser_exception import ParserException

class Event(object):

Expand Down Expand Up @@ -54,10 +54,20 @@ def update(self, other):
The assert statement checks this, nothing else needs to be done.
"""
assert id(self) == id(other)

def __cmp__(self, other):
return cmp(self.id, other.id) or cmp(self.name, other.name)

return 1 - self.__eq__(other) - 2 * self.__lt__(other)

def __lt__(self, other):
if self.id == other.id:
return self.name < other.name
if self.id is None:
return other.id is not None
return other.id is not None and self.id < other.id

def __eq__(self, other):
return self.id == other.id and self.name == other.name

def __str__(self):
if self.type is None:
type = None
Expand Down
16 changes: 8 additions & 8 deletions tools/system_design/xmlparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import sys
import logging

from parser_exception import ParserException
import utils
import type
import event
import component
import container
import domain
from .parser_exception import ParserException
from . import utils
from . import type
from . import event
from . import component
from . import container
from . import domain

#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.WARNING)
Expand Down Expand Up @@ -177,7 +177,7 @@ def _parse_document(self, xmldocument):
# Add file information that is not available in the lower classes
# to exception. See:
# http://www.ianbicking.org/blog/2007/09/re-raising-exceptions.html
e.args = ("'%s': %s" % (xmldocument.docinfo.URL, e.message),) + e.args[1:0]
e.args = ("'%s': %s" % (xmldocument.docinfo.URL, str(e)),) + e.args[1:0]
raise

def _evaluate_tree(self):
Expand Down
31 changes: 21 additions & 10 deletions tools/system_design/xmlparser/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re
import copy

from parser_exception import ParserException
import utils
import xml_utils
from .parser_exception import ParserException
from . import utils
from . import xml_utils

VALID_UNDERLYING_TYPES_FOR_ENUMS = [
'int8_t', 'uint8_t', 'int16_t', 'uint16_t', 'int32_t', 'uint32_t', 'int64_t', 'uint64_t' ]
Expand Down Expand Up @@ -70,16 +70,27 @@ def create_hierarchy(self):
def flattened(self):
""" Access the version with the flattened hierarchy """
return self

def __cmp__(self, other):
return 1 - self.__eq__(other) - 2 * self.__lt__(other)

def __lt__(self, other):
""" Compare two types
If types are sorted they are sorted first by level and then by name.

If types are sorted, they are sorted first by level and then by name.
"""
if isinstance(other, BaseType):
return cmp(self.level, other.level) or cmp(self.name, other.name)
else:
return 1
if not isinstance(other, BaseType):
return False
if self.level == other.level:
return self.name < other.name
if self.level is None:
return other.level is not None
return other.level is not None and self.level < other.level

def __eq__(self, other):
if not isinstance(other, BaseType):
return False
return self.level == other.level and self.name == other.name


class BuiltIn(BaseType):
Expand Down
12 changes: 7 additions & 5 deletions tools/system_design/xmlparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
import copy

from parser_exception import ParserException
from .parser_exception import ParserException

def check_name(name):
""" Checks if a string comply with some rules for the notation
Expand All @@ -21,12 +21,14 @@ class SortedDictionary(dict):
def __iter__(self):
class Iterator:
def __init__(self, list):
self.list = list
self.list.sort()
self.list = sorted(list)

def __iter__(self):
return self


def __next__(self):
return self.next()

def next(self):
try:
item = self.list.pop(0)
Expand All @@ -48,7 +50,7 @@ def __init__(self, name):
SortedDictionary.__init__(self)

def __setitem__(self, key, item):
if not self.has_key(key):
if key not in self:
SortedDictionary.__setitem__(self, key, item)
else:
raise ParserException("%s '%s' defined twice!" % (self.name.capitalize(), key))
Expand Down