File: quickstart.rst

package info (click to toggle)
python-oslo.config 1%3A8.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,192 kB
  • sloc: python: 10,740; makefile: 30; sh: 10
file content (78 lines) | stat: -rw-r--r-- 2,015 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
==========================
 oslo.config Quick Start!
==========================

Are you brand new to oslo.config? This brief tutorial will get you started
understanding some of the fundamentals.

Prerequisites
-------------
* A plain text editor or Python-enabled IDE
* A Python interpreter
* A command shell from which the interpreter can be invoked
* The oslo_config library in your Python path.

Test Script
-----------
Put this in a file called ``oslocfgtest.py``.

.. code:: python

  # The sys module lets you get at the command line arguments.
  import sys

  # Load up the cfg module, which contains all the classes and methods
  # you'll need.
  from oslo_config import cfg

  # Define an option group
  grp = cfg.OptGroup('mygroup')

  # Define a couple of options
  opts = [cfg.StrOpt('option1'),
          cfg.IntOpt('option2', default=42)]

  # Register your config group
  cfg.CONF.register_group(grp)

  # Register your options within the config group
  cfg.CONF.register_opts(opts, group=grp)

  # Process command line arguments.  The arguments tell CONF where to
  # find your config file, which it loads and parses to populate itself.
  cfg.CONF(sys.argv[1:])

  # Now you can access the values from the config file as
  # CONF.<group>.<opt>
  print("The value of option1 is %s" % cfg.CONF.mygroup.option1)
  print("The value of option2 is %d" % cfg.CONF.mygroup.option2)

Conf File
---------
Put this in a file called ``oslocfgtest.conf`` in the same directory as
``oslocfgtest.py``.

.. code:: ini

  [mygroup]
  option1 = foo
  # Comment out option2 to test the default value
  # option2 = 123

Run It!
-------
From your command shell, in the same directory as your script and conf, invoke:

.. code:: shell

  python oslocfgtest.py --config-file oslocfgtest.conf

Revel in the output being exactly as expected.  If you've done everything
right, you should see:

.. code:: shell

  The value of option1 is foo
  The value of option2 is 42

Now go play with some more advanced option settings!