def get_comments(comment_directory, comment_filename=None): """ Returns all the comments from a comments.txt file :param comment_directory: str, directory where comment.txt file is located :param comment_filename: str, name of the c omment file. By default, comment.txt. :return: dict(filename, value), (comment, user) """ comment_filename = comment_filename or 'comments.json' comment_file = path.join_path(comment_directory, comment_filename) if not comment_file or not os.path.isfile(comment_file): return comments = dict() comments_data = jsonio.read_file(comment_file) if not comments_data: return comments for version_dict in comments_data: file_name = version_dict.get('version', None) comment = version_dict.get('comment', '') user = version_dict.get('user', '') if comment_filename and comment_filename == file_name: return comment, comments[file_name] = [comment, user] return comments
def init_naming_data(self): """ Function that initializes naming data file """ if not self.has_valid_naming_file(): try: f = open(self._naming_file, 'w') f.close() except Exception: pass if not self.has_valid_naming_file(): LOGGER.warning( 'Impossible to initialize naming data because naming file: "{}" does not exists!' .format(self._naming_file)) return None if self._parser_format == 'yaml': data = yamlio.read_file(self.naming_file) else: data = jsonio.read_file(self._naming_file) if not data: data = self.DEFAULT_DATA if self._parser_format == 'yaml': yamlio.write_to_file(data, self._naming_file) else: jsonio.write_to_file(data, self._naming_file) else: self.load_session() return None
def get_version_data(self, version_number): """ Returns the data (comment and user) of the given version :param version_number: int :return: tuple(str, str) """ comment_path = self._get_comment_path() if not comment_path: return None, None if not path.is_file(comment_path): return None, None version_data = jsonio.read_file(comment_path) if not version_data: return None, None for version_dict in version_data: version = version_dict.get('version', None) comment = version_dict.get('comment', '') user = version_dict.get('user', '') if version == str(version_number): return comment, user return None, None
def load_data_file(self, file_path=None): if not self._active_library: logger.warning( 'Must set active library before running this function.') return if not file_path: file_path = path_utils.join_path( self._curves_data_path, '{}{}'.format(self._active_library, self._extension)) data = jsonio.read_file(file_path) or dict() for curve_name, curve_data in data.items(): self._library_curves[self._active_library][curve_name] = curve_data
def load_curve_from_path(curve_path): """ Loads the curve data from the given curve path :param curve_path: str, path that stores curve data :return: dict, dictionary containing curve data """ if not curve_path or not os.path.isfile(curve_path): return None path_ext = os.path.splitext(os.path.basename(curve_path))[0] if not path_ext != consts.CURVE_EXT: return None curve_data = jsonio.read_file(curve_path) return curve_data
def read(self): """ Reads the current session status :return: list """ if not os.path.exists(self._path): return list() try: return jsonio.read_file(self._path) except Exception as exc: LOGGER.error( 'Error while reading Script Editor session file: {} | {}'. format(self._path, exc)) return list()
def create_curve(curve_type, curves_path=None, curve_name='new_curve', curve_size=1.0, translate_offset=(0.0, 0.0, 0.0), scale=(1.0, 1.0, 1.0), axis_order='XYZ', mirror=None, color=None, parent=None): """ Creates the curve stored in the given path :param curve_type: str, type of the control to create :param curves_path: str, path that stores control data :param curve_name: str, name of the transform node of the created control :param curve_size: float, global size of the curve :param translate_offset: tuple(float, float, float), XYZ translation offset to apply to the curve :param scale: tuple(float, float, float), XYZ scale to apply to the curve :param axis_order: str, axis order of the curve. Default is XYZ. :param mirror: str or None, axis mirror to apply to the curve shapes (None, 'X', 'Y' or 'Z') :param color: tuple(float, float, float) or int, color of the curve :param parent: str or None, if given control shapes will be parented into this transform :return: """ if not curves_path or not os.path.isdir(curves_path): control_path = find_curve_path_by_name(curve_type) else: control_path = os.path.join( curves_path, '{}{}'.format(curve_type, consts.CURVE_EXT)) if not control_path or not os.path.isfile(control_path): return None control_data = jsonio.read_file(control_path, as_ordered_dict=True) if not control_data: return None return create_curve_from_data(control_data, name=curve_name, curve_size=curve_size, translate_offset=translate_offset, scale=scale, axis_order=axis_order, mirror=mirror, color=color, parent=parent)
def load_data(self, file_path=None): if not file_path or not os.path.isfile(file_path): file_path = dcc.select_file_dialog('Open File', pattern='*.json') if not file_path or not os.path.isfile(file_path): return False data = jsonio.read_file(file_path) if not data: LOGGER.warning('Impossible to read data from {}'.format(file_path)) return False self._model.prepareLoad.emit() for i, interpolator_data in enumerate(data): title = interpolator_data.get('title', None) data = interpolator_data.get('data', None) close_button_visible = False if i == 0 else True self.add_interpolator_widget(close_button_visible=close_button_visible, title=title, items=data)
def _read(self): """ Override function to add support to read JSON files """ if not self._has_json_file(): self.settings_dict = OrderedDict() return file_path = self._get_json_file() if not file_path: return self.file_path = file_path try: data = OrderedDict(jsonio.read_file(file_path)) except Exception: self.settings_dict = OrderedDict() return self.settings_dict = data
def save_comment(self, comment=None, version_file=None): """ Saves a comment to a log file :param comment: str, comment to save :param version_file: str, correspnding version file """ version = version_file.split('.') if version: version = version[-1] user = getpass.getuser() if not comment: comment = '-' comment.replace('"', '\"') current_data = jsonio.read_file(self._comment_file) or list() version_data = {'version': version, 'comment': comment, 'user': user} current_data.append(version_data) jsonio.write_to_file(current_data, self._comment_file)
def load_naming_data(self): """ Loads data contained in wrapped naming file :return: dict """ if not self.has_valid_naming_file(): LOGGER.warning( 'Impossible to read naming file because naming file: "{}" does not exists!' .format(self._naming_file)) return None try: if self._parser_format == 'yaml': data = yamlio.read_file(self._naming_file) else: data = jsonio.read_file(self._naming_file) return data except Exception as exc: LOGGER.error('Impossible to read naming file "{}": {} | {}'.format( self._naming_file, exc, traceback.format_exc())) return None
def update_dependencies(self, dependencies=None, recursive=True): if not dependencies or not isinstance(dependencies, dict): dependencies = dict() dependency_file_name = '{}.json'.format(self._db.get_uuid(self.format_identifier())) dependency_path = path_utils.join_path(self._db.get_dependencies_path(), dependency_file_name) if not os.path.isfile(dependency_path): fileio.create_file(dependency_path) all_dependencies = dict() current_dependencies = jsonio.read_file(dependency_path) or dict() for dependency_uuid, dependency_name in current_dependencies.items(): dependency = self._db.find_identifier_from_uuid(dependency_uuid) if not dependency: continue all_dependencies.update({dependency: dependency_name}) if dependencies: all_dependencies.update(dependencies) for dependency, dependency_name in all_dependencies.items(): self._db.add_dependency(self.format_identifier(), dependency, dependency_name) dependencies = self._db.get_dependencies(self.format_identifier(), as_uuid=True) if not dependencies: fileio.delete_file(dependency_path) return jsonio.write_to_file(dependencies, dependency_path) # We update all related dependencies if recursive: for dependency, dependency_name in all_dependencies.items(): dependency_item = self._db.get(dependency) if not dependency_item: continue dependency_item.update_dependencies( dependencies={self.format_identifier(): self.type()}, recursive=False)