Skip to content

Commit

Permalink
format with darken
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Nov 24, 2021
1 parent 277b86a commit 1ab87a1
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 186 deletions.
3 changes: 1 addition & 2 deletions cloudinit/config/cc_apk_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from cloudinit import temp_utils
from cloudinit import templater
from cloudinit import util
from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit.settings import PER_INSTANCE

LOG = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import pathlib
from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit import gpg
from cloudinit import log as logging
from cloudinit import subp
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_bootcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import os
from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit.settings import PER_ALWAYS
from cloudinit import temp_utils
from cloudinit import subp
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_chef.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from textwrap import dedent

from cloudinit import subp
from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit import templater
from cloudinit import temp_utils
from cloudinit import url_helper
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_resizefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import stat
from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit.settings import PER_ALWAYS
from cloudinit import subp
from cloudinit import util
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_runcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

"""Runcmd: run arbitrary commands at rc.local with output to the console"""

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit.distros import ALL_DISTROS
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from textwrap import dedent

from cloudinit import log as logging
from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit.settings import PER_INSTANCE
from cloudinit.subp import prepend_base_command
from cloudinit import subp
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_ubuntu_advantage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit import log as logging
from cloudinit.settings import PER_INSTANCE
from cloudinit import subp
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_ubuntu_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os
from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit import log as logging
from cloudinit.settings import PER_INSTANCE
from cloudinit import subp
Expand Down
3 changes: 1 addition & 2 deletions cloudinit/config/cc_write_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import os
from textwrap import dedent

from cloudinit.config.schema import (
get_meta_doc, validate_cloudconfig_schema)
from cloudinit.config.schema import get_meta_doc, validate_cloudconfig_schema
from cloudinit import log as logging
from cloudinit.settings import PER_INSTANCE
from cloudinit import util
Expand Down
122 changes: 68 additions & 54 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{property_doc}
{examples}
"""
SCHEMA_PROPERTY_TMPL = '{prefix}**{prop_name}:** ({prop_type}) {description}'
SCHEMA_PROPERTY_TMPL = "{prefix}**{prop_name}:** ({prop_type}) {description}"
SCHEMA_LIST_ITEM_TMPL = (
'{prefix}Each item in **{prop_name}** list supports the following keys:')
SCHEMA_EXAMPLES_HEADER = '\n**Examples**::\n\n'
Expand Down Expand Up @@ -105,9 +105,7 @@ def get_jsonschema_validator():
return (cloudinitValidator, FormatChecker, None)


def validate_cloudconfig_metaschema(
schema: dict,
throw=True):
def validate_cloudconfig_metaschema(schema: dict, throw=True):
"""Validate provided schema meets the metaschema definition. Return strict
Validator and FormatChecker for use in validation
@param schema: schema to validate
Expand All @@ -121,11 +119,12 @@ def validate_cloudconfig_metaschema(
"""

from jsonschema.exceptions import SchemaError

(cloudinitValidator, FormatChecker, _) = get_jsonschema_validator()
try:

# disable bottom-level keys
cloudinitValidator.META_SCHEMA['additionalProperties'] = False
cloudinitValidator.META_SCHEMA["additionalProperties"] = False
cloudinitValidator.check_schema(schema)
except SchemaError as e:
if throw:
Expand All @@ -135,10 +134,8 @@ def validate_cloudconfig_metaschema(


def validate_cloudconfig_schema(
config: dict,
schema: dict,
strict=False,
strict_metaschema=False):
config: dict, schema: dict, strict=False, strict_metaschema=False
):
"""Validate provided config meets the schema definition.
@param config: Dict of cloud configuration settings validated against
Expand All @@ -160,13 +157,14 @@ def validate_cloudconfig_schema(
else validate_cloudconfig_metaschema(schema, throw=False)
)
except ImportError:
logging.debug(
'Ignoring schema validation. jsonschema is not present')
logging.debug("Ignoring schema validation. jsonschema is not present")
return
if err:
logging.warning(
'Meta-schema validation failed, attempting to validate config'
' anyway: %s', err)
"Meta-schema validation failed, attempting to validate config"
" anyway: %s",
err,
)
validator = cloudinitValidator(schema, format_checker=FormatChecker())
errors = ()
for error in sorted(validator.iter_errors(config), key=lambda e: e.path):
Expand Down Expand Up @@ -361,10 +359,11 @@ def _get_property_type(property_dict: dict) -> str:
"""Return a string representing a property type from a given
jsonschema.
"""
property_type = property_dict.get('type')
if property_type is None and property_dict.get('enum'):
property_type = property_dict.get("type")
if property_type is None and property_dict.get("enum"):
property_type = [
str(_YAML_MAP.get(k, k)) for k in property_dict['enum']]
str(_YAML_MAP.get(k, k)) for k in property_dict["enum"]
]
if isinstance(property_type, list):
property_type = '/'.join(property_type)
items = property_dict.get('items', {})
Expand All @@ -375,8 +374,8 @@ def _get_property_type(property_dict: dict) -> str:
sub_property_type += '/'
sub_property_type += '(' + _get_property_type(sub_item) + ')'
if sub_property_type:
return '{0} of {1}'.format(property_type, sub_property_type)
return property_type or 'UNDEFINED'
return "{0} of {1}".format(property_type, sub_property_type)
return property_type or "UNDEFINED"


def _parse_description(description, prefix) -> str:
Expand All @@ -401,7 +400,7 @@ def _parse_description(description, prefix) -> str:
return description


def _get_property_doc(schema: dict, prefix=' ') -> str:
def _get_property_doc(schema: dict, prefix=" ") -> str:
"""Return restructured text describing the supported schema properties."""
new_prefix = prefix + ' '
properties = []
Expand All @@ -410,12 +409,15 @@ def _get_property_doc(schema: dict, prefix=' ') -> str:
description = prop_config.get('description', '')

# Define prop_name and description for SCHEMA_PROPERTY_TMPL
properties.append(SCHEMA_PROPERTY_TMPL.format(
prefix=prefix,
prop_name=prop_key,
description=_parse_description(description, prefix),
prop_type=_get_property_type(prop_config)))
items = prop_config.get('items')
properties.append(
SCHEMA_PROPERTY_TMPL.format(
prefix=prefix,
prop_name=prop_key,
description=_parse_description(description, prefix),
prop_type=_get_property_type(prop_config),
)
)
items = prop_config.get("items")
if items:
if isinstance(items, list):
for item in items:
Expand All @@ -434,7 +436,7 @@ def _get_property_doc(schema: dict, prefix=' ') -> str:

def _get_examples(meta: MetaSchema) -> str:
"""Return restructured text describing the meta examples if present."""
examples = meta.get('examples')
examples = meta.get("examples")
if not examples:
return ''
rst_content = SCHEMA_EXAMPLES_HEADER
Expand All @@ -458,27 +460,31 @@ def get_meta_doc(meta: MetaSchema, schema: dict) -> str:

# Some modules set this to falsy value, exit gracefully
if not meta or not schema:
return ''
return ""
keys = set(meta.keys())
expected = set({
'id',
'title',
'examples',
'frequency',
'distros',
'description',
'name'})
expected = set(
{
"id",
"title",
"examples",
"frequency",
"distros",
"description",
"name",
}
)
if keys != expected:
raise KeyError(
'Missing module metadata key(s) {}'.format(expected - keys))
"Missing module metadata key(s) {}".format(expected - keys)
)

# cast away type annotation
meta_copy = dict(deepcopy(meta))
meta_copy['property_doc'] = _get_property_doc(schema)
meta_copy['examples'] = _get_examples(meta)
meta_copy['distros'] = ', '.join(meta['distros'])
meta_copy["property_doc"] = _get_property_doc(schema)
meta_copy["examples"] = _get_examples(meta)
meta_copy["distros"] = ", ".join(meta["distros"])
# Need an underbar of the same length as the name
meta_copy['title_underbar'] = re.sub(r'.', '-', meta['name'])
meta_copy["title_underbar"] = re.sub(r".", "-", meta["name"])
template = SCHEMA_DOC_TMPL.format(**meta_copy)
return template

Expand All @@ -494,20 +500,24 @@ def error(message):


def load_doc(requested_modules: list) -> str:
'''Load module docstrings
"""Load module docstrings
Docstrings are generated on module load. Reduce, reuse, recycle.
'''
docs = ''
all_modules = list(get_modules().values()) + ['all']
"""
docs = ""
all_modules = list(get_modules().values()) + ["all"]
invalid_docs = set(requested_modules).difference(set(all_modules))
if invalid_docs:
error('Invalid --docs value {}. Must be one of: {}'.format(
list(invalid_docs), ', '.join(all_modules)))
error(
"Invalid --docs value {}. Must be one of: {}".format(
list(invalid_docs), ", ".join(all_modules)
)
)
for mod_name in all_modules:
if 'all' in requested_modules or mod_name in requested_modules:
if "all" in requested_modules or mod_name in requested_modules:
(mod_locs, _) = importer.find_module(
mod_name, ['cloudinit.config'], ['schema'])
mod_name, ["cloudinit.config"], ["schema"]
)
if mod_locs:
mod = importer.import_module(mod_locs[0])
docs += mod.__doc__ or ""
Expand All @@ -517,15 +527,18 @@ def load_doc(requested_modules: list) -> str:
def get_schema() -> dict:
"""Return jsonschema coalesced from all cc_* cloud-config module."""
full_schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'id': 'cloud-config-schema', 'allOf': []}
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "cloud-config-schema",
"allOf": [],
}

for (_, mod_name) in get_modules().items():
(mod_locs, _) = importer.find_module(
mod_name, ['cloudinit.config'], ['schema'])
mod_name, ["cloudinit.config"], ["schema"]
)
if mod_locs:
mod = importer.import_module(mod_locs[0])
full_schema['allOf'].append(mod.schema)
full_schema["allOf"].append(mod.schema)
return full_schema


Expand All @@ -534,10 +547,11 @@ def get_meta() -> dict:
full_meta = dict()
for (_, mod_name) in get_modules().items():
mod_locs, _ = importer.find_module(
mod_name, ['cloudinit.config'], ['meta'])
mod_name, ["cloudinit.config"], ["meta"]
)
if mod_locs:
mod = importer.import_module(mod_locs[0])
full_meta[mod.meta['id']] = mod.meta
full_meta[mod.meta["id"]] = mod.meta
return full_meta


Expand Down
24 changes: 13 additions & 11 deletions cloudinit/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@
# annotations add value for development, but don't break old versions
# pyver: 3.5 -> 3.8
# pylint: disable=E1101
if sys.version_info >= (3, 8) and hasattr(typing, 'TypeDict'):
if sys.version_info >= (3, 8) and hasattr(typing, "TypeDict"):
MetaSchema = typing.TypedDict(
'MetaSchema', {
'name': str,
'id': str,
'title': str,
'description': str,
'distros': typing.List[str],
'examples': typing.List[str],
'frequency': str
})
"MetaSchema",
{
"name": str,
"id": str,
"title": str,
"description": str,
"distros": typing.List[str],
"examples": typing.List[str],
"frequency": str,
},
)
else:
MetaSchema = dict
# pylint: enable=E1101
Expand All @@ -36,7 +38,7 @@ def import_module(module_name):


def find_module(base_name: str, search_paths, required_attrs=None) -> tuple:
'''Finds and imports specified modules'''
"""Finds and imports specified modules"""
if not required_attrs:
required_attrs = []
# NOTE(harlowja): translate the search paths to include the base name.
Expand Down
Loading

0 comments on commit 1ab87a1

Please sign in to comment.