示例#1
0
    def _concatOperation_1(self, node):
        result = ""
        assert node.type == "operation" and node.get(
            "operator"
        ) == "ADD", "Can only process concatenation of string literals"

        try:
            first = node.getChildByPosition(0).getChildByTypeAndAttribute(
                "constant", "constantType", "string")
            result += first.get("value")

            second = node.getChildByPosition(1).getFirstChild(True, True)
            if second.type == "operation" and second.get("operator") == "ADD":
                result += self._concatOperation(second)
            else:
                result += second.get("value")

        except NodeAccessException:
            console.warn(
                "Unknown expression as argument to translation method (%s:%s)"
                % (
                    treeutil.getFileFromSyntaxItem(node),
                    node.get("line"),
                ))

        return result
示例#2
0
 def _concatOperation(self, node):
     console = self.context['console']
     result = ""
     reduced_node = reducer.ast_reduce(node)
     if reduced_node.type == 'constant' and reduced_node.get("constantType",'') == "string":
         result = reduced_node.get('value')
     else:
         console.warn("Cannot extract string argument to translation method (%s:%s): %s" % (
             treeutil.getFileFromSyntaxItem(node), node.get("line"), node.toJS(None)))
     return result
 def _concatOperation(self, node):
     console = self.context['console']
     result = ""
     reduced_node = reducer.ast_reduce(node)
     if reduced_node.type == 'constant' and reduced_node.get("constantType",'') == "string":
         result = reduced_node.get('value')
     else:
         console.warn("Cannot extract string argument to translation method (%s:%s): %s" % (
             treeutil.getFileFromSyntaxItem(node), node.get("line"), node.toJS(None)))
     return result
示例#4
0
    def _concatOperation(self, node):
        result = ""
        assert node.type == "operation" and node.get(
            "operator"
        ) == "ADD", "Can only process concatenation of string literals"

        evaluate.evaluate(node)
        if node.evaluated != ():
            result = node.evaluated
        else:
            console.warn(
                "Unknown expression as argument to translation method (%s:%s)"
                % (
                    treeutil.getFileFromSyntaxItem(node),
                    node.get("line"),
                ))

        return result
示例#5
0
    def _concatOperation(self, node):
        result = ""
        console = self.context['console']

        try:
            first = node.getChild("first").getChildByTypeAndAttribute(
                "constant", "constantType", "string")
            result += first.get("value")

            second = node.getChild("second").getFirstChild(True, True)
            if second.type == "operation":
                result += self._concatOperation(second)
            else:
                result += second.get("value")

        except NodeAccessException:
            console.warn(
                "Unknown expression as argument to translation method (%s:%s)"
                % (
                    treeutil.getFileFromSyntaxItem(node),
                    node.get("line"),
                ))

        return result
示例#6
0
    def _addTranslationBlock(self, method, data, node, var):
        entry = {
            "method": method,
            "line": node.get("line"),
            "column": node.get("column")
        }
        console = self.context['console']

        # tr(msgid, args)
        # trn(msgid, msgid_plural, count, args)
        # trc(hint, msgid, args)
        # marktr(msgid)

        if method == "trn" or method == "trc": minArgc = 2
        else: minArgc = 1

        params = node.getChild("params", False)
        if not params or not params.hasChildren():
            raise NameError(
                "Invalid param data for localizable string method at line %s!"
                % node.get("line"))

        if len(params.children) < minArgc:
            raise NameError("Invalid number of parameters %s at line %s" %
                            (len(params.children), node.get("line")))

        strings = []
        for child in params.children:
            if child.type == "commentsBefore":
                continue

            elif child.type == "constant" and child.get(
                    "constantType") == "string":
                strings.append(child.get("value"))

            elif child.type == "operation":
                strings.append(self._concatOperation(child))

            elif len(strings) < minArgc:
                console.warn(
                    "Unknown expression as argument to translation method (%s:%s)"
                    % (
                        treeutil.getFileFromSyntaxItem(child),
                        child.get("line"),
                    ))

            # Ignore remaining (run time) arguments
            if len(strings) == minArgc:
                break

        lenStrings = len(strings)
        if lenStrings > 0:
            if method == "trc":
                entry["hint"] = strings[0]
                if lenStrings > 1 and strings[1]:  # msgid must not be ""
                    entry["id"] = strings[1]
            else:
                if strings[0]:
                    entry["id"] = strings[0]

            if method == "trn" and lenStrings > 1:
                entry["plural"] = strings[1]

        # register the entry only if we have a proper key
        if "id" in entry:
            data.append(entry)

        return
示例#7
0
    def _addTranslationBlock(self, method, data, node, var):
        entry = {
            "method" : method,
            "line"   : node.get("line"),
            "column" : node.get("column")
        }
        console = self.context['console']

        # tr(msgid, args)
        # trn(msgid, msgid_plural, count, args)
        # trc(hint, msgid, args)
        # trnc(hint, msgid, msgid_plural, count, args)
        # marktr(msgid)

        if method == "trnc": minArgc=3
        elif method == "trn" or method == "trc": minArgc=2
        else: minArgc=1

        params = node.getChild("arguments", False)
        if not params or not params.hasChildren():
            raise NameError("Invalid param data for localizable string method at line %s!" % node.get("line"))

        if len(params.children) < minArgc:
            raise NameError("Invalid number of parameters %s at line %s" % (len(params.children), node.get("line")))

        strings = []
        for child in params.children:
            if child.type == "commentsBefore":
                continue

            elif child.type == "constant" and child.get("constantType") == "string":
                strings.append(child.get("value"))

            elif child.type == "operation": # must be "foo" + "bar"
                strings.append(self._concatOperation(child))

            elif len(strings) < minArgc:
                console.warn("Unknown expression as argument to translation method (%s:%s)" % (treeutil.getFileFromSyntaxItem(child), child.get("line"),))

            # Ignore remaining (run time) arguments
            if len(strings) == minArgc:
                break

        lenStrings = len(strings)
        if lenStrings > 0:
            if method in ("trc", "trnc"):
                entry["hint"] = strings[0]
                if lenStrings > 1 and strings[1]:  # msgid must not be ""
                    entry["id"]   = strings[1]
            else:
                if strings[0]:
                    entry["id"] = strings[0]

            if method == "trn" and lenStrings > 1:
                entry["plural"] = strings[1]

            if method == "trnc" and lenStrings > 2:
                entry["plural"] = strings[2]

        # register the entry only if we have a proper key
        if "id" in entry:
            data.append(entry)

        return
示例#8
0
    def _concatOperation(self, node):
        result = ""
        console = self.context['console']

        try:
            first = node.getChild("first").getChildByTypeAndAttribute("constant", "constantType", "string")
            result += first.get("value")

            second = node.getChild("second").getFirstChild(True, True)
            if second.type == "operation":
                result += self._concatOperation(second)
            else:
                result += second.get("value")

        except NodeAccessException:
            console.warn("Unknown expression as argument to translation method (%s:%s)" % (treeutil.getFileFromSyntaxItem(node), node.get("line"),))

        return result
示例#9
0
    def _concatOperation_1(self, node):
        result = ""
        assert node.type=="operation" and node.get("operator")=="ADD", "Can only process concatenation of string literals"

        try:
            first = node.getChildByPosition(0).getChildByTypeAndAttribute("constant", "constantType", "string")
            result += first.get("value")

            second = node.getChildByPosition(1).getFirstChild(True, True)
            if second.type == "operation" and second.get("operator")=="ADD":
                result += self._concatOperation(second)
            else:
                result += second.get("value")

        except NodeAccessException:
            console.warn("Unknown expression as argument to translation method (%s:%s)" % (treeutil.getFileFromSyntaxItem(node), node.get("line"),))

        return result
示例#10
0
    def _concatOperation(self, node):
        result = ""
        assert node.type=="operation" and node.get("operator")=="ADD", "Can only process concatenation of string literals"

        evaluate.evaluate(node)
        if node.evaluated != ():
            result = node.evaluated
        else:
            console.warn("Unknown expression as argument to translation method (%s:%s)" % (treeutil.getFileFromSyntaxItem(node), node.get("line"),))

        return result