My blog is now accessible through https!

Thanks to Let’s Encrypt, my blog can now be accessed through https! 🙂

If you wish, you can do the same on your blog/website for free! Just follow the instructions on It is really easy!

Also, thank you Nihey Takizawa for  giving me that hint!

Update: A better way for setting https on your website is by using certbot. It automates the process for getting the certificates and also for renewing it when it is about to expire.

Reading and writing bits to a file in c

When trying to implement a lzw compressor for a university class, I discovered that in c, the minimum amount of data you can write/read to a file is 1 byte, mostly because of the filesystems.

To workaround that, I wrote a simple bit buffer. It is capable of writing/reading bits of any length to/from a file. I licensed it over GPLv2 in case it is useful to anybody out there. Feel free to use/modify it:

To read bits from the file, you will want to call bb_read until it returns NULL. To write, don’t forget to call ff_flush after finishing so any remaining bits that didn’t complete a byte are written to the file.

Downgrade and force some packages on Debian to a previous version

Today I learned something interesting: How to downgrade and force some packages to stay on that version using apt, without having to manually download and install any .deb.

Why I had to do that? The xserver 1.16 transition hit Debian jessie (the actual testing) today, and since the nvidia proprietary driver isn’t still compatible with it, it got removed. Yes, I know I could have avoid dist-upgrade and apt would hold it until it was safe to do the migration, but I wasn’t really paying attention at the moment (one mistake I’ll for sure never do again =P).

Well, downgrading something that you installed from another release (e.g. unstable, experimental) is easy. You can force it by version (apt-get install foo=1.0 for example) or play with apt preferences. But since the previous version vanished from the repository, I though I would need to download _all_ the xserver-xorg .deb and do a dpkg -i by hand.

That’s when I found Debian snapshots archive.  In that archive you can find any package version if you remember when you last saw it. Knowing that the last good xserver version (good as in “is co-installable with nvidia driver”) was on testing yesterday (2014-07-15), I added this line to my sources.list:

deb testing main contrib non-free

After an apt-get update I got the information of all packages in that archive. As stated on the snapshots webpage and commented by HearNothingron on the comments above, you might need to run apt-get update with “-o Acquire::Check-Valid-Until=false” to ignore the Valid-Until header, depending on how old the snapshot is.

Now comes the interesting part. I added this to my /etc/apt/preferences:

Package: xserver-*
Pin: origin ""
Pin-Priority: 1001

Because I set a priority of 1001 on all xserver-* packages that comes from the snapshot, it forces that version to be installed even if it ends up in a downgrade (that’s exactly what I needed). All you need to do is hit an apt-get upgrade (maybe dist-upgrad is needed here).

By doing that I was able to downgrade xserver and install the nvidia proprietary driver again. When an update to that driver with xserver 1.16 support comes, I can safely remove that pin and that source and upgrade normally.

I used this for a specific reason, downgrading and keeping xserver 1.15. Obviously you can do the same for any package out there.


Edit 2014-07-20:

For those of you who came here because of the broken nvidia packages, a new driver version with xserver 1.16 support has been uploaded to unstable, as you can see on this nvidia qa page. Its priority is set to high, so it should hit testing in 2 days. When that happens, this workaround can be safely removed.

Talk about Stoq on FISL14

I just came back from FISL14 at Porto Alegre – Brasil, where I gave a talk about Stoq. You can check the video here:

I just want to say thank you to my employer, Async Open Source, for allowing and providing me a way to go to the event. It was really great! Hope I can go again next year and give an even better talk!

Yet another Vim tips post

I’ve been using vim as my texteditor/codeeditor/ide/etc for about 2 years now. Since then, I tried a lot of different plugins, configurations, and end up with a good set for my workflow. I decided to share here some of those, maybe someone will find them useful 😛

First, here’s a list of my most useful plugins and some configurations I’ve done to them:

CtrlP is by far my most useful plugin. I used to use Command-T, but it’s dependency on ruby was giving me lots of headaches. It searches for files, recursively from the work directory (the place where you opened (g)vim) in a fuzzy way. You call it’s search function and start typing the file pattern.

ctrlp.vim plugin

You can also use it to search for opened buffers in the same fuzzy way.  And here is some options I’ve put on my ~/.vimrc:

" This will flush CtrlP and remake it's cache
nmap <C-F7> :CtrlPClearCache<CR>
" This will open the CtrlP fuzzy search window for files
nmap <silent> <Leader><Space>f :CtrlP<CR>
" This will open the CtrlP fuzzy search window for buffers
nmap <silent> <Leader><Space>b :CtrlPBuffer<CR>
" This will open the CrtrlP fuzzy search window for tags
nmap <silent> <Leader><Space>t :CtrlPTag<CR>

Indexer makes it very easy to automatically generate (and maintain) tags for your projects, so you can jump to them using CTRL+], g+] or even CTRL+Click. All you have to do is install it and it’s dependencies (see the homepage) and do a simple configuration.

There is tree ways of configuring it. One is by using it as an extension to project.tar.gz plugin. Another way is by creating a .vimprj folder inside your project’s folder, so as soon as it finds that folder, it’ll understand that that’s a projects’ folder and create tags inside that .vimprj. The last one, the one I use, is to create a ~/.indexer_files file and specify a projects parent folder. That way is perfect for people who keeps all of their projects inside the same directory, and it supports nested directories. Here is my configuration as an example:


By doing that, the tags will be generated/updated at a folder ~/.vim_indexer_tags.  It’s so simple that I never have to remember to generate/update tags for none of my projects, even one that I just created (since they will be on one of PROJECTS_PARENT directories).

That’s another very useful plugin for people who uses grep a lot. It grep for a pattern, and populate vim’s quickfix window with all the found patterns, so it’s easy to access them, so you can navigate through them using ]q (the same as :cnext)  and [q (the same as :cprev).

And obviously, you can setup some maps to make it easier to grep your pattern. Here is a piece of my ~/.vimrc:

" Files to skip
let Grep_Skip_Files='*.bak *~ *.pyc *.o *.obj'
" Directories to skip
let Grep_Skip_Dirs='.bzr .git .hg'
" The grep itself
nnoremap <silent> <Leader>gg :Grep<CR>
" Grep recursively
nnoremap <silent> <Leader>gr :Rgrep<CR>
" Grep buffer
nnoremap <silent> <Leader>gb :Bgrep<CR>

Those are just my must-have list. There are some other that I won’t detail here but I find useful too, like:

To finish, here is some general-use pieces of my ~/.vimrc:

" Dictionary and Spell Options
set spelllang=en,pt
set dictionary+=/usr/share/dict/words
" Jump to the last position when the file was last opened..
autocmd BufReadPost * if line("'"") > 1 && line("'"") <= line("$") | exe "normal! g'"" | endif
" Visual help to keep text on 80 columns
set textwidth=79
autocmd BufEnter * hi ColorColumn ctermbg=lightred ctermfg=white guibg=#592929
if version >= 703
  autocmd BufEnter * set cc=+1
  autocmd BufEnter * match ColorColumn /%80v.+/
" Clear highlight when refreshing.
nnoremap <C-L> :nohls<CR><C-L>
inoremap <C-L> <C-O>:nohls<CR>
" Make Y compatible with D
nnoremap Y y$
" Make Shift+Insert paste from global clipboard
nnoremap <S-Insert> "+p
inoremap <S-Insert> <C-O>"+p
noremap! <S-Insert> <MiddleMouse>
" Continous visual indenting
vnoremap < <gv
vnoremap > >gv

Most of those tips/plugins were found googling, on vim tips wiki and vim scripts itself.

Suggestions for improving this post are very welcome! 🙂

Gnome-shell Notifications Alert Extension

If there is anything that I dislike on gnome-shell are the notifications… Well, not the notifications themselves, I really like the whole bottom hot corner concept and stuff, but it’s very easy for me, specially at my work that I use dual monitors, to miss an instant messenger message that way.

Because of that, I tried searching for a shell extension that provides something to alert me of the existence of an unread notification. I found the Pidgin Persistent Notification‘s concept very good and decided to try to do a generic one (since it’s pidgin specific and I don’t use it). This is the result:

Without unread notifications / With unread notifications

It supports any shell version >= 3.2. You can download it on extensions.gnome at the link: Gnome-Shell Notifications Alert

The source code is on github <> and licensed at GPLv2. Fell free to fork it! Just don’t forget to send me a pull request, so I can merge your improvements into the official code ;D

Writing asynchronous Python code with Twisted using inlineCallbacks

A few weeks ago, I started using twisted to develop a plugin for synchronization between Stoq and Magento.

Twisted is a really great tool, and it makes it very easy to write asynchronous code. The only problem is that your code has a high probability of becoming Spaghetti code :P. And anyone who knows me knows that I’m crazy when it comes to code organization.

Well, those days, specially after a hint by a work mate, Johan Dahlin, I started to take a look on inlineCallbacks decorator. It’s just beautiful and solve all my problems :).

Since I found the documentation a lot hard to understand, and very few examples on the web, I decided to try to make one of my own. Hope you will enjoy!

To start, consider the following piece of code (using the classic Twisted way):

from twisted.internet import reactor
def on_failure(err):
    print "Error:", err
def on_success(*args):
    print "Success. Shutting down"
def print_file(file_):
    d = async_print_file(file_) # This will return a Deferred
def get_file():
    d = async_get_file() # This will return a Deferred
if __name__ == '__main__':

On this example, we want to get a file, print it, and then shutdown the application. Yeah, it’s ugly, a little spaghetti (could be a lot more if the code wasn’t a simple example)… but it works.

Just for a fast explanation, the async_*() functions are fictitious functions that will return a Deferred. When it’s fired, it’ll call the function added by addCallback, or, in case of failure, the one added by addErrback. If more than one callback (or errback) is added, when the first one returns, that return value will be passed to the second function, and so on, as a chain of callbacks.

Now, take a look at the following piece of code (utilizing the inlineCallbacks way):

from twisted.internet import defer, reactor
def print_file():
        # async_get_file still returns a Deferred
        file_ = yield async_get_file() # After yield, it's not a Deferred anymore
        yield async_print_file(file_)
        print "Success."
    except Exception as err:
        print "Error", err
        print "Shutting down"
if __name__ == '__main__':

Beautiful, isn’t it? Well, not really the most beautiful piece of code you will see around the world, but better than the previous example. What does all the magic is the yield statement (without using it, file_ would still be a Deferred)

When the code inside a function decorated by the inlineCallbacks decorator yields a Deferred (in that case, a function that returns a Deferred), the code goes on and the reactor will come back after the Deferred fires. It’s return value will be returned on the yield statement, and, if any errors occurred, the exception will be raised (that’s why I yielded inside a try/except clause).

Note that, because yield is captured by inlineCallbacks, there’s no way to use that function as an iterator generator.

And if we need to call another function decorated by inlineCallbacks? How to get it’s return value, as the return statement won’t work? Well, that’s why there is a function called returnValue. Take a look at this piece of code:

from twisted.internet import defer, reactor
def get_arg():
    retval = yield another_async_func()
def print_file():
        arg = yield get_arg()
    except Exception as err:
        arg = None
        file_ = yield async_get_file(arg)
        yield async_print_file(file_)
        print "Success."
    except Exception as err:
        print "Error", err
        print "Shutting down"
if __name__ == '__main__':

In this example, we assumed that async_get_file needed an expecific argument, that needs to be retrieved asynchronous too. By doing returnValue(arg), we make anyone who yields get_arg() to receive arg, or raise an exception if an error occour.

A little complicated but, after a while you get used to it! 😉

Any doughs?

Free software development, Open source, GNU/Linux – by Thiago Bellini

%d bloggers like this: