forked from spacecowboy/AndroidCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.py
More file actions
69 lines (55 loc) · 1.89 KB
/
generator.py
File metadata and controls
69 lines (55 loc) · 1.89 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
"""Writes the generated classes to file
>>> g = Generator()
"""
import os, errno
import dbitem
from dbitem import DBItem
from database_handler import DatabaseHandler
from provider import Provider
class Generator(object):
def __init__(self, path = "./", *sqltables):
self.path = path
self.tables = []
if sqltables is not None and len(sqltables) > 0:
self.add_tables(sqltables)
def add_tables(self, *sqltables):
self.tables.extend(sqltables)
def write(self):
mkdir_p(self.path)
db_handler = DatabaseHandler("SampleDB")
provider = Provider()
# Generate dbitem files
for table in self.tables:
item = DBItem(table)
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)
# 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))
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