<commit_before>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import os
import sys
import tempfile
import urllib
import posixpath

from robot.errors import FrameworkError
from robottypes import is_str, is_list, unic, is_integer
from normalizing import normpath


def get_doc(obj):
    try:
        doc = obj.__doc__
    except AttributeError:
        doc = ''
    if not doc:
        return ''
    return '\n'.join([ line.strip() for line in doc.strip().splitlines() ])


def printable_name(string, code_style=False):
    """Generates and returns printable name from the given string.

    Examples:
    'simple'           -> 'Simple'
    'name with spaces' -> 'Name With Spaces'
    'more   spaces'    -> 'More Spaces'
    'Cases AND spaces' -> 'Cases AND Spaces'
    ''                 -> ''
    
    If 'code_style' is True:
    
    'mixedCAPSCamel'   -> 'Mixed CAPS Camel'
    'camelCaseName'    -> 'Camel Case Name'
    'under_score_name' -> 'Under Score Name'
    'under_and space'  -> 'Under And Space'
    'miXed_CAPS_nAMe'  -> 'MiXed CAPS NAMe'
    ''                 -> ''
    """
    if code_style:
        string = string.replace('_', ' ')
    parts = string.split()
    if len(parts) == 0:
        return ''
    elif len(parts) == 1 and code_style:
        parts = _splitCamelCaseString(parts[0])
    parts = [ part[0].upper() + part[1:] for part in parts if part != '' ]
    return ' '.join(parts)


def _splitCamelCaseString(string):
    parts = []
    current_part = []          
    string = ' ' + string + ' '  # extra spaces make going through string easier
    for i in range(1, len(string)-1):
        # on 1st/last round prev/next is ' ' and char is 1st/last real char
        prev, char, next = string[i-1:i+2]  
        if _isWordBoundary(prev, char, next):
            parts.append(''.join(current_part))
            current_part = [ char ]
        else:
            current_part.append(char)
    parts.append(''.join(current_part))   # append last part
    return parts


def _isWordBoundary(prev, char, next):
    if char.isupper():
        return (prev.islower() or next.islower()) and prev.isalnum()
    if char.isdigit():
        return prev.isalpha()
    return prev.isdigit()


def cygpath(path, options='-wa'):
    """Converts given path using Cygwin command 'cygpath'.
    
    Default options convert unixy path to an absolute Windows path.
    
    Returns path unmodified if not in Cygwin.
    """
    if 'cygwin' not in sys.platform:
        return path
    output = os.popen('cygpath %s "%s"' % (options, path))
    winpath = output.read().splitlines()[0]
    output.close()
    return winpath
 

def get_temp_dir(extrapath=None):
    tempdir = os.path.normpath(tempfile.gettempdir())
    if not os.path.isdir(tempdir):
        raise EnvironmentError("Temp directory '%s' does not exist" % tempdir)
    if extrapath is not None:
        tempdir = os.path.join(tempdir, extrapath)
        # make sure there's no file with the same name as tempdir
        if os.path.isfile(tempdir):
            i = 1
            while os.path.isfile('%s%d' % (tempdir, i)):
                i += 1
            tempdir = '%s%d' % (tempdir, i)
        if not os.path.exists(tempdir):
            os.mkdir(tempdir)
    return tempdir


def plural_or_not(list_or_int):
    if is_integer(list_or_int):
        return list_or_int != 1 and "s" or ""
    else:
        return len(list_or_int) != 1 and "s" or ""
    

def seq2str(sequence, quote="'", sep=', ', lastsep=' and ', quote_all=False):
    """Returns sequence in format 'item 1', 'item 2' and 'item 3' """
    def elem(string):
        if not is_str and not quote_all: 
            return unic(string)
        return quote + unic(string) + quote

    if not is_list(sequence):
        return elem(sequence)
    elif len(sequence) == 0:
        return ''
    elif len(sequence) == 1:
        return elem(sequence[0])
    ret = []
    for i in range(len(sequence)):
        if i + 2 == len(sequence):
            ret.append(elem(sequence[i]) + lastsep + elem(sequence[i+1]))
            break
        ret.append(elem(sequence[i]))
    return sep.join(ret)


def seq2str2(sequence):
    """Returns sequence in format [ item 1 | item 2 | ... ] """
    if not sequence:
        return '[ ]'
    return '[ %s ]' % ' | '.join([unic(item) for item in sequence])


def printable_name_from_path(path):
    """Create suite name from given path that points to file or directory.
    
    Examples:
        '/tmp/tests.py'         -> 'Tests'
        '/var/data/my_tests/    -> 'My Tests'
        'c:\path\my_tests.html' -> 'My Tests'
        'd:\MY TESTS.HTML'      -> 'MY TESTS'
        'e:\myTestCases.html    -> 'My Test Cases'
    """
    # Get name of the file/dir without leading path and possible extension
    name = os.path.splitext(os.path.basename(os.path.normpath(path)))[0]
    name = _remove_prefix(name)
    return printable_name(name, code_style=True)

def _remove_prefix(name):
    return name.split('__', 1)[-1]

def get_directory(path):
    """Returns the directory part of the given path.
    
    If path already is a directory returns it as is, otherwise returns the
    directory containing the file
    """
    if path.lower().startswith('http://'):
        return posixpath.dirname(path)
    path = normpath(path)
    if os.path.isdir(path):
        return path
    return os.path.dirname(path)
    

def get_link_path(target, base):
    """Returns a relative path to a target from a base. If base is an existing
    file, then its parent directory is considered. Otherwise, base is assumed
    to be a directory.

    Rationale: os.path.relpath is not available before Python 2.6
    """

    target = normpath(target)
    base = normpath(base)
    if os.path.isfile(base):
        base = os.path.dirname(base)
    if base == target:
        return urllib.pathname2url(os.path.basename(target))
    base_drive, base_path = os.path.splitdrive(base)<commit_msg>handle unicode in link target<commit_after>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import os
import sys
import tempfile
import urllib
import posixpath

from robot.errors import FrameworkError
from robottypes import is_str, is_list, unic, is_integer
from normalizing import normpath


def get_doc(obj):
    try:
        doc = obj.__doc__
    except AttributeError:
        doc = ''
    if not doc:
        return ''
    return '\n'.join([ line.strip() for line in doc.strip().splitlines() ])


def printable_name(string, code_style=False):
    """Generates and returns printable name from the given string.

    Examples:
    'simple'           -> 'Simple'
    'name with spaces' -> 'Name With Spaces'
    'more   spaces'    -> 'More Spaces'
    'Cases AND spaces' -> 'Cases AND Spaces'
    ''                 -> ''
    
    If 'code_style' is True:
    
    'mixedCAPSCamel'   -> 'Mixed CAPS Camel'
    'camelCaseName'    -> 'Camel Case Name'
    'under_score_name' -> 'Under Score Name'
    'under_and space'  -> 'Under And Space'
    'miXed_CAPS_nAMe'  -> 'MiXed CAPS NAMe'
    ''                 -> ''
    """
    if code_style:
        string = string.replace('_', ' ')
    parts = string.split()
    if len(parts) == 0:
        return ''
    elif len(parts) == 1 and code_style:
        parts = _splitCamelCaseString(parts[0])
    parts = [ part[0].upper() + part[1:] for part in parts if part != '' ]
    return ' '.join(parts)


def _splitCamelCaseString(string):
    parts = []
    current_part = []          
    string = ' ' + string + ' '  # extra spaces make going through string easier
    for i in range(1, len(string)-1):
        # on 1st/last round prev/next is ' ' and char is 1st/last real char
        prev, char, next = string[i-1:i+2]  
        if _isWordBoundary(prev, char, next):
            parts.append(''.join(current_part))
            current_part = [ char ]
        else:
            current_part.append(char)
    parts.append(''.join(current_part))   # append last part
    return parts


def _isWordBoundary(prev, char, next):
    if char.isupper():
        return (prev.islower() or next.islower()) and prev.isalnum()
    if char.isdigit():
        return prev.isalpha()
    return prev.isdigit()


def cygpath(path, options='-wa'):
    """Converts given path using Cygwin command 'cygpath'.
    
    Default options convert unixy path to an absolute Windows path.
    
    Returns path unmodified if not in Cygwin.
    """
    if 'cygwin' not in sys.platform:
        return path
    output = os.popen('cygpath %s "%s"' % (options, path))
    winpath = output.read().splitlines()[0]
    output.close()
    return winpath
 

def get_temp_dir(extrapath=None):
    tempdir = os.path.normpath(tempfile.gettempdir())
    if not os.path.isdir(tempdir):
        raise EnvironmentError("Temp directory '%s' does not exist" % tempdir)
    if extrapath is not None:
        tempdir = os.path.join(tempdir, extrapath)
        # make sure there's no file with the same name as tempdir
        if os.path.isfile(tempdir):
            i = 1
            while os.path.isfile('%s%d' % (tempdir, i)):
                i += 1
            tempdir = '%s%d' % (tempdir, i)
        if not os.path.exists(tempdir):
            os.mkdir(tempdir)
    return tempdir


def plural_or_not(list_or_int):
    if is_integer(list_or_int):
        return list_or_int != 1 and "s" or ""
    else:
        return len(list_or_int) != 1 and "s" or ""
    

def seq2str(sequence, quote="'", sep=', ', lastsep=' and ', quote_all=False):
    """Returns sequence in format 'item 1', 'item 2' and 'item 3' """
    def elem(string):
        if not is_str and not quote_all: 
            return unic(string)
        return quote + unic(string) + quote

    if not is_list(sequence):
        return elem(sequence)
    elif len(sequence) == 0:
        return ''
    elif len(sequence) == 1:
        return elem(sequence[0])
    ret = []
    for i in range(len(sequence)):
        if i + 2 == len(sequence):
            ret.append(elem(sequence[i]) + lastsep + elem(sequence[i+1]))
            break
        ret.append(elem(sequence[i]))
    return sep.join(ret)


def seq2str2(sequence):
    """Returns sequence in format [ item 1 | item 2 | ... ] """
    if not sequence:
        return '[ ]'
    return '[ %s ]' % ' | '.join([unic(item) for item in sequence])


def printable_name_from_path(path):
    """Create suite name from given path that points to file or directory.
    
    Examples:
        '/tmp/tests.py'         -> 'Tests'
        '/var/data/my_tests/    -> 'My Tests'
        'c:\path\my_tests.html' -> 'My Tests'
        'd:\MY TESTS.HTML'      -> 'MY TESTS'
        'e:\myTestCases.html    -> 'My Test Cases'
    """
    # Get name of the file/dir without leading path and possible extension
    name = os.path.splitext(os.path.basename(os.path.normpath(path)))[0]
    name = _remove_prefix(name)
    return printable_name(name, code_style=True)

def _remove_prefix(name):
    return name.split('__', 1)[-1]

def get_directory(path):
    """Returns the directory part of the given path.
    
    If path already is a directory returns it as is, otherwise returns the
    directory containing the file
    """
    if path.lower().startswith('http://'):
        return posixpath.dirname(path)
    path = normpath(path)
    if os.path.isdir(path):
        return path
    return os.path.dirname(path)
    

def get_link_path(target, base):
    """Returns a relative path to a target from a base. If base is an existing
    file, then its parent directory is considered. Otherwise, base is assumed
    to be a directory.

    Rationale: os.path.relpath is not available before Python 2.6
    """
    target = target.encode('UTF-8')
    target = normpath(target)
    base = normpath(base)
    if os.path.isfile(base):
        base = os.path.dirname(base)
    if base == target:
        return urllib.pathname2url(os.path.basename(target))
    base_drive, base_path = os.path.splitdrive(base)
{'fraction_non_alphanumeric': 0.11796677639133431, 'fraction_numerical': 0.007272448901477455, 'mean_word_length': 2.5336759534757913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '2937812', 'n_tokens_mistral': 4167, 'n_tokens_neox': 4053, 'n_words': 1261}