Skip to content

Commit

Permalink
修复导出错误会中断其他prefab问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kaluluosi committed Sep 26, 2024
1 parent ec7fb13 commit 1e04cac
Show file tree
Hide file tree
Showing 6 changed files with 953 additions and 1,407 deletions.
2,268 changes: 907 additions & 1,361 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "robotframework-airtest"
version = "0.2.0"
version = "0.3.2"
description = "网易Airtest/pocoui的Robotframework测试库"
authors = ["kaluluosi <[email protected]>"]
license = "MIT"
Expand Down
13 changes: 9 additions & 4 deletions robotframework_airtest/cli/vmg.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Callable
import click
import os

import logging
import glob
from typing import Callable
from .util import title
from robotframework_airtest.vmg import get_valid_generator_names, get_valid_generators
from robotframework_airtest.vmg.settings import Setting, Settings

logger = logging.getLogger(__name__)

@click.group()
def vmg():
Expand Down Expand Up @@ -53,7 +54,11 @@ def gen():
outpath = file.replace(source, dist)
# 修改扩展名
output = os.path.splitext(outpath)[0] + ".resource"
generator(file, output, setting)
try:
generator(file, output, setting)
except Exception as e:
logger.error(e, exc_info=e)



@vmg.command
Expand Down Expand Up @@ -94,7 +99,7 @@ def init(default: bool):
vm_generator: str = click.prompt(
">选择导出器",
default="unity",
type=click.Choice(get_valid_generators().keys()),
type=click.Choice(list(get_valid_generators().keys())),
show_default=True,
show_choices=True,
)
Expand Down
10 changes: 4 additions & 6 deletions robotframework_airtest/vmg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import glob
import os
from importlib import metadata
from typing import Optional
from typing import Dict, Optional
from .generatorbase import GeneratorFunc, LoadError
from .logger import logger # noqa
from .settings import Setting


def get_valid_generators() -> dict:
def get_valid_generators() -> Dict[str, GeneratorFunc]:
entrypoints = metadata.entry_points().get(
"robotframework_airtest.vmg.generator", []
)
Expand All @@ -17,7 +17,7 @@ def get_valid_generators() -> dict:
generator = ep.load()
all_generators[ep.name] = generator
except Exception as e:
logger.error(f"{ep.__name__} {e}")
logger.error(f"{ep.name} {e}")

return all_generators

Expand All @@ -35,9 +35,7 @@ def gen_viewmodel(setting: Setting, name: Optional[str] = None):
installed_generators = get_valid_generators()
if generator_name not in installed_generators:
logger.info(
f">{generator_name} 导出器没有安装,目前安装的导出器只有{get_valid_generator_names()}",
err=True,
)
f">{generator_name} 导出器没有安装,目前安装的导出器只有{get_valid_generator_names()}",)
logger.info(f">配置 {name} 无法处理")
else:
generator: GeneratorFunc = installed_generators[generator_name]
Expand Down
55 changes: 26 additions & 29 deletions robotframework_airtest/vmg/generatorbase.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from abc import ABC, abstractmethod
from enum import Enum
from typing import Callable, Dict, Sequence, Set
from typing import Callable, Dict, Optional, Sequence, Set
from .settings import Setting

from jinja2 import Template
import pkg_resources
Expand All @@ -27,7 +28,7 @@
)

ActionMap = Dict
GeneratorFunc = Callable[[str, str, dict], None]
GeneratorFunc = Callable[[str, str, Setting], None]


class ControlType(Enum):
Expand Down Expand Up @@ -68,13 +69,14 @@ class ElementNode(ABC):
],
}

QUERY = "?nameMatches\={}.*"
QUERY = r"?nameMatches\={}.*"

def __init__(self):
def __init__(self,parent:Optional["ElementNode"]=None):
"""节点抽象基类,要怎么实现这个基类的所有虚方法看具体情况。"""
self._path = ""
self._name = ""
self._type = []
self._parent = parent

@property
@abstractmethod
Expand All @@ -94,18 +96,19 @@ def path(self, value: str):

@property
@abstractmethod
def name(self, value) -> str:
self._name = value

@name.setter
@abstractmethod
def name(self) -> str:
"""返回元素名
Returns:
str: 元素名
"""
return self._name

@name.setter
@abstractmethod
def name(self, value) -> str:
self._name = value


@property
@abstractmethod
Expand All @@ -118,7 +121,7 @@ def type(self) -> Set[ControlType]:
return self.type

@property
def parent(self) -> "ElementNode":
def parent(self) -> Optional["ElementNode"]:
return self._parent

@property
Expand All @@ -141,7 +144,7 @@ def actions(self) -> Sequence[Action]:


class ViewModel(ABC):
def __init__(self, ui_file: str, extra_info: dict = None) -> None:
def __init__(self, ui_file: str, extra_info: Optional[dict] = None) -> None:
"""[summary]
Args:
Expand Down Expand Up @@ -201,24 +204,18 @@ def DFS(node: ElementNode):
def gen(self, output: str):
vm = self

try:
# package data 文件需要用 pkg_resources来定位访问,不要自行拼接相对路径
temp_txt = pkg_resources.resource_string(__name__, "template.robot").decode(
"utf-8"
)
template = Template(temp_txt)
txt = template.render(ui=vm, type=ControlType)

outdir = os.path.dirname(output)

if not os.path.exists(outdir):
os.makedirs(outdir)

with open(output, "w", encoding="utf-8") as f:
f.write(txt)
logger.info("导出%s-->%s", self._ui_file, output)
except Exception as e:
logger.error(e, exc_info=1)
# package data 文件需要用 pkg_resources来定位访问,不要自行拼接相对路径
temp_txt = pkg_resources.resource_string(__name__, "template.robot").decode(
"utf-8"
)
template = Template(temp_txt)
txt = template.render(ui=vm, type=ControlType)
outdir = os.path.dirname(output)
if not os.path.exists(outdir):
os.makedirs(outdir)
with open(output, "w", encoding="utf-8") as f:
f.write(txt)
logger.info("导出%s-->%s", self._ui_file, output)


class LoadError(Exception):
Expand Down
12 changes: 6 additions & 6 deletions robotframework_airtest/vmg/generators/unity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Sequence, Set
from typing import Any, Optional, Sequence, Set
from ..generatorbase import (
ControlType,
ElementNode,
Expand Down Expand Up @@ -74,9 +74,9 @@ def find_root_entry(doc: UnityDocument):


class UnityElementNode(ElementNode):
def __init__(self, raw_data: RawData, parent: ElementNode = None):
def __init__(self, raw_data: RawData, parent: Optional[ElementNode] = None):
super().__init__(parent)
self._raw_data = raw_data
self._parent = parent
self._name = self.path = str(self._raw_data.entry.m_Name)

@property
Expand All @@ -98,7 +98,7 @@ def name(self) -> str:
return self._name

@name.setter
def name(self, value: str) -> str:
def name(self, value: str):
self._name = value

@property
Expand Down Expand Up @@ -151,7 +151,7 @@ def rect_transform(self) -> Any:


class UnityViewModel(ViewModel):
def __init__(self, ui_file: str, extra_info: dict = None) -> None:
def __init__(self, ui_file: str, extra_info: Optional[dict] = None) -> None:
super().__init__(ui_file, extra_info)
self._libraries = ["robotframework_airtest.poco.UnityPocoLibrary"]

Expand All @@ -167,5 +167,5 @@ def load(self, ui_file: str) -> ElementNode:
return root


def generate(input: str, output: str, extra_info: dict = None):
def generate(input: str, output: str, extra_info: Optional[dict] = None):
UnityViewModel(input, extra_info).gen(output)

0 comments on commit 1e04cac

Please sign in to comment.