Пример #1
0
def createJSONAttributesActions(injectAttributes=None, captureAttributes=None):
    '''
    Create the actions associated with the provided attributes.
    
    @param injectAttributes: dictionary{string: string}
        The attributes to be injected dictionary, as a key the action to be associated with the attribute injection and
        as a value the attribute name to be injected.
    @param captureAttributes: dictionary{string: string}
        The attributes to be captured dictionary, as a key the action to be associated with the attribute capture and
        as a value the name to be used latter on as a reference for the attribute capture.
    @return: list[Action]
        The created list of actions for the attributes.
    '''
    actions = []
    if injectAttributes:
        assert isinstance(injectAttributes, dict), 'Invalid inject attributes %s' % injectAttributes
        for action, nameAttr in injectAttributes.items():
            actions.append(Action(action,
                                  feed(SIND_ATTRS),
                                  setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR), feed(EIND_ATTRS),
                                  feedValue(',', FLAG_COMMA_ATTR),
                                  feedValue('"%s":"' % nameAttr), feedContent(escapes=ESCAPE_JSON), feedValue('"'),
                                  setFlag(FLAG_COMMA_ATTR),
                                  final=False, before=(ACTION_JSON_COMMA,)))
    
    if captureAttributes:
        assert isinstance(captureAttributes, dict), 'Invalid capture attributes %s' % captureAttributes
        for action, name in captureAttributes.items():
            actions.append(Action(action,
                                  skip(PSIND_ATTR_CAPTURE % name),
                                  feed(PEIND_ATTR_CAPTURE % name),
                                  rewind=True, final=False))
    return actions
Пример #2
0
def createJSONBlockForIndexed(name,
                              *actions,
                              injectAttributes=None,
                              captureAttributes=None):
    '''
    Create a new JSON block that can be used for injecting indexed content.
    
    @param name: string
        The name that can be latter on used as a reference for the block.
    @param actions: arguments[Action]
        Additional actions to be registered with the created block.
    @see: createJSONAttributesActions
    '''
    actions = list(actions)
    actions.extend(
        createJSONAttributesActions(injectAttributes, captureAttributes))

    actionsNamed = list(actions)
    actionsNamed.extend(BLOCK_JSON_NAMED.actions)
    actionsNamed.append(
        Action(ACTION_INJECT,
               feed(SIND_NAME),
               push(VAR_JSON_NAME, EIND_NAME),
               feedName(VAR_JSON_NAME),
               feed(SIND_VALUE),
               skip(SIND_ATTRS),
               push(VAR_JSON_ATTRS, EIND_ATTRS),
               skip(EIND_VALUE),
               feedIndexed(actions=(ACTION_JSON_ADJUST, )),
               feed(EIND_ENTRY),
               pop(VAR_JSON_NAME),
               pop(VAR_JSON_ATTRS),
               before=(ACTION_JSON_COMMA, )))

    actionsUnamed = list(actions)
    actionsUnamed.extend(BLOCK_JSON_UNNAMED.actions)
    actionsUnamed.append(
        Action(ACTION_INJECT,
               pushKey(VAR_JSON_NAME, KEY_NAME),
               feed(SIND_VALUE),
               skip(SIND_ATTRS),
               push(VAR_JSON_ATTRS, EIND_ATTRS),
               skip(EIND_VALUE),
               feedIndexed(actions=(ACTION_JSON_ADJUST, )),
               feed(EIND_ENTRY),
               pop(VAR_JSON_NAME),
               pop(VAR_JSON_ATTRS),
               before=(ACTION_JSON_COMMA, )))

    return {
        PATTERN_JSON_BLOCK_NAMED % name: Block(*actionsNamed),
        PATTERN_JSON_BLOCK_UNAMED % name: Block(*actionsUnamed,
                                                keys=(KEY_NAME, ))
    }
Пример #3
0
def createJSONAttributesActions(injectAttributes=None, captureAttributes=None):
    '''
    Create the actions associated with the provided attributes.
    
    @param injectAttributes: dictionary{string: string}
        The attributes to be injected dictionary, as a key the action to be associated with the attribute injection and
        as a value the attribute name to be injected.
    @param captureAttributes: dictionary{string: string}
        The attributes to be captured dictionary, as a key the action to be associated with the attribute capture and
        as a value the name to be used latter on as a reference for the attribute capture.
    @return: list[Action]
        The created list of actions for the attributes.
    '''
    actions = []
    if injectAttributes:
        assert isinstance(
            injectAttributes,
            dict), 'Invalid inject attributes %s' % injectAttributes
        for action, nameAttr in injectAttributes.items():
            actions.append(
                Action(action,
                       feed(SIND_ATTRS),
                       setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR),
                       feed(EIND_ATTRS),
                       feedValue(',', FLAG_COMMA_ATTR),
                       feedValue('"%s":"' % nameAttr),
                       feedContent(escapes=ESCAPE_JSON),
                       feedValue('"'),
                       setFlag(FLAG_COMMA_ATTR),
                       final=False,
                       before=(ACTION_JSON_COMMA, )))

    if captureAttributes:
        assert isinstance(
            captureAttributes,
            dict), 'Invalid capture attributes %s' % captureAttributes
        for action, name in captureAttributes.items():
            actions.append(
                Action(action,
                       skip(PSIND_ATTR_CAPTURE % name),
                       feed(PEIND_ATTR_CAPTURE % name),
                       rewind=True,
                       final=False))
    return actions
Пример #4
0
def createJSONBlockForIndexed(name, *actions, injectAttributes=None, captureAttributes=None):
    '''
    Create a new JSON block that can be used for injecting indexed content.
    
    @param name: string
        The name that can be latter on used as a reference for the block.
    @param actions: arguments[Action]
        Additional actions to be registered with the created block.
    @see: createJSONAttributesActions
    '''
    actions = list(actions)
    actions.extend(createJSONAttributesActions(injectAttributes, captureAttributes))
    
    actionsNamed = list(actions)
    actionsNamed.extend(BLOCK_JSON_NAMED.actions)
    actionsNamed.append(Action(ACTION_INJECT,
                               feed(SIND_NAME), push(VAR_JSON_NAME, EIND_NAME), feedName(VAR_JSON_NAME),
                               feed(SIND_VALUE),
                               skip(SIND_ATTRS), push(VAR_JSON_ATTRS, EIND_ATTRS),
                               skip(EIND_VALUE),
                               feedIndexed(actions=(ACTION_JSON_ADJUST,)),
                               feed(EIND_ENTRY),
                               pop(VAR_JSON_NAME), pop(VAR_JSON_ATTRS),
                               before=(ACTION_JSON_COMMA,))
                        )
    
    actionsUnamed = list(actions)
    actionsUnamed.extend(BLOCK_JSON_UNNAMED.actions)
    actionsUnamed.append(Action(ACTION_INJECT,
                                pushKey(VAR_JSON_NAME, KEY_NAME),
                                feed(SIND_VALUE),
                                skip(SIND_ATTRS), push(VAR_JSON_ATTRS, EIND_ATTRS),
                                skip(EIND_VALUE),
                                feedIndexed(actions=(ACTION_JSON_ADJUST,)),
                                feed(EIND_ENTRY),
                                pop(VAR_JSON_NAME), pop(VAR_JSON_ATTRS),
                                before=(ACTION_JSON_COMMA,))
                         )
    
    return {PATTERN_JSON_BLOCK_NAMED % name: Block(*actionsNamed),
            PATTERN_JSON_BLOCK_UNAMED % name: Block(*actionsUnamed, keys=(KEY_NAME,))}
Пример #5
0
def createJSONBlockForContent(name, *actions, injectAttributes=None, captureAttributes=None):
    '''
    Create a new JSON block that can be used for injecting text content.
    
    @param name: string
        The name that can be latter on used as a reference for the block.
    @param actions: arguments[Action]
        Additional actions to be registered with the created block.
    @see: createJSONAttributesActions
    '''
    actions = list(actions)
    actions.extend(createJSONAttributesActions(injectAttributes, captureAttributes))
    
    actionsNamed = list(actions)
    actionsNamed.extend(BLOCK_JSON_NAMED.actions)
    actionsNamed.append(Action(ACTION_INJECT,
                               feed(SIND_NAME), push(VAR_JSON_NAME, EIND_NAME), feedName(VAR_JSON_NAME),
                               feed(SIND_ATTRS),
                               setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR), feed(EIND_ATTRS),
                               feedValue(',', FLAG_COMMA_ATTR),
                               feedValue('"'), feedName(VAR_JSON_NAME), feedValue('":"'),
                               feedContent(escapes=ESCAPE_JSON), feedValue('"'), feed(EIND_ENTRY),
                               pop(VAR_JSON_NAME), pop(VAR_JSON_ATTRS),
                               before=(ACTION_JSON_COMMA,))
                        )
    
    actionsUnamed = list(actions)
    actionsUnamed.extend(BLOCK_JSON_UNNAMED.actions)
    actionsUnamed.append(Action(ACTION_INJECT,
                                pushKey(VAR_JSON_NAME, KEY_NAME),
                                feed(SIND_ATTRS),
                                setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR), feed(EIND_ATTRS),
                                feedValue(',', FLAG_COMMA_ATTR),
                                feedValue('"'), feedName(VAR_JSON_NAME), feedValue('":"'),
                                feedContent(escapes=ESCAPE_JSON), feedValue('"'), feed(EIND_ENTRY),
                                pop(VAR_JSON_NAME), pop(VAR_JSON_ATTRS),
                                before=(ACTION_JSON_COMMA,))
                         )
    
    return {PATTERN_JSON_BLOCK_NAMED % name: Block(*actionsNamed),
            PATTERN_JSON_BLOCK_UNAMED % name: Block(*actionsUnamed, keys=(KEY_NAME,))}
Пример #6
0
SIND_VALUE, EIND_VALUE = 'JSON start value', 'JSON end value'
SIND_NAME, EIND_NAME = 'JSON start name', 'JSON end name'
SIND_ATTRS, EIND_ATTRS = 'JSON start attributes', 'JSON end attributes'
KEY_NAME = 'JSON key block name'

# The JSON flags.
FLAG_COMMA = 'comma required'
FLAG_COMMA_ATTR = 'comma attribute required'

# The escape characters for values.
ESCAPE_JSON = {'\\': '\\\\', '"': '\\"', '\n': '\\n', '\r': '\\r', '\t': '\\t'}

# The common actions for JSON block.
ACTIONS_BLOCK_JSON = (
                      Action(ACTION_JSON_COMMA,
                             skip(SIND_ENTRY), feed(SIND_COMMA), feed(EIND_COMMA, FLAG_COMMA), skip(EIND_COMMA),
                             setFlag(FLAG_COMMA),
                             final=False),
                      Action(ACTION_STREAM,
                             feed(EIND_ENTRY), remFlag(FLAG_COMMA_ATTR),
                             before=(ACTION_JSON_COMMA,)),
                      Action(ACTION_DISCARD,
                             skip(EIND_ENTRY)),
                      )

# The JSON block.
BLOCK_JSON_NAMED = Block(
                         Action(ACTION_NAME,
                                skip(SIND_NAME), feed(EIND_NAME),
                                rewind=True, final=False),
                         *ACTIONS_BLOCK_JSON
Пример #7
0
def createJSONBlockForContent(name,
                              *actions,
                              injectAttributes=None,
                              captureAttributes=None):
    '''
    Create a new JSON block that can be used for injecting text content.
    
    @param name: string
        The name that can be latter on used as a reference for the block.
    @param actions: arguments[Action]
        Additional actions to be registered with the created block.
    @see: createJSONAttributesActions
    '''
    actions = list(actions)
    actions.extend(
        createJSONAttributesActions(injectAttributes, captureAttributes))

    actionsNamed = list(actions)
    actionsNamed.extend(BLOCK_JSON_NAMED.actions)
    actionsNamed.append(
        Action(ACTION_INJECT,
               feed(SIND_NAME),
               push(VAR_JSON_NAME, EIND_NAME),
               feedName(VAR_JSON_NAME),
               feed(SIND_ATTRS),
               setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR),
               feed(EIND_ATTRS),
               feedValue(',', FLAG_COMMA_ATTR),
               feedValue('"'),
               feedName(VAR_JSON_NAME),
               feedValue('":"'),
               feedContent(escapes=ESCAPE_JSON),
               feedValue('"'),
               feed(EIND_ENTRY),
               pop(VAR_JSON_NAME),
               pop(VAR_JSON_ATTRS),
               before=(ACTION_JSON_COMMA, )))

    actionsUnamed = list(actions)
    actionsUnamed.extend(BLOCK_JSON_UNNAMED.actions)
    actionsUnamed.append(
        Action(ACTION_INJECT,
               pushKey(VAR_JSON_NAME, KEY_NAME),
               feed(SIND_ATTRS),
               setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR),
               feed(EIND_ATTRS),
               feedValue(',', FLAG_COMMA_ATTR),
               feedValue('"'),
               feedName(VAR_JSON_NAME),
               feedValue('":"'),
               feedContent(escapes=ESCAPE_JSON),
               feedValue('"'),
               feed(EIND_ENTRY),
               pop(VAR_JSON_NAME),
               pop(VAR_JSON_ATTRS),
               before=(ACTION_JSON_COMMA, )))

    return {
        PATTERN_JSON_BLOCK_NAMED % name: Block(*actionsNamed),
        PATTERN_JSON_BLOCK_UNAMED % name: Block(*actionsUnamed,
                                                keys=(KEY_NAME, ))
    }
Пример #8
0
SIND_NAME, EIND_NAME = 'JSON start name', 'JSON end name'
SIND_ATTRS, EIND_ATTRS = 'JSON start attributes', 'JSON end attributes'
KEY_NAME = 'JSON key block name'

# The JSON flags.
FLAG_COMMA = 'comma required'
FLAG_COMMA_ATTR = 'comma attribute required'

# The escape characters for values.
ESCAPE_JSON = {'\\': '\\\\', '"': '\\"', '\n': '\\n', '\r': '\\r', '\t': '\\t'}

# The common actions for JSON block.
ACTIONS_BLOCK_JSON = (
    Action(ACTION_JSON_COMMA,
           skip(SIND_ENTRY),
           feed(SIND_COMMA),
           feed(EIND_COMMA, FLAG_COMMA),
           skip(EIND_COMMA),
           setFlag(FLAG_COMMA),
           final=False),
    Action(ACTION_STREAM,
           feed(EIND_ENTRY),
           remFlag(FLAG_COMMA_ATTR),
           before=(ACTION_JSON_COMMA, )),
    Action(ACTION_DISCARD, skip(EIND_ENTRY)),
)

# The JSON block.
BLOCK_JSON_NAMED = Block(
    Action(ACTION_NAME,
           skip(SIND_NAME),
Пример #9
0
SIND_ATTRS, EIND_ATTRS = 'XML start tag attributes', 'XML end tag attributes'
SIND_CLOSE_NAME, EIND_CLOSE_NAME = 'XML close start tag name', 'XML close end tag name'

# The JSON flags.
FLAG_TAG_CLOSED = 'close tag present'

# The escape characters for content value.
ESCAPE_XML_CONTENT = {'&': '&amp;', '>': '&gt;', '<': '&lt;'}
# The escape characters for attribute value.
ESCAPE_XML_ATTRIBUTE = {'"': "&quot;", '\n': '&#10;', '\r': '&#13;', '\t':'&#9;'}
ESCAPE_XML_ATTRIBUTE.update(ESCAPE_XML_CONTENT)

# The XML block.
BLOCK_XML = Block(
                  Action(ACTION_NAME,
                         skip(SIND_NAME), feed(EIND_NAME),
                         rewind=True, final=False),
                  Action(ACTION_STREAM,
                         skip(SIND_TAG), feed(EIND_CLOSE_TAG)),
                  Action(ACTION_DISCARD,
                         skip(EIND_CLOSE_TAG)),
                  )
                                  
# Provides the XML standard block definitions.
BLOCKS_XML = {
              NAME_XML_START_ADJUST:  
              Block(
                    Action(ACTION_XML_ADJUST,
                           skip(IND_DECL),
                           feed(SIND_NAME), skip(EIND_NAME), feedName(VAR_XML_NAME),
                           feed(EIND_ATTRS), feedName(VAR_XML_ATTRS),
Пример #10
0
# The escape characters for content value.
ESCAPE_XML_CONTENT = {'&': '&amp;', '>': '&gt;', '<': '&lt;'}
# The escape characters for attribute value.
ESCAPE_XML_ATTRIBUTE = {
    '"': "&quot;",
    '\n': '&#10;',
    '\r': '&#13;',
    '\t': '&#9;'
}
ESCAPE_XML_ATTRIBUTE.update(ESCAPE_XML_CONTENT)

# The XML block.
BLOCK_XML = Block(
    Action(ACTION_NAME,
           skip(SIND_NAME),
           feed(EIND_NAME),
           rewind=True,
           final=False),
    Action(ACTION_STREAM, skip(SIND_TAG), feed(EIND_CLOSE_TAG)),
    Action(ACTION_DISCARD, skip(EIND_CLOSE_TAG)),
)

# Provides the XML standard block definitions.
BLOCKS_XML = {
    NAME_XML_START_ADJUST:
    Block(
        Action(ACTION_XML_ADJUST, skip(IND_DECL), feed(SIND_NAME),
               skip(EIND_NAME), feedName(VAR_XML_NAME), feed(EIND_ATTRS),
               feedName(VAR_XML_ATTRS), feed(EIND_TAG)),
        Action(ACTION_STREAM, feed(EIND_TAG)),
    ),
Пример #11
0
from ally.core.impl.processor.render import json, xml
from ally.indexing.spec.model import Block, Action
from ally.indexing.spec.perform import skip, feed, feedName, setFlag


# --------------------------------------------------------------------
ADJUST_SELF_DISCARD = 'self discard'  # The action name to get the self reference discard.

# --------------------------------------------------------------------
# Provides the HTTP block definitions.
BLOCKS_PATCH = {}

BLOCKS_PATCH[json.PATTERN_JSON_START_ADJUST % ADJUST_SELF_DISCARD] = \
Block(
      Action(json.ACTION_JSON_ADJUST,
             feed(json.IND_DECL),
             setFlag(json.FLAG_COMMA)),
      Action(ACTION_STREAM,
             feed(json.IND_DECL),
             setFlag(json.FLAG_COMMA)),
      )
BLOCKS_PATCH[json.PATTERN_JSON_END_ADJUST % ADJUST_SELF_DISCARD] = json.BLOCKS_JSON[json.PATTERN_JSON_END_ADJUST % ADJUST_STANDARD]


BLOCKS_PATCH[xml.PATTERN_XML_START_ADJUST % ADJUST_SELF_DISCARD] = \
Block(
      Action(xml.ACTION_XML_ADJUST,
             skip(xml.IND_DECL),
             feed(xml.SIND_NAME), skip(xml.EIND_NAME), feedName(xml.VAR_XML_NAME),
             skip(xml.EIND_ATTRS), feedName(xml.VAR_XML_ATTRS),
             feed(xml.EIND_TAG)),