Search code examples
yamljythonibm-was

Read YAML from Jython


I'd like to know how I could read a YAML file from a Jython script.

I have created a Jython script calling Websphere Application Server commands to create several datasources, virtual hosts, name space bindings etc.

However, for now values are hard coded in the script, and lots of Jython code is duplicated because using arrays is not convenient.

Ideally I'd like to have something like this, in an external file, read by the Jython script:

Cell
  cellName: Cell01
  JAAS
    "alias1"
      aliasName: "j2cALiasA"
      aliasDesc: "First j2cAlias"
    "alias2"
      aliasName: "j2cALiasB"
      aliasDesc: "Second j2cAlias"
      Node:
        nodeName: Node01
        JAASAuthData:
          jdbcProviderType: ...
        Server
          serverName: server-1
          datasources
            "datasource1"
              datasourceName: "jdbc/datasource1"
              datasourceAuthDataAlias: 

And loop over those different objects (I am not sure about the YAML syntax here but it's just for the example)

How could I do that? Is there a YAML parser for Jython? I can't find anything.

If you have other suggestions about externalizing configuration for WAS Admin Jython scripts it will also be useful :)

SOLUTION

For WAS 8.5 I had to switch to Jython 2.7 by using a Thin client that I created with this procedure: http://www.ibm.com/developerworks/websphere/library/techarticles/1207_vansickel/1207_vansickel.html.

Then I had to manually download PyYAML-3.11 package and edit its setup.py because otherwise you get this error http://pyyaml.org/ticket/163. So I've just used this:

def ext_status(self, ext):
    return False

And then installed the package from the archive:

<THIN_CLIENT_HOME>/lib/jython/bin/pip install /root/PyYAML-3.11.tar.gz

And you execute the jython script like this:

./thinClient.sh -port 9809 -host websphere-1 -f /root/yaml.py

Solution

  • Your data is not really YAML, there are a few colons missing and a few unnecessary quotes:

    Cell:
      cellName: Cell01
      JAAS:
        alias1:
          aliasName: j2cALiasA
          aliasDesc: First j2cAlias
        alias2:
          aliasName: j2cALiasB
          aliasDesc: Second j2cAlias
          Node:
            nodeName: Node01
            JAASAuthData:
              jdbcProviderType: ...
            Server:
              serverName: server-1
              datasources:
                datasource1:
                  datasourceName: jdbc/datasource1
                  datasourceAuthDataAlias:
    

    Put it that way, it properly parses/loads under Jython 2.7.0 on Linux, with ruamel.yaml (disclaimer: I am the author of that package). You can install that package with pip install ruamel.yaml).