示例#1
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, ))
    }
示例#2
0
def createXMLBlockForIndexed(name,
                             *actions,
                             injectAttributes=None,
                             captureAttributes=None):
    '''
    Create a new XML 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: createXMLAttributesActions
    '''
    actions = list(actions)
    actions.extend(BLOCK_XML.actions)
    actions.extend(
        createXMLAttributesActions(injectAttributes, captureAttributes))
    actions.append(
        Action(
            ACTION_INJECT,
            skip(SIND_NAME),
            push(VAR_XML_NAME, EIND_NAME),
            skip(SIND_ATTRS),
            push(VAR_XML_ATTRS, EIND_ATTRS),
            skip(EIND_CLOSE_TAG),
            feedIndexed(actions=(ACTION_XML_ADJUST, )),
            pop(VAR_XML_NAME),
            pop(VAR_XML_ATTRS),
        ))
    return {PATTERN_XML_BLOCK % name: Block(*actions)}
示例#3
0
    def _obtainBlock(self, id):
        '''
        Obtains the block.
        '''
        block = self._blocks.get(id)
        if block is not None: return block

        oblock = self.requesterGetJSON.request(self.uri % id)
        if oblock is None: block = None
        else:
            uri = oblock['BlockAction']['href']
            oactions = self.requesterGetJSON.request(uri)

            actions = []
            if oactions is not None:
                for oaction in oactions['ActionList']:
                    actions.append(self._obtainAction(oaction['href']))
            block = Block(*actions, keys=oblock.get('Keys'))

        self._blocks[id] = block
        return block
示例#4
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, ))
    }
示例#5
0
           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)
BLOCK_JSON_UNNAMED = Block(Action(ACTION_NAME, feedKey(KEY_NAME), final=False),
                           *ACTIONS_BLOCK_JSON,
                           keys=(KEY_NAME, ))

# Provides the JSON standard block definitions.
BLOCKS_JSON = {
    NAME_JSON_START_ADJUST:
    Block(
        Action(
            ACTION_JSON_ADJUST, feed(SIND_ATTRS),
            setFlagIfBefore(EIND_ATTRS, FLAG_COMMA_ATTR),
            setFlagIfNotBefore(EIND_ATTRS, 'comma attribute required after'),
            feed(EIND_ATTRS), feedValue(',', FLAG_COMMA_ATTR),
示例#6
0
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)),
    ),
    PATTERN_XML_BLOCK % NAME_BLOCK: