Skip to content

Commit

Permalink
Rename example and fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
cderici committed Oct 12, 2023
1 parent eb6132b commit 9f30eb2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
73 changes: 73 additions & 0 deletions examples/annotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/python3

# Copyright 2023 Canonical Ltd.
# Licensed under the Apache V2, see LICENCE file for details.

import asyncio
import argparse
import logging
import sys

# from juju import jasyncio
import juju.model


async def get_annotations(model, args):
logging.info('getting annotations')
annotations = await model.get_annotations()
if args.key is None:
for key, value in annotations.items():
print(f'{key}: {value!r}')
else:
value = annotations[args.key]
print(value)


async def set_annotation(model, args):
logging.info(f'setting annotation for {args.key!r}')
await model.set_annotations({args.key: args.value})


async def get_model():
logging.info('getting model')
model = juju.model.Model()
await model.connect()
return model


async def run_func(args):
model = await get_model()
try:
await args.func(model, args)
finally:
logging.info('disconnecting the model')
await model.disconnect()


def parse_args(args):
p = argparse.ArgumentParser()
p.add_argument('--debug', action="store_const", dest="loglevel", const=logging.DEBUG, default=logging.WARNING)
p.add_argument('--verbose', '-v', action="store_const", dest="loglevel", const=logging.INFO)
p.add_argument('--quiet', action="store_const", dest="loglevel", const=logging.CRITICAL)
sub = p.add_subparsers()
getp = sub.add_parser('get')
getp.set_defaults(func=get_annotations)
getp.add_argument("key", help='the key to set', nargs='?')
setp = sub.add_parser('set')
setp.set_defaults(func=set_annotation)
setp.add_argument("key", help='the key to set')
setp.add_argument("value", help='set the value of key to this value, empty string removes the annotation')
return p.parse_args(args)


def main():
args = parse_args(sys.argv[1:])
rootLogger = logging.getLogger()
rootLogger.setLevel(args.loglevel)
asyncio.run(run_func(args))


if __name__ == '__main__':
main()

# vim: expandtab ts=4 sts=4 shiftwidth=4 autoindent:
File renamed without changes.
3 changes: 2 additions & 1 deletion juju/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ class LocalDeployType:
"""LocalDeployType deals with local only deployments.
"""

async def resolve(self, url, architecture, app_name=None, channel=None, series=None, revision=None, entity_url=None, force=False):
async def resolve(self, url, architecture, app_name=None, channel=None, series=None, revision=None,
entity_url=None, force=False):
"""resolve attempts to resolve a local charm or bundle using the url
and architecture. If information is missing, it will attempt to backfill
that information, before sending the result back.
Expand Down

0 comments on commit 9f30eb2

Please sign in to comment.