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).

  4. The namespace pragma only accepts CIM namespace name and not full CIM namespace path as defined in DSP0004. If the host component is part of the namespace name in a namespace pragma, the compiler generates an exception.

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 (byte string or unicode string). The default logger function prints to stdout. If None, compiler messages are not logged.

Methods:

compile_embedded_value(mof, ns[, filename])

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(filename, ns)

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

compile_string(mof, ns[, filename])

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

conn_close()

Close the underlying connection, if it is a WBEMConnection.

find_mof(classname)

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

rollback([verbose])

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

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. If None, the default namespace of the connection is used. The namespace specified in this parameter must exist.

  • 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 the target of the compilation, and is also used for lookup of any dependent CIM elements. If None, the default namespace of the connection is used. A namespace defined in a namespace pragma of the MOF superceeds this namespace from the point in the compilation unit(string/file) where it is declared. The namespace specified in this parameter or the MOF inamespace pragma must exist.

Raises
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 the target of the compilation, and is also used for lookup of any dependent CIM elements. If None, the default namespace of the connection is used. A namespace defined in a MOF ‘#pragma namespace’ directive supersedes this namespace from the point in the compilation unit(string/file) where it is declared. The namespace specified in this parameter or in the MOF ‘#pragma namespace’ directive must exist.

  • 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.

conn_close()[source]

Close the underlying connection, if it is a WBEMConnection.

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(*args, **kwargs)

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

CreateInstance(*args, **kwargs)

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

DeleteClass(*args, **kwargs)

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

DeleteInstance(*args, **kwargs)

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

DeleteQualifier(*args, **kwargs)

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

EnumerateInstanceNames(*args, **kwargs)

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

EnumerateQualifiers(*args, **kwargs)

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

GetClass(*args, **kwargs)

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

GetQualifier(*args, **kwargs)

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

ModifyClass(*args, **kwargs)

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

ModifyInstance(*args, **kwargs)

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

SetQualifier(*args, **kwargs)

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

Attributes:

default_namespace

The default repository namespace.

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

abstract 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().

property default_namespace

The default repository namespace.

This property is settable.

Type

string

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.

Attributes:

__cause__

exception cause

__context__

exception context

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.

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.

Methods:

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__new__(**kwargs)

Create and return a new object.

__reduce__

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__str__()

Return str(self).

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.

__str__()[source]

Return str(self).

property column

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

Type

integer

property 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

property 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

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

property lineno

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

Type

integer

property msg

Message text describing the error.

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.

Attributes:

__cause__

exception cause

__context__

exception context

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.

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.

Methods:

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__new__(**kwargs)

Create and return a new object.

__reduce__

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__str__()

Return str(self).

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.

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.

Attributes:

__cause__

exception cause

__context__

exception context

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.

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.

Methods:

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__new__(**kwargs)

Create and return a new object.

__reduce__

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__str__()

Return str(self).

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.

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.

Attributes:

__cause__

exception cause

__context__

exception context

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.

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.

Methods:

__delattr__(name, /)

Implement delattr(self, name).

__getattribute__(name, /)

Return getattr(self, name).

__new__(**kwargs)

Create and return a new object.

__reduce__

Helper for pickle.

__repr__()

Return repr(self).

__setattr__(name, value, /)

Implement setattr(self, name, value).

__str__()

Return str(self).

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.

property 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