Tag: scripts


Let us play with D-Bus \o/ (quick example with python and spotify)

Informatique,
Tags: python, scripts

D-Bus is a free and open-source inter-process communication (IPC) system, allowing multiple, concurrently-running computer programs (processes) to communicate with one another.

But if you are here, you probably already know what it is, and you may want to know how to quickly use it to talk with your applications.

You have probably already heard about tools like dbus-send or qdbus, but you first need to know which messages you can send.

The solution's name is D-Feet. It's a graphical tool that allows you to seamlessly and easily use D-Bus introspection functions to quickly find which methods and properties are available for your applications.
If you need a console tool, you can also try mdbus2.

Now, one quick example: let's say that you would like to control your spotify client.

  1. Launch Spotify
  2. Type "spotify" in D-Feet's search field. If nothing appears, check that you are looking in the "Session Bus", and not the "System Bus"
  3. You should easily find methods such as "Play", "Pause", "PlayPause", "Next", "Previous", "Stop" and a few other ones that are pretty self-explaining. Here, you can double clic on methods to call them, or on readable properties to read them.
  4. Now launch a python, with dbus-python installed (dbus-python >= 1.0 works with py2 & py3) and imported: >>> import dbus
  5. Get an interface object
bus = dbus.SessionBus()
proxy = bus.get_object('org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
interface = dbus.Interface(proxy, dbus_interface='org.mpris.MediaPlayer2.Player')

Where org.mpris.MediaPlayer2.spotify is the "bus name" you can see on the left column of D-Feet, /org/mpris/MediaPlayer2 is the "object path" in the first level of the tree in the right column, and org.mpris.MediaPlayer2.Player is the "dbus interface" which will allow you to directly call methods.

  1. What now ? Nothing, you're done ! Wanna switch play/pause ? interface.PlayPause() ! Wanna stop ? interface.Stop()
  2. Ok, ok, if you need to get or set properties, you have to use the getter and setter in the interface org.freedesktop.DBus.Properties:
props_iface = Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
props_iface.GetAll("org.mpris.MediaPlayer2.Player")

But it seems to be a really poor example because the spotify client just does not implement all the standard functionnalities of org.mpris.MediaPlayer2:( (tried interface.Play() ? Sorry :P)

Oh, and three more things:

  • You can find my script in py3 on github
  • if you really want bash:
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify \
    /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause
  • Another example to connect your bluetooth device: github

i3 free workspace

Informatique,
Tags: python, scripts, window-manager

A few years ago, I decided to give a try to some Tiling Window Manager. I began with Awesome, which was… Awesome. But sometimes, you want something new, and I tried XMonad. Great, but kind of hard to configure if you are not eating Haskell for your breakfast.

And a few days ago, I followed some friends and set up i3.

It is almost perfect for me, but it missed one important feature : beeing able to switch to (or move a container to) the next (or previous) free workspace. I think it is really cool to go to the next used workspace with mod4 + , but I needed my mod4 +ctrl + .

So, here is the quick-and-dirty script that let you do that https://github.com/nim65s/scripts/blob/master/i3-free-workspace.py

Then, in your .i3/config:

bindsym $mod+Control+Left exec i3-free-workspace.py switch prev
bindsym $mod+Control+Right exec i3-free-workspace.py switch next
bindsym $mod+Control+Shift+Left exec i3-free-workspace.py move prev
bindsym $mod+Control+Shift+Right exec i3-free-workspace.py move next

Well, with this quality of code, I don't think that it could help anybody, but why not ?


Git pre-commit hook & git commit --all

Informatique,
Tags: git, scripts

It's generally good when a task you have to do many times a day is really fast. For example, when I want to make a commit, I just type gc <message>, because I made gc an alias for git commit -am. And when you have to commit, it's hard to go faster.

But sometimes, fast is not enough.

Today, I just tried to add a pre-commit hook that checks pep8 for the python files I’m about to commit. Easy.

The man page says that pre-commit is bypassed when we use the option --no-verify, so everything should be OK.

BUT sadly git commit -a also bypasses the pre-commit hook. And I don't wanna loose my time adding manually all the modified files in the repo. git add * is definitively not a solution.

However, the solution is quite easy: let the pre-commit script do the right git add stuff !

By "the right stuff", I mean git update-index --add, because it seems that on some systems with some versions of git, git add in a pre-commit adds files but for the next commit… 

Here is my implementation of this solution: https://github.com/nim65s/scripts/blob/master/pre-commit

Now, you may want to add this pre-commit to all your git repositories in every computer you use… Well, just look at this one https://github.com/nim65s/scripts/blob/master/pre-commit-deploy.sh (it just takes a ~/.gitrepos which contains all the repositories you use on the current computer… ie the config file for this script https://github.com/nim65s/scripts/blob/master/gitup.sh)