Skip to content

Commit bd80401

Browse files
committed
feat: add cli
1 parent d94fa0b commit bd80401

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

uiviewer/app.py renamed to uiviewer/__main__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def http_exception_handler(request: Request, exc: HTTPException):
4646
)
4747

4848

49-
def open_browser():
50-
webbrowser.open_new("http://127.0.0.1:8000")
49+
def open_browser(port):
50+
webbrowser.open_new(f"http://127.0.0.1:{port}")
5151

5252

5353
@app.get("/")
@@ -86,9 +86,13 @@ def dump_hierarchy(platform: str, serial: str):
8686
return ApiResponse.doSuccess(data)
8787

8888

89-
if __name__ == "__main__":
90-
timer = threading.Timer(1.0, open_browser)
89+
def run(port=8000):
90+
timer = threading.Timer(1.0, open_browser, args=[port])
9191
timer.daemon = True
9292
timer.start()
9393

94-
uvicorn.run(app, host="127.0.0.1", port=8000)
94+
uvicorn.run(app, host="127.0.0.1", port=port)
95+
96+
97+
if __name__ == "__main__":
98+
run()

uiviewer/_device.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

33
import abc
4-
import traceback
54
import tempfile
65
from typing import List, Dict, Union, Tuple
76
from functools import cached_property # python3.8+
@@ -13,7 +12,6 @@
1312
import wda
1413
import uiautomator2 as u2
1514
from hmdriver2 import hdc
16-
from hmdriver2.driver import Driver
1715
from fastapi import HTTPException
1816

1917
from uiviewer._utils import file_to_base64, image_to_base64
@@ -48,21 +46,21 @@ def dump_hierarchy(self) -> Dict:
4846
class HarmonyDevice(DeviceMeta):
4947
def __init__(self, serial: str):
5048
self.serial = serial
51-
self.client = Driver(serial)
49+
self.hdc = hdc.HdcWrapper(serial)
5250

5351
@cached_property
5452
def _display_size(self) -> Tuple:
55-
return self.client.display_size
53+
return self.hdc.display_size()
5654

5755
def take_screenshot(self) -> str:
5856
with tempfile.NamedTemporaryFile(delete=True, suffix=".png") as f:
5957
path = f.name
60-
self.client.screenshot(path)
58+
self.hdc.screenshot(path)
6159
return file_to_base64(path)
6260

6361
def dump_hierarchy(self) -> BaseHierarchy:
64-
packageName, pageName = self.client.current_app()
65-
raw: Dict = self.client.dump_hierarchy()
62+
packageName, pageName = self.hdc.current_app()
63+
raw: Dict = self.hdc.dump_hierarchy()
6664
hierarchy: Dict = harmony_hierarchy.convert_harmony_hierarchy(raw)
6765
return BaseHierarchy(
6866
jsonHierarchy=hierarchy,
@@ -76,7 +74,6 @@ def dump_hierarchy(self) -> BaseHierarchy:
7674
class AndroidDevice(DeviceMeta):
7775
def __init__(self, serial: str):
7876
self.serial = serial
79-
adbutils.AdbClient()
8077
self.d: u2.Device = u2.connect(serial)
8178

8279
@cached_property
@@ -115,6 +112,11 @@ def scale(self) -> int:
115112
def _window_size(self) -> Tuple:
116113
return self.client.window_size()
117114

115+
def _check_wda_health(self) -> bool:
116+
resp = request("GET", f"{self.wda_url}/status", timeout=5).json()
117+
state = resp.get("value", {}).get("state")
118+
return state == "success"
119+
118120
def take_screenshot(self) -> str:
119121
img: Image.Image = self.client.screenshot()
120122
return image_to_base64(img)
@@ -136,13 +138,8 @@ def dump_hierarchy(self) -> BaseHierarchy:
136138
scale=self.scale
137139
)
138140

139-
def wda_health(self) -> bool:
140-
resp = request("GET", f"{self.wda_url}/status", timeout=5).json()
141-
state = resp.get("value", {}).get("state")
142-
return state == "success"
143-
144141

145-
def get_device(platform: str, serial: str, wda_url: str, max_depth: int) -> Union[HarmonyDevice, AndroidDevice]:
142+
def get_device(platform: str, serial: str, wda_url: str, max_depth: int) -> Union[HarmonyDevice, AndroidDevice, IosDevice]:
146143
if platform == Platform.HARMONY:
147144
return HarmonyDevice(serial)
148145
elif platform == Platform.ANDROID:
@@ -165,9 +162,8 @@ def init_device(platform: str, serial: str, wda_url: str = None, max_depth: int
165162
cached_devices[(platform, serial)] = device
166163

167164
if isinstance(device, IosDevice):
168-
return device.wda_health()
169-
except Exception:
170-
error = traceback.format_exc()
171-
raise HTTPException(status_code=500, detail=error)
165+
return device._check_wda_health()
166+
except Exception as e:
167+
raise HTTPException(status_code=500, detail=str(e))
172168

173169
return True

uiviewer/cli.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import argparse
4+
from uiviewer.__main__ import run
5+
6+
7+
def main():
8+
parser = argparse.ArgumentParser(description="My CLI Tool")
9+
parser.add_argument('-p', '--port', type=int, default=8000, help='local listen port for uiviewer')
10+
args = parser.parse_args()
11+
run(port=args.port)
12+
13+
14+
if __name__ == "__main__":
15+
main()

uiviewer/static/js/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const API_HOST = 'http://127.0.0.1:8000/';
1+
export const API_HOST = '/';

0 commit comments

Comments
 (0)