issue134:mon_histoire
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue134:mon_histoire [2018/07/01 15:02] – créée auntiee | issue134:mon_histoire [2018/07/25 20:04] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last month we ended on a happy note, with all the information we needed to attempt to solve the issue at stake. What was it? I wanted to be able to enter sort information in Rhythmbox in an easier way than to do it manually track by track. Which is boring when all the tracks on the same album share the the same data, plus it was frustrating that at times they even got lost (maybe a bug, maybe only my case). We figure out that an xml file contains the data we want to manipulate and python offers an xml library ready to do it. So what is next? | + | **Last month we ended on a happy note, with all the information we needed to attempt to solve the issue at stake. What was it? I wanted to be able to enter sort information in Rhythmbox in an easier way than to do it manually track by track. Which is boring when all the tracks on the same album share the the same data, plus it was frustrating that at times they even got lost (maybe a bug, maybe only my case). We figure out that an xml file contains the data we want to manipulate and python offers an xml library ready to do it. So what is next? |
I had to decide if I wanted to write a command line interface program (CLI) or a graphical user interface (GUI). The former are the text only ones you launch from the terminal, like grep or mp3diags, the latter ones are with windows and icons like audacity or Gnome text editor (gedit). | I had to decide if I wanted to write a command line interface program (CLI) or a graphical user interface (GUI). The former are the text only ones you launch from the terminal, like grep or mp3diags, the latter ones are with windows and icons like audacity or Gnome text editor (gedit). | ||
- | Surely enough a GUI program is nicer and looks like the preferable choice. As I said in the introduction my programming knowledge needed a brush up and I had no experience in linux / gnome graphic environment. Add to that it would be my first python program I was very sceptical on going down that road. | + | Surely enough a GUI program is nicer and looks like the preferable choice. As I said in the introduction my programming knowledge needed a brush up and I had no experience in linux / gnome graphic environment. Add to that it would be my first python program I was very sceptical on going down that road.** |
- | I have also seen that some linux applications are CLI based and then they also have GUI version, which looked like it was using the command based program to do the job. For example some software management application, | + | Le mois dernier, nous avons terminé sur une note positive, car nous avions toutes les informations nécessaires pour essayer de résoudre mon problème. Qu' |
+ | |||
+ | Je devais décider si je voulais écrire un programme en ligne de commande (CLI) ou avec une interface utilisateur graphique (GUI). Un programme écrit pour CLI est lancé dans un terminal, par exemple grep ou mp3diags alors qu'en GUI, il y a des fenêtres et des icônes, par exemple Audacity ou l' | ||
+ | |||
+ | Bien entendu, un programme en GUI est plus sympa et semble le choix préférable. Comme j'ai dit dans l' | ||
+ | |||
+ | **I have also seen that some linux applications are CLI based and then they also have GUI version, which looked like it was using the command based program to do the job. For example some software management application, | ||
I called it fixrhy on the assumption that it was fixing something that rhythmbox wasn’t doing the way I wanted it. I know it’s arguable but hey, my application, | I called it fixrhy on the assumption that it was fixing something that rhythmbox wasn’t doing the way I wanted it. I know it’s arguable but hey, my application, | ||
- | Ready to start my first ever python program I launched the Gnome text editor which I figured was more than enough for the task at hand. | + | Ready to start my first ever python program I launched the Gnome text editor which I figured was more than enough for the task at hand.** |
- | The logic of the program is very simple. As input it requires the name of the artist, the name of the album, the sorting name of the artist and the sorting name of the album. After writing the basic code I added a fifth parameter which I’ll talk about later. The output would be just some confirmation messages, while the real action is actually manipulating the xml file that rhythmbox uses to store its database. The full code of the final version is available here: https:// | + | J'ai également remarqué que certaines applications Linux sont basées sur CLI, mais ont également une version GUI, qui me donnait l' |
+ | |||
+ | Je l'ai appelé fixrhy en supposant qu'il réparerait quelque chose que rhythmbox ne faisait pas comme je voulais. Je sais que c'est discutable, mais après tout, c'est mon application et je peux lui donner le nom que je veux. | ||
+ | |||
+ | Étant prêt à commencer mon tout premier programme en Python, j'ai lancé l' | ||
+ | |||
+ | **The logic of the program is very simple. As input it requires the name of the artist, the name of the album, the sorting name of the artist and the sorting name of the album. After writing the basic code I added a fifth parameter which I’ll talk about later. The output would be just some confirmation messages, while the real action is actually manipulating the xml file that rhythmbox uses to store its database. The full code of the final version is available here: https:// | ||
Line 14 is: | Line 14 is: | ||
+ | # | ||
+ | |||
+ | La logique du programme est très simple. Vous devez renseigner le nom de l' | ||
+ | |||
+ | La ligne 14 est : | ||
# | # | ||
- | Technically this is needed to execute the code as a Python program. To me it is the statement that I wanted to put in practice something I learned on Full Circle pages, and knowing that if I need help there’ll be a community out there ready to help. I could have used C (or C++) that I already knew, but that would have taken away a bit of the fun. Learning something new it is always challenging and it offers more opportunity of reaching out for others and live in the community. Case in point, I eventually wrote to Greg Walter and Ronnie Tucker (I’ll come back to it later on). | + | **Technically this is needed to execute the code as a Python program. To me it is the statement that I wanted to put in practice something I learned on Full Circle pages, and knowing that if I need help there’ll be a community out there ready to help. I could have used C (or C++) that I already knew, but that would have taken away a bit of the fun. Learning something new it is always challenging and it offers more opportunity of reaching out for others and live in the community. Case in point, I eventually wrote to Greg Walter and Ronnie Tucker (I’ll come back to it later on). |
Line 18 is: | Line 18 is: | ||
Ligne 24: | Ligne 41: | ||
Line 22 is: | Line 22 is: | ||
+ | sys.setdefaultencoding( " | ||
+ | |||
+ | Techniquement, | ||
+ | |||
+ | la ligne 18 est : | ||
+ | import xml.etree.ElementTree as ET | ||
+ | |||
+ | Ceci est nécessaire pour pouvoir utiliser la bibliothèque ElementTree pour analyser les fichiers XML. J' | ||
+ | |||
+ | La ligne 22 est : | ||
sys.setdefaultencoding( " | sys.setdefaultencoding( " | ||
- | If I recall correctly this became necessary after struggling with accented letters (as in Beyoncé) that are quite common in names. | + | **If I recall correctly this became necessary after struggling with accented letters (as in Beyoncé) that are quite common in names. |
Next I have to check if I have enough information to go on. Remember we said we need 4 pieces of data, 2 to identify an album (artist and album name) and 2 to set their respective sort values. The fifth parameter is option (to override existing sorting information). We do it at line 25: | Next I have to check if I have enough information to go on. Remember we said we need 4 pieces of data, 2 to identify an album (artist and album name) and 2 to set their respective sort values. The fifth parameter is option (to override existing sorting information). We do it at line 25: | ||
Ligne 32: | Ligne 59: | ||
if len(sys.argv) < 5: | if len(sys.argv) < 5: | ||
- | Note that we check for 5 since the first value is always the script name (check https:// | + | Note that we check for 5 since the first value is always the script name (check https:// |
- | Line 42 and 43 looks like debugging lines: | + | Si je m'en souviens bien, cela est devenu obligatoire après m' |
+ | |||
+ | Ensuite, je dois vérifier si j'ai assez d' | ||
+ | |||
+ | if len(sys.argv) < 5: | ||
+ | |||
+ | Notez que nous vérifions pour 5, puisque la première valeur est toujours le nom du script (regardez à https:// | ||
+ | |||
+ | **Line 42 and 43 looks like debugging lines: | ||
print " | print " | ||
Ligne 51: | Ligne 86: | ||
if entry.find(' | if entry.find(' | ||
- | If any of them is false we can discard that given entry since we don’t need to amend it. | + | If any of them is false we can discard that given entry since we don’t need to amend it. ** |
- | From line 58 we check if the artist sort information is already there. The reason being that if the field is missing we need to create it new (60), if it is already there we have to change it (or leave it unchanged). Former case we could not modify something not existing, latter case we should not create something that is already there. | + | Les lignes 42 et 43 ressemblent à des lignes de débogage : |
+ | print " | ||
- | The rest of the code builds on that, and there is a similar part of the album sort information. The only two points I want to make here are that I put in the code the parameter to choose if to modify existing data or not. Personally I would always change it, but I wanted to write a program that eventually could be used by others, maybe with different needs. Also note that since I couldn’t change the element if existing I destroy and create a new one (68-70). Most of the print commands are there for debug originally and then to be used by GUI as I said before about line 42/ | + | print " |
- | Finally before saving we make a backup copy (always important!): | + | et, en fait, elles l' |
+ | |||
+ | De 45 à 50, nous réglons tout, chargeant la base de données de Rhythmbox dans un arbre (47) et déclarant quelques variables de comptage. | ||
+ | |||
+ | Le véritable traitement commence à la ligne 52 ou nous parcourons chaque entrée dans l' | ||
+ | |||
+ | if entry.attrib == {' | ||
+ | |||
+ | if entry.find(' | ||
+ | |||
+ | if entry.find(' | ||
+ | |||
+ | Si une entrée quelconque est fausse, nous pouvons la supprimer, puisque nous n' | ||
+ | |||
+ | **From line 58 we check if the artist sort information is already there. The reason being that if the field is missing we need to create it new (60), if it is already there we have to change it (or leave it unchanged). Former case we could not modify something not existing, latter case we should not create something that is already there. | ||
+ | |||
+ | The rest of the code builds on that, and there is a similar part of the album sort information. The only two points I want to make here are that I put in the code the parameter to choose if to modify existing data or not. Personally I would always change it, but I wanted to write a program that eventually could be used by others, maybe with different needs. Also note that since I couldn’t change the element if existing I destroy and create a new one (68-70). Most of the print commands are there for debug originally and then to be used by GUI as I said before about line 42/43.** | ||
+ | |||
+ | À partir de la ligne 58, nous vérifions si l' | ||
+ | |||
+ | Le reste du code s' | ||
+ | |||
+ | **Finally before saving we make a backup copy (always important!): | ||
shutil.copy2(filename, | shutil.copy2(filename, | ||
Ligne 65: | Ligne 123: | ||
tree.write(filename, | tree.write(filename, | ||
- | I am quite happy with my first Python program ever. It does what it says on the tin but there is room for much improvements. Amongst them surely pass the file position as a parameter (like this you should run the script in the folder with the Rhythmbox DB file). The parsing maybe not the most efficient and fast. The naming could be done automatically, | + | I am quite happy with my first Python program ever. It does what it says on the tin but there is room for much improvements. Amongst them surely pass the file position as a parameter (like this you should run the script in the folder with the Rhythmbox DB file). The parsing maybe not the most efficient and fast. The naming could be done automatically, |
+ | |||
+ | Enfin, avant de l' | ||
+ | |||
+ | shutil.copy2(filename, | ||
+ | |||
+ | et l' | ||
+ | |||
+ | Mon tout premier programme en Python me plaît bien. Il fait ce qu'il est censé faire, mais il y a une marge de progression et il peut être amélioré. Parmi de telles améliorations, | ||
+ | |||
issue134/mon_histoire.1530450167.txt.gz · Dernière modification : 2018/07/01 15:02 de auntiee