<commit_before>#
# This file is part of Export Layers.
#
# Copyright (C) 2013-2015 khalim19 <<EMAIL>>
#
# Export Layers is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Export Layers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Export Layers.  If not, see <http://www.gnu.org/licenses/>.
#

"""
This module:
* is the core of the plug-in
* defines a class that exports layers as individual images
* defines filter rules for layers
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

str = unicode

import collections
import contextlib
import datetime
import functools
import os

import gimp
import gimpenums

pdb = gimp.pdb

import export_layers.pygimplib as pygimplib

from export_layers.pygimplib import objectfilter
from export_layers.pygimplib import overwrite
from export_layers.pygimplib import pgfileformats
from export_layers.pygimplib import pgitemtree
from export_layers.pygimplib import pgpath
from export_layers.pygimplib import pgpdb
from export_layers.pygimplib import progress

#===============================================================================


class ExportLayersError(Exception):
  
  def __init__(self, message="", layer=None, file_extension=None):
    super(ExportLayersError, self).__init__()
    
    self.message = message
    
    try:
      self.layer_name = layer.name
    except AttributeError:
      self.layer_name = None
    
    self.file_extension = file_extension
  
  def __str__(self):
    str_ = self.message
    
    if self.layer_name:
      str_ += "\n" + _("Layer:") + " " + self.layer_name
    if self.file_extension:
      str_ += "\n" + _("File extension:") + " " + self.file_extension
    
    return str_


class ExportLayersCancelError(ExportLayersError):
  pass


class InvalidOutputDirectoryError(ExportLayersError):
  pass


#===============================================================================


class LayerFilterRules(object):
  
  @staticmethod
  def is_layer(layer_elem):
    return layer_elem.item_type == layer_elem.ITEM
  
  @staticmethod
  def is_nonempty_group(layer_elem):
    return layer_elem.item_type == layer_elem.NONEMPTY_GROUP
  
  @staticmethod
  def is_empty_group(layer_elem):
    return layer_elem.item_type == layer_elem.EMPTY_GROUP
  
  @staticmethod
  def is_top_level(layer_elem):
    return layer_elem.level == 0
  
  @staticmethod
  def is_path_visible(layer_elem):
    return layer_elem.path_visible
  
  @staticmethod
  def has_matching_file_extension(layer_elem, file_extension):
    return layer_elem.get_file_extension() == file_extension.lower()
  
  @staticmethod
  def has_tags(layer_elem, *tags):
    if tags:
      return any(tag for tag in tags if tag in layer_elem.tags)
    else:
      return bool(layer_elem.tags)
  
  @staticmethod
  def has_no_tags(layer_elem, *tags):
    return not LayerFilterRules.has_tags(layer_elem, *tags)
  
  @staticmethod
  def is_layer_in_selected_layers(layer_elem, selected_layers):
    return layer_elem.item.ID in selected_layers


#===============================================================================


class _FileExtensionProperties(object):
  """
  This class contains additional properties for a file extension.<commit_msg>exportlayers: Rename 'level' to 'depth'<commit_after>#
# This file is part of Export Layers.
#
# Copyright (C) 2013-2015 khalim19 <<EMAIL>>
#
# Export Layers is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Export Layers is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Export Layers.  If not, see <http://www.gnu.org/licenses/>.
#

"""
This module:
* is the core of the plug-in
* defines a class that exports layers as individual images
* defines filter rules for layers
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

str = unicode

import collections
import contextlib
import datetime
import functools
import os

import gimp
import gimpenums

pdb = gimp.pdb

import export_layers.pygimplib as pygimplib

from export_layers.pygimplib import objectfilter
from export_layers.pygimplib import overwrite
from export_layers.pygimplib import pgfileformats
from export_layers.pygimplib import pgitemtree
from export_layers.pygimplib import pgpath
from export_layers.pygimplib import pgpdb
from export_layers.pygimplib import progress

#===============================================================================


class ExportLayersError(Exception):
  
  def __init__(self, message="", layer=None, file_extension=None):
    super(ExportLayersError, self).__init__()
    
    self.message = message
    
    try:
      self.layer_name = layer.name
    except AttributeError:
      self.layer_name = None
    
    self.file_extension = file_extension
  
  def __str__(self):
    str_ = self.message
    
    if self.layer_name:
      str_ += "\n" + _("Layer:") + " " + self.layer_name
    if self.file_extension:
      str_ += "\n" + _("File extension:") + " " + self.file_extension
    
    return str_


class ExportLayersCancelError(ExportLayersError):
  pass


class InvalidOutputDirectoryError(ExportLayersError):
  pass


#===============================================================================


class LayerFilterRules(object):
  
  @staticmethod
  def is_layer(layer_elem):
    return layer_elem.item_type == layer_elem.ITEM
  
  @staticmethod
  def is_nonempty_group(layer_elem):
    return layer_elem.item_type == layer_elem.NONEMPTY_GROUP
  
  @staticmethod
  def is_empty_group(layer_elem):
    return layer_elem.item_type == layer_elem.EMPTY_GROUP
  
  @staticmethod
  def is_top_level(layer_elem):
    return layer_elem.depth == 0
  
  @staticmethod
  def is_path_visible(layer_elem):
    return layer_elem.path_visible
  
  @staticmethod
  def has_matching_file_extension(layer_elem, file_extension):
    return layer_elem.get_file_extension() == file_extension.lower()
  
  @staticmethod
  def has_tags(layer_elem, *tags):
    if tags:
      return any(tag for tag in tags if tag in layer_elem.tags)
    else:
      return bool(layer_elem.tags)
  
  @staticmethod
  def has_no_tags(layer_elem, *tags):
    return not LayerFilterRules.has_tags(layer_elem, *tags)
  
  @staticmethod
  def is_layer_in_selected_layers(layer_elem, selected_layers):
    return layer_elem.item.ID in selected_layers


#===============================================================================


class _FileExtensionProperties(object):
  """
  This class contains additional properties for a file extension.
{'fraction_non_alphanumeric': 0.12932871603622803, 'fraction_numerical': 0.0031965903036760787, 'mean_word_length': 4.549889135254989, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '750011', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2132, 'n_words': 661}