As an exercise for learning, I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.
I am just learning about Python and therefore I am trying to use another plugin as a guide, but some important changes need to be done and that's where I probably screwed up.
When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error, but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.
This is the code I have:
PLUGIN_NAME = "Sort Artist and Album Artist"
PLUGIN_AUTHOR = "Kevin Hernandez"
PLUGIN_DESCRIPTION = "Sorts artist/album artist by name as in Artist/Album Artist field instead of Last, First"
PLUGIN_VERSION = "0.1"
PLUGIN_API_VERSIONS = ["0.9.0", "0.10", "0.15", "0.16"]
from picard.metadata import register_album_metadata_processor
import re
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))
if match:
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release):
match = re.search($not($eq(metadata["artistsort"],metadata["artist"])))
if match:
metadata["artistsort"] = metadata["artist"]
register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_album_metadata_processor(copy_artist_to_artistsort)
and I also tried defining the functions as:
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release):
metadata["artistsort"] = metadata["artist"]
I must point out that I don't fully understand when these are called. I believe the plugin documentation here, here and here is not enough to follow the plugins they have there (e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.
If there's a more thorough documentation for it, you can pinpoint what I am doing wrong in my code, or know how to include the picard module in an interpreter (where to find it AND how to include it), then your comments are very much appreciated and valid answers to this question.
I think your biggest problem is that you're mixing up the plugin API with the tagger scripting language.
Tagger scripts are written in a simple custom language; plugins are written in Python. You can't mix and match syntax between the two languages. In particular:
match = re.search($not($eq(metadata["albumartistsort"],metadata["albumartist"])))
That $not, $eq, etc. doesn't mean anything in Python. If you want to check whether things are equal, you use the == operator. If you want to use re.search, you use regular expression syntax. And so on.
Also, your code has to be valid Python, with valid indentation. Your code, at least as posted here.
But let's go through your questions one by one:
I am trying to write a plugin for MusicBrainz that matches the albumartistsort to albumartist and artistsort to artist, as opposed to the (apparently) default of Last Name, First Name format it is currently using.
There are very few automatic defaults in MusicBrainz. Each artist has a name and a sort name, in the database, entered by a human user and verified by other users. You can see this from the web interface. For example, go to David Bowie, and in the "Artist Information" panel on the right side, you'll see "Sort name: Bowie, David". If you're not used to using MusicBrainz's web interface, you should explore it before trying to expand Picard.
When I try installing the plug in, it doesn't appear in the plugin list although it is copied to the plugin folder; and the .pyo file is not generated. I am guessing this is due to a compilation error
Yep. If you run Picard from the command-line with the -d flag, it will show you the errors instead of just silently disabling your plugin, so you don't have to guess. This is documented under Troubleshooting. (If you're on a Mac, the path will be something like /Applications/MusicBrainz Picard.app/Contents/MacOS/MusicBrainz Picard; I think the docs don't explain that because it's standard OS X app bundle stuff.)
but I haven't been able to include whatever I need so I can use the picard module (don't know where to find it nor import it) so I can test in my python interpreter.
You really can't test it in your interpreter. Picard bundles its own custom-built Python interpreter, rather than using your system Python. In that custom interpreter, the picard package is on the sys.path, but in your system Python interpreter, it's not. And trying to import that package and use things out of it while not actually running the Picard GUI wouldn't be a good idea anyway.
If you really want to explore what's in the picard package, download the source and run a local build of the code. But you really shouldn't need to do that. You shouldn't need any functions beyond those documented in the API, and if you want to debug things, you want to debug them in the right context, which generally means adding print functions and/or using the logging module in your code.
I must point out that I don't fully understand when these are called.
At some point after each album is downloaded from the MusicBrainz server, all registered album processor functions get called with the album, and all registered track processor function get called with each track on the album.
Notice that an album processor isn't going to be able to change track-level fields like the track artist sort; you will need a track processor for that.
e.g. the search and match methods they use in different plugins with re are not explained in the documentation links I am referring to.
That's because they're part of the Python standard library, which is documented as part of the standard Python docs—in this case, see re.
You're expected to know the basics of Python before being able to write a Picard plugin.
Meanwhile, I'm not sure what you were trying to write here, but it looks like a really convoluted attempt to say "if these two fields aren't equal, make them equal". Which does the same thing as "unconditionally make them equal". So, why even bother with the regexp and the if condition?
So, your functions could be simplified to:
def copy_albumartist_to_albumartistsort(tagger, metadata, release):
metadata["albumartistsort"] = metadata["albumartist"]
def copy_artist_to_artistsort(tagger, metadata, release, track):
metadata["artistsort"] = metadata["artist"]
register_album_metadata_processor(copy_albumartist_to_albumartistsort)
register_track_metadata_processor(copy_artist_to_artistsort)
However, you really don't need a plugin here at all. You should be able to write this whole thing as a trivial tagger script:
$set(artistsort,%artist%)
$set(albumartistsort,%albumartist%)
Related
My program’s documentation is mainly written in Sphinx, but it also includes two custom HTML pages:
an example report produced by the program;
an extended reference on certain features of the program.
These two HTML files are produced by the program itself, not by Sphinx.
I want to host my docs on Read the Docs, and it would be very convenient for me to build and host the two custom pages, versioned, together with the Sphinx docs.
My program is already installed in the RtD build environment as I have the Install Project option enabled. And since the RtD docs mention writing your own builder, I gather it might be possible to invoke my program from there and have it dump the HTML content in a specific place.
So I really have two questions:
Is this an appropriate use of Read the Docs? I guess it’s not designed to host arbitrary Web pages — but then again, those files are not arbitrary, they are an important part of the docs.
How would I implement it? I’m having a hard time making sense of the RtD API: is this “builder” related in any way to Sphinx builders? how do I hook it up to RtD? perhaps there is an example somewhere?
I achieved the desired result using Sphinx’s html_extra_path feature:
A list of paths that contain extra files [...] They are copied to the output directory.
To generate these files, I haven’t found a better place than right in my conf.py, which seems a bit precarious, but works so far. Of course, Install your project inside a virtualenv needs to be enabled in Read the Docs advanced settings.
Now my custom notices.html and showcase.html are treated just like the .html pages produced by Sphinx itself, with versioning and redirects: http://httpolice.readthedocs.io/page/notices.html
The signals are sort-of described here but, then what?
For example, one of the signals is desribed:
invoked before writing each article, the article is passed as content
How do I change that content? How do I access it? What functions are available?
I've been looking at examples in the pelican plugins repo on github, but I'm still confused. (How did those people even learn how to write those plugins?)
I hardly know where to start.
You have to look at the source code of pelican. I think there is no better way.
For example, search for the signal you are interested in, e.g. article_generator_write_article: https://github.com/getpelican/pelican/search?utf8=%E2%9C%93&q=article_generator_write_article
Then, look into the search result, e.g. generators.py and click on the line number containing your signal. Of course, you could also create a clone and do all this locally. This depends on your way of working.
Surrounding code:
def generate_articles(self, write):
"""Generate the articles."""
for article in chain(self.translations, self.articles):
signals.article_generator_write_article.send(self, content=article)
write(article.save_as, self.get_template(article.template),
self.context, article=article, category=article.category,
override_output=hasattr(article, 'override_save_as'), blog=True)
As you can see, the signal call provides you with an article object. You can now 1) look in the source code to find the respective python class of this object to find out about its inner workings, methods and attributes or 2) go the hacky path and simply print the object's members print(article.__dict__).
I suppose, without having looked in the code, that article has an attribute content which contains the HTML code generated from your source file. This is where your desired change comes in.
Note that if you want to change the source code before processing this is not that easy. I just wrote a little plugin which is capable of doing this.
There you can also see the signal API in action. You simply have to connect a handler function to the desired signal.
I hope this helps :)
I am developing an Ansible module that generates a url, fetches (like get_url) the tarball at that url from my internal artifactory and then extracts it. I am wondering if there is a way to include or extend the get_url Ansible core module in my module. I can't have this in multiple steps because the url being used is generated from a git hash and requires a multi-step search.
If there isn't a way, I will probably just copy the whole get_url module and use it in my module, but I would like to avoid that.
I'd like to do something like:
module_json_response = module.get_module('get_url').issue_command('url=http://myartifactory.com/my_artifact.tar.gz dest=/path/to/local/my_artifact.tar.gz');
My understanding of Ansible is that it uploads the module in use and executes it, including another module isn't supported or isn't documented.
Thanks in advance for any help.
To quote Michael DeHaan's post here:
Generally speaking, Ansible allows sharing code through
"lib/ansible/module_common.py" to make writing functionality easier.
It does not, however, make it possible for one module to call another,
which has not, to date, really been needed -- that's not entirely
true, we used to have something like this for file and copy until we
got smart and moved the file attribute code into common :)
It seems like since url access is frequent enough we could make a
common function in module common for url downloads -- IF we modify the
get_url code to also use it so we aren't repeating ourselves.
He later followed up with:
You can access the way template works by writing an action
plugin, but it's more involved than writing a simple client module.
+1 to moving get_url code into common, that's come up a few times.
I have been wanting to create an application using the Microsoft Speech Recognition.
My application's users are expected to often say abbreviated things, such as 'LHC' for 'Large Hadron Collider' or 'CERN'. Given that exact order, my application will return
You said: At age C.
You said: Cern
While it did work for 'CERN', it failed very badly for 'LHC'.
However, if I could make my own custom training files, I could easily place the term 'LHC' somewhere in there. Then, I could make the user access the Speech Control Panel and run my training file.
All the links I have found for this have been frustratingly useless, as they just say things like 'This is ----, you should try going to the ---- forum instead'.
If it does help, here is a list of the links:
http://compgroups.net/comp.speech.users/add-my-own-training/153194
https://groups.google.com/forum/#!topic/microsoft.public.speech.server/v58SH1ov22s
http://social.msdn.microsoft.com/Forums/en/servercorefordevelopers/thread/f7a35f3f-b352-464a-b264-e16eb4afd049
Is my problem even possible? Or are the training files themselves in a special format? If so, can that format be reproduced?
A solution that can also work on Windows XP would be ideal.
Thanks in advance!
P.S. If there are any libraries or modules out there already for this, could anyone point me to some? A Python or C/C++ solution would be splendid. Also, since I'd rather not post another question regarding this, is it possible to utilize the train utilities from command prompt (or without the GUI visible, but still having total command of all controls)?
Okay, pulling this from a thing I wrote three or four years ago now, but I believe you want to do something like this.
The grammar library is a trained system which can recognize words. You can create your own grammar library cued to specific words.
C#, sorry
using System.Speech
using System.Speech.Recognition
using System.Speech.AudioFormat
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
string[] words = {"L H C", "CERN"};
Choices choices = new Choices(words);
GrammarBuilder gb = new GrammarBuilder(choices);
Grammar grammar = new Grammar(gb);
sre.LoadGrammar(grammar);
That is as far as I can get you. From docs it looks like you can define the pronunciations somehow. So perhaps that way you could have LHC map directly to a single word. Here are the docs on the grammar class - http://msdn.microsoft.com/en-us/library/system.speech.recognition.grammar.aspx
Small update - see example in their docs here http://msdn.microsoft.com/en-us/library/ms554228.aspx
I would like to provide my Python GAE website in the user's own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext files (.po and .mo files).
Has someone successfully combined Python Google App Engine and gettext files? If so, could you please provide the steps you used?
I had started a discussion in GAE's Google group, but haven't been able to extract from it how I'd like to do it: I don't want to add external dependencies, like Babel (suggested in the discussion). I want to use plain vanilla Google App Engine, so no manual update of Django or this kind of stuff.
At first, I will start using the language sent by the browser, so no need to manually force the language by using cookies etc. However, I might add a language changing feature later, once the basic internationalization works.
As a background note to give you more details about what I'm trying to do, I would like to internationalize Issue Tracker Tracker, an open source application I've hosted on Launchpad. I plan to use Launchpad's translation platform (explaining why I'd like to use .mo files). You can have a look at the source code in it's Bazaar branch (sorry no link due to stackoverflow spam prevention limit for new users...)
Thanks for helping me advance on this project!
As my needs were simple, I used a simple hack instead of (unavailable) gettext. I created a file with string translations, translate.py. Approximately like this:
en={}
ru={}
en['default_site_title']=u"Site title in English"
ru['default_site_title']=u"Название сайта по-русски"
Then in the main code I defined a function which returns a dictionary with translations into the most suitable language from the list (the first one to have a translation is used or English):
import translate
def get_messages(languages=[]):
msgs=translate.en
for lang in languages:
if hasattr(translate,lang):
msgs=getattr(translate,lang)
break
return msgs
Usage:
msgs = get_messages(["it","ru","en"])
hi = msgs['hello_message'] % 'yourname'
I also defined a helper function which extracts a list of languages from Accept-Language header.
It's not the most flexible solution, but it doesn't have any external dependencies and works for me (in a toy project). I think translate.py may be generated automatically from gettext files.
In case you want to see more, my actual source is here.
You can use the Django internationalisation tool, like explained here.
They are also saying that there is no easy way to do this.
I hope that helps you :)