0

I've written a small utility in Python3 to help me copy my music collection from my NAS to a mobile device. The usefulness of this is that it will auto-convert flac files to ogg-vorbis (to save space) and also exclude some files based on their audio tags (i.e. artist, album, date, etc).

I'm not happy with the limited nature of the exclude feature and I want to improve it but I've hit a mental block and I'm looking for advice on how to proceed.

I would like the user to write an exclude file which will look something like this:

exclude {
    artist is "U2"

    artist is "Uriah Heep" {
        album is "Spellbinder"
        album is "Innocent Victim"
    }
}

This would translate to:

exclude if 
(artist = "U2") OR 
(artist = "Uriah Heep" AND (album = "Spellbinder" OR album = "Innocent Victim"))

There will be more conditionals such as sub-string matching and date ranges.

I've been checking out PLY but I'm struggling with the concepts of how to parse this type of nested structure and also how to represent the resulting conditional so that I can execute it in code when applying the exclude filter during the copy operation.

4
  • I don't know about the parsing, but representing the conditional in code could be something like lambda song: song.artist != 'U2' used with e.g. filter.
    – jonrsharpe
    Nov 12, 2015 at 15:19
  • please don't cross-post: stackoverflow.com/questions/33656929/…
    – gnat
    Nov 12, 2015 at 20:35
  • First comment against the SO question: General questions like this should go on programmers.sx. - so that's what I did. Also did you miss my comment about deleting the SO comment? I AM trying to do the right thing here but contradictory comments don't help. Sorry for the squashed up reply but Chrome browser doesn't let me put in carriage returns. Nov 13, 2015 at 12:35
  • Do you really need PLY to parse something like this? Seems like a simple hand-written parser would suffice. Nov 16, 2015 at 7:26

1 Answer 1

2

Language agnostic answer (I don't know python):

Since your structure is a tree, you need to store it as a tree. You need to know whether your tree is going to be only two levels or more, this may affect the way to handle it (recursively or brute force).

If you don't need to optimise for speed you can start with a naive algorithm.
Match your tree with a handling algorithm.
Start from the top level.
1/ Loop for each branch until you find a match (exclusion), if there is no match return to caller with no exclusion.
2/ If the node has no branch at the next level return to caller with an exclusion.
3/ If the node has branches or leaves at the next level, go down one level and start from action 1.

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.