-
Notifications
You must be signed in to change notification settings - Fork 113
/
appstream.py
76 lines (52 loc) · 1.97 KB
/
appstream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import subprocess
from typing import Optional
from lxml import etree
# for mypy
Element = etree._Element
ElementTree = etree._ElementTree
def validate(path: str) -> dict:
if not os.path.isfile(path):
raise FileNotFoundError("AppStream file not found")
cmd = subprocess.run(
["appstream-util", "validate", "--nonet", path],
capture_output=True,
)
ret = {
"stdout": cmd.stdout.decode("utf-8"),
"stderr": cmd.stderr.decode("utf-8"),
"returncode": cmd.returncode,
}
return ret
def parse_xml(path: str) -> ElementTree:
return etree.parse(path)
def components(path: str) -> list:
components = parse_xml(path).xpath("/components/component")
return list(components)
def is_developer_name_present(path: str) -> bool:
developer = components(path)[0].xpath("developer_name")
return bool(developer)
def is_project_license_present(path: str) -> bool:
plicense = components(path)[0].xpath("project_license")
return bool(plicense)
def component_type(path: str) -> str:
return str(components(path)[0].attrib.get("type"))
def name(path: str) -> Optional[str]:
for name in parse_xml(path).findall("component/name"):
if not name.attrib.get(r"{http://www.w3.org/XML/1998/namespace}lang"):
return str(name.text)
return None
def summary(path: str) -> Optional[str]:
for summary in parse_xml(path).findall("component/summary"):
if not summary.attrib.get(r"{http://www.w3.org/XML/1998/namespace}lang"):
return str(summary.text)
return None
def check_caption(path: str) -> bool:
exp = "//screenshot[not(caption/text()) or not(caption)]"
return not any(e is not None for e in parse_xml(path).xpath(exp))
def has_manifest_key(path: str) -> bool:
metadata = parse_xml(path).xpath("/components/component/metadata")
for key in metadata:
if key.attrib.get("key") == "flathub::manifest":
return True
return False