Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI output optionally target a folder #114

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions PyPoE/cli/exporter/dat/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ def _read_dat_files(self, args, prefix=''):
remove.append(name)
continue

df = dat.DatFile(name)
df.read(file_path_or_raw=node.record.extract(), use_dat_value=False)
try:
df = dat.DatFile(name)
df.read(file_path_or_raw=node.record.extract(), use_dat_value=False)

dat_files[name] = df
dat_files[name] = df
except:
print('Error occured for %s' % name)
remove.append(name)

for file_name in remove:
args.files.remove(file_name)
Expand Down
121 changes: 66 additions & 55 deletions PyPoE/cli/exporter/dat/parsers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

# Python
import argparse
from pathlib import Path
from json import dump

# self
Expand Down Expand Up @@ -101,67 +102,77 @@ def handle(self, args):

dict_spec = args.spec.as_dict()

with open(
args.target,
mode='w',
encoding='ascii' if args.ascii else 'utf-8'
) as f:
dat_files = self._read_dat_files(args)

console('Building data object...')
out = []

for file_name in args.files:
dat_file = dat_files[file_name]

header = [
dict({ 'name': name, 'rowid': index }, **props)
for index, (name, props)
in enumerate(dict_spec[file_name]['fields'].items())
]

virtual_header = [
dict({ 'name': name, 'rowid': index }, **props)
for index, (name, props)
in enumerate(dict_spec[file_name]['virtual_fields'].items())
]

if args.use_object_format:
out_obj = {
'filename': file_name,
'header': {row['name']: row for row in header},
'data': [{
cid: row[i] for i, cid in enumerate(
dat_file.reader.columns_data
)
} for row in dat_file.reader.table_data
],
}

virtual_header = (
{row['name']: row for row in virtual_header}
)
else:
out_obj = {
'filename': file_name,
'header': header,
'data': dat_file.reader.table_data,
}

if args.include_virtual_fields:
out_obj['virtual_header'] = virtual_header

if args.include_record_length:
out_obj['record_length'] = dat_files[file_name].reader.table_record_length

dat_files = self._read_dat_files(args)

console('Building data object...')
out = []
out_path = Path(args.target)

for file_name in args.files:
dat_file = dat_files[file_name]

header = [
dict({ 'name': name, 'rowid': index }, **props)
for index, (name, props)
in enumerate(dict_spec[file_name]['fields'].items())
]

virtual_header = [
dict({ 'name': name, 'rowid': index }, **props)
for index, (name, props)
in enumerate(dict_spec[file_name]['virtual_fields'].items())
]

if args.use_object_format:
out_obj = {
'filename': file_name,
'header': {row['name']: row for row in header},
'data': [{
cid: row[i] for i, cid in enumerate(
dat_file.reader.columns_data
)
} for row in dat_file.reader.table_data
],
}

virtual_header = (
{row['name']: row for row in virtual_header}
)
else:
out_obj = {
'filename': file_name,
'header': header,
'data': dat_file.reader.table_data,
}

if args.include_virtual_fields:
out_obj['virtual_header'] = virtual_header

if args.include_record_length:
out_obj['record_length'] = dat_files[file_name].reader.table_record_length

if out_path.is_dir():
file_path = (out_path / file_name).with_suffix('.json')
console('Dumping data to "%s"...' % file_path)
with file_path.open(
mode='w',
encoding='ascii' if args.ascii else 'utf-8'
) as f:
dump(out_obj, f, ensure_ascii=args.ascii, indent=4)
else:
out.append(out_obj)

if out_path.is_file():
console('Dumping data to "%s"...' % args.target)

dump(out, f, ensure_ascii=args.ascii, indent=4)
with out_path.open(
mode='w',
encoding='ascii' if args.ascii else 'utf-8'
) as f:
dump(out, f, ensure_ascii=args.ascii, indent=4)

console('Done.')


# =============================================================================
# Functions
# =============================================================================