Ejemplo n.º 1
0
    def inject_assignment(self,
                          name,
                          value,
                          extra_trailing_newline=False,
                          convert_value=True,
                          line_index=0):
        '''
        Injects a variable assignment, name = value, at line_index.

        Generated variables get one newline before. If you want to pass in raw
        source (instead of a value to be converted into source by a formatter),
        use convert_value=False.

        If extra_trailing_newline==True, one additional newline will be added
        after the variable declaration, so that there's a full blank line
        between it and the next line of source code.

        line_index is relative to the node body. If line_index is not provided,
        the variable will be inserted at node.body_start_line.
        '''

        indentation = self.node.inner_indentation
        if convert_value:
            value = format_value(value, starting_indentation=indentation)

        generated_source = '\n' + indentation + name + ' = ' + value + '\n'

        if extra_trailing_newline:
            generated_source += '\n'

        return self.inject_at_body_line(line_index, generated_source)
Ejemplo n.º 2
0
    def inject_assignment(self,
            name,
            value,
            extra_trailing_newline=False,
            convert_value=True,
            line_index=0):
        '''
        Injects a variable assignment, name = value, at line_index.

        Generated variables get one newline before. If you want to pass in raw
        source (instead of a value to be converted into source by a formatter),
        use convert_value=False.

        If extra_trailing_newline==True, one additional newline will be added
        after the variable declaration, so that there's a full blank line
        between it and the next line of source code.

        line_index is relative to the node body. If line_index is not provided,
        the variable will be inserted at node.body_start_line.
        '''

        indentation = self.node.inner_indentation
        if convert_value:
            value = format_value(
                value,
                starting_indentation=indentation)

        generated_source = '\n' + indentation + name + ' = ' + value + '\n'

        if extra_trailing_newline:
            generated_source += '\n'

        return self.inject_at_body_line(
            line_index,
            generated_source)
Ejemplo n.º 3
0
    def value(self, value):
        '''Generate a change that changes the value of the variable to value.
        Value must be an int, string, bool, list, tuple, or dict. Lists, tuples,
        and dicts, must ALSO only contain ints, strings, bools, lists, tuples,
        or dicts.

        To put it another way, .value() takes in a value, converts it to Python
        source, and then overwrites the variable body with that source.'''

        return self.overwrite_body(
            format_value(value,
                         starting_indentation=self.node.outer_indentation,
                         indent_first_line=False))
Ejemplo n.º 4
0
    def value(self, value):
        '''Generate a change that changes the value of the variable to value.
        Value must be an int, string, bool, list, tuple, or dict. Lists, tuples,
        and dicts, must ALSO only contain ints, strings, bools, lists, tuples,
        or dicts.

        To put it another way, .value() takes in a value, converts it to Python
        source, and then overwrites the variable body with that source.'''

        return self.overwrite_body(
            format_value(
                value,
                starting_indentation=self.node.outer_indentation,
                indent_first_line=False))
Ejemplo n.º 5
0
def test_nested():
    assert_equal(format_value((True, (3, 4))), EXPECTED_NESTED)
Ejemplo n.º 6
0
def test_tuple():
    assert_equal(format_value(('foo/bar/baz.quux', True)), EXPECTED_TUPLE)
Ejemplo n.º 7
0
def test_dict():
    assert_equal(format_value({'a':42, 'b':False}), EXPECTED_DICT)
Ejemplo n.º 8
0
def test_literal():
    assert_equal(format_value(3), '3')
    assert_equal(format_value(True), 'True')
    assert_equal(format_value('foo'), "'foo'")
Ejemplo n.º 9
0
def test_list():
    assert_equal(format_value([3, 4, 5]), EXPECTED_LIST)
Ejemplo n.º 10
0
def test_nested():
    assert_equal(format_value((True, (3, 4))), EXPECTED_NESTED)
Ejemplo n.º 11
0
def test_dict():
    assert_equal(format_value({'a': 42, 'b': False}), EXPECTED_DICT)
Ejemplo n.º 12
0
def test_tuple():
    assert_equal(format_value(('foo/bar/baz.quux', True)), EXPECTED_TUPLE)
Ejemplo n.º 13
0
def test_list():
    assert_equal(format_value([3, 4, 5]), EXPECTED_LIST)
Ejemplo n.º 14
0
def test_literal():
    assert_equal(format_value(3), '3')
    assert_equal(format_value(True), 'True')
    assert_equal(format_value('foo'), "'foo'")