Skip to content

Commit

Permalink
Option -j to format list output as JSON on omnidb-config
Browse files Browse the repository at this point in the history
  • Loading branch information
wind39 committed Jan 7, 2020
1 parent 2cd060e commit 1bc4f1a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
30 changes: 30 additions & 0 deletions OmniDB/OmniDB_app/include/Spartacus/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from abc import ABC, abstractmethod
import datetime
import decimal
import json
import math

import OmniDB_app.include.Spartacus as Spartacus
Expand Down Expand Up @@ -324,6 +325,35 @@ def Compare(self, p_datatable, p_pkcols, p_statuscolname, p_diffcolname, p_order
raise Spartacus.Database.Exception('Can not compare tables with different columns.')
else:
raise Spartacus.Database.Exception('Can not compare tables with no columns.')
def Jsonify(self):
if self.Simple:
if len(self.Rows) > 0:
if isinstance(self.Rows[0], OrderedDict):
return json.dumps(self.Rows)
else:
v_table = []
for r in self.Rows:
v_row = []
for c in range(0, len(self.Columns)):
v_row.append(r[c])
v_table.append(OrderedDict(zip(self.Columns, tuple(v_row))))
return json.dumps(v_table)
else:
return json.dumps(self.Rows)
else:
if len(self.Rows) > 0:
if isinstance(self.Rows[0], OrderedDict):
return json.dumps(self.Rows)
else:
v_table = []
for r in self.Rows:
v_row = []
for c in self.Columns:
v_row.append(r[c])
v_table.append(OrderedDict(zip(self.Columns, tuple(v_row))))
return json.dumps(v_table)
else:
return json.dumps(self.Rows)
def Pretty(self, p_transpose=False):
if self.Simple:
if p_transpose:
Expand Down
Binary file modified OmniDB/db.sqlite3
Binary file not shown.
17 changes: 13 additions & 4 deletions OmniDB/omnidb-config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
default=False, action="store_true",
help="databases maintenance")
group.add_option("-r", "--resetdatabase", dest="reset",
default=False,action="store_true",
default=False, action="store_true",
help="reset user and session databases")
group.add_option("-t", "--deletetemp", dest="deletetemp",
default=False,action="store_true",
default=False, action="store_true",
help="delete temporary files")
group.add_option("-j", "--jsonoutput", dest="jsonoutput",
default=False, action="store_true",
help="format list output as json")
parser.add_option_group(group)

group = optparse.OptionGroup(parser, "User Management Options",
Expand Down Expand Up @@ -178,7 +181,10 @@ def list_users():
from users
order by user_id
''')
print(v_table.Pretty())
if options.jsonoutput:
print(v_table.Jsonify())
else:
print(v_table.Pretty())
except Exception as exc:
print('Error:')
print(exc)
Expand Down Expand Up @@ -275,7 +281,10 @@ def list_connections(p_user):
v_row['port'] = v_cryptor.Decrypt(v_row['port'])
v_row['database'] = v_cryptor.Decrypt(v_row['database'])
v_row['dbuser'] = v_cryptor.Decrypt(v_row['dbuser'])
print(v_table.Pretty())
if options.jsonoutput:
print(v_table.Jsonify())
else:
print(v_table.Pretty())
except Exception as exc:
print('Error:')
print(exc)
Expand Down
Binary file modified OmniDB/omnidb.db
Binary file not shown.

0 comments on commit 1bc4f1a

Please sign in to comment.