Exemplo n.º 1
0
    def __init__(self, value, comments=None):
        self._original_value = value

        if isinstance(value, Collection):
            self.value_type = 'array'
            self.value = value
        elif isinstance(value, Document):
            self.value_type = 'object'
            self.value = value
        else:
            if is_string_value(value):
                self.value_type = 'string'
                self.value = unwrap_quotes(value)
            else:
                if value == 'true' or value == 'false':
                    self.value_type = 'boolean'
                elif re.compile('^-?\\d*\\.{0,1}\\d+$').match(value):
                    self.value_type = 'number'
                else:
                    self.value_type = 'unknown'

                self.value = value

        self.comments = comments if comments else []
Exemplo n.º 2
0
    def __init__(self, value, comments = None):
        self._original_value = value

        if isinstance(value, Collection):
            self.value_type = 'array'
            self.value = value
        elif isinstance(value, Document):
            self.value_type = 'object'
            self.value = value
        else:
            if is_string_value(value):
                self.value_type = 'string'
                self.value = unwrap_quotes(value)
            else:
                if value == 'true' or value == 'false':
                    self.value_type = 'boolean'
                elif re.compile('^-?\\d*\\.{0,1}\\d+$').match(value):
                    self.value_type = 'number'
                else:
                    self.value_type = 'unknown'

                self.value = value

        self.comments = comments if comments else []
Exemplo n.º 3
0
    def format(self):
        output = ''
        indent = 0
        reader = self._reader
        has_properties = False
        is_last_token_comment = False
        spacer = self._options['spacer']
        newline = self._options['newline']
        remove_comments = self._options['remove_comments']

        while True:
            should_linebreak = False
            result = reader.read()
            is_comment = False

            if not result:
                break

            if is_last_token_comment:
                should_linebreak = True
                is_last_token_comment = False

            if result.type == 'begin_object':
                has_properties = False
                indent += 1
            elif result.type == 'end_object':
                indent -= 1
                if has_properties:
                    should_linebreak = True
            elif result.type == 'property':
                should_linebreak = True
                has_properties = True
            elif result.type == 'new_line_comment':
                is_comment = True
                is_last_token_comment = True
                should_linebreak = True
            elif result.type == 'end_line_comment':
                is_comment = True
                is_last_token_comment = True
            elif result.type == 'new_line_comment_block':
                is_comment = True
                should_linebreak = True
            elif result.type == 'in_line_comment_block':
                is_comment = True

            if is_comment and remove_comments:
                continue


            # new line
            if should_linebreak:
                output = join(newline, strip_trailing(output, spacer), indent * self._options['indent_character'])


            # actual character
            if result.type == 'property' and 'force_property_quotes' in self._options and self._options['force_property_quotes']:
                output += ensure_quotes(result.value, self._options['quote_char'])
            elif result.type == 'value' and 'normalize_strings' in self._options and self._options['normalize_strings'] and is_string_value(result.value):
                output += ensure_quotes(result.value, self._options['quote_char'])
            elif result.type == 'end_line_comment':
                output = strip_trailing(output, spacer)
                output += spacer + result.value
            elif result.type == 'new_line_comment_block':
                output += format_block_comment(result.value, indent * self._options['indent_character']) + newline
            elif result.type == 'in_line_comment_block':
                output = strip_trailing(output, spacer)
                output += spacer + format_block_comment(result.value, indent * self._options['indent_character'])
            else:
                output += result.value


            # suffix
            if result.type == 'property_separator':
                output += spacer
            elif result.type == 'value_separator':
                output += spacer
            elif result.type == 'in_line_comment_block':
                output += spacer

        return output
Exemplo n.º 4
0
    def format(self):
        output = ''
        indent = 0
        reader = self._reader
        has_properties = False
        is_last_token_comment = False
        spacer = self._options['spacer']
        newline = self._options['newline']
        remove_comments = self._options['remove_comments']

        while True:
            should_linebreak = False
            result = reader.read()
            is_comment = False

            if not result:
                break

            if is_last_token_comment:
                should_linebreak = True
                is_last_token_comment = False

            if result.type == 'begin_object':
                has_properties = False
                indent += 1
            elif result.type == 'end_object':
                indent -= 1
                if has_properties:
                    should_linebreak = True
            elif result.type == 'property':
                should_linebreak = True
                has_properties = True
            elif result.type == 'new_line_comment':
                is_comment = True
                is_last_token_comment = True
                should_linebreak = True
            elif result.type == 'end_line_comment':
                is_comment = True
                is_last_token_comment = True
            elif result.type == 'new_line_comment_block':
                is_comment = True
                should_linebreak = True
            elif result.type == 'in_line_comment_block':
                is_comment = True

            if is_comment and remove_comments:
                continue

            # new line
            if should_linebreak:
                output = join(newline, strip_trailing(output, spacer),
                              indent * self._options['indent_character'])

            # actual character
            if result.type == 'property' and 'force_property_quotes' in self._options and self._options[
                    'force_property_quotes']:
                output += ensure_quotes(result.value,
                                        self._options['quote_char'])
            elif result.type == 'value' and 'normalize_strings' in self._options and self._options[
                    'normalize_strings'] and is_string_value(result.value):
                output += ensure_quotes(result.value,
                                        self._options['quote_char'])
            elif result.type == 'end_line_comment':
                output = strip_trailing(output, spacer)
                output += spacer + result.value
            elif result.type == 'new_line_comment_block':
                output += format_block_comment(
                    result.value,
                    indent * self._options['indent_character']) + newline
            elif result.type == 'in_line_comment_block':
                output = strip_trailing(output, spacer)
                output += spacer + format_block_comment(
                    result.value, indent * self._options['indent_character'])
            else:
                output += result.value

            # suffix
            if result.type == 'property_separator':
                output += spacer
            elif result.type == 'value_separator':
                output += spacer
            elif result.type == 'in_line_comment_block':
                output += spacer

        return output