def __init__(self, iotype, client, rpath, component): ProxyMixin.__init__(self, client, rpath) self._component = component self._path = 'AS-%s.dat' % rpath # Local filename for remote data. desc = client.get(rpath+'.description') metadata = {} if iotype == 'out': metadata['path'] = self._path File.__init__(self, default_value=None, iotype=iotype, desc=desc, **metadata)
class Model(Assembly): """ Run multiple `Unique` component instances. """ infile = File(iotype='in', local_path='input') outfile = File(FileRef('output'), iotype='out') def configure(self): self.add('a', Unique()) self.add('b', Unique()) self.driver.workflow.add(['a', 'b']) self.connect('infile', 'a.infile') self.connect('a.outfile', 'b.infile') self.connect('b.outfile', 'outfile')
class Subcontainer(Container): """ Just a subcontainer for Source. """ binary_data = Array(dtype='d', iotype='in') binary_file = File(path=os.path.join('..', 'sub', 'source.bin'), iotype='out', binary=True)
class ReadFile(Component): file_in = File(None, iotype='in', allow_none=True) x = Float(iotype='out') y = Float(iotype='out') def execute(self): with open(self.file_in.abspath(), 'r') as f: self.x = float(f.readline()) self.y = float(f.readline())
class WriteFile(Component): x = Float(1.0, iotype='in') y = Float(2.0, iotype='in') file_out = File('x.in', iotype='out') def execute(self): with open(self.file_out.abspath(), 'w') as f: f.write('{:7.3f}\n'.format(self.x)) f.write('{:7.3f}\n'.format(self.y))
class Sleeper(ExternalCode): """ Used to test external code functionality. """ delay = Int(1, units='s', iotype='in') env_filename = Str(iotype='in') infile = File(iotype='in', local_path='input') outfile = File(iotype='out', path='output') def __init__(self): super(Sleeper, self).__init__(directory=DIRECTORY) self.external_files = [ FileMetadata(path='sleep.py', input=True, constant=True), ] def execute(self): """ Runs code and sets `outfile`. """ self.command = ['python', 'sleep.py', str(self.delay)] if self.env_filename: self.command.append(self.env_filename) super(Sleeper, self).execute()
class Sink(Component): """ Sink data from test component. """ z = Float(iotype='in') tof = Float(iotype='in') sof = Float(iotype='in') input = File(iotype='in') data = Str(iotype='out') def execute(self): with self.input.open() as inp: self.data = inp.read()
class Sink(Component): """ Consumes files. """ text_data = Str(iotype='out') binary_data = Array(dtype='d', iotype='out') text_file = File(iotype='in') binary_file = File(iotype='in') executions = Int(0, iotype='in', desc='Count of Oddball instance_method() calls.') def __init__(self, *args, **kwargs): super(Sink, self).__init__(*args, **kwargs) global SINK_INIT SINK_INIT = True def execute(self): """ Read test data from files. """ with self.text_file.open() as inp: self.text_data = inp.read() with self.binary_file.open() as inp: self.binary_data = cPickle.load(inp)
class Source(Component): """ Source data for test component. """ x = Float(iotype='out') y = Float(iotype='out') tof = Float(iotype='out') sof = Float(iotype='out') output = File(path='output', iotype='out') def execute(self): self.x = 6 self.y = 7 self.tof = 2.781828 self.sof = 3.14159 with open(self.output.path, 'w') as out: out.write('Hello world!')
class Source(Assembly): """ Produces files. A fair amount of stuff happens in Component.save_to_egg() in relation to handling external files and file variables. """ write_files = Bool(True, iotype='in') text_data = Str(iotype='in') text_file = File(path='source.txt', iotype='out') def __init__(self, *args, **kwargs): super(Source, self).__init__(*args, **kwargs) global SOURCE_INIT SOURCE_INIT = True def configure(self): """ Called once we have a valid hierarchy above us. """ self.add('sub', Subcontainer()) self.create_passthrough('sub.binary_file') # Some custom objects that must be restored. self.obj_list = [DataObj(i) for i in range(3)] # External file that doesn't exist at time of save. self.external_files.append(FileMetadata(path='does-not-exist')) self.directory = self.get_abs_directory() # Force absolute. # Absolute external file that exists at time of save. path = os.path.join(self.directory, EXTERNAL_FILES[0]) with open(path, 'w') as out: out.write('Twisty narrow passages.\n') self.external_files.append( FileMetadata(path=path, input=True, constant=True)) # Absolute external file that exists at time of save, in separate tree. path = os.path.join(self.directory, EXTERNAL_FILES[1]) leaf = os.path.dirname(path) if not os.path.exists(leaf): os.makedirs(leaf) with open(path, 'w') as out: out.write('Some external data.\n') self.external_files.append(FileMetadata(path=path)) # Relative external file that exists at time of save. with self.dir_context: path = EXTERNAL_FILES[2] with open(path, 'w') as out: out.write('Hello world!\n') self.external_files.append(FileMetadata(path=path)) # Relative external file that exists at time of save, in separate tree. with self.dir_context: path = EXTERNAL_FILES[3] leaf = os.path.dirname(path) if not os.path.exists(leaf): os.makedirs(leaf) with open(path, 'w') as out: out.write('Some more external data.\n') self.external_files.append(FileMetadata(path=path)) def execute(self): """ Write test data to files. """ if self.write_files: cwd = os.getcwd() self._logger.debug("opening file '%s' in %s" % (self.text_file.path, cwd)) with open(self.text_file.path, 'w') as out: out.write(self.text_data) self._logger.debug("opening file '%s' in %s" % (self.sub.binary_file.path, cwd)) with open(self.sub.binary_file.path, 'wb') as out: cPickle.dump(self.sub.binary_data, out, 2)