pyview-web 0.2.4__py3-none-any.whl → 0.2.6__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.

Potentially problematic release.


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

pyview/cli/__init__.py ADDED
File without changes
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})")
pyview/cli/main.py ADDED
@@ -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()
@@ -55,10 +55,10 @@ class BaseEventHandler:
55
55
  else:
56
56
  logging.warning(f"Unhandled event: {event} {payload}")
57
57
 
58
- async def handle_info(self, info: "InfoEvent", socket):
59
- handler = self._info_handlers.get(info.name)
58
+ async def handle_info(self, event: "InfoEvent", socket):
59
+ handler = self._info_handlers.get(event.name)
60
60
 
61
61
  if handler:
62
- return await handler(self, info, socket)
62
+ return await handler(self, event, socket)
63
63
  else:
64
- logging.warning(f"Unhandled info: {info.name} {info}")
64
+ logging.warning(f"Unhandled info: {event.name} {event}")
pyview/live_socket.py CHANGED
@@ -25,8 +25,6 @@ from pyview.async_stream_runner import AsyncStreamRunner
25
25
  if TYPE_CHECKING:
26
26
  from .live_view import LiveView
27
27
 
28
- scheduler = AsyncIOScheduler()
29
- scheduler.start()
30
28
 
31
29
  pub_sub_hub = PubSubHub()
32
30
 
@@ -55,7 +53,13 @@ class ConnectedLiveViewSocket(Generic[T]):
55
53
  upload_manager: UploadManager
56
54
  prev_rendered: Optional[dict[str, Any]] = None
57
55
 
58
- def __init__(self, websocket: WebSocket, topic: str, liveview: LiveView):
56
+ def __init__(
57
+ self,
58
+ websocket: WebSocket,
59
+ topic: str,
60
+ liveview: LiveView,
61
+ scheduler: AsyncIOScheduler,
62
+ ):
59
63
  self.websocket = websocket
60
64
  self.topic = topic
61
65
  self.liveview = liveview
@@ -65,6 +69,7 @@ class ConnectedLiveViewSocket(Generic[T]):
65
69
  self.pending_events = []
66
70
  self.upload_manager = UploadManager()
67
71
  self.stream_runner = AsyncStreamRunner(self)
72
+ self.scheduler = scheduler
68
73
 
69
74
  @property
70
75
  def meta(self) -> PyViewMeta:
@@ -81,13 +86,13 @@ class ConnectedLiveViewSocket(Generic[T]):
81
86
 
82
87
  def schedule_info(self, event, seconds):
83
88
  id = f"{self.topic}:{event}"
84
- scheduler.add_job(
89
+ self.scheduler.add_job(
85
90
  self.send_info, args=[event], id=id, trigger="interval", seconds=seconds
86
91
  )
87
92
  self.scheduled_jobs.append(id)
88
93
 
89
94
  def schedule_info_once(self, event, seconds=None):
90
- scheduler.add_job(
95
+ self.scheduler.add_job(
91
96
  self.send_info,
92
97
  args=[event],
93
98
  trigger="date",
@@ -114,7 +119,7 @@ class ConnectedLiveViewSocket(Generic[T]):
114
119
  except Exception:
115
120
  for id in self.scheduled_jobs:
116
121
  print("Removing job", id)
117
- scheduler.remove_job(id)
122
+ self.scheduler.remove_job(id)
118
123
 
119
124
  async def push_patch(self, path: str, params: dict[str, Any] = {}):
120
125
  # or "replace"
@@ -156,7 +161,7 @@ class ConnectedLiveViewSocket(Generic[T]):
156
161
  async def close(self):
157
162
  self.connected = False
158
163
  for id in self.scheduled_jobs:
159
- scheduler.remove_job(id)
164
+ self.scheduler.remove_job(id)
160
165
  await self.pub_sub.unsubscribe_all_async()
161
166
 
162
167
  try:
pyview/ws_handler.py CHANGED
@@ -8,6 +8,7 @@ from pyview.csrf import validate_csrf_token
8
8
  from pyview.session import deserialize_session
9
9
  from pyview.auth import AuthProviderFactory
10
10
  from pyview.phx_message import parse_message
11
+ from apscheduler.schedulers.asyncio import AsyncIOScheduler
11
12
 
12
13
 
13
14
  class AuthException(Exception):
@@ -19,6 +20,8 @@ class LiveSocketHandler:
19
20
  self.routes = routes
20
21
  self.manager = ConnectionManager()
21
22
  self.sessions = 0
23
+ self.scheduler = AsyncIOScheduler()
24
+ self.scheduler.start()
22
25
 
23
26
  async def check_auth(self, websocket: WebSocket, lv):
24
27
  if not await AuthProviderFactory.get(lv).has_required_auth(websocket):
@@ -43,7 +46,7 @@ class LiveSocketHandler:
43
46
  url = urlparse(payload["url"])
44
47
  lv, path_params = self.routes.get(url.path)
45
48
  await self.check_auth(websocket, lv)
46
- socket = ConnectedLiveViewSocket(websocket, topic, lv)
49
+ socket = ConnectedLiveViewSocket(websocket, topic, lv, self.scheduler)
47
50
 
48
51
  session = {}
49
52
  if "session" in payload:
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pyview-web
3
- Version: 0.2.4
3
+ Version: 0.2.6
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
@@ -30,13 +29,13 @@ Classifier: Topic :: Software Development :: Libraries
30
29
  Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
31
30
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
32
31
  Classifier: Typing :: Typed
33
- Requires-Dist: APScheduler (==3.9.1.post1)
34
- Requires-Dist: itsdangerous (>=2.1.2,<3.0.0)
35
- Requires-Dist: markupsafe (>=2.1.2,<3.0.0)
36
- Requires-Dist: psutil (>=5.9.4,<6.0.0)
32
+ Requires-Dist: APScheduler (==3.11.0)
33
+ Requires-Dist: click (>=8.1.7,<9.0.0)
34
+ Requires-Dist: itsdangerous (>=2.2.0,<3.0.0)
35
+ Requires-Dist: markupsafe (>=3.0.2,<4.0.0)
37
36
  Requires-Dist: pydantic (>=2.9.2,<3.0.0)
38
- Requires-Dist: starlette (==0.40.0)
39
- Requires-Dist: uvicorn (==0.30.6)
37
+ Requires-Dist: starlette (==0.47.1)
38
+ Requires-Dist: uvicorn (==0.34.3)
40
39
  Requires-Dist: wsproto (==1.2.0)
41
40
  Project-URL: Homepage, https://pyview.rocks
42
41
  Project-URL: Repository, https://github.com/ogrodnek/pyview
@@ -8,13 +8,17 @@ pyview/auth/provider.py,sha256=fwriy2JZcOStutVXD-8VlMPAFXjILCM0l08lhTgmuyE,935
8
8
  pyview/auth/required.py,sha256=ZtNmLFth9nK39RxDiJkSzArXwS5Cvr55MUAzfJ1F2e0,1418
9
9
  pyview/changesets/__init__.py,sha256=55CLari2JHZtwy4hapHe7CqUyKjcP4dkM_t5d3CY2gU,46
10
10
  pyview/changesets/changesets.py,sha256=hImmvB_jS6RyLr5Mas5L7DO_0d805jR3c41LKJlnNL4,1720
11
+ pyview/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ pyview/cli/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
+ pyview/cli/commands/create_view.py,sha256=9hrjc1J1sJHyp3Ihi5ls4Mj19OMNeaj6fpIwDFWrdZ8,5694
14
+ pyview/cli/main.py,sha256=OEHqNwl9fqIPTI6qy5sLYndPHMEJ3fA9qsjSUuKzYSE,296
11
15
  pyview/csrf.py,sha256=VIURva9EJqXXYGC7engweh3SwDQCnHlhV2zWdcdnFqc,789
12
- pyview/events/BaseEventHandler.py,sha256=0xcjFFMLMN8Aj6toI31vzeYhRQqaX9rm-G7XGXMvqsE,1923
16
+ pyview/events/BaseEventHandler.py,sha256=rBE0z_nN6vIZ8TQ9eRfnCnTEcMOaMX7Na7YgtQmSW3s,1928
13
17
  pyview/events/__init__.py,sha256=oP0SG4Af4uf0GEa0Y_zHYhR7TcBOcXQlTAsgOSaIcC4,156
14
18
  pyview/events/info_event.py,sha256=JOwf3KDodHkmH1MzqTD8sPxs0zbI4t8Ff0rLjwRSe2Y,358
15
19
  pyview/js.py,sha256=E6HMsUfXQjrcLqYq26ieeYuzTjBeZqfJwwOm3uSR4ME,3498
16
20
  pyview/live_routes.py,sha256=IN2Jmy8b1umcfx1R7ZgFXHZNbYDJp_kLIbADtDJknPM,1749
17
- pyview/live_socket.py,sha256=p1KtX9Exwhgsf0yOp3Eb32zdUOo5hSnYDJrpJuTu3QI,5084
21
+ pyview/live_socket.py,sha256=rAa11A7hfbx7DoGC1PQnahxxgGpwOFXwQjYkR9-QtXY,5166
18
22
  pyview/live_view.py,sha256=mwAp7jiABSZCBgYF-GLQCB7zcJ7Wpz9cuC84zjzsp2U,1455
19
23
  pyview/meta.py,sha256=01Z-qldB9jrewmIJHQpUqyIhuHodQGgCvpuY9YM5R6c,74
20
24
  pyview/phx_message.py,sha256=DUdPfl6tlw9K0FNXJ35ehq03JGgynvwA_JItHQ_dxMQ,2007
@@ -44,8 +48,9 @@ pyview/vendor/ibis/nodes.py,sha256=TgFt4q5MrVW3gC3PVitrs2LyXKllRveooM7XKydNATk,2
44
48
  pyview/vendor/ibis/template.py,sha256=6XJXnztw87CrOaKeW3e18LL0fNM8AI6AaK_QgMdb7ew,2353
45
49
  pyview/vendor/ibis/tree.py,sha256=hg8f-fKHeo6DE8R-QgAhdvEaZ8rKyz7p0nGwPy0CBTs,2509
46
50
  pyview/vendor/ibis/utils.py,sha256=nLSaxPR9vMphzV9qinlz_Iurv9c49Ps6Knv8vyNlewU,2768
47
- pyview/ws_handler.py,sha256=CY1iDx5GETjIkqhgFbo2fkE3FhrqucSdg4AjuJ2P0Qg,9041
48
- pyview_web-0.2.4.dist-info/LICENSE,sha256=M_bADaBm9_MV9llX3lCicksLhwk3eZUjA2srE0uUWr0,1071
49
- pyview_web-0.2.4.dist-info/METADATA,sha256=v6WKqv3gIV-4BdP9M91sFKcCjpeg5_Beug68wgqlvrU,5256
50
- pyview_web-0.2.4.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
51
- pyview_web-0.2.4.dist-info/RECORD,,
51
+ pyview/ws_handler.py,sha256=qe_rQV5z4GTZOgMDSTC29zv8VrUJ0B5AApGdQ9GzrV0,9192
52
+ pyview_web-0.2.6.dist-info/LICENSE,sha256=M_bADaBm9_MV9llX3lCicksLhwk3eZUjA2srE0uUWr0,1071
53
+ pyview_web-0.2.6.dist-info/METADATA,sha256=Iwe5cP1CvsCCvf-2qi077zYcxFj5HhTJwtD10yZqhcM,5199
54
+ pyview_web-0.2.6.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
55
+ pyview_web-0.2.6.dist-info/entry_points.txt,sha256=GAT-ic-VYmmSMUSUVKdV1bp4w-vgEeVP-XzElvarQ9U,42
56
+ pyview_web-0.2.6.dist-info/RECORD,,
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ pv=pyview.cli.main:cli
3
+