我试图从几千个库的setup.py中提取setup_requires
和test_requires
值。我有这个语法忽略除了我创建解析器的位
grammar SetupPy ;
file_input: (ignore setupRequires | ignore testRequires)* EOF;
setupRequires : SETUPDEC '[' dependencyValue* (',' dependencyValue)* ']';
testRequires : TESTDEC '[' dependencyValue* (',' dependencyValue)* ']';
ignore: UNKNOWN_CHAR;
dependencyValue: LISTVAL;
//ignore : UNKNOWN_CHAR? ;
LISTVAL: SHORT_STRING;
SETUPDEC: 'setup_requires' '=';
TESTDEC: 'tests_require' '=';
UNKNOWN_CHAR: . -> channel(HIDDEN);
fragment SHORT_STRING: '\'' (STRING_ESCAPE_SEQ | ~[\\\r\n\f'])* '\''
| '"' (STRING_ESCAPE_SEQ | ~[\\\r\n\f"])* '"';
fragment STRING_ESCAPE_SEQ
: '\\' .
| '\\'
;
它是一个非常简单的例子。但是,当我把它放在一个完整的文件中时,令牌就会被绊倒在文件中的其他东西上。
# -*- coding: utf-8 -*-
from __future__ import with_statement
from setuptools import setup
def get_version(fname='mccabe.py'):
with open(fname) as f:
for line in f:
if line.startswith('__version__'):
return eval(line.split('=')[-1])
def get_long_description():
descr = []
for fname in ('README.rst',):
with open(fname) as f:
descr.append(f.read())
return '\n\n'.join(descr)
setup(
name='mccabe',
version=get_version(),
description="McCabe checker, plugin for flake8",
long_description=get_long_description(),
keywords='flake8 mccabe',
author='Tarek Ziade',
author_email='[email protected]',
maintainer='Ian Cordasco',
maintainer_email='[email protected]',
url='https://github.com/pycqa/mccabe',
license='Expat license',
py_modules=['mccabe'],
zip_safe=False,
setup_requires=['pytest-runner'],
tests_require=['pytest'],
entry_points={
'flake8.extension': [
'C90 = mccabe:McCabeChecker',
],
},
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Quality Assurance',
],
)
如何设置此语法忽略除了指定的两个值之外的所有内容?
我认为你正处在正确的轨道上。我很乐意很快看到它。 – TomServo