-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option for Issue #377 Allows usage as: ``` import MDAnalysis as mda mda.ten2eleven('myscript.py') ```
- Loading branch information
1 parent
e044ddd
commit ae4a00e
Showing
15 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import sys | ||
from lib2to3.main import main | ||
from . import ten2elevenfixes | ||
|
||
def ten2eleven(*files): | ||
"""Convert MDAnalysis 0.10.0 scripts to 0.11.0 | ||
Usage: | ||
ten2eleven('myscript.py', 'myotherscript.py') | ||
""" | ||
print files | ||
|
||
main('MDAnalysis.ten2elevenfixes', ['-w'] + list(files)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
''' | ||
run with: python ten2eleven.py -f agcountmethods test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
|
||
|
||
class FixAgcountmethods(BaseFix): | ||
|
||
PATTERN = """ | ||
power< head =any+ | ||
trailer< dot = '.' method=('numberOfAtoms'|'numberOfResidues'|'numberOfSegments')> | ||
parens=trailer< '(' ')' > | ||
tail=any*> | ||
""" | ||
|
||
def transform(self, node, results): | ||
head = results['head'] | ||
method = results['method'][0] | ||
tail = results['tail'] | ||
syms = self.syms | ||
method_name = method.value | ||
head = [n.clone() for n in head] | ||
tail = [n.clone() for n in tail] | ||
replacement_dict = {'numberOfAtoms': 'n_atoms', 'numberOfResidues': 'n_residues', 'numberOfSegments':'n_segments'} | ||
method_name = replacement_dict[method_name] | ||
args = head + [pytree.Node(syms.trailer, [Dot(), Name(method_name, prefix = method.prefix)])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
''' | ||
run with: python ten2eleven.py -f agmethods test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
|
||
|
||
class FixAgmethods(BaseFix): | ||
|
||
PATTERN = """ | ||
power< head =any+ | ||
trailer< dot = '.' method=('residues'|'charges'|'indices'| | ||
'masses'|'names'|'types'| | ||
'radii'|'resids'|'resnames'| | ||
'resnums'|'segids')> | ||
parens=trailer< '(' ')' > | ||
tail=any*> | ||
""" | ||
|
||
def transform(self, node, results): | ||
head = results['head'] | ||
method = results['method'][0] | ||
tail = results['tail'] | ||
syms = self.syms | ||
method_name = method.value | ||
head = [n.clone() for n in head] | ||
tail = [n.clone() for n in tail] | ||
args = head + [pytree.Node(syms.trailer, [Dot(), Name(method_name, prefix = method.prefix)])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
''' | ||
run with: python ten2eleven.py -f agmethods2 test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
|
||
|
||
class FixAgmethods2(BaseFix): | ||
|
||
PATTERN = """ | ||
power< head =any+ | ||
trailer< dot = '.' method=('bond'|'angle'|'torsion'| | ||
'improper')> | ||
parens=trailer< '(' ')' > | ||
tail=any*> | ||
""" | ||
|
||
def transform(self, node, results): | ||
head = results['head'] | ||
method = results['method'][0] | ||
tail = results['tail'] | ||
syms = self.syms | ||
method_name = method.value | ||
if method_name == 'torsion': | ||
method_name = 'dihedral' | ||
head = [n.clone() for n in head] | ||
tail = [n.clone() for n in tail] | ||
args = head + [pytree.Node(syms.trailer, [Dot(), Name(method_name, prefix = method.prefix), Dot(), Name('value'), LParen(), RParen()])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
39 changes: 39 additions & 0 deletions
39
package/MDAnalysis/ten2elevenfixes/fix_agsetterpluralization.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
''' | ||
run with: python ten2eleven.py -f agsetterpluralization test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
|
||
|
||
class FixAgsetterpluralization(BaseFix): | ||
|
||
PATTERN = """ | ||
power< head =any+ | ||
trailer< dot = '.' method=('set_mass'|'set_charge'|'set_name'| | ||
'set_type'|'set_radius'|'set_bfactor'| | ||
'set_altloc'|'set_serial'|'set_resid'| | ||
'set_resname'|'set_resnum'|'set_segid')> | ||
tail=any*> | ||
""" | ||
|
||
def transform(self, node, results): | ||
head = results['head'] | ||
method = results['method'][0] | ||
tail = results['tail'] | ||
syms = self.syms | ||
method_name = method.value | ||
head = [n.clone() for n in head] | ||
tail = [n.clone() for n in tail] | ||
if method_name == 'set_radius': #different plural conversion | ||
method_name = 'set_radii' | ||
elif method_name == 'set_mass': #another different plural form | ||
method_name = 'set_masses' | ||
else: | ||
method_name += 's' #standard plural conversion for all others | ||
args = head + [pytree.Node(syms.trailer, [Dot(), Name(method_name, prefix = method.prefix)])] + tail | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
''' | ||
run with: python ten2eleven.py -f calctorsions test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
|
||
|
||
class FixCalctorsions(BaseFix): | ||
|
||
PATTERN = """ | ||
trailer< '.' method='calc_torsions' > | ||
| | ||
import_from< 'from' dotted_name< 'MDAnalysis' '.' 'lib' '.' 'distances' > 'import' import_name='calc_torsions' > | ||
""" | ||
|
||
def transform(self, node, results): | ||
if 'method' in results.keys(): | ||
method = results['method'] | ||
syms = self.syms | ||
method_name = method.value | ||
replacement_dict = {'calc_torsions': 'calc_dihedrals'} | ||
method_name = replacement_dict[method_name] | ||
args = [pytree.Node(syms.trailer, [Dot(), Name(method_name, prefix = method.prefix)])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
elif 'import_name' in results.keys(): | ||
import_name = results['import_name'] | ||
import_name.replace(Name(' calc_dihedrals')) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
''' | ||
run with: python ten2eleven.py -f camelcase test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Call, LParen, RParen, ArgList, Dot | ||
from lib2to3 import pytree | ||
import re | ||
|
||
|
||
class FixCamelcase(BaseFix): | ||
|
||
PATTERN = """ | ||
trailer< dot = '.' method=('totalMass'|'totalCharge'|'centerOfGeometry'| | ||
'radiusOfGyration'|'shapeParameter'|'momentOfInertia'| | ||
'principalAxes'|'packIntoBox'|'asUniverse'| | ||
'applyPBC')> | ||
""" | ||
|
||
def transform(self, node, results): | ||
method = results['method'][0] | ||
method_name = method.value | ||
#with the exception of applyPBC, all camelcase changes here involve replacing a capital letter with an underscore and the lower case version of that letter | ||
if not method_name == 'applyPBC': | ||
method_name = re.sub(r'([A-Z]{1})', r'_\1', method_name).lower() | ||
else: | ||
method_name = 'apply_PBC' | ||
|
||
syms = self.syms | ||
args = [pytree.Node(syms.trailer, [Dot(), Name(method_name)])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
''' | ||
run with: python ten2eleven.py -f mdaimports test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name | ||
|
||
class FixMdaimports(BaseFix): | ||
|
||
PATTERN = """ | ||
import_name< start_statement='import' dotted_name< import_1 = 'MDAnalysis' dot = '.' second_statement='KDTree' > > | | ||
import_name< start_statement='import' dotted_name< import_1 = 'MDAnalysis' dot = '.' second_statement='core' '.' core_module={core_modules} > > | | ||
import_from< start_statement='from' import_1 = 'MDAnalysis' 'import' second_statement='KDTree' > | | ||
import_from< start_statement='from' dotted_name< import_1 = 'MDAnalysis' dot = '.' second_statement='core' > 'import' core_module={core_modules}> | ||
""".format(core_modules = "('transformations'|'util'|'log'|'units'|'distances'|'parallel')") | ||
|
||
def transform(self, node, results): | ||
start_statement = results['start_statement'] | ||
second_statement = results['second_statement'] | ||
import_1 = results['import_1'] | ||
|
||
if second_statement.value == 'core': | ||
core_module = results['core_module'] | ||
dot = results['dot'] | ||
if core_module[0].value == 'units': #not placed in lib | ||
second_statement.replace(Name('', prefix = second_statement.prefix)) | ||
dot.replace(Name('', prefix = second_statement.prefix)) | ||
else: | ||
second_statement.replace(Name('lib', prefix = second_statement.prefix)) | ||
elif second_statement.value == 'KDTree' and start_statement.value == 'import': | ||
second_statement.replace(Name('lib.KDTree', prefix = second_statement.prefix)) | ||
elif second_statement.value == 'KDTree' and start_statement.value == 'from': | ||
import_1.replace(Name(' MDAnalysis.lib')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
''' | ||
run with: python ten2eleven.py -f numframes test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Dot | ||
from lib2to3 import pytree | ||
|
||
class FixNumframes(BaseFix): | ||
|
||
PATTERN = """ | ||
trailer< dot = '.' method='numframes'> | ||
""" | ||
|
||
def transform(self, node, results): | ||
method = results['method'] | ||
method_name = method.value | ||
if method_name == 'numframes': | ||
method_name = 'n_frames' | ||
syms = self.syms | ||
args = [pytree.Node(syms.trailer, [Dot(), Name(method_name)])] | ||
new = pytree.Node(syms.power, args) | ||
return new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
''' | ||
run with: python ten2eleven.py -f selectatoms test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.pgen2 import token | ||
|
||
|
||
class FixSelectatoms(BaseFix): | ||
|
||
_accept_type = token.NAME | ||
|
||
def match(self, node): | ||
if node.value == 'selectAtoms': | ||
return True | ||
return False | ||
|
||
def transform(self, node, results): | ||
node.value = 'select_atoms' | ||
node.changed() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
''' | ||
run with: python ten2eleven.py -f torsionclasses test_dummy_old_MDA_code.py | ||
Author: Tyler Reddy (but effectively a hack of lib2to3/fixes/fix_metaclass.py) | ||
''' | ||
|
||
from lib2to3.fixer_base import BaseFix | ||
from lib2to3.fixer_util import Name, Dot, syms, Node, Leaf | ||
from lib2to3 import pytree | ||
from lib2to3.fixes import fix_metaclass | ||
from lib2to3.fixes.fix_metaclass import has_metaclass, fixup_parse_tree, find_metas, fixup_indent | ||
from lib2to3.pygram import token | ||
|
||
|
||
class FixTorsionclasses(fix_metaclass.FixMetaclass): | ||
def transform(self, node, results): | ||
fixup_parse_tree(node) | ||
|
||
text_type = node.children[0].type # always Leaf(nnn, 'class') | ||
|
||
# figure out what kind of classdef we have | ||
if len(node.children) == 7: | ||
# Node(classdef, ['class', 'name', '(', arglist, ')', ':', suite]) | ||
# 0 1 2 3 4 5 6 | ||
if node.children[3].type == syms.arglist: | ||
arglist = node.children[3] | ||
# Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite]) | ||
else: | ||
parent = node.children[3].clone() | ||
arglist = Node(syms.arglist, [parent]) | ||
node.set_child(3, arglist) | ||
elif len(node.children) == 6: | ||
# Node(classdef, ['class', 'name', '(', ')', ':', suite]) | ||
# 0 1 2 3 4 5 | ||
arglist = Node(syms.arglist, []) | ||
node.insert_child(3, arglist) | ||
elif len(node.children) == 4: | ||
# Node(classdef, ['class', 'name', ':', suite]) | ||
# 0 1 2 3 | ||
arglist = Node(syms.arglist, []) | ||
node.insert_child(2, Leaf(token.RPAR, u')')) | ||
node.insert_child(2, arglist) | ||
node.insert_child(2, Leaf(token.LPAR, u'(')) | ||
else: | ||
raise ValueError("Unexpected class definition") | ||
|
||
for child in arglist.children: | ||
if child.value == 'Torsion': | ||
child.value = 'Dihedral' | ||
if child.value == 'Improper_Torsion': | ||
child.value = 'ImproperDihedral' | ||
|
||
|
||
|
Oops, something went wrong.