pyview-web 0.2.3__tar.gz → 0.2.5__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.

Potentially problematic release.


This version of pyview-web might be problematic. Click here for more details.

Files changed (57) hide show
  1. {pyview_web-0.2.3 → pyview_web-0.2.5}/PKG-INFO +3 -3
  2. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyproject.toml +6 -3
  3. pyview_web-0.2.5/pyview/async_stream_runner.py +65 -0
  4. pyview_web-0.2.5/pyview/cli/commands/__init__.py +0 -0
  5. pyview_web-0.2.5/pyview/cli/commands/create_view.py +199 -0
  6. pyview_web-0.2.5/pyview/cli/main.py +17 -0
  7. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/events/BaseEventHandler.py +28 -2
  8. pyview_web-0.2.5/pyview/events/__init__.py +4 -0
  9. pyview_web-0.2.5/pyview/events/info_event.py +16 -0
  10. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/live_socket.py +2 -0
  11. pyview_web-0.2.5/pyview/vendor/__init__.py +0 -0
  12. pyview_web-0.2.3/pyview/events/__init__.py +0 -4
  13. pyview_web-0.2.3/pyview/events/info_event.py +0 -8
  14. {pyview_web-0.2.3 → pyview_web-0.2.5}/LICENSE +0 -0
  15. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/__init__.py +0 -0
  16. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/assets/js/app.js +0 -0
  17. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/assets/package-lock.json +0 -0
  18. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/assets/package.json +0 -0
  19. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/auth/__init__.py +0 -0
  20. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/auth/provider.py +0 -0
  21. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/auth/required.py +0 -0
  22. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/changesets/__init__.py +0 -0
  23. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/changesets/changesets.py +0 -0
  24. {pyview_web-0.2.3/pyview/vendor → pyview_web-0.2.5/pyview/cli}/__init__.py +0 -0
  25. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/csrf.py +0 -0
  26. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/js.py +0 -0
  27. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/live_routes.py +0 -0
  28. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/live_view.py +0 -0
  29. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/meta.py +0 -0
  30. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/phx_message.py +0 -0
  31. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/pyview.py +0 -0
  32. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/secret.py +0 -0
  33. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/session.py +0 -0
  34. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/static/assets/app.js +0 -0
  35. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/__init__.py +0 -0
  36. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/context_processor.py +0 -0
  37. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/live_template.py +0 -0
  38. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/render_diff.py +0 -0
  39. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/root_template.py +0 -0
  40. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/serializer.py +0 -0
  41. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/template/utils.py +0 -0
  42. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/test_csrf.py +0 -0
  43. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/uploads.py +0 -0
  44. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/flet/pubsub/__init__.py +0 -0
  45. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/flet/pubsub/pub_sub.py +0 -0
  46. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/__init__.py +0 -0
  47. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/compiler.py +0 -0
  48. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/context.py +0 -0
  49. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/errors.py +0 -0
  50. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/filters.py +0 -0
  51. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/loaders.py +0 -0
  52. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/nodes.py +0 -0
  53. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/template.py +0 -0
  54. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/tree.py +0 -0
  55. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/vendor/ibis/utils.py +0 -0
  56. {pyview_web-0.2.3 → pyview_web-0.2.5}/pyview/ws_handler.py +0 -0
  57. {pyview_web-0.2.3 → pyview_web-0.2.5}/readme.md +0 -0
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pyview-web
3
- Version: 0.2.3
3
+ Version: 0.2.5
4
4
  Summary: LiveView in Python
5
5
  License: MIT
6
6
  Keywords: web,api,LiveView
7
7
  Author: Larry Ogrodnek
8
8
  Author-email: ogrodnek@gmail.com
9
- Requires-Python: >=3.10,<3.13
9
+ Requires-Python: >=3.11,<3.13
10
10
  Classifier: Development Status :: 4 - Beta
11
11
  Classifier: Environment :: Web Environment
12
12
  Classifier: Framework :: AsyncIO
@@ -18,7 +18,6 @@ Classifier: License :: OSI Approved :: MIT License
18
18
  Classifier: Operating System :: OS Independent
19
19
  Classifier: Programming Language :: Python
20
20
  Classifier: Programming Language :: Python :: 3
21
- Classifier: Programming Language :: Python :: 3.10
22
21
  Classifier: Programming Language :: Python :: 3.11
23
22
  Classifier: Programming Language :: Python :: 3.12
24
23
  Classifier: Programming Language :: Python :: 3 :: Only
@@ -31,6 +30,7 @@ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
31
30
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
32
31
  Classifier: Typing :: Typed
33
32
  Requires-Dist: APScheduler (==3.9.1.post1)
33
+ Requires-Dist: click (>=8.1.7,<9.0.0)
34
34
  Requires-Dist: itsdangerous (>=2.1.2,<3.0.0)
35
35
  Requires-Dist: markupsafe (>=2.1.2,<3.0.0)
36
36
  Requires-Dist: psutil (>=5.9.4,<6.0.0)
@@ -5,7 +5,7 @@ packages = [
5
5
  { include = "pyview" },
6
6
  ]
7
7
 
8
- version = "0.2.3"
8
+ version = "0.2.5"
9
9
  description = "LiveView in Python"
10
10
  authors = ["Larry Ogrodnek <ogrodnek@gmail.com>"]
11
11
  license = "MIT"
@@ -33,7 +33,6 @@ classifiers = [
33
33
  "Intended Audience :: Developers",
34
34
  "License :: OSI Approved :: MIT License",
35
35
  "Programming Language :: Python :: 3 :: Only",
36
- "Programming Language :: Python :: 3.10",
37
36
  "Programming Language :: Python :: 3.11",
38
37
  "Programming Language :: Python :: 3.12",
39
38
  "Topic :: Internet :: WWW/HTTP :: HTTP Servers",
@@ -41,7 +40,7 @@ classifiers = [
41
40
  ]
42
41
 
43
42
  [tool.poetry.dependencies]
44
- python = ">=3.10,<3.13"
43
+ python = ">=3.11,<3.13"
45
44
  starlette = "0.40.0"
46
45
  uvicorn = "0.30.6"
47
46
  wsproto = "1.2.0"
@@ -50,6 +49,10 @@ psutil = "^5.9.4"
50
49
  markupsafe = "^2.1.2"
51
50
  itsdangerous = "^2.1.2"
52
51
  pydantic = "^2.9.2"
52
+ click = "^8.1.7"
53
+
54
+ [tool.poetry.scripts]
55
+ pv = "pyview.cli.main:cli"
53
56
 
54
57
  [tool.poetry.group.dev.dependencies]
55
58
  pytest = "^7.2.0"
@@ -0,0 +1,65 @@
1
+ import asyncio
2
+ import uuid
3
+ import logging
4
+ from typing import Any, AsyncGenerator, Callable, Optional
5
+ from pyview.events.info_event import InfoEvent, InfoEventScheduler
6
+
7
+
8
+ class AsyncStreamRunner:
9
+ def __init__(self, scheduler: InfoEventScheduler):
10
+ self._stream_tasks: dict[str, asyncio.Task] = {}
11
+ self._scheduler = scheduler
12
+
13
+ def start_stream(
14
+ self,
15
+ gen: AsyncGenerator[Any, None],
16
+ *,
17
+ on_yield: Callable[[Any], InfoEvent],
18
+ on_done: Optional[InfoEvent] = None,
19
+ on_error: Optional[Callable[[Exception], InfoEvent]] = None,
20
+ on_cancel: Optional[InfoEvent] = None,
21
+ ) -> str:
22
+ """
23
+ Run `gen` in the background, returning an op_id you can later use
24
+ to cancel. Hooks:
25
+
26
+ - on_yield(item) → scheduled per chunk
27
+ - on_done → scheduled once at normal completion
28
+ - on_error(exc) → scheduled on unexpected exception
29
+ - on_cancel → scheduled if the task is cancelled
30
+ """
31
+ task_id = uuid.uuid4().hex
32
+
33
+ async def driver():
34
+ try:
35
+ async for item in gen:
36
+ self._scheduler.schedule_info_once(on_yield(item))
37
+ except asyncio.CancelledError:
38
+ # user-requested cancellation
39
+ if on_cancel:
40
+ self._scheduler.schedule_info_once(on_cancel)
41
+ # swallow so it doesn’t log as an “error”
42
+ except Exception as exc:
43
+ if on_error:
44
+ self._scheduler.schedule_info_once(on_error(exc))
45
+ else:
46
+ logging.exception(f"Error in stream {task_id}", exc_info=True)
47
+ else:
48
+ if on_done:
49
+ self._scheduler.schedule_info_once(on_done)
50
+ finally:
51
+ self._stream_tasks.pop(task_id, None)
52
+
53
+ task = asyncio.create_task(driver())
54
+ self._stream_tasks[task_id] = task
55
+ return task_id
56
+
57
+ def cancel_stream(self, task_id: str) -> bool:
58
+ """
59
+ Cancel a running stream. Returns True if a task was found & cancelled.
60
+ """
61
+ task = self._stream_tasks.get(task_id)
62
+ if not task:
63
+ return False
64
+ task.cancel()
65
+ return True
File without changes
@@ -0,0 +1,199 @@
1
+ import click
2
+ from pathlib import Path
3
+ import tomllib
4
+ from typing import Optional
5
+
6
+
7
+ def snake_case(name: str) -> str:
8
+ """Convert PascalCase or camelCase to snake_case."""
9
+ import re
10
+
11
+ s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
12
+ return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
13
+
14
+
15
+ def pascal_case(name: str) -> str:
16
+ """Convert snake_case or other formats to PascalCase."""
17
+ # If already in PascalCase or camelCase, convert to snake_case first
18
+ snake_name = snake_case(name)
19
+ return "".join(word.capitalize() for word in snake_name.split("_"))
20
+
21
+
22
+ def kebab_case(name: str) -> str:
23
+ """Convert any case to kebab-case."""
24
+ return snake_case(name).replace("_", "-")
25
+
26
+
27
+ def generate_python_file(name: str) -> str:
28
+ """Generate the Python LiveView file content."""
29
+ class_name = f"{pascal_case(name)}LiveView"
30
+ context_name = f"{pascal_case(name)}Context"
31
+
32
+ return f"""from pyview import LiveView, LiveViewSocket
33
+ from dataclasses import dataclass
34
+ from pyview.events import event, BaseEventHandler
35
+
36
+
37
+ @dataclass
38
+ class {context_name}:
39
+ pass
40
+
41
+
42
+ class {class_name}(BaseEventHandler, LiveView[{context_name}]):
43
+ async def mount(self, socket: LiveViewSocket[{context_name}], session):
44
+ socket.context = {context_name}()
45
+ """
46
+
47
+
48
+ def generate_html_file(name: str) -> str:
49
+ """Generate the HTML template file content."""
50
+ css_class = kebab_case(name)
51
+ return f'''<div class="{css_class}-container">
52
+ <h1>{pascal_case(name)}</h1>
53
+
54
+ <div class="content">
55
+ <!-- Add your content here -->
56
+ </div>
57
+ </div>
58
+ '''
59
+
60
+
61
+ def generate_css_file(name: str) -> str:
62
+ """Generate the CSS file content."""
63
+ css_class = kebab_case(name)
64
+ return f""".{css_class}-container {{
65
+ padding: 1rem;
66
+ }}
67
+
68
+ .{css_class}-container h1 {{
69
+ margin-bottom: 1rem;
70
+ }}
71
+
72
+ .{css_class}-container .content {{
73
+ /* Add your styles here */
74
+ }}
75
+ """
76
+
77
+
78
+ def generate_init_file(name: str) -> str:
79
+ """Generate the __init__.py file content."""
80
+ module_name = snake_case(name)
81
+ class_name = f"{pascal_case(name)}LiveView"
82
+
83
+ return f'''from .{module_name} import {class_name}
84
+
85
+ __all__ = ["{class_name}"]
86
+ '''
87
+
88
+
89
+ def detect_package_structure(directory: Optional[Path] = None):
90
+ """Detect the package structure from pyproject.toml.
91
+
92
+ Args:
93
+ directory: Directory to look for pyproject.toml in. Defaults to current directory.
94
+
95
+ Returns:
96
+ tuple: (package_name, views_path)
97
+ """
98
+ if directory is None:
99
+ directory = Path.cwd()
100
+
101
+ pyproject_path = directory / "pyproject.toml"
102
+
103
+ if not pyproject_path.exists():
104
+ return None, "views"
105
+
106
+ try:
107
+ with open(pyproject_path, "rb") as f:
108
+ config = tomllib.load(f)
109
+ except Exception:
110
+ return None, "views"
111
+
112
+ # Check for packages configuration
113
+ packages = config.get("tool", {}).get("poetry", {}).get("packages", [])
114
+ if not packages:
115
+ # Check for modern pyproject.toml structure
116
+ packages = config.get("project", {}).get("packages", [])
117
+
118
+ if not packages:
119
+ return None, "views"
120
+
121
+ # Find the first package entry
122
+ for package in packages:
123
+ if isinstance(package, dict) and "include" in package:
124
+ package_name = package["include"]
125
+ from_dir = package.get("from", ".")
126
+
127
+ # Construct the path where views should go
128
+ if from_dir == ".":
129
+ package_path = Path(package_name)
130
+ else:
131
+ package_path = Path(from_dir) / package_name
132
+
133
+ views_path = package_path / "views"
134
+ return package_name, str(views_path)
135
+
136
+ return None, "views"
137
+
138
+
139
+ @click.command()
140
+ @click.argument("name")
141
+ @click.option(
142
+ "--path",
143
+ "-p",
144
+ default=None,
145
+ help="Directory to create the view in (default: auto-detect from pyproject.toml)",
146
+ )
147
+ @click.option("--no-css", is_flag=True, help="Skip creating CSS file")
148
+ def create_view(name: str, path: Optional[str], no_css: bool):
149
+ """Create a new LiveView with boilerplate files.
150
+
151
+ Example: pv create-view Counter
152
+ """
153
+ module_name = snake_case(name)
154
+
155
+ # Always try to detect package structure for import advice
156
+ package_name, detected_path = detect_package_structure()
157
+
158
+ # Use detected path if no path specified, otherwise use provided path
159
+ if path is None:
160
+ path = detected_path
161
+
162
+ view_dir = Path(path) / module_name
163
+
164
+ # Check if directory already exists
165
+ if view_dir.exists():
166
+ click.secho(f"Error: Directory '{view_dir}' already exists", fg="red")
167
+ raise click.Abort()
168
+
169
+ view_dir.mkdir(parents=True, exist_ok=True)
170
+ click.secho(f"Created directory: {view_dir}", fg="green")
171
+
172
+ # Generate files
173
+ files_to_create = [
174
+ ("__init__.py", generate_init_file(name)),
175
+ (f"{module_name}.py", generate_python_file(name)),
176
+ (f"{module_name}.html", generate_html_file(name)),
177
+ ]
178
+
179
+ if not no_css:
180
+ files_to_create.append((f"{module_name}.css", generate_css_file(name)))
181
+
182
+ # Create files
183
+ for filename, content in files_to_create:
184
+ file_path = view_dir / filename
185
+ file_path.write_text(content)
186
+ click.secho(f"Created: {file_path}", fg="green")
187
+
188
+ class_name = f"{pascal_case(name)}LiveView"
189
+ click.echo("\nLiveView created successfully! 🎉")
190
+ click.echo("\nTo use this view, add it to your app:")
191
+
192
+ # import statement based on detected package structure
193
+ if package_name:
194
+ import_path = f"{package_name}.views.{module_name}"
195
+ else:
196
+ import_path = f"{path.replace('/', '.')}.{module_name}"
197
+
198
+ click.echo(f" from {import_path} import {class_name}")
199
+ click.echo(f" app.add_live_view('/{module_name}', {class_name})")
@@ -0,0 +1,17 @@
1
+ import click
2
+
3
+ from pyview.cli.commands.create_view import create_view
4
+
5
+
6
+ @click.group()
7
+ @click.version_option(package_name="pyview-web")
8
+ def cli():
9
+ """PyView CLI - Generate boilerplate for LiveView applications."""
10
+ pass
11
+
12
+
13
+ cli.add_command(create_view)
14
+
15
+
16
+ if __name__ == "__main__":
17
+ cli()
@@ -1,6 +1,9 @@
1
- from typing import Callable
1
+ from typing import Callable, TYPE_CHECKING
2
2
  import logging
3
3
 
4
+ if TYPE_CHECKING:
5
+ from pyview.live_view import InfoEvent
6
+
4
7
 
5
8
  def event(*event_names):
6
9
  """Decorator that marks methods as event handlers."""
@@ -12,22 +15,37 @@ def event(*event_names):
12
15
  return decorator
13
16
 
14
17
 
18
+ def info(*info_names):
19
+ """Decorator that marks methods as info handlers."""
20
+
21
+ def decorator(func):
22
+ func._info_names = info_names
23
+ return func
24
+
25
+ return decorator
26
+
27
+
15
28
  class BaseEventHandler:
16
- """Base class for event handlers to handle dispatching events."""
29
+ """Base class for event handlers to handle dispatching events and info."""
17
30
 
18
31
  _event_handlers: dict[str, Callable] = {}
32
+ _info_handlers: dict[str, Callable] = {}
19
33
 
20
34
  def __init_subclass__(cls, **kwargs):
21
35
  super().__init_subclass__(**kwargs)
22
36
 
23
37
  # Find all decorated methods and register them
24
38
  cls._event_handlers = {}
39
+ cls._info_handlers = {}
25
40
  for attr_name in dir(cls):
26
41
  if not attr_name.startswith("_"):
27
42
  attr = getattr(cls, attr_name)
28
43
  if hasattr(attr, "_event_names"):
29
44
  for event_name in attr._event_names:
30
45
  cls._event_handlers[event_name] = attr
46
+ if hasattr(attr, "_info_names"):
47
+ for info_name in attr._info_names:
48
+ cls._info_handlers[info_name] = attr
31
49
 
32
50
  async def handle_event(self, event: str, payload: dict, socket):
33
51
  handler = self._event_handlers.get(event)
@@ -36,3 +54,11 @@ class BaseEventHandler:
36
54
  return await handler(self, event, payload, socket)
37
55
  else:
38
56
  logging.warning(f"Unhandled event: {event} {payload}")
57
+
58
+ async def handle_info(self, info: "InfoEvent", socket):
59
+ handler = self._info_handlers.get(info.name)
60
+
61
+ if handler:
62
+ return await handler(self, info, socket)
63
+ else:
64
+ logging.warning(f"Unhandled info: {info.name} {info}")
@@ -0,0 +1,4 @@
1
+ from .BaseEventHandler import BaseEventHandler, event, info
2
+ from .info_event import InfoEvent
3
+
4
+ __all__ = ["BaseEventHandler", "event", "info", "InfoEvent"]
@@ -0,0 +1,16 @@
1
+ from dataclasses import dataclass
2
+ from typing import Any, Optional, Protocol
3
+
4
+
5
+ @dataclass
6
+ class InfoEvent:
7
+ name: str
8
+ payload: Any = None
9
+
10
+
11
+ class InfoEventScheduler(Protocol):
12
+ def schedule_info(self, event: InfoEvent, seconds: float):
13
+ pass
14
+
15
+ def schedule_info_once(self, event: InfoEvent, seconds: Optional[float] = None):
16
+ pass
@@ -19,6 +19,7 @@ from pyview.uploads import UploadConstraints, UploadConfig, UploadManager
19
19
  from pyview.meta import PyViewMeta
20
20
  from pyview.template.render_diff import calc_diff
21
21
  import datetime
22
+ from pyview.async_stream_runner import AsyncStreamRunner
22
23
 
23
24
 
24
25
  if TYPE_CHECKING:
@@ -63,6 +64,7 @@ class ConnectedLiveViewSocket(Generic[T]):
63
64
  self.pub_sub = PubSub(pub_sub_hub, topic)
64
65
  self.pending_events = []
65
66
  self.upload_manager = UploadManager()
67
+ self.stream_runner = AsyncStreamRunner(self)
66
68
 
67
69
  @property
68
70
  def meta(self) -> PyViewMeta:
File without changes
@@ -1,4 +0,0 @@
1
- from .BaseEventHandler import BaseEventHandler, event
2
- from .info_event import InfoEvent
3
-
4
- __all__ = ["BaseEventHandler", "event", "InfoEvent"]
@@ -1,8 +0,0 @@
1
- from dataclasses import dataclass
2
- from typing import Any
3
-
4
-
5
- @dataclass
6
- class InfoEvent:
7
- name: str
8
- payload: Any = None
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes