CADSはSOAPのインターフェースを持っていますが、Essentials for PythonのPESImpl モジュールを使うとPythonでCADSを操作できます。
PESImplでCADSのレポジトリのコンテンツをリストするpythonスクリプトの実行
このPESImplを使ってCADSレポジトリのコンテンツのリストをつくり、ストリームについてはダウンロードしてみます。
listContentsDownload.pyというpythonスクリプトを作りました。
from pes.api.PESImpl import PESImpl
# 接続
# pesImpl = PESImpl("user", "password", "host", "port", ssl=True, “appserver_type”)
pesImpl = PESImpl("admin", "password", "ms184-win19", "9080",
ssl=False, appserv_type="liberty")
def getChildren(folder):
resourceList = pesImpl.getChildren(source=folder)
if len(resourceList) != 0:
for resource in resourceList:
resource = resource.Resource
# フォルダの場合一つ下のレベルに移動
if 'hasChildren' in resource:
print("Resource title:", resource.ResourcePath.value, 'folder')
getChildren(folder=resource.ResourcePath.value)
# オブジェクトならMimeTypeとVersionを出力
if 'MimeType' in resource:
print("Resource title:", resource.ResourcePath.value,
resource.MimeType.value, resource.Version.marker)
# ストリームの場合はc:/temp/downloadにダウンロードする
if resource.MimeType.value == "application/x-vnd.spss-clementine-stream":
pesImpl.downloadFile(
source=resource.ResourcePath.value, target="c:/temp/download")
# ファイルを探すルートフォルダを指定して実行する
getChildren(folder="/Demo/creditdemo")
# getChildren(folder="/")
このlistContentsDownload.pyをEssentials for Pythonのパスに配置します。
ここでは/Demo/creditdemoのフォルダ内のコンテンツのリストを作り、ストリームファイルであるcreditrisk.strを"c:/temp/download"にダウンロードします。
この例ではフォルダは1階層ですが、フォルダの中にフォルダがある複数階層にも対応しています。
以下のようにlistContentsDownload.pyを呼び出します。
python "E:\Program Files\IBM\SPSS\Deployment\8.4\PythonEssentials\listContentsDownload.py"
以下のようにコンテンツとパスとMimeTypeとバージョンのリストが出力されます。
C:\Users\Administrator>python "E:\Program Files\IBM\SPSS\Deployment\8.4\PythonEssentials\listContentsDownload.py"
Resource title: /Demo/creditdemo/credit application/x-vnd.spss-prms-job 2:2022-07-12 14:19:05.812
Resource title: /Demo/creditdemo/creditrisk_acccuracy.html text/html 4:2022-07-12 14:55:11.741
Resource title: /Demo/creditdemo/リフレッシュ application/x-vnd.spss-prms-job 2:2022-07-12 14:49:51.691
Resource title: /Demo/creditdemo/creditrisk.str application/x-vnd.spss-clementine-stream 5:2022-07-12 14:55:07.506
Resource title: /Demo/creditdemo/モニタリング application/x-vnd.spss-prms-job 4:2022-07-12 14:54:39.581
Resource title: /Demo/creditdemo/creditrisk_acccuracy_eval.tab application/octet-stream 4:2022-07-12 14:55:11.413
Resource title: /Demo/creditdemo/creditrisk_acccuracy_governance.xml text/xml 4:2022-07-12 14:55:12.069
ストリームファイルであるcreditrisk.strは"c:/temp/download"にダウンロードされました。
コード解説
CADSへ接続するためのユーザー、パスワード、ホスト名、ポート番号、SSLのありなし、サーバーのタイプを指定します。appserv_typeはwebsphere、liberty、またはjbossから選択します。
pesImpl = PESImpl("admin", "password", "ms184-win19", "9080",
ssl=False, appserv_type="liberty")
getChildrenメソッドを使ってフォルダ内のリソースを取得します。
resourceList = pesImpl.getChildren(source=folder)
参考:getChildren メソッド - IBM Documentation
フォルダーかどうかを判定しています。
if 'hasChildren' in resource:
オブジェクトの内容を書出しています。
コード | 意味 |
---|---|
ResourcePath.value | CADS内のパス |
resource.MimeType.value | オブジェクトの種類。ストリームなら"application/x-vnd.spss-clementine-stream" |
resource.Version.marker | リソースのバージョン |
print("Resource title:", resource.ResourcePath.value,
resource.MimeType.value, resource.Version.marker)
downloadFileメソッドでストリームをtargetのローカルパスにダウンロードしています。
pesImpl.downloadFile(source=resource.ResourcePath.value, target="c:/temp/download")
参考:The downloadFile method - IBM Documentation
テスト環境
- CADS 8.4
- Essentials for Python
- Python 3.8.10
- zeep 4.1.0
- Windows Server 2019
参考
- PESImpl モジュール - IBM Documentation
- Python 3.8 +Zeepの導入 on Windows Server
Essentials for Pythonの前提となるPython 3.8.xとZeep4.0.0以降を導入します。
- CADS 8.4本体のインストール on Windows Server
Essentials for PythonはIIMで導入します。以下ではCADSレポジトリ本体も導入していますがEssentials for Pythonだけの導入も可能です。