mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-03 21:44:30 +08:00
Script PythonFormat uses yapf to check the formatting of Python files
This commit is contained in:
@@ -51,5 +51,5 @@ if __name__ == '__main__':
|
||||
for m in suppl:
|
||||
i1, i2, i3 = GetMoments(m, True)
|
||||
mi1, mi2, mi3 = GetMoments(m, False)
|
||||
print("%s %.4f %.4f %.4f %.4f %.4f %.4f" %
|
||||
(m.GetProp("_Name"), i1, i2, i3, mi1, mi2, mi3), file=output)
|
||||
print("%s %.4f %.4f %.4f %.4f %.4f %.4f" % (m.GetProp("_Name"), i1, i2, i3, mi1, mi2, mi3),
|
||||
file=output)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
from rdkit import RDConfig
|
||||
import os, sys
|
||||
from rdkit import Chem
|
||||
@@ -12,11 +12,11 @@ if sys.argv[1].endswith('.sdf'):
|
||||
elif sys.argv[1].endswith('.smi'):
|
||||
suppl = Chem.SmilesMolSupplier(sys.argv[1])
|
||||
else:
|
||||
print 'Need a file ending in .sdf or .smi'
|
||||
print('Need a file ending in .sdf or .smi')
|
||||
exit(1)
|
||||
|
||||
for mol in suppl:
|
||||
print mol.GetProp('_Name')
|
||||
print(mol.GetProp('_Name'))
|
||||
fn = mol.GetProp('_Name') + '.png'
|
||||
AllChem.Compute2DCoords(mol)
|
||||
Draw.MolToFile(mol, fn)
|
||||
|
||||
@@ -3,10 +3,10 @@ the coverage tool mistakenly finds in the build tree.
|
||||
It replaces the paths with the ones from the source tree
|
||||
n.b. if a file with the same name (i.e. sln.yy) is found twice
|
||||
in the source tree, this will break"""
|
||||
|
||||
from __future__ import print_function
|
||||
import os, sys
|
||||
source_dir, info_file = sys.argv[1:3]
|
||||
print source_dir, info_file
|
||||
print(source_dir, info_file)
|
||||
|
||||
paths = {}
|
||||
for root, dir, files in os.walk(source_dir):
|
||||
@@ -20,13 +20,13 @@ for line in lines:
|
||||
if "SF:" in line:
|
||||
fn = line.split("SF:")[-1].strip()
|
||||
if not os.path.exists(fn):
|
||||
print "Does not exist:", fn.strip()
|
||||
print("Does not exist:", fn.strip())
|
||||
head, rest = os.path.split(fn)
|
||||
potential = paths[rest]
|
||||
if len(potential) == 1:
|
||||
line = "SF:" + potential[0]
|
||||
else:
|
||||
asdf
|
||||
raise NotImplementedError('asdf')
|
||||
newlines.append(line)
|
||||
|
||||
open(info_file, 'w').write("\n".join(newlines))
|
||||
|
||||
69
Scripts/PythonFormat.py
Normal file
69
Scripts/PythonFormat.py
Normal file
@@ -0,0 +1,69 @@
|
||||
'''
|
||||
|
||||
Script will test the RDkit python code for conformance with the agreed format using
|
||||
yapf.
|
||||
|
||||
For each Python file that is found in $RDBASE (excluding the build and External
|
||||
directories), yapf is used with the style configuration in $RDBASE/setup.cfg.
|
||||
If a change is required, the difference is printed. At the end of the process,
|
||||
all non-conformant files are listed and the required yapf command(s) printed.
|
||||
|
||||
If changes are found, the script will exit with error code 1, otherwise 0.
|
||||
|
||||
'''
|
||||
from __future__ import print_function
|
||||
import os
|
||||
from yapf.yapflib.yapf_api import FormatCode
|
||||
import sys
|
||||
|
||||
rdbase = os.environ.get('RDBASE', '')
|
||||
styleConfig = os.path.join(rdbase, 'setup.cfg')
|
||||
|
||||
excludeDirs = [os.path.join(rdbase, 'build'),
|
||||
os.path.join(rdbase, 'External'), ]
|
||||
|
||||
|
||||
def pythonFiles(dirname=rdbase):
|
||||
""" Find all python files below directory dirname """
|
||||
for root, _, files in os.walk(dirname):
|
||||
if any(root.startswith(d) for d in excludeDirs):
|
||||
continue
|
||||
for file in files:
|
||||
if file.endswith(".py"):
|
||||
yield os.path.join(root, file)
|
||||
|
||||
|
||||
def yapfChanges(filename):
|
||||
""" Use yapf with the default settings to format file filename """
|
||||
try:
|
||||
with open(filename) as f:
|
||||
codeBefore = f.read()
|
||||
except UnicodeError:
|
||||
with open(filename, encoding='latin-1') as f:
|
||||
codeBefore = f.read()
|
||||
try:
|
||||
changes, changed = FormatCode(codeBefore, style_config=styleConfig, print_diff=True,
|
||||
filename=filename)
|
||||
except Exception:
|
||||
print(filename)
|
||||
raise
|
||||
if changed:
|
||||
print(changes)
|
||||
return changed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
changedFiles = []
|
||||
for s in pythonFiles():
|
||||
if yapfChanges(s):
|
||||
changedFiles.append(s)
|
||||
print()
|
||||
if changedFiles:
|
||||
print('yapf will make changes to the following files:')
|
||||
print('\n'.join(sorted(changedFiles)))
|
||||
print('To apply the required changes to your code use the following command(s)')
|
||||
for s in sorted(set(s.replace(rdbase, '').split(os.sep)[1] for s in changedFiles)):
|
||||
print('yapf --style $RDBASE/setup.cfg --in-place --recursive $RDBASE/{0}'.format(s))
|
||||
sys.exit(1)
|
||||
print('Code complies with the agreed formatting rules.')
|
||||
sys.exit(0)
|
||||
@@ -107,8 +107,8 @@ def RunScript(script, doLongTests, verbose):
|
||||
nTests = len(tests) + len(longTests)
|
||||
del sys.modules[script]
|
||||
if verbose and failed:
|
||||
for exeName,args,extras in failed:
|
||||
print("!!! TEST FAILURE: ",exeName,args,extras,file=sys.stderr)
|
||||
for exeName, args, extras in failed:
|
||||
print("!!! TEST FAILURE: ", exeName, args, extras, file=sys.stderr)
|
||||
return failed, nTests
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user