json.loads("{1: 2} # comments") raises a ValueError (it does not understand the comment. Striping manually comments is tricky, # is allowed in URLs. You would have to parse the JSON to strip the comment and then call json.loads()...
I don't want to use YAML because there is no builtin parser.
eval() is usually seen as evil, so I propose to use a simple Python parser: ast.literal_eval(). It supports simple types (int, float, tuple, list, dict, etc.), but not statements (ex: "import os" or "raise ValueError" raise a SyntaxError).
http://docs.python.org/dev/library/ast.html#ast.literal_evalIn my opinion, the most classic format is a dictionary key => value. Examples:
- pathlib # old format
- {'name': 'pathlib >= 0.9'} # new format
- {'name': 'futures', 'markers': "python_version < '2.7'"}
- {'name': 'INITools==2.0', 'global-options': ['--help'], 'install-options': ['--prefix', '/opt']}
The new format would only be used if the line starts with "{", otherwise the current format will be used.
I don't know the whole requirement format, I don't know how SCM urls should be declared.
I don't think that the version should be moved to a new field (ex: {'name': 'pathlib', 'version': '>=0.9'}, the current format is more convinient.
I suppose that unknown keys should raise an error, it would allow to detect typo in keys (ex: {'name': 'futures', 'marker': "python_version < '2.7'"}).
For global-options and install-options, I don't know if space separated options should be allowed as a string (and then parsed by shlex or something else before being passed to subprocess):
- {'name': 'INITools==2.0', 'global-options': '--help', 'install-options': '--prefix /opt'}
It's maybe safer (but less convinient) to require a list.
I will try to implement a new parser using ast.literal_eval().
Each option (name, global-options, markers, ...) will have to check its input type (ex: name should only accept str, global-options requires a list of strings, etc.).
Note: strings can be quoted using ' and " in Python, which is convinient for markers.
Victor