Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Move some attr utils from col_info to attr_utils
Browse files Browse the repository at this point in the history
Move convert_none_to_empty_dict() and convert_single_to_list()
from models/collection_info.py to utils/attr_utils.py so
other modules can use them.
  • Loading branch information
alikins committed May 28, 2019
1 parent d20c0ac commit 077817e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
27 changes: 3 additions & 24 deletions ansible_galaxy/models/collection_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import six

from ansible_galaxy.data import spdx_licenses
from ansible_galaxy.utils import attr_utils

log = logging.getLogger(__name__)

Expand All @@ -29,35 +30,13 @@
MATCH_LEADING_NUMBER_REGEXP = re.compile(r'^[0-9]')


def convert_none_to_empty_dict(val):
''' if val is None, return an empty dict'''

# if val is not a dict or val 'None' return val
# and let the validators raise errors later
if val is None:
return {}
return val


def convert_single_to_list(val):
'''If a single object is provided, replace with a list containing only that object'''

if val is None:
return []

if not isinstance(val, list):
return [val]

return val


@attr.s(frozen=True)
class CollectionInfo(object):
namespace = attr.ib(default=None)
name = attr.ib(default=None)
version = attr.ib(default=None)
# license = attr.ib(default=None)
license = attr.ib(factory=list, converter=convert_single_to_list)
license = attr.ib(factory=list, converter=attr_utils.convert_single_to_list)
description = attr.ib(default=None)

repository = attr.ib(default=None)
Expand All @@ -76,7 +55,7 @@ class CollectionInfo(object):

# Note galaxy.yml 'dependencies' field is what mazer and ansible
# consider 'requirements'. ie, install time requirements.
dependencies = attr.ib(factory=dict, converter=convert_none_to_empty_dict)
dependencies = attr.ib(factory=dict, converter=attr_utils.convert_none_to_empty_dict)

def __attrs_post_init__(self):
# validate with have something in license or license_file
Expand Down
22 changes: 22 additions & 0 deletions ansible_galaxy/utils/attr_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
log = logging.getLogger(__name__)


def convert_none_to_empty_dict(val):
''' if val is None, return an empty dict'''

# if val is not a dict or val 'None' return val
# and let the validators raise errors later
if val is None:
return {}
return val


def convert_single_to_list(val):
'''If a single object is provided, replace with a list containing only that object'''

if val is None:
return []

if not isinstance(val, list):
return [val]

return val


def is_attr(obj):
if isinstance(obj, six.class_types):
try:
Expand Down

0 comments on commit 077817e

Please sign in to comment.