def __init__(self, configuration_path=None, configuration_file=None, defaults=None): raw_defaults = None if defaults: raw_defaults = {} for key, value in defaults.items(): raw_defaults[key] = json.dumps(value) self._raw_config = ConfigParser.RawConfigParser(raw_defaults) self._configuration_path = configuration_path if configuration_path: configuration_segments = local_filesystem.getSegmentsFromRealPath( configuration_path) if not local_filesystem.isFile(configuration_segments): raise UtilsError(u'1011', _( u'Configuration file "%s" does not exists.' % ( configuration_path))) try: self._configuration_file = ( local_filesystem.openFileForReading( configuration_segments, utf8=True)) except IOError: raise UtilsError(u'1012', _( u'Server process could not read the configuration file ' u'"%s".' % (configuration_path)) ) elif configuration_file: self._configuration_file = configuration_file else: raise AssertionError('You must specify a path or a file.')
def save(self, configuration_file=None): """ Store the configuration into file. By default, it stores the data, in the same *path* fromw which it was loaded from. `configuration_file` argument is provided to help with testing. """ if configuration_file: self._writeToFile(store_file=configuration_file) return if not self._configuration_path: raise AssertionError( 'Trying to save a configuration that was not loaded from ' ' a file from disk.') real_segments = local_filesystem.getSegmentsFromRealPath( self._configuration_path) tmp_segments = real_segments[:] tmp_segments[-1] = tmp_segments[-1] + u'.tmp' store_file = local_filesystem.openFileForWriting( tmp_segments, utf8=True) self._writeToFile(store_file=store_file) store_file.close() # We delete the file first to work around windows problems. local_filesystem.deleteFile(real_segments) local_filesystem.rename(tmp_segments, real_segments)
def _deletePID(self): pid_path = os.path.abspath(self.options.pid) pid_segments = local_filesystem.getSegmentsFromRealPath(pid_path) try: local_filesystem.deleteFile(pid_segments) except Exception: # We don't care if remove operation fail or success. # We are going to close the server anyway. # Just change the exit value to signal that something went # wrong. self.onStop(1)
def _writePID(self): """ Write process ID in pid file. """ pid_path = os.path.abspath(self.options.pid) pid_segments = local_filesystem.getSegmentsFromRealPath(pid_path) try: pid_file = local_filesystem.openFileForWriting(pid_segments) pid_file.write('%d' % os.getpid()) pid_file.close() except (OSError, IOError): raise CompatError( 1008, _(u'Could not write PID file at %s.' % (pid_path)), )
def __init__(self, path=None, file=None): """ It can be initialized with a path of a file like object. File like object initialization is mainly for testing. """ from chevah.compat import local_filesystem self._path = path if self._path: self._segments = local_filesystem.getSegmentsFromRealPath( self._path) else: self._segments = None self._file = file if not (self._path or self._file) or (self._path and self._file): raise AssertionError('You must specify a path or a file.') self._data = {}