Skip to content

Developing your first Sarah Plugin in Python

SemiCode Inc edited this page Dec 25, 2016 · 3 revisions

Before going through this process you need to insure that sarah core and old plugins in this repo are working perfectly in your system . if you didn't clone sarah repo you need to check the README file for the installation guide.

Prepare for your plugin :

1- The Idea :

first of all you need to have a good idea on what will your plugin do exactly and please focus on the idea because the users won't love or use your plugins if they don't enjoy it :)

2- Creating files :

first navigate to the plugins folder using terminal or your file manager and create a new folder inside your plugins folder and name it with your plugin name . after creating the folder you will also need to create other two files : .plugin file and .py file and also prefered to have the same name of your plugin .

3- Writing the code :

lets do an example of a plugin to print hello world sentence so first thing we need to write in .plugin file so open it with your favourite text editor and paste the below lines :

[Plugin]

Module=hello

Loader=python3

Name=hello

Description=python hello plugin

change the values with your plugin's values but don't chane the Loader value you will need to write your plugin in python3 . now we need to write the python code for our plugin so open .py file with your editor and paste the below line :

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#

# hello.py

#

# Copyright 2016 Semicode Inc <[email protected]>

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

# MA 02110-1301, USA.

#

#

import gi

gi.require_version('Peas', '1.0')

gi.require_version('Sarah', '1.0')

from gi.repository import GObject, Peas, Sarah

class HiPlugin(GObject.Object, Sarah.IExtension):

`__gtype_name__ = 'HiPlugin'`

`object = GObject.property(type=GObject.Object)`

`def do_activate(self, args, argv):`
    `print("hello from Python plugin", args)`

`def do_deactivate(self):`

pass

and compile sarah again and do :

./sarah yourplugin