Skip to content

Commit

Permalink
Bug fixes, DatFile uses AbstractFileReadOnly now
Browse files Browse the repository at this point in the history
- fixed the argument passing for AbstractFileReadOnly and AbstractFile
- switched instances of read_from_raw to use read
- fixed deprecated decorators to show the calling line, not the decoline itself
- enable deprecation warnings on package load
  • Loading branch information
OmegaK2 committed Sep 5, 2015
1 parent f7eccdb commit 3c34a87
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions PyPoE/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Python
import platform
import os
import warnings

# =============================================================================
# Globals
Expand Down Expand Up @@ -73,6 +74,7 @@ def _get_app_dir():
# Init
# =============================================================================

warnings.simplefilter('default', DeprecationWarning)
APP_DIR = _get_app_dir()
DATA_DIR = os.path.join(os.path.dirname(__file__), '_data')
DAT_SPECIFICATION = os.path.join(DATA_DIR, 'dat.specification.ini')
Expand Down
12 changes: 6 additions & 6 deletions PyPoE/poe/file/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def read(self, file_path_or_raw, *args, **kwargs):
:raises TypeError: if file_path_or_raw has an invalid type
"""
if isinstance(file_path_or_raw, BytesIO):
return self._read(file_path_or_raw)
return self._read(file_path_or_raw, *args, **kwargs)
elif isinstance(file_path_or_raw, bytes):
return self._read(BytesIO(file_path_or_raw))
return self._read(BytesIO(file_path_or_raw), *args, **kwargs)
elif isinstance(file_path_or_raw, str):
with open(file_path_or_raw, 'rb') as f:
return self._read(f)
return self._read(f, *args, **kwargs)
else:
raise TypeError('file_path_or_raw must be a file path or bytes object')

Expand Down Expand Up @@ -122,11 +122,11 @@ def write(self, file_path_or_raw, *args, **kwargs):
:raises TypeError: if file_path_or_raw has an invalid type
"""
if isinstance(file_path_or_raw, BytesIO):
return self._write(file_path_or_raw)
return self._write(file_path_or_raw, *args, **kwargs)
elif isinstance(file_path_or_raw, bytes):
return self._write(BytesIO(file_path_or_raw))
return self._write(BytesIO(file_path_or_raw), *args, **kwargs)
elif isinstance(file_path_or_raw, str):
with open(file_path_or_raw, 'wb') as f:
return self._write(f)
return self._write(f, *args, **kwargs)
else:
raise TypeError('file_path_or_raw must be a file path or bytes object')
29 changes: 16 additions & 13 deletions PyPoE/poe/file/dat.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
# Library imports
from PyPoE import DAT_SPECIFICATION, DAT_SPECIFICATION_CONFIGSPEC
from PyPoE.shared.decorators import deprecated
from PyPoE.poe.file._shared import AbstractFileReadOnly
from PyPoE.poe.file.ggpk import GGPKFile

# =============================================================================
Expand Down Expand Up @@ -639,7 +640,7 @@ def export_to_html(self, export_table=True, export_data=False):
return ''.join(outstr)


class DatFile(object):
class DatFile(AbstractFileReadOnly):
"""
"""

Expand All @@ -651,31 +652,33 @@ def __init__(self, file_name, *args, read_file=None, read_raw=None, options={}):
raise ValueError('Only one of read_file and read_raw should be set.')

if read_file:
self.read_from_file(read_file, **options)
self.read(os.path.join(read_file, file_name), **options)
elif read_raw:
self.read_from_raw(read_raw, **options)
self.read(read_raw, **options)

def print_data(self):
for row in d.table_data:
print('Row: %s' % row.rowid)
for k in row.keys():
v = row[k]
print('|- %s: %s' % (k, v))


def _read(self, buffer, *args, **kwargs):
self.reader = DatReader(self._file_name, **kwargs)
self.reader.read(buffer.read())

return self.reader

@deprecated(message='Use of %(func)s is deprecated, use read instead.')
def read_from_file(self, path, **options):
file_path = os.path.join(path, self._file_name)
with open(file_path, mode='br') as datfile:
raw = datfile.read()
return self.read_from_raw(raw, **options)

return self.read(os.path.join(path, self._file_name), **options)

@deprecated(message='Use of %(func)s is deprecated, use read instead.')
def read_from_raw(self, raw, **options):
"""
Specification as _ordered_ dictionary key:value format
"""
self.reader = DatReader(self._file_name, **options)
self.reader.read(raw)

return self.reader
return self.read(raw, **options)


class RelationalReader(object):
Expand Down
2 changes: 1 addition & 1 deletion PyPoE/shared/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def deprecated_function(*args, **kwargs):
warnings.warn(
self.message % {
'func': function.__name__,
}, DeprecationWarning
}, DeprecationWarning, stacklevel=2,
)

return function(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion PyPoE/ui/shared/file/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class DatDataHandler(FileDataHandler):
def get_widget(self, file_data, file_name='', parent=None, *args, **kwargs):
dat_file = DatFile(file_name)
# We want dat values here
dat_file.read_from_raw(file_data, use_dat_value=True)
dat_file.read(file_data, use_dat_value=True)

frame = DatFrame(dat_file=dat_file, parent=parent)

Expand Down
2 changes: 1 addition & 1 deletion tests/PyPoE/poe/file/test_dat.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_dat_file():
spec = test_load_spec()

df = dat.DatFile('TestSpec.dat')
dr = df.read_from_raw(temp_file, specification=spec)
dr = df.read(temp_file, specification=spec)

for row in dr:
for test in test_data:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_definitions(node):
}
# Will raise errors accordingly if it fails
df = dat.DatFile(node.name, options=opt)
df.read_from_raw(node.record.extract())
df.read(node.record.extract())



0 comments on commit 3c34a87

Please sign in to comment.