[arch-commits] Commit in vim-ansible/trunk (PKGBUILD generate.py)
David Runge
dvzrv at archlinux.org
Tue Jul 14 08:06:43 UTC 2020
Date: Tuesday, July 14, 2020 @ 08:06:42
Author: dvzrv
Revision: 663613
upgpkg: vim-ansible 3.0-1: Upgrading to 3.0.
The generate.py changes have been upstreamed \o/.
Modified:
vim-ansible/trunk/PKGBUILD
Deleted:
vim-ansible/trunk/generate.py
-------------+
PKGBUILD | 13 +-
generate.py | 276 ----------------------------------------------------------
2 files changed, 5 insertions(+), 284 deletions(-)
Modified: PKGBUILD
===================================================================
--- PKGBUILD 2020-07-14 08:05:44 UTC (rev 663612)
+++ PKGBUILD 2020-07-14 08:06:42 UTC (rev 663613)
@@ -2,7 +2,7 @@
_name=ansible-vim
pkgname=vim-ansible
-pkgver=2.2
+pkgver=3.0
pkgrel=1
pkgdesc="A vim plugin for syntax highlighting Ansible's common filetypes "
arch=('any')
@@ -12,12 +12,9 @@
depends=('python-jinja' 'vim')
makedepends=('ansible' 'python')
optdepends=('vim-ultisnips: for ansible snippets')
-source=("$pkgname-$pkgver.tar.gz::https://github.com/pearofducks/${_name}/archive/${pkgver}.tar.gz"
- "generate.py")
-sha512sums=('73c9147c713b3eb2965f015c76c7c0495bf10518159ea69c16ee65cade2b670b59581b5a335f8e68bc91ff95e9238dd73228963aaa7a8f7d400904a973259f9c'
- 'e24f8c4cebb0a5f1f9e9bc0b005af1a957d1a986be8746039ca8f73d8b7c4c93ae352822e709c96f71b7726d1a6ca91be14f5553ef0153db9ad23a01b7a285a8')
-b2sums=('98b673265ee4274c285d49ea11d17d4055bbac15d8a0726123c1b75f473e55dc112a5a0bac5a08725a2fc2a6811e33bd8d2cfb2bcea9cd8d1088ef7eba87b6dd'
- '0938ca439158af536ca04544c2015214b2d33d73dc36c93c0e048704bf71d6b0abc11f91f2c487a828bffe12b9c293587a027ee8883b95a2e411422f33ca4f74')
+source=("$pkgname-$pkgver.tar.gz::https://github.com/pearofducks/${_name}/archive/${pkgver}.tar.gz")
+sha512sums=('77890d945926b7042259dcdd031ed83447512e234e621622afa377ed0d6b183cc936a63d48e406e69a6cfa7e2ce0d3f6e2b0d68c6b5858d76c67861c65df0573')
+b2sums=('9a294ff99cb3638d69178c5fcc155692ed56179cb576e73688ad2c9b9bdabc04dd603d531331c83d674d3c3371f984ab8dea9718766bcb4df2c6332a13c8b468')
prepare() {
mv -v "${_name}-$pkgver" "$pkgname-$pkgver"
@@ -32,7 +29,7 @@
cd "$pkgname-$pkgver"
# generating a reproducible UltiSnips snippet file:
# https://github.com/pearofducks/ansible-vim/pull/105
- python ../generate.py --style=dictionary
+ python UltiSnips/generate.py --style=dictionary
}
package() {
Deleted: generate.py
===================================================================
--- generate.py 2020-07-14 08:05:44 UTC (rev 663612)
+++ generate.py 2020-07-14 08:06:42 UTC (rev 663613)
@@ -1,276 +0,0 @@
-#!/usr/bin/env python3
-import argparse
-import os
-import os.path
-import ansible.modules
-from ansible.utils.plugin_docs import get_docstring
-from ansible.plugins.loader import fragment_loader
-from typing import Any, List
-
-
-OUTPUT_FILENAME = "ansible.snippets"
-OUTPUT_STYLE = ["multiline", "dictionary"]
-HEADER = [
- "# NOTE: This file is auto-generated. Modifications may be overwritten.",
- "priority -50",
-]
-MAX_DESCRIPTION_LENGTH = 512
-
-
-def get_files() -> List[str]:
- """Return the sorted list of all module files that ansible provides
-
- Returns
- -------
- List[str]
- A list of strings representing the Python module files provided by
- Ansible
- """
-
- file_names: List[str] = []
- for root, dirs, files in os.walk(os.path.dirname(ansible.modules.__file__)):
- file_names += [
- f"{root}/{file_name}"
- for file_name in files
- if file_name.endswith(".py") and not file_name.startswith("__init__")
- ]
-
- return sorted(file_names)
-
-
-def get_docstrings(file_names: List[str]) -> List[Any]:
- """Extract and return a list of docstring information from a list of files
-
- Parameters
- ----------
- file_names: List[str]
- A list of strings representing file names
-
- Returns
- -------
- List[Any]
- A list of AnsibleMapping objects, representing docstring information
- (in dict form), excluding those that are marked as deprecated.
-
- """
-
- docstrings: List[Any] = []
- docstrings += [
- get_docstring(file_name, fragment_loader)[0] for file_name in file_names
- ]
- return [
- docstring
- for docstring in docstrings
- if docstring and not docstring.get("deprecated")
- ]
-
-
-def escape_strings(escapist: str) -> str:
- """Escapes strings as required for ultisnips snippets
-
- Escapes instances of \\, `, {, }, $
-
- Parameters
- ----------
- escapist: str
- A string to apply string replacement on
-
- Returns
- -------
- str
- The input string with all defined replacements applied
- """
-
- return (
- escapist.replace("\\", "\\\\")
- .replace("`", "\`")
- .replace("{", "\{")
- .replace("}", "\}")
- .replace("$", "\$")
- .replace("\"", "'")
- )
-
-
-def option_data_to_snippet_completion(option_data: Any) -> str:
- """Convert Ansible option info into a string used for ultisnip completion
-
- Converts data about an Ansible module option (retrieved from an
- AnsibleMapping object) into a formatted string that can be used within an
- UltiSnip macro.
-
- Parameters
- ----------
- option_data: Any
- The option parameters
-
- Returns
- -------
- str
- A string representing one formatted option parameter
- """
-
- # join descriptions that are provided as lists and crop them
- description = escape_strings(
- "".join(option_data.get("description"))[0:MAX_DESCRIPTION_LENGTH]
- )
- default = option_data.get("default")
- choices = option_data.get("choices")
- option_type = option_data.get("type")
-
- # if the option is of type "bool" return "yes" or "no"
- if option_type and "bool" in option_type:
- if default in [True, "True", "true", "yes"]:
- return "true"
- if default in [False, "False", "false", "no"]:
- return "false"
-
- # if there is no default and no choices, return the description
- if not choices and default is None:
- return f"# {description}"
-
- # if there is a default but no choices return the default as string
- if default is not None and not choices:
- if len(str(default)) == 0:
- return '""'
- else:
- if isinstance(default, str) and "\\" in default:
- return f'"{escape_strings(str(default))}"'
- elif isinstance(default, str):
- return escape_strings(str(default))
- else:
- return default
-
- # if there is a default and there are choices return the list of choices
- # with the default prefixed with #
- if default is not None and choices:
- if isinstance(default, list):
- # prefix default choice(s)
- prefixed_choices = [
- f"#{choice}" if choice in default else f"{choice}" for choice in choices
- ]
- return str(prefixed_choices)
- else:
- # prefix default choice
- prefixed_choices = [
- f"#{choice}" if str(choice) == str(default) else f"{choice}"
- for choice in choices
- ]
- return "|".join(prefixed_choices)
-
- # if there are choices but no default, return the choices as pipe separated
- # list
- if choices and default is None:
- return "|".join([str(choice) for choice in choices])
-
- # as fallback return empty string
- return ""
-
-
-def module_options_to_snippet_options(module_options: Any) -> List[str]:
- """Convert module options to UltiSnips snippet options
-
- Parameters
- ----------
- module_options: Any
- The "options" attribute of an AnsibleMapping object
-
- Returns
- -------
- List[str]
- A list of strings representing converted options
- """
-
- options: List[str] = []
- delimiter = ": " if args.style == "dictionary" else "="
-
- if not module_options:
- return options
-
- # order by option name
- module_options = sorted(module_options.items(), key=lambda x: x[0])
- # order by "required" attribute
- module_options = sorted(
- module_options, key=lambda x: x[1].get("required", False), reverse=True
- )
-
- # insert an empty option above the list of non-required options
- for index, (_, option) in enumerate(module_options):
- if not option.get("required"):
- if index != 0:
- module_options.insert(index, (None, None))
- break
-
- for index, (name, option_data) in enumerate(module_options, start=1):
- # insert a line to seperate required/non-required options
- if not name and not option_data:
- options += [""]
- else:
- # the free_form option in some modules are special
- if name == "free_form":
- options += [
- f"\t${{{index}:{name}{delimiter}{option_data_to_snippet_completion(option_data)}}}"
- ]
- else:
- options += [
- f"\t{name}{delimiter}${{{index}:{option_data_to_snippet_completion(option_data)}}}"
- ]
-
- return options
-
-
-def convert_docstring_to_snippet(docstring: Any) -> List[str]:
- """Converts data about an Ansible module into an UltiSnips snippet string
-
- Parameters
- ----------
- docstring: Any
- An AnsibleMapping object representing the docstring for an Ansible
- module
-
- Returns
- -------
- str
- A string representing an ultisnips compatible snippet of an Ansible
- module
- """
-
- snippet: List[str] = []
- snippet_options = "b"
- module_name = docstring["module"]
- module_short_description = docstring["short_description"]
-
- snippet += [f'snippet {module_name} "{escape_strings(module_short_description)}" {snippet_options}']
- if args.style == "dictionary":
- snippet += [f"{module_name}:"]
- else:
- snippet += [f"{module_name}:{' >' if docstring.get('options') else ''}"]
- module_options = module_options_to_snippet_options(docstring.get("options"))
- snippet += module_options
- snippet += ["endsnippet"]
-
- return snippet
-
-
-if __name__ == "__main__":
-
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "--output",
- help=f"Output filename (default: {OUTPUT_FILENAME})",
- default=OUTPUT_FILENAME,
- )
- parser.add_argument(
- "--style",
- help=f"YAML format used for snippets (default: {OUTPUT_STYLE[0]})",
- choices=OUTPUT_STYLE,
- default=OUTPUT_STYLE[0],
- )
- args = parser.parse_args()
-
- docstrings = get_docstrings(get_files())
- with open(args.output, "w") as f:
- f.writelines(f"{header}\n" for header in HEADER)
- for docstring in docstrings:
- f.writelines(
- f"{line}\n" for line in convert_docstring_to_snippet(docstring)
- )
More information about the arch-commits
mailing list