alcarney/Module-Planner — pull request #15 by drvinceknight (2014-12-14 11:10:24+00:00)
Title: Data specification written

So I've written a script that checks the data specification of a file against a yml file that contains the regex for everything we want.

A couple of things:

1. This is currently not in `management.py`. I know @alcarney wanted it in there but I took a look and didn't want to break anything as there are still no tests (I know @alcarney isn't writing those but someone needs to, maybe me if I find time).
2. I've used [docopt](https://github.com/docopt/docopt) to parse two arguments (the md file and the yml file). It's a pretty nice library we could perhaps use it throughout. My `check_specifications.py` script (in `bin/`) is run like this:

$ python check_specifications.py module files_for_tests/module_data_file_test.md specification data_specification.yml
True

Or indeed if a file doesn't obey the specs it tells you where/why:

$ python check_specifications.py module files_for_tests/wrong_code_module_data_file_test.md specification data_specification.yml
Traceback (most recent call last):
File "check_specifications.py", line 99, in
print check_module_obeys_specifications(arguments[''], arguments[''])
File "check_specifications.py", line 92, in check_module_obeys_specifications
raise ValueError("Variable '%s' in '%s' does not match specification" % (variable, module_file))
ValueError: Variable 'code' in 'files_for_tests/wrong_code_module_data_file_test.md' does not match specification

I haven't actually set up default variables etc but that can all be done in docopt.

This means that we can either leave my `check_specifications.py` out of `management.py` and just use `call` to run it or indeed we can just grab everything and throw it in to `management.py` (I prefer the first suggestion).

3. The `specification.yml` file just lists variables and corresponding regex. This means that it's not terribly readable (because of regex) but I think it's the best way to do this: only way to make the checking generic (no need to differentiate whether or not things are lists etc...)
4. I have written doctests for everything I've written. To check they pass simply run:

python -m doctest check_specifications.py

See the next point as to why there is a single test that fails (if you have more then perhaps you're missing libraries: see the next next point).

5. One of the regex's is not quite correct. The regex for a list of module codes I'm using is:

\[('MA[0-9]{4}',+(,'MA[0-9]{4}')*)|'None'\]

This works fine as you can see by the tests although there's a slight error. It _correctly_ doesn't match:

['Ice Cream', 'MA0110']

but for some reason it _does_ match:

['MA0110', 'Ice Cream']

I have written a test file for this as well and it fails.

6. This requires `docopt`and `re` (the first is available through pip).