Skip to content

Commit

Permalink
Even more splitting of modules
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Sep 28, 2022
1 parent 1ae8003 commit 1354968
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
52 changes: 4 additions & 48 deletions scripts/idl/xml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,16 @@
from typing import Optional, Union, List

try:
from idl.zapxml.handlers import Context, ZapXmlHandler
from idl.matter_idl_types import Idl
except:
import sys

sys.path.append(os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
from idl.zapxml.handlers import Context, ZapXmlHandler
from idl.matter_idl_types import Idl

from idl.matter_idl_types import Idl
import idl.zapxml

class ParseHandler(xml.sax.handler.ContentHandler):
def __init__(self):
super().__init__()
self._idl = Idl()
self._processing_stack = []
# Context persists across all
self._context = Context()

def PrepareParsing(self, filename):
# This is a bit ugly: filename keeps changing during parse
# IDL meta is not prepared for this (as source is XML and .matter is
# single file)
self._idl.parse_file_name = filename

def ProcessResults(self) -> Idl:
return self._idl

def startDocument(self):
self._context.locator = self._locator
self._processing_stack = [ZapXmlHandler(self._context, self._idl)]

def endDocument(self):
if len(self._processing_stack) != 1:
raise Exception("Unexpected nesting!")

self._context.PostProcess(self._idl)

def startElement(self, name: str, attrs):
logging.debug("ELEMENT START: %r / %r" % (name, attrs))
self._context.path.push(name)
self._processing_stack.append(self._processing_stack[-1].GetNextProcessor(name, attrs))

def endElement(self, name: str):
logging.debug("ELEMENT END: %r" % name)

last = self._processing_stack.pop()
last.EndProcessing()

# important to pop AFTER processing end to allow processing
# end to access the current context
self._context.path.pop()

def characters(self, content):
self._processing_stack[-1].HandleContent(content)


@ dataclass
Expand All @@ -94,7 +50,7 @@ def source_file_name(self):


def ParseXmls(sources: List[ParseSource]) -> Idl:
handler = ParseHandler()
handler = idl.zapxml.ParseHandler()

for source in sources:
logging.info('Parsing %s...' % source.source_file_name)
Expand Down
50 changes: 50 additions & 0 deletions scripts/idl/zapxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,53 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import xml.sax.handler

from idl.zapxml.handlers import Context, ZapXmlHandler
from idl.matter_idl_types import Idl

class ParseHandler(xml.sax.handler.ContentHandler):
def __init__(self):
super().__init__()
self._idl = Idl()
self._processing_stack = []
# Context persists across all
self._context = Context()

def PrepareParsing(self, filename):
# This is a bit ugly: filename keeps changing during parse
# IDL meta is not prepared for this (as source is XML and .matter is
# single file)
self._idl.parse_file_name = filename

def ProcessResults(self) -> Idl:
return self._idl

def startDocument(self):
self._context.locator = self._locator
self._processing_stack = [ZapXmlHandler(self._context, self._idl)]

def endDocument(self):
if len(self._processing_stack) != 1:
raise Exception("Unexpected nesting!")

self._context.PostProcess(self._idl)

def startElement(self, name: str, attrs):
logging.debug("ELEMENT START: %r / %r" % (name, attrs))
self._context.path.push(name)
self._processing_stack.append(self._processing_stack[-1].GetNextProcessor(name, attrs))

def endElement(self, name: str):
logging.debug("ELEMENT END: %r" % name)

last = self._processing_stack.pop()
last.EndProcessing()

# important to pop AFTER processing end to allow processing
# end to access the current context
self._context.path.pop()

def characters(self, content):
self._processing_stack[-1].HandleContent(content)

0 comments on commit 1354968

Please sign in to comment.