nonebot-plugin-receipts 0.2.3__tar.gz → 0.2.4__tar.gz

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.
Files changed (27) hide show
  1. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/PKG-INFO +1 -1
  2. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/__init__.py +2 -1
  3. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/command_handlers.py +2 -2
  4. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/config.py +35 -23
  5. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts.egg-info/PKG-INFO +1 -1
  6. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/pyproject.toml +1 -1
  7. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/tests/test_command_handlers.py +8 -0
  8. nonebot_plugin_receipts-0.2.4/tests/test_config.py +69 -0
  9. nonebot_plugin_receipts-0.2.3/tests/test_config.py +0 -45
  10. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/README.md +0 -0
  11. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/escpos_backend.py +0 -0
  12. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/raster_backend.py +0 -0
  13. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/render_blocks.py +0 -0
  14. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/render_types.py +0 -0
  15. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/render_utils.py +0 -0
  16. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/renderer.py +0 -0
  17. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/spooler.py +0 -0
  18. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/template.py +0 -0
  19. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts/text_markup.py +0 -0
  20. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts.egg-info/SOURCES.txt +0 -0
  21. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts.egg-info/dependency_links.txt +0 -0
  22. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts.egg-info/requires.txt +0 -0
  23. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/nonebot_plugin_receipts.egg-info/top_level.txt +0 -0
  24. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/setup.cfg +0 -0
  25. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/tests/test_render_blocks.py +0 -0
  26. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/tests/test_renderer.py +0 -0
  27. {nonebot_plugin_receipts-0.2.3 → nonebot_plugin_receipts-0.2.4}/tests/test_text_markup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot_plugin_receipts
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: NoneBot2 plugin for sending text and image messages to receipts-spooler
5
5
  Project-URL: homepage, https://github.com/Effect-Wei/nonebot_plugin_receipts
6
6
  Project-URL: repository, https://github.com/Effect-Wei/nonebot_plugin_receipts
@@ -8,7 +8,8 @@ __plugin_meta__ = PluginMetadata(
8
8
  name="小票打印",
9
9
  description="将发送给 Bot 的文本和图片消息通过 receipts-spooler 打印",
10
10
  usage=(
11
- "发送 /ticket 或 /receipt 后附带文本/图片,或单独发送命令后按提示再发送一条要打印的消息。"
11
+ "发送 /ticket 或 /receipt 后附带文本/图片,"
12
+ "或单独发送命令后按提示再发送一条要打印的消息。"
12
13
  ),
13
14
  type="application",
14
15
  config=Config,
@@ -112,8 +112,8 @@ def get_runtime_config() -> Config:
112
112
  def is_receipt_submission_allowed(event: MessageEvent) -> bool:
113
113
  """Return whether the incoming event is allowed to submit a print job."""
114
114
  config = get_runtime_config()
115
- allowed_user_ids = set(config.receipt_allowed_user_ids)
116
- allowed_group_ids = set(config.receipt_allowed_group_ids)
115
+ allowed_user_ids = set(config.allowed_user_ids)
116
+ allowed_group_ids = set(config.allowed_group_ids)
117
117
 
118
118
  if not allowed_user_ids and not allowed_group_ids:
119
119
  return True
@@ -5,6 +5,31 @@ from typing import Any, Literal
5
5
  from pydantic import BaseModel, Field, field_validator
6
6
 
7
7
 
8
+ def normalize_whitelist_ids(value: Any) -> list[str]:
9
+ if value is None:
10
+ return []
11
+
12
+ if isinstance(value, str):
13
+ stripped = value.strip()
14
+ if not stripped:
15
+ return []
16
+
17
+ try:
18
+ value = json.loads(stripped)
19
+ except json.JSONDecodeError:
20
+ return [
21
+ item
22
+ for part in re.split(r"[\s,,]+", stripped)
23
+ if (item := part.strip())
24
+ ]
25
+
26
+ if isinstance(value, list | tuple | set | frozenset):
27
+ return [item for entry in value if (item := str(entry).strip())]
28
+
29
+ item = str(value).strip()
30
+ return [item] if item else []
31
+
32
+
8
33
  class Config(BaseModel):
9
34
  receipts_spooler_url: str = "http://127.0.0.1:8000"
10
35
  receipts_spooler_token: str | None = None
@@ -20,8 +45,8 @@ class Config(BaseModel):
20
45
  receipt_session_timeout_seconds: int = Field(default=120, ge=10, le=3600)
21
46
  receipt_feed_lines: int = Field(default=4, ge=0, le=10)
22
47
  receipt_enable_cut: bool = True
23
- receipt_allowed_user_ids: list[str] = Field(default_factory=list)
24
- receipt_allowed_group_ids: list[str] = Field(default_factory=list)
48
+ receipt_allowed_user_ids: str | list[str] | None = Field(default_factory=list)
49
+ receipt_allowed_group_ids: str | list[str] | None = Field(default_factory=list)
25
50
 
26
51
  @field_validator(
27
52
  "receipt_allowed_user_ids",
@@ -30,25 +55,12 @@ class Config(BaseModel):
30
55
  )
31
56
  @classmethod
32
57
  def normalize_whitelist_ids(cls, value: Any) -> list[str]:
33
- if value is None:
34
- return []
58
+ return normalize_whitelist_ids(value)
59
+
60
+ @property
61
+ def allowed_user_ids(self) -> list[str]:
62
+ return normalize_whitelist_ids(self.receipt_allowed_user_ids)
35
63
 
36
- if isinstance(value, str):
37
- stripped = value.strip()
38
- if not stripped:
39
- return []
40
-
41
- try:
42
- value = json.loads(stripped)
43
- except json.JSONDecodeError:
44
- return [
45
- item
46
- for part in re.split(r"[\s,,]+", stripped)
47
- if (item := part.strip())
48
- ]
49
-
50
- if isinstance(value, list | tuple | set | frozenset):
51
- return [item for entry in value if (item := str(entry).strip())]
52
-
53
- item = str(value).strip()
54
- return [item] if item else []
64
+ @property
65
+ def allowed_group_ids(self) -> list[str]:
66
+ return normalize_whitelist_ids(self.receipt_allowed_group_ids)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot_plugin_receipts
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: NoneBot2 plugin for sending text and image messages to receipts-spooler
5
5
  Project-URL: homepage, https://github.com/Effect-Wei/nonebot_plugin_receipts
6
6
  Project-URL: repository, https://github.com/Effect-Wei/nonebot_plugin_receipts
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "nonebot_plugin_receipts"
7
- version = "0.2.3"
7
+ version = "0.2.4"
8
8
  description = "NoneBot2 plugin for sending text and image messages to receipts-spooler"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12, <4.0"
@@ -114,6 +114,14 @@ class DummyPluginConfig:
114
114
  self.receipt_allowed_user_ids = allowed_user_ids or []
115
115
  self.receipt_allowed_group_ids = allowed_group_ids or []
116
116
 
117
+ @property
118
+ def allowed_user_ids(self) -> list[str]:
119
+ return self.receipt_allowed_user_ids
120
+
121
+ @property
122
+ def allowed_group_ids(self) -> list[str]:
123
+ return self.receipt_allowed_group_ids
124
+
117
125
 
118
126
  class CommandHandlersTestCase(unittest.TestCase):
119
127
  def setUp(self) -> None:
@@ -0,0 +1,69 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import unittest
5
+ from unittest.mock import patch
6
+
7
+ from nonebot.config import BaseSettings
8
+ from nonebot_plugin_receipts.config import Config
9
+
10
+
11
+ class ConfigTestCase(unittest.TestCase):
12
+ def test_whitelist_defaults_to_empty_lists(self) -> None:
13
+ config = Config()
14
+
15
+ self.assertEqual(config.allowed_user_ids, [])
16
+ self.assertEqual(config.allowed_group_ids, [])
17
+
18
+ def test_whitelist_accepts_comma_separated_strings(self) -> None:
19
+ config = Config.model_validate(
20
+ {
21
+ "receipt_allowed_user_ids": "123456, 234567,345678",
22
+ "receipt_allowed_group_ids": "456789 567890",
23
+ }
24
+ )
25
+
26
+ self.assertEqual(
27
+ config.allowed_user_ids,
28
+ ["123456", "234567", "345678"],
29
+ )
30
+ self.assertEqual(
31
+ config.allowed_group_ids,
32
+ ["456789", "567890"],
33
+ )
34
+
35
+ def test_whitelist_accepts_json_arrays(self) -> None:
36
+ config = Config.model_validate(
37
+ {
38
+ "receipt_allowed_user_ids": '["123456", 234567, ""]',
39
+ "receipt_allowed_group_ids": [456789, "567890", ""],
40
+ }
41
+ )
42
+
43
+ self.assertEqual(config.allowed_user_ids, ["123456", "234567"])
44
+ self.assertEqual(config.allowed_group_ids, ["456789", "567890"])
45
+
46
+ def test_whitelist_accepts_nonebot_env_style_values(self) -> None:
47
+ with patch.dict(
48
+ os.environ,
49
+ {
50
+ "RECEIPT_ALLOWED_USER_IDS": "123456,234567",
51
+ "RECEIPT_ALLOWED_GROUP_IDS": "[\"345678\", \"456789\"]",
52
+ },
53
+ clear=False,
54
+ ):
55
+ values = BaseSettings._settings_build_values(
56
+ Config,
57
+ {},
58
+ env_file=None,
59
+ env_file_encoding="utf-8",
60
+ env_nested_delimiter="__",
61
+ )
62
+ config = Config.model_validate(values)
63
+
64
+ self.assertEqual(config.allowed_user_ids, ["123456", "234567"])
65
+ self.assertEqual(config.allowed_group_ids, ["345678", "456789"])
66
+
67
+
68
+ if __name__ == "__main__":
69
+ unittest.main()
@@ -1,45 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import unittest
4
-
5
- from nonebot_plugin_receipts.config import Config
6
-
7
-
8
- class ConfigTestCase(unittest.TestCase):
9
- def test_whitelist_defaults_to_empty_lists(self) -> None:
10
- config = Config()
11
-
12
- self.assertEqual(config.receipt_allowed_user_ids, [])
13
- self.assertEqual(config.receipt_allowed_group_ids, [])
14
-
15
- def test_whitelist_accepts_comma_separated_strings(self) -> None:
16
- config = Config.model_validate(
17
- {
18
- "receipt_allowed_user_ids": "123456, 234567,345678",
19
- "receipt_allowed_group_ids": "456789 567890",
20
- }
21
- )
22
-
23
- self.assertEqual(
24
- config.receipt_allowed_user_ids,
25
- ["123456", "234567", "345678"],
26
- )
27
- self.assertEqual(
28
- config.receipt_allowed_group_ids,
29
- ["456789", "567890"],
30
- )
31
-
32
- def test_whitelist_accepts_json_arrays(self) -> None:
33
- config = Config.model_validate(
34
- {
35
- "receipt_allowed_user_ids": '["123456", 234567, ""]',
36
- "receipt_allowed_group_ids": [456789, "567890", ""],
37
- }
38
- )
39
-
40
- self.assertEqual(config.receipt_allowed_user_ids, ["123456", "234567"])
41
- self.assertEqual(config.receipt_allowed_group_ids, ["456789", "567890"])
42
-
43
-
44
- if __name__ == "__main__":
45
- unittest.main()