home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / !ibrowse / files / pylibi-8 < prev    next >
Encoding:
GNU Info File  |  1996-11-14  |  35.5 KB  |  927 lines

  1. This is Info file pylibi, produced by Makeinfo-1.55 from the input file
  2. lib.texi.
  3.  
  4. This file describes the built-in types, exceptions and functions and the
  5. standard modules that come with the Python system.  It assumes basic
  6. knowledge about the Python language.  For an informal introduction to
  7. the language, see the Python Tutorial.  The Python Reference Manual
  8. gives a more formal definition of the language.  (These manuals are not
  9. yet available in INFO or Texinfo format.)
  10.  
  11. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The
  12. Netherlands.
  13.  
  14. All Rights Reserved
  15.  
  16. Permission to use, copy, modify, and distribute this software and its
  17. documentation for any purpose and without fee is hereby granted,
  18. provided that the above copyright notice appear in all copies and that
  19. both that copyright notice and this permission notice appear in
  20. supporting documentation, and that the names of Stichting Mathematisch
  21. Centrum or CWI or Corporation for National Research Initiatives or CNRI
  22. not be used in advertising or publicity pertaining to distribution of
  23. the software without specific, written prior permission.
  24.  
  25. While CWI is the initial source for this software, a modified version
  26. is made available by the Corporation for National Research Initiatives
  27. (CNRI) at the Internet address ftp://ftp.python.org.
  28.  
  29. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  30. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  31. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  32. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  33. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  34. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  35. ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  36. THIS SOFTWARE.
  37.  
  38. 
  39. File: pylibi,  Node: Bastion,  Prev: rexec,  Up: Restricted Execution
  40.  
  41. Standard Module `Bastion'
  42. =========================
  43.  
  44. According to the dictionary, a bastion is "a fortified area or
  45. position", or "something that is considered a stronghold."  It's a
  46. suitable name for this module, which provides a way to forbid access to
  47. certain attributes of an object.  It must always be used with the
  48. `rexec' module, in order to allow restricted-mode programs access to
  49. certain safe attributes of an object, while denying access to other,
  50. unsafe attributes.
  51.  
  52.  - function of module Bastion: Bastion (OBJECT[, FILTER, NAME, CLASS])
  53.      Protect the class instance OBJECT, returning a bastion for the
  54.      object.  Any attempt to access one of the object's attributes will
  55.      have to be approved by the FILTER function; if the access is
  56.      denied an AttributeError exception will be raised.
  57.  
  58.      If present, FILTER must be a function that accepts a string
  59.      containing an attribute name, and returns true if access to that
  60.      attribute will be permitted; if FILTER returns false, the access
  61.      is denied.  The default filter denies access to any function
  62.      beginning with an underscore (`_').  The bastion's string
  63.      representation will be `<Bastion for NAME>' if a value for NAME is
  64.      provided; otherwise, `repr(OBJECT)' will be used.
  65.  
  66.      CLASS, if present, would be a subclass of `BastionClass'; see the
  67.      code in `bastion.py' for the details.  Overriding the default
  68.      `BastionClass' will rarely be required.
  69.  
  70.  
  71. 
  72. File: pylibi,  Node: Cryptographic Services,  Next: RISCOS ONLY,  Prev: Restricted Execution,  Up: Top
  73.  
  74. Cryptographic Services
  75. **********************
  76.  
  77. The modules described in this chapter implement various algorithms of a
  78. cryptographic nature.  They are available at the discretion of the
  79. installation.  Here's an overview:
  80.  
  81. md5
  82.      -- RSA's MD5 message digest algorithm.
  83.  
  84. mpz
  85.      -- Interface to the GNU MP library for arbitrary precision
  86.      arithmetic.
  87.  
  88. rotor
  89.      -- Enigma-like encryption and decryption.
  90.  
  91. Hardcore cypherpunks will probably find the cryptographic modules
  92. written by Andrew Kuchling of further interest; the package adds
  93. built-in modules for DES and IDEA encryption, provides a Python module
  94. for reading and decrypting PGP files, and then some.  These modules are
  95. not distributed with Python but available separately.  See the URL
  96. `http://www.magnet.com/~amk/python/pct.html' or send email to
  97. `amk@magnet.com' for more information.
  98.  
  99. * Menu:
  100.  
  101. * md5::
  102. * mpz::
  103. * rotor::
  104.  
  105. 
  106. File: pylibi,  Node: md5,  Next: mpz,  Prev: Cryptographic Services,  Up: Cryptographic Services
  107.  
  108. Built-in Module `md5'
  109. =====================
  110.  
  111. This module implements the interface to RSA's MD5 message digest
  112. algorithm (see also Internet RFC 1321).  Its use is quite
  113. straightforward: use the `md5.new()' to create an md5 object.  You can
  114. now feed this object with arbitrary strings using the `update()'
  115. method, and at any point you can ask it for the "digest" (a strong kind
  116. of 128-bit checksum, a.k.a. "fingerprint") of the contatenation of the
  117. strings fed to it so far using the `digest()' method.
  118.  
  119. For example, to obtain the digest of the string "Nobody inspects the
  120. spammish repetition":
  121.  
  122.      >>> import md5
  123.      >>> m = md5.new()
  124.      >>> m.update("Nobody inspects")
  125.      >>> m.update(" the spammish repetition")
  126.      >>> m.digest()
  127.      '\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
  128. More condensed:
  129.  
  130.      >>> md5.new("Nobody inspects the spammish repetition").digest()
  131.      '\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351'
  132.  
  133.  - function of module md5: new ([ARG])
  134.      Return a new md5 object.  If ARG is present, the method call
  135.      `update(ARG)' is made.
  136.  
  137.  - function of module md5: md5 ([ARG])
  138.      For backward compatibility reasons, this is an alternative name
  139.      for the `new()' function.
  140.  
  141. An md5 object has the following methods:
  142.  
  143.  - Method on md5: update (ARG)
  144.      Update the md5 object with the string ARG.  Repeated calls are
  145.      equivalent to a single call with the concatenation of all the
  146.      arguments, i.e. `m.update(a); m.update(b)' is equivalent to
  147.      `m.update(a+b)'.
  148.  
  149.  - Method on md5: digest ()
  150.      Return the digest of the strings passed to the `update()' method
  151.      so far.  This is an 16-byte string which may contain non-ASCII
  152.      characters, including null bytes.
  153.  
  154.  - Method on md5: copy ()
  155.      Return a copy ("clone") of the md5 object.  This can be used to
  156.      efficiently compute the digests of strings that share a common
  157.      initial substring.
  158.  
  159. 
  160. File: pylibi,  Node: mpz,  Next: rotor,  Prev: md5,  Up: Cryptographic Services
  161.  
  162. Built-in Module `mpz'
  163. =====================
  164.  
  165. This is an optional module.  It is only available when Python is
  166. configured to include it, which requires that the GNU MP software is
  167. installed.
  168.  
  169. This module implements the interface to part of the GNU MP library,
  170. which defines arbitrary precision integer and rational number
  171. arithmetic routines.  Only the interfaces to the *integer* (`mpz_...')
  172. routines are provided. If not stated otherwise, the description in the
  173. GNU MP documentation can be applied.
  174.  
  175. In general, "mpz"-numbers can be used just like other standard Python
  176. numbers, e.g. you can use the built-in operators like `+', `*', etc.,
  177. as well as the standard built-in functions like `abs', `int', ...,
  178. `divmod', `pow'.  *Please note:* the bitwise-xor operation has been
  179. implemented as a bunch of ands, inverts and ors, because the library
  180. lacks an `mpz_xor' function, and I didn't need one.
  181.  
  182. You create an mpz-number by calling the function called `mpz' (see
  183. below for an exact description). An mpz-number is printed like this:
  184. `mpz(VALUE)'.
  185.  
  186.  - function of module mpz: mpz (VALUE)
  187.      Create a new mpz-number. VALUE can be an integer, a long, another
  188.      mpz-number, or even a string. If it is a string, it is interpreted
  189.      as an array of radix-256 digits, least significant digit first,
  190.      resulting in a positive number. See also the `binary' method,
  191.      described below.
  192.  
  193. A number of *extra* functions are defined in this module. Non
  194. mpz-arguments are converted to mpz-values first, and the functions
  195. return mpz-numbers.
  196.  
  197.  - function of module mpz: powm (BASE, EXPONENT, MODULUS)
  198.      Return `pow(BASE, EXPONENT) % MODULUS'. If `EXPONENT == 0', return
  199.      `mpz(1)'. In contrast to the C-library function, this version can
  200.      handle negative exponents.
  201.  
  202.  - function of module mpz: gcd (OP1, OP2)
  203.      Return the greatest common divisor of OP1 and OP2.
  204.  
  205.  - function of module mpz: gcdext (A, B)
  206.      Return a tuple `(G, S, T)', such that `A*S + B*T == G == gcd(A,
  207.      B)'.
  208.  
  209.  - function of module mpz: sqrt (OP)
  210.      Return the square root of OP. The result is rounded towards zero.
  211.  
  212.  - function of module mpz: sqrtrem (OP)
  213.      Return a tuple `(ROOT, REMAINDER)', such that `ROOT*ROOT +
  214.      REMAINDER == OP'.
  215.  
  216.  - function of module mpz: divm (NUMERATOR, DENOMINATOR, MODULUS)
  217.      Returns a number Q. such that `Q * DENOMINATOR % MODULUS ==
  218.      NUMERATOR'.  One could also implement this function in Python,
  219.      using `gcdext'.
  220.  
  221. An mpz-number has one method:
  222.  
  223.  - Method on mpz: binary ()
  224.      Convert this mpz-number to a binary string, where the number has
  225.      been stored as an array of radix-256 digits, least significant
  226.      digit first.
  227.  
  228.      The mpz-number must have a value greater than or equal to zero,
  229.      otherwise a `ValueError'-exception will be raised.
  230.  
  231. 
  232. File: pylibi,  Node: rotor,  Prev: mpz,  Up: Cryptographic Services
  233.  
  234. Built-in Module `rotor'
  235. =======================
  236.  
  237. This module implements a rotor-based encryption algorithm, contributed
  238. by Lance Ellinghouse.  The design is derived from the Enigma device, a
  239. machine used during World War II to encipher messages.  A rotor is
  240. simply a permutation.  For example, if the character `A' is the origin
  241. of the rotor, then a given rotor might map `A' to `L', `B' to `Z', `C'
  242. to `G', and so on.  To encrypt, we choose several different rotors, and
  243. set the origins of the rotors to known positions; their initial
  244. position is the ciphering key.  To encipher a character, we permute the
  245. original character by the first rotor, and then apply the second
  246. rotor's permutation to the result. We continue until we've applied all
  247. the rotors; the resulting character is our ciphertext.  We then change
  248. the origin of the final rotor by one position, from `A' to `B'; if the
  249. final rotor has made a complete revolution, then we rotate the
  250. next-to-last rotor by one position, and apply the same procedure
  251. recursively.  In other words, after enciphering one character, we
  252. advance the rotors in the same fashion as a car's odometer. Decoding
  253. works in the same way, except we reverse the permutations and apply
  254. them in the opposite order.
  255.  
  256. The available functions in this module are:
  257.  
  258.  - function of module rotor: newrotor (KEY[, NUMROTORS])
  259.      Return a rotor object. KEY is a string containing the encryption
  260.      key for the object; it can contain arbitrary binary data. The key
  261.      will be used to randomly generate the rotor permutations and their
  262.      initial positions.  NUMROTORS is the number of rotor permutations
  263.      in the returned object; if it is omitted, a default value of 6
  264.      will be used.
  265.  
  266. Rotor objects have the following methods:
  267.  
  268.  - Method on rotor: setkey ()
  269.      Reset the rotor to its initial state.
  270.  
  271.  - Method on rotor: encrypt (PLAINTEXT)
  272.      Reset the rotor object to its initial state and encrypt PLAINTEXT,
  273.      returning a string containing the ciphertext.  The ciphertext is
  274.      always the same length as the original plaintext.
  275.  
  276.  - Method on rotor: encryptmore (PLAINTEXT)
  277.      Encrypt PLAINTEXT without resetting the rotor object, and return a
  278.      string containing the ciphertext.
  279.  
  280.  - Method on rotor: decrypt (CIPHERTEXT)
  281.      Reset the rotor object to its initial state and decrypt CIPHERTEXT,
  282.      returning a string containing the ciphertext.  The plaintext
  283.      string will always be the same length as the ciphertext.
  284.  
  285.  - Method on rotor: decryptmore (CIPHERTEXT)
  286.      Decrypt CIPHERTEXT without resetting the rotor object, and return a
  287.      string containing the ciphertext.
  288.  
  289. An example usage:
  290.      >>> import rotor
  291.      >>> rt = rotor.newrotor('key', 12)
  292.      >>> rt.encrypt('bar')
  293.      '\2534\363'
  294.      >>> rt.encryptmore('bar')
  295.      '\357\375$'
  296.      >>> rt.encrypt('bar')
  297.      '\2534\363'
  298.      >>> rt.decrypt('\2534\363')
  299.      'bar'
  300.      >>> rt.decryptmore('\357\375$')
  301.      'bar'
  302.      >>> rt.decrypt('\357\375$')
  303.      'l(\315'
  304.      >>> del rt
  305. The module's code is not an exact simulation of the original Enigma
  306. device; it implements the rotor encryption scheme differently from the
  307. original. The most important difference is that in the original Enigma,
  308. there were only 5 or 6 different rotors in existence, and they were
  309. applied twice to each character; the cipher key was the order in which
  310. they were placed in the machine.  The Python rotor module uses the
  311. supplied key to initialize a random number generator; the rotor
  312. permutations and their initial positions are then randomly generated.
  313. The original device only enciphered the letters of the alphabet, while
  314. this module can handle any 8-bit binary data; it also produces binary
  315. output.  This module can also operate with an arbitrary number of
  316. rotors.
  317.  
  318. The original Enigma cipher was broken in 1944.  The version implemented
  319. here is probably a good deal more difficult to crack (especially if you
  320. use many rotors), but it won't be impossible for a truly skilful and
  321. determined attacker to break the cipher.  So if you want to keep the
  322. NSA out of your files, this rotor cipher may well be unsafe, but for
  323. discouraging casual snooping through your files, it will probably be
  324. just fine, and may be somewhat safer than using the Unix `crypt'
  325. command.
  326.  
  327. 
  328. File: pylibi,  Node: RISCOS ONLY,  Next: Function Index,  Prev: Cryptographic Services,  Up: Top
  329.  
  330. RISCOS ONLY
  331. ***********
  332.  
  333. The modules described in this chapter provide interfaces to features
  334. that are unique to the RISCOS operating system and UNIX dependent
  335. features that have been ported to RISCOS.
  336.  
  337. riscos
  338.      -- RiscOS ports of some of the posix module (normally used via
  339.      module `os').
  340.  
  341. riscospath
  342.      -- Common RiscOS pathname manipulations (normally used via
  343.      `os.path').
  344.  
  345. swi
  346.      -- An interface to RiscOS SWI operating system calls
  347.  
  348. drawf
  349.      -- An interface to the drawfile module.
  350.  
  351. riscoslib
  352.      -- An experimental interface to the RiscOS toolbox modules.
  353.  
  354. * Menu:
  355.  
  356. * riscos::
  357. * riscospath::
  358. * swi::
  359. * drawf::
  360. * RiscLib::
  361.  
  362. 
  363. File: pylibi,  Node: riscos,  Next: riscospath,  Prev: RISCOS ONLY,  Up: RISCOS ONLY
  364.  
  365. Built-in Module `riscos'
  366. ========================
  367.  
  368. This module provides RiscOS ports of some POSIX funtions, and some
  369. RiscOS specific functions.
  370.  
  371. Errors are reported as exceptions; the usual exceptions are given for
  372. type errors, while errors reported by the system calls raise
  373. `riscos.error', described below.
  374.  
  375.  - exception of module riscos: error
  376.      This exception is raised when an RiscOS function returns a
  377.      RiscOS-related error (e.g., not for illegal argument types).  Its
  378.      string value is `'riscos.error''.  The accompanying value is a
  379.      string describing the error, often that returned by the relevant
  380.      SWI.
  381.  
  382. It defines the following functions:
  383.  
  384.  - function of module riscos: chdir (PATH)
  385.      Change the current working directory to PATH.
  386.  
  387.  - function of module riscos: chmod (PATH, MODE)
  388.      Change the mode of PATH to the numeric MODE.
  389.  
  390.  - function of module riscos: expand (PATH)
  391.      Returns the canonical expansion of a pathname.
  392.  
  393.  - function of module riscos: getcwd ()
  394.      Return a string representing the current directory.
  395.  
  396.  - function of module riscos: getenv (STRING)
  397.      Returns the string value of the environment variable STRING or
  398.      `None' if it does not exist.
  399.  
  400.  - function of module riscos: listdir (PATH)
  401.      Return a list containing the names of the entries in the directory.
  402.      The list is in arbitrary order.
  403.  
  404.  - function of module riscos: mkdir (PATH [, MODE])
  405.      Create a directory named PATH, at present MODE is ignored.
  406.  
  407.  - function of module riscos: putenv (NAME, STRING [, TYPE])
  408.      Sets the environment variable NAME to STRING.
  409.     *type*
  410.           --  *Meaning*
  411.  
  412.     `0'
  413.           GSTrans the string
  414.  
  415.     `1'
  416.           Evaluate the string
  417.  
  418.     `3'
  419.           Macro variable
  420.  
  421.     `4'
  422.           No translation
  423.  
  424.      TYPE defaults to 4 (no translation of the value string).
  425.  
  426.  - function of module riscos: remove (PATH)
  427.      Remove the directory or file PATH.
  428.  
  429.  - function of module riscos: rename (SRC, DST)
  430.      Rename the file or directory SRC to DST.
  431.  
  432.  - function of module riscos: rmdir (PATH)
  433.      Remove the directory or file PATH (Same as remove).
  434.  
  435.  - function of module riscos: settype (FILENAME, TYPE)
  436.      Sets the type of FILENAME. TYPE can be an integer or a string.
  437.  
  438.  - function of module riscos: stat (PATH)
  439.      Perform a *stat* system call on the given path.  The return value
  440.      is a tuple of at least 10 integers giving the most important (and
  441.      portable) members of the *stat* structure, in the order `st_mode',
  442.      `st_ino', `st_dev', `st_nlink', `st_uid', `st_gid', `st_size',
  443.      `st_atime', `st_mtime', `st_ctime'.  The RiscOS version adds three
  444.      extra values, the file type, attributes and object type, as
  445.      returned by osfile_read_stamped_no_path.  The values `st_ino',
  446.      `st_dev', `st_nlink', `st_uid' and `st_gid' are all zero. The
  447.      three times are all equal.
  448.  
  449.      Note: The standard module `stat' defines functions and constants
  450.      that are useful for extracting information from a stat structure.
  451.  
  452.  - function of module riscos: system (COMMAND)
  453.      Execute the command (a string).  This is implemented by calling
  454.      the Standard C function `system()', and has the same limitations.
  455.      The return value is the exit status of the process as returned by
  456.      Standard C `system()'.
  457.  
  458.  - function of module riscos: unlink (PATH)
  459.      Unlink PATH (Same as remove).
  460.  
  461.  - function of module riscos: utime (PATH, (ATIME, MTIME))
  462.      Set the time stamp of the file.  (The second argument is a tuple
  463.      of two items.) At present the second argument is ignored and the
  464.      time is set to the current time.
  465.  
  466. 
  467. File: pylibi,  Node: riscospath,  Next: swi,  Prev: riscos,  Up: RISCOS ONLY
  468.  
  469. Standard Module `riscospath'
  470. ============================
  471.  
  472. This module implements some useful functions on RiscOS pathnames.
  473.  
  474. *Do not import this module directly.*  Instead, import the module `os'
  475. and use `os.path'.
  476.  
  477.  - function of module riscospath: basename (P)
  478.      Return the base name of pathname P.  This is the second half of
  479.      the pair returned by `riscospath.split(P)'.
  480.  
  481.  - function of module riscospath: commonprefix (LIST)
  482.      Return the longest string that is a prefix of all strings in LIST.
  483.      If LIST is empty, return the empty string (`''').
  484.  
  485.  - function of module riscospath: exists (P)
  486.      Return true if P refers to an existing path.
  487.  
  488.  - function of module riscospath: expandvars (P)
  489.      Return the argument with environment variables expanded.
  490.      Substrings of the form `$NAME' or `${NAME}' are replaced by the
  491.      value of environment variable NAME.  Malformed variable names and
  492.      references to non-existing variables are left unchanged.
  493.  
  494.  - function of module riscospath: isabs (P)
  495.      Return true if P is an absolute pathname (has a dollar).
  496.  
  497.  - function of module riscospath: isfile (P)
  498.      Return true if P is an existing file.  This includes image files.
  499.  
  500.  - function of module riscospath: isdir (P)
  501.      Return true if P is an existing directory. This includes image
  502.      files.
  503.  
  504.  - function of module riscospath: islink (P)
  505.      Always returns false.
  506.  
  507.  - function of module riscospath: join (P, Q)
  508.      Join the paths P and Q intelligently: If Q is an absolute path,
  509.      the return value is Q.  Otherwise, the concatenation of P and Q is
  510.      returned, with a full stop (`'.'') inserted unless P is empty or
  511.      ends in a slash.
  512.  
  513.  - function of module riscospath: normcase (P)
  514.      Normalize the case of a pathname.  This converts upper case to
  515.      lower case.
  516.  
  517.  - function of module riscospath: split (P)
  518.      Split the pathname P in a pair `(HEAD, TAIL)', where TAIL is the
  519.      last pathname component and HEAD is everything leading up to that.
  520.      The TAIL part will never contain a full stop; if P ends in a full
  521.      stop, TAIL will be empty.  If there is no full stop in P, HEAD
  522.      will be empty.  If P is empty, both HEAD and TAIL are empty.
  523.      Trailing full stops are stripped from HEAD unless it is the root.
  524.      In nearly all cases, `join(HEAD, TAIL)' equals P.
  525.  
  526.  - function of module riscospath: splitext (P)
  527.      Split the pathname P in a pair `(ROOT, EXT)' such that `ROOT + EXT
  528.      == P', the last component of ROOT contains no slashess, and EXT is
  529.      empty or begins with a slash.
  530.  
  531.  - function of module riscospath: walk (P, VISIT, ARG)
  532.      Calls the function VISIT with arguments `(ARG, DIRNAME, NAMES)'
  533.      for each directory in the directory tree rooted at P (including P
  534.      itself, if it is a directory).  The argument DIRNAME specifies the
  535.      visited directory, the argument NAMES lists the files in the
  536.      directory (gotten from `riscos.listdir(DIRNAME)'.  The VISIT
  537.      function may modify NAMES to influence the set of directories
  538.      visited below DIRNAME, e.g., to avoid visiting certain parts of
  539.      the tree.  (The object referred to by NAMES must be modified in
  540.      place, using `del' or slice assignment.)
  541.  
  542. 
  543. File: pylibi,  Node: swi,  Next: drawf,  Prev: riscospath,  Up: RISCOS ONLY
  544.  
  545. Built-in Module `swi'
  546. =====================
  547.  
  548. This module provides access to the RiscOS SWI interface. It provides a
  549. function `swi' that puts suitable values in the arm registers, calls
  550. the SWI and extracts values from registers.
  551.  
  552. As many SWIs manipulate blocks of memory a new object type is provided
  553. for handling blocks of memory and inserting and extracting values from
  554. them.
  555.  
  556. Errors are reported as exceptions; the usual exceptions are given for
  557. type, index and value errors, while errors reported by X versions of the
  558. system calls raise `swi.error', described below.
  559.  
  560. Module `swi' defines the following data item:
  561.  
  562.  - exception of module swi: error
  563.      This exception is raised when an RiscOS X SWI returns a
  564.      RiscOS-related error (e.g., not for illegal argument types).  Its
  565.      string value is `'swi.error''.  The accompanying value is a string
  566.      returned by the SWI.
  567.  
  568. It defines the following functions:
  569.  
  570.  - function of module swi: swi (NAME,DESCRIPTOR,...)
  571.      Calls the swi given by NAME with registers given by DESCRIPTOR.
  572.      nAME can either be an integer SWI number, or a string SWI name.
  573.      SWI names are case dependent. To call the X version of the SWI you
  574.      should set the X bit in the number, or preceed the name with X.
  575.  
  576.      DESCRIPTOR is a string that determines the registers to be passed
  577.      and returned. Values to pass are taken sequentially from the
  578.      remaining arguments, they are assigned to registers starting from
  579.      r0. Characters of DESCRIPTOR indicate how to pass values.
  580.  
  581.     *Character*
  582.           *Meaning*  --  *Argument Type*
  583.  
  584.     `i'
  585.           pass an integer  --  int
  586.  
  587.     `s'
  588.           pass a pointer to a string  --  string
  589.  
  590.     `b'
  591.           pass a pointer to the start of a block  --  block
  592.  
  593.     `e'
  594.           pass a pointer after the end of a block  --  block
  595.  
  596.     `0-9'
  597.           insert 0-9 in the register  --  -
  598.  
  599.     `-'
  600.           insert  in the register  --  -
  601.  
  602.     `.'
  603.           skip a register  --  -
  604.  
  605.      Note: Strings are read-only. If you want to alter a buffer passed
  606.      to a SWI you must use a block.
  607.  
  608.      If the descriptor contains a semi-colon, characters after this
  609.      refer to values returned by registers. A single value is returned
  610.      as an object. Several values are returned as a tuple.
  611.  
  612.     *Character*
  613.           *Meaning*  --  *Output Type*
  614.  
  615.     `i'
  616.           return an integer  --  int
  617.  
  618.     `s'
  619.           return a string  --  string
  620.  
  621.     `*'
  622.           return the carry flag  --  int
  623.  
  624.     `.'
  625.           skip a register  --  -
  626.  
  627.      Example: wimp_initialise
  628.  
  629.           >>> from swi import *
  630.           >>> msgs=block(2,[0x600c0,0])
  631.           >>> pname="myprog"
  632.           >>> wimpver,taskhand=swi(0x600c0,"iisb;ii",310,0x4b534154,pname,msgs)
  633.  
  634.  - function of module swi: block (SIZE [, INITIALIZER])
  635.      Returns a new block of SIZE words. The initializer must be a string
  636.      or a list of integers. The initializer is truncated to fit the
  637.      block.  If it is too short it is padded with zeros. If there is no
  638.      initializer the content of the block is undefined.
  639.  
  640. The memory of a block is guaranteed not to move unless the block is
  641. deleted or resized.
  642.  
  643. Blocks support index and slice operations as for lists of integers,
  644. except that the block cannot change size, so assignments are truncated
  645. or padded as for initialization. Slices beyond the end of the block
  646. give index errors.
  647.  
  648.  - function of module swi: register (SIZE,ADDRESS)
  649.      Registers a prexisting memory block of SIZE words as a block.
  650.      Returns a new block. The memory supplied will not be freed when
  651.      the block is deleted, or moved when it is resized.
  652.  
  653. Block items support the following data items and methods.
  654.  
  655.  - data of module swi: length
  656.      The length of the block in bytes. Equal to `4*len(BLOCK)'.
  657.  
  658.  - data of module swi: start
  659.      The start address of the memory block.
  660.  
  661.  - data of module swi: end
  662.      The address after the end of the memory block.
  663.  
  664.  - function of module swi: padstring (STRING,PADCHAR [,X,Y])
  665.      The string is copied into the block between bytes `x'  and `y-1'.
  666.      The string is truncated or padded with PADCHAR as required.  X and
  667.      Y default to 0 and `block.length'.
  668.  
  669.  - function of module swi: tostring ([X,Y])
  670.      The bytes between `x'  and `y-1' are returned as a string.  X and
  671.      Y default to 0 and `block.length'.
  672.  
  673.  - function of module swi: nullstring ([X,Y])
  674.      The bytes between `x'  and `y-1' are returned as a string, the
  675.      string is terminated before the first null character if any.  X
  676.      and Y default to 0 and `block.length'.
  677.  
  678.  - function of module swi: ctrlstring ([X,Y])
  679.      The bytes between `x'  and `y-1' are returned as a string, the
  680.      string is terminated before the first control character if any.  X
  681.      and Y default to 0 and `block.length'.
  682.  
  683.  - function of module swi: bitset (I,X,Y)
  684.      `b.bitset(x,y)' is equivalent to b[i]=(b[i]&y)^x.
  685.  
  686.  - function of module swi: resize (SIZE)
  687.      Changes the size of the block. The new size is in words. If the
  688.      block was created using `register' this just changes the recorded
  689.      end of the block. Otherwise the block may move, and the data in
  690.      the block is truncated or zero padded as neccesary.
  691.  
  692. 
  693. File: pylibi,  Node: drawf,  Next: RiscLib,  Prev: swi,  Up: RISCOS ONLY
  694.  
  695. Built-in Module `drawf'
  696. =======================
  697.  
  698. This module provides an interface to the RiscOS drawfile module. It
  699. defines a new object type that holds a drawfile. The drawf type is a
  700. sequence type.  It is considered as a sequence of objects. Assignment
  701. to elements and slices, and repetition are not yet implemented.
  702.  
  703. For more details see the documentation on the draw file format, and the
  704. DrawFile module.
  705.  
  706. Errors are reported as exceptions; the usual exceptions are given for
  707. type, index and value errors, while draw specific errors raise
  708. `drawf.error' as described below.
  709.  
  710. Module `drawf' defines the following data items:
  711.  
  712.  - exception of module drawf: error
  713.      This exception is raised on draw specific errors (e.g., not for
  714.      illegal argument types).  Its string value is `'drawf.error''.
  715.      The accompanying value is a string describing the error.
  716.  
  717. It defines the following functions:
  718.  
  719.  - function of module drawf: load (FILENAME)
  720.      Creates a new drawf object containing the drawfile loaded from the
  721.      named file.
  722.  
  723.  - function of module drawf: new ()
  724.      Creates a new empty drawf object.
  725.  
  726. Drawf objects support the following data items and methods:
  727.  
  728.  - data of module drawf: size
  729.      Returns the length in bytes of the drawfile.
  730.  
  731.  - function of module drawf: box ([N])
  732.      Returns the bounding box of object number N, or of the whole file
  733.      if no N is given. The result is a tuple of 4 integers.
  734.  
  735.  - function of module drawf: find (X,Y [, N])
  736.      Returns the index of the first object in the drawfile whose
  737.      bounding box contains (X,Y), or the first object after object N if
  738.      N is given.
  739.  
  740.  - function of module drawf: fonttable (FONTDICT)
  741.      Adds a font table object at the end of a diagram. This would
  742.      normally be called for an empty diagram, as there should only be
  743.      one such object, and it must preceed all text objects.  FONTDICT
  744.      is a dictionary with keys the font number, and values the font
  745.      names.
  746.  
  747.  - function of module drawf: group (DRAWF)
  748.      Appends the contents of DRAWF as a group object.
  749.  
  750.  - function of module drawf: path (PATHDESC,STYLE [, DASHPATTERN ,
  751.           DASHSTART])
  752.      Adds a path. PATHDESC is a list consisting of integers or pairs of
  753.      integers. The data is that required for a draw path component,
  754.      except the initial move and final end are added automatically.
  755.      The STYLE is a tuple of 4 integers representing the fill colour,
  756.      outline colour, outline width and path style description.
  757.  
  758.      The DASHPATTERN is a list of integers representing lengths of dash
  759.      elements. DASHSTART is an integer representing the distance into
  760.      the pattern to start, it defaults to zero.  The dash pattern bit
  761.      of the style description word is set automatically according to
  762.      the presence of a dash pattern.
  763.  
  764.  - function of module drawf: pathcolour (COLOUR [, N ...])
  765.      Sets the colour of paths. If no N is given all paths in the diagram
  766.      are changed, otherwise the N must be indices of path or group
  767.      objects. The path objects have their colours changed. The group
  768.      objects are searched recursively for paths.
  769.  
  770.  - function of module drawf: pathfill (COLOUR [, N ...])
  771.      Sets the fill colour. N is as for `pathcolour'.
  772.  
  773.  - function of module drawf: render (FLAGS,TRANS,CLIP,FLAT)
  774.      Renders the diagram. FLAGS and FLAT are integers.  TRANS is the
  775.      address of a block of 6 integers.  CLIP is the address of a block
  776.      of 4 integers.  See the documentation of SWI DrawFile_Render for
  777.      details.
  778.  
  779.  - function of module drawf: save (FILENAME)
  780.      Saves the contents as a drawfile with name FILENAME.
  781.  
  782.  - function of module drawf: text (POINT, TEXT, STYLE)
  783.      Adds a text object. POINT is a pair giving the x and y coordinates
  784.      of the start of the text base line.  TEXT is the text to print.
  785.      STYLE is a tuple of 5 integers, the text colour, text background
  786.      colour hint, font number, and x and y sizes of the font. The sizes
  787.      are in  point.
  788.  
  789.  - function of module drawf: textbackground (COLOUR [, N ...])
  790.      Sets the text background colour hint. N is interpreted as for
  791.      `pathcolour' but using groups and text objects.
  792.  
  793.  - function of module drawf: textcolour (COLOUR [, N ...])
  794.      Sets the text colour. N is interpreted as for `pathcolour' but
  795.      using groups and text objects.
  796.  
  797. All distances in group objects are in draw units  inch.
  798.  
  799. Colours are integers of the form `0xbbggrr00' or`-1' for transparent.
  800.  
  801. 
  802. File: pylibi,  Node: RiscLib,  Prev: drawf,  Up: RISCOS ONLY
  803.  
  804. Built-in Module `RiscLib'
  805. =========================
  806.  
  807. risclibexperimental
  808.  
  809. These modules provide an experimental interface to the RiscOS toolbox.
  810.  
  811. They provide a class `TBObject' that implements the common toolbox
  812. methods, and derived classes `ColourDbox', `IconBar', `Menu', `Quit',
  813. `SaveAs', `Window'. They provide a simple event polling system.
  814.  
  815. Neither the modules or the documentation are complete. I have added
  816. methods and objects when I needed to use them. The methods provided
  817. just call the toolbox SWIs, and can be inspected by examining the code.
  818.  
  819. The event polling system works as follows (using !Ibrowse as an
  820. example) :
  821.  
  822. The controlling program imports `tbevent'.  `tbevent' imports three
  823. dictionaries from a module `events' that include details of the toolbox
  824. events, wimp events and wimp messages that the application is
  825. interested in. The dictionaries are called `edict', `wdict' and
  826. `mdict'. They contain event or message numbers as keys and names as
  827. values. See !ibrowse.py.events for an example.
  828.  
  829. The program must supply classes with methods to handle these events. The
  830. methods will have the same names as the dictionary values.
  831.  
  832. The program calls tbevent.initialise("<ibrowse$Dir>") supplying an
  833. application directory incuding a Resource file "res" and a "Messages"
  834. file.
  835.  
  836. The program registers instances of the handler classes with tbevent
  837. (see below).
  838.  
  839. The program then calls `tbevent.poll()' repeatedly.
  840.  
  841. Toolbox event handling works as follows:
  842.  
  843. The ancestor object number of the event is used to find a class
  844. instance to handle the event. It is looked up in a dictionary
  845. `tbevent.tbclasses' to find the handler, so each class that handles
  846. toolbox methods should have a `__init__' method that registers itself
  847. in this dictionary.
  848.  
  849. Here is an example from "!Graph"
  850.  
  851.      class Display(window.Window):
  852.            actions={}                                 #1
  853.            def __init__(self):
  854.                window.Window.__init__(self,"Display") #2
  855.                tbevent.tbclasses[self.id]=self        #3
  856.                self.handle=self.getwimphandle()       #4
  857.                tbevent.winclasses[self.handle]=self   #5
  858.  
  859. In line #2 the underlying window class is initialised. This creates a
  860. toolbox object from a template named "Display" (This must be a window
  861. object. It should be an ancestor object, but not autocreated).
  862. `self.id' is set to the object identifier.  Line #3 now registers the
  863. class instance as a toolbox event handler.
  864.  
  865. If an event now arrives for an object with this ancestor its event
  866. number is looked up in `edict'. The corresponding name should be the
  867. name of a method in class `Display'. It will be called for this class
  868. instance.
  869.  
  870. The class method names are cached in a dictionary called `actions', so
  871. each handler class should contain an empty dictionary as in line #1.
  872.  
  873. Some toolbox events will have no ancestor. These will be sent to a class
  874. instance registered with a zero identifier. Most programs will have code
  875. such as this, which will handle an autocreated bar icon object.
  876.  
  877.  
  878.      class Orphans: #class to handle events with zero ancestor
  879.            actions={}
  880.            def __init__(self):
  881.                tbevent.tbclasses[0]=self
  882.            def BarClick(self):
  883.                a=Display("dir","Top")
  884.            def E_Q_Quit(self):
  885.                exit()
  886.      
  887.      oi=Orphans()  #create an instance of the class (the only one!)
  888.  
  889. Wimp event handling is similar. Wimp codes other than 7, 9, 13, 17 and
  890. 18 are sent to class instances registered by their wimp window handles.
  891. Codes 7, 9 and 13 are given handles of zero. The classes are registered
  892. in the dictionary `tbevent.winclasses'. Lines #4 and #5 in the example
  893. above give an example of registration. The names of the actions are
  894. obtained from dictionary `wdict'. Codes not in this dictionary are
  895. ignored. Note that wimp and toolbox codes use the same `actions'
  896. dictionary. This means they must have *different event numbers*. In
  897. practice this means not giving low numbers to user defined toolbox
  898. events.  Starting user defined nubers at `0x1000' is a safe rule.
  899.  
  900. Messages are handled by a single class instance `tbevent.msghandler'
  901. method names are looked up from the message number in dictionary
  902. `mdict'.
  903.  
  904. For example here is a trivial message handling class that just handles
  905. the quit message.
  906.  
  907.      class Messenger:                #This is the message handler class
  908.            def Wimp_MQuit(self):
  909.                exit()
  910.      
  911.      tbevent.msghandler=Messenger()  #Register the instance
  912.  
  913. Toolbox event methods can examine the toolbox id block as
  914. TOOLBOX.IDBLOCK.  All methods can use the wimp poll block as
  915. TBEVENT.WBLOCK.
  916.  
  917. There is a simple system to assist with debugging built in to `tbevent'.
  918. The function `tbevent.report(string,[priority])' broadcasts a wimp
  919. message containing the string (truncated to 200 characters). The
  920. application "!Reporter" can display these messages. Messages are only
  921. sent if `priority<=tbevent.usereport', so debugging messages can be
  922. suppressed by setting this variable. `priority' defaults to 16.
  923. `tbevent.usereport' is initially 8. `tbevent' generates some reports
  924. itself, error reports with priority 2, and reports of all events with
  925. priority 32.
  926.  
  927.