すべてインラインテーブルで記述したもののなんか見づらい。
なんかいい方法はないかな、と模索。
Python 3.11 で同梱された tomllib
を使って別フォーマットで整形して表示してみます。
👉 How to Use Python 3.11's New TOML Parser, tomllib - The Invent with Python Blog
記事にするには大きすぎるので [plugins]
だけで。
import tomllib
import pprint
import json
import yaml
import pandas
import tabulate
file = "/path/to/libs.versions.toml"
with open(file, "rb") as fp:
data = tomllib.load(fp)["plugins"] # only plugins
# raw (dic)
print(data)
# pretty print
pprint.pprint(data)
# json
print(json.dumps(data, indent=2))
# yaml
print(yaml.dump(data))
# pandas + tabulate
df = pandas.DataFrame.from_dict(data, orient='index')
print(tabulate.tabulate(df, headers='keys'))
以下、出力結果。
■ raw (dic)
{'android-application': {'id': 'com.android.application', 'version': {'ref': 'agp'}}, 'kotlin-android': {'id': 'org.jetbrains.kotlin.android', 'version': {'ref': 'kotlin'}}, 'kotlin-kapt': {'id': 'org.jetbrains.kotlin.kapt', 'version': {'ref': 'kotlin'}}, 'kotlin-plugin-serialization': {'id': 'org.jetbrains.kotlin.plugin.serialization', 'version': {'ref': 'kotlin'}}, 'hilt': {'id': 'com.google.dagger.hilt.android', 'version': {'ref': 'hilt'}}, 'sqldelight': {'id': 'app.cash.sqldelight', 'version': {'ref': 'sqldelight'}}, 'google-services': {'id': 'com.google.gms.google-services', 'version': {'ref': 'google-services'}}, 'firebase-crashlytics': {'id': 'com.google.firebase.crashlytics', 'version': {'ref': 'firebase-crashlytics'}}, 'ben-manes-versions': {'id': 'com.github.ben-manes.versions', 'version': '0.44.0'}}
■ pretty print
{'android-application': {'id': 'com.android.application',
'version': {'ref': 'agp'}},
'ben-manes-versions': {'id': 'com.github.ben-manes.versions',
'version': '0.44.0'},
'firebase-crashlytics': {'id': 'com.google.firebase.crashlytics',
'version': {'ref': 'firebase-crashlytics'}},
'google-services': {'id': 'com.google.gms.google-services',
'version': {'ref': 'google-services'}},
'hilt': {'id': 'com.google.dagger.hilt.android', 'version': {'ref': 'hilt'}},
'kotlin-android': {'id': 'org.jetbrains.kotlin.android',
'version': {'ref': 'kotlin'}},
'kotlin-kapt': {'id': 'org.jetbrains.kotlin.kapt',
'version': {'ref': 'kotlin'}},
'kotlin-plugin-serialization': {'id': 'org.jetbrains.kotlin.plugin.serialization',
'version': {'ref': 'kotlin'}},
'sqldelight': {'id': 'app.cash.sqldelight', 'version': {'ref': 'sqldelight'}}}
■ json
{
"android-application": {
"id": "com.android.application",
"version": {
"ref": "agp"
}
},
"kotlin-android": {
"id": "org.jetbrains.kotlin.android",
"version": {
"ref": "kotlin"
}
},
"kotlin-kapt": {
"id": "org.jetbrains.kotlin.kapt",
"version": {
"ref": "kotlin"
}
},
"kotlin-plugin-serialization": {
"id": "org.jetbrains.kotlin.plugin.serialization",
"version": {
"ref": "kotlin"
}
},
"hilt": {
"id": "com.google.dagger.hilt.android",
"version": {
"ref": "hilt"
}
},
"sqldelight": {
"id": "app.cash.sqldelight",
"version": {
"ref": "sqldelight"
}
},
"google-services": {
"id": "com.google.gms.google-services",
"version": {
"ref": "google-services"
}
},
"firebase-crashlytics": {
"id": "com.google.firebase.crashlytics",
"version": {
"ref": "firebase-crashlytics"
}
},
"ben-manes-versions": {
"id": "com.github.ben-manes.versions",
"version": "0.44.0"
}
}
■ yaml
android-application:
id: com.android.application
version:
ref: agp
ben-manes-versions:
id: com.github.ben-manes.versions
version: 0.44.0
firebase-crashlytics:
id: com.google.firebase.crashlytics
version:
ref: firebase-crashlytics
google-services:
id: com.google.gms.google-services
version:
ref: google-services
hilt:
id: com.google.dagger.hilt.android
version:
ref: hilt
kotlin-android:
id: org.jetbrains.kotlin.android
version:
ref: kotlin
kotlin-kapt:
id: org.jetbrains.kotlin.kapt
version:
ref: kotlin
kotlin-plugin-serialization:
id: org.jetbrains.kotlin.plugin.serialization
version:
ref: kotlin
sqldelight:
id: app.cash.sqldelight
version:
ref: sqldelight
■ pandas + tabulate
id version
--------------------------- ----------------------------------------- -------------------------------
android-application com.android.application {'ref': 'agp'}
kotlin-android org.jetbrains.kotlin.android {'ref': 'kotlin'}
kotlin-kapt org.jetbrains.kotlin.kapt {'ref': 'kotlin'}
kotlin-plugin-serialization org.jetbrains.kotlin.plugin.serialization {'ref': 'kotlin'}
hilt com.google.dagger.hilt.android {'ref': 'hilt'}
sqldelight app.cash.sqldelight {'ref': 'sqldelight'}
google-services com.google.gms.google-services {'ref': 'google-services'}
firebase-crashlytics com.google.firebase.crashlytics {'ref': 'firebase-crashlytics'}
ben-manes-versions com.github.ben-manes.versions 0.44.0
■ まとめ
Python って、簡単にいろんなことできるんですね!