<p>I have a bunch of files (TV episodes, although that is fairly arbitrary) that I want to check match a specific naming/organisation scheme..</p>

<p>Currently: I have three arrays of regex, one for valid filenames, one for files missing an episode name, and one for valid paths.</p>

<p>Then, I loop though each valid-filename regex, if it matches, append it to a "valid" dict, if not, do the same with the missing-ep-name regexs, if it matches this I append it to an "invalid" dict with an error code (2:'missing epsiode name'), if it matches neither, it gets added to invalid with the 'malformed name' error code.</p>

<p>The current code can be found <a href="http://github.com/dbr/checktveps/tree/8a6dc68ad61e684c8d8f0ca1dc37a22d1c51aa82/2checkTvEps.py" rel="nofollow noreferrer">here</a></p>

<p>I want to add a rule that checks for the presence of a folder.jpg file in each directory, but to add this would make the code substantially more messy in it's current state.. </p>

<p>How could I write this system in a more expandable way?</p>

<p>The rules it needs to check would be..</p>

<ul>
<li>File is in the format <code>Show Name - [01x23] - Episode Name.avi</code> or <code>Show Name - [01xSpecial02] - Special Name.avi</code> or <code>Show Name - [01xExtra01] - Extra Name.avi</code></li>
<li>If filename is in the format <code>Show Name - [01x23].avi</code> display it a 'missing episode name' section of the output</li>
<li>The path should be in the format <code>Show Name/season 2/the_file.avi</code> (where season 2 should be the correct season number in the filename)</li>
<li>each <code>Show Name/season 1/</code> folder should contain "folder.jpg"</li>
</ul>

<p>.any ideas? While I'm trying to check TV episodes, this concept/code should be able to apply to many things..</p>

<p>The only thought I had was a list of dicts in the format:</p>

<pre><code>checker = [
{
    'name':'valid files',
    'type':'file',
    'function':check_valid(), # runs check_valid() on all files
    'status':0 # if it returns True, this is the status the file gets
}
</code></pre>

[{'answer_id': 19389, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 0, 'selected': False, 'text': '<p>maybe you should take the approach of defaulting to: "the filename is correct" and work from there to disprove that statement:</p>\n\n<p>with the fact that you only allow filenames with: \'show name\', \'season number x episode number\' and \'episode name\', you know for certain that these items should be separated by a "-" (dash) so you have to have 2 of those for a filename to be correct.<br>\nif that checks out, you can use your code to check that the show name matches the show name as seen in the parent\'s parent folder (case insensitive i assume), the season number matches the parents folder numeric value (with or without an extra 0 prepended).</p>\n\n<p>if however you don\'t see the correct amount of dashes you instantly know that there is something wrong and stop before the rest of the tests etc.</p>\n\n<p>and separately you can check if the file <code>folder.jpg</code> exists and take the necessary actions. <strong>or</strong> do that first and filter that file from the rest of the files in that folder.</p>\n'}, {'answer_id': 21302, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 3, 'selected': True, 'text': '<blockquote>\n  <p>I want to add a rule that checks for\n  the presence of a folder.jpg file in\n  each directory, but to add this would\n  make the code substantially more messy\n  in it\'s current state..</p>\n</blockquote>\n\n<p>This doesn\'t look bad.  In fact your current code does it very nicely, and Sven mentioned a good way to do it as well:</p>\n\n<ol>\n<li>Get a list of all the files</li>\n<li>Check for "required" files</li>\n</ol>\n\n<p>You would just have have add to your dictionary a list of required files:</p>\n\n<pre><code>checker = {\n  ...\n  \'required\': [\'file\', \'list\', \'for_required\']\n}\n</code></pre>\n\n<p>As far as there being a better/extensible way to do this?  I am not exactly sure.  I could only really think of a way to possibly drop the "multiple" regular expressions and build off of Sven\'s idea for using a delimiter.  So my strategy would be defining a dictionary as follows (and I\'m sorry I don\'t know Python syntax and I\'m a tad to lazy to look it up but it should make sense.  The /regex/ is shorthand for a regex):</p>\n\n<pre><code>check_dict = {\n  \'delim\'    : /\\-/,\n  \'parts\'    : [ \'Show Name\', \'Episode Name\', \'Episode Number\' ],\n  \'patterns\' : [/valid name/, /valid episode name/, /valid number/ ],\n  \'required\' : [\'list\', \'of\', \'files\'],\n  \'ignored\'  : [\'.*\', \'hidden.txt\'],\n  \'start_dir\': \'/path/to/dir/to/test/\'\n}\n</code></pre>\n\n<ol>\n<li>Split the filename based on the delimiter.</li>\n<li>Check each of the parts.</li>\n</ol>\n\n<p>Because its an ordered list you can determine what parts are missing and if a section doesn\'t match any pattern it is malformed.  Here the <code>parts</code> and <code>patterns</code> have a 1 to 1 ratio.  Two arrays instead of a dictionary enforces the order.</p>\n\n<p>Ignored and required files can be listed.  The <code>.</code> and <code>..</code> files should probably be ignored automatically.  The user should be allowed to input "globs" which can be shell expanded.  I\'m thinking here of <code>svn:ignore</code> properties, but globbing is natural for listing files.</p>\n\n<p>Here <code>start_dir</code> would be default to the current directory but if you wanted a single file to run automated testing of a bunch of directories this would be useful.</p>\n\n<p>The real loose end here is the path template and along the same lines what path is required for "valid files".  I really couldn\'t come up with a solid idea without writing one large regular expression and taking groups from it... to build a template.  It felt a lot like writing a TextMate language grammar.  But that starts to stray on the ease of use.  The real problem was that the path template was not composed of <code>parts</code>, which makes sense but adds complexity.</p>\n\n<p>Is this strategy in tune with what you were thinking of?</p>\n'}]
['https://Stackoverflow.com/questions/19030', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/745/']