Skip to content

Commit

Permalink
Adding profile list command
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Ferraz committed Feb 9, 2016
1 parent ff11278 commit 514f11d
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 16 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ test.warn:

setup: clean requirements test

cov.badge:
@rm -rf coverage.svg
@coverage-badge -o coverage.svg

dist:
@python setup.py sdist
@python setup.py bdist_wheel --universal
Expand Down
1 change: 1 addition & 0 deletions iprofile/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
from .deactivate import * # noqa
from .delete import * # noqa
from .init import * # noqa
from .list import * # noqa
from .save import * # noqa
from .shell import * # noqa
2 changes: 1 addition & 1 deletion iprofile/cli/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Activate(ICommand):

def run(self, **options):
name = options.get('name')
name = self.slugify_name(options)
profile = get_profile_path(name)

if not os.path.isdir(profile):
Expand Down
6 changes: 3 additions & 3 deletions iprofile/cli/clear.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-

from iprofile import texts
from iprofile.core.decorators import icommand
from iprofile.core.models import ICommand
from iprofile.core.utils import get_ipython_path
from iprofile.core.utils import get_ipython_name
from iprofile import texts
from iprofile.core.utils import get_ipython_path
import click
import os
import shutil
Expand All @@ -16,7 +16,7 @@
class Clear(ICommand):

def run(self, **options):
name = options.pop('name')
name = self.slugify_name(options)

if not os.path.isdir(self.project_path):
return
Expand Down
2 changes: 1 addition & 1 deletion iprofile/cli/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@icommand(help=texts.HELP_DELETE, short_help=texts.HELP_DELETE)
@click.argument('name', required=False)
@click.option('--no-input', is_flag=True)
@click.option('--no-input', is_flag=True, help=texts.HELP_NO_INPUT)
class Delete(ICommand):

def run(self, **options):
Expand Down
4 changes: 2 additions & 2 deletions iprofile/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from iprofile import texts
from iprofile.core.decorators import icommand
from iprofile.core.models import ICommand
from iprofile.core.utils import get_profile_path
from iprofile.core.utils import get_ipython_path
from iprofile.core.utils import get_profile_path
from iprofile.core.utils import get_user_home
from iprofile.core.utils import PROJECT_PATH
import click
Expand All @@ -18,7 +18,7 @@
class Init(ICommand):

def run(self, **options):
name = options.get('name')
name = self.slugify_name(options)
profile_dir = options.get('profile_dir')
profile = get_profile_path(name)

Expand Down
45 changes: 45 additions & 0 deletions iprofile/cli/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-

from iprofile import texts
from iprofile.core.decorators import icommand
from iprofile.core.models import ICommand
from iprofile.core.utils import get_ipython_path
from iprofile.core.utils import get_profile_path
import click
import os


@icommand(help=texts.HELP_LIST, short_help=texts.HELP_LIST)
@click.option('--name-only', is_flag=True, help=texts.HELP_NAME_ONLY)
class List(ICommand):

def run(self, **options):
try:
profiles = filter(
lambda x: os.path.isdir('{0}/{1}'.format(self.project_path, x))
and 'ipython_config.py' in os.listdir('{0}/{1}'.format(
self.project_path, x)), os.listdir('iprofiles'))

if len(profiles) == 0:
self.no_profiles()
return

name_only = options.get('name_only', False)
if not name_only:
self.green(texts.LOG_QTD_PROFILES.format(len(profiles)))

for profile in profiles:
if name_only:
click.echo(profile)
else:
ipython_path, _, _ = get_ipython_path(profile)
click.echo('\nName: {}'.format(profile))
click.echo('IPython profile path:\t{}'.format(
ipython_path))
click.echo('Project profile path:\t{}'.format(
get_profile_path(profile)))
except OSError:
self.no_profiles()

def no_profiles(self):
self.red(texts.ERROR_NO_PROFILES_TO_LIST)
4 changes: 2 additions & 2 deletions iprofile/cli/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from iprofile.core.models import ICommand
from iprofile.core.utils import create_ipython_profile
from iprofile.core.utils import get_ipython_path
from iprofile.core.utils import get_profile_path
from iprofile.core.utils import get_profile_directory
from iprofile.core.utils import get_profile_path
import click
import os
import shutil
Expand All @@ -18,7 +18,7 @@
class Save(ICommand):

def run(self, **options):
name = options.pop('name')
name = self.slugify_name(options, pop=True)
if not name:
for profile in os.listdir(self.project_path):
self.run_for_profile(profile, **options)
Expand Down
2 changes: 1 addition & 1 deletion iprofile/cli/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Shell(ICommand):

def run(self, **options):
name = options.get('name')
name = self.slugify_name(options)

if not name:
IPython.start_ipython(argv=[])
Expand Down
10 changes: 8 additions & 2 deletions iprofile/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
from iprofile.core.utils import echo_red
from iprofile.core.utils import echo_green
from iprofile.core.utils import PROJECT_PATH
from slugify import slugify
import click


class ICommand(object):
project_path = PROJECT_PATH

def __init__(self, _autorun=True, *args, **kwargs):
self.options = kwargs.copy()
if _autorun:
self.run(**self.options)
self.run(**kwargs.copy())
kwargs = {}
super(ICommand, self).__init__(*args, **kwargs)

Expand All @@ -25,6 +25,12 @@ def red(self, text):
def green(self, text):
return echo_green(text)

def slugify_name(self, options, pop=False):
name = options.get('name', None) if not pop else options.pop('name')
if not name:
return None
return slugify(name)


class Command(click.Command):

Expand Down
3 changes: 3 additions & 0 deletions iprofile/texts/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
ERROR_NO_PROFILES_TO_CLEAR = (
"There are no profiles to clear."
)
ERROR_NO_PROFILES_TO_LIST = (
"There are no profiles to list."
)
ERROR_NO_PROFILES_TO_DELETE = (
"There are no profiles to delete."
)
3 changes: 3 additions & 0 deletions iprofile/texts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
HELP_INIT = "Create a new profile without saving it on IPython's path."
HELP_SAVE = "Save changes to IPython's path."
HELP_SHELL = "Open IPython with a given profile name (or the active profile)."
HELP_LIST = "List all profiles."

HELP_NO_SYMLINKS = "Doesn't save the files as symlinks."
HELP_PROFILE_DIR = "Set the IPython profile path."
HELP_SAVE_ALL = "Save all existing profiles."
HELP_NO_INPUT = "Take no input from the command."
HELP_NAME_ONLY = "Show only the names of the profiles."
3 changes: 3 additions & 0 deletions iprofile/texts/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@
LOG_QTD_CLEARED = (
"Successfully cleared {0} profiles."
)
LOG_QTD_PROFILES = (
"{0} profiles were found:"
)
33 changes: 33 additions & 0 deletions tests/test_cli/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-

from iprofile.cli import Create
from iprofile.cli import Delete
from iprofile.cli import List
import shutil

mock_options = {
'name': 'test'
}

mock_options_1 = {
'no_input': True
}

mock_options_2 = {
'name_only': True
}


def test_run():
Create.run(mock_options)
Create.run(mock_options)
List.run({})
List.run(mock_options_2)
Delete.run(mock_options_1)
List.run({})


def test_no_iprofiles_folder():
shutil.move('iprofiles', 'iprofiles2')
List.run({})
shutil.move('iprofiles2', 'iprofiles')

0 comments on commit 514f11d

Please sign in to comment.