Exemple #1
0
    def yaml_set_comment_before_after_key(self,
                                          key,
                                          before=None,
                                          indent=0,
                                          after=None,
                                          after_indent=None):
        # type: (Any, Any, Any, Any, Any) -> None
        """
        expects comment (before/after) to be without `#` and possible have multiple lines
        """
        from ruamel.yaml.error import CommentMark
        from ruamel.yaml.tokens import CommentToken

        def comment_token(s, mark):
            # type: (Any) -> Any
            # handle empty lines as having no comment
            return CommentToken(('# ' if s else '') + s + '\n', mark, None)

        if after_indent is None:
            after_indent = indent + 2
        if before and before[-1] == '\n':
            before = before[:-1]  # strip final newline if there
        if after and after[-1] == '\n':
            after = after[:-1]  # strip final newline if there
        start_mark = CommentMark(indent)
        c = self.ca.items.setdefault(key, [None, [], None, None])
        if before:
            for com in before.split('\n'):
                c[1].append(comment_token(com, start_mark))
        if after:
            start_mark = CommentMark(after_indent)
            if c[3] is None:
                c[3] = []
            for com in after.split('\n'):
                c[3].append(comment_token(com, start_mark))
Exemple #2
0
def fix(repo_root):
    # repo_root = "/home/tamal/go/src/stash.appscode.dev/stash"
    directory = os.path.join(repo_root, ".github", "workflows")

    for filename in os.listdir(directory):
        if not filename.endswith(".yml"):
            continue

        with open(os.path.join(directory, filename), 'r+') as f:
            yaml = ruamel.yaml.YAML(typ='rt')
            yaml.preserve_quotes = True
            yaml.width = 4096
            data = yaml.load(f)
            for job in data['jobs']:
                for i, step in enumerate(data['jobs'][job]['steps']):
                    if 'name' in data['jobs'][job]['steps'][i].keys() and data['jobs'][job]['steps'][i]['name'] == 'Available platforms':
                        data['jobs'][job]['steps'].pop(i)
                        break

                for i, step in enumerate(data['jobs'][job]['steps']):
                    if 'id' in step.keys() and step['id'] == 'buildx':
                        data['jobs'][job]['steps'].pop(i)

                        # add blank line before
                        data['jobs'][job]['steps'][i].ca.comment = [
                            None, [CT('\n', CommentMark(0), None)]]

                        e1 = CM({
                            'name': 'Set up Docker Buildx',
                            'uses': 'docker/setup-buildx-action@v1'
                        })
                        # e1.yaml_set_start_comment('\n')
                        e1.ca.comment = [
                            None, [CT('\n', CommentMark(0), None)]]
                        data['jobs'][job]['steps'].insert(i, e1)

                        e2 = CM({
                            'name': 'Available platforms',
                            'run': 'echo ${{steps.qemu.outputs.platforms}}'
                        })
                        e2.ca.comment = [
                            None, [CT('\n', CommentMark(0), None)]]
                        data['jobs'][job]['steps'].insert(i, e2)

                        data['jobs'][job]['steps'].insert(i, CM({
                            'name': 'Set up QEMU',
                            'id': 'qemu',
                            'uses': 'docker/setup-qemu-action@v1'
                        }))

                        break
            f.seek(0)
            f.truncate(0)
            yaml.indent(mapping=2, sequence=4, offset=2)
            yaml.dump(data, f)
Exemple #3
0
def add_ending_newline(obj):
    last_key = list(obj.keys())[-1]
    if type(obj[last_key]) == list:
        obj[last_key] = CommentedSeq(obj[last_key])
    if hasattr(obj[last_key], "ca"):
        ct = CommentToken("\n\n", CommentMark(0), None)
        index = len(obj[last_key]) - 1
        obj[last_key].ca.items[index] = [ct, None, None, None]
    else:
        ct = CommentToken("\n\n", CommentMark(0), None)
        obj.ca.items[last_key] = [None, None, ct, None]
Exemple #4
0
    def add_jinja_var(self, name: str, value: Any):
        if self._yaml.ca.comment:
            if self._yaml.ca.comment[1]:
                self._yaml.ca.comment[1][-1].value = re.sub(
                    r"[\n]+$", "\n", self._yaml.ca.comment[1][-1].value,
                    re.MULTILINE)
        else:
            self._yaml.ca.comment = [None, []]

        self._yaml.ca.comment[1].append(
            CommentToken(
                f'#% set {name} = "{value}" %}}',
                start_mark=CommentMark(0),
                end_mark=CommentMark(0),
            ))
Exemple #5
0
def yamkix_add_eol_comment(self, comment, key=NoComment, column=None):
    """Custom add_eol_comment function.

    We need to be able to tune the number of spaces between
    the content and the comment for CommentedSeqs and CommentedMaps
    see https://stackoverflow.com/q/60915926
    """
    # pylint: disable=protected-access
    org_col = column
    if column is None:
        try:
            column = self._yaml_get_column(key)
        except AttributeError:
            column = 0
    if comment[0] != "#":
        comment = "# " + comment
    if (
            org_col != 0
    ):  # only do this if the specified column is not the beginning of the line
        if comment[0] == "#":
            additional_spaces = 1 if org_col is None else org_col - 1
            comment = " " * additional_spaces + comment
            column = 0
    start_mark = CommentMark(column)
    comment_as_list = [CommentToken(comment, start_mark, None), None]
    self._yaml_add_eol_comment(comment_as_list, key=key)
Exemple #6
0
    def add_jinja_var(self, name: str, value: Any, quote: bool = True):
        if quote:
            value = f'"{value}"'
        if self._yaml.ca.comment:
            if self._yaml.ca.comment[1]:
                self._yaml.ca.comment[1][-1].value = re.sub(
                    r"[\n]+$", "\n", self._yaml.ca.comment[1][-1].value,
                    re.MULTILINE)
        else:
            self._yaml.ca.comment = [None, []]

        self._yaml.ca.comment[1].append(
            CommentToken(
                f"#% set {name} = {value} %}}",
                start_mark=CommentMark(0),
                end_mark=CommentMark(0),
            ))
Exemple #7
0
def replace_colorscheme(colors_path: str, config_path: str, colorscheme: str,
                        base16_vim: bool):
    with open(expanduser(config_path), 'r') as config_file,\
            open(expanduser(colors_path), 'r') as color_file,\
            tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        config_yaml = yaml.load(config_file)
        colors_yaml = yaml.load(color_file)

        try:
            # NOTE: update method doesn't read the first comment
            config_yaml['colors'].update(colors_yaml['colors'])
        except KeyError:
            config_yaml['colors'] = colors_yaml['colors']

        new_comment_token = CommentToken(
            f'# COLORSCHEME: {colorscheme}\n',
            CommentMark(2),
            None,
        )

        if has_comment_token(config_yaml['colors'].ca.comment):
            # removing all comments for colors in config_file
            while len(config_yaml['colors'].ca.comment[1]) > 0:
                config_yaml['colors'].ca.comment[1].pop()

            # adding current colorscheme name in comment
            config_yaml['colors'].ca.comment[1].append(new_comment_token)
        else:
            # adding current colorscheme name in comment
            config_yaml['colors'].ca.comment = [None, [new_comment_token]]

        # adding all comments for colors from colors_file
        if has_comment_token(colors_yaml['colors'].ca.comment):
            config_yaml['colors'].ca.comment[1].extend(
                colors_yaml['colors'].ca.comment[1])

        # NOTE: not directly writing to config_file as it causes
        # multiple reload during write
        tmp_file_path = tmp_file.name
        yaml.dump(config_yaml, tmp_file)

    copyfile(tmp_file_path, expanduser(config_path))
    unlink(tmp_file_path)

    if base16_vim:
        vimrc_background_path = join('~', '.vimrc_background')
        with open(expanduser(vimrc_background_path),
                  'w') as vimrc_background_file:
            colorscheme_no_extension = splitext(colorscheme)[0]
            vimrc_background_content = generate_vimrc_background(
                colorscheme_no_extension)
            vimrc_background_file.write(vimrc_background_content)
 def yaml_set_start_comment(self, comment, indent=0):
     """overwrites any preceding comment lines on an object
     expects comment to be without `#` and possible have multiple lines
     """
     from .error import CommentMark
     from .tokens import CommentToken
     pre_comments = self._yaml_get_pre_comment()
     if comment[-1] == '\n':
         comment = comment[:-1]  # strip final newline if there
     start_mark = CommentMark(indent)
     for com in comment.split('\n'):
         pre_comments.append(
             CommentToken('# ' + com + '\n', start_mark, None))
Exemple #9
0
def yaml_comments(text: str, stream_mark: bool = False) -> list:
    from ruamel.yaml.error import CommentMark, StreamMark
    from ruamel.yaml.tokens import CommentToken

    if text[-1] == "\n":
        text = text[:-1]

    if stream_mark:
        mark = StreamMark(name="", index=0, line=0, column=0)
    else:
        mark = CommentMark(0)
    return [
        CommentToken(f"# {s}\n" if s else "\n", mark, None)
        for s in text.split("\n")
    ]
Exemple #10
0
    def yaml_set_start_comment(self, comment, indent=0):
        # type: (Any, Any) -> None
        """overwrites any preceding comment lines on an object
        expects comment to be without `#` and possible have multiple lines
        """
        from .error import CommentMark
        from .tokens import CommentToken

        pre_comments = self._yaml_clear_pre_comment()  # type: ignore
        if comment[-1] == '\n':
            comment = comment[:-1]  # strip final newline if there
        start_mark = CommentMark(indent)
        for com in comment.split('\n'):
            c = com.strip()
            if len(c) > 0 and c[0] != '#':
                com = '# ' + com
            pre_comments.append(CommentToken(com + '\n', start_mark))
 def yaml_add_eol_comment(self, comment, key=NoComment, column=None):
     """
     there is a problem as eol comments should start with ' #'
     (but at the beginning of the line the space doesn't have to be before
     the #. The column index is for the # mark
     """
     from .tokens import CommentToken
     from .error import CommentMark
     if column is None:
         column = self._yaml_get_column(key)
     if comment[0] != '#':
         comment = '# ' + comment
     if column is None:
         if comment[0] == '#':
             comment = ' ' + comment
             column = 0
     start_mark = CommentMark(column)
     ct = [CommentToken(comment, start_mark, None), None]
     self._yaml_add_eol_comment(ct, key=key)
Exemple #12
0
def replace_colorscheme(
    colors_path: str,
    config_path: str,
    colorscheme: str,
    base16_vim: bool,
    debug: bool,
) -> None:
    try:
        with open(expanduser(config_path), 'r') as config_file:
            config_yaml = yaml.load(config_file)
    except OSError:
        raise RuntimeError(f'Could not find a valid alacritty config file: {config_path}')

    try:
        with open(expanduser(colors_path), 'r') as color_file:
            colors_yaml = yaml.load(color_file)
    except OSError:
        raise RuntimeError(f'Could not find a valid alacritty colorscheme file: {colors_path}')

    try:
        # NOTE: update method doesn't read the first comment
        config_yaml['colors'].update(colors_yaml['colors'])
    except KeyError:
        config_yaml['colors'] = colors_yaml['colors']
    except TypeError:
        if not config_yaml:
            config_yaml = {'colors': colors_yaml['colors']}
        else:
            raise

    new_comment_token = CommentToken(
        f'# COLORSCHEME: {colorscheme}\n',
        CommentMark(2),
        None,
    )

    if _has_comment_token(config_yaml['colors'].ca.comment):
        # removing all comments for colors in config_file
        while len(config_yaml['colors'].ca.comment[1]) > 0:
            config_yaml['colors'].ca.comment[1].pop()

        # adding current colorscheme name in comment
        config_yaml['colors'].ca.comment[1].append(new_comment_token)
    else:
        # adding current colorscheme name in comment
        config_yaml['colors'].ca.comment = [None, [new_comment_token]]

    # adding all comments for colors from colors_file
    if _has_comment_token(colors_yaml['colors'].ca.comment):
        config_yaml['colors'].ca.comment[1].extend(
            colors_yaml['colors'].ca.comment[1]
        )

    try:
        with NamedTemporaryFile(delete=False) as tmp_file:
            # NOTE: not directly writing to config_file as it causes
            # multiple reload during write
            tmp_file_path = tmp_file.name
            yaml.dump(config_yaml, tmp_file)
            copyfile(tmp_file_path, expanduser(config_path))
        unlink(tmp_file_path)
    except OSError:
        raise RuntimeError(f'Could not modify alacritty config file: {config_path}')

    if debug:
        print(f'Applied colorscheme: {colorscheme}')

    if base16_vim:
        vimrc_background_path = join('~', '.vimrc_background')
        try:
            with open(expanduser(vimrc_background_path), 'w') as vimrc_background_file:
                colorscheme_no_extension = splitext(colorscheme)[0]
                vimrc_background_content = template_vimrc_background(
                    colorscheme_no_extension
                )
                vimrc_background_file.write(vimrc_background_content)
        except OSError:
            raise RuntimeError(f'Could not save file: {vimrc_background_path}')

        reload_neovim_sessions(vimrc_background_path)