Skip to main content

A python package for parsing FGUI project files

Project description

py-fgui(Python-FairyGUI)

在Python中实现的,对于FGUI项目进行解析的库。可以解析到元件的层级。

未引用任何第三方库,仅使用Python自带的库。

目前功能还比较简陋,但已经满足了自己的大部分需求,欢迎各位大佬提出建议和PR,一起完善这个小项目。

快速开始

可以通过pip来安装:pip install py-fgui

接下来就能拿到关键数据了:

from py_fgui import *

# 加载一个FGUI项目
fgui_file_path:str = r"D:\FGUIProject\FGUIProject.fairy"
project:FGUIProject = FGUIProject(fgui_file_path)

# 获取项目的主分支(目前仅支持)
branch:FGUIBranch = project.main_branch

# 获取该分支下所有包
package_list:list[FGUIPackage] = branch.package_list

# 获取某个名字的包
master_package:FGUIPackage = branch.get_package_by_name("master")

# 获取指定url对应的资源
component_url:str = "ui://pkgidandresid"
resource:FGUIResource = branch.get_resource_by_url(component_url)

# 获取一个组件资源内部的元件
obj_list:list[FGUIObject] = resource.object_list
test_obj:FGUIObject = obj_list[0]

# 获取对其他资源的引用关系,会返回有效的引用资源和无效的引用资源
# 包层级的引用(包括包内所有资源,以及资源内所有元件)
pkg_ref_list, pkg_invalid_ref_list = master_package.get_references()
# 资源层级的引用(包括资源内所有元件)
res_ref_list, res_invalid_ref_list = resource.get_references()
# 元件层级的引用
obj_ref_list, obj_invalid_ref_list = test_obj.get_references()

# 你也可以判断一个对应的资源文件是否真的存在
if resource.file_exists is False:
    print("资源文件不存在")
else:
    print("资源文件存在于:" + resource.full_path)

想把依赖关系可视化成网络图? 试试这个:

import networkx as nx
from pyvis.network import Network

from py_fgui import *

fgui_file_path:str = r"D:\FGUIProject\FGUIProject.fairy"
project:FGUIProject = FGUIProject(fgui_file_path)
branch:FGUIBranch = project.main_branch

# 创建依赖关系的映射
network_map:dict[str,list[str]] = dict()

for package in branch.package_list:
    print(f"Package: {package.package_name}")
    package_name_set:set[str] = set()
    all_references, _ = package.get_references()
    for reference_url in all_references:
        reference_package_name:str = branch.get_package_by_id(reference_url.package_id).package_name
        package_name_set.add(reference_package_name)

    for package_name in package_name_set:
        print(f"    {package_name}")
        if package.package_name not in network_map:
            network_map[package.package_name] = []
        network_map[package.package_name].append(package_name)

# 绘制网络关系图
print("=" * 20)
G = nx.Graph()

for package_name in network_map:
    for reference_package_name in network_map[package_name]:
        G.add_edge(package_name, reference_package_name)

# 画图
nx.draw(G, with_labels=True)

nt = Network(
    height="100vh",
    width="100%",
    directed=True,
    notebook=False,  # 必须设置为 False 才能启用完整响应式支持
    bgcolor="#ffffff",
    cdn_resources="in_line"  # 内联资源,避免 CDN 依赖
)

nt.toggle_physics(False)

# 手动计算网格坐标
grid_spacing = 400  # 像素间距

# 按照引用数量排序,从多到少排序
nodes = list(G.nodes())
nodes.sort(key=lambda x: G.degree[x], reverse=True)


for idx, node in enumerate(nodes):
    row = idx // 20
    col = idx % 20
    x = col * grid_spacing
    y = row * grid_spacing
    nt.add_node(
        node,
        x=x,
        y=y,
        label=node,
        font={"size": 14, "color": "#333"},  # 优化标签可读性
        shape="box",  # 节点显示为方块
        color="#4CAF50"
    )

# 设置边的样式为直线
nt.set_edge_smooth('false')

# 添加边
for edge in G.edges():
    nt.add_edge(edge[0], edge[1], width=1.2, color="#666")

# 生成 HTML 并注入自定义 CSS
html_content = nt.generate_html()

# 插入响应式 CSS 样式
css_injection = """
<style>
  body { margin: 0; padding: 0; overflow: hidden; }
  #mynetwork { 
    width: 100vw !important; 
    height: 100vh !important;
  }
</style>
"""
html_content = html_content.replace("</head>", css_injection + "</head>")


with open("fgui_net.html", "w", encoding="utf-8") as f:
    f.write(html_content)

print("Export network to fgui_net.html")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_fgui-0.0.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_fgui-0.0.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file py_fgui-0.0.2.tar.gz.

File metadata

  • Download URL: py_fgui-0.0.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for py_fgui-0.0.2.tar.gz
Algorithm Hash digest
SHA256 cb48de02b5ee1e4f47129fe6de29321dd57318bed903ea1938eaa1db12da736e
MD5 69a779354b62c6604a7e6b062d74c600
BLAKE2b-256 e4c2e4eeeb03f85c2f74d1c1bc127a85190c060a16de91b59ae36c87a2012f82

See more details on using hashes here.

File details

Details for the file py_fgui-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: py_fgui-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for py_fgui-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5628be14156abfb9f0795335316462bc227a0e46847b786755b7b7d60f157bb1
MD5 0f6d5dee2c9c1e5d8bf56c1f2e6b8140
BLAKE2b-256 fcdfcb86ea69694271cedf03691c1db3be2ada256041a8a0b6dbbaca15082f41

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page