def changed_at(self, change_time): """ Set the datetime the model was last changed. This will change the data directory for images created for the model, effectively busting any cached versions of those images. This method is called, among other places, in __init__, before the model has a base_dir or an ID, so we defend against that possibility. Note that previous data directories are kept around. XXX: Seems bad that updating the `changed_at` field also creates a directory, changes a renderer's image directory and writes a background image... """ self._changed_at = change_time if self.data_dir and not os.path.exists(self.data_dir): util.mkdir_p(self.data_dir) if self.renderer: self.renderer.images_dir = self.data_dir # Save a new background image. self.renderer.prepare_for_model_run(self._cache)
def __init__(self, *args, **kwargs): self.lock = threading.RLock() kwargs['cache_enabled'] = kwargs.get('cache_enabled', True) data_dir = kwargs.pop('data_dir') self.package_root = kwargs.pop('package_root') self.renderer = None # Model.__init__ ends up calling rewind(), which looks for base_dir to # set up the data directory. Set `base_dir` to None to skip those steps # during superclass initialization. self.base_dir = None # Set the model's ID, which we need to set the base_dir. super(WebModel, self).__init__(*args, **kwargs) # Remove the default map object. if self.map: self.remove_map() self.base_dir = os.path.join(self.package_root, data_dir, str(self.id)) self.base_dir_relative = os.path.join(data_dir, str(self.id)) # The static data dir is for things like file uploads that are not bound # to a datetime for caching purposes. self.static_data_dir = os.path.join(self.base_dir, 'data') # Create the base directory for all of the model's data. util.mkdir_p(self.base_dir) util.mkdir_p(self.static_data_dir) # Patch the object with an empty ``time_steps`` array for the time being. # TODO: Add output caching in the model? self.time_steps = [] self.changed_at = datetime.datetime.now()
def __init__(self, *args, **kwargs): self.lock = threading.RLock() kwargs['cache_enabled'] = kwargs.get('cache_enabled', True) data_dir = kwargs.pop('data_dir') self.package_root = kwargs.pop('package_root') self.renderer = None # Model.__init__ ends up calling rewind(), which looks for base_dir to # set up the data directory. Set `base_dir` to None to skip those steps # during superclass initialization. self.base_dir = None # Set the model's ID, which we need to set the base_dir. super(WebModel, self).__init__(*args, **kwargs) # Remove the default map object. if self.map: self.remove_map() self.base_dir = os.path.join(self.package_root, data_dir, str(self.id)) self.base_dir_relative = os.path.join(data_dir, str(self.id)) # The static data dir is for things like file uploads that are # not bound to a datetime for caching purposes. self.static_data_dir = os.path.join(self.base_dir, 'data') # Create the base directory for all of the model's data. util.mkdir_p(self.base_dir) util.mkdir_p(self.static_data_dir) # Patch the object with an empty ``time_steps`` array for # the time being. # TODO: Add output caching in the model? self.time_steps = [] self.changed_at = datetime.datetime.now()