Skip to content

Commit

Permalink
Merge pull request #27 from ocefpaf/more_tests
Browse files Browse the repository at this point in the history
Adding test_coding_standards.py + py.test
  • Loading branch information
rhattersley committed May 13, 2015
2 parents c8790de + 7bd7228 commit c2ff677
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 102 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ install:
- SITE_CFG=cf_units/etc/site.cfg
- echo "[System]" >> $SITE_CFG
- echo "udunits2_path = $PREFIX/lib/libudunits2.so" >> $SITE_CFG
- python setup.py --quiet install

script:
nosetests --verbose --nocapture cf_units/tests/test_unit.py
- python setup.py test
5 changes: 2 additions & 3 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with cf_units. If not, see <http://www.gnu.org/licenses/>.

from __future__ import (absolute_import, division, print_function)

__version__ = '0.1.0'

from .cf_units import *
from .cf_units import (CALENDAR_STANDARD, CALENDARS, UT_NAMES, UT_DEFINITION,
FLOAT32, FLOAT64, julian_day2date, date2julian_day,
is_time, is_vertical)
98 changes: 54 additions & 44 deletions cf_units/cf_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,32 @@
from . import util


__all__ = ['Unit', 'date2num', 'decode_time', 'encode_clock', 'encode_date',
'encode_time', 'num2date']
__all__ = ['CALENDAR_STANDARD',
'CALENDAR_GREGORIAN',
'CALENDAR_PROLEPTIC_GREGORIAN',
'CALENDAR_NO_LEAP',
'CALENDAR_JULIAN',
'CALENDAR_ALL_LEAP',
'CALENDAR_365_DAY',
'CALENDAR_366_DAY',
'CALENDAR_360_DAY',
'CALENDARS',
'UT_NAMES',
'UT_DEFINITION',
'FLOAT32',
'FLOAT64',
'julian_day2date',
'date2julian_day',
'is_time',
'is_vertical',
'Unit',
'date2num',
'decode_time',
'encode_clock',
'encode_date',
'encode_time',
'num2date',
'suppress_errors']


########################################################################
Expand Down Expand Up @@ -312,28 +336,40 @@
FLOAT64: _cv_convert_doubles}
_numpy2ctypes = {np.float32: FLOAT32, np.float64: FLOAT64}
_ctypes2numpy = {v: k for k, v in _numpy2ctypes.iteritems()}
#
# load the UDUNITS-2 xml-formatted unit-database
#
if not _ud_system:


@contextmanager
def suppress_errors():
"""
Suppresses all error messages from UDUNITS-2.
"""
_func_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p,
use_errno=True)
_set_handler_type = ctypes.CFUNCTYPE(_func_type, _func_type)
_ut_set_error_message_handler = _set_handler_type((_UT_HANDLER, _lib_ud))
_ut_ignore = _func_type((_UT_IGNORE, _lib_ud))
# ignore standard UDUNITS-2 start-up preamble redirected to stderr stream
_default_handler = _ut_set_error_message_handler(_ut_ignore)
# Load the unit-database from the default location (modified via
# the UDUNITS2_XML_PATH environment variable) and if that fails look
# relative to sys.prefix to support environments such as conda.
_ud_system = _ut_read_xml(None)
if _ud_system is None:
_alt_xml_path = os.path.join(sys.prefix, 'share',
'udunits', 'udunits2.xml')
_ud_system = _ut_read_xml(_alt_xml_path)
# reinstate old error handler
_ut_set_error_message_handler(_default_handler)
del _func_type
try:
yield
finally:
_ut_set_error_message_handler(_default_handler)


#
# load the UDUNITS-2 xml-formatted unit-database
#
if not _ud_system:
# Ignore standard noisy UDUNITS-2 start-up.
with suppress_errors():
# Load the unit-database from the default location (modified via
# the UDUNITS2_XML_PATH environment variable) and if that fails look
# relative to sys.prefix to support environments such as conda.
_ud_system = _ut_read_xml(None)
if _ud_system is None:
_alt_xml_path = os.path.join(sys.prefix, 'share',
'udunits', 'udunits2.xml')
_ud_system = _ut_read_xml(_alt_xml_path)
if not _ud_system:
_status_msg = 'UNKNOWN'
_error_msg = ''
Expand Down Expand Up @@ -705,32 +741,6 @@ def num2date(time_value, unit, calendar):
return cdftime.num2date(time_value)


def _handler(func):
"""Set the error message handler."""

_ut_set_error_message_handler(func)


@contextmanager
def suppress_unit_warnings():
"""
Suppresses all warnings raised because of invalid units in loaded data.
"""
# Suppress any warning messages raised by UDUNITS2.
_func_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p,
use_errno=True)
_set_handler_type = ctypes.CFUNCTYPE(_func_type, _func_type)
_ut_set_error_message_handler = _set_handler_type((_UT_HANDLER, _lib_ud))
_ut_ignore = _func_type((_UT_IGNORE, _lib_ud))
_default_handler = _ut_set_error_message_handler(_ut_ignore)
with warnings.catch_warnings():
# Also suppress invalid units warnings from the cf_units loader code.
warnings.filterwarnings("ignore", message=".*invalid units")
yield
_ut_set_error_message_handler(_default_handler)


########################################################################
#
# unit wrapper class for unidata/ucar UDUNITS-2
Expand Down
Loading

0 comments on commit c2ff677

Please sign in to comment.