changeset 55:9341896b93f0

Added library tutorial documentation.
author Brian Neal <bgneal@gmail.com>
date Fri, 05 Jul 2013 19:32:45 -0500
parents 7fd3ec5580ab
children 21627ec5b1ad
files docs/index.rst examples/decrypt.py examples/encrypt.py
diffstat 3 files changed, 67 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/docs/index.rst	Fri Jul 05 14:18:52 2013 -0500
+++ b/docs/index.rst	Fri Jul 05 19:32:45 2013 -0500
@@ -144,12 +144,41 @@
 Library Tutorial
 ----------------
 
+Here is one way to perform the encrypt and decrypt operations from the
+command-line tutorial, above. In order to produce the same output, we explicity
+specify the encryption parameters: the key list, the external message
+indicator, and the system indicator. These parameters are explained in the
+reference documentation.
+
+.. literalinclude:: ../examples/encrypt.py
+
+This program outputs::
+
+   IIPDU FHLMB LASGD KTLDO OSRMZ PWGEB HYMCB IKSPT IUEPF FUHEO NQTWI VTDPC GSPQX IIPDU FHLMB
+
+A decrypt is just a bit more complicated. After constructing a ``StdProcedure``
+object, you hand it the encrypted message to analyze. The procedure object
+examines the groups in the message and extracts all the indicators. These are
+returned as a ``DecryptParams`` named tuple which indicates, amongst other
+things, what key list is required. It is then up to you to obtain this key list
+somehow. Here we use the ``read_key_list()`` function to do so. After
+installing the key list into the procedure object, you can finally call
+``decrypt()``:
+
+.. literalinclude:: ../examples/decrypt.py
+
+This program prints::
+
+   THE PI  A HAS ARRIVED STOP NO SIGN OF ENEMY FORCES STOP
+
+
 Requirements
 ------------
 
 ``m209`` is written in Python_, specifically Python 3.3. It has no other
 requirements or dependencies.
 
+
 Installation
 ------------
 
@@ -159,8 +188,8 @@
    $ pip install m209                  # install
    $ pip install --upgrade m209        # upgrade
 
-You may also download a tarball or .zip file of the latest code using the "get
-source" link on the `m209 Bitbucket page`_. Alternatively if you use
+You may also download a tarball or .zip file of the latest code by visiting the
+Downloads tab on the `m209 Bitbucket page`_. Alternatively if you use
 Mercurial_, you can clone the repository with the following command::
 
    $ hg clone https://bitbucket.org/bgneal/m209
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/decrypt.py	Fri Jul 05 19:32:45 2013 -0500
@@ -0,0 +1,20 @@
+"""Example of how to perform a decrypt operation using the standard
+procedure. Assumes a key file named m209keys.cfg exists in the current directory
+and contains the key list with indicator MB.
+
+"""
+from m209.procedure import StdProcedure
+from m209.keylist.config import read_key_list
+
+msg = ('IIPDU FHLMB LASGD KTLDO OSRMZ PWGEB HYMCB IKSPT IUEPF FUHEO NQTWI VTDPC'
+      ' GSPQX IIPDU FHLMB')
+
+proc = StdProcedure()
+params = proc.set_decrypt_message(msg)
+key_list = read_key_list('m209keys.cfg', params.key_list_ind)
+if key_list:
+    proc.set_key_list(key_list)
+    plaintext = proc.decrypt()
+    print(plaintext)
+else:
+    print("Key list '{}' not found".format(params.key_list_ind))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/encrypt.py	Fri Jul 05 19:32:45 2013 -0500
@@ -0,0 +1,16 @@
+"""Example of how to perform an encrypt operation using the standard
+procedure. Assumes a key file named m209keys.cfg exists in the current directory
+and contains the key list with indicator MB.
+
+"""
+from m209.procedure import StdProcedure
+from m209.keylist.config import read_key_list
+
+key_list = read_key_list('m209keys.cfg', 'MB')
+if key_list:
+    proc = StdProcedure(key_list=key_list)
+    plaintext = "THE PIZZA HAS ARRIVED STOP NO SIGN OF ENEMY FORCES STOP"
+    msg = proc.encrypt(plaintext, spaces=True, ext_msg_ind='PDUFHL', sys_ind='I')
+    print(msg)
+else:
+    print("Key list MB not found")