@@ -135,7 +135,7 @@ Put next lines into the ``Dockerfile`` file:
135
135
136
136
.. code-block :: bash
137
137
138
- FROM python:3.9 -buster
138
+ FROM python:3.10 -buster
139
139
140
140
ENV PYTHONUNBUFFERED=1
141
141
@@ -204,11 +204,11 @@ Logging and configuration
204
204
205
205
In this section we will configure the logging and configuration file parsing.
206
206
207
- Let's start with the the main part of our application - the container. Container will keep all of
207
+ Let's start with the the main part of our application – the container. Container will keep all of
208
208
the application components and their dependencies.
209
209
210
- First two components that we're going to add are the config object and the provider for
211
- configuring the logging.
210
+ First two components that we're going to add are the configuration provider and the resource provider
211
+ for configuring the logging.
212
212
213
213
Put next lines into the ``containers.py `` file:
214
214
@@ -224,7 +224,7 @@ Put next lines into the ``containers.py`` file:
224
224
225
225
class Container (containers .DeclarativeContainer ):
226
226
227
- config = providers.Configuration()
227
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
228
228
229
229
logging = providers.Resource(
230
230
logging.basicConfig,
@@ -233,26 +233,18 @@ Put next lines into the ``containers.py`` file:
233
233
format = config.log.format,
234
234
)
235
235
236
- .. note ::
237
-
238
- We have used the configuration value before it was defined. That's the principle how the
239
- ``Configuration `` provider works.
240
-
241
- Use first, define later.
242
-
243
- The configuration file will keep the logging settings.
244
-
245
- Put next lines into the ``config.yml `` file:
236
+ The configuration file will keep the logging settings. Put next lines into the ``config.yml `` file:
246
237
247
238
.. code-block :: yaml
248
239
249
240
log :
250
241
level : " INFO"
251
242
format : " [%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s"
252
243
253
- Now let's create the function that will run our daemon. It's traditionally called
254
- ``main() ``. The ``main() `` function will create the container. Then it will use the container
255
- to parse the ``config.yml `` file and call the logging configuration provider.
244
+ Now let's create the function that will run our daemon. It's traditionally called ``main() ``.
245
+ The ``main() `` function will start the dispatcher, but we will keep it empty for now.
246
+ We will create the container instance before calling ``main() `` in ``if __name__ == "__main__" ``.
247
+ Container instance will parse ``config.yml `` and then we will call the logging configuration provider.
256
248
257
249
Put next lines into the ``__main__.py `` file:
258
250
@@ -269,7 +261,6 @@ Put next lines into the ``__main__.py`` file:
269
261
270
262
if __name__ == " __main__" :
271
263
container = Container()
272
- container.config.from_yaml(" config.yml" )
273
264
container.init_resources()
274
265
275
266
main()
@@ -419,7 +410,7 @@ Edit ``containers.py``:
419
410
420
411
class Container (containers .DeclarativeContainer ):
421
412
422
- config = providers.Configuration()
413
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
423
414
424
415
logging = providers.Resource(
425
416
logging.basicConfig,
@@ -442,7 +433,7 @@ and call the ``run()`` method. We will use :ref:`wiring` feature.
442
433
Edit ``__main__.py ``:
443
434
444
435
.. code-block :: python
445
- :emphasize- lines: 3 - 5 ,9 - 11 ,18
436
+ :emphasize- lines: 3 - 5 ,9 - 11 ,17
446
437
447
438
""" Main module."""
448
439
@@ -459,7 +450,6 @@ Edit ``__main__.py``:
459
450
460
451
if __name__ == " __main__" :
461
452
container = Container()
462
- container.config.from_yaml(" config.yml" )
463
453
container.init_resources()
464
454
container.wire(modules = [__name__ ])
465
455
@@ -559,7 +549,7 @@ Edit ``containers.py``:
559
549
560
550
class Container (containers .DeclarativeContainer ):
561
551
562
- config = providers.Configuration()
552
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
563
553
564
554
logging = providers.Resource(
565
555
logging.basicConfig,
@@ -664,7 +654,7 @@ Edit ``containers.py``:
664
654
665
655
class Container (containers .DeclarativeContainer ):
666
656
667
- config = providers.Configuration()
657
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
668
658
669
659
logging = providers.Resource(
670
660
logging.basicConfig,
@@ -763,7 +753,7 @@ Edit ``containers.py``:
763
753
764
754
class Container (containers .DeclarativeContainer ):
765
755
766
- config = providers.Configuration()
756
+ config = providers.Configuration(yaml_files = [ " config.yml " ] )
767
757
768
758
logging = providers.Resource(
769
759
logging.basicConfig,
@@ -888,7 +878,7 @@ Create ``tests.py`` in the ``monitoringdaemon`` package:
888
878
and put next into it:
889
879
890
880
.. code-block :: python
891
- :emphasize- lines: 54 ,70 - 71
881
+ :emphasize- lines: 54 ,70 - 73
892
882
893
883
""" Tests module."""
894
884
@@ -909,28 +899,28 @@ and put next into it:
909
899
910
900
@pytest.fixture
911
901
def container ():
912
- container = Container()
913
- container.config.from_dict({
914
- " log" : {
915
- " level" : " INFO" ,
916
- " formant" : " [%(asctime)s ] [%(levelname)s ] [%(name)s ]: %(message)s " ,
917
- },
918
- " monitors" : {
919
- " example" : {
920
- " method" : " GET" ,
921
- " url" : " http://fake-example.com" ,
922
- " timeout" : 1 ,
923
- " check_every" : 1 ,
902
+ return Container(
903
+ config = {
904
+ " log" : {
905
+ " level" : " INFO" ,
906
+ " formant" : " [%(asctime)s ] [%(levelname)s ] [%(name)s ]: %(message)s " ,
924
907
},
925
- " httpbin" : {
926
- " method" : " GET" ,
927
- " url" : " https://fake-httpbin.org/get" ,
928
- " timeout" : 1 ,
929
- " check_every" : 1 ,
908
+ " monitors" : {
909
+ " example" : {
910
+ " method" : " GET" ,
911
+ " url" : " http://fake-example.com" ,
912
+ " timeout" : 1 ,
913
+ " check_every" : 1 ,
914
+ },
915
+ " httpbin" : {
916
+ " method" : " GET" ,
917
+ " url" : " https://fake-httpbin.org/get" ,
918
+ " timeout" : 1 ,
919
+ " check_every" : 1 ,
920
+ },
930
921
},
931
- },
932
- })
933
- return container
922
+ }
923
+ )
934
924
935
925
936
926
@pytest.mark.asyncio
@@ -959,9 +949,10 @@ and put next into it:
959
949
example_monitor_mock = mock.AsyncMock()
960
950
httpbin_monitor_mock = mock.AsyncMock()
961
951
962
- with container.example_monitor.override(example_monitor_mock), \
963
- container.httpbin_monitor.override(httpbin_monitor_mock):
964
-
952
+ with container.override_providers(
953
+ example_monitor = example_monitor_mock,
954
+ httpbin_monitor = httpbin_monitor_mock,
955
+ ):
965
956
dispatcher = container.dispatcher()
966
957
event_loop.create_task(dispatcher.start())
967
958
await asyncio.sleep(0.1 )
@@ -980,25 +971,25 @@ You should see:
980
971
981
972
.. code-block :: bash
982
973
983
- platform linux -- Python 3.9 , pytest-6.0.1 , py-1.9 .0, pluggy-0.13.1
974
+ platform linux -- Python 3.10.0 , pytest-6.2.5 , py-1.10 .0, pluggy-1.0.0
984
975
rootdir: /code
985
- plugins: asyncio-0.14 .0, cov-2.10 .0
976
+ plugins: asyncio-0.16 .0, cov-3.0 .0
986
977
collected 2 items
987
978
988
979
monitoringdaemon/tests.py .. [100%]
989
980
990
- ----------- coverage: platform linux, python 3.9 -----------
981
+ ---------- coverage: platform linux, python 3.10.0-final-0 -----------
991
982
Name Stmts Miss Cover
992
983
----------------------------------------------------
993
984
monitoringdaemon/__init__.py 0 0 100%
994
- monitoringdaemon/__main__.py 13 13 0%
985
+ monitoringdaemon/__main__.py 11 11 0%
995
986
monitoringdaemon/containers.py 11 0 100%
996
- monitoringdaemon/dispatcher.py 44 5 89%
987
+ monitoringdaemon/dispatcher.py 45 5 89%
997
988
monitoringdaemon/http.py 6 3 50%
998
989
monitoringdaemon/monitors.py 23 1 96%
999
- monitoringdaemon/tests.py 37 0 100%
990
+ monitoringdaemon/tests.py 35 0 100%
1000
991
----------------------------------------------------
1001
- TOTAL 134 22 84 %
992
+ TOTAL 131 20 85 %
1002
993
1003
994
.. note ::
1004
995
0 commit comments