Skip to content

Commit

Permalink
Fix double exceptions if a file was not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Feb 11, 2018
1 parent 05115bb commit ba6fd86
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
48 changes: 25 additions & 23 deletions pystardict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import gzip
import hashlib
import os
import re
import warnings
from struct import unpack
from warnings import warn

import six

Expand Down Expand Up @@ -52,8 +52,8 @@ def __init__(self, dict_prefix, container):

try:
_file = open(ifo_filename)
except IOError:
raise Exception('.ifo file does not exists')
except Exception as e:
raise Exception('ifo file opening error: "{}"'.format(e))

_file.readline()

Expand Down Expand Up @@ -132,8 +132,7 @@ def __init__(self, dict_prefix, container):
try:
file = open_file(idx_filename, idx_filename_gz)
except Exception as e:
warn(e.message)
raise Exception('.idx file does not exists')
raise Exception('idx file opening error: "{}"'.format(e))

self._file = file.read()

Expand All @@ -149,7 +148,8 @@ def __init__(self, dict_prefix, container):
idx_cords_bytes_size = idx_offset_bytes_size + 4

""" parse data via regex """
record_pattern = br'([\d\D]+?\x00[\d\D]{'+str(idx_cords_bytes_size).encode('utf-8')+br'})'
record_pattern = br'([\d\D]+?\x00[\d\D]{' + str(
idx_cords_bytes_size).encode('utf-8') + br'})'
matched_records = re.findall(record_pattern, self._file)

""" check records count """
Expand Down Expand Up @@ -362,18 +362,16 @@ def __init__(self, dict_prefix, container, in_memory=False):
dict_filename = '%s.dict' % dict_prefix
dict_filename_dz = '%s.dz' % dict_filename

try:
f = open_file(dict_filename, dict_filename_dz)
except Exception as e:
raise Exception('dict file opening error: "{}"'.format(e))

if in_memory:
try:
f = open_file(dict_filename, dict_filename_dz)
self._file = f.read()
f.close()
except:
raise Exception('.dict file does not exists')
self._file = f.read()
f.close()
else:
try:
self._file = open_file(dict_filename, dict_filename_dz)
except:
raise Exception('.dict file does not exists')
self._file = f

def __getitem__(self, word):
"""
Expand Down Expand Up @@ -646,12 +644,16 @@ def open_file(regular, gz):
Open regular file if it exists, gz file otherwise.
If no file exists, raise ValueError.
"""
try:
return open(regular, 'rb')
except IOError as e:
warn(e.message)
if os.path.exists(regular):
try:
return open(regular, 'rb')
except Exception as e:
raise Exception('regular file opening error: "{}"'.format(e))

if os.path.exists(gz):
try:
return gzip.open(gz, 'rb')
except IOError:
warn(e.message)
raise ValueError('Neither regular nor gz file exists')
except Exception as e:
raise Exception('gz file opening error: "{}"'.format(e))

raise ValueError('Neither regular nor gz file exists')
5 changes: 3 additions & 2 deletions tests/test_pystardict.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# -*- coding: utf-8 -*-
import os

import pytest

from pystardict import Dictionary


@pytest.fixture(params=['on_disk', 'in_memory'])
@pytest.fixture(params=['on_disk', 'in_memory'], scope="session")
def fixture_dict(request):
return {
'on_disk': Dictionary(
Expand All @@ -28,7 +29,7 @@ def fixture_dict(request):

@pytest.fixture
def fixture_in_memory_dict():
return
return


def test001Idx(fixture_dict):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
envlist = py27, py36

[testenv]
commands = py.test
commands = py.test -vvv
deps =
pytest

0 comments on commit ba6fd86

Please sign in to comment.