Skip to content

Commit

Permalink
Fixed runtime errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Oct 14, 2024
1 parent cb8cd8a commit 53aab70
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
6 changes: 4 additions & 2 deletions pysmi/borrower/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
# License: https://www.pysnmp.com/pysmi/license.html
#
from pysmi import debug, error
from pysmi.reader.base import AbstractReader


class AbstractBorrower:
genTexts = False
exts = ""
_reader: "AbstractReader"

def __init__(self, reader, genTexts=False):
"""Creates an instance of *Borrower* class.
Expand All @@ -31,7 +33,7 @@ def __str__(self):
return f"{self.__class__.__name__}{{{self._reader}, genTexts={self.genTexts}, exts={self.exts}}}"

def set_options(self, **kwargs):
self._reader.setOptions(**kwargs)
self._reader.set_options(**kwargs)

for k in kwargs:
setattr(self, k, kwargs[k])
Expand All @@ -52,4 +54,4 @@ def get_data(self, mibname, **options):
if "exts" not in options:
options["exts"] = self.exts

return self._reader.getData(mibname, **options)
return self._reader.get_data(mibname, **options)
43 changes: 17 additions & 26 deletions pysmi/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
import time
import warnings

from pysmi.codegen.base import AbstractCodeGen
from pysmi.reader.base import AbstractReader
from pysmi.searcher.base import AbstractSearcher
from pysmi.writer.base import AbstractWriter

from pysmi import __name__ as package_name
from pysmi import __version__ as package_version
from pysmi import debug
from pysmi import error
from pysmi.borrower.base import AbstractBorrower
from pysmi.codegen.base import AbstractCodeGen
from pysmi.codegen.symtable import SymtableCodeGen
from pysmi.mibinfo import MibInfo
Expand Down Expand Up @@ -83,6 +79,8 @@ class MibCompiler:
indexFile = "index"
_searchers: list[AbstractSearcher]
_sources: list[AbstractReader]
_borrowers: list[AbstractBorrower]
_parsedMibs: dict[str, tuple]

def __init__(self, parser, codegen: AbstractCodeGen, writer: AbstractWriter):
"""Creates an instance of *MibCompiler* class.
Expand Down Expand Up @@ -286,8 +284,7 @@ class instances (values)
)
continue

except error.PySmiError:
exc_class, exc, tb = sys.exc_info()
except error.PySmiError as exc:
exc.source = source
exc.mibname = mibname
exc.msg += f" at MIB {mibname}"
Expand Down Expand Up @@ -331,7 +328,7 @@ class instances (values)
for searcher in self._searchers:
try:
searcher.file_exists(
mibname, fileInfo.mtime, rebuild=options.get("rebuild")
mibname, fileInfo.mtime, rebuild=options.get("rebuild") # type: ignore
)

except error.PySmiFileNotFoundError:
Expand All @@ -348,8 +345,7 @@ class instances (values)
processed[mibname] = status_untouched
break

except error.PySmiError:
exc_class, exc, tb = sys.exc_info()
except error.PySmiError as exc:
exc.searcher = searcher
exc.mibname = mibname
exc.msg += f" at MIB {mibname}"
Expand Down Expand Up @@ -412,8 +408,7 @@ class instances (values)
f"{mibname} read from {fileInfo.path} and compiled by {self._writer}"
)

except error.PySmiError:
exc_class, exc, tb = sys.exc_info()
except error.PySmiError as exc:
exc.handler = self._codegen
exc.mibname = mibname
exc.msg += f" at MIB {mibname}"
Expand Down Expand Up @@ -447,7 +442,7 @@ class instances (values)
f"trying to borrow {mibname} from {borrower}"
)
try:
fileInfo, fileData = borrower.getData(
fileInfo, fileData = borrower.get_data(
mibname, genTexts=options.get("genTexts")
)

Expand Down Expand Up @@ -487,7 +482,7 @@ class instances (values)
for searcher in self._searchers:
try:
searcher.file_exists(
mibname, fileInfo.mtime, rebuild=options.get("rebuild")
mibname, fileInfo.mtime, rebuild=options.get("rebuild") # type: ignore
)

except error.PySmiFileNotFoundError:
Expand All @@ -504,8 +499,7 @@ class instances (values)
processed[mibname] = status_untouched
break

except error.PySmiError:
exc_class, exc, tb = sys.exc_info()
except error.PySmiError as exc:
exc.searcher = searcher
exc.mibname = mibname
exc.msg += f" at MIB {mibname}"
Expand Down Expand Up @@ -570,7 +564,7 @@ class instances (values)
try:
if options.get("writeMibs", True):
self._writer.put_data(
mibname, mibData, dryRun=options.get("dryRun")
mibname, mibData, dryRun=options.get("dryRun") # type: ignore
)

debug.logger & debug.FLAG_COMPILER and debug.logger(
Expand Down Expand Up @@ -624,17 +618,16 @@ def build_index(self, processedMibs, **options):
]

try:
self._writer.putData(
self._writer.put_data(
self.indexFile,
self._codegen.genIndex(
processedMibs,
comments=comments,
old_index_data=self._writer.getData(self.indexFile),
old_index_data=self._writer.get_data(self.indexFile),
),
dryRun=options.get("dryRun"),
dryRun=options.get("dryRun"), # type: ignore
)
except error.PySmiError:
exc_class, exc, tb = sys.exc_info()
except error.PySmiError as exc:
exc.msg += f" at MIB index {self.indexFile}"

debug.logger & debug.FLAG_COMPILER and debug.logger(
Expand All @@ -644,10 +637,7 @@ def build_index(self, processedMibs, **options):
if options.get("ignoreErrors"):
return

if hasattr(exc, "with_traceback"):
raise exc.with_traceback(tb)
else:
raise exc
raise exc

# compatibility with legacy code
# Old to new attribute mapping
Expand All @@ -657,6 +647,7 @@ def build_index(self, processedMibs, **options):
}

def __getattr__(self, attr: str):
"""Handle deprecated attributes."""
if new_attr := self.deprecated_attributes.get(attr):
warnings.warn(
f"{attr} is deprecated. Please use {new_attr} instead.",
Expand Down
11 changes: 11 additions & 0 deletions pysmi/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@
#


from pysmi.codegen.base import AbstractCodeGen
from pysmi.reader.base import AbstractReader
from pysmi.searcher.base import AbstractSearcher


class PySmiError(Exception):
msg: str
source: AbstractReader
mibname: str
searcher: AbstractSearcher
handler: AbstractCodeGen

def __init__(self, *args, **kwargs):
Exception.__init__(self, *args)
self.msg = args and args[0] or ""
Expand Down

0 comments on commit 53aab70

Please sign in to comment.