8. MOF compiler

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

MOF compilers take MOF files as input, compile them and use the result (CIM classes, instances, and/or qualifier declarations) to update a target CIM repository. The repository may initially be empty, or may contain CIM classes, instances, and/or qualifier declarations that are used to resolve dependencies the new MOF compilation may have.

The pywbem package includes a MOF compiler that is provided in two forms:

  • as an API (described in this chapter)
  • as a command (described in section mof_compiler)

The pywbem MOF compiler will compile MOF files whose syntax complies with DSP0004, with some limitations:

  1. Although there is no formal keyword list of illegal words for property/parameter.etc. names , there is a list of mof syntax tokens in DSP0004 section A.3. Generally these should not be used as property names. The pywbem MOF compiler largely enforces this so that words like ‘indication’ are not allowed as property/parameter/etc. names.
  2. The key properties of instances with aliases must be initialized in the instance specification, or their default values must be non-NULL. (See pywbem issue #1079).
  3. An alias must be defined before it is used. In DSP0004, no such requirement is documented. (See pywbem issue #1079).

The MOF compiler API provides for invoking the MOF compiler programmatically. It consists of the following parts, which are described in the remaining sections of this chapter:

  • MOFCompiler Class - Describes the MOFCompiler class, which allows invoking the MOF compiler programmatically.
  • Repository connections - Describes the BaseRepositoryConnection class that defines the interface for connecting to a CIM repository. This is an extension point where users can implement a CIM repository for use by the MOF compiler.
  • Exceptions - Describes the exceptions that can be raised by the MOF compiler API.

8.1. MOFCompiler Class

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

A MOF compiler. See MOF compiler for an explanation of MOF compilers in general.

A MOF compiler may be associated with one 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 of a MOF compiler with a CIM repository is established when creating an object of this class. The interactions with the CIM repository are defined in the abstract base class BaseRepositoryConnection.

Parameters:
  • handle (BaseRepositoryConnection or WBEMConnection) –

    A handle identifying the CIM repository that will be associated with the MOF compiler.

    If the provided object is a repository connection (i.e. derived from BaseRepositoryConnection, typically that would be a MOFWBEMConnection object), it is directly used by the MOF compiler to interface with the repository.

    If the provided object is a WBEM connection (i.e. WBEMConnection or FakedWBEMConnection), the MOF compiler connects directly to interface defined by handle.

    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 or string) –

    Directory path name(s) where the MOF compiler will search for MOF dependent files if the MOF element they define is not in the target namespace of the CIM repository. The compiler searches the specified directories and their subdirectories.

    MOF dependent files are:

    • The MOF file defining the superclass of a class that is compiled. The MOF file searched for is ‘<classname>.mof’.
    • The MOF file defining the qualifier type of a qualifier that is specified on a MOF element that is compiled. The MOF files searched for are ‘qualifiers.mof’ and ‘qualifiers_optional.mof’.
    • The MOF file of a class specified in a reference property or in the EmbeddedInstance qualifier that is compiled. The MOF file searched for is ‘<classname>.mof’. This is only partly implemented, see issue #1138.

    Note that MOF include files are not searched for; they are specified in the pragma include directive as a file path relative to the including file.

  • 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 function prints to stdout. If None, compiler messages are not logged.

Methods

compile_embedded_value Compile a string of MOF statements that must represent one or CIMInstance objects and save the resulting CIMInstance(s) in a known variable.
compile_file Compile a MOF file into a namespace of the associated CIM repository.
compile_string Compile a string of MOF statements into a namespace of the associated CIM repository.
find_mof Find the MOF file that defines a particular CIM class, in the search path of the MOF compiler.
rollback Rollback any changes to the CIM repository that were performed by compilations using this MOF compiler object, since the object was created.

Attributes

Details

compile_embedded_value(mof, ns, filename=None)[source]

Compile a string of MOF statements that must represent one or CIMInstance objects and save the resulting CIMInstance(s) in a known variable. Thiis method in conjunction with the compiler does not put the compiled instances into the repository but returns them to the user.

Parameters:
  • mof (string or list of string) – The string or list of strings 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:

MOFCompileError – Error compiling the MOF.

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:

MOFCompileError – Error compiling the MOF.

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:
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:Path name of the MOF file defining the CIM class, if it was found. None, if it was not found.
Return type:string
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.

8.2. Repository connections

class pywbem.BaseRepositoryConnection[source]

An abstract base class for implementing CIM repository connections (or an entire CIM repository) for use by the MOF compiler. This class defines the interface that is used by the MOFCompiler class when it interacts with its associated CIM repository.

Class MOFCompiler invokes only the WBEM operations that are defined as methods on this class:

Raises:Implementation classes should raise only exceptions derived from Error or use assert for methods that are not implemented. Other exceptions are considered programming errors.

Methods

CreateClass Create a CIM class in a namespace of the CIM repository.
CreateInstance Create a CIM instance in a namespace of the CIM repository.
DeleteClass Delete a CIM class in a namespace of the CIM repository.
DeleteInstance Delete a CIM instance in a namespace of the CIM repository.
DeleteQualifier Delete a CIM qualifier type in a namespace of the CIM repository.
EnumerateInstanceNames Enumerate the instance paths of CIM instances in a namespace of the CIM repository.
EnumerateQualifiers Enumerate the CIM qualifier types in a namespace of the CIM repository.
GetClass Retrieve a CIM class in a namespace of the CIM repository.
GetQualifier Retrieve a CIM qualifier type in a namespace of the CIM repository.
ModifyClass Modify a CIM class in a namespace of the CIM repository.
ModifyInstance Modify a CIM instance in a namespace of the CIM repository.
SetQualifier Create or modify a CIM qualifier type in a namespace of the CIM repository.

Attributes

default_namespace The default repository namespace.

Details

default_namespace

The default repository namespace.

This property is settable.

Type:string
EnumerateInstanceNames(*args, **kwargs)[source]

Enumerate the instance paths of CIM instances in a namespace of the CIM repository.

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

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

Create a CIM instance in a namespace of the CIM repository.

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

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

Modify a CIM instance in a namespace of the CIM repository.

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

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

Delete a CIM instance in a namespace of the CIM repository.

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

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

Retrieve a CIM class in a namespace of the CIM repository.

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

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

Modify a CIM class in a namespace of the CIM repository.

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

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

Create a CIM class in a namespace of the CIM repository.

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

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

Delete a CIM class in a namespace of the CIM repository.

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

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

Enumerate the CIM qualifier types in a namespace of the CIM repository.

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

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

Retrieve a CIM qualifier type in a namespace of the CIM repository.

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

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

Create or modify a CIM qualifier type in a namespace of the CIM repository.

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

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

Delete a CIM qualifier type in a namespace of the CIM repository.

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

8.3. Exceptions

The MOF compiler API raises exceptions that are derived from the common base class MOFCompileError.

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

Base class for exceptions indicating issues with compiling MOF.

Derived from Error.

Parameters:
  • msg (string) – Message text describing the error.
  • parser_token (lex.LexToken or yacc.YaccProduction) –

    PLY lex or yacc parser token (that is, the p argument of a yacc parser function or the t argument of a lex parser function). This token is used to obtain the MOF source text and location information.

    None will result in no MOF source text and location information to be obtained.

Methods

get_err_msg Return a multi-line error message for being printed, in the following format.
with_traceback Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Attributes

args
column Position within the line in the MOF file or MOF string where the error occurred (1-based).
conn_id Connection ID of the connection in whose context the error happened.
conn_str String that identifies the connection in exception messages.
context MOF context, consisting of a first line that is the MOF line in error, and a second line that uses the ‘^’ character to indicate the position of the token in error in the MOF line.
error_kind
file File name of the MOF file where the error occurred.
lineno Line number in the MOF file or MOF string where the error occurred (1-based).
msg Message text describing the error.

Details

lineno

Line number in the MOF file or MOF string where the error occurred (1-based).

Type:integer
column

Position within the line in the MOF file or MOF string where the error occurred (1-based).

Type:integer
file

File name of the MOF file where the error occurred.

None if the error occurred in a MOF string that was compiled.

Type:string
context

MOF context, consisting of a first line that is the MOF line in error, and a second line that uses the ‘^’ character to indicate the position of the token in error in the MOF line.

Type:string
msg

Message text describing the error.

Type:string
get_err_msg()[source]

Return a multi-line error message for being printed, in the following format. The text in angle brackets refers to the same-named properties of the exception instance:

<error kind>:<file>:<lineno>:<colno>: <msg>
<context - MOF line>
<context - position indicator line>
Returns:Multi-line error message.
Return type:string
class pywbem.MOFParseError(msg, parser_token=None)[source]

Exception indicating that MOF cannot be parsed correctly, e.g. for syntax errors.

Derived from MOFCompileError.

Parameters:
  • msg (string) – Message text describing the error.
  • parser_token (lex.LexToken or yacc.YaccProduction) –

    PLY lex or yacc parser token (that is, the p argument of a yacc parser function or the t argument of a lex parser function). This token is used to obtain the MOF source text and location information.

    None will result in no MOF source text and location information to be obtained.

Methods

get_err_msg Return a multi-line error message for being printed, in the following format.
with_traceback Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Attributes

args
column Position within the line in the MOF file or MOF string where the error occurred (1-based).
conn_id Connection ID of the connection in whose context the error happened.
conn_str String that identifies the connection in exception messages.
context MOF context, consisting of a first line that is the MOF line in error, and a second line that uses the ‘^’ character to indicate the position of the token in error in the MOF line.
error_kind
file File name of the MOF file where the error occurred.
lineno Line number in the MOF file or MOF string where the error occurred (1-based).
msg Message text describing the error.

Details

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

Exception indicating that MOF cannot be compiled because of a missing dependency. For example, the MOF to be compiled specifies a class but the superclass of the class cannot be found.

Derived from MOFCompileError.

Parameters:
  • msg (string) – Message text describing the error.
  • parser_token (lex.LexToken or yacc.YaccProduction) –

    PLY lex or yacc parser token (that is, the p argument of a yacc parser function or the t argument of a lex parser function). This token is used to obtain the MOF source text and location information.

    None will result in no MOF source text and location information to be obtained.

Methods

get_err_msg Return a multi-line error message for being printed, in the following format.
with_traceback Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Attributes

args
column Position within the line in the MOF file or MOF string where the error occurred (1-based).
conn_id Connection ID of the connection in whose context the error happened.
conn_str String that identifies the connection in exception messages.
context MOF context, consisting of a first line that is the MOF line in error, and a second line that uses the ‘^’ character to indicate the position of the token in error in the MOF line.
error_kind
file File name of the MOF file where the error occurred.
lineno Line number in the MOF file or MOF string where the error occurred (1-based).
msg Message text describing the error.

Details

class pywbem.MOFRepositoryError(msg, parser_token=None, cim_error=None)[source]

Exception indicating that MOF cannot be compiled because of a CIM error returned by the target CIM repository. The CIM error is attached to this exception and is part of the exception message.

Derived from MOFCompileError.

Parameters:
  • msg (string) – Message text describing the error.
  • parser_token (lex.LexToken or yacc.YaccProduction) –

    PLY lex or yacc parser token (that is, the p argument of a yacc parser function or the t argument of a lex parser function). This token is used to obtain the MOF source text and location information.

    None will result in no MOF source text and location information to be obtained.

  • cim_error (CIMError) – CIM error returned by the CIM repository.

Methods

get_err_msg Return a multi-line error message for being printed, in the following format.
with_traceback Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Attributes

args
cim_error CIM error returned by the CIM repository.
column Position within the line in the MOF file or MOF string where the error occurred (1-based).
conn_id Connection ID of the connection in whose context the error happened.
conn_str String that identifies the connection in exception messages.
context MOF context, consisting of a first line that is the MOF line in error, and a second line that uses the ‘^’ character to indicate the position of the token in error in the MOF line.
error_kind
file File name of the MOF file where the error occurred.
lineno Line number in the MOF file or MOF string where the error occurred (1-based).
msg Message text describing the error.

Details

cim_error

CIM error returned by the CIM repository.

Type:CIMError
get_err_msg()[source]

Return a multi-line error message for being printed, in the following format. The text in angle brackets refers to the same-named properties of the exception instance:

<error kind>:<file>:<lineno>:<colno>: <msg>
<context - MOF line>
<context - position indicator line>
<CIM error>
Returns:Multi-line error message.
Return type:string