<commit_before># ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2015 - 2017 Pixar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# ##### END MIT LICENSE BLOCK #####

from rman_utils.rman_assets import core as ra
from rman_utils.rman_assets import lib as ral
from rman_utils.rman_assets.core import RmanAsset
from rman_utils.rman_assets.common.definitions import TrMode, TrStorage, TrSpace, TrType
from rman_utils.filepath import FilePath

import os
import os.path
import re
import sys
import time
import bpy as mc  # just a test
import bpy
import mathutils
from math import radians
from ..rfb_utils import filepath_utils
from ..rfb_utils import string_utils
from ..rfb_utils import shadergraph_utils
from ..rfb_utils import object_utils   
from ..rfb_utils import transform_utils
from ..rfb_utils import texture_utils
from ..rfb_utils.prefs_utils import get_pref, get_addon_prefs
from ..rfb_utils.property_utils import __GAINS_TO_ENABLE__
from ..rman_bl_nodes import __BL_NODES_MAP__, __RMAN_NODE_TYPES__
from ..rman_constants import RMAN_STYLIZED_FILTERS

def default_label_from_file_name(filename):
    # print filename
    lbl = os.path.splitext(os.path.basename(filename))[0]
    # print lbl
    lbl = re.sub('([A-Z]+)', r' \1', lbl)
    # print lbl
    lbl = lbl.replace('_', ' ')
    return lbl.strip().capitalize()

def asset_name_from_label(label):
    """Builds a filename from the asset label string.

    Args:
    - label (str): User-friendly label

    Returns:
    - the asset file name
    """
    assetDir = re.sub(r'[^\w]', '', re.sub(' ', '_', label)) + '.rma'
    return assetDir



class BlenderHostPrefs(ral.HostPrefs):
    def __init__(self):
        super(BlenderHostPrefs, self).__init__('24.0')
        self.debug = False

        # === Library Prefs ===
        #
        self.rpbConfigFile = FilePath(self.getHostPref('rpbConfigFile', u''))
        # the list of user libraries from Maya prefs
        self.rpbUserLibraries = self.getHostPref('rpbUserLibraries', [])
        # We don't initialize the library configuration just yet. We want
        # to do it only once the prefs objects has been fully contructed.
        # This is currently done in rman_assets.ui.Ui.__init__()
        self.cfg = None

        # === UI Prefs ===
        #
        # our prefered swatch size in the UI.
        self.rpbSwatchSize = self.getHostPref('rpbSwatchSize', 64)
        # the last selected preview type
        self.rpbSelectedPreviewEnv = self.getHostPref(
            'rpbSelectedPreviewEnv', 0)
        # the last selected category
        self.rpbSelectedCategory = self.getHostPref('rpbSelectedCategory', u'')
        # the last selected preset
        self.rpbSelectedPreset = self.getHostPref('rpbSelectedPreset', u'')

        # the last selected library
        self.rpbSelectedLibrary = FilePath(self.getHostPref(
            'rpbSelectedLibrary', u''))
    
        # store these to make sure we render the previews with the same version.
        self.hostTree = os.environ.get('RFMTREE', '')
        self.rmanTree = os.environ.get('RMANTREE', '')

        # === User Prefs ===
        #
        # render all HDR environments ?
        self.rpbRenderAllHDRs = self.getHostPref('rpbRenderAllHDRs', 0)
        self.rpbHideFactoryLib = self.getHostPref('rpbHideFactoryLib', 0)

        self._nodesToExport = list()

    def getHostPref(self, prefName, defaultValue): # pylint: disable=unused-argument
        if prefName == 'rpbUserLibraries':
            val = list()
            prefs = get_addon_prefs()
            for p in prefs.rpbUserLibraries:
                val.append(p.path)
        else:
            val = get_pref(pref_name=prefName, default=defaultValue)
        return val

    def setHostPref(self, prefName, value): # pylint: disable=unused-argument
        """Save the given value in the host's preferences.
        First look at the value's type and call the matching host API.

        Args:
            prefName (str): The class attribute name for that pref.
            value (any): The value we should pass to the delegate.

        Raises:
            RmanAssetLibError: If we don't support the given data type.
        """
        isArray = isinstance(value, list)
        tvalue = value
        prefs = get_addon_prefs()
        if isArray:
            tvalue = value[0]
        if isinstance(tvalue, int):
            if isArray:
                pass
            else:
                setattr(prefs, prefName, value) 
        elif isinstance(tvalue, str):
            if isArray:
                if prefName == 'rpbUserLibraries':
                    prefs = get_addon_prefs()
                    prefs.rpbUserLibraries.clear()
                    for val in value:
                        p = prefs.rpbUserLibraries.add()
                        p.path = val
            else:
                setattr(prefs, prefName, value)                
        else:
            arrayStr = ''
            if isArray:
                arrayStr = ' array'
            msg = ('HostPrefs.setHostPref: %s%s NOT supported !' %<commit_msg>Try and remove duplicate libraries from the preset browser.<commit_after># ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2015 - 2017 Pixar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# ##### END MIT LICENSE BLOCK #####

from rman_utils.rman_assets import core as ra
from rman_utils.rman_assets import lib as ral
from rman_utils.rman_assets.core import RmanAsset
from rman_utils.rman_assets.common.definitions import TrMode, TrStorage, TrSpace, TrType
from rman_utils.filepath import FilePath

import os
import os.path
import re
import sys
import time
import bpy as mc  # just a test
import bpy
import mathutils
from math import radians
from ..rfb_utils import filepath_utils
from ..rfb_utils import string_utils
from ..rfb_utils import shadergraph_utils
from ..rfb_utils import object_utils   
from ..rfb_utils import transform_utils
from ..rfb_utils import texture_utils
from ..rfb_utils.prefs_utils import get_pref, get_addon_prefs
from ..rfb_utils.property_utils import __GAINS_TO_ENABLE__
from ..rman_bl_nodes import __BL_NODES_MAP__, __RMAN_NODE_TYPES__
from ..rman_constants import RMAN_STYLIZED_FILTERS

def default_label_from_file_name(filename):
    # print filename
    lbl = os.path.splitext(os.path.basename(filename))[0]
    # print lbl
    lbl = re.sub('([A-Z]+)', r' \1', lbl)
    # print lbl
    lbl = lbl.replace('_', ' ')
    return lbl.strip().capitalize()

def asset_name_from_label(label):
    """Builds a filename from the asset label string.

    Args:
    - label (str): User-friendly label

    Returns:
    - the asset file name
    """
    assetDir = re.sub(r'[^\w]', '', re.sub(' ', '_', label)) + '.rma'
    return assetDir



class BlenderHostPrefs(ral.HostPrefs):
    def __init__(self):
        super(BlenderHostPrefs, self).__init__('24.0')
        self.debug = False

        # === Library Prefs ===
        #
        self.rpbConfigFile = FilePath(self.getHostPref('rpbConfigFile', u''))
        # the list of user libraries from Maya prefs
        self.rpbUserLibraries = self.getHostPref('rpbUserLibraries', [])
        # We don't initialize the library configuration just yet. We want
        # to do it only once the prefs objects has been fully contructed.
        # This is currently done in rman_assets.ui.Ui.__init__()
        self.cfg = None

        # === UI Prefs ===
        #
        # our prefered swatch size in the UI.
        self.rpbSwatchSize = self.getHostPref('rpbSwatchSize', 64)
        # the last selected preview type
        self.rpbSelectedPreviewEnv = self.getHostPref(
            'rpbSelectedPreviewEnv', 0)
        # the last selected category
        self.rpbSelectedCategory = self.getHostPref('rpbSelectedCategory', u'')
        # the last selected preset
        self.rpbSelectedPreset = self.getHostPref('rpbSelectedPreset', u'')

        # the last selected library
        self.rpbSelectedLibrary = FilePath(self.getHostPref(
            'rpbSelectedLibrary', u''))
    
        # store these to make sure we render the previews with the same version.
        self.hostTree = os.environ.get('RFMTREE', '')
        self.rmanTree = os.environ.get('RMANTREE', '')

        # === User Prefs ===
        #
        # render all HDR environments ?
        self.rpbRenderAllHDRs = self.getHostPref('rpbRenderAllHDRs', 0)
        self.rpbHideFactoryLib = self.getHostPref('rpbHideFactoryLib', 0)

        self._nodesToExport = list()

    def getHostPref(self, prefName, defaultValue): # pylint: disable=unused-argument
        if prefName == 'rpbUserLibraries':
            val = list()
            prefs = get_addon_prefs()
            for p in prefs.rpbUserLibraries:
                path = p.path
                if path.endswith('/'):
                    path = path[:-1]
                if path not in val:
                    val.append(path)
        else:
            val = get_pref(pref_name=prefName, default=defaultValue)
        return val

    def setHostPref(self, prefName, value): # pylint: disable=unused-argument
        """Save the given value in the host's preferences.
        First look at the value's type and call the matching host API.

        Args:
            prefName (str): The class attribute name for that pref.
            value (any): The value we should pass to the delegate.

        Raises:
            RmanAssetLibError: If we don't support the given data type.
        """
        isArray = isinstance(value, list)
        tvalue = value
        prefs = get_addon_prefs()
        if isArray:
            tvalue = value[0]
        if isinstance(tvalue, int):
            if isArray:
                pass
            else:
                setattr(prefs, prefName, value) 
        elif isinstance(tvalue, str):
            if isArray:
                if prefName == 'rpbUserLibraries':
                    prefs = get_addon_prefs()
                    prefs.rpbUserLibraries.clear()
                    for val in list(dict.fromkeys(value)):
                        p = prefs.rpbUserLibraries.add()
                        p.path = val
            else:
                setattr(prefs, prefName, value)                
        else:
            arrayStr = ''
            if isArray:
                arrayStr = ' array'
            msg = ('HostPrefs.setHostPref: %s%s NOT supported !' %
{'fraction_non_alphanumeric': 0.08657799274486094, 'fraction_numerical': 0.003143893591293833, 'mean_word_length': 2.561871949468849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1526878', 'n_tokens_mistral': 3633, 'n_tokens_neox': 3529, 'n_words': 1175}