Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

override settings and schema as CLI options #139

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ Advanced Features
--single-order \
--template my_template.yml

2. Use a custom settings file
::

# 1. save and edit a copy of the default settings
$ line_item_manager show settings > my_settings.yml

# 2. edit my_settings.yml; e.g. use a custom bidder code

# 3. create line items referencing your custom settings
$ line_item_manager create my_config.yml \
--single-order \
--settings my_settings.yml

3. Use a custom schema file
::

# 1. save and edit a copy of the default schema
$ line_item_manager show schema > my_schema.yml

# 2. edit my_schema.yml; e.g. use a custom currency list

# 3. create line items referencing your custom schema
$ line_item_manager create my_config.yml \
--single-order \
--schema my_schema.yml

Local Development
-----------------

Expand Down
2 changes: 1 addition & 1 deletion line_item_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__version__ = '0.2.10'

# For an official release, use dev_version = ''
dev_version = ''
dev_version = '1'

version = __version__
if dev_version:
Expand Down
13 changes: 12 additions & 1 deletion line_item_manager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ def cli(ctx: click.core.Context, version: bool) -> None:
@click.option('--template',
type=click.Path(exists=True), help='Advanced users: path to custom line item template. ' \
'Use "line_item_manager show template" to see the package default')
@click.option('--settings',
type=click.Path(exists=True), help='Advanced users: path to settings file. ' \
'Use "line_item_manager show settings" to see the package default')
@click.option('--schema',
type=click.Path(exists=True), help='Advanced users: path to schema file. ' \
'Use "line_item_manager show schema" to see the package default')
@click.option('--single-order', '-s', is_flag=True,
help='Create a single set of orders instead of orders per bidder.')
@click.option('--bidder-code', '-b', multiple=True,
Expand Down Expand Up @@ -154,13 +160,18 @@ def show_resource(filename: str) -> None:
print(fp.read())

@cli.command()
@click.argument('resource', type=click.Choice(['config', 'bidders', 'template']))
@click.argument('resource', type=click.Choice(['config', 'bidders', 'template',
'settings', 'schema']))
def show(resource: str) -> None:
"""Show resources"""
if resource == 'config':
show_resource('conf.d/line_item_manager.yml')
if resource == 'template':
show_resource('conf.d/line_item_template.yml')
if resource == 'settings':
show_resource('conf.d/settings.yml')
if resource == 'schema':
show_resource('conf.d/schema.yml')
if resource == 'bidders':
print("%-25s%s" % ('Code', 'Name'))
print("%-25s%s" % ('----', '----'))
Expand Down
12 changes: 10 additions & 2 deletions line_item_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Config:
def __init__(self):
self._schema = None
self._cpm_names = None
self._app = load_package_file('settings.yml')
self._app = None
self._start_time = datetime.now()
self.set_logger()

Expand All @@ -42,6 +42,8 @@ def set_log_level(self) -> None:

@property
def app(self) -> dict:
if self._app is None:
self._app = self.settings_obj()
return self._app

@property
Expand Down Expand Up @@ -88,7 +90,8 @@ def network_name(self) -> str:
@property
def schema(self) -> dict:
if self._schema is None:
self._schema = load_package_file('schema.yml')
self._schema = load_file(self.cli['schema']) if self.cli['schema'] else \
load_package_file('schema.yml')
return self._schema

def bidder_codes(self) -> List[str]:
Expand Down Expand Up @@ -141,6 +144,11 @@ def template_src(self) -> str:
return fp.read()
return read_package_file('line_item_template.yml')

def settings_obj(self) -> dict:
if self.cli['settings']:
return load_file(self.cli['settings'])
return load_package_file('settings.yml')

def pre_create(self) -> None:
li_ = self.user['line_item']
is_standard = li_['item_type'].upper() == "STANDARD"
Expand Down
5 changes: 3 additions & 2 deletions line_item_manager/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ class CreativeVideo(Creative):
'vastRedirectType', 'duration')

def __init__(self, *args, xsi_type: str='VastRedirectCreative', vastRedirectType: str='LINEAR',
duration: int=config.app['prebid']['creative']['video']['max_duration'], **kwargs):
duration: int, **kwargs):
kwargs['xsi_type'] = xsi_type
kwargs['vastRedirectType'] = vastRedirectType
kwargs['duration'] = duration
kwargs['duration'] = duration if duration else \
config.app['prebid']['creative']['video']['max_duration']
super().__init__(*args, **kwargs)

class CreativeBanner(Creative):
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def pytest_configure():

@pytest.fixture
def cli_config(request):
config._app = None
config._cpm_names = None
config._schema = None

# patch
cli_str = request.node.get_closest_marker('command').args[0]
cli_args = shlex.split(cli_str)
Expand Down
1 change: 1 addition & 0 deletions tests/resources/li_template.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# custom template file
orderId: {{ li.order.id }}
name: "{{ li_cfg.name }}"

Expand Down
Loading