<commit_before>'''
BSD Licence
Copyright (c) 2012, Science & Technology Facilities Council (STFC)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
    * Neither the name of the Science & Technology Facilities Council (STFC) 
        nor the names of its contributors may be used to endorse or promote 
        products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Created on 5 May 2012

@author: <NAME>
'''
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom
from os_engine import get_mimetype
from osquery import URL_REL_DEFAULT, URL_INDEX_OFFSET_DEFAULT,\
    URL_PAGE_OFFSET_DEFAULT

MAX_OS_SHORT_NAME_LEN = 16
MAX_OS_LONG_NAME_LEN = 48
MAX_OS_TAGS_LEN = 256
MAX_OS_DESCRIPTION_LEN = 1024
MAX_OS_DEVELOPER_LEN = 64
MAX_OS_ATTRIBUTION_LEN = 256

SYNDACATION_OPEN = 'open'
SYNDACATION_LIMITED = 'limited'
SYNDACATION_PRIVATE = 'private'
SYNDACATION_CLOSED = 'closed'
OS_SYNDACATION_RIGHT = [SYNDACATION_OPEN, SYNDACATION_LIMITED, SYNDACATION_PRIVATE, SYNDACATION_CLOSED]
OS_SYNDACATION_RIGHT_DEFAULT = SYNDACATION_OPEN

OS_ADULT_CONTENT_DEFAULT = False
OS_INPUT_ENCODING_DEFAULT = 'UTF-8'
OS_OUTPUT_ENCODING_DEFAULT = 'UTF-8' 

class OpenSearchRequest(object):
    '''
    classdocs
    '''

    NAMESPACE = 'http://a9.com/-/spec/opensearch/1.1/'
    PREFIX = ''
    ROOT_TAG = 'OpenSearchDescription'

    def __init__(self, query, responses, os_short_name, os_description, \
                 os_contact = None, os_tags = None, os_long_name = None, \
                 os_image = [], os_developer = None, os_attribution = None, \
                 os_syndacation_right = None, os_adult_content = None, \
                 os_language = ['*'], os_input_encoding = [OS_INPUT_ENCODING_DEFAULT], \
                 os_output_encoding = [OS_OUTPUT_ENCODING_DEFAULT]):
        '''
            @param query: an OSQuery implementation 
            @param responses: a list of OSResponse instances            
            @param oshelper: a list of OSResponse implementations 
        '''
        self.query = query
        self.responses = responses
        self.os_syndacation_right = None
        self.os_adult_content = True

        if os_description:
            self.os_description = os_description[:MAX_OS_DESCRIPTION_LEN]
        
        if os_short_name:
            self.os_short_name = os_short_name[:MAX_OS_SHORT_NAME_LEN]
                    
        #Should check that is an email format
        if os_contact:        
            self.os_contact = os_contact
         
        if os_tags:
            self.os_tags = os_tags[:MAX_OS_TAGS_LEN]

        if os_long_name:
            self.os_long_name = os_long_name[:MAX_OS_LONG_NAME_LEN]

        if os_developer:
            self.os_developer = os_developer[:MAX_OS_DEVELOPER_LEN]

        if os_attribution:
            self.os_attribution = os_attribution[:MAX_OS_ATTRIBUTION_LEN]            
            
        if os_syndacation_right and os_syndacation_right in OS_SYNDACATION_RIGHT:
            self.os_syndacation_right = os_syndacation_right            
            
        if os_adult_content and os_adult_content in ['false', 'FALSE', '0', 'no', 'NO']:
            self.os_adult_content = False                        
            
        self.os_image = os_image
        self.os_language = os_language
        self.os_input_encoding = os_input_encoding        
        self.os_output_encoding = os_output_encoding         
        
    def getDescription(self, ospath):
        top = Element(OpenSearchRequest.ROOT_TAG)
        top.set("xmlns", OpenSearchRequest.NAMESPACE)
       
        shortName = SubElement(top, 'ShortName')
        shortName.text = self.os_short_name
        
        description = SubElement(top, 'Description')
        description.text = self.os_description
        
        if hasattr(self, 'os_tags'):<commit_msg>Incomplete - # 22518: The description is broken  http://team.ceda.ac.uk/trac/ceda/ticket/22518<commit_after>'''
BSD Licence
Copyright (c) 2012, Science & Technology Facilities Council (STFC)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
    * Neither the name of the Science & Technology Facilities Council (STFC) 
        nor the names of its contributors may be used to endorse or promote 
        products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Created on 5 May 2012

@author: <NAME>
'''
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom
from os_engine import get_mimetype
from osquery import URL_REL_DEFAULT, URL_INDEX_OFFSET_DEFAULT,\
    URL_PAGE_OFFSET_DEFAULT

MAX_OS_SHORT_NAME_LEN = 16
MAX_OS_LONG_NAME_LEN = 48
MAX_OS_TAGS_LEN = 256
MAX_OS_DESCRIPTION_LEN = 1024
MAX_OS_DEVELOPER_LEN = 64
MAX_OS_ATTRIBUTION_LEN = 256

SYNDACATION_OPEN = 'open'
SYNDACATION_LIMITED = 'limited'
SYNDACATION_PRIVATE = 'private'
SYNDACATION_CLOSED = 'closed'
OS_SYNDACATION_RIGHT = [SYNDACATION_OPEN, SYNDACATION_LIMITED, SYNDACATION_PRIVATE, SYNDACATION_CLOSED]
OS_SYNDACATION_RIGHT_DEFAULT = SYNDACATION_OPEN

OS_ADULT_CONTENT_DEFAULT = False
OS_INPUT_ENCODING_DEFAULT = 'UTF-8'
OS_OUTPUT_ENCODING_DEFAULT = 'UTF-8' 

class OpenSearchRequest(object):
    '''
    classdocs
    '''

    NAMESPACE = 'http://a9.com/-/spec/opensearch/1.1/'
    PREFIX = ''
    ROOT_TAG = 'OpenSearchDescription'

    def __init__(self, query, responses, os_short_name, os_description, \
                 os_contact = None, os_tags = None, os_long_name = None, \
                 os_image = [], os_developer = None, os_attribution = None, \
                 os_syndacation_right = None, os_adult_content = None, \
                 os_language = ['*'], os_input_encoding = [OS_INPUT_ENCODING_DEFAULT], \
                 os_output_encoding = [OS_OUTPUT_ENCODING_DEFAULT]):
        '''
            @param query: an OSQuery implementation 
            @param responses: a list of OSResponse instances            
            @param oshelper: a list of OSResponse implementations 
        '''
        self.query = query
        self.responses = responses
        self.os_syndacation_right = None
        
        # should be set to True but because of 
        # http://code.google.com/p/gdata-python-client/issues/detail?id=611
        # we cannot (for now)
        self.os_adult_content = '1'

        if os_description:
            self.os_description = os_description[:MAX_OS_DESCRIPTION_LEN]
        
        if os_short_name:
            self.os_short_name = os_short_name[:MAX_OS_SHORT_NAME_LEN]
                    
        #Should check that is an email format
        if os_contact:        
            self.os_contact = os_contact
         
        if os_tags:
            self.os_tags = os_tags[:MAX_OS_TAGS_LEN]

        if os_long_name:
            self.os_long_name = os_long_name[:MAX_OS_LONG_NAME_LEN]

        if os_developer:
            self.os_developer = os_developer[:MAX_OS_DEVELOPER_LEN]

        if os_attribution:
            self.os_attribution = os_attribution[:MAX_OS_ATTRIBUTION_LEN]            
            
        if os_syndacation_right and os_syndacation_right in OS_SYNDACATION_RIGHT:
            self.os_syndacation_right = os_syndacation_right            
            
        if os_adult_content and os_adult_content in ['false', 'FALSE', '0', 'no', 'NO']:
            # should be set to False but because of 
            # http://code.google.com/p/gdata-python-client/issues/detail?id=611
            # we cannot (for now)            
            self.os_adult_content = '0'                        
            
        self.os_image = os_image
        self.os_language = os_language
        self.os_input_encoding = os_input_encoding        
        self.os_output_encoding = os_output_encoding         
        
    def getDescription(self, ospath):
        top = Element(OpenSearchRequest.ROOT_TAG)
        top.set("xmlns", OpenSearchRequest.NAMESPACE)
       
        shortName = SubElement(top, 'ShortName')
        shortName.text = self.os_short_name
        
        description = SubElement(top, 'Description')
        description.text = self.os_description
        
        if hasattr(self, 'os_tags'):
{'fraction_non_alphanumeric': 0.06235933983495874, 'fraction_numerical': 0.007501875468867217, 'mean_word_length': 2.8321954725116782, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2870699', 'n_tokens_mistral': 3230, 'n_tokens_neox': 2979, 'n_words': 901}