Outils pour utilisateurs

Outils du site


issue133:mon_histoire

A quick recap of last month’s introduction. I wasn’t happy with how Rhythmbox handles song sorting information and decided to brush up my programming skills to write a little fix/improvement. In the process, I reinforced my praise for open source software and picked up a new language thanks to Full Circle. Let’s start with the former point, and keep in mind that most of what is valid for the Gnome music player can be easily applied to other open source software. My first idea was to see if I could patch the source code. This is one of the ways you can contribute to the OSS community. See if there’s a bug or room for improvement, and propose your solution. Before I could do that I had to understand if it was a feasible option since I quit programming long ago and didn’t have too much free time. I searched the Rhythmbox project page and landed on a Gnome wiki packed with information. Scrolling down on the page, you can reach a Development Resources section (see Illustration) where you are presented with several options. At the time I am writing this, the Getting Started link goes to a non-existing page, I am sure it will be fixed by the time you read it, in any case I believe the right page should be this one: https://wiki.gnome.org/Rhythmbox/FAQ#Development_information

Voici un bref résumé de l'introduction du mois dernier. La façon dont Rhythmbox gère les informations de tri des chansons ne me plaisait pas et j'ai décidé de parfaire mes compétences en programmation pour écrire un petit correctif et/ou une amélioration. Au cours de ce processus, mes louanges des logiciels Open Source se sont renforcées et j'ai appris un nouveau langage grâce au Full Circle. Commençons par le premier point en gardant à l'esprit que la plupart des choses qui fonctionnent pour le lecteur de musique de Gnome peuvent être appliquées facilement à d'autres logiciels Open Source.

Ma première idée était d'essayer de modifier le code source. C'est l'une des façons de contribuer à la communauté Open Source : trouver un bogue ou une amélioration possible quelque part et proposer votre solution. Avant de pouvoir le faire, je devais voir si c'était réalisable, puisque j'avais abandonné la programmation il y a longtemps et n'avais pas trop de loisirs. En cherchant sur la page du projet Rhythmbox, j'ai trouvé un wiki Gnome contenant une pléthore de renseignements. Vers le bas de la page, il y a une section de « Development Resources » (voir l'illustration) où vous voyez plusieurs options. Au moment où j'écris cet article, le lien « Getting Started » (Pour commencer) mène à une page inexistante, mais je suis sûr que cela aura été corrigé au moment où vous le lirez. Quoi qu'il en soit, je crois que la page suivante devrait être la bonne : https://wiki.gnome.org/Rhythmbox/FAQ#Development_information

At the top, there is a link to a Newcomers Guide, which explains the process of how to get involved. On that page you find this sentence: “There are no requirements to start development. It can be an advantage if you know a bit of object oriented programming and git.”. I do know OOP (Object Oriented Programming), but only a faint idea of how git works. Nevertheless, I dug into the list of recommended reading, and was fascinated with the whole Gnome project, and I encourage you to do so. Although I eventually went down a different road, I would like to point out how all the tasks I mentioned are easy and achievable in the open source world where projects are shared, source code is available, and there is a community of people who are working to spread the knowledge and encourage others to contribute. With proprietary software, you normally find Application Programming Interface (API) documentation, where available, and that’s it! Anyway, going back to my browsing the wiki, I realised that I had to digest some information before I could start typing some code, but along the way I came to understand that sorting information is stored in the Rhythmbox DB, and that is stored in an XML file on the computer. I believe the location of this file has changed over time, but now you should be able to find it in ~/.local/share/rhythmbox, and you should find a file there with the name rhythmdb.xml. If you use nautilus to browse the file system, you have to enable the view of hidden files (shortcut Ctrl+H) to see the .local folder.

En haut, il y a un lien vers un Guide pour les débutants, qui explique comment s'impliquer. La phrase suivante s'y trouve : « Il n'y a pas de pré-requis pour commencer à développer, mais quelques connaissances de la programmation orientée objet et de git peuvent vous avantager. » En fait, je connais l'OOP (la Programmation orientée objet), mais je n'ai qu'une très vague idée du fonctionnement de git. J'ai néanmoins commencé les lectures recommandées et trouvé la totalité du projet Gnome passionnante ; aussi, je vous encourage à entreprendre ces lectures aussi. Bien que, finalement, j'aie choisi un autre chemin, je tiens à rappeler à quel point les tâches mentionnées sont facilement réalisables dans le monde de l'Open Source où les projets sont partagés, le code source est disponible et il y a une communauté de gens qui travaillent en vue d'étendre les connaissances et encourager d'autres à contribuer. Quant aux logiciels propriétaires, vous ne trouvez habituellement que la documentation sur l'interface de programmation de l'application (l'API), si c'est disponible, et c'est tout !

Quoi qu'il en soit, pour revenir à ma navigation sur le wiki, je me suis rendu compte que je devrais assimiler des informations avant de pouvoir commencer à taper du code, mais, au cours de mes lectures, j'ai compris que les informations de triage sont stockées dans la base de données de Rhythmbox et que celle-ci est stockée dans un fichier XML sur l'ordinateur. Je pense savoir que l'emplacement de ce fichier s'est modifié au fil du temps, mais, actuellement, vous devriez pouvoir le trouver dans ~/.local/share/rhythmbox où vous verrez en principe un fichier appelé rhythmdb.xml. Si vous utilisez Nautilus pour parcourir le système de fichiers, il faut activer l'affichage des fichiers cachés (raccourci clavier Ctrl+H) pour voir le dossier .local.

I took a peek at the file itself, you can do it too by opening the file in a text editor or in a browser. XML files are human readable by definition, and that simplified my task a lot. I could easily identify the structure of information and understand that every song’s information was packed in between two tags: <entry type=“song”> and </entry>. Inside those two markers is stored all the information for each track; you can see an example in Text. I immediately realised where sorting information was stored… towards the end there are two fields: <mb-artistsortname> and <album-sortname>. Note that these are not part of the standard MP3 tag structure, so Rhythmbox can rely only on the one inside its database. Browsing the file, I discovered that some tracks had no sorting information despite that I entered them in the program itself. I also realised that if I entered them manually in the file they would stick, and that would partially solve my problem. If not, that it is a tedious job, you have to enter them for each and every track, and if you do something wrong, you could possibly destroy the whole database. Therefore I took the decision of writing some code that would do the job for me. I knew several languages with C and C++ my favourites. I never wrote anything in Python, but I had the chance of following the lessons from Greg D. Walters on this same publication. It sounded like a good compromise for me: I didn’t go for the hard job of learning the whole Gnome/Rhythmbox development structure, but at least I would learn something new. Plus Greg and I both enjoy cooking!

J'ai jeté un œil au fichier lui-même et vous pouvez le faire aussi en l'ouvrant dans un éditeur de texte ou un navigateur Web. Par définition, les fichiers XML peuvent être lus par des êtres humains et ce fait a bien simplifié ma tâche. Je pouvais facilement identifier la structure de l'information et comprendre que l'information concernant chaque chanson se trouve entre deux balises : <entry type=“song”> et </entry>. Toute l'information pour chaque piste est stockée entre ces deux balises. Je me suis tout de suite rendu compte de l'emplacement du stockage des informations de tri… vers la fin, il y a deux champs : <mb-artistsortname> et <album-sortname>. Remarquez que ceux-ci ne font pas partie de la structure standard des étiquettes MP3, ce qui fait que Rhythmbox ne peut compter que sur la structure de sa base de données. En parcourant le fichier, j'ai découvert que certaines pistes n'avaient pas d'informations de tri, bien que je les aie entrées moi-même dans le programme. Je me suis également rendu compte que, si je les entrais dans le fichier à la main, elles restaient en place, ce qui résolvait mon problème en partie. Autrement, j'ai compris qu'il s'agit d'un travail fastidieux - il faut les rentrer pour chacune des pistes - et si vous le faites mal, vous pourriez éventuellement détruire toute la base de données.

Ainsi,j'ai décidé d'écrire un peu de code qui ferait le travail à ma place. Je connaissais plusieurs langages, dont C et C++ sont mes préférés. Je n'ai jamais écrit quelque chose en Python, mais j'ai eu l'occasion de suivre les leçons de Greg D. Walters dans cette même revue. Cela me semblait un bon compromis : je n'aimais pas l'idée de devoir travailler dur pour apprendre toute la structure de développement de Gnome/Rhythmbox, mais au moins j'apprendrais quelque chose de nouveau. En plus, Greg et moi aimons bien faire la cuisine tous les deux !

As most people do when picking up a programming language, I went back to the first chapters of Greg’s lessons and tinkered with the “Hello World” programs to understand a little how Python works. It was very easy (see FCM#27 onwards), and soon I felt ready to take on my challenge. My objective was plain: given any album, I wanted to be able to enter quickly the sorting information. As an example, for Hunky Dory by David Bowie, I wanted all the tracks to have “Bowie, David” in the field mb-artistsortname, and 1971 in the album-sortname field (the year it was released so that all the records would be chronologically sorted). To achieve that, I had to create something to manage the XML structure, look for track entries that match the given one (David Bowie/Hunky Dory), check if the sorting information is already there, if not create it, and then populate it. Now XML files are text files, but directly manipulating them seemed not a good option. Could I avoid the task of interpreting XML? A quick search in Google provided the answer: “python xml library” returned at the top of the page: https://docs.python.org/3/library/xml.html. Now I had an idea, plenty of documentation, a programming language, and already a library to tackle the issue. Next month we’ll see the code I wrote and how it works.

Comme le font la plupart des gens quand ils commencent à apprendre un langage de programmation, je suis retourné aux premiers chapitres des leçons de Greg ; j'ai bricolé un peu avec les programmes « Hello World » pour commencer à comprendre le fonctionnement de Python. C'était très facile (CF le FCM n° 27 et suivants) et je me suis bientôt senti prêt à relever mon défi.

Mon objectif était clair : pour un album donné, je voulais pouvoir rapidement saisir les informations de tri. Par exemple, pour Hunky Dory de David Bowie, je voulais que, pour toutes les pistes, le champ mb-artistsortname contienne « David Bowie », alors que le champ album-sortname devrait contenir 1971 (l'année de sa sortie, afin que tous mes disques soient triés de façon chronologique). Pour arriver à faire cela, je devais créer quelque chose qui gère la structure XML, chercher les entrées des pistes qui correspondent à celle donnée (David Bowie/Hunky Dory), vérifier si les informations de tri y sont déjà, sinon, les créer, puis les remplir. Il se trouve que les fichiers XML sont des fichiers texte ; pourtant, leur manipulation directe ne me semblait pas une bonne idée. Pourrais-je éviter l'interprétation du XML ? Une rapide recherche dans Google a fourni la réponse : après une recherche sur « python xml library » le premier résultat était : https://docs.python.org/3/library/xml.html .

Et, là, j'avais une idée, la documentation qu'il me fallait, un langage de programmation et déjà une bibliothèque avec laquelle attaquer le problème. Le mois prochain, nous regarderons le code que j'ai écrit, ainsi que son fonctionnement.

issue133/mon_histoire.txt · Dernière modification : 2018/06/01 16:01 de andre_domenech