Sunday, August 16, 2009

Random Zsigs in Barnowl

Screw this, I'm breaking this "Intro to MIT Athena" stuff up into multiple entries. :) I mean, what was that maxim, "Real Artists Ship"? And considering I have 1 day left (work == no bloggy on Tuesday before 5 pm), I think I need to get going. So instead of going in order, I'm going to go with the stuff I can get done the fastest / the info that I feel can least be found elsewhere online. That'd be a) setting up a random zsig and b) using the szs service.

Abstract: We use the Zephyr (MIT messaging sytem) client Barnowl to set our zsig as a randomly selected line from a file consisting of zsigs separated by newlines.

Random Zsig: Intro

Highly recommend background reading: /help.c - barnowl - Trac and the "intro" file at /docs - barnowl - Trac.
OR view it inside barnowl. :help and :show quickstart pulls each up respectively.

A "zsig" appears after your kusername in Zephyr (see my previous post if you don't know what Zephyr is). By default, if your name is Jane Lillian Doe, your zsig is (Jane L Doe). For instance, if you're on Zephyr, you might see
   2013 / personal / jdoe  22:16  (Jane L Doe)
       ...hey I'm talking here.
A good percentage of people keep it that way, or remove the middle initial. Another set of people leave it at some interesting quote. To do so, as mentioned in the previous entry,
:set zsig 'this is a very interesting quote'
However, I had too much time on my hands earlier on in the summer, and so I set up a random zsig generator. It's very simple, actually, 'cos Barnowl is awesome :) Unfortunately, the documentation that I could find online all seemed outdated.

Well, the basic theory is that you can write some code that prints stuff out (stdout), and setup Barnowl to run that code, take the stdout, and pipe it to your zsig. (The wikipedia article is kind of technical on "piping", if it doesn't make sense just follow along).

Note: Unless you've gone and figured out Screen, don't start up Barnowl until you need to (I'll mention when).

Here we go!

  1. Remote connect to Athena.
    ssh kusername@linux.mit.edu
  2. Create a file of zsigs, each separated by a newline. Use "nano" instead of "vi" if you're not familiar with vi, and modify the second set of instructions accordingly. For instance
    vi zsigs
    i #Enter into Insert mode
    a zsig
    random zsig2
    random234
    :wq  #Write the file and quit
  3. Create a file to store your code
    vi .zrandom
  4. Write a program, for instance:
    #!/usr/athena/bin/python
    import random
    zfile = open('.zsigs')
    linelist = zfile.read().splitlines()
    zfile.close()
    print random.choice(linelist)
    If you want to use another language (e.g. perl or C), or check out what version is running on Athena, check out What Runs Where on Athena: Languages | IS&T for how to indicate on the first line the language you want.
The Above Program, Explained
Since I don't program much if at all, I relied on Google to write this program. This progam imports the module random, which has pre-written functions such as the random.choice() used later. It opens a file ("~/zsigs") where I've written a list of zsigs I want to choose from randomly. These zsigs are separated by newlines, and so I read each line into a list. For instance, if I add a
print linelist
in there, I get this:
['zsig one', '2--this is a random zsig', 'foo', 'etc.']
Once I have it in list form, I can use random.choice() to randomly choose one of the items in the list. Meanwhile, I exit the "zsigs" file once I am done reading from it.

Note: You can test your program, for instance in my case:
nouyang@dr-wily:~$ python .zrandom
www.google.com/search?q=*RECURSION*
The second line should be one of the zsigs from your list.

Setting Up Barnowl

Now, there's two ways to accomplish the same thing. We have to set it so that, when Barnowl starts up, it sets your zsig as an empty string. Then, we tell it to run the Barnowl command "zsigproc" and tell it where our zsig program is located. (If you have anything in "zsig" it will take precedence over your "zsigproc" variable). [1]

Note: To check what you've set variables as, use
:print variablename
Method 1: Manually
This really shouldn't be first, but when I set up my zsig generator I did it this way, and I learned a bit about Barnowl this way. Skip below for the easier method if you just want to get something working.
  1. Edit the file that Barnowl reads when it starts up. Again, use nano instead of vi if you're not familiar with vi, and modify the second set of instructions accordingly.
    vi .owl/startup
    i  #Enter "Insert" mode
    set zsig ''
    set zsigproc '~/.zrandom'  # The file where our zsig program is located
    [Esc]  #Escape into command mode
    :wq  #Write the file and quit
  2. Then open up Barnowl
    add barnowl; barnowl
  3. Voila! That's it.
  4. You can check that Barnowl is setup correctly. This should output zsigproc = '~/.zrandom'
    :print zsigproc
    Also, zsig should be set to an empty string. This should output zsig = ''
    :print zsig
  5. If you want to force Barnowl to reload the startup file,
    :source "~/.owl/startup"
    Also, you can zwrite yourself to check that everything's working.
Method 2: Easier
  1. Open up Barnowl
    add barnowl; barnowl
  2. Use the ":startup" command in Barnowl to write to the "~/.owl/startup" file (or wherever you decided to put the file) [2]
    :startup set zsig ''
    :startup set zsigproc '~/.zrandom'
  3. Force Barnowl to reload the startup file
    :source "~/.owl/startup"
  4. Optional: Check that the correct startup file is loaded
    :show startup
That's all there is to it!

Footnotes

[1] From /help.c - barnowl - Trac,
zsig - zephyr signature (default: '')
zsigproc - name of a program to run that will generate zsigs (default: '<null>')
[2] If you run :help startup inside Barnowl, you get
startup - run a command and set it to be run at every Owl startup  

No comments:

Post a Comment