[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[no subject]



myKeyBindings = orca.keybindings.KeyBindings()

#Define the sayTime function
def sayTime(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%I:%M%p", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  return True
#end sayTime function

#Define the sayDate function
def sayDate(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%A, %B %d, %Y", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  return True
#end sayDate function

#Set up sayTime keys
sayTimeHandler = orca.input_event.InputEventHandler(
    sayTime,
    "Presents the time.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "t",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayTimeHandler)) # Sets Orca-t as the say time key

#add sayDate info
sayDateHandler = orca.input_event.InputEventHandler(
    sayDate,
    "Presents the date.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "d",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayDateHandler)) # Sets Orca-d as the say date key

orca.settings.keyBindingsMap["default"] = myKeyBindings
#end time and date code
As promised, this code is almost exactly the same as the one in the last section.  If you go back and compare the two files you will notice that they both have exactly the same code with the only additions being the sayDate function and the extra key binding commands.
4: Extra Functionality
Now that you have the code to add speach and/or Braille as well as that necessary to add key bindings, all that is left is adding the scripts you want.  Basically, more functions and imports.  As a good example of what is possible let's build on our existing customizations.py.  So far we can speak and Braille the current date and time.  Perhaps, however, there are times when you would like to be able to add this information to a document.  You can, of course, just type it in.  What's the fun in that though?  Also, the rules of laziness dictate that a quick key press should be used in this matter.  So, let's take our time and date script and add the information spoken or Brailled to the clipboard.  This way, once we have pressed orca-t for the time or orca-d for the date, we can press control-v to paste the provided information from the clipboard.  Here's the code.
"""This script adds time and date functionality to Orca
New in this script, time or date can be pasted from the clipboard
Adapted by Storm Dragon from the script posted at:
http://live.gnome.org/Orca/FrequentlyAskedQuestions#head-6a8c1c2511ba01d7397f68f754eec0d923d166f1
feel free to modify and/or redistribute this script as you see fit."""
#import gets code that has already been written.
#think of it as using wheels instead of trying to reinvent them.
import orca.input_event # watches for input that Orca recognizes
import orca.keybindings # Handles binding keystrokes for Orca to use.
import orca.orca # Imports the main screen reader
import orca.speech # Handles Orca's speaking abilities
import orca.braille # Displays information in Braille format
import re # Not sure

#places text in the clipboard
def setClipboardText(text):
  import gtk # import the gtk library
  cb = gtk.Clipboard()
  cb.set_text(text)
  cb.store()

myKeyBindings = orca.keybindings.KeyBindings()

#Define the sayTime function
def sayTime(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%I:%M%p", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  setClipboardText(message)
  return True
#end sayTime function

#Define the sayDate function
def sayDate(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%A, %B %d, %Y", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  setClipboardText(message)
  return True
#end sayDate function

#Set up sayTime keys
sayTimeHandler = orca.input_event.InputEventHandler(
    sayTime,
    "Presents the time.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "t",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayTimeHandler)) # Sets Orca-t as the say time key

#add sayDate info
sayDateHandler = orca.input_event.InputEventHandler(
    sayDate,
    "Presents the date.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "d",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayDateHandler)) # Sets Orca-d as the say date key

orca.settings.keyBindingsMap["default"] = myKeyBindings
#end time and date code
Although new functionality was added, the ability to paste from the clipboard, the basic script is the exact same.  We had to import the gtk library, but this was only for the setClipboardText function.  Originally I had the import gtk statement at the end of all of the Orca imports.  I got to thinking that the library was only necessary for the clipboard though so I put it in the setClipboardText function.  This serves two purposes.  First, it shows us that it's not necessary for the whole Orca script, only for the clipboard function.  Second, functions are locally scoped.  This means that things inside a function are only there while the function is in use.  So, and I may be wrong about this, because we only import gtk when it is necessary, it should speed up the script by a few mili seconds.  It doesn't sound like much, but every bit helps.
5: Adding Weather
IN this section I will show you how to get weather.  The key binding is orca-w.  This will present the current temperature | current conditions.  For example
64 | mostly cloudy
I really wanted to get this information from the top panel (where the clock is) but I couldn't find any information on accessing it.  So, I did the next best thing.  The weather info comes from Yahoo.  This was a tricky piece of code, but after studying the developer documentation and writing and rewriting, I got it working.
Here is the new script with documentation:
"""This script adds time and date functionality to Orca
New in this script, time or date can be pasted from the clipboard
Adapted by Storm Dragon from the script posted at:
http://live.gnome.org/Orca/FrequentlyAskedQuestions#head-6a8c1c2511ba01d7397f68f754eec0d923d166f1
feel free to modify and/or redistribute this script as you see fit."""
import orca.input_event # watches for input that Orca recognizes
import orca.keybindings # Handles binding keystrokes for Orca to use.
import orca.orca # Imports the main screen reader
import orca.speech # Handles Orca's speaking abilities
import orca.braille # Displays information in Braille format
import re # Not sure

#change the next line to your zip code:
zipCode = 28624

#places text in the clipboard
def setClipboardText(text):
  import gtk # import the gtk library
  cb = gtk.Clipboard()
  cb.set_text(text)
  cb.store()

#getWeather function gets weather from Yahoo
def getWeather(zip_code):
  if zip_code != 0:
    import urllib
    from xml.dom import minidom
    WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=%s'
    WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'
    url = WEATHER_URL % zip_code
    dom = minidom.parse(urllib.urlopen(url))
    ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
    weatherReport = ycondition.getAttribute('temp') + ' | ' + ycondition.getAttribute('text')
  else:
    weatherReport = "No zip code set: Please edit .orca/orca-customizations.py"
  return weatherReport

myKeyBindings = orca.keybindings.KeyBindings()

#Define the sayTime function
def sayTime(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%I:%M%p", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  setClipboardText(message)
  return True
#end sayTime function


#Define the sayDate function
def sayDate(script, inputEvent=None):
  import time # imports the Python time library
  message = time.strftime("%A, %B %d, %Y", time.localtime())
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  setClipboardText(message)
  return True
#end sayDate function

#Define the sayWeather function
def sayWeather(script, inputEvent=None):
  message = getWeather(zipCode)
  orca.speech.speak(message)
  orca.braille.displayMessage(message)
  return True
#end sayWeather function

#Set up sayTime keys
sayTimeHandler = orca.input_event.InputEventHandler(
    sayTime,
    "Presents the time.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "t",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayTimeHandler)) # Sets Orca-t as the say time key

#add sayDate info
sayDateHandler = orca.input_event.InputEventHandler(
    sayDate,
    "Presents the date.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "d",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayDateHandler)) # Sets Orca-d as the say date key

#add sayWeather info
sayWeatherHandler = orca.input_event.InputEventHandler(
    sayWeather,
    "Get current temperature and conditions.") # Shows the function of the key press in learn mode

myKeyBindings.add(orca.keybindings.KeyBinding(
    "w",
    1 << orca.settings.MODIFIER_ORCA,
    1 << orca.settings.MODIFIER_ORCA,
    sayWeatherHandler)) # Sets Orca-d as the say date key

orca.settings.keyBindingsMap["default"] = myKeyBindings
#end time, date, and weather code
Once again, the code is basically the same.  The only new additions are the sayWeather function which should seem awfully familiar, and the getWeather function which was quite interesting to write.  This script is getting kind of long wouldn't you say?  So, in the next section, let's tidy things up a bit.
6:  Tidying UP
As you have probably noticed, this orca-customizations.py file can get rather long in a hurry.  Never fear though, import to the rescue.  It is possible to write your scripts in seperate files and use import to import them into your orca-customizations.py script.  So, for example, if you took all of the code for the weather and placed it into a file called weather.py in your .orca directory, you would then use:
import weather
This, in theory, will import the weather code from weather.py and because it is now in your orca-customizations.py file, it will work as expected.
You can do pretty much anything you want with your Orca customiztions file.  The only things you really are required to have are the keybinding.  As long as you follow the pattern above, everything should be ok  Attached is the customizations.py file created in this tutorial with the added functionality of battery status reporting.
I hope you have found this helpful.  Happy coding.
Storm

--=-TjU+CGCAOsWm2aBYxnsy--



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]