Beispiel #1
0
    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
Beispiel #2
0
    def write(self, data):
        """
        Writes session into disk
        :param data: dict
        :return:
        """

        jsonio.write_to_file(data, self._path, indent=4)

        return self._path
Beispiel #3
0
def save_curve(curve_node,
               curve_name,
               curves_path=None,
               override=True,
               save_matrix=False,
               normalize=True):
    """
    Saves the given curve transform node shapes into the given directory path
    :param curve_node: str
    :param curve_name: str
    :param curves_path: str
    :param override: bool
    :param save_matrix: bool
    :param normalize: bool
    :return:
    """

    if not curves_path or not os.path.isdir(curves_path):
        curves_path = list(iterate_curve_root_paths())
        if not curves_path:
            logger.warning(
                'Impossible to save curve because no path to save curve defined'
            )
            return False
        curves_path = curves_path[0]
    if not curves_path or not os.path.isdir(curves_path):
        logger.warning(
            'Impossible to save curve because path to save curve into does not exists: "{}"'
            .format(curves_path))
        return False

    curve_file_name = curve_name
    if not curve_name.endswith(consts.CURVE_EXT):
        curve_file_name = '{}{}'.format(curve_name, consts.CURVE_EXT)

    if not override and curve_file_name in get_curve_names():
        logger.warning(
            'Curve name: "{}" already exists in curves paths'.format(
                curve_name))
        return None, None

    curve_data = serialize_curve(curve_node, normalize=normalize)
    if not save_matrix:
        for curve_shape in curve_data:
            curve_data[curve_shape].pop('matrix', None)

    curve_path = path_utils.clean_path(
        os.path.join(curves_path, curve_file_name))

    jsonio.write_to_file(curve_data, curve_path)

    return curve_data, curve_path
Beispiel #4
0
    def write_data_to_file(self):
        if not self._active_library:
            logger.warning(
                'Must set active library before running this function')
            return

        file_path = path_utils.join_path(self._curves_data_path,
                                         self._active_library)
        if not file_path.endswith(self._extension):
            file_path = '{}{}'.format(file_path, self._extension)

        library_data = self._library_curves[self._active_library]
        jsonio.write_to_file(library_data, file_path)

        return file_path
Beispiel #5
0
    def create_metadata(self, version):

        # We make sure the current item exists and also, we make sure the item has all composition metadata
        item = self._db.get(self._id)
        if not item:
            return

        metadata = item.metadata_dict() or dict()
        metadata_path = self.get_metadata_path(version)
        metadata_dir = os.path.dirname(metadata_path)
        if not os.path.isdir(metadata_dir):
            folder.create_folder(metadata_dir)
        jsonio.write_to_file(metadata, metadata_path)

        self.set_metadata(version, metadata)
    def save_data(self, file_path=None):
        interpolator_widgets = self._model.interpolator_widgets
        if not interpolator_widgets:
            return False

        data_to_store = list()
        for interpolator_widget in interpolator_widgets:
            interpolator_data = {
                'title': interpolator_widget.model.title,
                'data': interpolator_widget.model.items
            }
            data_to_store.append(interpolator_data)
        if not data_to_store:
            LOGGER.warning('No data to store')
            return False

        if not file_path or not os.path.isdir(file_path):
            file_path = dcc.save_file_dialog('Save File', pattern='*.json')
        if not file_path:
            return False

        valid = jsonio.write_to_file(data_to_store, file_path)
        if not valid:
            LOGGER.error('Error while saving JSON file ...')
            return False

        return True
Beispiel #7
0
    def save_naming_data(self, data):
        """
        Saves data into the wrapped naming file
        """

        if not self.has_valid_naming_file():
            LOGGER.warning(
                'Impossible to save naming file because naming file: "{}" does not exists!'
                .format(self._naming_file))
            return None

        try:
            if self._parser_format == 'yaml':
                yamlio.write_to_file(data, self._naming_file)
            else:
                jsonio.write_to_file(data, self._naming_file)
        except Exception as exc:
            LOGGER.error('Impossible to read naming file "{}": {} | {}'.format(
                self._naming_file, exc, traceback.format_exc()))
Beispiel #8
0
def save_curve_from_data(curve_data,
                         curve_name,
                         curves_path=None,
                         override=True):
    """
    Saves the given curve data into the given directory path
    :param curve_data: dict
    :param curve_name: str
    :param curves_path: str
    :param override: bool
    :return:
    """

    if not curves_path or not os.path.isdir(curves_path):
        curves_path = list(iterate_curve_root_paths())
        if not curves_path:
            logger.warning(
                'Impossible to save curve because no path to save curve defined'
            )
            return False
        curves_path = curves_path[0]
    if not curves_path or not os.path.isdir(curves_path):
        logger.warning(
            'Impossible to save curve because path to save curve into does not exists: "{}"'
            .format(curves_path))
        return False

    curve_file_name = curve_name
    if not curve_name.endswith(consts.CURVE_EXT):
        curve_file_name = '{}{}'.format(curve_name, consts.CURVE_EXT)

    if not override and curve_file_name in get_curve_names():
        logger.warning(
            'Control name: "{}" already exists in curves paths'.format(
                curve_name))
        return None, None

    curve_path = path_utils.clean_path(
        os.path.join(curves_path, curve_file_name))

    jsonio.write_to_file(curve_data, curve_path)

    return curve_data, curve_path
Beispiel #9
0
    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)
Beispiel #10
0
    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)