forked from containers/podman-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Container and ContainersManager
* Add unit and integration tests * Refactor api.prepare_body to recusively cleanup dictionary * Remove logging setup from PodmanClient() caller will be responsibl. Python logging best practice. Loggers used: podman, podman.images, podman.containers, etc * Cleaned up code using dict.get() with None default * Integration tests now create logging events for Podman service output rather than writing to a file Signed-off-by: Jhon Honce <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,114 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""Utility functions for working with URL's.""" | ||
import collections.abc | ||
import json | ||
from typing import Dict, List, Mapping, Optional, Union, MutableMapping, Any | ||
|
||
|
||
def prepare_filters(filters: Union[str, List[str], Mapping[str, str]]) -> Optional[str]: | ||
"""Returns filters as an URL quoted JSON Dict[str, List[Any]].""" | ||
|
||
if filters is None or len(filters) == 0: | ||
return None | ||
|
||
criteria: Dict[str, List[str]] = dict() | ||
if isinstance(filters, str): | ||
_format_string(filters, criteria) | ||
elif isinstance(filters, collections.abc.Mapping): | ||
_format_dict(filters, criteria) | ||
else: | ||
_format_list(filters, criteria) | ||
|
||
if len(criteria) == 0: | ||
return None | ||
|
||
return json.dumps(criteria, sort_keys=True) | ||
|
||
|
||
def _format_list(filters, criteria): | ||
for item in filters: | ||
if item is None: | ||
continue | ||
|
||
key, value = item.split("=", 1) | ||
if key in criteria: | ||
criteria[key].append(value) | ||
else: | ||
criteria[key] = [value] | ||
|
||
|
||
def _format_dict(filters, criteria): | ||
for key, value in filters.items(): | ||
if value is None: | ||
continue | ||
value = str(value) | ||
|
||
if key in criteria: | ||
criteria[key].append(value) | ||
else: | ||
criteria[key] = [value] | ||
|
||
|
||
def _format_string(filters, criteria): | ||
key, value = filters.split("=", 1) | ||
criteria[key] = [value] | ||
|
||
|
||
def prepare_body(body: MutableMapping[str, Any]) -> str: | ||
"""Returns JSON payload to be uploaded to server. | ||
Notes: | ||
Values of None and empty Iterables are removed, False and zero-values are retained. | ||
""" | ||
if body is None: | ||
return "" | ||
|
||
body = _filter_values(body) | ||
return json.dumps(body, sort_keys=True) | ||
|
||
|
||
def _filter_values(mapping: Mapping[str, Any]) -> Dict[str, Any]: | ||
"""Returns a canonical dictionary with values == None or empty Iterables removed.""" | ||
canonical = dict() | ||
for key, value in mapping.items(): | ||
# quick filter if possible... | ||
if value is None or (isinstance(value, collections.abc.Sized) and len(value) <= 0): | ||
continue | ||
|
||
# depending on type we need details... | ||
if isinstance(value, collections.abc.MutableMapping): | ||
proposal = _filter_values(value) | ||
elif isinstance(value, collections.abc.Set): | ||
# Convert set() to list() to satisfy json encoder | ||
proposal = [i for i in list(value) if i is not None] | ||
elif isinstance(value, collections.abc.Sequence) and not isinstance(value, str): | ||
proposal = [i for i in value if i is not None] | ||
else: | ||
proposal = value | ||
|
||
if proposal not in (None, str(), list(), dict()): | ||
canonical[key] = proposal | ||
|
||
return canonical |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.