我正在尝试从几千个库的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='tarek@ziade.org',
maintainer='Ian Cordasco',
maintainer_email='graffatcolmingov@gmail.com',
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',
],
)
如何设置此语法以忽略除我指定的两个值之外的所有内容?
答案 0 :(得分:0)
在查看了您之前的问题以及解析简化输入的简易性之后,我认为最快的方法是对这些文件进行简单的文本预处理,丢弃您不需要的所有内容并保留你感兴趣的两个部分。 例如:
setup_requires
的起始位置。]
的位置,这将表示setup_requires
数组定义的结束。setup_requires=['pytest-runner']
写入输出文件。tests_require
的类似方法,附加到输出文件。