-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_counter.py
More file actions
70 lines (58 loc) · 2.47 KB
/
process_counter.py
File metadata and controls
70 lines (58 loc) · 2.47 KB
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
import json
from system.plugins import PluginBase
PLUGIN_API_VERSION = "2.0.0"
class ProcessCounterPlugin(PluginBase):
name = "process_counter"
version = "1.0.0"
description = "Shows process count and detected work languages"
author = "vlalikoffc"
repo_url = "https://github.com/telegrambotrepos/plugins"
_CONFIG_PATH = "self:/config.json"
def on_load(self, ctx) -> None:
if not ctx.fs.exists(self._CONFIG_PATH):
ctx.fs.write_text(self._CONFIG_PATH, json.dumps(self._default_config(), ensure_ascii=False, indent=2))
def on_render(self, render_ctx, ctx) -> None:
default_status = getattr(render_ctx, "default_status", None)
if default_status is None:
return
config = self._load_config(ctx)
if not config.get("enabled", True):
return
count = default_status.process_count
if count is None:
return
line = config.get("line", "?? ?????????: {count}")
line = self._safe_format(line, {"count": count})
if line:
ctx.status.add_line(line)
languages = list(default_status.work_languages or [])
if languages:
lang_line = config.get("languages_line", "?? ?????: {languages}")
lang_line = self._safe_format(lang_line, {"languages": ", ".join(languages)})
if lang_line:
ctx.status.add_line(lang_line)
def _load_config(self, ctx) -> dict:
if not ctx.fs.exists(self._CONFIG_PATH):
cfg = self._default_config()
ctx.fs.write_text(self._CONFIG_PATH, json.dumps(cfg, ensure_ascii=False, indent=2))
return cfg
try:
raw = ctx.fs.read_text(self._CONFIG_PATH)
return json.loads(raw)
except (OSError, json.JSONDecodeError):
cfg = self._default_config()
ctx.fs.write_text(self._CONFIG_PATH, json.dumps(cfg, ensure_ascii=False, indent=2))
return cfg
@staticmethod
def _default_config() -> dict:
return {
"enabled": False,
"line": "?? ?????????: {count}",
"languages_line": "?? ?????: {languages}",
}
@staticmethod
def _safe_format(template: str, values: dict) -> str:
class SafeDict(dict):
def __missing__(self, key):
return "{" + key + "}"
return str(template).format_map(SafeDict(values)).strip()