- 1. モジュール構成
- 2. ユーザインタフェース jmmaker.py
- 3. メイン CLI.py
1. モジュール構成
モジュールは、ログインユーザのホームディレクトリに展開するものとして、以下のような構造になっています。
jmenumaker
┣ CLI.py
┣ jmmaker.py
┗ MenuMaker
┣ __init__.py
┣ CLI.py
┣ Env.py
┣ Fluxbox.py
┣ IceWM.py
┣ JWM.py
┗ Openbox.py
このうち、「__init__.py」は、実はよくわかっていないので、ソース内には何も記述していません。
実行文のあるソースに関しては、「Doxygen」でドキュメント化できるようにコメントをいれています。
「Doxgen」によるドキュメント化に関しては、「Eclipse - ドキュメント自動化」をご参照ください。
2. ユーザインタフェース jmmaker.py
ユーザインタフェースを提供するのは、「jmmaker.py」になります。
python3.7 ~/jmenumaker/jmmaker.py
で起動します(バージョンはインストールしている「python」によって異なりますが、「3.6」か「3.7」以外では動作確認していません)。
ソースは下記の通りで
import os
import sys; sys.path = [os.getcwd()] + sys.path
import MenuMaker.CLI
MenuMaker.CLI.main()
「CLI.main()」をコールするのみです。
3. メイン CLI.py
これがメインのプログラムになります。
##
# @file CLI.py
# @version 0.1
# @author 作成:Show.Kit
# @date 2020年6月6日
# @brief jmmaker のほぼメイン
import os
import pprint
import subprocess
import MenuMaker
from MenuMaker import Env
##
# カテゴリ名の 英語:日本語 辞書
label_dict = {
"Configure" : "設定",
"Development" : "開発",
"Editors" : "エディタ",
"Exit" : "終了",
"Files" : "ファイル",
"GNOME" : "GNOME",
"Graphics" : "グラフィックス",
"Multimedia" : "マルチメディア",
"Network" : "ネットワーク",
"Office" : "オフィス",
"OpenBox" : "OpenBox",
"Reconfig" : "再設定",
"Reconfigure" : "再設定",
"Restart" : "再起動",
"Settings" : "設定",
"Shells" : "シェル",
"Styles" : "スタイル",
"System" : "システム",
"Utilities" : "ユーティリティ",
"Workspaces" : "ワークスペース",
}
##
# カテゴリに対するアイコン
dic_category_icon = {
"Configure" : "configurator",
"Development" : "applications-development",
"Editors" : "accessories-text-editor",
"Exit" : "computer-log-out",
"Files" : "system-file-manager",
"GNOME" : "gnome_apps",
"Graphics" : "applications-graphics",
"Multimedia" : "applications-multimedia",
"Network" : "applications-internet",
"OpenBox" : "openbox",
"Office" : "applications-office",
"Reconfig" : "system-reboot",
"Reconfigure" : "system-reboot",
"Restart" : "system-restart",
"Settings" : "configurator",
"Shells" : "terminal",
"Styles" : "style",
"System" : "applications-system",
"Utilities" : "applications-utilities",
"Workspaces" : "workspace-switcher",
}
##
# メニューファイル読み込み
# @brief メニューファイルのフルパスをもらい、読み込んで全行を返す
# @param filename メニューファイル名フルパス
# @return lines メニューファイル全行
def read_menu(filename):
f = open(filename, "r") ## File Open
lines = f.readlines() ## Read
f.close() ## Close
return lines
##
# @brief メニューファイルの内容書き換え
# @param lines 更新前 メニューファイルの中身
# @param env プラットフォーム特有の項目
# @param dic_exec_icon exec : icon 辞書
def update_menu(lines, env, dic_exec_icon):
for i in range(0, len(lines)-1): ## 全行検索
for key, value in label_dict.items(): ## カテゴリ名辞書検索
if (env.prefix + key + env.suffix in lines[i]): ## カテゴリ名のラベルであれば
try: ## 更新
lines[i] = env.update_category(lines[i], key, value, dic_category_icon[key])
except AttributeError:
pass
## exec キーワードが含まれている行であれば icon 設定
try:
if (env.exec_keyword != None):
if (env.exec_keyword in lines[i]):
lines[i] = env.set_menu_icon(lines[i], lines[i+1], dic_exec_icon)
except AttributeError:
pass
env.set_end_menu(lines) # 終了メニュー書込み
##
# exec → icon 辞書を作成する
# @param env 環境クラス
# @return dic_exec_icon exec → icon 辞書
def get_dic_exec_icon(env):
## exec → icon 辞書
dic_exec_icon = None
try:
dic_exec_icon = env.get_dic_exec_icon() ## get_dic_exec_icon メソッドがあれば検索
except AttributeError:
pass
return dic_exec_icon
##
# @brief 出来上がったメニューを更新する
# @param filename メニューファイル フルパス
# @param lines メニューの全行
def write_menu(filename, lines):
f = open(filename, "w") ## File Open
f.writelines(lines) ## Write
f.close() ## Close
##
# @brief メイン
def main():
env = Env.Env() ## 環境設定
if (not os.path.exists(env.mmaker)):
print('MenuMaker を先にインストールしてください')
return
print("mmaker -vf --no-debian --no-legacy --no-merge "+env.name)
result = subprocess.call(['mmaker', '-vf', '--no-debian', '--no-legacy', '--no-merge', env.name])
l = read_menu(env.filename)
lines = list()
for i in l:
lines.append(i)
dic_exec_icon = get_dic_exec_icon(env) ## exec → icon 辞書作成
update_menu(lines, env, dic_exec_icon)
write_menu(env.filename, lines)
if __name__=='__main__':
main()
14~38行が、カテゴリ名の日本語変換用辞書です。
40~64行が、カテゴリ名からアイコンを参照する際の辞書です。
136~159行の大雑把な流れを説明しておくと。
137行で、環境設定(Env)クラスのコンストラクタで、オペレーティングシステム・デスクトップの情報を取得して、本プログラムの環境変数を設定しておきます。
144行で、「MenuMaker」本体を実行します。
146行は、上記で作成したメニューの読み込み。
153行は、「applications/*.desktop」のファイルから、「ロードモジュール」→「アイコンファイル」の辞書を作成。
155行は、これまでの情報を元に、145行で読み込んだ元のメニュー内のカテゴリ名を日本語化したり、アイコンファイルの定義を記述します。
156行で、更新した内容で、メニューファイルを書き換えて終了、という段取りです。
|