Exemple #1
0
    def _get_files_without_assistants(cls, dap, dirname, files):
        folders = set()
        assistants = set()
        assistant_dirs = set(['crt', 'twk', 'prep', 'extra'])

        for f in files:
            # Directories
            if dap._is_dir(f):
                prefix = os.path.join(dirname, 'files', '')
                if f.startswith(prefix):
                    remainder = strip_prefix(f, prefix) # crt/foo/bar/baz
                    name = os.path.join(*remainder.split(os.path.sep)[:2]) # crt/foo
                    folders.add(name)
            else:
                # Assistants
                prefix = os.path.join(dirname, 'assistants', '')
                remainder = strip_prefix(f, prefix)
                for kind in assistant_dirs:
                    if remainder.startswith(kind + os.path.sep):
                        name = strip_suffix(remainder, '.yaml')
                        assistants.add(name)

                # Snippets
                prefix = os.path.join(dirname, 'snippets', '')
                if f.startswith(prefix):
                    name = strip_suffix(strip_prefix(f, dirname + os.path.sep), '.yaml')
                    assistants.add(name)

        return list(folders - assistant_dirs - set(('snippets',)) - assistants)
Exemple #2
0
    def _get_files_without_assistants(cls, dap, dirname, files):
        folders = set()
        assistants = set()
        assistant_dirs = set(['crt', 'twk', 'prep', 'extra'])

        for f in files:
            # Directories
            if dap._is_dir(f):
                prefix = os.path.join(dirname, 'files', '')
                if f.startswith(prefix):
                    remainder = strip_prefix(f, prefix)  # crt/foo/bar/baz
                    name = os.path.join(*remainder.split(
                        os.path.sep)[:2])  # crt/foo
                    folders.add(name)
            else:
                # Assistants
                prefix = os.path.join(dirname, 'assistants', '')
                remainder = strip_prefix(f, prefix)
                for kind in assistant_dirs:
                    if remainder.startswith(kind + os.path.sep):
                        name = strip_suffix(remainder, '.yaml')
                        assistants.add(name)

                # Snippets
                prefix = os.path.join(dirname, 'snippets', '')
                if f.startswith(prefix):
                    name = strip_suffix(strip_prefix(f, dirname + os.path.sep),
                                        '.yaml')
                    assistants.add(name)

        return list(folders - assistant_dirs - set(('snippets', )) -
                    assistants)
Exemple #3
0
 def _format_files(cls, files, kind):
     '''Format the list of files (e. g. assistants or snippets'''
     result = []
     if files:
         result.append('The following {kind} are contained in this DAP:'.format(kind=kind.title()))
         for f in files:
             result.append('* ' + strip_prefix(f, kind).replace(os.path.sep, ' ').strip())
         return '\n'.join(result)
     else:
         return 'No {kind} are contained in this DAP'.format(kind=kind.title())
Exemple #4
0
def _get_assistants_snippets(path, name):
    '''Get Assistants and Snippets for a given DAP name on a given path'''
    result = []
    subdirs = {'assistants': 2, 'snippets': 1} # Values used for stripping leading path tokens

    for loc in subdirs:
        for root, dirs, files in os.walk(os.path.join(path, loc)):
            for filename in [utils.strip_prefix(os.path.join(root, f), path) for f in files]:
                stripped = os.path.sep.join(filename.split(os.path.sep)[subdirs[loc]:])
                if stripped.startswith(os.path.join(name, '')) or stripped == name + '.yaml':
                    result.append(os.path.join('fakeroot', filename))

    return result
Exemple #5
0
    def format_assistants(cls, assistants):
        '''Return formatted assistants from the given list in human readable form.'''
        result = cls._format_files(assistants, 'assistants')

        # Assistant help
        if assistants:
            assistant = strip_prefix(random.choice(assistants), 'assistants').replace(os.path.sep, ' ').strip()
            if len(assistants) == 1:
                string = 'After you install this DAP, you can find help about the Assistant\nby running "da {a} -h" .'
            else:
                string = 'After you install this DAP, you can find help, for example about the Assistant\n"{a}", by running "da {a} -h".'
            result += '\n\n' + string.format(a=assistant)

        return result
Exemple #6
0
 def _format_files(cls, files, kind):
     '''Format the list of files (e. g. assistants or snippets'''
     lines = []
     if files:
         lines.append(
             'The following {kind} are contained in this DAP:'.format(
                 kind=kind.title()))
         for f in files:
             lines.append(
                 '* ' +
                 strip_prefix(f, kind).replace(os.path.sep, ' ').strip())
         return lines
     else:
         return [
             'No {kind} are contained in this DAP'.format(kind=kind.title())
         ]
Exemple #7
0
    def format_assistants_lines(cls, assistants):
        '''Return formatted assistants from the given list in human readable form.'''
        lines = cls._format_files(assistants, 'assistants')

        # Assistant help
        if assistants:
            lines.append('')
            assistant = strip_prefix(random.choice(assistants), 'assistants').replace(os.path.sep, ' ').strip()
            if len(assistants) == 1:
                strings = ['After you install this DAP, you can find help about the Assistant',
                           'by running "da {a} -h" .']
            else:
                strings = ['After you install this DAP, you can find help, for example about the Assistant',
                           '"{a}", by running "da {a} -h".']
            lines.extend([l.format(a=assistant) for l in strings])

        return lines
Exemple #8
0
def _get_assistants_snippets(path, name):
    '''Get Assistants and Snippets for a given DAP name on a given path'''
    result = []
    subdirs = {
        'assistants': 2,
        'snippets': 1
    }  # Values used for stripping leading path tokens

    for loc in subdirs:
        for root, dirs, files in os.walk(os.path.join(path, loc)):
            for filename in [
                    utils.strip_prefix(os.path.join(root, f), path)
                    for f in files
            ]:
                stripped = os.path.sep.join(
                    filename.split(os.path.sep)[subdirs[loc]:])
                if stripped.startswith(os.path.join(
                        name, '')) or stripped == name + '.yaml':
                    result.append(os.path.join('fakeroot', filename))

    return result
Exemple #9
0
    def format_assistants_lines(cls, assistants):
        '''Return formatted assistants from the given list in human readable form.'''
        lines = cls._format_files(assistants, 'assistants')

        # Assistant help
        if assistants:
            lines.append('')
            assistant = strip_prefix(random.choice(assistants),
                                     'assistants').replace(os.path.sep,
                                                           ' ').strip()
            if len(assistants) == 1:
                strings = [
                    'After you install this DAP, you can find help about the Assistant',
                    'by running "da {a} -h" .'
                ]
            else:
                strings = [
                    'After you install this DAP, you can find help, for example about the Assistant',
                    '"{a}", by running "da {a} -h".'
                ]
            lines.extend([l.format(a=assistant) for l in strings])

        return lines
Exemple #10
0
 def test_fails(self, inp, prefix):
     with pytest.raises(TypeError) as e:
         utils.strip_prefix(inp, prefix)
Exemple #11
0
 def test_strip_regex(self, inp, prefix, out):
     assert utils.strip_prefix(inp, prefix, regex=True) == out
Exemple #12
0
 def test_strip_noregex(self, inp, prefix, out):
     assert utils.strip_prefix(inp, prefix) == out
Exemple #13
0
 def test_fails(self, inp, prefix):
     with pytest.raises(TypeError) as e:
         utils.strip_prefix(inp, prefix)
Exemple #14
0
 def test_strip_regex(self, inp, prefix, out):
     assert utils.strip_prefix(inp, prefix, regex=True) == out
Exemple #15
0
 def test_strip_noregex(self, inp, prefix, out):
     assert utils.strip_prefix(inp, prefix) == out