from mediachain.datastore.ipfs import get_ipfs_datastore
import sys
import os
from os.path import expanduser, join

class ChDir(object):
    """
    Step into a directory temporarily
    """
    def __init__(self, path):
        self.old_dir = os.getcwd()
        self.new_dir = path

    def __enter__(self):
        os.chdir(self.new_dir)

    def __exit__(self, *args):
        os.chdir(self.old_dir)

def get_translator(translator_id):
	try:
		name, version = translator_id.split('@')
	except ValueError:
		raise LookupError(
			"Bad translator id `{}`, must be `name@multihash` format".format(translator_id)
		)

	ipfs = get_ipfs_datastore() # FIXME: memoize this

	path = join(expanduser('~'), '.mediachain')
	if not os.path.exists(path):
	    os.makedirs(path)

	with ChDir(path):
		translator = ipfs.client.get(version) # FIXME: timeout, error handling

	sys.path.append(path)
	# print('dynamic module load path: {}'.format(path))
	full_path = version + '.translator'
	# print('loading translator module from {}'.format(full_path))
	translator_module = __import__(full_path, globals(), locals(), [name])
	translator = getattr(translator_module, name.capitalize())

	return translator

from mediachain.datastore.ipfs import get_ipfs_datastore
import sys
import os
import shutil
from os.path import expanduser, join

class ChDir(object):
    """
    Step into a directory temporarily
    """
    def __init__(self, path):
        self.old_dir = os.getcwd()
        self.new_dir = path

    def __enter__(self):
        os.chdir(self.new_dir)

    def __exit__(self, *args):
        os.chdir(self.old_dir)

def get_translator(translator_id):
	try:
		name, version = translator_id.split('@')
	except ValueError:
		raise LookupError(
			"Bad translator id `{}`, must be `name@multihash` format".format(translator_id)
		)

	ipfs = get_ipfs_datastore() # FIXME: memoize this

	basepath = join(expanduser('~'), '.mediachain')
	path = join(basepath, 'mediachain', 'translation')
	if not os.path.exists(path):
	    os.makedirs(path)

	with ChDir(path):
		shutil.rmtree(name)
		translator = ipfs.client.get(version) # FIXME: timeout, error handling
		os.rename(version, name) # ipfsApi doesn't support -o

	sys.path.append(path)

	full_name = 'mediachain.translation.' + name + '.translator'
	translator_module = __import__(full_name, globals(), locals(), [name])
	translator = getattr(translator_module, name.capitalize())

	return translator

Use normal full names for translators
Use normal full names for translators

- Remove and replace any existing module
- Pull new module from ipfs and rename from multihash to normal name
  (ipfsApi doesn't support specifying output paths)
- Rename full_path to full_name for consistency
