pyomo.common.tempfiles

class pyomo.common.tempfiles.TempfileManagerClass[source]

A class for managing tempfile contexts

Pyomo declares a global instance of this class as TempfileManager:

>>> from pyomo.common.tempfiles import TempfileManager

This class provides an interface for managing TempfileContext contexts. It implements a basic stack, where users can push() a new context (causing it to become the current “active” context) and pop() contexts off (optionally deleting all files associated with the context). In general usage, users will either use this class to create new tempfile contexts and use them explicitly (i.e., through a context manager):

>>> import os
>>> with TempfileManager.new_context() as tempfile:
...     fd, fname = tempfile.mkstemp()
...     dname = tempfile.mkdtemp()
...     os.path.isfile(fname)
...     os.path.isdir(dname)
True
True
>>> os.path.exists(fname)
False
>>> os.path.exists(dname)
False

or through an implicit active context accessed through the manager class:

>>> TempfileManager.push()
<pyomo.common.tempfiles.TempfileContext object ...>
>>> fname = TempfileManager.create_tempfile()
>>> dname = TempfileManager.create_tempdir()
>>> os.path.isfile(fname)
True
>>> os.path.isdir(dname)
True

>>> TempfileManager.pop()
<pyomo.common.tempfiles.TempfileContext object ...>
>>> os.path.exists(fname)
False
>>> os.path.exists(dname)
False
create_tempfile(suffix=None, prefix=None, text=False, dir=None)[source]

Call TempfileContext.create_tempfile() on the active context

create_tempdir(suffix=None, prefix=None, dir=None)[source]

Call TempfileContext.create_tempdir() on the active context

add_tempfile(filename, exists=True)[source]

Call TempfileContext.add_tempfile() on the active context

clear_tempfiles(remove=True)[source]

Delete all temporary files and remove all contexts.

sequential_files(ctr=0)[source]

DEPRECATED.

Deprecated since version TBD: The TempfileManager.sequential_files() method has been removed. All temporary files are created with guaranteed unique names. Users wishing sequentially numbered files should create a temporary (empty) directory using mkdtemp / create_tempdir and place the sequential files within it.

new_context()[source]

Create and return an new tempfile context

Returns:the newly-created tempfile context
Return type:TempfileContext
push()[source]

Create a new tempfile context and set it as the active context.

Returns:the newly-created tempfile context
Return type:TempfileContext
pop(remove=True)[source]

Remove and release the active context

Parameters:remove (bool) – If True, delete all managed files / directories
class pyomo.common.tempfiles.TempfileContext(manager)[source]

A context for managing collections of temporary files

Instances of this class hold a “temporary file context”. That is, this records a collection of temporary file system objects that are all managed as a group. The most common use of the context is to ensure that all files are deleted when the context is released.

This class replicates a significant portion of the tempfile module interface.

Instances of this class may be used as context managers (with the temporary files / directories getting automatically deleted when the context manager exits).

Instances will also attempt to delete any temporary objects from the filesystem when the context falls out of scope (although this behavior is not guaranteed for instances existing when the interpreter is shutting down).

mkstemp(suffix=None, prefix=None, dir=None, text=False)[source]

Create a unique temporary file using tempfile.mkstemp()

Parameters are handled as in tempfile.mkstemp(), with the exception that the new file is created in the directory returned by gettempdir()

Returns:
  • fd (int) – the opened file descriptor
  • fname (str or bytes) – the absolute path to the new temporary file
mkdtemp(suffix=None, prefix=None, dir=None)[source]

Create a unique temporary directory using tempfile.mkdtemp()

Parameters are handled as in tempfile.mkdtemp(), with the exception that the new file is created in the directory returned by gettempdir()

Returns:dname – the absolute path to the new temporary directory
Return type:str or bytes
gettempdir()[source]

Return the default name of the directory used for temporary files.

This method returns the first non-null location returned from:

  • This context’s tempdir (i.e., self.tempdir)
  • This context’s manager’s tempdir (i.e., self.manager().tempdir)
  • tempfile.gettempdir()
Returns:dir – The default directory to use for creating temporary objects
Return type:str
gettempdirb()[source]

Same as gettempdir(), but the return value is bytes

gettempprefix()[source]

Return the filename prefix used to create temporary files.

See tempfile.gettempprefix()

gettempprefixb()[source]

Same as gettempprefix(), but the return value is bytes

create_tempfile(suffix=None, prefix=None, text=False, dir=None)[source]

Create a unique temporary file.

The file name is generated as in tempfile.mkstemp().

Any file handles to the new file (e.g., from mkstemp()) are closed.

Returns:fname – The absolute path of the new file.
Return type:str or bytes
create_tempdir(suffix=None, prefix=None, dir=None)[source]

Create a unique temporary directory.

The file name is generated as in tempfile.mkdtemp().

Returns:dname – The absolute path of the new directory.
Return type:str or bytes
add_tempfile(filename, exists=True)[source]

Declare the specified file/directory to be temporary.

This adds the specified path as a “temporary” object to this context’s list of managed temporary paths (i.e., it will be potentially be deleted when the context is released (see release()).

Parameters:
  • filename (str) – the file / directory name to be treated as temporary
  • exists (bool) – if True, the file / directory must already exist.
release(remove=True)[source]

Release this context

This releases the current context, potentially deleting all managed temporary objects (files and directories), and resetting the context to generate unique names.

Parameters:remove (bool) – If True, delete all managed files / directories