From ba6fd863bfb5df134c92519302e43d5fb9f942c4 Mon Sep 17 00:00:00 2001 From: Serge Matveenko Date: Sun, 11 Feb 2018 06:23:06 +0300 Subject: [PATCH] Fix double exceptions if a file was not found. --- pystardict.py | 48 +++++++++++++++++++++------------------- tests/test_pystardict.py | 5 +++-- tox.ini | 2 +- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pystardict.py b/pystardict.py index 169ce17..682dfa1 100644 --- a/pystardict.py +++ b/pystardict.py @@ -1,9 +1,9 @@ import gzip import hashlib +import os import re import warnings from struct import unpack -from warnings import warn import six @@ -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() @@ -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() @@ -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 """ @@ -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): """ @@ -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') diff --git a/tests/test_pystardict.py b/tests/test_pystardict.py index 15ec4f7..5c6ecd9 100644 --- a/tests/test_pystardict.py +++ b/tests/test_pystardict.py @@ -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( @@ -28,7 +29,7 @@ def fixture_dict(request): @pytest.fixture def fixture_in_memory_dict(): - return + return def test001Idx(fixture_dict): diff --git a/tox.ini b/tox.ini index 6cf5d74..3166e54 100644 --- a/tox.ini +++ b/tox.ini @@ -7,6 +7,6 @@ envlist = py27, py36 [testenv] -commands = py.test +commands = py.test -vvv deps = pytest