pytho****@googl*****
pytho****@googl*****
2011年 6月 27日 (月) 08:51:30 JST
2 new revisions: Revision: 101ddae9504b Author: Akihiro Uchida <uchid****@ike-d*****> Date: Sun Jun 26 03:49:57 2011 Log: translate howto/curses.rst http://code.google.com/p/python-doc-ja/source/detail?r=101ddae9504b Revision: 0fc5a14ba4c6 Author: Akihiro Uchida <uchid****@ike-d*****> Date: Sun Jun 26 16:50:43 2011 Log: 細かい修正 howto/{curses,regex,webservers}.rst http://code.google.com/p/python-doc-ja/source/detail?r=0fc5a14ba4c6 ============================================================================== Revision: 101ddae9504b Author: Akihiro Uchida <uchid****@ike-d*****> Date: Sun Jun 26 03:49:57 2011 Log: translate howto/curses.rst http://code.google.com/p/python-doc-ja/source/detail?r=101ddae9504b Modified: /howto/curses.rst ======================================= --- /howto/curses.rst Fri May 20 04:17:35 2011 +++ /howto/curses.rst Sun Jun 26 03:49:57 2011 @@ -1,383 +1,770 @@ +.. + ***************************************** + Curses Programming with Python (英語) + ***************************************** + .. _curses-howto: -***************************************** - Curses Programming with Python (英語) -***************************************** +*********************************** + Python で Curses プログラミング +*********************************** :Author: A.M. Kuchling, Eric S. Raymond :Release: 2.03 - -.. topic:: Abstract - - This document describes how to write text-mode programs with Python 2.x, using - the :mod:`curses` extension module to control the display. - - -What is curses? -=============== - -The curses library supplies a terminal-independent screen-painting and -keyboard-handling facility for text-based terminals; such terminals include -VT100s, the Linux console, and the simulated terminal provided by X11 programs -such as xterm and rxvt. Display terminals support various control codes to -perform common operations such as moving the cursor, scrolling the screen, and -erasing areas. Different terminals use widely differing codes, and often have -their own minor quirks. - -In a world of X displays, one might ask "why bother"? It's true that -character-cell display terminals are an obsolete technology, but there are -niches in which being able to do fancy things with them are still valuable. One -is on small-footprint or embedded Unixes that don't carry an X server. Another -is for tools like OS installers and kernel configurators that may have to run -before X is available. - -The curses library hides all the details of different terminals, and provides -the programmer with an abstraction of a display, containing multiple -non-overlapping windows. The contents of a window can be changed in various -ways-- adding text, erasing it, changing its appearance--and the curses library -will automagically figure out what control codes need to be sent to the terminal -to produce the right output. - -The curses library was originally written for BSD Unix; the later System V -versions of Unix from AT&T added many enhancements and new functions. BSD curses -is no longer maintained, having been replaced by ncurses, which is an -open-source implementation of the AT&T interface. If you're using an -open-source Unix such as Linux or FreeBSD, your system almost certainly uses -ncurses. Since most current commercial Unix versions are based on System V -code, all the functions described here will probably be available. The older -versions of curses carried by some proprietary Unixes may not support -everything, though. - -No one has made a Windows port of the curses module. On a Windows platform, try -the Console module written by Fredrik Lundh. The Console module provides -cursor-addressable text output, plus full support for mouse and keyboard input, -and is available from http://effbot.org/zone/console-index.htm. - - -The Python curses module ------------------------- - -Thy Python module is a fairly simple wrapper over the C functions provided by -curses; if you're already familiar with curses programming in C, it's really -easy to transfer that knowledge to Python. The biggest difference is that the -Python interface makes things simpler, by merging different C functions such as -:func:`addstr`, :func:`mvaddstr`, :func:`mvwaddstr`, into a single -:meth:`addstr` method. You'll see this covered in more detail later. - -This HOWTO is simply an introduction to writing text-mode programs with curses -and Python. It doesn't attempt to be a complete guide to the curses API; for -that, see the Python library guide's section on ncurses, and the C manual pages -for ncurses. It will, however, give you the basic ideas. - - -Starting and ending a curses application -======================================== - -Before doing anything, curses must be initialized. This is done by calling the -:func:`initscr` function, which will determine the terminal type, send any -required setup codes to the terminal, and create various internal data -structures. If successful, :func:`initscr` returns a window object representing -the entire screen; this is usually called ``stdscr``, after the name of the -corresponding C variable. :: +.. + .. topic:: Abstract + + This document describes how to write text-mode programs with Python 2.x, using + the :mod:`curses` extension module to control the display. + +.. topic:: 概要 + + このドキュメントでは Python 2.x でテキストモードプログラムを + 表示をコントロールする :mod:`curses` 拡張モジュールを利用して + 書く方法について記述します。 + +.. + What is curses? + =============== + +curses ってなに? +================ + +.. + The curses library supplies a terminal-independent screen-painting and + keyboard-handling facility for text-based terminals; such terminals include + VT100s, the Linux console, and the simulated terminal provided by X11 programs + such as xterm and rxvt. Display terminals support various control codes to + perform common operations such as moving the cursor, scrolling the screen, and + erasing areas. Different terminals use widely differing codes, and often have + their own minor quirks. + +curses ライブラリは端末に依存せずに、スクリーンに描画やキーボード処理する機 能を +テキストベースの端末、VT100s や Linux コンソールや xterm や rxvt などの X11 プログラムから提供される +端末エミュレータなど、に供給します。 +表示端末は様々な制御コードをサポートしていて、 +カーソル移動、画面のスクロール、領域の消去などの一般的な操作を実行できま す。 +端末によって大きく異なるコードを使うことがあるので、しばしば独自の癖があり ます。 + +.. + In a world of X displays, one might ask "why bother"? It's true that + character-cell display terminals are an obsolete technology, but there are + niches in which being able to do fancy things with them are still valuable. One + is on small-footprint or embedded Unixes that don't carry an X server. Another + is for tools like OS installers and kernel configurators that may have to run + before X is available. + +X ディスプレイの世界では、「どうしてこんな面倒なことを?」と疑問に思うかもし れません。 +確かに文字表示端末は時代遅れな技術ではありますが、ニッチな領域が存在してい て、 +意匠を凝らすことができるため、未だ価値あるものとなっています。 +その領域は例えば X サーバを持たない、小さな机上用コンピュータや組み込みの Unix です。 +他のものとしては OS インストーラーやカーネル設定ツールなどで、 +これらは X が利用できるようになる前に動作する必要があります。 + +.. + The curses library hides all the details of different terminals, and provides + the programmer with an abstraction of a display, containing multiple + non-overlapping windows. The contents of a window can be changed in various + ways-- adding text, erasing it, changing its appearance--and the curses library + will automagically figure out what control codes need to be sent to the terminal + to produce the right output. + +curses ライブラリは異なる端末の詳細を全て隠蔽し、 +プログラマに対して、重なり合わない複数のウィンドウを持つ抽象化されたディス プレイを提供します。 +ウィンドウの内容は様々な方法で変更されます +-- テキストの追加、消去、外観の変更 -- そして curses ライブラリは +送るべき制御コードを自動的に理解し、正しい出力を生成してくれます。 + +.. + The curses library was originally written for BSD Unix; the later System V + versions of Unix from AT&T added many enhancements and new functions. BSD curses + is no longer maintained, having been replaced by ncurses, which is an + open-source implementation of the AT&T interface. If you're using an + open-source Unix such as Linux or FreeBSD, your system almost certainly uses + ncurses. Since most current commercial Unix versions are based on System V + code, all the functions described here will probably be available. The older + versions of curses carried by some proprietary Unixes may not support + everything, though. + +curses ライブラリは元々 BSD UNIX 向けに書かれました; +後の AT&T から出た Unix System V バージョンで多くの機能と新機能が追加されま した。 +BSD curses はいまやメンテナンスされておらず、 +これは AT&T インターフェースのオープンソース実装である ncurses にとって置き 換えられました。 +Linux や FreeBSD のようなオープンソース Unix を利用している場合は、 +おそらくシステムは ncurses を利用しています。 +現在のほとんどの商用 Unix は System V のコードを基にしているため、 +ここれ述べる全ての関数が利用できるはずです。 +しかし、古いバージョンの curses を持ついくつかのプロプライエタリ Unix は +全てには対応していないでしょう。 + +.. + No one has made a Windows port of the curses module. On a Windows platform, try + the Console module written by Fredrik Lundh. The Console module provides + cursor-addressable text output, plus full support for mouse and keyboard input, + and is available from http://effbot.org/zone/console-index.htm. + +curses モジュールの Windows 移植は作られていません。 +Windows プラットフォーム上では、Fredrik Lundh が書いた Console モジュールを 試してみてください。 +Console モジュールはカーソル位置を指定してテキスト出力することができ、 +マウス、キーボード入力の両方を完全にサポートしていて、 +http://effbot.org/zone/console-index.htm から入手できます。 + + +.. + The Python curses module + ------------------------ + +Python の curses module +----------------------- + +.. + Thy Python module is a fairly simple wrapper over the C functions provided by + curses; if you're already familiar with curses programming in C, it's really + easy to transfer that knowledge to Python. The biggest difference is that the + Python interface makes things simpler, by merging different C functions such as + :func:`addstr`, :func:`mvaddstr`, :func:`mvwaddstr`, into a single + :meth:`addstr` method. You'll see this covered in more detail later. + +Python モジュールは curses が提供する C 関数に対するまったく単純なラッパー です; +既に C での curses プログラミングに慣れ親しんでいるなら、 +その知識を Python に持ち込むのは実に簡単です。 +最大の違いは Python インターフェースが +異なる関数 :func:`addstr`, :func:`mvaddstr`, :func:`mvwaddstr` を一つのメソ ッド +:meth:`addstr` method に統合して単純化していることです。 +このことは後でより詳しく扱います。 + +.. + This HOWTO is simply an introduction to writing text-mode programs with curses + and Python. It doesn't attempt to be a complete guide to the curses API; for + that, see the Python library guide's section on ncurses, and the C manual pages + for ncurses. It will, however, give you the basic ideas. + +この HOWTO は curses と Python を使って +テキストプログラムを書くための簡単な入門記事です。 +curses API に対する完全な解説をすることは意図していません; +その目的のためには Python ライブラリガイドの ncurses 節と +ncurses の C 言語マニュアルページを参照してください。 +しかし、この文章が基本的な考えを提供してくれるでしょう。 + +.. + Starting and ending a curses application + ======================================== + +curses アプリケーションの起動と終了 +=================================== + +.. + Before doing anything, curses must be initialized. This is done by calling the + :func:`initscr` function, which will determine the terminal type, send any + required setup codes to the terminal, and create various internal data + structures. If successful, :func:`initscr` returns a window object representing + the entire screen; this is usually called ``stdscr``, after the name of the + corresponding C variable. :: + +何をするにもまず、curses を初期化する必要があります。 +初期化は :func:`initscr` 関数を呼びだすことでできます、 +この関数は端末のタイプを決定し、必要とされるセットアップコードを端末に送 り、 +様々な内部データ構造を作成します。 +成功すると、 :func:`initscr` は画面全体を表わすウィンドウオブジェクトを返し ます; +これは、 ``stdscr`` と呼ばれます、この名前は C での対応する変数にちなんでい ます。:: import curses stdscr = curses.initscr() -Usually curses applications turn off automatic echoing of keys to the screen, in -order to be able to read keys and only display them under certain circumstances. -This requires calling the :func:`noecho` function. :: +.. + Usually curses applications turn off automatic echoing of keys to the screen, in + order to be able to read keys and only display them under certain circumstances. + This requires calling the :func:`noecho` function. :: + +通常 curses アプリケーションは画面へのキーエコーを自動的に止めます、 +これはキーを読みとり特定状況下でのみで表示するためです。 +これには :func:`noecho` 関数を呼び出す必要があります。:: curses.noecho() -Applications will also commonly need to react to keys instantly, without -requiring the Enter key to be pressed; this is called cbreak mode, as opposed to -the usual buffered input mode. :: +.. + Applications will also commonly need to react to keys instantly, without + requiring the Enter key to be pressed; this is called cbreak mode, as opposed to + the usual buffered input mode. :: + +通常アプリケーションはまた、Enter キーを押すことなく、キーに対してすぐに反 応する必要があります; +これは cbreak モードと呼ばれ、通常の入力がバッファされるモードと逆に動作し ます。:: curses.cbreak() -Terminals usually return special keys, such as the cursor keys or navigation -keys such as Page Up and Home, as a multibyte escape sequence. While you could -write your application to expect such sequences and process them accordingly, -curses can do it for you, returning a special value such as -:const:`curses.KEY_LEFT`. To get curses to do the job, you'll have to enable -keypad mode. :: +.. + Terminals usually return special keys, such as the cursor keys or navigation + keys such as Page Up and Home, as a multibyte escape sequence. While you could + write your application to expect such sequences and process them accordingly, + curses can do it for you, returning a special value such as + :const:`curses.KEY_LEFT`. To get curses to do the job, you'll have to enable + keypad mode. :: + +端末は通常、カーソルキーや Page Up や Home といった操作キーなどの特別なキー を +マルチバイトエスケープシーケンスとして返します。 +それらのシーケンスを想定して対応する処理を行うアプリケーションを書けるよう に、 +curses はそれを :const:`curses.KEY_LEFT` のような特別な値を返して行ってくれ ます。 +curses にその仕事をさせるには、キーパッドモードを有効にする必要がありま す。:: stdscr.keypad(1) -Terminating a curses application is much easier than starting one. You'll need -to call :: +.. + Terminating a curses application is much easier than starting one. You'll need + to call :: + +curses アプリケーションを終了させるのは起動よりも簡単です。 +curses に親和的な端末設定を元に戻すために以下を呼び出す必要があります:: curses.nocbreak(); stdscr.keypad(0); curses.echo() -to reverse the curses-friendly terminal settings. Then call the :func:`endwin` -function to restore the terminal to its original operating mode. :: - - curses.endwin() - -A common problem when debugging a curses application is to get your terminal -messed up when the application dies without restoring the terminal to its -previous state. In Python this commonly happens when your code is buggy and -raises an uncaught exception. Keys are no longer be echoed to the screen when -you type them, for example, which makes using the shell difficult. - -In Python you can avoid these complications and make debugging much easier by -importing the module :mod:`curses.wrapper`. It supplies a :func:`wrapper` -function that takes a callable. It does the initializations described above, -and also initializes colors if color support is present. It then runs your -provided callable and finally deinitializes appropriately. The callable is -called inside a try-catch clause which catches exceptions, performs curses -deinitialization, and then passes the exception upwards. Thus, your terminal -won't be left in a funny state on exception. - - -Windows and Pads -================ - -Windows are the basic abstraction in curses. A window object represents a -rectangular area of the screen, and supports various methods to display text, -erase it, allow the user to input strings, and so forth. - -The ``stdscr`` object returned by the :func:`initscr` function is a window -object that covers the entire screen. Many programs may need only this single -window, but you might wish to divide the screen into smaller windows, in order -to redraw or clear them separately. The :func:`newwin` function creates a new -window of a given size, returning the new window object. :: +.. + to reverse the curses-friendly terminal settings. Then call the :func:`endwin` + function to restore the terminal to its original operating mode. :: + +そして、 :func:`endwin` 関数を呼び出し、端末設定を通常の操作モードに復旧し ます。:: + + curses.endwin() + +.. + A common problem when debugging a curses application is to get your terminal + messed up when the application dies without restoring the terminal to its + previous state. In Python this commonly happens when your code is buggy and + raises an uncaught exception. Keys are no longer be echoed to the screen when + you type them, for example, which makes using the shell difficult. + +curses アプリケーションをデバッグするときの一般的な問題は、 +アプリケーションが端末を以前の状態に復旧することなく異常終了したときに +端末がめちゃめちゃになることです。 +Python ではこの問題はコードにバグがあって、捕捉できない例外が発生したときに よく起きます。 +タイプしたキーはもはやエコーされません、例えば、シェルを使うのが難しくなり ます。 + +.. + In Python you can avoid these complications and make debugging much easier by + importing the module :mod:`curses.wrapper`. It supplies a :func:`wrapper` + function that takes a callable. It does the initializations described above, + and also initializes colors if color support is present. It then runs your + provided callable and finally deinitializes appropriately. The callable is + called inside a try-catch clause which catches exceptions, performs curses + deinitialization, and then passes the exception upwards. Thus, your terminal + won't be left in a funny state on exception. + +Python では :mod:`curses.wrapper` をインポートすることで +この複雑な問題を避け、デバッグを容易にすることができます。 +それは 呼び出し可能オブジェクトを引数にとることができる +:func:`wrapper` 関数を提供します。 +これは上に述べた初期化をし、カラーサポートがあればカラーの初期化をします。 +そして、与えられた呼び出し可能オブジェクトを実行し、 +最終的に適切に初期化の逆操作 (deinitialization) を行います。 +呼び出し可能オブジェクトは例外を捕捉する try-catch 節内部で呼び出され、 +curses の初期化操作の逆操作を実行します、 +そして例外が上に渡されます。 +そうして端末は例外が発生しても端末はおかしな状態にならずにすみます。 + +.. + Windows and Pads + ================ + +ウィンドウとパッド +================== + +.. + Windows are the basic abstraction in curses. A window object represents a + rectangular area of the screen, and supports various methods to display text, + erase it, allow the user to input strings, and so forth. + +ウィンドウは curses での基本的な抽象概念です。 +ウィンドウオブジェクトは画面の長方形の領域を表わし、 +テキストの表示、消去、ユーザに文字列入力を許可などの様々なメソッドをサポー トしています。 + +.. + The ``stdscr`` object returned by the :func:`initscr` function is a window + object that covers the entire screen. Many programs may need only this single + window, but you might wish to divide the screen into smaller windows, in order + to redraw or clear them separately. The :func:`newwin` function creates a new + window of a given size, returning the new window object. :: + +:func:`initscr` 関数によって返される ``stdscr`` オブジェクトはウィンドウオ ブジェクトで +画面全体を扱います。 +多くのプログラムはこのウィンドウだけを必要としますが、 +画面を小さなウィンドウに分けて +それらを別々に再描画したり消去したいと思うかもしれません。 +:func:`newwin` 関数は新しいウィンドウを与えられたサイズで作成し、 +新しいウィンドウオブジェクトを返します。:: begin_x = 20 ; begin_y = 7 height = 5 ; width = 40 win = curses.newwin(height, width, begin_y, begin_x) -A word about the coordinate system used in curses: coordinates are always passed -in the order *y,x*, and the top-left corner of a window is coordinate (0,0). -This breaks a common convention for handling coordinates, where the *x* -coordinate usually comes first. This is an unfortunate difference from most -other computer applications, but it's been part of curses since it was first -written, and it's too late to change things now. - -When you call a method to display or erase text, the effect doesn't immediately -show up on the display. This is because curses was originally written with slow -300-baud terminal connections in mind; with these terminals, minimizing the time -required to redraw the screen is very important. This lets curses accumulate -changes to the screen, and display them in the most efficient manner. For -example, if your program displays some characters in a window, and then clears -the window, there's no need to send the original characters because they'd never -be visible. - -Accordingly, curses requires that you explicitly tell it to redraw windows, -using the :func:`refresh` method of window objects. In practice, this doesn't -really complicate programming with curses much. Most programs go into a flurry -of activity, and then pause waiting for a keypress or some other action on the -part of the user. All you have to do is to be sure that the screen has been -redrawn before pausing to wait for user input, by simply calling -``stdscr.refresh()`` or the :func:`refresh` method of some other relevant -window. - -A pad is a special case of a window; it can be larger than the actual display -screen, and only a portion of it displayed at a time. Creating a pad simply -requires the pad's height and width, while refreshing a pad requires giving the -coordinates of the on-screen area where a subsection of the pad will be -displayed. :: - - pad = curses.newpad(100, 100) - # These loops fill the pad with letters; this is - # explained in the next section - for y in range(0, 100): - for x in range(0, 100): - try: pad.addch(y,x, ord('a') + (x*x+y*y) % 26 ) - except curses.error: pass - - # Displays a section of the pad in the middle of the screen - pad.refresh( 0,0, 5,5, 20,75) - -The :func:`refresh` call displays a section of the pad in the rectangle -extending from coordinate (5,5) to coordinate (20,75) on the screen; the upper -left corner of the displayed section is coordinate (0,0) on the pad. Beyond -that difference, pads are exactly like ordinary windows and support the same -methods. - -If you have multiple windows and pads on screen there is a more efficient way to -go, which will prevent annoying screen flicker at refresh time. Use the -:meth:`noutrefresh` method of each window to update the data structure -representing the desired state of the screen; then change the physical screen to -match the desired state in one go with the function :func:`doupdate`. The -normal :meth:`refresh` method calls :func:`doupdate` as its last act. - - -Displaying Text -=============== - -From a C programmer's point of view, curses may sometimes look like a twisty -maze of functions, all subtly different. For example, :func:`addstr` displays a -string at the current cursor location in the ``stdscr`` window, while -:func:`mvaddstr` moves to a given y,x coordinate first before displaying the -string. :func:`waddstr` is just like :func:`addstr`, but allows specifying a -window to use, instead of using ``stdscr`` by default. :func:`mvwaddstr` follows -similarly. - -Fortunately the Python interface hides all these details; ``stdscr`` is a window -object like any other, and methods like :func:`addstr` accept multiple argument -forms. Usually there are four different forms. - -+---------------------------------+-----------------------------------------------+ -| Form | Description | -+=================================+===============================================+ -| *str* or *ch* | Display the string *str* or character *ch* at | -| | the current position | -+---------------------------------+-----------------------------------------------+ -| *str* or *ch*, *attr* | Display the string *str* or character *ch*, | -| | using attribute *attr* at the current | -| | position | -+---------------------------------+-----------------------------------------------+ -| *y*, *x*, *str* or *ch* | Move to position *y,x* within the window, and | -| | display *str* or *ch* | -+---------------------------------+-----------------------------------------------+ -| *y*, *x*, *str* or *ch*, *attr* | Move to position *y,x* within the window, and | -| | display *str* or *ch*, using attribute *attr* | -+---------------------------------+-----------------------------------------------+ - -Attributes allow displaying text in highlighted forms, such as in boldface, -underline, reverse code, or in color. They'll be explained in more detail in -the next subsection. - -The :func:`addstr` function takes a Python string as the value to be displayed, -while the :func:`addch` functions take a character, which can be either a Python -string of length 1 or an integer. If it's a string, you're limited to -displaying characters between 0 and 255. SVr4 curses provides constants for -extension characters; these constants are integers greater than 255. For -example, :const:`ACS_PLMINUS` is a +/- symbol, and :const:`ACS_ULCORNER` is the -upper left corner of a box (handy for drawing borders). - -Windows remember where the cursor was left after the last operation, so if you -leave out the *y,x* coordinates, the string or character will be displayed -wherever the last operation left off. You can also move the cursor with the -``move(y,x)`` method. Because some terminals always display a flashing cursor, -you may want to ensure that the cursor is positioned in some location where it -won't be distracting; it can be confusing to have the cursor blinking at some -apparently random location. - -If your application doesn't need a blinking cursor at all, you can call -``curs_set(0)`` to make it invisible. Equivalently, and for compatibility with -older curses versions, there's a ``leaveok(bool)`` function. When *bool* is -true, the curses library will attempt to suppress the flashing cursor, and you -won't need to worry about leaving it in odd locations. - - -Attributes and Color --------------------- - -Characters can be displayed in different ways. Status lines in a text-based -application are commonly shown in reverse video; a text viewer may need to -highlight certain words. curses supports this by allowing you to specify an -attribute for each cell on the screen. - -An attribute is a integer, each bit representing a different attribute. You can -try to display text with multiple attribute bits set, but curses doesn't -guarantee that all the possible combinations are available, or that they're all -visually distinct. That depends on the ability of the terminal being used, so -it's safest to stick to the most commonly available attributes, listed here. +.. + A word about the coordinate system used in curses: coordinates are always passed + in the order *y,x*, and the top-left corner of a window is coordinate (0,0). + This breaks a common convention for handling coordinates, where the *x* + coordinate usually comes first. This is an unfortunate difference from most + other computer applications, but it's been part of curses since it was first + written, and it's too late to change things now. + +curses で利用されている座標形について、言っておくことがあります: +座標は常に *y,x* の順で渡し、ウィンドウの左上の座標を (0,0) とします。 +これは *x* 座標が最初にくる一般的な慣習には反しています。 +これは多くの計算機のアプリケーションにとって不幸な違いですが、 +curses が最初に書かれて以来そうだったので、 +今から変えるには遅すぎます。 + +.. + When you call a method to display or erase text, the effect doesn't immediately + show up on the display. This is because curses was originally written with slow + 300-baud terminal connections in mind; with these terminals, minimizing the time + required to redraw the screen is very important. This lets curses accumulate + changes to the screen, and display them in the most efficient manner. For + example, if your program displays some characters in a window, and then clears + the window, there's no need to send the original characters because they'd never + be visible. + +メソッドを呼びだして、テキストを表示、消去するとき、 +その効果はディスプレイにすぐには現われません。 +それは curses が元々 300 ボーの端末接続を念頭にいれていたためですl +それらの端末では画面の再描画時間を減らすことがとても重要です。 +このため curses は画面への変更を積み上げ、最も効率良い方法でそれらを表示し ます。 +例えば、プログラムがウィンドウにいくつかの文字を表示し、 +それからウィンドウを消去する場合、元々の文字を送信する必要はありません、 +それらはもう見られることはないのです。 + +.. + Accordingly, curses requires that you explicitly tell it to redraw windows, + using the :func:`refresh` method of window objects. In practice, this doesn't + really complicate programming with curses much. Most programs go into a flurry + of activity, and then pause waiting for a keypress or some other action on the + part of the user. All you have to do is to be sure that the screen has been + redrawn before pausing to wait for user input, by simply calling + ``stdscr.refresh()`` or the :func:`refresh` method of some other relevant + window. + +従って curses に明示的にウィンドウの再描画を明示的に伝えてやる必要がありま す、 +それはウィンドウオブジェクトのメソッド :func:`refresh` を使うことでできま す。 +実際には、これは curses を使ったプログラムをそれほど複雑にするものではあり ません。 +ほとんどのプログラムはせわしなく動いた後キーを押すなどのユーザからの動作を 待ちます。 +あなたがするべきことはユーザからの入力を待つ前に単に ``stdscr.refresh()`` や +関連するウィンドウの :func:`refresh` メソッドを呼び出して +画面を再描画するだけです。 + +.. + A pad is a special case of a window; it can be larger than the actual display + screen, and only a portion of it displayed at a time. Creating a pad simply + requires the pad's height and width, while refreshing a pad requires giving the + coordinates of the on-screen area where a subsection of the pad will be + displayed. :: + +パッドはウィンドウの特別な場合を指します; それは実際に画面に表示されるもの より大きくなることがあり、 +画面にはその一部だけが表示されます。 +パッドを作るのに必要なものはパッドの高さと幅だけです、 +一方でパッドの再描画には、表示される部分を指す画面上の領域の座標を指定する 必要があります。:: + + pad = curses.newpad(100, 100) + # These loops fill the pad with letters; this is + # explained in the next section + for y in range(0, 100): + for x in range(0, 100): + try: pad.addch(y,x, ord('a') + (x*x+y*y) % 26 ) + except curses.error: pass + + # Displays a section of the pad in the middle of the screen + pad.refresh( 0,0, 5,5, 20,75) + +.. + The :func:`refresh` call displays a section of the pad in the rectangle + extending from coordinate (5,5) to coordinate (20,75) on the screen; the upper + left corner of the displayed section is coordinate (0,0) on the pad. Beyond + that difference, pads are exactly like ordinary windows and support the same + methods. + +:func:`refresh` はパッドの一部、画面上の座標 (5,5) から (20,75) に及ぶ長方 形領域を表示します; +表示される一部の左上の角は パッドの座標 (0,0) です。 +この違いを除けば、パッドは通常のウィンドウと全く同じで、 +同じメソッドを持ちます。 + +.. + If you have multiple windows and pads on screen there is a more efficient way to + go, which will prevent annoying screen flicker at refresh time. Use the + :meth:`noutrefresh` method of each window to update the data structure + representing the desired state of the screen; then change the physical screen to + match the desired state in one go with the function :func:`doupdate`. The + normal :meth:`refresh` method calls :func:`doupdate` as its last act. + +複数のウィンドウとパッドが画面上にある場合、より効率的な方法があり、 +再描画時に画面をちらつかせるのを防いでくれます。 +Use the +各ウィンドウの +:meth:`noutrefresh` メソッドを利用して、 +画面の望ましい状態を表わすデータ構造をアップデートし、 +:func:`doupdate` 関数で望む状態に合った物理的な画面に変更します。 +通常の :meth:`refresh` メソッドは自身の動作時の最後に :func:`doupdate` を呼 び出します。 + +.. + Displaying Text + =============== + +テキストの表示 +============== + +.. + From a C programmer's point of view, curses may sometimes look like a twisty + maze of functions, all subtly different. For example, :func:`addstr` displays a + string at the current cursor location in the ``stdscr`` window, while + :func:`mvaddstr` moves to a given y,x coordinate first before displaying the + string. :func:`waddstr` is just like :func:`addstr`, but allows specifying a + window to use, instead of using ``stdscr`` by default. :func:`mvwaddstr` follows + similarly. + +C 言語のプログラマの視点からすると、curses はしばしば、 +混乱を招きやすい、微妙に似た関数を持っています。 +例えば :func:`addstr` は ``stdscr`` ウィンドウの現在のカーソル位置の文字列 を表示し、 +一方で :func:`mvaddstr` は与えられた y,x 座標にまず移動して、文字列を表示し ます。 +:func:`waddstr` はほとんど :func:`addstr` に似ていますが、 +デフォルトの ``stdscr`` を使う代わりに使うウィンドウを指定できます。 +:func:`mvwaddstr` も同様です。 + +.. + Fortunately the Python interface hides all these details; ``stdscr`` is a window + object like any other, and methods like :func:`addstr` accept multiple argument + forms. Usually there are four different forms. + +幸運にも、Python インターフェースはこれらの詳細を全て隠蔽してくれます; +``stdscr`` は他のものと同様のウィンドウオブジェクトであり、 :func:`addstr` のようなメソッドは +複数の引数形式を許容してくれます。 +通常それらは4つの形式です。 + +.. + +---------------------------------+-----------------------------------------------+ + | Form | Description | + +=================================+===============================================+ + | *str* or *ch* | Display the string *str* or character *ch* at | + | | the current position | + +---------------------------------+-----------------------------------------------+ + | *str* or *ch*, *attr* | Display the string *str* or character *ch*, | + | | using attribute *attr* at the current | + | | position | + +---------------------------------+-----------------------------------------------+ + | *y*, *x*, *str* or *ch* | Move to position *y,x* within the window, and | + | | display *str* or *ch* | + +---------------------------------+-----------------------------------------------+ + | *y*, *x*, *str* or *ch*, *attr* | Move to position *y,x* within the window, and | + | | display *str* or *ch*, using attribute *attr* | + +---------------------------------+-----------------------------------------------+ + ++-------------------------------------+--------------------------------------------+ +| 形式 | 説 明 | ++=====================================+============================================+ +| *str* または *ch* | 文字列 *str* または 文字 *ch* を現 在位置に | +| | 表示しま す | ++-------------------------------------+--------------------------------------------+ +| *str* または *ch*, *attr* | 文字列 *str* または 文字 *ch* を | +| | 属性 *attr* を利用して現在位置に表 示します | ++-------------------------------------+--------------------------------------------+ +| *y*, *x*, *str* または *ch* | ウィンドウ内の位置 *y,x* に移動 し | +| | *str* または *ch* を表示しま す | ++-------------------------------------+--------------------------------------------+ +| *y*, *x*, *str* または *ch*, *attr* | ウィンドウ内の位置 *y,x* に移動 し | +| | 属性 *attr* を利用し て | +| | *str* または *ch* を表示しま す | ++-------------------------------------+--------------------------------------------+ + +.. + Attributes allow displaying text in highlighted forms, such as in boldface, + underline, reverse code, or in color. They'll be explained in more detail in + the next subsection. + +属性によって表示するテキストをハイライトすることができます、 +ボールド体、アンダーライン、反転、カラーなど。 +より詳しくは次の小節で説明します。 + +.. + The :func:`addstr` function takes a Python string as the value to be displayed, + while the :func:`addch` functions take a character, which can be either a Python + string of length 1 or an integer. If it's a string, you're limited to + displaying characters between 0 and 255. SVr4 curses provides constants for + extension characters; these constants are integers greater than 255. For + example, :const:`ACS_PLMINUS` is a +/- symbol, and :const:`ACS_ULCORNER` is the + upper left corner of a box (handy for drawing borders). + +:func:`addstr` 関数は Python 文字列を引数にとり、表示します。 +一方 :func:`addch` 関数は文字を引数にとります、引数は長さ1の文字列か整数の どちらでもかまいません。 +文字列の場合には、表示する文字は 0 から 255 の間に制限されます。 +SVr4 curses は文字拡張のための定数を提供しています; それらの定数は 255 より 大きい整数です。 +例えば :const:`ACS_PLMINUS` は +/- 記号で +:const:`ACS_ULCORNER` はボックスの左上角です (境界を描くのに便利です)。 + +.. + Windows remember where the cursor was left after the last operation, so if you + leave out the *y,x* coordinates, the string or character will be displayed + wherever the last operation left off. You can also move the cursor with the + ``move(y,x)`` method. Because some terminals always display a flashing cursor, + you may want to ensure that the cursor is positioned in some location where it + won't be distracting; it can be confusing to have the cursor blinking at some + apparently random location. + +ウィンドウは最後の操作の後のカーソル位置を覚えているため、 +*y,x* 座標をうっかり忘れてしまっても、文字列や文字は最後の操作位置に表示さ れます。 +``move(y,x)`` メソッドでカーソルを移動させることもできます。 +常に点滅するカーソルを表示する端末もあるため、 +カーソルが特定の位置にいることを保証して +注意が反れないようにしたいと思うかもしれません; +ランダムに見える位置でカーソルがちらつくとと面を食らってしまいます。 + +.. + If your application doesn't need a blinking cursor at all, you can call + ``curs_set(0)`` to make it invisible. Equivalently, and for compatibility with + older curses versions, there's a ``leaveok(bool)`` function. When *bool* is + true, the curses library will attempt to suppress the flashing cursor, and you + won't need to worry about leaving it in odd locations. + +アプリケーションがちらつくカーソルを全く必要としない場合、 +``curs_set(0)`` を呼び出してカーソル見えなくすることができます。 +同じことを、古い curses バージョンに対する互換性を保ちつつ行うために +``leaveok(bool)`` 関数があります。 +*bool* が true の場合、curses ライブラリは点滅するカーソルを外に出さなくす るので、 +変な場所に現われるのを心配する必要は無くなります。 + +.. + Attributes and Color + -------------------- + +属性とカラー +------------ + +.. + Characters can be displayed in different ways. Status lines in a text-based + application are commonly shown in reverse video; a text viewer may need to + highlight certain words. curses supports this by allowing you to specify an + attribute for each cell on the screen. + +文字は様々な方法で表示することができます。 +テキストベースアプリケーションでのステータスラインは通常反転して表示されま す; +テキストビュアーは特定の単語をハイライトする必要があるかもしれせん。 +curses は属性を画面上の各セルに対して指定することで、それをサポートします。 + +.. + An attribute is a integer, each bit representing a different attribute. You can + try to display text with multiple attribute bits set, but curses doesn't + guarantee that all the possible combinations are available, or that they're all + visually distinct. That depends on the ability of the terminal being used, so + it's safest to stick to the most commonly available attributes, listed here. + +属性は整数値で、それぞれのビットが異なる属性を表わします。 +複数の属性ビットをセットしてテキストの表示を試みることができますが、 +curses は全ての組み合わせが利用可能であるかや +視覚的に区別できるかどうかは保証してくれません、 +それらは利用している端末の能力に依存しているため、 +最も安全なのは、最も一般的に利用可能な属性を設定する方法です、 +ここに列挙します + +.. + +----------------------+--------------------------------------+ + | Attribute | Description | + +======================+======================================+ + | :const:`A_BLINK` | Blinking text | + +----------------------+--------------------------------------+ + | :const:`A_BOLD` | Extra bright or bold text | + +----------------------+--------------------------------------+ + | :const:`A_DIM` | Half bright text | + +----------------------+--------------------------------------+ + | :const:`A_REVERSE` | Reverse-video text | + +----------------------+--------------------------------------+ + | :const:`A_STANDOUT` | The best highlighting mode available | + +----------------------+--------------------------------------+ + | :const:`A_UNDERLINE` | Underlined text | + +----------------------+--------------------------------------+ +----------------------+--------------------------------------+ -| Attribute | Description | +| 属性 | 説明 | +======================+======================================+ -| :const:`A_BLINK` | Blinking text | +| :const:`A_BLINK` | テキストを点滅 | +----------------------+--------------------------------------+ -| :const:`A_BOLD` | Extra bright or bold text | +| :const:`A_BOLD` | 高輝度またはボールドテキスト | +----------------------+--------------------------------------+ -| :const:`A_DIM` | Half bright text | +| :const:`A_DIM` | 低輝度テキスト | +----------------------+--------------------------------------+ -| :const:`A_REVERSE` | Reverse-video text | +| :const:`A_REVERSE` | 反転テキスト | +----------------------+--------------------------------------+ -| :const:`A_STANDOUT` | The best highlighting mode available | +| :const:`A_STANDOUT` | 利用できる最良のハイライトモード | +----------------------+--------------------------------------+ -| :const:`A_UNDERLINE` | Underlined text | +| :const:`A_UNDERLINE` | 下線付きテキスト | +----------------------+--------------------------------------+ -So, to display a reverse-video status line on the top line of the screen, you -could code:: - - stdscr.addstr(0, 0, "Current mode: Typing mode", - curses.A_REVERSE) - stdscr.refresh() - -The curses library also supports color on those terminals that provide it, The -most common such terminal is probably the Linux console, followed by color -xterms. - -To use color, you must call the :func:`start_color` function soon after calling -:func:`initscr`, to initialize the default color set (the -:func:`curses.wrapper.wrapper` function does this automatically). Once that's -done, the :func:`has_colors` function returns TRUE if the terminal in use can -actually display color. (Note: curses uses the American spelling 'color', -instead of the Canadian/British spelling 'colour'. If you're used to the -British spelling, you'll have to resign yourself to misspelling it for the sake -of these functions.) - -The curses library maintains a finite number of color pairs, containing a -foreground (or text) color and a background color. You can get the attribute -value corresponding to a color pair with the :func:`color_pair` function; this -can be bitwise-OR'ed with other attributes such as :const:`A_REVERSE`, but -again, such combinations are not guaranteed to work on all terminals. - -An example, which displays a line of text using color pair 1:: +.. + So, to display a reverse-video status line on the top line of the screen, you + could code:: + +つまり、反転するステータスラインを画面の最上部に表示するには、コードをこう します:: + + stdscr.addstr(0, 0, "Current mode: Typing mode", + curses.A_REVERSE) + stdscr.refresh() + +.. + The curses library also supports color on those terminals that provide it, The + most common such terminal is probably the Linux console, followed by color + xterms. + +curses ライブラリはカラー機能を提供している端末でのカラーもサポートしていま す、 +そんな端末の中で最も一般的なものは Linux コンソールで、color xterm もそれに 続きます。 + +.. + To use color, you must call the :func:`start_color` function soon after calling + :func:`initscr`, to initialize the default color set (the + :func:`curses.wrapper.wrapper` function does this automatically). Once that's + done, the :func:`has_colors` function returns TRUE if the terminal in use can + actually display color. (Note: curses uses the American spelling 'color', + instead of the Canadian/British spelling 'colour'. If you're used to the + British spelling, you'll have to resign yourself to misspelling it for the sake + of these functions.) + +カラーを利用するには、 func:`:initscr` を呼び出したすぐ後に +:func:`start_color` 関数を呼びし、デフォルトカラーセット +を初期化しなければいけません +(:func:`curses.wrapper.wrapper` 関数はこれを自動的に行ないます)。 +一旦それを行えば、 :func:`has_colors` 関数は、端末が実際にカラーを表示でき る場合に +TRUE を返します。 +(ノート: curses はカナダ/イギリスつづりに 'colour' ではなくアメリカつづり の 'color' を使います。 +イギリスつづりを使っている場合には、これらの関数のミススペルを修正する必要 があります。) + +.. + The curses library maintains a finite number of color pairs, containing a + foreground (or text) color and a background color. You can get the attribute + value corresponding to a color pair with the :func:`color_pair` function; this + can be bitwise-OR'ed with other attributes such as :const:`A_REVERSE`, but + again, such combinations are not guaranteed to work on all terminals. + +curses ライブラリは有限の数の、 +フォラグラウンド(またはテキスト)カラーとバックグラウンドカラーペアを保持し ます。 +カラーペアに対応する属性値は :func:`color_pair` 関数で取得できます; +これは :const:`A_REVERSE` のような他の属性と OR 論理演算組み合わせることが できます、 +ただし、繰り返しになりますが、組み合わせは全ての端末で保証されていません。 + +.. + An example, which displays a line of text using color pair 1:: + +例として、テキスト行をカラーペア 1 を使って表示します:: stdscr.addstr( "Pretty text", curses.color_pair(1) ) stdscr.refresh() -As I said before, a color pair consists of a foreground and background color. -:func:`start_color` initializes 8 basic colors when it activates color mode. -They are: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and -7:white. The curses module defines named constants for each of these colors: -:const:`curses.COLOR_BLACK`, :const:`curses.COLOR_RED`, and so forth. - -The ``init_pair(n, f, b)`` function changes the definition of color pair *n*, to -foreground color f and background color b. Color pair 0 is hard-wired to white -on black, and cannot be changed. - -Let's put all this together. To change color 1 to red text on a white -background, you would call:: +.. + As I said before, a color pair consists of a foreground and background color. + :func:`start_color` initializes 8 basic colors when it activates color mode. + They are: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, and + 7:white. The curses module defines named constants for each of these colors: + :const:`curses.COLOR_BLACK`, :const:`curses.COLOR_RED`, and so forth. + +前に述べたように、カラーペアはフォアグラウンドカラーとバックグラウンドカ ラーから構成されています。 +:func:`start_color` はカラーモードを有効にした場合 8 の基本カラーを初期化し ます。 +基本カラーは: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan, 7:white です。 +curses モジュールは各名前に対する名前付き定数を定義しています: +:const:`curses.COLOR_BLACK`, :const:`curses.COLOR_RED`, など。 + +.. + The ``init_pair(n, f, b)`` function changes the definition of color pair *n*, to + foreground color f and background color b. Color pair 0 is hard-wired to white + on black, and cannot be changed. + +``init_pair(n, f, b)`` 関数はカラーペア *n* の定義を +フォアグラウンド f バックグラウンド b に +変更します。 +カラーペア 0 は黒背景に白で組み込まれていて変更できません。 + +.. + Let's put all this together. To change color 1 to red text on a white + background, you would call:: + +やってみましょう。カラー 1 を白背景に赤に変更してみましょう、こうして呼び出 します:: curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) -When you change a color pair, any text already displayed using that color pair -will change to the new colors. You can also display new text in this color -with:: +.. + When you change a color pair, any text already displayed using that color pair + will change to the new colors. You can also display new text in this color + with:: + +カラーペアを変更するときには、既に表示された任意のテキストが利用するカラー ペアを新しい色に変更します。 +新しいテキストをこの色で使うこともできます:: stdscr.addstr(0,0, "RED ALERT!", curses.color_pair(1) ) -Very fancy terminals can change the definitions of the actual colors to a given -RGB value. This lets you change color 1, which is usually red, to purple or -blue or any other color you like. Unfortunately, the Linux console doesn't -support this, so I'm unable to try it out, and can't provide any examples. You -can check if your terminal can do this by calling :func:`can_change_color`, -which returns TRUE if the capability is there. If you're lucky enough to have ***The diff for this file has been truncated for email.*** ============================================================================== Revision: 0fc5a14ba4c6 Author: Akihiro Uchida <uchid****@ike-d*****> Date: Sun Jun 26 16:50:43 2011 Log: 細かい修正 howto/{curses,regex,webservers}.rst http://code.google.com/p/python-doc-ja/source/detail?r=0fc5a14ba4c6 Modified: /howto/curses.rst /howto/regex.rst /howto/webservers.rst ======================================= --- /howto/curses.rst Sun Jun 26 03:49:57 2011 +++ /howto/curses.rst Sun Jun 26 16:50:43 2011 @@ -619,7 +619,7 @@ British spelling, you'll have to resign yourself to misspelling it for the sake of these functions.) -カラーを利用するには、 func:`:initscr` を呼び出したすぐ後に +カラーを利用するには、 :func:`initscr` を呼び出したすぐ後に :func:`start_color` 関数を呼びし、デフォルトカラーセット を初期化しなければいけません (:func:`curses.wrapper.wrapper` 関数はこれを自動的に行ないます)。 ======================================= --- /howto/regex.rst Sun May 22 06:08:07 2011 +++ /howto/regex.rst Sun Jun 26 16:50:43 2011 @@ -2449,10 +2449,10 @@ そのような作業をする場合には HTML や XML パーサを利用しましょう。) .. - NOT USING RE.VERBOSE + NOT USING re.VERBOSE -------------------- -RE.VERBOSE を利用しない場合 +re.VERBOSE を利用しない場合 --------------------------- .. ======================================= --- /howto/webservers.rst Wed Jun 22 22:25:26 2011 +++ /howto/webservers.rst Sun Jun 26 16:50:43 2011 @@ -1298,6 +1298,7 @@ テンプレートエンジンにも同様のことがいえます。 TurboGears の強力な点は、構成要素が他のプロジェクトでも TurboGears への依存 無しに 簡単に利用できるということです、例えば TurboGears を支えている web サーバ CherryPy もそうです。 + .. The documentation can be found in the `TurboGears wiki <http://docs.turbogears.org/>`_, where links to screencasts can be found.