# Path: adsft/utils.py
# def get_filenames(file_string):
#     """convert passed string containing one or more files to an array of files
# 
#     file_string could be a sigle file, a simple comma separated list of files
#     or it could include a comman in either the filename or the pathname
#     we can't use a comma as a delimeter
#     instead, since all paths are absolute, we use the first two directory names
#     in the absolution path as a delimter
#     example simple input:
#     /proj/ads/fulltext/sources/A+A/backdata/2003/17/aah3724/aah3724.right.html,/proj/ads/fulltext/sources/A+A/backdata/2003/17/aah3724/tableE.1.html
#     example input with comman in filename:
#     /proj/ads/fulltext/sources/downloads/cache/POS/pos.sissa.it//archive/conferences/075/001/BHs,%20GR%20and%20Strings_001.pdf
#     """
#     if file_string[0] != '/':
#         raise ValueError('expected absolute pathname to start with / character: {}'.format(file_string))
#     second_slash_index = file_string.index('/', 1)
#     third_slash_index = file_string.index('/', second_slash_index+1)
#     prefix = file_string[:third_slash_index+1]
# 
#     # split input string over prefix delimeter, add prefix back to string
#     files = [prefix+f for f in file_string.split(prefix) if f]
#     # remove trailing commas, they are actual delimiters
#     for i in range(0, len(files)):
#         if files[i][-1] == ',':
#             files[i] = files[i][:-1]
# 
#     return files


import sys
import os
import json
import ptree
import traceback
from . import utils
from stat import ST_MTIME
from datetime import datetime
from dateutil.parser import parse
from adsft.utils import get_filenames
from adsputils import setup_logging, load_config
    :return: dictionary containing two lists. One for PDF files and the other
    for normal files. It adds the extra keyword UPDATE which explains why the
    extraction of the full text is required.
    """

    NEEDS_UPDATE = ["MISSING_FULL_TEXT", "DIFFERING_FULL_TEXT", "STALE_CONTENT",
                    "STALE_META", "NOT_EXTRACTED_BEFORE", "FORCE_TO_EXTRACT",
                    "FORCE_TO_SEND"]

    publish_list_of_standard_dictionaries = []
    publish_list_of_pdf_dictionaries = []

    for message in message_list:

        # message should be a dictionary
        if 'UPDATE' in message \
                and message['UPDATE'] == 'FORCE_TO_EXTRACT':
            update = 'FORCE_TO_EXTRACT'
        elif 'UPDATE' in message \
                and message['UPDATE'] == 'FORCE_TO_SEND':
            update = 'FORCE_TO_SEND'
        elif meta_output_exists(message, extract_path):
            meta_content = load_meta_file(message, extract_path)
            update = meta_needs_update(message, meta_content,
                                       extract_path)
        else:
            logger.debug('No existing meta file')
            update = 'NOT_EXTRACTED_BEFORE'

        # only check the first filename

# Path: adsft/utils.py
# def get_filenames(file_string):
#     """convert passed string containing one or more files to an array of files
# 
#     file_string could be a sigle file, a simple comma separated list of files
#     or it could include a comman in either the filename or the pathname
#     we can't use a comma as a delimeter
#     instead, since all paths are absolute, we use the first two directory names
#     in the absolution path as a delimter
#     example simple input:
#     /proj/ads/fulltext/sources/A+A/backdata/2003/17/aah3724/aah3724.right.html,/proj/ads/fulltext/sources/A+A/backdata/2003/17/aah3724/tableE.1.html
#     example input with comman in filename:
#     /proj/ads/fulltext/sources/downloads/cache/POS/pos.sissa.it//archive/conferences/075/001/BHs,%20GR%20and%20Strings_001.pdf
#     """
#     if file_string[0] != '/':
#         raise ValueError('expected absolute pathname to start with / character: {}'.format(file_string))
#     second_slash_index = file_string.index('/', 1)
#     third_slash_index = file_string.index('/', second_slash_index+1)
#     prefix = file_string[:third_slash_index+1]
# 
#     # split input string over prefix delimeter, add prefix back to string
#     files = [prefix+f for f in file_string.split(prefix) if f]
#     # remove trailing commas, they are actual delimiters
#     for i in range(0, len(files)):
#         if files[i][-1] == ',':
#             files[i] = files[i][:-1]
# 
#     return files


# Path: adsft/checker.py
import sys
import os
import json
import ptree
import traceback
from . import utils
from stat import ST_MTIME
from datetime import datetime
from dateutil.parser import parse
from adsft.utils import get_filenames
from adsputils import setup_logging, load_config

    :return: dictionary containing two lists. One for PDF files and the other
    for normal files. It adds the extra keyword UPDATE which explains why the
    extraction of the full text is required.
    """

    NEEDS_UPDATE = ["MISSING_FULL_TEXT", "DIFFERING_FULL_TEXT", "STALE_CONTENT",
                    "STALE_META", "NOT_EXTRACTED_BEFORE", "FORCE_TO_EXTRACT",
                    "FORCE_TO_SEND"]

    publish_list_of_standard_dictionaries = []
    publish_list_of_pdf_dictionaries = []

    for message in message_list:

        # message should be a dictionary
        if 'UPDATE' in message \
                and message['UPDATE'] == 'FORCE_TO_EXTRACT':
            update = 'FORCE_TO_EXTRACT'
        elif 'UPDATE' in message \
                and message['UPDATE'] == 'FORCE_TO_SEND':
            update = 'FORCE_TO_SEND'
        elif meta_output_exists(message, extract_path):
            meta_content = load_meta_file(message, extract_path)
            update = meta_needs_update(message, meta_content,
                                       extract_path)
        else:
            logger.debug('No existing meta file')
            update = 'NOT_EXTRACTED_BEFORE'

        # only check the first filename

        ft = get_filenames(message['ft_source'])[0]