Skip to content

Commit

Permalink
Merge pull request #23 from uogbuji/develop
Browse files Browse the repository at this point in the history
3.2.1 release prep
  • Loading branch information
uogbuji authored Jul 15, 2021
2 parents fb6fc10 + 30dc626 commit 1093635
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 36 deletions.
8 changes: 5 additions & 3 deletions example/legacyparse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
'''
Not sure what this file was all about. WIll delete soon
'''

from amara3.uxml.xml import parse, buffer_handler
evs = []
Expand All @@ -7,6 +9,6 @@
print(evs)


@asyncio.coroutine
def handle_start_element(ev)
def handle_start_element(ev):
pass

2 changes: 0 additions & 2 deletions exec/microx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ import sys
import os
import time
import argparse
from asyncio import coroutine
from itertools import islice, chain

import logging
Expand Down Expand Up @@ -187,7 +186,6 @@ def run(command_name, command_detail, sources=None, foreach=None, partition=None
if search_for in node.xml_value:
print(xpath_to(elem, show_attrs), file=out)

@coroutine
def sink():
while True:
root = yield
Expand Down
25 changes: 19 additions & 6 deletions pylib/uxml/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def hasContent(self):
"""
return bool(self.xml_children)

def xml_encode(self):
return self.toxml()


def qname_to_local(qname):
return qname.split(':')[-1]
Expand Down Expand Up @@ -130,23 +133,33 @@ def cloneNode(self):
return element(self.xml_name, attrs=attrs)


#class comment(tree.comment):
class comment(object):
# class comment(tree.comment):
class comment(node):
type = 6
value = tree.text.xml_value
def __init__(self, data):
self.data = data
# XXX Next 2 wererequired by html5lib tree API,
# but maybe only for __str__, which is now overridden
self.attributes = {}
self.name = '#comment'
return

def toxml(self):
return "<!--%s-->" % self.data
return f'<!--{self.data}-->'

def __str__(self):
return f'<#comment {self.data}>'

#Note: element = 6

class document(object):
class document(node):
type = 9
def __init__(self):
self.root_nodes = []
# Next 2 required by html5lib tree API
self.attributes = {}
self.name = '#document'
return

def appendChild(self, node):
Expand All @@ -155,13 +168,13 @@ def appendChild(self, node):
def toxml(self):
return "<!--%s-->" % self.data


class doctype(object):
class doctype(node):
type = 10
def __init__(self, name, publicId, systemId):
self.name = name
self.publicId = publicId
self.systemId = systemId
self.attributes = {} # Required by html5lib tree API
return

def toxml(self):
Expand Down
11 changes: 7 additions & 4 deletions pylib/uxml/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

import sys
import weakref
import asyncio
from xml.sax.saxutils import escape, quoteattr

from amara3.uxml.parser import parse, parser, parsefrags, event
from amara3.uxml.parser import parser, event # parse, parsefrags

# NO_PARENT = object()

Expand Down Expand Up @@ -69,15 +68,20 @@ def xml_encode(self, indent=None, depth=0):
if indent:
strbits.append('\n')
strbits.append(indent*depth)
else:
elif isinstance(child, str):
strbits.append(escape(child))
else:
strbits.append(child.xml_encode())
strbits.extend(['</', self.xml_name, '>'])
return ''.join(strbits)

# What's the difference from strval?
@property
def xml_value(self):
'''
Accumulated text in all descendant elements (similar to XPath text value)
'''
# Recursive action
return ''.join(map(lambda x: x.xml_value, self.xml_children))

#Really just an alias that forbids specifying position
Expand Down Expand Up @@ -161,7 +165,6 @@ def __init__(self):
self._root = None
self._parent = None

@asyncio.coroutine
def _handler(self):
while True:
ev = yield
Expand Down
10 changes: 4 additions & 6 deletions pylib/uxml/treeiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#
# -----------------------------------------------------------------------------

import asyncio
import collections
from collections.abc import Iterable

from .parser import parser, parsefrags, event
from .parser import parser, event # parsefrags
from .tree import element, text, name_test


Expand Down Expand Up @@ -56,10 +55,10 @@ def __init__(self, patterns, sinks, prime_sinks=True):
self._stateses = [None] * self._pattern_count
self._evstacks = [[]] * self._pattern_count
self._building_depths = [0] * self._pattern_count
#if asyncio.iscoroutine(sink):
# if asyncio.iscoroutine(sink):
if prime_sinks:
for sink in self._sinks:
if isinstance(sink, collections.Iterable):
if isinstance(sink, Iterable):
next(sink) # Prime coroutine
self._currents = [None] * self._pattern_count
self._prep_patterns()
Expand Down Expand Up @@ -121,7 +120,6 @@ def _match_state(self, ix):
return False
return False

@asyncio.coroutine
def _handler(self):
while True:
ev = yield
Expand Down
2 changes: 1 addition & 1 deletion pylib/uxml/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# http://legacy.python.org/dev/peps/pep-0440/
version_info = ('3', '2', '0')
version_info = ('3', '2', '1')
7 changes: 3 additions & 4 deletions pylib/uxml/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
#
# -----------------------------------------------------------------------------

import asyncio
import xml.parsers.expat
from xml.sax.saxutils import escape # also quoteattr?
# from xml.sax.saxutils import escape # also quoteattr?

from . import tree
from .parser import parser, parsefrags, event
from .parser import event # parser, parsefrags


class expat_callbacks(object):
def __init__(self, handler, prime_handler=True):
self._handler = handler
self._elem_stack = []
#if asyncio.iscoroutine(handler):
# if asyncio.iscoroutine(handler):
if prime_handler:
next(handler) # Prime coroutine
return
Expand Down
3 changes: 1 addition & 2 deletions pylib/uxml/xmliter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
#
# -----------------------------------------------------------------------------

import asyncio
import xml.parsers.expat

from . import treeiter
from .xml import expat_callbacks, ns_expat_callbacks
from .xml import expat_callbacks # ns_expat_callbacks


def buffer_handler(accumulator):
Expand Down
1 change: 0 additions & 1 deletion test/uxml/test_chunk_parse_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ def record_sink(writer_obj):
if __name__ == '__main__':
raise SystemExit("Run with py.test")

#@coroutine
11 changes: 10 additions & 1 deletion test/uxml/test_html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
import io
import logging
from asyncio import coroutine

import pytest #Consider also installing pytest_capturelog
from amara3.uxml import tree
Expand Down Expand Up @@ -34,5 +33,15 @@ def test_basic_nav(doc):
assert elem.xml_parent is root, (elem, root)


DOC2 = '<html><head><title>HELLO</title></head><body><p>WORLD<!--Comment--></body></html>'
DOC2_NORMALIZED = '<html><head><title>HELLO</title></head><body><p>WORLD<!--Comment--></p></body></html>'

def test_xml_encode_with_comment():
root = html5.parse(io.StringIO(DOC2))
logging.debug('root: {}'.format(repr(root)))
# Round trip
assert root.xml_encode() == DOC2_NORMALIZED


if __name__ == '__main__':
raise SystemExit("Run with py.test")
2 changes: 0 additions & 2 deletions test/uxml/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from asyncio import coroutine

from amara3.uxml.parser import parse, parser, parsefrags, event

Expand Down Expand Up @@ -103,7 +102,6 @@

#def test_basic():

@coroutine
def handler(accumulator):
while True:
event = yield
Expand Down
1 change: 0 additions & 1 deletion test/uxml/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import sys
import logging
from asyncio import coroutine

import pytest #Consider also installing pytest_capturelog
from amara3.uxml import tree
Expand Down
2 changes: 0 additions & 2 deletions test/uxml/test_treegc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import sys
import gc
from asyncio import coroutine

import pytest
from amara3.uxml import tree
Expand All @@ -24,7 +23,6 @@

@pytest.mark.parametrize('doc,pat,expected', TREESEQ_CASES)
def test_ts_gc(doc, pat, expected):
@coroutine
def sink(accumulator):
old_e = None
while True:
Expand Down
1 change: 0 additions & 1 deletion test/uxml/test_treeutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest
from amara3.uxml import tree
from amara3.uxml.treeutil import *
#from amara3.util import coroutine

DOC1 = '<a><b>1</b><b>2</b><b>3</b></a>'
DOC2 = '<a><b>1</b><c>2</c><d>3</d></a>'
Expand Down

0 comments on commit 1093635

Please sign in to comment.