Skip to content

Commit b51e089

Browse files
committed
Method cleanups from #75.
1 parent 832ea12 commit b51e089

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

lsl/common/_metabundle_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ def get_asp_configuration(tarname, which='beginning'):
207207
if which == 'end' and mib.name.find('_ASP_end') == -1:
208208
continue
209209

210-
aspMIB = MIB()
211-
aspMIB.from_file(os.path.join(tempDir, mib.name))
210+
aspMIB = MIB.from_file(os.path.join(tempDir, mib.name))
212211
break
213212

214213
# Extract the configuration

lsl/common/mcs.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -930,42 +930,41 @@ def parse_init_file(self, filename):
930930
# Done
931931
return True
932932

933-
def from_file(self, filename, init_filename=None):
933+
@classmethod
934+
def from_file(klass, filename, init_filename=None):
934935
"""
935936
Given the name of a GDBM database file, initialize the MIB.
936937
Optionally, use the name of the MCS MIB initialization file to
937938
help convert indicies to names.
938939
"""
939940

940941
# Parse the init. file (if we have one)
942+
mib = klass()
941943
if init_filename is not None:
942-
self.parse_init_file(init_filename)
944+
mib.parse_init_file(init_filename)
943945

944946
# Make sure we have the .pag file
945947
if filename[-4:] == '.dir':
946948
filename = filename.replace('.dir', '.pag')
947949

948950
# Open the database
949-
db = dbm.open(filename, 'ru')
950-
951-
# Go!
952-
entry = db.firstkey()
953-
while entry is not None:
954-
value = db[entry]
955-
956-
try:
957-
record = MIBEntry()
958-
record.from_entry(value)
959-
self.entries[record.index] = record
951+
with dbm.open(filename, 'ru') as db:
952+
# Go!
953+
entry = db.firstkey()
954+
while entry is not None:
955+
value = db[entry]
960956

961-
except ValueError:
962-
pass
957+
try:
958+
record = MIBEntry.from_entry(value)
959+
mib.entries[record.index] = record
960+
961+
except ValueError:
962+
pass
963+
964+
entry = db.nextkey(entry)
963965

964-
entry = db.nextkey(entry)
965-
db.close()
966-
967966
# Done
968-
return True
967+
return mib
969968

970969

971970
class MIBEntry(object):
@@ -999,6 +998,7 @@ def __str__(self):
999998

1000999
return f"Index: {self.index}; Value: {self.value}; Updated at {self.updateTime}"
10011000

1001+
@staticmethod
10021002
def _parse_value(self, value, dataType):
10031003
"""
10041004
Convert an encoded value to something Pythonic (if possible).
@@ -1087,7 +1087,8 @@ def _parse_value(self, value, dataType):
10871087
else:
10881088
raise ValueError(f"Unknown data type '{dataType}'")
10891089

1090-
def from_entry(self, value):
1090+
@classmethod
1091+
def from_entry(klass, value):
10911092
"""
10921093
Given an MIB entry straight out of a GDBM database, populate the
10931094
MIBEntry instance.
@@ -1136,14 +1137,15 @@ def from_entry(self, value):
11361137
raise ValueError(f"Entry index '{record.index}' does not appear to be numeric")
11371138

11381139
# Basic information
1139-
self.eType = int(record.eType)
1140-
self.index = index
1141-
self.value = self._parse_value(record.val, dbmType)
1142-
self.dbmType = dbmType
1143-
self.icdType = icdType
1144-
self._tv = (int(record.tv[0]), int(record.tv[1]))
1140+
mibe = klass()
1141+
mibe.eType = int(record.eType)
1142+
mibe.index = index
1143+
mibe.value = mibe._parse_value(record.val, dbmType)
1144+
mibe.dbmType = dbmType
1145+
mibe.icdType = icdType
1146+
mibe._tv = (int(record.tv[0]), int(record.tv[1]))
11451147

11461148
# Time
1147-
self.updateTime = datetime.utcfromtimestamp(record.tv[0] + record.tv[1]/1e9)
1149+
mibe.updateTime = datetime.utcfromtimestamp(record.tv[0] + record.tv[1]/1e9)
11481150

1149-
return True
1151+
return mibe

0 commit comments

Comments
 (0)