Statistiques
| Révision :

root / tools / ASE2ase.py @ 20

Historique | Voir | Annoter | Télécharger (2,78 ko)

1
#!/usr/bin/env python
2
import sys
3

    
4
def convert(filename):
5
    lines = open(filename).readlines()
6
    t1 = ''.join(lines)
7

    
8
    first = True
9
    for i in range(len(lines)):
10
        line = lines[i]
11
        if line.startswith('from ASE'):
12
            if first:
13
                lines[i] = 'from ase.all import *\n'
14
                first = False
15
            else:
16
                lines[i] = ''
17

    
18
    t = ''.join(lines)
19

    
20
    for old, new in [('GetCartesianPositions', 'get_positions'),
21
                     ('SetCartesianPositions', 'set_positions'),
22
                     ('GetPotentialEnergy', 'get_potential_energy'),
23
                     ('SetCalculator', 'set_calculator'),
24
                     ('GetScaledPositions', 'get_scaled_positions'),
25
                     ('SetScaledPositions', 'set_scaled_positions'),
26
                     ('SetUnitCell', 'set_cell'),
27
                     ('GetUnitCell', 'get_cell'),
28
                     ('GetBoundaryConditions', 'get_pbc'),
29
                     ('GetCartesianForces', 'get_forces'),
30
                     ('GetCartesianVelocities', 'get_velocities'),
31
                     ('SetCartesianVelocities', 'set_velocities'),
32
                     ('GetCartesianMomenta', 'get_momenta'),
33
                     ('SetCartesianMomenta', 'set_momenta'),
34
                     ('ListOfAtoms', 'Atoms'),
35
                     ('periodic', 'pbc'),
36
                     ('pbcity', 'periodicity'),
37
                     ('.Converge(', '.run('),
38
                     ('Repeat', 'repeat'),
39
                     ('Numeric', 'numpy'),
40
                     ('numpyal', 'Numerical'),
41
                     ('GetAtomicNumber()', 'number'),
42
                     ('GetChemicalSymbol()', 'symbol'),
43
                     ('GetCartesianPosition()', 'position'),
44
                     ('GetTag()', 'tag'),
45
                     ('GetCharge()', 'charge'),
46
                     ('GetMass()', 'mass'),
47
                     ('GetCartesianMomentum()', 'momentum'),
48
                     ('GetMagneticMoment()', 'magmom'),
49
                     ]:
50
        t = t.replace(old, new)
51

    
52
    t2 = ''
53
    while 1:
54
        i = t.find('.')
55
        i2 = t.find('def ')
56
        if 0 <= i < i2:
57
            n = 1
58
        elif i2 != -1:
59
            n = 4
60
            i = i2
61
        else:
62
            break
63
        t2 += t[:i + n]
64
        t = t[i + n:]
65
        if t[0].isupper() and t[1].islower():
66
            j = t.find('(')
67
            if j != -1 and t[2: j].isalpha():
68
                for k in range(j):
69
                    if t[k].isupper() and k > 0:
70
                        t2 += '_'
71
                    t2 += t[k].lower()
72
                t = t[j:]
73

    
74
    t2 += t
75

    
76
    if t2 != t1:
77
        print filename, len(t1) - len(t2)
78
        open(filename + '.bak', 'w').write(t1)
79
        open(filename, 'w').write(t2)
80

    
81
for filename in sys.argv[1:]:
82
    convert(filename)