diff --git a/brpylib/brpylib.py b/brpylib/brpylib.py index ed07623..b32bdc7 100644 --- a/brpylib/brpylib.py +++ b/brpylib/brpylib.py @@ -507,7 +507,10 @@ def __init__(self, datafile=""): self.datafile = datafile self.basic_header = {} self.extended_headers = [] - + + self.openfile() + + def openfile(self): # Run openfilecheck and open the file passed or allow user to browse to one self.datafile = openfilecheck( "rb", @@ -1003,6 +1006,13 @@ def close(self): name = self.datafile.name self.datafile.close() print("\n" + name.split("/")[-1] + " closed") + + # add "enter" and "exit" methods for compatibility with "with": + def __enter__(self): + return self # thankfully this returns a reference, not a copy + + def __exit__(self,*args): + self.close() class NsxFile: @@ -1016,7 +1026,10 @@ def __init__(self, datafile=""): self.datafile = datafile self.basic_header = {} self.extended_headers = [] - + + self.openfile() + + def openfile(self): # Run openfilecheck and open the file passed or allow user to browse to one self.datafile = openfilecheck( "rb", @@ -1720,3 +1733,10 @@ def close(self): name = self.datafile.name self.datafile.close() print("\n" + name.split("/")[-1] + " closed") + + # add "enter" and "exit" methods for compatibility with "with": + def __enter__(self): + return self # thankfully this returns a reference, not a copy + + def __exit__(self,*args): + self.close()