-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlparse.py
executable file
·69 lines (55 loc) · 1.54 KB
/
xmlparse.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
#!/usr/bin/env python
from xml.etree import ElementTree as ET
import os
import sys
def add():
if len(sys.argv) < 4:
print "Error: Not enough arguments"
return
print "Adding AP"
db_file = os.path.abspath(__file__)
db_file = os.path.dirname(db_file)
db_file = os.path.join(db_file, "db.xml")
tree = ET.parse(db_file)
ap = ET.SubElement(tree.getroot(), "ap")
ap.set("essid", sys.argv[2])
ap.set("pw", sys.argv[3])
tree.write(db_file)
def get():
if len(sys.argv) < 3:
print "Error: Not enough arguments"
return
print "TODO, happy new year"
return
db_file = os.path.abspath(__file__)
db_file = os.path.dirname(db_file)
db_file = os.path.join(db_file, "db.xml")
tree = ET.parse(db_file)
def main():
if sys.argv[1] == "add":
add()
return
if sys.argv[1] == "get":
get()
return
db = ET.Element("db")
ap = ET.SubElement(db, "ap")
ap.set("essid", "testo")
ap.set("pw", "topsecr3t")
ap = ET.SubElement(db, "ap")
ap.set("essid", "popcorn")
ap.set("pw", "cola")
# print ET.tostring(db)
db_file = os.path.abspath(__file__)
db_file = os.path.dirname(db_file)
db_file = os.path.join(db_file, "db.xml")
tree = ET.ElementTree(db)
if not os.path.exists(db_file) :
open(db_file, 'w').close()
try:
tree.write(db_file)
except Exception, inst:
print "Unexpected error %s: %s" % (db_file, inst)
return
if __name__ == "__main__":
main()