Example #1
0
    def setup(self):
        if self._level == 'single':
            if not self._count and not self._from:
                self._level = SIMPLE
            else:
                self._level = SINGLE
        elif self._level == 'multiple':
            self._level = MULTIPLE
        elif self._level == 'any':
            self._level = ANY

        if self._format.constant and self._lang.constant:
            format = self._format.evaluate_as_string(None)
            lang = self._lang.evaluate_as_string(None)
            self._formatter = numbers.formatter(lang, format)
        else:
            self._formatter = None
        return
Example #2
0
    def setup(self):
        if self._level == 'single':
            if not self._count and not self._from:
                self._level = SIMPLE
            else:
                self._level = SINGLE
        elif self._level == 'multiple':
            self._level = MULTIPLE
        elif self._level == 'any':
            self._level = ANY

        if self._format.constant and self._lang.constant:
            format = self._format.evaluate_as_string(None)
            lang = self._lang.evaluate_as_string(None)
            self._formatter = numbers.formatter(lang, format)
        else:
            self._formatter = None
        return
Example #3
0
    def instantiate(self, context):
        context.instruction = self
        context.namespaces = self.namespaces

        formatter = self._formatter
        if not formatter:
            format = self._format
            if format:
                format = format.evaluate_as_string(context)
            if not format:
                format = DEFAULT_FORMAT
            lang = self._lang
            if lang:
                lang = lang.evaluate_as_string(context)
            if not lang:
                lang = DEFAULT_LANG
            formatter = numbers.formatter(lang, format)

        letter_value = self._letter_value.evaluate_as_string(context)
        if self._grouping_separator and self._grouping_size:
            separator = self._grouping_separator.evaluate_as_string(context)
            grouping = int(self._grouping_size.evaluate_as_number(context))
        else:
            separator = grouping = None

        # get the formatted value(s)
        if self._value:
            value = self._value.evaluate_as_number(context)
            # XSLT 1.0, Section 7.7, Paragraph 1
            # ERROR: the number is NaN, infinite or less than 0.5
            # RECOVERY: convert to string and insert into the result tree
            if not value.isfinite() or value < 0.5:
                result = datatypes.string(value)
            else:
                value = int(round(value))
                result = formatter.format(value, letter_value, grouping,
                                          separator)
        else:
            node = context.node
            if self._level == SINGLE:
                value = self._single_value(context, node, self._count,
                                           self._from)
                if value == 0:
                    value = None
                result = formatter.format(value, letter_value, separator,
                                          grouping)
            elif self._level == MULTIPLE:
                values = self._multiple_values(context, node)
                result = formatter.formatmany(values, letter_value, grouping,
                                              separator)
            elif self._level == ANY:
                value = self._any_value(context, node)
                if value == 0:
                    value = None
                result = formatter.format(value, letter_value, grouping,
                                          separator)
            else:
                # 'single' without count or from attributes
                value = 1
                prev = node.xml_preceding_sibling
                node_type = node.xml_type
                node_name = node.xml_name
                while prev:
                    if prev.xml_type == node_type and \
                       prev.xml_name == node_name:
                        value += 1
                    prev = prev.xml_preceding_sibling
                result = formatter.format(value, letter_value, grouping,
                                          separator)
        # add the resulting formatted value(s) to the result tree
        context.text(result)
        return
Example #4
0
    def instantiate(self, context):
        context.instruction = self
        context.namespaces = self.namespaces

        formatter = self._formatter
        if not formatter:
            format = self._format
            if format:
                format = format.evaluate_as_string(context)
            if not format:
                format = DEFAULT_FORMAT
            lang = self._lang
            if lang:
                lang = lang.evaluate_as_string(context)
            if not lang:
                lang = DEFAULT_LANG
            formatter = numbers.formatter(lang, format)

        letter_value = self._letter_value.evaluate_as_string(context)
        if self._grouping_separator and self._grouping_size:
            separator = self._grouping_separator.evaluate_as_string(context)
            grouping = int(self._grouping_size.evaluate_as_number(context))
        else:
            separator = grouping = None

        # get the formatted value(s)
        if self._value:
            value = self._value.evaluate_as_number(context)
            # XSLT 1.0, Section 7.7, Paragraph 1
            # ERROR: the number is NaN, infinite or less than 0.5
            # RECOVERY: convert to string and insert into the result tree
            if not value.isfinite() or value < 0.5:
                result = datatypes.string(value)
            else:
                value = int(round(value))
                result = formatter.format(value, letter_value, grouping,
                                          separator)
        else:
            node = context.node
            if self._level == SINGLE:
                value = self._single_value(context, node, self._count,
                                           self._from)
                if value == 0:
                    value = None
                result = formatter.format(value, letter_value, separator,
                                          grouping)
            elif self._level == MULTIPLE:
                values = self._multiple_values(context, node)
                result = formatter.formatmany(values, letter_value, grouping,
                                              separator)
            elif self._level == ANY:
                value = self._any_value(context, node)
                if value == 0:
                    value = None
                result = formatter.format(value, letter_value, grouping,
                                          separator)
            else:
                # 'single' without count or from attributes
                value = 1
                prev = node.xml_preceding_sibling
                node_type = node.xml_type
                node_name = node.xml_name
                while prev:
                    if prev.xml_type == node_type and \
                       prev.xml_name == node_name:
                        value += 1
                    prev = prev.xml_preceding_sibling
                result = formatter.format(value, letter_value, grouping,
                                          separator)
        # add the resulting formatted value(s) to the result tree
        context.text(result)
        return