Skip to content

Commit

Permalink
[DBC] Very ugly json outputter for db2 files
Browse files Browse the repository at this point in the history
  • Loading branch information
navv1234 committed Jul 29, 2018
1 parent c623e86 commit e77684c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
36 changes: 33 additions & 3 deletions dbc_extract3/dbc/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def field(self, *args):

return f

def field_names(self, delim = ", "):
def field_names(self, delim = ", ", raw = False):
fields = []
if self._id_block and self.__output_field('id'):
fields.append('id')
Expand All @@ -267,7 +267,10 @@ def field_names(self, delim = ", "):
if self._key_block and self.__output_field('id_parent'):
fields.append('id_parent')

return delim.join(fields)
if raw:
return fields
else:
return delim.join(fields)

def __output_field(self, field):
if len(self._dbcp.options.fields) == 0:
Expand Down Expand Up @@ -308,6 +311,33 @@ def __str__(self):

return ' '.join(s)

def obj(self):
d = {}
if self._id_block and self.__output_field('id'):
d['id'] = self._id

for i in range(0, len(self._fi)):
field = self._fi[i]
type_ = self._fo[i]
if not field:
continue

if not self.__output_field(field):
continue

if type_ == 'S':
if self._d[i] > 0:
d[field] = self._dbcp.get_string(self._d[i], self._record_id, i)
else:
d[field] = ""
else:
d[field] = self._d[i]

if self._key_block and self.__output_field('id_parent'):
d['id_parent'] = self._key

return d

def csv(self, delim = ',', header = False):
s = ''
if self._id_block and self.__output_field('id'):
Expand Down Expand Up @@ -455,7 +485,7 @@ def __new__(mcl, name, bases, namespace):
return super(Meta, mcl).__new__(mcl, name, bases, namespace)

def requires_data_model(options):
return options.type in ['csv', 'view', 'output', 'generator', 'class_flags']
return options.type in ['csv', 'view', 'output', 'generator', 'class_flags', 'json']

def initialize_data_model(options):
if not requires_data_model(options):
Expand Down
61 changes: 60 additions & 1 deletion dbc_extract3/dbc_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def parse_fields(value):
parser.add_argument("-t", "--type", dest = "type",
help = "Processing type [output]", metavar = "TYPE",
default = "output", action = "store",
choices = [ 'output', 'scale', 'view', 'csv', 'header',
choices = [ 'output', 'scale', 'view', 'csv', 'header', 'json',
'class_flags', 'generator', 'validate', 'generate_format' ])
parser.add_argument("-o", dest = "output")
parser.add_argument("-a", dest = "append")
Expand Down Expand Up @@ -200,6 +200,65 @@ def parse_fields(value):
print('{}{}'.format(record, hotfix and ' [hotfix]' or ''))
else:
print('No record for DBC ID {} found'.format(id))
elif options.type == 'json':
path = os.path.abspath(os.path.join(options.path, options.args[0]))
id = None
if len(options.args) > 1:
id = int(options.args[1])

dbc_file = DBCFile(options, path)
if not dbc_file.open():
sys.exit(1)

cache = HotfixFile(options)
if not cache.open():
sys.exit(1)
else:
entries = {}
for entry in cache.entries(dbc_file.parser):
entries[entry.id] = entry

str_ = '[\n'
logging.debug(dbc_file)
if id == None:
replaced_ids = []
for record in dbc_file:
if not options.raw and record.id in entries:
data_ = entries[record.id].obj()
data_['hotfixed'] = True
replaced_ids.append(record.id)
else:
str_ += '\t{},\n'.format(json.dumps(record.obj()))

for id, entry in entries.items():
if id in replaced_ids:
continue

data_ = entry.obj()
data_['hotfixed'] = True

str_ += '\t{},\n'.format(json.dumps(data_))

else:
if id in entries:
record = entries[id]
else:
record = dbc_file.find(id)

if record:
data_ = record.obj()
if id in entries:
data_['hotfixed'] = True

str_ += '\t{},\n'.format(json.dumps(data_))
else:
print('No record for DBC ID {} found'.format(id))

str_ = str_[:-2] + '\n'
str_ += ']\n'

print(str_)

elif options.type == 'csv':
path = os.path.abspath(os.path.join(options.path, options.args[0]))
id = None
Expand Down

0 comments on commit e77684c

Please sign in to comment.