# Path: packtools/validations.py
# def is_valid_value_for_pid_v2(value):
# VALIDATE_FUNCTIONS = dict((
#     ("scielo_pid_v2", is_valid_value_for_pid_v2),
# ))
# 
# Path: packtools/sps/exceptions.py
# class NotAllowedtoChangeAttributeValueError(Exception):
# class InvalidAttributeValueError(Exception):
# class InvalidValueForOrderError(Exception):
# class SPSLoadToXMLError(Exception):
# class SHA1Error(Exception):
# class SPSConnectionError(Exception):
# class SPSHTTPError(Exception):
# class SPSHTTPForbiddenError(Exception):
# class SPSHTTPResourceNotFoundError(Exception):
# class SPSHTTPInternalServerError(Exception):
# class SPSHTTPBadGatewayError(Exception):
# class SPSHTTPServiceUnavailableError(Exception):
# class SPSDownloadXMLError(Exception):
# class SPSXMLLinkError(Exception):
# class SPSXMLContentError(Exception):
# class SPSXMLFileError(Exception):
# class SPSAssetOrRenditionFileError(Exception):
# class SPSMakePackageFromPathsMissingKeyError(Exception):
# 
# Path: packtools/file_utils.py
# def is_folder(source):
# def is_zipfile(source):
# def xml_files_list(path):
# def files_list(path):
# def read_file(path, encoding="utf-8", mode="r"):
# def read_from_zipfile(zip_path, filename):
# def xml_files_list_from_zipfile(zip_path):
# def files_list_from_zipfile(zip_path):
# def write_file(path, source, mode="w"):
# def create_zip_file(files, zip_name, zip_folder=None):
# def delete_folder(path):
# def create_temp_file(filename, content=None, mode='w'):
# def copy_file(source, target):
# def size(file_path):
# def get_prefix_by_xml_filename(xml_filename):
# def get_file_role(file_path, prefix, pdf_langs):
# def extract_issn_from_zip_uri(zip_uri):
# def is_valid_file(file_path, check_mimetype=False):
# def get_mimetype(file_path):
# def get_filename(file_path):


import logging
import re
from copy import deepcopy
from lxml import etree
from packtools import validations
from packtools.sps import exceptions
from packtools import file_utils

def parse_value(value):
    value = value.lower()
    if value.isdigit():
        return value.zfill(2)
    if "spe" in value:
        return "spe"
    if "sup" in value:
        return "s"
    return value


def parse_issue(issue):
    issue = " ".join([item for item in issue.split()])
    parts = issue.split()
    parts = [parse_value(item) for item in parts]
    s = "-".join(parts)
    s = s.replace("spe-", "spe")
    s = s.replace("s-", "s")
    if s.endswith("s"):
        s += "0"
    return s


def is_allowed_to_update(xml_sps, attr_name, attr_new_value):
    """
    Se há uma função de validação associada com o atributo,
    verificar se é permitido atualizar o atributo, dados seus valores
    atual e/ou novo
    """

# Path: packtools/validations.py
# def is_valid_value_for_pid_v2(value):
# VALIDATE_FUNCTIONS = dict((
#     ("scielo_pid_v2", is_valid_value_for_pid_v2),
# ))
# 
# Path: packtools/sps/exceptions.py
# class NotAllowedtoChangeAttributeValueError(Exception):
# class InvalidAttributeValueError(Exception):
# class InvalidValueForOrderError(Exception):
# class SPSLoadToXMLError(Exception):
# class SHA1Error(Exception):
# class SPSConnectionError(Exception):
# class SPSHTTPError(Exception):
# class SPSHTTPForbiddenError(Exception):
# class SPSHTTPResourceNotFoundError(Exception):
# class SPSHTTPInternalServerError(Exception):
# class SPSHTTPBadGatewayError(Exception):
# class SPSHTTPServiceUnavailableError(Exception):
# class SPSDownloadXMLError(Exception):
# class SPSXMLLinkError(Exception):
# class SPSXMLContentError(Exception):
# class SPSXMLFileError(Exception):
# class SPSAssetOrRenditionFileError(Exception):
# class SPSMakePackageFromPathsMissingKeyError(Exception):
# 
# Path: packtools/file_utils.py
# def is_folder(source):
# def is_zipfile(source):
# def xml_files_list(path):
# def files_list(path):
# def read_file(path, encoding="utf-8", mode="r"):
# def read_from_zipfile(zip_path, filename):
# def xml_files_list_from_zipfile(zip_path):
# def files_list_from_zipfile(zip_path):
# def write_file(path, source, mode="w"):
# def create_zip_file(files, zip_name, zip_folder=None):
# def delete_folder(path):
# def create_temp_file(filename, content=None, mode='w'):
# def copy_file(source, target):
# def size(file_path):
# def get_prefix_by_xml_filename(xml_filename):
# def get_file_role(file_path, prefix, pdf_langs):
# def extract_issn_from_zip_uri(zip_uri):
# def is_valid_file(file_path, check_mimetype=False):
# def get_mimetype(file_path):
# def get_filename(file_path):


# Path: packtools/sps/utils/xml_utils.py
import logging
import re
from copy import deepcopy
from lxml import etree
from packtools import validations
from packtools.sps import exceptions
from packtools import file_utils


def parse_value(value):
    value = value.lower()
    if value.isdigit():
        return value.zfill(2)
    if "spe" in value:
        return "spe"
    if "sup" in value:
        return "s"
    return value


def parse_issue(issue):
    issue = " ".join([item for item in issue.split()])
    parts = issue.split()
    parts = [parse_value(item) for item in parts]
    s = "-".join(parts)
    s = s.replace("spe-", "spe")
    s = s.replace("s-", "s")
    if s.endswith("s"):
        s += "0"
    return s


def is_allowed_to_update(xml_sps, attr_name, attr_new_value):
    """
    Se há uma função de validação associada com o atributo,
    verificar se é permitido atualizar o atributo, dados seus valores
    atual e/ou novo
    """

    validate_function = validations.VALIDATE_FUNCTIONS.get(attr_name)