def save(self, data): """ Given a dictionary conforming to the IConfigurationBackend specification, write the data to the file configured with this backend in a format suitable to be read back using load(). """ lines = self._build_group(data, 0) config_directory = os.path.dirname(self.filename) tmp_filename = '%s.%d.%08X' % (self.filename, os.getpid(), random.getrandbits(32)) try: if config_directory: makedirs(config_directory) file = os.fdopen( os.open(tmp_filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0600), 'wb') file.write( (os.linesep.join(lines) + os.linesep).encode(self.encoding)) file.close() if platform.system() == 'Windows': # os.rename does not work on Windows if the destination file already exists. # It seems there is no atomic way to do this on Windows. unlink(self.filename) os.rename(tmp_filename, self.filename) except (IOError, OSError), e: raise FileBuilderError("failed to write configuration data: %s" % str(e))
def save_certificates(self, sip_address, crt, key, ca): crt = crt.strip() + os.linesep key = key.strip() + os.linesep ca = ca.strip() + os.linesep X509Certificate(crt) X509PrivateKey(key) X509Certificate(ca) makedirs(ApplicationData.get('tls')) certificate_path = ApplicationData.get(os.path.join('tls', sip_address+'.crt')) file = open(certificate_path, 'w') os.chmod(certificate_path, 0600) file.write(crt+key) file.close() ca_path = ApplicationData.get(os.path.join('tls', 'ca.crt')) try: existing_cas = open(ca_path).read().strip() + os.linesep except: file = open(ca_path, 'w') file.write(ca) file.close() else: if ca not in existing_cas: file = open(ca_path, 'w') file.write(existing_cas+ca) file.close() settings = SIPSimpleSettings() settings.tls.ca_list = ca_path settings.save() return certificate_path
def store_image(self, data): if data is None: return None data_hash = sha512(data).hexdigest() try: return self.filemap[data_hash].destination except KeyError: pass try: destination_name = os.path.join('images', self.available_names.popleft()) except IndexError: # No more available file names. return None pixmap = QPixmap() if pixmap.loadFromData(data): pixmap = pixmap.scaled(32, 32, Qt.KeepAspectRatio, Qt.SmoothTransformation) makedirs(ApplicationData.get('images')) if pixmap.save(ApplicationData.get(destination_name)): file_mapping = FileMapping(data_hash, destination_name) self.filemap[data_hash] = file_mapping map_filename = ApplicationData.get(os.path.join('images', '.cached_icons.map')) map_tempname = map_filename + '.tmp' try: file = open(map_tempname, 'wb') pickle.dump(self.filemap, file) file.close() if sys.platform == 'win32': unlink(map_filename) os.rename(map_tempname, map_filename) except Exception, e: log.error("could not save icon cache file mappings: %s" % e) return destination_name
def __init__(self): makedirs(ApplicationData.get('images')) try: self.filemap = pickle.load(open(ApplicationData.get(os.path.join('images', '.cached_icons.map')))) except Exception: self.filemap = {} all_names = set('cached_icon_%04d.png' % x for x in xrange(1, 10000)) used_names = set(os.listdir(ApplicationData.get('images'))) self.available_names = deque(sorted(all_names - used_names))
def start(self): # There is still a race condition here in that the directory can be removed # before the PJSIP opens the file. There's nothing that can be done about # it as long as PJSIP doesn't accept an already open file descriptor. -Luci makedirs(os.path.dirname(self.filename)) self._recording_wave_file = RecordingWaveFile(self.mixer, self.filename) self._recording_wave_file.start() notification_center = NotificationCenter() notification_center.post_notification('AudioPortDidChangeSlots', sender=self, data=TimestampedNotificationData(consumer_slot_changed=True, producer_slot_changed=False, old_consumer_slot=None, new_consumer_slot=self._recording_wave_file.slot))
def _init_log_directory(self): settings = SIPSimpleSettings() log_directory = settings.logs.directory.normalized try: makedirs(log_directory) except Exception, e: if not self._log_directory_error: print "failed to create logs directory '%s': %s" % (log_directory, e) self._log_directory_error = True self._siptrace_error = True self._pjsiptrace_error = True self._notifications_error = True raise
def start(self): # There is still a race condition here in that the directory can be removed # before the PJSIP opens the file. There's nothing that can be done about # it as long as PJSIP doesn't accept an already open file descriptor. -Luci makedirs(os.path.dirname(self.filename)) self._recording_wave_file = RecordingWaveFile(self.mixer, self.filename) self._recording_wave_file.start() notification_center = NotificationCenter() notification_center.post_notification( 'AudioPortDidChangeSlots', sender=self, data=TimestampedNotificationData( consumer_slot_changed=True, producer_slot_changed=False, old_consumer_slot=None, new_consumer_slot=self._recording_wave_file.slot))
def store(self, filename, pixmap=None): if filename is None: return None if not os.path.isabs(filename): return filename if filename.startswith(ApplicationData.directory + os.path.sep): return filename[len(ApplicationData.directory + os.path.sep):] try: file_mapping = self.filemap[filename] except KeyError: pass else: source_info = FileInfo(filename) destination_info = FileInfo(file_mapping.destination.name) if (source_info, destination_info) == (file_mapping.source, file_mapping.destination): return destination_info.name try: destination_name = os.path.join('images', self.available_names.popleft()) except IndexError: # No more available file names. Return original file for now return filename if pixmap is None: pixmap = QPixmap() if pixmap.load(filename): pixmap = pixmap.scaled(32, 32, Qt.KeepAspectRatio, Qt.SmoothTransformation) makedirs(ApplicationData.get('images')) if pixmap.save(ApplicationData.get(destination_name)): source_info = FileInfo(filename) destination_info = FileInfo(destination_name) file_mapping = FileMapping(source_info, destination_info) self.filemap[filename] = file_mapping map_filename = ApplicationData.get(os.path.join('images', '.cached_icons.map')) map_tempname = map_filename + '.tmp' try: file = open(map_tempname, 'wb') pickle.dump(self.filemap, file) file.close() if sys.platform == 'win32': unlink(map_filename) os.rename(map_tempname, map_filename) except Exception, e: log.error("could not save icon cache file mappings: %s" % e) return destination_name
def save(self, data): """ Given a dictionary conforming to the IConfigurationBackend specification, write the data to the file configured with this backend in a format suitable to be read back using load(). """ lines = self._build_group(data, 0) config_directory = os.path.dirname(self.filename) tmp_filename = '%s.%d.%08X' % (self.filename, os.getpid(), random.getrandbits(32)) try: if config_directory: makedirs(config_directory) file = os.fdopen(os.open(tmp_filename, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0600), 'wb') file.write((os.linesep.join(lines)+os.linesep).encode(self.encoding)) file.close() if platform.system() == 'Windows': # os.rename does not work on Windows if the destination file already exists. # It seems there is no atomic way to do this on Windows. unlink(self.filename) os.rename(tmp_filename, self.filename) except (IOError, OSError), e: raise FileBuilderError("failed to write configuration data: %s" % str(e))
def file(self): if 'file' not in self.__dict__: directory = os.path.dirname(self.filename) makedirs(directory) self.__dict__['file'] = open(self.filename, 'a') return self.__dict__['file']