PSCF v1.1
command.py
1"""! Module for processing PSCF command scripts. """
2
3from pscfpp.text import *
4
5
44class Script:
45
46
58 def __init__(self, filename = None):
59 self._commands = []
60 self._indices = {}
61 if (filename != None):
62 self.read(filename)
63
64
69 def read(self, filename):
70
71 # Determine type of parameter in
72 if isinstance(filename, str):
73 file = open(filename, 'r')
74 elif isinstance(filename, file):
75 file = filename
76 else:
77 raise Exception("Invalid parameter in")
78
79 # Split file into list of line strings
80 lines = file.readlines();
81
82 # Process lines
83 i = 0
84 finish = False
85 while i < len(lines) and not finish:
86 line = lines[i]
87 if not isinstance(line, str):
88 raise Exception('Command line object is not a string')
89 command = Command(line)
90
91 # Add command to list self._commands
92 self._commands.append(command)
93
94 # Add index i to dictionary self._indices
95 label = command.label
96 if label in self._indices:
97 index = self._indices[label]
98 if isinstance(index, int):
99 self._indices[label] = [index]
100 elif not instance(index, list):
101 raise Exception('Values of _indices must be int or list')
102 self._indices[label].append(i)
103 else:
104 self._indices[label] = i
105
106 if (command.label == 'FINISH'):
107 finish = True
108 i += 1
109
110
113 def __str__(self):
114 list = []
115 for command in self._commands:
116 list.append(str(command))
117 return '\n'.join(list)
118
119
124 def __getitem__(self, key):
125 if isinstance(key, int):
126 return self._commands[key]
127 elif isinstance(key, str):
128 if key in self._indices:
129 index = self._indices[key]
130 if isinstance(index, int):
131 return self._commands[index]
132 elif isinstance(index, list):
133 clist = []
134 for j in index:
135 clist.append(self._commands[j])
136 return clist
137 else:
138 raise Exception("Key must an int or a command string")
139
140
143 def __len__(self):
144 return len(self._commands)
145
146
154
155
160 def __init__(self, line):
161 Record.__init__(self, line)
162 self.label = self.fields[0]
163
164
167 def nParam(self):
168 return (len(self.fields) - 1)
169
170
175 def param(self, i = 0):
176 return self.fields[i+1]
177
178
184 def setParam(self, value, i = 0):
185 self.fields[i+1] = str(value)
186
187
A single command, with a label and zero or more parameters.
Definition: command.py:153
def setParam(self, value, i=0)
Set the value of a specific command parameter.
Definition: command.py:184
def __init__(self, line)
Constructor.
Definition: command.py:160
def nParam(self)
Number of parameter / arguments of the command.
Definition: command.py:167
Class to parse a PSCF command script.
Definition: command.py:44
def __str__(self)
Return a string containing the command script.
Definition: command.py:113
def __len__(self)
Get the number of commands in the script.
Definition: command.py:143
def __init__(self, filename=None)
Constructor.
Definition: command.py:58
def __getitem__(self, key)
Get a Command by integer index or command name.
Definition: command.py:124
def read(self, filename)
Read and parse a command script.
Definition: command.py:69
A Record represents a string of fields separated by whitespace.
Definition: text.py:90
Utilities for manipulating and searching text strings.
Definition: text.py:1