6. MOF compiler API

The language in which CIM classes are specified, is called MOF (for Managed Object Format). It is defined in DSP0004.

The pywbem package includes a MOF compiler.

MOF compilers take MOF files as input, compile them and the result is used to update a target CIM repository. The repository may initially be empty, or may contain the result of earlier MOF compilations that are used to resolve dependencies the new MOF compilation may have.

The MOF compiler in this package also has an option to remove CIM elements from the repository it has a definition for in the MOF files it processes.

The MOF compiler API provides for invoking the MOF compiler and for plugging in your own CIM repository into the MOF compiler.

The MOF compiler API is available in the pywbem.mof_compiler module.

This chapter has the following sections:

6.1. MOFCompiler

class pywbem.mof_compiler.MOFCompiler(handle, search_paths=None, verbose=False, log_func=<function _print_logger>)[source]

A MOF compiler.

A MOF compiler is associated with a CIM repository. The repository is used for looking up dependent CIM elements (e.g. the superclass specified in a class whose MOF definition is being compiled), and it is also updated with the result of the compilation. A repository contains CIM namespaces, and the namespaces contain CIM classes, instances and qualifier types.

The association with a CIM repository is established when creating an instance of this class. The interactions with the CIM repository are defined in the abstract base class BaseRepositoryConnection.

Parameters:
  • handle (BaseRepositoryConnection) –

    A connection to the CIM repository that will be associated with the MOF compiler.

    None means that no CIM repository will be associated. In this case, the MOF compiler can only process standalone MOF that does not depend on existing CIM elements in the repository.

  • search_paths (iterable of string) – An iterable of path names of directories where MOF include files should be looked up.
  • verbose (bool) – Indicates whether to issue more detailed compiler messages.
  • log_func (callable) – A logger function that is invoked for each compiler message. The logger function must take one parameter of string type. The default logger prints to stdout.
compile_string(mof, ns, filename=None)[source]

Compile a string of MOF statements into a namespace of the associated CIM repository.

Parameters:
  • mof (string) – The string of MOF statements to be compiled.
  • ns (string) – The name of the CIM namespace in the associated CIM repository that is used for lookup of any dependent CIM elements, and that is also the target of the compilation.
  • filename (string) – The path name of the file that the MOF statements were read from. This information is used only in compiler messages.
Raises:
  • MOFParseError – Syntax error in the MOF.
  • Any exceptions that are raised by the repository connection class.
compile_file(filename, ns)[source]

Compile a MOF file into a namespace of the associated CIM repository.

Parameters:
  • filename (string) – The path name of the MOF file containing the MOF statements to be compiled.
  • ns (string) – The name of the CIM namespace in the associated CIM repository that is used for lookup of any dependent CIM elements, and that is also the target of the compilation.
Raises:
  • MOFParseError – Syntax error in the MOF.
  • Any exceptions that are raised by the repository connection class.
find_mof(classname)[source]

Find the MOF file that defines a particular CIM class, in the search path of the MOF compiler.

The MOF file is found based on its file name: It is assumed that the base part of the file name is the CIM class name.

Example: The class “CIM_ComputerSystem” is expected to be in a file “CIM_ComputerSystem.mof”.

Parameters:classame (string) – The name of the CIM class to look up.
Returns:A string with the path name of the MOF file, if it was found. None, otherwise.
rollback(verbose=False)[source]

Rollback any changes to the CIM repository that were performed by compilations using this MOF compiler object, since the object was created.

6.2. Repository connections

class pywbem.mof_compiler.BaseRepositoryConnection[source]

An abstract base class for implementing repository connections for the MOF compiler. This class defines the interface that is used by the MOF compiler (class MOFCompiler) when it interacts with the repository.

Class MOFCompiler invokes only the operations that are defined as methods on this class. Specifically, it does not invoke:

  • EnumerateInstances
  • GetInstance
  • EnumerateClasses
  • EnumerateClassNames
  • association or query operations
  • method invocations

Exceptions:

Implementation classes should raise only exceptions derived from Error. Other exceptions are considered programming errors.
default_namespace

The default repository namespace, as a string (readable and writeable).

EnumerateInstanceNames(*args, **kwargs)[source]

Enumerate instance paths of CIM instances in the repository.

For a description of the parameters, see pywbem.WBEMConnection.EnumerateInstanceNames().

CreateInstance(*args, **kwargs)[source]

Create a CIM instance in the repository.

For a description of the parameters, see pywbem.WBEMConnection.CreateInstance().

ModifyInstance(*args, **kwargs)[source]

Modify a CIM instance in the repository.

For a description of the parameters, see pywbem.WBEMConnection.ModifyInstance().

DeleteInstance(*args, **kwargs)[source]

Delete a CIM instance in the repository.

For a description of the parameters, see pywbem.WBEMConnection.DeleteInstance().

GetClass(*args, **kwargs)[source]

Retrieve a CIM class from the repository.

For a description of the parameters, see pywbem.WBEMConnection.GetClass().

ModifyClass(*args, **kwargs)[source]

Modify a CIM class in the repository.

For a description of the parameters, see pywbem.WBEMConnection.ModifyClass().

CreateClass(*args, **kwargs)[source]

Create a CIM class in the repository.

For a description of the parameters, see pywbem.WBEMConnection.CreateClass().

DeleteClass(*args, **kwargs)[source]

Delete a CIM class in the repository.

For a description of the parameters, see pywbem.WBEMConnection.DeleteClass().

EnumerateQualifiers(*args, **kwargs)[source]

Enumerate the qualifier types in the repository.

For a description of the parameters, see pywbem.WBEMConnection.EnumerateQualifiers().

GetQualifier(*args, **kwargs)[source]

Retrieve a qualifier type from the repository.

For a description of the parameters, see pywbem.WBEMConnection.GetQualifier().

SetQualifier(*args, **kwargs)[source]

Create or modify a qualifier type in the repository.

For a description of the parameters, see pywbem.WBEMConnection.SetQualifier().

DeleteQualifier(*args, **kwargs)[source]

Delete a qualifier type from the repository.

For a description of the parameters, see pywbem.WBEMConnection.DeleteQualifier().

class pywbem.mof_compiler.MOFWBEMConnection(conn=None)[source]

A repository connection that stores CIM elements locally in the instance of this class. It also supports removal of CIM elements via its rollback() method, by rolling back the changes applied locally to the instance.

It is instantiated on top of an underlying repository connection that is connected with the CIM repository that is actually being updated.

This class implements the BaseRepositoryConnection interface.

Exceptions:

The methods of this class may raise any exceptions described for class WBEMConnection.
Parameters:conn (BaseRepositoryConnection) –

The underlying repository connection.

None means that there is no underlying repository and all operations performed through this object will fail.

getns()

Return either connection default or universal default namespace

setns(value)

Set the namespace in value into either the connection default or package wide default namespace

default_namespace

The default repository namespace, as a string (readable and writeable).

EnumerateInstanceNames(*args, **kwargs)[source]

This method is used by the MOF compiler only when it creates a namespace in the course of handling CIM_ERR_NAMESPACE_NOT_FOUND. Because the operations of this class silently create every namespace that is needed and never return that error, this method is never called, and is therefore not implemented.

ModifyInstance(*args, **kwargs)[source]

This method is used by the MOF compiler only in the course of handling CIM_ERR_ALREADY_EXISTS after trying to create an instance. Because CreateInstance() overwrites existing instances, this method is never called, and is therefore not implemented.

CreateInstance(*args, **kwargs)[source]

Create a CIM instance in the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.CreateInstance().

DeleteInstance(*args, **kwargs)[source]

This method is only invoked by rollback() (on the underlying repository), and never by the MOF compiler, and is therefore not implemented.

GetClass(*args, **kwargs)[source]

Retrieve a CIM class from the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.GetClass().

ModifyClass(*args, **kwargs)[source]

This method is used by the MOF compiler only in the course of handling CIM_ERR_ALREADY_EXISTS after trying to create a class. Because CreateClass() overwrites existing classes, this method is never called, and is therefore not implemented.

CreateClass(*args, **kwargs)[source]

Create a CIM class in the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.CreateClass().

DeleteClass(*args, **kwargs)[source]

This method is only invoked by rollback() (on the underlying repository), and never by the MOF compiler, and is therefore not implemented.

EnumerateQualifiers(*args, **kwargs)[source]

Enumerate the qualifier types in the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.EnumerateQualifiers().

GetQualifier(*args, **kwargs)[source]

Retrieve a qualifier type from the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.GetQualifier().

SetQualifier(*args, **kwargs)[source]

Create or modify a qualifier type in the local repository of this class.

For a description of the parameters, see pywbem.WBEMConnection.SetQualifier().

DeleteQualifier(*args, **kwargs)[source]

This method is only invoked by rollback() (on the underlying repository), and never by the MOF compiler, and is therefore not implemented.

rollback(verbose=False)[source]

Remove classes and instances from the underlying repository, that have been created in the local repository of this class.

Limitation: At this point, only classes and instances will be removed, but not qualifiers.

6.3. Exceptions

class pywbem.mof_compiler.MOFParseError(parser_token=None, msg=None)[source]

Exception raised when MOF cannot be parsed correctly, e.g. for syntax errors. Derived from Error.

Parameters:
  • parser_token – PLY parser token for the error (that is, the p argument of a PLY parser function). This token contains information on the location of the error in the MOF file, which is copied into this object, and is accessible via properties.
  • msg (string) – Message text supplied by the creator of the error
lineno

Line number in the MOF file where the error occurred.

column

Position within the line where the error occurred.

file

File name of the MOF file where the error occurred, as a string.

context

Context string that can be inserted when printing the error message. The context string consists of a first line with a segment of the MOF surrounding the error position, and a second line that uses the ‘^’ character to indicate the token in error.

msg

Message that may be part of the error, as a string. Generally, this is produced when the actual error position is not known but may be added by some production errors.

get_err_msg()[source]

Return the MOF compiler error message as a string, in the format (assuming all components are provided):

Syntax error:<file>:<lineno>: <msg>
<context - MOF segment>
<context - location indicator>