Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Fixed stripping prefixes and suffixes in devassistant.dapi
Browse files Browse the repository at this point in the history
  • Loading branch information
tradej committed Apr 7, 2015
1 parent e526d98 commit 8b73c16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 10 additions & 9 deletions devassistant/dapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from . import licenses, platforms
from devassistant.exceptions import DapFileError, DapMetaError, DapInvalid
from devassistant.logger import logger
from devassistant.utils import strip_prefix, strip_suffix


class DapProblem(object):
Expand Down Expand Up @@ -107,7 +108,7 @@ def _format_files(cls, files, kind):
if files:
result.append('The following {kind} are contained in this DAP:'.format(kind=kind.title()))
for f in files:
result.append('* ' + f.lstrip(kind).replace(os.path.sep, ' ').strip())
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())
Expand All @@ -119,7 +120,7 @@ def format_assistants(cls, assistants):

# Assistant help
if assistants:
assistant = random.choice(assistants).lstrip('assistants').replace(os.path.sep, ' ').strip()
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:
Expand Down Expand Up @@ -393,22 +394,22 @@ def _get_files_without_assistants(cls, dap, dirname, files):
if dap._is_dir(f):
prefix = os.path.join(dirname, 'files', '')
if f.startswith(prefix):
remainder = f.lstrip(prefix) # crt/foo/bar/baz
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 = f.lstrip(prefix)
remainder = strip_prefix(f, prefix)
for kind in assistant_dirs:
if remainder.startswith(kind + os.path.sep):
name = remainder.rstrip('.yaml')
name = strip_suffix(remainder, '.yaml')
assistants.add(name)

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

return list(folders - assistant_dirs - set(('snippets',)) - assistants)
Expand Down Expand Up @@ -489,12 +490,12 @@ def _stripped_files(self):
@property
def assistants(self):
'''Get all assistants in this DAP'''
return [f.rstrip('.yaml') for f in self._stripped_files if self._assistants_pattern.match(f)]
return [strip_suffix(f, '.yaml') for f in self._stripped_files if self._assistants_pattern.match(f)]

@property
def snippets(self):
'''Get all snippets in this DAP'''
return [f.rstrip('.yaml') for f in self._stripped_files if self._snippets_pattern.match(f)]
return [strip_suffix(f, '.yaml') for f in self._stripped_files if self._snippets_pattern.match(f)]

@property
def assistants_and_snippets(self):
Expand All @@ -505,7 +506,7 @@ def icons(self, strip_ext=False):
'''Get all icons in this DAP, optionally strip extensions'''
result = [f for f in self._stripped_files if self._icons_pattern.match(f)]
if strip_ext:
result = [f.rstrip('.' + self._icons_ext) for f in result]
result = [strip_suffix(f, '.' + self._icons_ext) for f in result]

return result

Expand Down
14 changes: 14 additions & 0 deletions devassistant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,17 @@ def bold(message):
def unexpanduser(path):
"""Replaces expanded ~ back with ~"""
return path.replace(os.path.expanduser('~'), '~')

def strip_prefix(string, prefix):
"""Strip the prefix from the string"""
if string.startswith(prefix):
return string[len(prefix):]
else:
return string

def strip_suffix(string, suffix):
"""Strip the suffix from the string"""
if string.endswith(suffix):
return string[:-len(suffix)]
else:
return string

0 comments on commit 8b73c16

Please sign in to comment.