-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgenerator.py
More file actions
122 lines (97 loc) · 3.72 KB
/
generator.py
File metadata and controls
122 lines (97 loc) · 3.72 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Writes the generated classes to file
Will print to the terminal what is needed to
be added to the Manifest.
>>> g = Generator(srcdir="./src", pkg="com.ex.app.db")
"""
import os, errno
import dbitem
from dbitem import DBItem
from database_handler import DatabaseHandler
from database_triggers import DatabaseTriggers
from database_views import DatabaseViews
from provider import Provider
class Generator(object):
def __init__(self, srcdir, pkg):
"""Need to specify srcdir and pkg. Srcdir
is the directory where your java files lives.
If srcdir is /projectdir/src for example,
then inside you will get the directories:
/projectdir/src/com/example/....
given that your pkg was specified as
com.example...
So specify srcdir and package to be sensible values!
To just see what the output is before you write to the
final location, you can pass srcdir='./' and pkg='sample'
for example"""
self.srcdir = srcdir
self.pkg = pkg
self.tables = []
self.triggers = []
self.views = []
# Make the full path to java dir
self.path = os.path.join(srcdir, *pkg.split("."))
def add_tables(self, *sqltables):
self.tables.extend(sqltables)
def add_triggers(self, *triggers):
self.triggers.extend(triggers)
def add_views(self, *views):
self.views.extend(views)
def write(self):
mkdir_p(self.path)
db_handler = DatabaseHandler("SampleDB", pkg=self.pkg)
provider = Provider(classname="ItemProvider", pkg=self.pkg)
db_triggers = DatabaseTriggers(pkg=self.pkg)
db_triggers.add(*self.triggers)
db_views = DatabaseViews(pkg=self.pkg)
db_views.add(*self.views)
# Generate dbitem files
for table in self.tables:
item = DBItem(table, pkg=self.pkg)
filename = item.classname + ".java"
fpath = os.path.join(self.path, filename)
with open(fpath, 'w') as javafile:
javafile.write(str(item))
# Add to other classes
db_handler.add_dbitems(item)
provider.add_dbitems(item)
# Abstract DBItem
fpath = os.path.join(self.path,
"DBItem.java")
with open(fpath, 'w') as javafile:
javafile.write(dbitem.DBITEM_CLASS.format(pkg=self.pkg))
# Triggers
fpath = os.path.join(self.path,
"DatabaseTriggers.java")
with open(fpath, 'w') as javafile:
javafile.write(str(db_triggers))
# Views
fpath = os.path.join(self.path,
"DatabaseViews.java")
with open(fpath, 'w') as javafile:
javafile.write(str(db_views))
# Database handler
fpath = os.path.join(self.path,
db_handler.classname + ".java")
with open(fpath, 'w') as javafile:
javafile.write(str(db_handler))
# Provider
fpath = os.path.join(self.path,
provider.classname + ".java")
with open(fpath, 'w') as javafile:
javafile.write(str(provider))
# And print manifest stuff
self.print_manifest(provider)
def print_manifest(self, provider):
"""Print necessary manifest entries"""
print("Make sure your AndroidManifest.xml contains the following:")
print(provider.manifest_entry)
def mkdir_p(path):
"""Like mkdir -p it creates all directories
in a path if they do not exist"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise