Skip to content

Commit

Permalink
Merge branch 'master' of github.com:release-engineering/ubi-config
Browse files Browse the repository at this point in the history
  • Loading branch information
JayZ12138 committed Mar 14, 2019
2 parents 78645f2 + fecce93 commit d2b16c9
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 325 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ubiconfig
=========

A Python library for accessing Universal Base Image configurations
A Python library for accessing Universal Base Image configuration

[![Build Status](https://travis-ci.org/release-engineering/ubi-config.svg?branch=master)](https://travis-ci.org/release-engineering/ubi-config)
[![Coverage Status](https://coveralls.io/repos/github/release-engineering/ubi-config/badge.svg?branch=master)](https://coveralls.io/github/release-engineering/ubi-config?branch=master)
Expand All @@ -22,30 +22,35 @@ pip install ubi-config

Usage Example
-------------

When there is `DEFAULT_UBI_REPO` set, user can load the config by passing the config file
name to `get_loader().load()`

```python
from ubiconfig import get_loader

config = get_loader().load('rhel-8-for-x86_64-appstream')
config = get_loader().load('enterprise-linux-server-x86_64')
# config has been validated and is now a Python object with relevant properties
package_whitelist = config.packages.whitelist
print package_whitelist
print(package_whitelist)
```

Or, get all config files from the repo:

```python

from ubiconfig import get_loader

configs = get_loader().load_all()
# returns a list of UbiConfig objects
```

Or, user can also load the config from local file:

```python
from ubiconfig import get_loader

config = get_loader(local=True).load('/path/to/rhel-8-for-x86_64-appstream.yaml')
config = get_loader(local=True).load('/path/to/enterprise-linux-server-x86_64.yaml')
```

License
Expand Down
5 changes: 3 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@
'github_repo': 'ubi-config',
'github_button': False,
'github_banner': True,
'description': 'A tool for loading UBI Configurations',
'description': 'A library for loading UBI configuration',
'extra_nav_links': {
'API Reference': 'index.html',
'Index': 'genindex.html',
'Source': 'https://github.com/release-engineering/ubi-config',
'PyPI': 'https://pypi.org/project/ubi-config',
}
}

Expand Down
84 changes: 19 additions & 65 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
ubi-config
==========

A tool for loading UBI configurations
A library for loading UBI configuration

.. toctree::
:maxdepth: 2
:caption: Contents:

- `Source <https://github.com/release-engineering/ubi-config>`_
- `Documentation <https://release-engineering.github.io/ubi-config/>`_
- `PyPI <https://pypi.org/project/ubi-config>`_
.. contents::
:local:

Quick Start
-----------

Install the ubi-config from PyPI:
Install ubi-config from PyPI:

::

Expand All @@ -28,25 +23,26 @@ in your environment:

export DEFAULT_UBI_REPO='https://some/url/'

In your python code, simply call the function ``get_loader`` without passing any
argument and call ``load`` on the returned object with the configuration file name
No matter which branch is the config file in, it will load it for you.
In your python code, simply call the function ``get_loader`` without passing
any argument and call ``load`` on the returned object with the configuration
file name. No matter which branch is the config file in, it will load it
for you.

.. code-block:: python
from ubiconfig import get_loader
default_loader = get_loader()
config = default_loader.load("ubi7_config_file")
config = default_loader.load("ubi7_config_file.yaml")
# the return config is an UbiConfig instance, wraps all types of UBI configurations
# the returned config is an UbiConfig instance, wraps all types of UBI configuration
modules = config.modules
module_name = modules[0].name
print(module_name)
# the returned Load object can be reused to load other configuration file in the
# same repo
config = default_loader.load("ubi8_config_file")
config = default_loader.load("ubi8_config_file.yaml")
content_sets = config.content_sets
More Use Cases
Expand All @@ -63,69 +59,27 @@ Except the above usage, there are some other use cases:
configs = loader.load_all()
# returns a list of UbiConfig objects
2. Load configuration files from a local repo by setting ``local`` to ``True``

.. code-block:: python
from ubiconfig import get_loader
local_loader = get_loader(local=True)
config = local_loader.load("full/path/to/local_ubi7_config")
3. If there's a local repo inlcudes several configuration files, you can also set the repo
path by passing ``local_repo`` to ``get_loader``
2. Load configuration files from a directory by passing a local path:

.. code-block:: python
from ubiconfig import get_loader
local_loader = get_loader(use=True, local_repo='repo/path')
config = local_loader.load('local_ubi7_config')
# or load all config files
configs = local_loader.load_all()
# if there's sub directories include configuration files,
# user can set ``recursive`` to True to load all of them
local_loader = get_loader("/my/config/dir")
config = local_loader.load("path/to/local_ubi7_config.yaml")
# or try load_all with recursive to load all available config files
configs = local_loader.load_all(recursive=True)
You can always reuse the loader
API Reference
-------------
.. currentmodule:: ubiconfig
.. function:: get_loader

Get a Loader instance which is used to load configurations.

The default config file source is as ``DEFAULT_UBI_URL/configfile.yaml``,
when ``local`` is not set, it will check if the ``DEFAULT_UBI_URL`` is set
or not, then creates a requests session and pass it to ``Loader``.

Or if ``local`` is set, then user can pass the local_repo address to
Loader or send full path to ``loader.load()``, for example:

.. code-block:: python
# use default config source
>>> loader = get_loader()
>>> config_ubi7 = loader.load('ubi7')
>>> config)ubi7.content_sets.rpm.input
# loader can be used repeatedly
>>> config_ubi8 = loader.load('ubi8')
# now use local file
>>> loader = get_loader(use_local=True)
>>> config = loader.load('full/path/to/configfile')
# can pass local repo address as well
>>> loader = get_loader(use_local=True, local_repo='some/repo/path')
>>> config = loader.load('ubi7')
# can be reused
>>> config_ubi8 = loader.load('ubi8')
If the default ubi url is not defined and use_local not set, error will
be raised.
.. currentmodule:: ubiconfig
.. autofunction:: get_loader

.. autoclass:: UbiConfig

.. autoclass:: Loader
:members: load, load_all
:members: load, load_all
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
six
# Old python needs reqests<=2.18
requests<=2.18.4; python_version<'3'
requests; python_version>='3'
requests
jsonschema==2.5.1
# Old Python needs old PyYAML
PyYAML<3.12; python_version<'3'
Expand Down
14 changes: 7 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from setuptools import setup
from setuptools import setup, find_packages


def get_description():
return 'A Python Library for accessing Universal Base Image configurations'
return 'A Python Library for accessing Universal Base Image configuration'


def get_long_description():
Expand All @@ -24,18 +24,15 @@ def get_requirements():
version='0.1.0',
author='',
author_email='',
packages=['ubiconfig',
'ubiconfig.utils',
'ubiconfig.utils.api',
'ubiconfig.config_types'],
packages=find_packages(exclude=['tests', 'tests.*']),
package_data={'ubiconfig': ['utils/config_schema.json']},
url='https://github.com/release-engineering/ubi-config',
license='GNU General Public License',
description=get_description(),
long_description=get_long_description(),
long_description_content_type='text/markdown',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Programming Language :: Python :: 2',
Expand All @@ -45,4 +42,7 @@ def get_requirements():
'Topic :: Software Development :: Libraries :: Python Modules',
],
install_requires=get_requirements(),
project_urls={
'Documentation': 'https://release-engineering.github.io/ubi-config/',
},
)
32 changes: 32 additions & 0 deletions tests/ubiconfig/loaders/test_gitlab_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from mock import patch, MagicMock, Mock
import pytest
import yaml

from ubiconfig._impl.loaders import GitlabLoader


def mock_json(value):
out = MagicMock()
out.json.return_value = value
return out


def test_bad_yaml():
with patch('requests.Session') as mock_session_class:
session = mock_session_class.return_value
session.get.side_effect = [
# branches
mock_json([{'name': 'master'}]),

# files
mock_json([{'name': 'badfile.yaml', 'path': 'badfile.yaml'}]),

# content (not valid yaml!)
Mock(content='[oops not yaml'),
]

loader = GitlabLoader('https://some-repo.example.com/foo/bar')

# It should propagate the YAML load exception
with pytest.raises(yaml.YAMLError):
loader.load('badfile.yaml')
15 changes: 15 additions & 0 deletions tests/ubiconfig/loaders/test_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pytest import raises

from ubiconfig import Loader


def test_no_load():
"""load must be implemented in subclass"""
with raises(NotImplementedError):
Loader().load('something.yaml')


def test_no_load_all():
"""load_all must be implemented in subclass"""
with raises(NotImplementedError):
Loader().load_all()
Loading

0 comments on commit d2b16c9

Please sign in to comment.