The article is devoted to the new Ubuntu Unity environment feature - the lens. What is a lens in Unity? Simply put, a lens is an area of the main menu where the user can search for local and network content. About how you can simply create it is discussed in this article. Original text
from here (eng.)
Requirements
So, to create a lens, we need:
')
*
Ubuntu 12.04 LTS -
get Ubuntu ›
*
Quickly -
install free quickly ›
*
Quickly Lens template -
install Quickly Lens template ›
Creating a lens
To begin with, we will write a lens that looks for among Wikipedia articles. Creating a lens begins with a simple step - creating a project.
To do this, press Ctrl + Alt + T and in the terminal window that appears, enter the following commands:
quickly create unity-lens wikipedia cd wikipedia

Let's get started!
quickly edit
This command will open three files in your default text editor, we are only interested in
__init__.pyThe first thing we need is the Meta class. This is our lens description. See:
class Meta: name = 'Wikipedia' description = 'Wikipedia Lens' search_hint = 'Search Wikipedia' icon = 'wikipedia.svg' search_on_blank=True
To begin with, since we are making a simple lens, everything can be left unchanged.
The lens needs categories for the visual separation of different types of results. For Wikipedia, we will need only one category, which we will call “Articles”
After the Meta class, we see the following lines of code:
example_category = ListViewCategory("Examples", 'help')
We will modify it for our needs.
* First, we will change the category name to articles_category
* Then, we have the choice between ListView and IconView for a different view of the results within the category. We choose: ListView
* We also need to give a display name for our category. It's simple: Articles
* And finally, we need to select an icon for our category and we will take it from the system workspace, namely: dialog-information-symbolic
As a result, we get the line:
articles_category = ListViewCategory("Articles", "dialog-information-symbolic")
The internal architecture of the lens is ready, now we begin to design the search.
The standard template code demonstrates how the result hits the lens:
def search(self, search, results):
... but we want to ask Wikipedia ...
Wikipedia Search
Let's make a new function that is designed to perform a search. We do this as follows.
We will call our function wikipedia_query. It will take the search string from the user as an argument. We will also take two more Python modules for our needs: urllib2 for sending an HTTP request to the network and simplejson for processing data from Wikipedia.
At the very beginning of our file, we connect the necessary modules using the import command
import urllib2 import simplejson
Then, in the WikipediaLens (SingleScopeLens) class, our main class, we add a wiki variable that simplifies our code:
wiki = "http://en.wikipedia.org"
And create a function
def wikipedia_query(self, search):
where
search is the string that the user enters into the search box. We need to adjust it a little bit, before sending Wikipedia, replace the spaces with “|”, otherwise Vika will not understand our request.
search = search.replace(" ", "|")
We create our query using the open Wikipedia API.
url = ("%s/w/api.php?action=opensearch&limit=25&format=json&search=%s" % (self.wiki, search))
And we output the result to the results variable, which is in json, for this we use the simplejson module
results = simplejson.loads(urllib2.urlopen(url).read())
We add a debugging output to understand what we are doing:
print "Searching Wikipedia for %s" % (search)
And we finish our work on the function of displaying the results, respectively
return results[1]
Our wikipedia_query function looks almost right, we need to add more try and except to prevent errors (network errors, results, etc.). To do this, we create an informational message and an empty output.
def wikipedia_query(self,search): try: search = search.replace(" ", "|") url = ("%s/w/api.php?action=opensearch&limit=25&format=json&search=%s" % (self.wiki, search)) results = simplejson.loads(urllib2.urlopen(url).read()) print "Searching Wikipedia" return results[1] except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError): print "Error : Unable to search Wikipedia" return []
Now we need to connect a freshly written function to a long-existing search.
def search(self, search, results): for article in self.wikipedia_query(search): results.append("%s/wiki/%s" % (self.wiki, article), "http://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia-logo.png", self.articles_category, "text/html", article, "Wikipedia Article", "%s/wiki/%s" % (self.wiki, article)) pass
It works like this: the search request is sent to
wikipedia_query , sent to wikipedia, the response in JSON is returned to
wikipedia_query , passed to
search and displayed in the lens. To understand what is happening in results.append it is very important to look at the output in the lens. It is passed to Unity by the pattern:
results.append (url, icon, category, mime-type, text, comment, drag and drop url)
That's all ready!Use lenses
Now the most long-awaited moment has come - we are starting to use a lens!
Enter commands in the terminal:
sudo quickly install quickly run
Lens installation
After successfully testing and debugging the lens, you can finally install it into the system. To do this, briefly talk about its internal structure. Lenses require at least three files for their work: a .lens file containing basic information about the lens; an executable file (daemon) that does all the work and a .service file containing the name of the lens and the path to the executable file. A setup.py file has been created in the project folder to install them into the system.
I used the following commands to install:
chmod 777 setup.py ./setup.py build sudo ./setup.py install

Links
*
Read a detailed article about the structure of lenses and their creation (rus.)
*
Learn more about Unity and related technologies .
*
Learn more about Unity lenses .
*
Unity API*
Wikipedia opensearch API*
Lens made for this manual.*
Selection of lenses for Dash