2

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

1 Answers1

0

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

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Thank you for your help. I ended up using PyYaml because I found more examples with this (I'm very new to Python) but it helped. I had to do some tricks on WAS environment though, I will list them in my question. – Guillaume Lucazeau Oct 09 '15 at 13:28
  • @Kemper I think you'll be hard pressed to find any examples for PyYAML that don't work with ruamel.yaml. The latter is a superset (with several new features, bug fixes) and AFAIK still backwards compatible. At least all ruamel.yaml passes all PyYAML unittests (but not vv). But the documentation on ruamel.yaml sucks, I have to admit that. – Anthon Oct 09 '15 at 14:57
  • I trust you on that, I didn't mean to offense you. I've just struggled a lot with configuring Jython with WAS and then import the packages, so I somehow ended up using PyYAML (I am not familiar at all with Python and packages management). I only need really basic operations (just needed list that are not available in .ini files read by Jython) so all the nice features provided by your package are too complex for me. So I didn't discard your package on purpose, just went with the flow and used what worked first. However, your answer put me on tracks about YAML processing and package management. – Guillaume Lucazeau Oct 09 '15 at 15:06