hungercore 1.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hungercore/__init__.py +18 -0
- hungercore/email/__init__.py +9 -0
- hungercore/email/client.py +35 -0
- hungercore/email/email_templates/restart.html +87 -0
- hungercore/email/manager.py +55 -0
- hungercore/email/message.py +33 -0
- hungercore/messagerouter.py +196 -0
- hungercore/utils/__init__.py +15 -0
- hungercore/utils/buffer.py +20 -0
- hungercore/utils/convert.py +61 -0
- hungercore/utils/exceptions.py +12 -0
- hungercore/utils/methods.py +60 -0
- hungercore/utils/schedule.py +24 -0
- hungercore/utils/snapshot.py +92 -0
- hungercore/utils/terminal.py +4 -0
- hungercore/webhook/__init__.py +4 -0
- hungercore/webhook/client.py +20 -0
- hungercore/webhook/server.py +56 -0
- hungercore-1.0.dev1.dist-info/METADATA +41 -0
- hungercore-1.0.dev1.dist-info/RECORD +23 -0
- hungercore-1.0.dev1.dist-info/WHEEL +5 -0
- hungercore-1.0.dev1.dist-info/licenses/LICENSE +674 -0
- hungercore-1.0.dev1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from statistics import mean
|
|
3
|
+
from hungercore.panel import Panel
|
|
4
|
+
from hungercore.servers.generic import GenericServer
|
|
5
|
+
from hungercore.servers.minecraft import MinecraftServer
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Snapshot:
|
|
9
|
+
def __init__(
|
|
10
|
+
self,
|
|
11
|
+
Server: 'Server',
|
|
12
|
+
rounding: int = 2,
|
|
13
|
+
duration: float = 0.0,
|
|
14
|
+
interval: float = 0.5,
|
|
15
|
+
drop_outliers: int = 0,
|
|
16
|
+
gb: bool = False,
|
|
17
|
+
):
|
|
18
|
+
'''
|
|
19
|
+
Takes a snapshot of server resources.
|
|
20
|
+
If duration > 0, performs smoothing by sampling repeatedly.
|
|
21
|
+
|
|
22
|
+
duration: total seconds to sample (0 = no smoothing)
|
|
23
|
+
interval: delay between samples
|
|
24
|
+
drop_outliers: number of highest and lowest samples to drop (per metric)
|
|
25
|
+
'''
|
|
26
|
+
self.Server = Server
|
|
27
|
+
|
|
28
|
+
# --- sampling mode ---
|
|
29
|
+
if duration > 0:
|
|
30
|
+
samples = {
|
|
31
|
+
'ram': [],
|
|
32
|
+
'cpu': [],
|
|
33
|
+
'network_in': [],
|
|
34
|
+
'network_out': [],
|
|
35
|
+
'tps': [],
|
|
36
|
+
'players': []
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
end = time.time() + duration
|
|
40
|
+
while time.time() < end:
|
|
41
|
+
samples['ram'].append(Server.getRAM(rounding, gb))
|
|
42
|
+
samples['cpu'].append(Server.getCPU())
|
|
43
|
+
samples['network_in'].append(Server.getNetworkIn(rounding, gb))
|
|
44
|
+
samples['network_out'].append(Server.getNetworkOut(rounding, gb))
|
|
45
|
+
if hasattr(Server, 'getTPS'):
|
|
46
|
+
samples['tps'].append(Server.getTPS())
|
|
47
|
+
if hasattr(Server, 'getPlayers'):
|
|
48
|
+
samples['players'].append(Server.getPlayers())
|
|
49
|
+
time.sleep(interval)
|
|
50
|
+
|
|
51
|
+
def smooth(values):
|
|
52
|
+
if not values:
|
|
53
|
+
return None
|
|
54
|
+
values = sorted(values)
|
|
55
|
+
if drop_outliers > 0:
|
|
56
|
+
values = values[drop_outliers:len(values) - drop_outliers]
|
|
57
|
+
return mean(values) if values else None
|
|
58
|
+
|
|
59
|
+
self.ram = smooth(samples['ram'])
|
|
60
|
+
self.cpu = smooth(samples['cpu'])
|
|
61
|
+
self.network_in = smooth(samples['network_in'])
|
|
62
|
+
self.network_out = smooth(samples['network_out'])
|
|
63
|
+
self.tps = smooth(samples['tps'])
|
|
64
|
+
self.players = Server.getPlayers('count')
|
|
65
|
+
|
|
66
|
+
# --- instant snapshot (no smoothing) ---
|
|
67
|
+
else:
|
|
68
|
+
self.ram = Server.getRAM(rounding, gb)
|
|
69
|
+
self.cpu = Server.getCPU()
|
|
70
|
+
self.network_in = Server.getNetworkIn(rounding, gb)
|
|
71
|
+
self.network_out = Server.getNetworkOut(rounding, gb)
|
|
72
|
+
self.tps = Server.getTPS() if hasattr(Server, 'getTPS') else None
|
|
73
|
+
self.players = Server.getPlayers('count')
|
|
74
|
+
|
|
75
|
+
# uptime is static
|
|
76
|
+
self.uptime = Server.getUptime()
|
|
77
|
+
self.uptime_formatted = Server.getUptime(True)
|
|
78
|
+
|
|
79
|
+
def __str__(self):
|
|
80
|
+
base = (
|
|
81
|
+
f'Server: {self.Server.name}\n'
|
|
82
|
+
f'Uptime: {self.uptime_formatted}\n'
|
|
83
|
+
f'RAM: {self.ram}\n'
|
|
84
|
+
f'CPU: {self.cpu}\n'
|
|
85
|
+
f'Network (in): {self.network_in}\n'
|
|
86
|
+
f'Network (out): {self.network_out}'
|
|
87
|
+
)
|
|
88
|
+
if self.tps is not None:
|
|
89
|
+
base += f'\nTPS: {self.tps}'
|
|
90
|
+
if self.players is not None:
|
|
91
|
+
base += f'\nOnline players: {self.players}'
|
|
92
|
+
return base
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
|
|
3
|
+
class WebhookClient:
|
|
4
|
+
def __init__(self, url: str, token: str, timeout: int = 5):
|
|
5
|
+
self.url = url
|
|
6
|
+
self.token = token
|
|
7
|
+
self.timeout = timeout
|
|
8
|
+
|
|
9
|
+
def send(self, event: str, **ctx):
|
|
10
|
+
payload = {'event': event, **ctx}
|
|
11
|
+
|
|
12
|
+
resp = requests.post(
|
|
13
|
+
self.url,
|
|
14
|
+
json=payload,
|
|
15
|
+
headers={'x-webhook-token': self.token},
|
|
16
|
+
timeout=self.timeout
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
resp.raise_for_status()
|
|
20
|
+
return resp.json()
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import uvicorn
|
|
2
|
+
from fastapi import FastAPI, Request, Header, HTTPException
|
|
3
|
+
|
|
4
|
+
class WebhookServer:
|
|
5
|
+
def __init__(self, port: int, token: str, host: str = "0.0.0.0", logLevel: str = "info"):
|
|
6
|
+
self.port = port
|
|
7
|
+
self.token = token
|
|
8
|
+
self.host = host
|
|
9
|
+
self.logLevel = logLevel
|
|
10
|
+
|
|
11
|
+
self.app = FastAPI()
|
|
12
|
+
self._server = None
|
|
13
|
+
|
|
14
|
+
# routing tables
|
|
15
|
+
self._endpointHandlers: dict[str, callable] = {}
|
|
16
|
+
self._eventHandlers: dict[str, callable] = {}
|
|
17
|
+
|
|
18
|
+
def addEndpoint(self, path: str, handler: callable):
|
|
19
|
+
# register endpoint
|
|
20
|
+
self._endpointHandlers[path] = handler
|
|
21
|
+
|
|
22
|
+
@self.app.post(path)
|
|
23
|
+
async def _endpoint(request: Request, x_webhook_token: str | None = Header(None)):
|
|
24
|
+
if x_webhook_token != self.token:
|
|
25
|
+
raise HTTPException(status_code=401, detail="invalid token")
|
|
26
|
+
|
|
27
|
+
data = await request.json()
|
|
28
|
+
event = (data.get("event") or "").lower()
|
|
29
|
+
|
|
30
|
+
# endpoint handler
|
|
31
|
+
await handler(event, data)
|
|
32
|
+
|
|
33
|
+
# event handler
|
|
34
|
+
if event in self._eventHandlers:
|
|
35
|
+
await self._eventHandlers[event](data)
|
|
36
|
+
|
|
37
|
+
return {"status": "ok"}
|
|
38
|
+
|
|
39
|
+
def addHandler(self, event: str, handler: callable):
|
|
40
|
+
# register event handler
|
|
41
|
+
self._eventHandlers[event.lower()] = handler
|
|
42
|
+
|
|
43
|
+
async def start(self):
|
|
44
|
+
config = uvicorn.Config(
|
|
45
|
+
self.app,
|
|
46
|
+
host=self.host,
|
|
47
|
+
port=self.port,
|
|
48
|
+
log_level=self.logLevel
|
|
49
|
+
)
|
|
50
|
+
self._server = uvicorn.Server(config)
|
|
51
|
+
await self._server.serve()
|
|
52
|
+
|
|
53
|
+
async def stop(self):
|
|
54
|
+
# stop server
|
|
55
|
+
if self._server:
|
|
56
|
+
self._server.should_exit = True
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hungercore
|
|
3
|
+
Version: 1.0.dev1
|
|
4
|
+
Summary: HungerNet's core Python library.
|
|
5
|
+
Author: iFamished
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HungerNet/HungerCore
|
|
8
|
+
Project-URL: Documentation, https://github.com/HungerNet/HungerCore
|
|
9
|
+
Project-URL: Source, https://github.com/HungerNet/HungerCore
|
|
10
|
+
Project-URL: Issues, https://github.com/HungerNet/HungerCore/issues
|
|
11
|
+
Keywords: minecraft,automation,server-management,hungernet
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Development Status :: 4 - Beta
|
|
19
|
+
Classifier: Intended Audience :: Developers
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
21
|
+
Classifier: Topic :: System :: Systems Administration
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: requests
|
|
26
|
+
Requires-Dist: PyYaml
|
|
27
|
+
Requires-Dist: mapres
|
|
28
|
+
Requires-Dist: jinja2
|
|
29
|
+
Requires-Dist: cloudflare
|
|
30
|
+
Requires-Dist: uvicorn
|
|
31
|
+
Requires-Dist: fastapi
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# HungerCore
|
|
35
|
+
HungerNet's core Python library.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install hungercore
|
|
41
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
hungercore/__init__.py,sha256=EQdwmlPx-QZppxQbpkBl7J3S1DY_x0j53hb00v2lbo0,358
|
|
2
|
+
hungercore/messagerouter.py,sha256=v2JoiJCQ1rlqxaisWUbsqJAlMd1CNJt601SVZhjG630,6654
|
|
3
|
+
hungercore/email/__init__.py,sha256=1XQCQazfLp3G6T1n4PuNFi_Z2JvGS8TywaWAN5_-ROY,160
|
|
4
|
+
hungercore/email/client.py,sha256=I-BFb9azz4vv2PVUyR2JCSu0_AjTBgKgFSZNWqgcnlc,947
|
|
5
|
+
hungercore/email/manager.py,sha256=JU3psw80fvduSCueGyyPZSslJRSF4AGFMXZmxikE0DI,1683
|
|
6
|
+
hungercore/email/message.py,sha256=j3ERUrCzXgvNx3ZpHyQ9_ShYO3eRf4BxzqPnIejFaGc,966
|
|
7
|
+
hungercore/email/email_templates/restart.html,sha256=x0OGltq-ZwtLbxx6H3ufIRXFEXGF27yFdJtQwKPKY-s,1538
|
|
8
|
+
hungercore/utils/__init__.py,sha256=qzDr2WUSWhRcs8wG9CNv8-ZQbpOsZKsvIg4MChp9Cv8,287
|
|
9
|
+
hungercore/utils/buffer.py,sha256=sMxR_ssNi-JY109qCx9oXgzW3cKUYMWGKKPymApao9s,456
|
|
10
|
+
hungercore/utils/convert.py,sha256=Ptgo1EjP5_48gbZMboX5fphMgodzME8lwzn-dYwwK7o,1233
|
|
11
|
+
hungercore/utils/exceptions.py,sha256=YXmJ3f3W2PgpoqZcoCV5YNqHFJ7m6fp8K97N6KTokY4,324
|
|
12
|
+
hungercore/utils/methods.py,sha256=175wFeodwcEmUQQi1EAgN1wl99LQqZC2cWfJkgtfneY,1720
|
|
13
|
+
hungercore/utils/schedule.py,sha256=Lu3hMF-yboU4i1n7HkKSL_nxaJzTt3-f8Iatkc-77Z8,781
|
|
14
|
+
hungercore/utils/snapshot.py,sha256=e0byhmKK45fzZ2Z1kruXMONaXqzAVQYRsfW5_h82vco,3323
|
|
15
|
+
hungercore/utils/terminal.py,sha256=cDrbm5R5VprySiVB3P8OVy2lx5NDEsZNNgc2UPX308k,80
|
|
16
|
+
hungercore/webhook/__init__.py,sha256=6dyoO5LwhprOxaqMf3xaDpGVWOpybMgT-LQD4Agma9A,114
|
|
17
|
+
hungercore/webhook/client.py,sha256=cJ4itPWd5V6SUbZRjdtlLbR7inCZw0jefH5xL-Er09Y,500
|
|
18
|
+
hungercore/webhook/server.py,sha256=WBMsBAp8ZwhpiAehX54U-aTqW5LE56FNowNiO65YXj4,1733
|
|
19
|
+
hungercore-1.0.dev1.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
20
|
+
hungercore-1.0.dev1.dist-info/METADATA,sha256=pOlCK6eHsrAjQZrp5FlUMW0PxwJi650AfPdGhJSHKQA,1313
|
|
21
|
+
hungercore-1.0.dev1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
22
|
+
hungercore-1.0.dev1.dist-info/top_level.txt,sha256=F1e1cGv_RvvJr3dEF9d6vG7jjB1YutmEuyT02RRujbM,11
|
|
23
|
+
hungercore-1.0.dev1.dist-info/RECORD,,
|