Python - MenuMaker 日本語化 - アイコン - Fluxbox

 クラウディア
1. Fluxbox
2. ソース

1. Fluxbox

 ここから以降は、デスクトップごとのモジュールになります。  これは、「Fluxbox」の処理を行うモジュールです。  「Fluxbox」のメニューの構造は

[submenu] (カテゴリ名) <アイコンファイル名>
    [exec] (プログラム名) {ロードモジュール} <アイコンファイル名>
[end]
 という形式になっています。  サブカテゴリを分ける場合は、「[submenu]」ネストさせます。  「[exec]」の行は、縦に並べて、カテゴリ内に複数表示します。  「<アイコンファイル名>」は、オリジナルの「MenuMaker」では、カッコを含めて何も出力されません。  何故か、「FreeBSD」の「Fluxbox」は、「.png」も「.svg」も表示できないので、すべて「.xpm」に変換してやる必要があります。

2. ソース

 ソースは、以下になります。
##
# @file Fluxbox.py
# @version 0.1
# @author show.kit・更新者
# @date 2020年6月5日-2020年6月5日
# @brief Fluxbox の環境変数設定ファイル

import os
import pprint
import re
import sys

##
# @class Fluxbox
# @brief Fluxbox の環境変数設定

class Fluxbox:

    ## Fluxbox::__init__
    # @brief 初期化関数
    # @return 戻り値なし
    def __init__(self):

        ## デスクトップ名
        self.frontend = 'Fluxbox'

        ## メニューファイル名
        self.filename = os.environ.get('HOME') + '/.fluxbox/menu'

        ## 項目名の先頭括弧
        self.prefix = "("

        ## 項目名の括弧閉じ
        self.suffix = ")"

        ## カスタムアイコンディレクトリ
        self.icon_directory = os.environ.get('HOME') + '/.fluxbox/icons'

        if (not os.path.exists(self.icon_directory)):
            os.mkdir(self.icon_directory)

        ## メニューのプログラム行検出キーワード
        self.exec_keyword = '[exec]'

    ##
    # メニューのカテゴリ行を日本語に変える
    # @param env            環境クラス
    # @param line           対象行
    # @param label_en       カテゴリ名英語
    # @param label_jp       カテゴリ名日本語
    # @param directory_icon カテゴリアイコンのディレクトリ
    # @param icon           アイコンファイル(拡張子なし)
    # @return line          変換後の行

    def update_category(self, env, line, label_en, label_jp, directory_icon, icon):

        ## カテゴリ名を英語から日本語へ

        line = line.replace(self.prefix + label_en + self.suffix, self.prefix + label_jp + self.suffix)

        ## メニューアイコンの .svg は使えないので .xpm に変換して使用する
        if (not os.path.exists(self.icon_directory)):                           ## カスタムディレクトリがなければ
            os.mkdir(self.icon_directory)                                       ## 作成する

        icon_fullpath = self.icon_directory + '/' + icon + '.xpm'               ## アイコンのフルパス

        if (not os.path.exists(icon_fullpath)):                                 ## アイコンファイルがなければ
            from_file_path = directory_icon + '/' + icon + '.svg'               ## .svg を変換して
            env.convert(from_file_path, icon_fullpath)                          ## .xpm へする

        ## icon 追加

        line = line.rstrip('\n')                                                ## 末尾を削除
        return line + " <" + icon_fullpath + ">\n"                              ## icon 定義を追加して戻す

    ##
    # @brief applications/*.desktop を検索して
    #    プログラム名 → アイコンファイル名の辞書を返す
    #    Fluxbox と違って png で良い
    # @param env           環境変数クラス
    # @param dic_exec_icon .desktop より取得した編集前の exec → icon 辞書
    # @return exec → icon ファイル名フルパス の辞書

    def get_dic_exec_icon(self, env, dic_exec_icon):

        ## exe → icon 辞書の妥当性をチェック
        ## 妥当でなければ変更

        ## 有効な拡張子、ディレクトリ(どちらも優先順位順)

        effect_extend = [ '.xpm', '.png', '.svg' ]
        effect_directory = [ env.directory_xpm, env.directory_png, env.directory_icon_apps ]

        for key, value in dic_exec_icon.items():                                ## exec → icon 辞書作成
            if (os.path.exists(value)):                                         ## icon が存在する
                dirname, name, extend = env.mysplit(value)                      ## フルパスを分解

                if (extend == effect_extend[0]):                                ## 拡張子が .xpm である
                    continue                                                    ## continue

                ## 拡張子が .png や .svg の場合

                if ((extend == effect_extend[1]) or (extend == effect_extend[2])):
                    to_icon = self.icon_directory + '/' + name + effect_extend[0]
                    env.convert(value, to_icon)                                 ## 変換して
                    dic_exec_icon[key] = to_icon                                ## .xpm を設定
                    continue                                                    ## 成功しても失敗しても continue

            elif(not '.' in value):                                             ## icon が存在しない . を含まない
                for extend in effect_extend:                                    ## 拡張子として有効なものを検索

                    for directory in effect_directory:
                        icon = directory + '/' + value + extend                 ## icon ファイル名組み立て

                        if (os.path.exists(icon)):                              ## 存在する
                            if (extend != effect_extend[0]):                    ## .xpm ではない

                                ## .xpm へ変換

                                to_icon = self.icon_directory + '/' + value + effect_extend[0]

                                env.convert(icon, to_icon)
                                icon = to_icon

                            dic_exec_icon[key] = icon                           ## icon として設定する
                            break                                               ## break
                    else:
                        continue
                    break
            else:                                                               ## . を含む
                ## 苦し紛れの特殊処理(これは、あんまりだけれども)

                if (value == 'org.gnome.nautilus'):
                    value += '.svg'

                for directory in effect_directory:                              ## 有効なディレクトリ順に検索
                    icon = directory + "/" + value

                    if (os.path.exists(icon)):                                  ## フルパス/ファイル名 が存在する
                        dirname, name, extend = env.mysplit(icon)               ## フルパスを分解

                        for ef_extend in effect_extend:                         ## 拡張子として有効なものを検索
                            if (ef_extend == extend):                           ## 有効なもの

                                if (extend != effect_extend[0]):                ## 拡張子が .xpm でなければ

                                    ## .xpm へ変換

                                    to_icon = self.icon_directory + '/' + value + effect_extend[0]

                                    env.convert(icon, to_icon)
                                    icon = to_icon

                                dic_exec_icon[key] = icon                       ## icon として設定する
                                break                                           ##
                else:
                    continue
                break

        return dic_exec_icon;

    ##
    # @brief  icon をメニューに設定する
    # @param  line     メニューの対象行
    # @param  nextline 対象行の次の行(Fluxbox では使用しない)
    # @param  dic_exec_icon exec → icon 辞書
    # @return line 更新した行

    def set_menu_icon(self, line, nextline, dic_exec_icon):

        match = re.search(r"\{.*\}" , line)                                     ## {...} に挟まれた部分を取り出し

        if (' ' in match.group()):
            exename = match.group().split(' ')[0]
            exename = exename[1:]
        else:
            exename = match.group()[1:-1]

        if (exename in dic_exec_icon):                                          ## exec → icon 辞書に exe が存在するならば
            line = line.rstrip('\n')                                            ## 末尾を削除
            return line + " <" + dic_exec_icon[exename] + ">\n"                 ## icon 定義を追加して戻す

        return line                                                             ## 更新がない場合そのまま返す


ハイスピードプラン
損保との違い