• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from threading import RLock
20
21from _core.context.single import SingleType
22from _core.context.proxy import SessionInfo
23from _core.context.abs import Sub
24
25__all__ = ["ActiveState", "ContextImpl"]
26
27
28class ActiveState:
29
30    def __init__(self, scheduler: Sub or None = None):
31        self._active = scheduler
32
33    def is_running(self) -> bool:
34        if self._active:
35            return self._active.is_executing()
36        return False
37
38    def get_active_scheduler(self) -> Sub:
39        return self._active
40
41
42class ContextImpl(metaclass=SingleType):
43    def __init__(self):
44        self._lock = RLock()
45        self._session_info: SessionInfo = SessionInfo()
46        self._state: ActiveState = ActiveState()
47        self._global_attr = dict()
48
49    def get_session_info(self) -> SessionInfo:
50        return self._session_info
51
52    def update_session_info(self, info: SessionInfo):
53        self._session_info = info
54
55    def update_state(self, state: ActiveState):
56        with self._lock:
57            self._state = state
58
59    def get_active_state(self):
60        with self._lock:
61            return self._state
62
63    def write_attr(self, key, value):
64        self._global_attr.update({key: value})
65
66    def has_attr(self, key):
67        return key in self._global_attr.keys()
68
69    def read_attr(self, key):
70        if key in self._global_attr.keys():
71            return self._global_attr.get(key)
72        return None
73
74    def remove_attr(self, key):
75        if key in self._global_attr.keys():
76            self._global_attr.pop(key)
77        return None
78