22import glob
33import os
44
5+ import toml
6+
7+ patchright_version = os .environ .get ('playwright_version' )
58
69def patch_file (file_path : str , patched_tree : ast .AST ) -> None :
710 with open (file_path , "w" ) as f :
811 f .write (ast .unparse (ast .fix_missing_locations (patched_tree )))
912
13+ # Adding _repo_version.py (Might not be intended but fixes the build)
14+ with open ("playwright-python/playwright/_repo_version.py" , "w" ) as f :
15+ #f.write(f"version = {os.environ.get('playwright_version')}")
16+ f .write (f"version = '{ patchright_version } '" )
17+
1018# Patching pyproject.toml
1119with open ("playwright-python/pyproject.toml" , "r+" ) as f :
12- pyproject_source = f .read ()
13- pyproject_source = pyproject_source .replace ('version_file = "playwright/_repo_version.py"' , 'version_file = "patchright/_repo_version.py"' )
20+ pyproject_source = toml .load (f )
21+
22+ pyproject_source ["project" ]["name" ] = "patchright"
23+ pyproject_source ["project" ]["description" ] = "Undetected Python version of the Playwright testing and automation library."
24+ pyproject_source ["project" ]["authors" ] = [{'name' : 'Microsoft Corporation, patched by github.com/Kaliiiiiiiiii-Vinyzu/' }]
25+
26+ pyproject_source ["project" ]["urls" ]["homepage" ] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"
27+ pyproject_source ["project" ]["urls" ]["Release notes" ] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/releases"
28+ pyproject_source ["project" ]["urls" ]["Bug Reports" ] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/issues"
29+ pyproject_source ["project" ]["urls" ]["homeSource Codepage" ] = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"
30+
31+ del pyproject_source ["project" ]["scripts" ]["playwright" ]
32+ pyproject_source ["project" ]["scripts" ]["patchright" ] = "patchright.__main__:main"
33+ pyproject_source ["project" ]["entry-points" ]["pyinstaller40" ]["hook-dirs" ] = "patchright._impl.__pyinstaller:get_hook_dirs"
34+
35+ pyproject_source ["tool" ]["setuptools" ]["packages" ] = ['patchright' , 'patchright.async_api' , 'patchright.sync_api' , 'patchright._impl' , 'patchright._impl.__pyinstaller' ]
36+ pyproject_source ["tool" ]["setuptools_scm" ] = {'version_file' : 'patchright/_repo_version.py' }
37+
1438 f .seek (0 )
15- f .write (pyproject_source )
16- f .truncate ()
39+ toml .dump (pyproject_source , f )
1740
1841
1942# Patching setup.py
@@ -71,55 +94,11 @@ def patch_file(file_path: str, patched_tree: ast.AST) -> None:
7194 # Modify Setup Call
7295 if isinstance (node , ast .Call ) and isinstance (node .func , ast .Name ):
7396 if node .func .id == "setup" :
74- for keyword in node .keywords :
75- match keyword .arg :
76- case "name" :
77- keyword .value .value = "patchright"
78- case "author" :
79- keyword .value .value = "Microsoft Corportation, patched by github.com/Kaliiiiiiiiii-Vinyzu/"
80- case "description" :
81- keyword .value .value = "Undetected Python version of the Playwright testing and automation library. "
82- case "url" :
83- keyword .value .value = "https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python"
84- case "project_urls" :
85- keyword .value = ast .Dict (
86- keys = [
87- ast .Constant (value = 'Release notes' ), ast .Constant (value = 'Bug Reports' ), ast .Constant (value = 'Source Code' )
88- ],
89- values = [
90- ast .Constant (value = 'https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/releases' ),
91- ast .Constant (value = 'https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/issues' ),
92- ast .Constant (value = 'https://github.com/Kaliiiiiiiiii-Vinyzu/patchright-python/' )
93- ]
94- )
95- case "packages" :
96- keyword .value .elts = [
97- ast .Constant (value = 'patchright' ),
98- ast .Constant (value = 'patchright.async_api' ),
99- ast .Constant (value = 'patchright.sync_api' ),
100- ast .Constant (value = 'patchright._impl' ),
101- ast .Constant (value = 'patchright._impl.__pyinstaller' )
102- ]
103- case "entry_points" :
104- keyword .value = ast .Dict (
105- keys = [
106- ast .Constant (value = 'console_scripts' ),
107- ast .Constant (value = 'pyinstaller40' )
108- ],
109- values = [
110- ast .List (elts = [ast .Constant (value = 'patchright=patchright.__main__:main' )], ctx = ast .Load ()),
111- ast .List (elts = [ast .Constant (value = 'hook-dirs=patchright._impl.__pyinstaller:get_hook_dirs' )], ctx = ast .Load ())
112- ]
113- )
114-
11597 node .keywords .append (ast .keyword (
11698 arg = "version" ,
117- value =
118- # ast.parse("os.environ.get('playwright_version')'")
119- ast .parse ("os.environ.get('playwright_version') + '.post0'" )
99+ value = ast .Constant (value = patchright_version )
120100 ))
121101
122-
123102 patch_file ("playwright-python/setup.py" , setup_tree )
124103
125104# Patching playwright/_impl/_driver.py
@@ -144,6 +123,10 @@ def patch_file(file_path: str, patched_tree: ast.AST) -> None:
144123 if node .func .id == "Path" and node .args [0 ].value .id == "playwright" :
145124 node .args [0 ].value .id = "patchright"
146125
126+ elif isinstance (node , ast .Attribute ) and isinstance (node .value , ast .Attribute ) and isinstance (node .value .value , ast .Attribute ) and isinstance (node .value .value .value , ast .Name ):
127+ if node .value .value .value .id == "playwright" and node .value .value .attr == "_impl" and node .value .attr == "_impl_to_api_mapping" :
128+ node .value .value .value .id = "patchright"
129+
147130 patch_file ("playwright-python/playwright/_impl/_connection.py" , connection_source_tree )
148131
149132# Patching playwright/_impl/_js_handle.py
@@ -195,7 +178,20 @@ async def route_handler(route: Route) -> None:
195178 await route.continue_()
196179
197180 if not self.route_injecting:
198- await self.route("**/*", mapping.wrap_handler(route_handler))
181+ if self._connection._is_sync:
182+ self._routes.insert(
183+ 0,
184+ RouteHandler(
185+ self._options.get("baseURL"),
186+ "**/*",
187+ mapping.wrap_handler(route_handler),
188+ False,
189+ None,
190+ ),
191+ )
192+ await self._update_interception_patterns()
193+ else:
194+ await self.route("**/*", mapping.wrap_handler(route_handler))
199195 self.route_injecting = True""" ).body [0 ])
200196
201197 patch_file ("playwright-python/playwright/_impl/_browser_context.py" , browser_context_tree )
@@ -236,7 +232,20 @@ async def route_handler(route: Route) -> None:
236232 await route.continue_()
237233
238234 if not self.route_injecting and not self.context.route_injecting:
239- await self.route("**/*", mapping.wrap_handler(route_handler))
235+ if self._connection._is_sync:
236+ self._routes.insert(
237+ 0,
238+ RouteHandler(
239+ self._options.get("baseURL"),
240+ "**/*",
241+ mapping.wrap_handler(route_handler),
242+ False,
243+ None,
244+ ),
245+ )
246+ await self._update_interception_patterns()
247+ else:
248+ await self.route("**/*", mapping.wrap_handler(route_handler))
240249 self.route_injecting = True""" ).body [0 ])
241250
242251 patch_file ("playwright-python/playwright/_impl/_page.py" , page_tree )
0 commit comments