Skip to content

各種 CLI 小工具

Wei-Hsiang (Matt) Wang edited this page Nov 6, 2023 · 5 revisions

PO (portable object) 是乘載翻譯原文與譯文的常用檔案格式之一,以 PO 為基礎已經有各種工具被開發出來,這份文件會講解幾項常用 CLI 工具的目的與用法。

powrap

  • 用途:PO 檔案自動格式化(自動斷行、換行)
  • 專案連結:https://git.afpy.org/AFPy/powrap
  • 安裝方式:pip install powrap
  • 使用方式:
    • powrap {po path}(為某份 PO 檔案做 formatting)
  • 備註:我們有設定 powrap 的 pre-commit hook,只要用以下指令安裝後便會自動在每次 git commit 時自動檢查該 commit 改動並格式化
    pip install pre-commit
    pre-commit install

potodo

  • 用途:列出專案中每個 PO 檔案的完成度(已完成的檔案則會被隱藏)
  • 專案連結:https://git.afpy.org/AFPy/potodo
  • 安裝方式:pip install potodo
  • 使用方式:
    • potodo(列出所有未完成檔案的字串數量與完成度)
      > potodo
      # extending (3.17% done)
      
      - building.po                      0 /  23 (  0.0% translated)
      - embedding.po                     1 /  45 (  2.0% translated)
      - extending.po                     2 / 158 (  1.0% translated)
      - newtypes.po                      0 /  91 (  0.0% translated)
      - newtypes_tutorial.po             0 / 123 (  0.0% translated)
      - windows.po                       0 /  21 (  0.0% translated)
      
      
      # faq (10.27% done)
      
      - design.po                       13 / 141 (  9.0% translated), 2 fuzzy
      - extending.po                     0 /  60 (  0.0% translated)
      - general.po                      33 /  91 ( 36.0% translated)
      - gui.po                           2 /  17 ( 11.0% translated)
      - library.po                      11 / 142 (  7.0% translated)
      - programming.po                  13 / 372 (  3.0% translated)
      - windows.po                       3 /  47 (  6.0% translated)
      ...
      
    • potodo --only-fuzzy(只列出 fuzzy entries 數量)
      > potodo -f
      # c-api (5.20% done)
      
      - datetime.po                      8 /  41 ( 19.0% translated), 5 fuzzy
      - dict.po                          5 /  30 ( 16.0% translated), 1 fuzzy
      - function.po                      4 /  18 ( 22.0% translated), 5 fuzzy
      - unicode.po                       5 / 334 (  1.0% translated), 1 fuzzy
      
      
      # distutils (0.00% done)
      
      - examples.po                      0 /  46 (  0.0% translated), 1 fuzzy
      
      
      # faq (9.22% done)
      
      - design.po                       13 / 141 (  9.0% translated), 2 fuzzy
      ...
      

pogrep

  • 用途:在專案中搜尋某字詞的所有現有譯文
  • 專案連結:https://git.afpy.org/AFPy/pogrep
  • 安裝方式:pip install pogrep
  • 使用方式:
    • pogrep --recursive {term}(搜索包含某詞的現有譯文)
      > pogrep --recursive metadata
       ╒════════════════════════════════════════════════════════╤══════════════════════════════════════════════════════════════════════════════════════════════════╕
      │ :ref:`Function annotations <function>` are completely  │ :ref:`函式註釋 <function>`\                                                                      │
      │ optional metadata information about the types used by  │ 是選擇性的元資料(metadata)資訊,描述使用者定義函式所使用的型別(更多資訊詳見                   │
      │ user-defined functions (see :pep:`3107` and :pep:`484` │ :pep:`3107` 和 :pep:`484`\ )。                                                                  │
      │ for more information).                                 │                                                                                                  │
      ├────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
      │ the `Python Packaging Authority                        │ `Python 封裝管理站 (Python Packaging Authority)                                                  │
      │ <https://www.pypa.io/>`__ is the group of developers   │ <https://www.pypa.io/>`__                                                                        │
      │ and documentation authors responsible for the          │ 是一個由開發者和說明文件作者組成的團隊,負責維護及改進標準封裝工具,以及相關的元資料 (metadata)  │
      │ maintenance and evolution of the standard packaging    │ 和檔案格式標準。他們在 `GitHub <https://github.com/pypa>`__                                      │
      │ tools and the associated metadata and file format      │ 平台上維護各種工具、說明文件及問題追蹤系統。                                                     │
      │ standards. They maintain a variety of tools,           │                                                                                                  │
      │ documentation, and issue trackers on `GitHub           │                                                                                                  │
      │ <https://github.com/pypa>`__.                          │                                                                                                  │
      ├────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
      ...
      
    • pogrep --recursive --word-regexp {regex}(下 regex 搜索現有譯文)
      > pogrep --recursive --word-regexp "ABC[a-zA-Z]{4}"
       ╒═════════════════════════════════════════════════════════════════════════════════════╤═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╕
      │ The :class:`collections.abc.Sequence` abstract base class defines a much richer     │ 抽象基底類別 (abstract base class) :class:`collections.abc.Sequence` 定義了一個更加豐富的介面,並不僅止於                                 │
      │ interface that goes beyond just :meth:`__getitem__` and :meth:`__len__`, adding     │ :meth:`__getitem__` 和 :meth:`__len__`,還增加了                                                                                          │
      │ :meth:`count`, :meth:`index`, :meth:`__contains__`, and :meth:`__reversed__`. Types │ :meth:`count`、:meth:`index`、:meth:`__contains__` 和                                                                                     │
      │ that implement this expanded interface can be registered explicitly using           │ :meth:`__reversed__`。實作此擴充介面的型別,可以使用 :func:`~abc.ABCMeta.register` 被明確地註冊。                                         │
      │ :func:`~abc.ABCMeta.register`.                                                      │                                                                                                                                           │
      ├─────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
      │ ``batched('ABCDEFG', n=3) --> ABC DEF G``                                           │ ``batched('ABCDEFG', n=3) --> ABC DEF G``                                                                                                 │
      ├─────────────────────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
      ...
      

sphinx-lint

  • 用途:sphinx 語法檢查
  • 專案連結:https://github.com/sphinx-contrib/sphinx-lint
  • 安裝方式:pip install sphinx-lint
  • 使用方式:
    • sphinx-lint {directory path}(檢查某資料夾底下所有 PO 檔)
      > sphinx-lint library 
      library/ast.po:85: trailing whitespace (trailing-whitespace)
      library/wsgiref.po:97: found an unbalanced inline literal markup. (unbalanced-inline-literals-delimiters)
      
    • sphinx-lint {file path}(檢查單一份 PO 檔)
      > sphinx-lint library/ast.po
      library/ast.po:85: trailing whitespace (trailing-whitespace)