Skip to content

Commit 6475f03

Browse files
committed
optimized script and added new flag for decimal comments
- optimize script - added feature of adding comments with decimal ids (optional) - updated README.md
1 parent 6efa953 commit 6475f03

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ Requires installed **Python 3.9+**
99
## How to use
1010

1111
```
12-
-f <path to public.xml>
12+
usage: rclass_generator.py [-h] -f <pathname> -p <packagename> [-c]
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-f <pathname>, --file <pathname>
17+
pathname of public.xml
18+
-p <packagename>, --package <packagename>
19+
packagename of generated class
20+
-c, --comments add comments with decimal ids
1321
```
1422

15-
Adding a package name is also supported (Optional)
23+
## Example
1624

1725
```
18-
-f <path to public.xml> -p <package name>
19-
```
20-
21-
### Example
22-
23-
```
24-
-f public.xml -p com.vtosters.lite
26+
-f public.xml -p com.vtosters.lite -c
2527
```

rclass_generator.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import argparse, sys, re
33
from typing import List
44

5+
TABULATION = " " * 4
6+
57
class AndroidResource:
68
name: str
79
id: str
@@ -11,7 +13,7 @@ def __init__(self, name: str, id: str):
1113
self.id = id
1214

1315
def build_line(self) -> str:
14-
return " public static final int {0} = {1};".format(self.name, self.id)
16+
return TABULATION * 2 + "public static final int {0} = {1};".format(self.name, self.id)
1517

1618
class Node:
1719
name: str
@@ -24,32 +26,33 @@ def __init__(
2426
self.name = name
2527
self.resources = list()
2628

27-
def build_lines(self) -> list[str]:
29+
def build_lines(self, add_comments) -> list[str]:
2830
lst = list()
29-
lst.append(" public static final class {0} {{".format(self.name))
31+
lst.append(TABULATION + "public static final class {0} {{".format(self.name))
3032
for resource in self.resources:
31-
lst.append(resource.build_line())
32-
lst.append(" }")
33+
line = resource.build_line()
34+
if add_comments:
35+
line += " // " + str(int(resource.id, 16))
36+
lst.append(line)
37+
lst.append(TABULATION + "}")
3338
return lst
3439

35-
nodes: list[Node] = list()
40+
NODES: list[Node] = list()
3641

3742
def find_node(name):
38-
for node in nodes:
43+
for node in NODES:
3944
if name == node.name:
4045
return node
4146

42-
def generate_rclass(package_name):
47+
def generate_rclass(package_name, add_comments):
4348
with open("R.java", "w") as f:
4449
if package_name:
45-
f.write("package {package};".format(package=package_name))
46-
f.write("\n\n")
50+
f.write("package {0};\n\n".format(package_name))
4751
f.write("public class R {")
48-
for node in nodes:
52+
for node in NODES:
4953
f.write("\n")
50-
for line in node.build_lines():
51-
f.write(line)
52-
f.write("\n")
54+
for line in node.build_lines(add_comments):
55+
f.write(line + "\n")
5356
f.write("}")
5457

5558
def parse_resources(pathname):
@@ -61,16 +64,18 @@ def parse_resources(pathname):
6164
node = find_node(type)
6265
if not node:
6366
node = Node(type)
64-
nodes.append(node)
67+
NODES.append(node)
6568
node.resources.append(AndroidResource(name, id))
6669

6770
if __name__ == "__main__":
6871
parser = argparse.ArgumentParser()
69-
parser.add_argument("-f", type=str, help="Pathname of public.xml")
70-
parser.add_argument("-p", type=str, help="Package of generated class")
72+
parser.add_argument("-f", "--file", help="Pathname of public.xml")
73+
parser.add_argument("-p", "--package", help="Package of generated class")
74+
parser.add_argument("-c", "--comments", action="store_true", help="Add comments with decimal resource ids")
7175
args = parser.parse_args()
72-
if not args.f:
76+
print(args)
77+
if not args.file:
7378
print("null pathname", file=sys.stderr)
7479
exit(-1)
75-
parse_resources(args.f)
76-
generate_rclass(args.p)
80+
parse_resources(args.file)
81+
generate_rclass(args.package, args.comments)

0 commit comments

Comments
 (0)