PLUTO
pluto_files_IO.py
Go to the documentation of this file.
1 import os
2 import sys
3 
4 class PlutoFiles(object):
5  def __init__(self, fname):
6  self.fname = fname
7 
8  def OpenFile(self):
9  try:
10  self.f = open(self.fname, 'r')
11  except IOError:
12  print "File : %s not found"%self.fname
13 
14  def List2File(self, contents):
15  self.fc = open(self.fname, 'w')
16  # contents is a List.
17  for item in contents:
18  self.fc.write("%s"%item)
19 
20  self.fc.close()
21 
22  def File2List(self):
23  NewList = []
24  self.OpenFile()
25  for line in self.f.readlines():
26  NewList.append(line)
27  return NewList
28 
29  def LocateString(self,string):
30  strlist = []
31  scrh = self.File2List()
32  for item in scrh:
33  if len(string.split()) == 1 and string.split()[0] == string: # Look for 1 word
34  if string in item.split():
35  strlist.append([scrh.index(item), item])
36 
37  # Look for the whole string... Careful with \n matches thats why 1 word match is better
38  elif len(string.split()) > 1:
39  if string == item:
40  strlist.append([scrh.index(item), item])
41 
42  else:
43  print "Invalid String in LocateString"
44  sys.exit()
45 
46  return strlist
47 
48  def DeleteLines(self, lbeg, lend):
49  scrh = self.File2List()
50  del scrh[lbeg:lend+1]
51  self.List2File(scrh)
52 
53  def InsertLine(self, string, lbeg):
54  scrh = self.File2List()
55  scrh.insert(lbeg, string) #Inserts String at position lbeg.
56  self.List2File(scrh)
57 
58  def ReplaceLine(self, NewLine, OldLineIndex):
59  scrh = self.File2List()
60  scrh[OldLineIndex] = NewLine
61  self.List2File(scrh)
62 
63  def ReplaceWord(self, oldword, newword, DelOld = False):
64  scrh = self.LocateString(oldword)
65  try:
66  scrh[0]
67  except IndexError:
68  #print 'Word %s not found in file %s'%(oldword, self.fname)
69  pass
70  else:
71  l = scrh[0][1].split(' ')
72  l[l.index(oldword)] = newword
73  newline = ' '.join(l)
74  if DelOld:
75  self.DeleteLines(scrh[0][0], scrh[0][0])
76  self.InsertLine(newword, scrh[0][0])
77  else:
78  self.ReplaceLine(newline,scrh[0][0])
79 
80  def ReadLines(self, beg, end):
81  scrh = self.File2List()
82  return scrh[beg:end+1]
83 
84  def UpdateListFromFile(self, scrhlist, updatelist):
85  self.OpenFile()
86  for line in self.f.readlines():
87  for e in scrhlist:
88  if e in line.split():
89  updatelist[scrhlist.index(e)] = line.split()[-1]
90  else:
91  continue
92 
93 
94  def ReplaceObsoletes(self):
95  oldwords = ['INCLUDE_ROTATION', 'INCLUDE_BODY_FORCE', 'INCLUDE_COOLING', 'INCLUDE_BACKGROUND_FIELD',
96  'TIME_EVOLUTION', 'RAYMOND', 'NEQ', 'CT_VEC_POT_INIT', 'USE_VECTOR_POTENTIAL',
97  'SAVE_VEC_POT']
98  newwords = ['ROTATING_FRAME', 'BODY_FORCE', 'COOLING','BACKGROUND_FIELD', 'TIME_STEPPING', 'SNEq',
99  'MINEq', 'USE_VECTOR_POTENTIAL', 'ASSIGN_VECTOR_POTENTIAL', 'UPDATE_VECTOR_POTENTIAL']
100 
101  for i in range(len(oldwords)):
102  self.ReplaceWord(oldwords[i],newwords[i])
103 
def LocateString(self, string)
def ReplaceLine(self, NewLine, OldLineIndex)
def ReadLines(self, beg, end)
def List2File(self, contents)
def InsertLine(self, string, lbeg)
def DeleteLines(self, lbeg, lend)
def UpdateListFromFile(self, scrhlist, updatelist)
def __init__(self, fname)