Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
technocake committed Jul 11, 2019
1 parent 593818a commit 6662f94
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 60 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -37,9 +37,9 @@ Do the same as above, but **open git bash as Administrator**
#### Linux - User Site install
On linux, the recommended way to install goto is by `pip install --user magicgoto`

This requires that you have your User site bin in your path.
Usually this is `~/.local/bin`. So adding `PATH="${HOME}/.local/bin:$PATH"` to
your rcfile should do the trick in most cases.
> This requires that you have your User site bin in your path.
> Usually this is `~/.local/bin`. So adding `PATH="${HOME}/.local/bin:$PATH"` to
> your rcfile should do the trick in most cases.

### Commands
Expand Down Expand Up @@ -125,6 +125,7 @@ You tell Goto with the command:




#### How does Goto know which shortcuts there are in the project?

Define them once, and use them a thousand times. By this command:
Expand Down
7 changes: 3 additions & 4 deletions bin/goto
Expand Up @@ -7,7 +7,7 @@


function _goto_main {
local PROJECT PROJECTFILE URI
local PROJECT URI

# If goto is run in sourced mode, contiune.
# Else: print warning. NO EXIT statement.
Expand All @@ -22,7 +22,6 @@ function _goto_main {


PROJECT=$(cat "${GOTOPATH}/active-project")
PROJECTFILE="${GOTOPATH}/projects/${PROJECT}.json"

# Catching deactivated state
# TODO: make a better way to print to stderr
Expand Down Expand Up @@ -59,7 +58,7 @@ function _goto_main {
&& ! "$1" =~ ^(list|help|add|rm|mv|rename|update|copy|subl|vscode|intelij|idea|show|cd|open)$ \
&& ! "$1" =~ ^"--" \
&& ! "$1" =~ ^"-" \
]]; then
]]; then
# if run like: goto <magicword>
URI=$(goto show "$1")

Expand All @@ -79,7 +78,7 @@ function _goto_main {

# General case
if [ -n "$(command -v the_real_goto.py)" ]; then
the_real_goto.py "$PROJECTFILE" "$@"
the_real_goto.py "$PROJECT" "$@"
else
echo "Error the_real_goto.py not found" >&2
return 1
Expand Down
28 changes: 15 additions & 13 deletions bin/project
Expand Up @@ -2,8 +2,6 @@
. _gotoutils # load common utility functions
load_gotopath

STATEDIR="$GOTOPATH"


_usage() {
echo "Usage: project [add <projectname>] | [list]"
Expand All @@ -16,31 +14,35 @@ _usage() {
}

_addproject() {
touch "$STATEDIR/projects/$1"
local PROJECT_FOLDER="$GOTOPATH/projects/$1"
if [[ ! -d $PROJECT_FOLDER ]]; then
mkdir "$PROJECT_FOLDER"
else
echo "Project $1 already exists"
fi
}

_rmproject() {
if prompt "Are you sure you want to delete project $1? [y|n]: "; then
rm "$STATEDIR/projects/$1"
rm "$STATEDIR/projects/$1.json"
echo "Removed project $2"
rm -rf "$GOTOPATH/projects/$1"
echo "Removed project $1"
else
echo
echo "Aborting delete of project $1"
fi
}

_listprojects() {
ls "$STATEDIR/projects" | grep -v .json
ls "$GOTOPATH/projects/"
}

_deactivate_project() {
echo "" > "$STATEDIR/active-project"
echo "" > "$GOTOPATH/active-project"
}

_changeproject() {
if [ -f "$STATEDIR/projects/$1" ]; then
echo "$1" > "$STATEDIR/active-project"
if [ -d "$GOTOPATH/projects/$1" ]; then
echo "$1" > "$GOTOPATH/active-project"
echo "active project is now: $1"
else
echo "Warning - tried to change to a non existing project."
Expand All @@ -52,7 +54,7 @@ _changeproject() {
}

_showactiveproject() {
cat "$STATEDIR/active-project"
cat "$GOTOPATH/active-project"
}

# ---------------------------
Expand Down Expand Up @@ -90,9 +92,9 @@ if [ "$1" = "rm" ]
then
if [ "$2" = "" ] ; then
_usage
exit 1
else
_rmproject "$2" "$3"
# TODO if active project == removed what to do? _changeproject "$2"
_rmproject "$2"
_deactivate_project
exit 0
fi
Expand Down
15 changes: 2 additions & 13 deletions goto/gotomagic/git.py
Expand Up @@ -3,11 +3,7 @@
import sys
import git
from .magic import GotoMagic


# HACK HACK
GOTOPATH = os.environ.get('GOTOPATH', os.path.expanduser('~/.goto'))

from . import utils


class GitMagic():
Expand All @@ -25,13 +21,7 @@ class GitMagic():

def __init__(self, project):
""" Loads json from jfile """
shared_file = os.path.join(
GOTOPATH, 'projects', 'shared', project, '{}.json'.format(project)
)

shared_folder = os.path.dirname(shared_file)
if not os.path.exists(shared_folder):
os.makedirs(shared_folder)
utils.create_project_folder(project, 'shared')

if not is_git_repo(shared_folder):
self.repo = git.Repo.init(shared_folder)
Expand All @@ -55,7 +45,6 @@ def share(self):
gotohub.pull('master')
gotohub.push('master')


def save(self):
""" Saves the magic to jsonfile jfile """
self.magic.save()
Expand Down
23 changes: 18 additions & 5 deletions goto/gotomagic/magic.py
Expand Up @@ -2,13 +2,19 @@
"""
Magic is stored here.
"""
from __future__ import absolute_import, unicode_literals
from builtins import dict, str # redefine dict and str to be py3-like in py2.
# http://johnbachman.net/building-a-python-23-compatible-unicode-sandwich.html

import json
import codecs
import os

from . import text
from .text import print_text
from .text import GotoWarning
from .. import settings
from . import utils


class GotoMagic():
Expand All @@ -21,10 +27,15 @@ class GotoMagic():
tl-dr: saving magic as json.
"""

def __init__(self, jfile):
def __init__(self, project, degree='private', GOTOPATH=None):
""" Loads json from jfile """
self.jfile = jfile
self.magic = load_magic(jfile)
if GOTOPATH is None:
GOTOPATH = settings.GOTOPATH
self.project = project
utils.create_project_folder(project, degree, GOTOPATH)
self.jfile = os.path.join(
GOTOPATH, 'projects', project, degree, "{}.json".format(project))
self.magic = load_magic(self.jfile)

def reload(self):
""" reload the magic """
Expand Down Expand Up @@ -195,10 +206,12 @@ def save_magic(jfile, magic):
"saves the magic shortcuts as json into a file"
with codecs.open(jfile, 'w+', encoding='utf-8') as f:
try:
data = json.dumps(magic, sort_keys=True, indent=4, ensure_ascii=False)
data = str(json.dumps(magic, sort_keys=True, indent=4, ensure_ascii=False))
f.write(data)
except Exception as e:
print_text(
text.error.messages["magic_could_not_be_saved"],
message=e.message
message=str(e)
)
# TODO: handle more elegantly
exit(1)
6 changes: 6 additions & 0 deletions goto/gotomagic/text/text.py
Expand Up @@ -20,6 +20,9 @@ def message(self):
"Missing error message with name: {}".format(self.name))
return text.format(**self.kwargs)

def __str__(self):
return self.message


class GotoWarning:
def __init__(self, name, **kwargs):
Expand All @@ -36,6 +39,9 @@ def message(self):
"Missing warning message with name: {}".format(self.name))
return text.format(**self.kwargs)

def __str__(self):
return self.message


def print_text(text, **kwargs):
print(text.format(**kwargs))
18 changes: 18 additions & 0 deletions goto/gotomagic/utils.py
@@ -1,6 +1,7 @@
import os
import sys
from ..gotomagic.text import GotoWarning
from .. import settings


def detect_platform():
Expand All @@ -23,6 +24,23 @@ def is_file(raw_uri):
return os.path.exists(candidate)


def create_project_folder(project, degree='private', GOTOPATH=None):
'''
Creates project folder in goto state folders,
if not already existing.
degree can be either private, public or shared
'''
if GOTOPATH is None:
GOTOPATH = settings.GOTOPATH

project_folder = os.path.join(GOTOPATH, 'projects', project, degree)

if not os.path.exists(project_folder):
os.makedirs(project_folder)


def detect_unescaped_ampersand_url():
'''
Detects if user has entered a url with ampersand,
Expand Down
3 changes: 3 additions & 0 deletions goto/settings.py
@@ -0,0 +1,3 @@
import os
# HACK HACK
GOTOPATH = os.environ.get('GOTOPATH', os.path.expanduser('~/.goto'))
9 changes: 5 additions & 4 deletions goto/tests/test_end_to_end.sh
Expand Up @@ -22,7 +22,8 @@ function set_up {
echo "setting up tests"
create_goto_folders "$GOTOPATH"
touch "$OUTPUTFILE"
PROJECTFILE="$GOTOPATH/projects/$TESTPROJECT.json"
PROJECTFOLDER="$GOTOPATH/projects/$TESTPROJECT"
PROJECTFILE="$PROJECTFOLDER/private/$TESTPROJECT.json"
}


Expand Down Expand Up @@ -179,8 +180,8 @@ function test_04_switch_to_nonexistent_project {

function test_05_add_project {
_cmd_should_succeed "project add $TESTPROJECT"
if [ ! -f "${GOTOPATH}/projects/${TESTPROJECT}" ]; then
_fail_test "project file not created"
if [ ! -d "$PROJECTFOLDER" ]; then
_fail_test "project folder not created"
fi
_cmd_should_succeed "goto list"
}
Expand Down Expand Up @@ -239,7 +240,7 @@ function test_07_goto {
_projectfile_should_contain "$magicword"
}

function TODO_test_08_goto_add_æøå {
function test_08_goto_add_æøå {
existing_magicword="test_æøå"
nonexisting_magicword="IDoNotExist"
uri="http://example.com/æøå"
Expand Down
20 changes: 13 additions & 7 deletions goto/tests/test_gotomagic.py
@@ -1,15 +1,23 @@
from unittest import TestCase
from unittest import TestCase, skip
import os
import shutil

from ..gotomagic.magic import GotoMagic

tmpgotofile = 'testgotofile.json'

TMPGOTOPATH = '/tmp/.goto-unit-tests'
project = '__testgoto__'
tmpgotofile = os.path.join(TMPGOTOPATH, 'projects', project, 'private', '%s.json' % project) # noqa


# now, these tests would run on the real GOTOPATH.
# Find a way to point gotopath away first.


class TestMagic(TestCase):
def setUp(self):
""" Sets up goto magic to use tmp file """
self.magic = GotoMagic(tmpgotofile)
self.magic = GotoMagic(project, degree='private', GOTOPATH=TMPGOTOPATH)

def test_adding_shortcut(self):
""" Adding shortcuts through the gotomagic module"""
Expand All @@ -22,10 +30,8 @@ def test_adding_shortcut(self):

def tearDown(self):
""" Empty testgotofile after each test """
if os.path.exists(tmpgotofile):
if os.path.exists(TMPGOTOPATH):
# Displaying contents of testfile
with open(tmpgotofile) as f:
print(f.read())
os.remove(tmpgotofile)


shutil.rmtree(TMPGOTOPATH)

0 comments on commit 6662f94

Please sign in to comment.