1
|
|
|
# Copyright 2019 Virantha N. Ekanayake |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
|
15
|
|
|
"""Super-class of all the Tasks in the event-loop |
16
|
|
|
""" |
17
|
|
|
from enum import Enum |
18
|
|
|
import logging |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class Process: |
22
|
|
|
"""Subclass this for anything going into the Async Event Loop |
23
|
|
|
|
24
|
|
|
This class keeps track of a unique numeric ID for each process, and its name. |
25
|
|
|
It also provides some utilty functions to log messages at various levels. |
26
|
|
|
|
27
|
|
|
Attributes: |
28
|
|
|
id (int) : Process ID (unique) |
29
|
|
|
name (str): Human readable name for process (does not need to be unique) |
30
|
|
|
|
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
_next_id = 0 |
34
|
|
|
|
35
|
|
|
def __init__(self, name): |
36
|
|
|
self.name = name |
37
|
|
|
|
38
|
|
|
# Assign ID |
39
|
|
|
self.id = Process._next_id |
40
|
|
|
Process._next_id += 1 |
41
|
|
|
|
42
|
|
|
self.logger = logging.getLogger(str(self)) |
43
|
|
|
|
44
|
|
|
def __str__(self): |
45
|
|
|
return f'{self.name}.{self.id}' |
46
|
|
|
|
47
|
|
|
def __repr__(self): |
48
|
|
|
return f'{type(self).__name__}("{self.name}")' |
49
|
|
|
|
50
|
|
|
def message(self, m : str , level = logging.INFO): |
51
|
|
|
"""Print message *m* if its level is lower than the instance level""" |
52
|
|
|
|
53
|
|
|
if level == logging.DEBUG: |
54
|
|
|
self.logger.debug(m) |
55
|
|
|
elif level == logging.INFO: |
56
|
|
|
self.logger.info(m) |
57
|
|
|
elif level == logging.ERROR: |
58
|
|
|
self.logger.error(m) |
59
|
|
|
|
60
|
|
|
def message_info(self, m): |
61
|
|
|
"""Helper function for logging messages at INFO level""" |
62
|
|
|
self.message(m, logging.INFO) |
63
|
|
|
|
64
|
|
|
def message_debug(self, m): |
65
|
|
|
"""Helper function for logging messages at DEBUG level""" |
66
|
|
|
self.message(m, logging.DEBUG) |
67
|
|
|
|
68
|
|
|
def message_error(self, m): |
69
|
|
|
"""Helper function for logging messages at ERROR level""" |
70
|
|
|
self.message(m, logging.ERROR) |
71
|
|
|
|