-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleandbTable.py
103 lines (94 loc) · 3.78 KB
/
leandbTable.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
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
"""
Handle a table inside a database
Both table and database fundamentally a directory.
-> Create Table
-> Delete Table
-> Rename Table
"""
from leandbHelper import *
import os
import shutil
pathEndsWith = pathEndWith()
# fetch and assign leandb configuration data
# configData = starterConf()
def createTable( commandJsonObj, databaseStorage ):
ret = {}
missing = False
missedItems = []
if( not commandJsonObj['databaseName'] or commandJsonObj['databaseName'] == "" ):
missing = True
missedItems.append("databaseName")
if( not commandJsonObj['tableName'] or commandJsonObj['tableName'] == "" ):
missing = True
missedItems.append("tableName")
if( missing == False ):
tableAbsolutePath = databaseStorage+commandJsonObj['databaseName']+pathEndsWith+commandJsonObj['tableName']+pathEndsWith+'_ldb'+pathEndsWith+'_index'
# Attempt to cretate a new table directory
# If the database was not found then it will
# try to create the database directory first and then table dir.
if( not os.path.isdir( tableAbsolutePath ) ):
tableCreated = False
try:
os.makedirs( tableAbsolutePath )
ret['status'] = {
'status_type': True,
'status_message': 'Table '+commandJsonObj['tableName']+' created successfully'
}
tableCreated = True
except Exception as e:
ret['status'] = {
'status_type': False,
'status_message': 'Error while creating table: '+str(e.args)
}
# create table system id pointer
if tableCreated == True:
idFile = databaseStorage+commandJsonObj['databaseName']+pathEndsWith+commandJsonObj['tableName']+pathEndsWith+'_ldb'+pathEndsWith+'id'
try:
with open( idFile, 'a+' ) as tsid:
print( '0', file=tsid )
except Exception as e:
ret['status'] = {
'status_type': False,
'status_message': 'Error on creating the system id.'
}
else:
ret['status'] = {
'status_type': False,
'status_message': 'Error while creating table: Table already exists'
}
else:
ret['status'] = {
'status_type': False,
'status_message': 'Error while creating table: '+str( missedItems )+' values are missing'
}
return ret
def deleteTable( commandJsonObj, databaseStorage ):
ret = {}
tableAbsolutePath = databaseStorage+commandJsonObj['databaseName']+pathEndsWith+commandJsonObj['tableName']
try:
shutil.rmtree( tableAbsolutePath )
ret['status'] = {
'status_type': True,
'status_message': 'Database '+commandJsonObj['tableName']+' deleted successfully'
}
except Exception as e:
ret['status'] = {
'status_type': False,
'status_message': 'Error: '+str(e.args)
}
return ret
def renameTable( commandJsonObj, databaseStorage ):
ret = {}
try:
os.rename( databaseStorage+commandJsonObj['databaseName']+pathEndsWith+commandJsonObj['tableName'],
databaseStorage+commandJsonObj['databaseName']+pathEndsWith+commandJsonObj['newTableName'] )
ret['status'] = {
'status_type': True,
'status_message': 'Database '+commandJsonObj['tableName']+' renamed to '+commandJsonObj['newTableName']+' successfully'
}
except Exception as e:
ret['status'] = {
'status_type': False,
'status_message': 'Error: '+str(e.args)
}
return ret