Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. def factory():
  2.     document = {'root': None,
  3.                 'stack': []}
  4.  
  5.     class Tag(object):
  6.  
  7.         def __init__(self, name, namespace='', **attrs):
  8.  
  9.             _base = namespace + name if namespace else name
  10.  
  11.             self._el = etree.Element(_base, **attrs)
  12.  
  13.         def __enter__(self):
  14.             if document['root'] is None:
  15.                 document['root'] = self._el
  16.             document['stack'].append(self._el)
  17.             return self._el
  18.  
  19.         def __exit__(self, *args):
  20.             document['stack'].pop()
  21.             if document['stack']:
  22.                 document['stack'][-1].append(self._el)
  23.  
  24.     return Tag, document
  25.  
  26.  
  27. tag, doc = factory()
  28.  
  29. with tag('GetRequestRequest', NS_1):
  30.     with tag('MessageTypeSelector', NS_2):
  31.         with tag('NamespaceURI') as ns_uri:
  32.             ns_uri.text = 'some bullshit about ns'
  33.         with tag('RootElementLocalName'):
  34.             pass
  35.         with tag('Timestamp'):
  36.             pass
  37.     with tag('CallerInformationSystemSignature', NS_1):
  38.         with tag('Signature'):
  39.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement