Skip to content

Commit

Permalink
Merge pull request #1348 from Unidata/nc_rc_set
Browse files Browse the repository at this point in the history
expose nc_rc_set, nc_rc_get (via rc_set, rc_get module functions).
  • Loading branch information
jswhit authored Jul 19, 2024
2 parents 82b11bf + 180584a commit 84e3dd5
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cibuildwheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ jobs:
- os: macos-14
arch: arm64
CIBW_ENVIRONMENT: MACOSX_DEPLOYMENT_TARGET=14.0
- os: macos-11
- os: macos-12
arch: x86_64
CIBW_ENVIRONMENT: MACOSX_DEPLOYMENT_TARGET=11.0
CIBW_ENVIRONMENT: MACOSX_DEPLOYMENT_TARGET=12.0

steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion Changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version 1.7.2 (tag v1.7.2rel)
version 1.7.2 (not yet released)
===============================
* add static type hints (PR #1302)
* Expose nc_rc_set, nc_rc_get (via rc_set, rc_get module functions). (PR #1348)

version 1.7.1 (tag v1.7.1rel)
===============================
Expand Down
126 changes: 72 additions & 54 deletions docs/index.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/netCDF4.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ cdef extern from "netcdf-compat.h":
int nc_set_alignment(int threshold, int alignment)
int nc_get_alignment(int *threshold, int *alignment)
int nc_rc_set(char* key, char* value) nogil
const_char_ptr *nc_rc_get(char* key)

int nc_open_mem(const char *path, int mode, size_t size, void* memory, int *ncidp) nogil
int nc_create_mem(const char *path, int mode, size_t initialize, int *ncidp) nogil
Expand Down
1 change: 1 addition & 0 deletions include/netcdf-compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ static inline int nc_get_alignment(int* thresholdp, int* alignmentp) {
#else
#define HAS_NCRCSET 0
static inline int nc_rc_set(const char* key, const char* value) { return NC_EINVAL; }
static inline const char *nc_rc_get(const char* key) { return NC_EINVAL; }
#endif

#if NC_VERSION_GE(4, 4, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/netCDF4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
__has_parallel4_support__, __has_pnetcdf_support__,
__has_quantization_support__, __has_zstandard_support__,
__has_bzip2_support__, __has_blosc_support__, __has_szip_support__,
__has_set_alignment__, __has_parallel_support__, __has_ncfilter__)
__has_set_alignment__, __has_parallel_support__, __has_ncfilter__, __has_nc_rc_set__)
import os
__all__ = [
'Dataset', 'Variable', 'Dimension', 'Group', 'MFDataset', 'MFTime', 'CompoundType',
'VLType', 'date2num', 'num2date', 'date2index', 'stringtochar', 'chartostring',
'stringtoarr', 'getlibversion', 'EnumType', 'get_chunk_cache', 'set_chunk_cache',
'set_alignment', 'get_alignment'
'set_alignment', 'get_alignment', 'rc_get', 'rc_set',
]
__pdoc__ = {'utils': False}
# if HDF5_PLUGIN_PATH not set, point to package path if plugins live there
Expand Down
5 changes: 4 additions & 1 deletion src/netCDF4/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ __all__ = [
'Dataset', 'Variable', 'Dimension', 'Group', 'MFDataset', 'MFTime', 'CompoundType',
'VLType', 'date2num', 'num2date', 'date2index', 'stringtochar', 'chartostring',
'stringtoarr', 'getlibversion', 'EnumType', 'get_chunk_cache', 'set_chunk_cache',
'set_alignment', 'get_alignment'
'set_alignment', 'get_alignment', 'rc_get', 'rc_set',
]
__pdoc__ = {'utils': False}

Expand Down Expand Up @@ -87,6 +87,7 @@ __has_blosc_support__: BoolInt
__has_szip_support__: BoolInt
__has_set_alignment__: BoolInt
__has_ncfilter__: BoolInt
__has_nc_rc_set__: BoolInt
is_native_little: bool
is_native_big: bool
default_encoding: Final = 'utf-8'
Expand Down Expand Up @@ -624,6 +625,8 @@ def chartostring(
) -> npt.NDArray[np.str_] | npt.NDArray[np.bytes_]: ...

def getlibversion() -> str: ...
def rc_get(key: str) -> str | None: ...
def rc_set(key: str, value: str)-> None: ...

def set_alignment(threshold: int, alignment: int): ...
def get_alignment() -> tuple[int, int]: ...
Expand Down
3 changes: 2 additions & 1 deletion src/netCDF4/_netCDF4.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ from . import (
stringtoarr, getlibversion, EnumType, get_chunk_cache, set_chunk_cache,
set_alignment, get_alignment, default_fillvals, default_encoding,
NetCDF4MissingFeatureException, is_native_big, is_native_little, unicode_error,
rc_get, rc_set,
__version__, __netcdf4libversion__, __hdf5libversion__, __has_rename_grp__,
__has_nc_inq_path__, __has_nc_inq_format_extended__, __has_nc_open_mem__,
__has_nc_create_mem__, __has_cdf5_format__, __has_parallel4_support__,
__has_pnetcdf_support__, __has_parallel_support__,
__has_quantization_support__, __has_zstandard_support__,
__has_bzip2_support__, __has_blosc_support__, __has_szip_support__,
__has_set_alignment__, __has_ncfilter__
__has_set_alignment__, __has_ncfilter__, __has_nc_rc_set__,
)


Expand Down
66 changes: 56 additions & 10 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Version 1.7.1
"""Version 1.7.2
-------------
# Introduction
Expand Down Expand Up @@ -1272,7 +1272,7 @@ import sys
import functools
from typing import Union

__version__ = "1.7.1.post1"
__version__ = "1.7.2"

# Initialize numpy
import posixpath
Expand Down Expand Up @@ -1308,15 +1308,61 @@ __has_blosc_support__ = HAS_BLOSC_SUPPORT
__has_szip_support__ = HAS_SZIP_SUPPORT
__has_set_alignment__ = HAS_SET_ALIGNMENT
__has_ncfilter__ = HAS_NCFILTER
__has_nc_rc_set__ = HAS_NCRCSET


# set path to SSL certificates (issue #1246)
# available starting in version 4.9.1
if HAS_NCRCSET:
if __has_nc_rc_set__:
import certifi
if nc_rc_set("HTTP.SSL.CAINFO", _strencode(certifi.where())) != 0:
raise RuntimeError('error setting path to SSL certificates')

def rc_get(key):
"""
**```rc_get(key)```**
Returns the internal netcdf-c rc table value corresponding to key.
"""
cdef int ierr
cdef char *keyc
cdef char *valc
if __has_nc_rc_set__:
bytestr = _strencode(_tostr(key))
keyc = bytestr
valc = <char *>nc_rc_get(keyc)
if valc is NULL:
return None
else:
return valc.decode('utf-8')
else:
raise RuntimeError(
"This function requires netcdf-c 4.9.0+ to be used at compile time"
)

def rc_set(key, value):
"""
**```rc_set(key, value)```**
Sets the internal netcdf-c rc table value corresponding to key.
"""
cdef int ierr
cdef char *keyc
cdef char *valuec
if __has_nc_rc_set__:
key_bytestr = _strencode(_tostr(key))
keyc = key_bytestr
val_bytestr = _strencode(_tostr(value))
valuec = val_bytestr
with nogil:
ierr = nc_rc_set(keyc,valuec)
_ensure_nc_success(ierr)
else:
raise RuntimeError(
"This function requires netcdf-c 4.9.0+ to be used at compile time"
)



# check for required version of netcdf-4 and hdf5.
def _gethdf5libversion():
Expand All @@ -1330,7 +1376,7 @@ def _gethdf5libversion():

def getlibversion():
"""
**`getlibversion()`**
**```getlibversion()```**
returns a string describing the version of the netcdf library
used to build the module, and when it was built.
Expand All @@ -1339,7 +1385,7 @@ used to build the module, and when it was built.

def get_chunk_cache():
"""
**`get_chunk_cache()`**
**```get_chunk_cache()```**
return current netCDF chunk cache information in a tuple (size,nelems,preemption).
See netcdf C library documentation for `nc_get_chunk_cache` for
Expand All @@ -1355,7 +1401,7 @@ details. Values can be reset with `set_chunk_cache`."""

def set_chunk_cache(size=None,nelems=None,preemption=None):
"""
**`set_chunk_cache(self,size=None,nelems=None,preemption=None)`**
**```set_chunk_cache(size=None,nelems=None,preemption=None)```**
change netCDF4 chunk cache settings.
See netcdf C library documentation for `nc_set_chunk_cache` for
Expand Down Expand Up @@ -1383,7 +1429,7 @@ details."""


def get_alignment():
"""**`get_alignment()`**
"""**```get_alignment()```**
return current netCDF alignment within HDF5 files in a tuple
(threshold,alignment). See netcdf C library documentation for
Expand All @@ -1394,7 +1440,7 @@ def get_alignment():

if not __has_set_alignment__:
raise RuntimeError(
"This function requires netcdf4 4.9.0+ to be used at compile time"
"This function requires netcdf-c 4.9.0+ to be used at compile time"
)

cdef int ierr
Expand All @@ -1407,7 +1453,7 @@ def get_alignment():


def set_alignment(threshold, alignment):
"""**`set_alignment(threshold,alignment)`**
"""**```set_alignment(threshold,alignment)```**
Change the HDF5 file alignment.
See netcdf C library documentation for `nc_set_alignment` for
Expand All @@ -1417,7 +1463,7 @@ def set_alignment(threshold, alignment):

if not __has_set_alignment__:
raise RuntimeError(
"This function requires netcdf4 4.9.0+ to be used at compile time"
"This function requires netcdf-c 4.9.0+ to be used at compile time"
)

cdef int ierr
Expand Down
20 changes: 20 additions & 0 deletions test/test_ncrc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
import netCDF4
from netCDF4 import __has_nc_rc_set__

class NCRCTestCase(unittest.TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def runTest(self):
"""test rc_get, rc_set functions"""
if __has_nc_rc_set__:
netCDF4.rc_set('foo','bar')
assert netCDF4.rc_get('foo') == 'bar'
assert netCDF4.rc_get('bar') == None

if __name__ == '__main__':
unittest.main()

0 comments on commit 84e3dd5

Please sign in to comment.