Skip to content

Commit

Permalink
Adding a private attr _fileid for the open class.
Browse files Browse the repository at this point in the history
The _fileid attribute is a sha1 hash that is generated from the
absolute file path, size, and inode number.
  • Loading branch information
EricEngle-NOAA committed Mar 24, 2024
1 parent 5c13e11 commit 24e7df0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ class `grib2io.open`, the file named `filename` is opened for reading (`mode
levels : tuple
Tuple containing a unique list of wgrib2-formatted level/layer strings.
"""
__slots__ = ('_filehandle','_hasindex','_index','mode','name','messages',
'current_message','size','closed','variables','levels','_pos')
__slots__ = ('_fileid', '_filehandle', '_hasindex', '_index', '_pos',
'closed', 'current_message', 'levels', 'messages', 'mode',
'name', 'size', 'variables')
def __init__(self, filename: str, mode: str="r", **kwargs):
"""
Initialize GRIB2 File object instance.
Expand Down Expand Up @@ -129,16 +130,19 @@ def __init__(self, filename: str, mode: str="r", **kwargs):
else:
self._filehandle = builtins.open(filename, mode=mode)

self.name = os.path.abspath(filename)
fstat = os.stat(self.name)
self._hasindex = False
self._index = {}
self.mode = mode
self.name = os.path.abspath(filename)
self.messages = 0
self.current_message = 0
self.size = os.path.getsize(self.name)
self.size = fstat.st_size
self.closed = self._filehandle.closed
self.levels = None
self.variables = None
self._fileid = hashlib.sha1((self.name+str(fstat.st_ino)+
str(self.size)).encode('ASCII')).hexdigest()
if 'r' in self.mode:
try:
self._build_index(no_data=kwargs['_xarray_backend'])
Expand Down

0 comments on commit 24e7df0

Please sign in to comment.