sphinxcontrib-screenshot 0.0.2__py3-none-any.whl → 0.0.4__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 sphinxcontrib-screenshot might be problematic. Click here for more details.

@@ -19,6 +19,8 @@ import typing
19
19
  from docutils import nodes
20
20
  from docutils.parsers.rst import directives
21
21
  from docutils.statemachine import ViewList
22
+ from playwright.sync_api import Browser, Playwright
23
+ from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
22
24
  from playwright.sync_api import sync_playwright
23
25
  from sphinx.application import Sphinx
24
26
  from sphinx.util.docutils import SphinxDirective
@@ -29,8 +31,10 @@ Meta = typing.TypedDict('Meta', {
29
31
  'parallel_write_safe': bool
30
32
  })
31
33
 
32
- p = sync_playwright().start()
33
- browser = p.chromium.launch()
34
+ p: typing.Optional[Playwright] = None
35
+ browser: typing.Optional[Browser] = None
36
+
37
+ __version__ = '0.0.4'
34
38
 
35
39
 
36
40
  class ScreenshotDirective(SphinxDirective):
@@ -107,23 +111,31 @@ class ScreenshotDirective(SphinxDirective):
107
111
  filename = hashlib.md5(hash_input.encode()).hexdigest() + '.png'
108
112
  filepath = os.path.join(ss_dirpath, filename)
109
113
 
114
+ if not browser:
115
+ raise RuntimeError('Browser has not been initiated.')
116
+
110
117
  # Check if the file already exists. If not, take a screenshot
111
118
  if not os.path.exists(filepath):
112
119
  page = browser.new_page()
120
+ page.set_default_timeout(10000)
113
121
  page.set_viewport_size({'width': width, 'height': height})
114
- if screenshot_init_script:
115
- page.add_init_script(screenshot_init_script)
116
- if url_or_path.startswith(('http://', 'https://')):
117
- page.goto(url_or_path)
118
- elif os.path.isabs(url_or_path):
119
- page.goto('file://' + url_or_path)
120
- else:
121
- page.goto('file://' + str(self.env.srcdir.joinpath(url_or_path)))
122
- page.wait_for_load_state('networkidle')
123
-
124
- # Execute interactions
125
- if interactions:
126
- exec(interactions)
122
+ try:
123
+ if screenshot_init_script:
124
+ page.add_init_script(screenshot_init_script)
125
+ if url_or_path.startswith(('http://', 'https://')):
126
+ page.goto(url_or_path)
127
+ elif os.path.isabs(url_or_path):
128
+ page.goto('file://' + url_or_path)
129
+ else:
130
+ page.goto('file://' + str(self.env.srcdir.joinpath(url_or_path)))
131
+ page.wait_for_load_state('networkidle')
132
+
133
+ # Execute interactions
134
+ if interactions:
135
+ exec(interactions)
136
+ except PlaywrightTimeoutError:
137
+ raise RuntimeError('Timeout error occured at %s in executing\n%s' %
138
+ (url_or_path, interactions))
127
139
 
128
140
  page.screenshot(path=filepath)
129
141
  page.close()
@@ -145,17 +157,29 @@ class ScreenshotDirective(SphinxDirective):
145
157
  return [figure_node]
146
158
 
147
159
 
160
+ def on_builder_inited(app: Sphinx):
161
+ global p
162
+ global browser
163
+ p = sync_playwright().start()
164
+ browser = p.chromium.launch()
165
+
166
+
148
167
  def on_build_finished(app: Sphinx, exception: Exception):
149
- browser.close()
150
- p.stop()
168
+ global p
169
+ global browser
170
+ if browser:
171
+ browser.close()
172
+ if p:
173
+ p.stop()
151
174
 
152
175
 
153
176
  def setup(app: Sphinx) -> Meta:
154
177
  app.add_directive('screenshot', ScreenshotDirective)
155
178
  app.connect('build-finished', on_build_finished)
179
+ app.connect('builder-inited', on_builder_inited)
156
180
  app.add_config_value('screenshot_init_script', '', 'env')
157
181
  return {
158
- 'version': '0.0.2',
182
+ 'version': __version__,
159
183
  'parallel_read_safe': True,
160
184
  'parallel_write_safe': True,
161
185
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sphinxcontrib-screenshot
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: A Shpinx extension to embed webpage screenshots.
5
5
  Author: Shuhei Iitsuka
6
6
  Author-email: tushuhei@gmail.com
@@ -0,0 +1,6 @@
1
+ sphinxcontrib/screenshot.py,sha256=tcT_d5r0pMFBNQq2MYx7knjuWEIEySox-_O-TqEPyJE,5642
2
+ sphinxcontrib_screenshot-0.0.4.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
+ sphinxcontrib_screenshot-0.0.4.dist-info/METADATA,sha256=1u2FkF2ciErdKKVmwhPUhxEdaXUuND30QAilfvqWNMs,3208
4
+ sphinxcontrib_screenshot-0.0.4.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
5
+ sphinxcontrib_screenshot-0.0.4.dist-info/top_level.txt,sha256=VJrV3_vaiKQVgVpR0I1iecxoO0drzGu-M0j40PVP2QQ,14
6
+ sphinxcontrib_screenshot-0.0.4.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- sphinxcontrib/screenshot.py,sha256=qi351sBkCYZ5sIBr5XYtxTZnMaYOQJN_7XBSR9VFPDE,4915
2
- sphinxcontrib_screenshot-0.0.2.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
- sphinxcontrib_screenshot-0.0.2.dist-info/METADATA,sha256=y7FWmuAFeqc_9P8Sl2krRFYyscYM6jqbogo3JxjbKwk,3208
4
- sphinxcontrib_screenshot-0.0.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
5
- sphinxcontrib_screenshot-0.0.2.dist-info/top_level.txt,sha256=VJrV3_vaiKQVgVpR0I1iecxoO0drzGu-M0j40PVP2QQ,14
6
- sphinxcontrib_screenshot-0.0.2.dist-info/RECORD,,