マクロ/Python設計ガイド

From The Document Foundation Wiki
Jump to: navigation, search
This page is a translated version of the page Macros/Python Design Guide and the translation is 35% complete.

Other languages:
English • ‎español • ‎français • ‎日本語 • ‎русский

はじめに

このページはLibreOfficeアプリケーションの設計、またはLibreOfficeとのコラボレーションを希望するPythonマクロ開発者を対象にしています。初心者から経験豊富なPythonプログラマーにとって、このページの開発ガイドラインは役に立つでしょう。LibreOfficeでのPythonについて理解するには、Pythonプログラマーズガイドを参考にしてください。

LibreOfficeプロジェクトに参加をしたい方は、このTDF Wikiを参考にしてください。

このページは、Pythonをある程度理解していることとLibreOfficeについて慣れていることを前提としています。そして、いくつかのPythonのサンプルコードとLibreOffice拡張機能についても簡単に解説しています。

文書

オンライン講座・ガイド

電子文献

統一モデリング言語 (UML)

Numerous UML-driven tools are available. Herewith is an arbitrary list that focuses on free open source interoperable applications mentioning their featured languages.

  • ArgoUML : ArgoUML is running on any Java platforms and is available in ten languages (American English, British English, French, German, Italian, Portuguese, Spanish, Russian, Norwegian Bokmål and Chinese).
  • StarUML : StarUML is proposed for macOS 10.6+, Windows XP+ and Ubuntu (64 or 32-bit). StarUML has a rich feature list and is available in English only.
  • Umbrello : Multi-lingual Umbrello comes with GNU/Linux distributions, Windows (64 or 32-bit) and macOS.

An extensive list of (Unified Modeling Language (UML) tools is available from Wikipedia.

StarOffice Programmer's Tutorial details LibreOffice documents with the help of UML diagrams.

Software Design Patterns are described in Wikipedia, sometimes accompanied with Python examples.

序文

Pythonスクリプトのライブラリ管理

LibreOfficeのBasic言語マクロ開発には、Basicマクロの編集、デバッグなどができる統合開発環境(IDE)が付属しています。Pythonマクロを作成するには、選択したIDEで開発をするための追加の設定が必要となります。その代わり、あなたのPython開発環境や好みに基づいて作られた柔軟な開発環境、設計経験が利用できます。以下に初心者から上級者まで使えるIDE設定ガイドラインを示します。

The handling of libraries and macros equivalent to that of Basic is not available for Python. However managing Python traditional modules and functions is quite possible, while the building of Python packages translates somehow partially to creating LibreOffice extensions or UNO packages. This page uses interchangeably modules for libraries and functions for macros. Python's terminology may be preferred in order to better accommodate Python's cultural usage.

LibreOffice scripts come in three distincts flavors, they can be personal, shared or embedded in documents. Depending on their types and depending on the platform, they end up being stored in varying locations that are detailed in the AOO wiki article Python as a macro language.

To manage Python scripts one can either use Windows libraries either resort to GNU/Linux file manager shortcuts. Thus .py files can be organised using casual drag & drop. A description of folder locations according to your environment to help you setting up libraries or file manager shortcuts can be found in the AOO wiki article Python as a macro language.

Python projects can be arranged per IDE in a subsequent Windows library as illustrated.

APSOメニュー

The must-have Alternative Python Script Organizer extension leverages Python scripts management. You get greater Python scripts integration in LibreOffice by installing it. Once LibreOffice is restarted, check Tools menu for Macros - Organize python scripts sub-menu.

This extension proposes various actions in order to manage Python scripts. The Execute button runs the selected macro. Actions available from Menu button depend on the selected element:

APSOダイアログ
  • Create macro or library whenever you select personal, shared or document containers
  • Edit, Rename, Delete a macro or library
  • Embed a macro into a document from personal or shared containers
  • Substitute a document embedded library with a platform Python module
  • Export a document embedded library as a Python module
  • Debug the selected macro
  • Run Python interactive console also as known as Python interpreter

Other information related to scripts organization and Python packages can be found in Debugging

Script LibreOffice details how to use scripts in LibreOffice application suite.

開発について

Numerous applications make use of Python, either natively or specifically. LibreOffice embeds its own Python interpreter which permits to use the API through Universal Network Objects (UNO). Python language holds its own interpreter that can be used in parallel to that of LibreOffice. Both share a common base and are extensible, thus they provide identical features as well as they offer different ones. Native Python does not include the UNO interface to LibreOffice API. Reversely LibreOffice does not fully behave as native Python and does not extend its functionalities the exact same way as native Python. However, neither LibreOffice nor native Python facilitate application development with Python language for Writer, Calc, Impress, Draw, Base or Math.

LibreOffice exists in 32- or 64-bit versions. The Integrated Development Environment (IDE) that accompany it requires to execute in the same context, both in 32- or 64-bit. Numerous IDEs exist from the easiest like IDLE, although disputable, to more elaborated ones like PyCharm or PyDev for Eclipse. The following list arbitrarily focuses on tools that are free, open source, multi platform and possibly multi lingual. Below Debugging chapter uses Pyzo and PyCharm solely for illustration. A list of IDEs for Python is available from Python.org site. A comparison of Integrated Development Environments (IDE) for numerous languages is available from Wikipedia.

  • Integrated Development and Learning Environment (IDLE) supports 32- and 64-bit. It is available on many platforms. IDLE is the recommended tool to learn Python.
  • Geany operates in 32- or 64-bit and is available in 43 different languages. It runs on GNU/Linux, *BSD, macOS, AIX v5.3, Solaris Express and Windows. Geany is light, fits small projects and supports LibreOffice Python interpreter.
  • Liclipse
  • Debugging with Aptana looks possible cf. this thread
  • PyCharm (free edition) supports Gnu/Linux, macOS and Windows platforms. It is available as 32bits, and apparently accommodates 64bits too. Along with numerous features accompanying Python development, PyCharm holds a Python debugger that's compatible with LibreOffice.
  • Pyzo is available for Linux, OS X and Windows in 10 different languages. Multiple Python interpreters can be configured, be they 32bits or 64bits. Pyzo offers debugging facilities.

Here's a list of features available from these IDEs, LibreOffice Basic IDE does not offer some of them.

ソースコード・エクスプローラー、構文ハイライト

Source exploration and syntax highlighting within IDLE, Geany and PyCharm are facilitating script edition. Geany and PyCharm support code folding. IDLE class browser does not support inner or embedded classes.

コード補完

Code completion assists the edition of Python scripts in IDLE, Geany and PyCharm.

デバッギング

Debugging chapter below goes into this subject in more details.

コーディング規約 (PEP)

PyCharm IDE displays Python Enhancement Proposal (PEP) compliance checks.

PyCharm Coding Conventions Assistant

Python coding conventions and recommendations are available on the rightmost portion of the editor. This is particularly convenient for unexperienced programmers.

テスト駆動開発 (TDD) - 草稿

</Unit testing>

バージョン管理

A source code local history comes with PyCharm as illustrated below:

Source code updates local history

Two copies of apso.py Python module are jointly displayed and respectively labelled v0.8.3 and v0.8.7. Version differences are precisely highlighted.

PyCharm supports other version control solutions (VCS) such as Git, GitHub, Mercurial, Perforce or Subversion.

デバッギング

LibreOffice内部

Xray - Methods of a Writer text object
MRI - Properties, methods, services and interfaces for a Writer document
Object Inspector - 'StarDesktop' properties, methods, supported services and exported interfaces

One can edit and debug Python macros directly within LibreOffice with the help of Alternative Python Script Organizer extension. Managing libraries and modules is possible, as well as selecting your preferred editor without leaving LibreOffice.

Developing out of LibreOffice is equally feasible, thus debugging becomes possible. Remote coding and testing using an Integrated Development Environment (IDE) brings more features detailed hereafter.

Here are three extensions for programmers to benefit from in those two contexts. They can be used regardless of the programming language, be it LibreOffice Basic, Python, JavaScript or BeanShell. All three extend the API's introspection from LibreOffice suite.

便利な拡張機能

It is not always clear at design time which services a UNO object actually supports. However extensions that help the developer to inspect arbitrary UNO-Objects exist, but are not made available from the extensions' catalog, with the exception of MRI. They're built upon LibreOffice API reflection ability.

  • X-Ray inspects LibreOffice API objects. It displays the properties, methods, services, interfaces which are provided by an object variable. X-Ray is available in french, english, german, spanish, czech, polish and russian. X-Ray is written in Basic.
  • MRI (My Reflection and Introspection) is an introspection tool of UNO objects, written in Python. Its features are similar to that of X-Ray. MRI is available in english only. Note: Extensions' catalog MRI copy is outdated.
  • Object Inspector The Object Inspector extension displays the total capabilities of an UNO-object such as all supported services, exported interfaces, and their respective methods and properties in a TreeView window. The Object Inspector is written in Java and is available only in english.
  • APSO The Alternate Python Script Organizer extension nicely integrates objects inspection in its debugger. It uses either X-Ray either MRI.

Pythonマクロの使用方法

Python macros in LibreOffice are stored in numerous locations and come in varied species as explained in Python as a macro language. Scripts can be personal, shared, stored in extensions, integrated in packages or embedded in documents. This diversity imply different uses or constraints.

  • In a document each module is autonomous, no module import.
Refer to Python import online/local help in order to circumvent this
  • Calling a Basic or Python extension from an embedded Python module is possible.
  • Python scripts in <user>\python\pythonpath can be imported by embedded modules or by <user>\python scripts.
  • No Basic support from an IDE: No X-Ray
Basic presence is depending on the bridge type established with the IDE.
  • Python and Java are supported when invoked from an IDE: MRI, Object Inspector can be used.

Here's how to use Mri in a Python macro:

import uno
def mri(target):
    ctx = uno.getComponentContext()
    mri = ctx.ServiceManager.createInstanceWithContext(
        "mytools.Mri",ctx)
    mri.inspect(target)
    
if __name__ == '__main__':
    def Mri_test():
        document = XSCRIPTCONTEXT.getDocument()
        mri(document)
 
    Mri_test()

Stopping a running macro with X-Ray :

import uno
from com.sun.star.uno import RuntimeException as _rtex

def xray(myObject):
  try:
    sm = uno.getComponentContext().ServiceManager
    mspf = sm.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", uno.getComponentContext())
    scriptPro = mspf.createScriptProvider("")
    xScript = scriptPro.getScript("vnd.sun.star.script:XrayTool._Main.Xray?language=Basic&location=application")
    xScript.invoke((myObject,), (), ())
    return
  except:
     raise _rtex("\nBasic library Xray is not installed", uno.getComponentContext())

# ___________  example of use  _______________ 

def demoXray(): 
  """ example : using xray in a Writer document """ 
  
  xTextDoc = XSCRIPTCONTEXT.getDocument() 
  xText = xTextDoc.Text 
  xray(xText)
  xray("Demo is finished") 

g_exportedScripts = demoXray,

Displaying UNO objets in a tree view using Object Inspector :

import uno
def inspectObject( obj, title="Object Inspector" ):
        ctx = uno.getComponentContext()
        oi = ctx.ServiceManager.createInstanceWithContext("org.openoffice.InstanceInspector", ctx)
        if oi is None:  # i.e. InstanceInspector is not installed or deactivated
                return  # exit silently
        oi.inspect( obj, title )
def exObjInspector():
        doc = XSCRIPTCONTEXT.getDocument()
        dsk = XSCRIPTCONTEXT.getDesktop()
        inspectObject(doc)
        inspectObject(dsk, title="StarDesktop")

IDEからLibreOffice Pythonインタープリターを使用する

Developing Python macros within an IDE requires the use of LibreOffice interpreter. Native Python comes with IDLE's interpreter that's not compatible with UNO objects. While selecting a Python IDE programmers benefit from numerous features such as class browser, syntax highlighting, code completion, coding standards enforcement, test driven development, debugging, version control and many more.

The setting of LibreOffice Python interpreter for Geany, PyCharm or Pyzo is illustrated in Python IDE Basics.

In order to execute a script from <user>\python or from <user>\python\pythonpath, a communication must be established between the IDE and LibreOffice. UNO objects are not reachable without such a bridge. Once a communication is in place, the IDE and LibreOffice operate together. The following links document that process:

IDE_utils module borrows from these resources and provides LibreOffice Python macro simple enablement for IDEs.

Pythonスクリプトの使用方法

  • ...calling Python packages...

Geanyから外部で実行

Geany initiates the opening of a window within LibreOffice. Geany EDI invokes info() function, which utilizes ..awt.createMessageBox API method defined in the SDK. The user script is named SysInfo.py.

Geany executes info() function that calls ..awt.createMessageBox() API method  defined in SDK - Script name is <user>\SysInfo.py

PyZo上でのデバッギング

PyUNO Workspace is an object introspection Pyzo IDE plugin for PyUNO and LibreOffice UNO API:

Pyzo runs and stops into <user>\..\IDE_utils.py
Pyzo runs and stops into <user>\..\IDE_utils.py

PyCharmでのデバッギング

PyCharm's debugger displays UNO objects, Python classes or objects:

Watching UNO objects UNO, Python classes or variables

APSO上でのデバッギング

The Alternative Python Script Organizer extension allows step by step execution, as well as stack trace display and UNO objects introspection, with xRay or MRI.

APSO debug window

コンソールへの出力

Python scripts can be prototyped by use of the Python shell and its Read Execute Print Loop (REPL) process. The Alternative Python Script Organizer integrates this facility, which is part of LibreOffice Python.

APSO console

Python対話モード

The Python interactive console, also known as Python interpreter or Python shell, provides programmers with a quick way to execute commands and try out and test code without creating a file. UNO objects introspection as well as LibreOfficeDev Python modules documentation can be obtained from the terminal. LibreOffice online/local details how to run Python interactive console.

Python shell

Windows上のコンソール

By default, there is no default stdout on Windows. What is sent by function “print” will not be visible anywhere. To get a console to be used as stdout, apply the following instructions.

  1. LibreOfficeを閉じる。
  2. Visual Studio Express 2010 C++ (ほかのバージョンでもおそらく大丈夫でしょう)をダウンロードしてインストールする。
  3. Visual Studioのコンソールを開く。
  4. 以下を入力:
editbin.exe /subsystem:console "C:\Program Files\LibreOffice 5\program\soffice.exe"
editbin.exe /subsystem:console "C:\Program Files\LibreOffice 5\program\soffice.bin"

cf. EditBin reference at TechNet

Now, a console will be shown when you open LibreOffice. It will be the stdout for the “print” command. And you can get Python errors while LO is running if you use traceback.

import traceback
[]
try:
    []
    except:
        traceback.print_exc()

But syntax errors still won’t be reported there.

source: [Python] Errors report - Debugging configuration

例外処理 - 草稿

ダイアログの構築 - 草稿

cf. UNODiT UNO Dialog Tools 始めるための参考文献を上げておきます:

ダイアログの設計

イベント駆動マクロ

地域化

ディスパッチャー・コマンド - 草稿

Interface-oriented programming in *Office, Christopher Bourez (2015)

拡張機能の作成 - 草稿

拡張機能

Extensions_development

Extension_Manager

UNO component packaging - Python component testing

Accept events created by dialog controlsn

Building a Service

Building a Calc Add-In

Building and Extension

Miscellaneous - draft

Document module import

cf. Importing Python modules in online help

Python package vs. LibreOffice Extension

(cf. french draft version up to now)

Static Properties or Methods

Q: How about the behaviour of Python global attribute?

PyCharmでGitを使う - 草稿