vchrome 0.1.8__py3-none-any.whl → 0.1.10__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.
vchrome/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = '0.1.8'
1
+ __version__ = '0.1.10'
2
2
  __author__ = 'v'
3
3
  # ----------------------------------------------------------------------------------------------------
4
4
  _allowed = {'Chrome'}
@@ -15,6 +15,7 @@ import copy
15
15
  import types
16
16
  import queue
17
17
  import time
18
+ import ast
18
19
  import inspect
19
20
  import textwrap
20
21
  import threading
@@ -252,6 +253,18 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
252
253
  return data
253
254
  if is_err:
254
255
  raise Exception(is_err)
256
+ def has_print(func):
257
+ try:
258
+ if not func:
259
+ return False
260
+ code = textwrap.dedent(inspect.getsource(func))
261
+ tree = ast.parse(code)
262
+ for node in ast.walk(tree):
263
+ if isinstance(node, ast.Call):
264
+ if isinstance(node.func, ast.Name) and node.func.id == "print":
265
+ return True
266
+ except:
267
+ return False
255
268
  def is_function(obj):
256
269
  return type(obj) == types.FunctionType or type(obj) == types.MethodType
257
270
  def create_connection_saf(*a, **kw):
@@ -402,6 +415,7 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
402
415
  self.call_listen = {}
403
416
  self.call_listen_keys = []
404
417
  self.call_listen_vals = {}
418
+ self.call_listen_lock = {}
405
419
  self.tools = SniffTools(self)
406
420
  def _listen_cdp(self, sessionId):
407
421
  self.f.cdp('Network.enable',{
@@ -431,6 +445,10 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
431
445
  self.call_listen[pk] = [waiter, on_request, on_response]
432
446
  self.call_listen_keys = self.call_listen.keys()
433
447
  self.call_listen_vals[pk] = [pattern, is_regex]
448
+ # To make it easier to synchronize function printing (if there are print calls within the function).
449
+ self.call_listen_lock[pk] = 0
450
+ if has_print(on_request): self.call_listen_lock[pk] = self.call_listen_lock[pk] | 1
451
+ if has_print(on_response): self.call_listen_lock[pk] = self.call_listen_lock[pk] | 2
434
452
  return waiter
435
453
  def _handle_network(self):
436
454
  self.f.set_method_callback('Network.requestWillBeSent', self.Network_requestWillBeSent)
@@ -453,8 +471,11 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
453
471
  waiter, on_request, on_response = self.call_listen[k]
454
472
  if on_request:
455
473
  r = SniffNetwork.NetworkRequest(self.info_listen[requestId])
456
- if on_request(r):
457
- waiter.add(r)
474
+ if self.call_listen_lock[k] & 1:
475
+ with rl:
476
+ if on_request(r): waiter.add(r)
477
+ else:
478
+ if on_request(r): waiter.add(r)
458
479
  def Network_requestWillBeSentExtraInfo(self, rdata):
459
480
  request_extra = rdata['params']
460
481
  requestId = request_extra['requestId']
@@ -467,8 +488,11 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
467
488
  waiter, on_request, on_response = self.call_listen[k]
468
489
  if on_request:
469
490
  r = SniffNetwork.NetworkRequest(self.info_listen[requestId])
470
- if on_request(r):
471
- waiter.add(r)
491
+ if self.call_listen_lock[k] & 1:
492
+ with rl:
493
+ if on_request(r): waiter.add(r)
494
+ else:
495
+ if on_request(r): waiter.add(r)
472
496
  else:
473
497
  self.info_listen[requestId] = {}
474
498
  self.info_listen[requestId]['request_extra'] = request_extra
@@ -504,8 +528,11 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
504
528
  rbody = self.f.cdp('Network.getResponseBody', {"requestId": requestId}, sessionId=rdata.get('sessionId'), limit_time=3)
505
529
  self.info_listen[requestId]['response_body'] = rbody
506
530
  r = SniffNetwork.NetworkResponse(self.info_listen[requestId])
507
- if on_response(r):
508
- waiter.add(r)
531
+ if self.call_listen_lock[k] & 2:
532
+ with rl:
533
+ if on_response(r): waiter.add(r)
534
+ else:
535
+ if on_response(r): waiter.add(r)
509
536
  self.info_listen.pop(requestId, None)
510
537
  self.qlist.get('V')
511
538
  def Network_loadingFailed(self, rdata):
@@ -682,6 +709,7 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
682
709
  self.call_change = {}
683
710
  self.call_change_keys = []
684
711
  self.call_change_vals = {}
712
+ self.call_change_lock = {}
685
713
  self.tools = SniffTools(self)
686
714
  def _change_cdp(self, sessionId):
687
715
  self.f.cdp('Fetch.enable',{
@@ -712,6 +740,10 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
712
740
  self.call_change[pk] = [waiter, on_request, on_response]
713
741
  self.call_change_keys = self.call_change.keys()
714
742
  self.call_change_vals[pk] = [pattern, is_regex]
743
+ # To make it easier to synchronize function printing (if there are print calls within the function).
744
+ self.call_change_lock[pk] = 0
745
+ if has_print(on_request): self.call_change_lock[pk] = self.call_change_lock[pk] | 1
746
+ if has_print(on_response): self.call_change_lock[pk] = self.call_change_lock[pk] | 2
715
747
  return waiter
716
748
  def _handle_fetch(self):
717
749
  self.f.set_method_callback('Fetch.requestPaused', self.Fetch_requestPaused)
@@ -738,8 +770,11 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
738
770
  })
739
771
  if on_request:
740
772
  try:
741
- if on_request(x):
742
- waiter.add(x)
773
+ if self.call_change_lock[k] & 1:
774
+ with rl:
775
+ if on_request(x): waiter.add(x)
776
+ else:
777
+ if on_request(x): waiter.add(x)
743
778
  except:
744
779
  self.f.logger.log('[ERROR] in request on_request', traceback.format_exc())
745
780
  continue
@@ -771,6 +806,15 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
771
806
  self.f.logger.log('[ERROR] in request on_request continue', traceback.format_exc())
772
807
  self.f.cdp('Fetch.continueRequest', {'requestId': requestId}, sessionId=rdata.get('sessionId'))
773
808
  else:
809
+ # request: Redirect.
810
+ if 'responseHeaders' in rdata['params'] and type(rdata['params']['responseHeaders']) == list:
811
+ for kv in rdata['params']['responseHeaders']:
812
+ if kv['name'] == 'Location':
813
+ return self.f.cdp('Fetch.continueRequest', {'requestId': requestId, 'url':kv['value']}, sessionId=rdata.get('sessionId'))
814
+ # request: fail Request.
815
+ if 'responseErrorReason' in rdata['params']:
816
+ return self.f.cdp('Fetch.failRequest', {'requestId': requestId, 'errorReason':rdata['params']['responseErrorReason']}, sessionId=rdata.get('sessionId'))
817
+ # response
774
818
  for k in self.call_change_keys:
775
819
  if self._match_url(self.call_change_vals[k], url):
776
820
  waiter, on_request, on_response = self.call_change[k]
@@ -785,8 +829,11 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
785
829
  "request": SniffFetch.FetchRequest(rdata['params']['request'])
786
830
  })
787
831
  try:
788
- if on_response(x):
789
- waiter.add(x)
832
+ if self.call_change_lock[k] & 2:
833
+ with rl:
834
+ if on_response(x): waiter.add(x)
835
+ else:
836
+ if on_response(x): waiter.add(x)
790
837
  except:
791
838
  self.f.logger.log('[ERROR] in response on_response', traceback.format_exc())
792
839
  continue
@@ -1072,16 +1119,16 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
1072
1119
  return self.f.cdp('DOM.getBoxModel', {'objectId': self.objectId})
1073
1120
  def __repr__(self):
1074
1121
  return '<DOM:[{}] [{}]>'.format(self.className, self.objectId)
1075
- def clear(self, delay_uptime=0.15, timegap=0.01):
1076
- self.wait_show()
1122
+ def clear(self, delay_uptime=0.15, timegap=0.01, timeout=15):
1123
+ self.wait_show(timeout)
1077
1124
  self.f.cdp('DOM.focus', {"objectId": self.objectId})
1078
1125
  clear_ents = self.f.rootf.make_clear()
1079
1126
  for kdn in clear_ents[:2]: time.sleep(timegap); self.f.cdp('Input.dispatchKeyEvent', kdn)
1080
1127
  time.sleep(delay_uptime)
1081
1128
  for kup in clear_ents[2:4]: time.sleep(timegap); self.f.cdp('Input.dispatchKeyEvent', kup)
1082
1129
  for delt in clear_ents[4:]: time.sleep(timegap); self.f.cdp('Input.dispatchKeyEvent', delt)
1083
- def input(self, str, delay_time=0.02, delay_uptime=0.15, random_gap=0.01):
1084
- self.wait_show()
1130
+ def input(self, str, delay_time=0.02, delay_uptime=0.15, random_gap=0.01, timeout=15):
1131
+ self.wait_show(timeout)
1085
1132
  self.f.cdp('DOM.focus', {"objectId": self.objectId})
1086
1133
  for kdn, kup in self.f.rootf.make_input_events(str):
1087
1134
  if kdn == 'insertText':
@@ -1112,8 +1159,8 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
1112
1159
  return _x, _y
1113
1160
  def _set_in_view(self):
1114
1161
  self.f.cdp('DOM.scrollIntoViewIfNeeded', {"objectId": self.objectId})
1115
- def click(self, x=1, y=2, zero='center', button='left', count=1):
1116
- self.wait_show()
1162
+ def click(self, x=1, y=2, zero='center', button='left', count=1, timeout=15):
1163
+ self.wait_show(timeout)
1117
1164
  self._set_in_view()
1118
1165
  _x, _y = self._get_xy(x, y, zero)
1119
1166
  self.f.cdp('Input.dispatchMouseEvent',{"buttons":0,"type":"mouseMoved","x":_x,"y":_y,"button":"none","clickCount":0})
@@ -1146,7 +1193,7 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
1146
1193
  y += points[i][1]*B
1147
1194
  r.append([x, y])
1148
1195
  return r
1149
- def wait_show(self, timeout=5, pretime=0.1):
1196
+ def wait_show(self, timeout=15, pretime=0.1):
1150
1197
  start = perf_counter()
1151
1198
  count, acount = 0, 0
1152
1199
  px, py = 0, 0
@@ -1168,8 +1215,8 @@ def cdp_client(hostname, port, debug=False, runtimeEnable=False, cfg={}):
1168
1215
  time.sleep(0.08)
1169
1216
  if pretime:
1170
1217
  time.sleep(pretime) # When the component appears, the event might not have been attached yet. Please wait a moment.
1171
- def drag(self, x=1, y=2, zero='center', button='left', count=1):
1172
- self.wait_show()
1218
+ def drag(self, x=1, y=2, zero='center', button='left', count=1, timeout=15):
1219
+ self.wait_show(timeout)
1173
1220
  self._set_in_view()
1174
1221
  x, y = self._get_xy(x, y, zero)
1175
1222
  self.cache_xy = [x, y]
@@ -2805,6 +2852,7 @@ class Chrome:
2805
2852
  userAgent = property(lambda s:s.get_userAgent(), lambda s,v:s.set_userAgent(v))
2806
2853
  languages = property(lambda s:s.get_languages(), lambda s,v:s.set_languages(v))
2807
2854
  platform = property(lambda s:s.get_platform(), lambda s,v:s.set_platform(v))
2855
+ html = property(lambda s:s['document']['documentElement']['outerHTML'])
2808
2856
  return Chrome(self.root, self.dr.root._new_driver(not foreground))
2809
2857
  frames = property(lambda s:s.dr.frames)
2810
2858
  cookies = property(lambda s:s.get_cookies(), lambda s,v:s.set_cookies(v))
@@ -2812,6 +2860,7 @@ class Chrome:
2812
2860
  userAgent = property(lambda s:s.get_userAgent(), lambda s,v:s.set_userAgent(v))
2813
2861
  languages = property(lambda s:s.get_languages(), lambda s,v:s.set_languages(v))
2814
2862
  platform = property(lambda s:s.get_platform(), lambda s,v:s.set_platform(v))
2863
+ html = property(lambda s:s['document']['documentElement']['outerHTML'])
2815
2864
  def _check_lower_version(self):
2816
2865
  try:
2817
2866
  version = self.dr.root.version
@@ -2833,6 +2882,7 @@ class Chrome:
2833
2882
  def _connect(self):
2834
2883
  v_cfg = {}
2835
2884
  v_cfg['headless'] = self.headless
2885
+ v_cfg['proxy'] = self.proxy
2836
2886
  v_cfg_fpath = self.user_path / 'v_config.json'
2837
2887
  if not is_port_open(self.port):
2838
2888
  self._init()
@@ -2841,8 +2891,9 @@ class Chrome:
2841
2891
  v_cfg = self.loadSaveF(v_cfg_fpath, v_cfg)
2842
2892
  else:
2843
2893
  self.writeSaveF(v_cfg_fpath, v_cfg)
2844
- if v_cfg['headless'] != self.headless:
2894
+ if v_cfg.get('headless', 'v') != self.headless or v_cfg.get('proxy', 'v') != self.proxy:
2845
2895
  v_cfg['headless'] = self.headless
2896
+ v_cfg['proxy'] = self.proxy
2846
2897
  self.root = cdp_client(self.hostname, port=self.port, debug=self.debug, runtimeEnable=self.runtimeEnable, cfg=v_cfg)
2847
2898
  self.root.active.quit()
2848
2899
  self._init()
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vchrome
3
- Version: 0.1.8
3
+ Version: 0.1.10
4
4
 
@@ -0,0 +1,6 @@
1
+ v/__init__.py,sha256=6ZddLaDcwLudHWmFr2utAcYhperXw9AYoItNPm8EZSM,65
2
+ vchrome/__init__.py,sha256=aMD6Ua-33LnoLUcyX5mu7FKffntOAWa84b2GpQyExec,230224
3
+ vchrome-0.1.10.dist-info/METADATA,sha256=1cFcdEsGP3QhKOSGQbkP1RM_AjwxrIDXLgNeb89C2Ng,57
4
+ vchrome-0.1.10.dist-info/WHEEL,sha256=hPN0AlP2dZM_3ZJZWP4WooepkmU9wzjGgCLCeFjkHLA,92
5
+ vchrome-0.1.10.dist-info/top_level.txt,sha256=IYbkbnFb2FGoroYj_ZXIitmnJDgYBA29_HmK9BdEUkY,10
6
+ vchrome-0.1.10.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- v/__init__.py,sha256=6ZddLaDcwLudHWmFr2utAcYhperXw9AYoItNPm8EZSM,65
2
- vchrome/__init__.py,sha256=3Y9tBJV5R1aId75qSUdN0PKGrTmoYgE7jz4VobzOCQQ,226977
3
- vchrome-0.1.8.dist-info/METADATA,sha256=hk-Kct-6jTMpTF4sQAy_daHA6HNkBaBXYoVC18WdVYw,56
4
- vchrome-0.1.8.dist-info/WHEEL,sha256=hPN0AlP2dZM_3ZJZWP4WooepkmU9wzjGgCLCeFjkHLA,92
5
- vchrome-0.1.8.dist-info/top_level.txt,sha256=IYbkbnFb2FGoroYj_ZXIitmnJDgYBA29_HmK9BdEUkY,10
6
- vchrome-0.1.8.dist-info/RECORD,,