Skip to content
Permalink
Browse files

cleanup

  • Loading branch information...
technocake committed Jul 11, 2019
1 parent 593818a commit 6662f94e6f224bcef426323d80f69306b7b294e9
@@ -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
@@ -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:
@@ -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.
@@ -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
@@ -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")

@@ -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
@@ -2,8 +2,6 @@
. _gotoutils # load common utility functions
load_gotopath

STATEDIR="$GOTOPATH"


_usage() {
echo "Usage: project [add <projectname>] | [list]"
@@ -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."
@@ -52,7 +54,7 @@ _changeproject() {
}

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

# ---------------------------
@@ -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
@@ -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():
@@ -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)
@@ -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()
@@ -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():
@@ -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 """
@@ -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)
@@ -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):
@@ -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))
@@ -1,6 +1,7 @@
import os
import sys
from ..gotomagic.text import GotoWarning
from .. import settings


def detect_platform():
@@ -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,
@@ -0,0 +1,3 @@
import os
# HACK HACK
GOTOPATH = os.environ.get('GOTOPATH', os.path.expanduser('~/.goto'))
@@ -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"
}


@@ -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"
}
@@ -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/æøå"
@@ -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"""
@@ -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.
You can’t perform that action at this time.