Beispiel #1
0
def paste_image():
    """
    Takes an image from the clipboard, pastes it into the .media directory
    and inserts a link to it into the buffer.
    """

    translator = basehash.base(k.constants.ALPHABET)
    identifier = translator.encode(uuid.uuid4().int >> 34).zfill(16)

    file_basedir = os.path.dirname(k.utils.get_absolute_filepath())
    filepath = os.path.join(file_basedir, '.media', identifier + '.png')

    # Create the .media directory
    media_basedir = os.path.dirname(filepath)
    if not os.path.exists(media_basedir):
        os.mkdir(media_basedir)

    if sys.platform == 'linux':
        command = 'xclip -selection clipboard -t image/png -o'
    elif sys.platform == 'darwin':
        command = f'pngpaste {filepath}'

    stdout, stderr, code = k.utils.run(shlex.split(command))

    if code != 0:
        raise k.errors.KnowledgeException(
            f"Image could not be pasted: {stderr}")

    # For linux, the output is on stdout so we need to write out the file
    if sys.platform == 'linux':
        with open(filepath, 'wb') as f:
            f.write(stdout)

    column = k.utils.get_current_column_number()
    vim_file_link = ''.join(
        ['{{file:',
         os.path.relpath(filepath, start=file_basedir), '}}'])

    modified_line = ''.join([
        vim.current.line[:(column + 1)], vim_file_link,
        vim.current.line[(column + 1):]
    ])

    vim.current.line = modified_line
    vim.current.window.cursor = vim.current.window.cursor[0], column + len(
        vim_file_link)
Beispiel #2
0
"""
Provides long term storage backend implementations.
"""

import basehash
import uuid

from pony import orm
from knowledge import config, errors, constants

db = orm.Database('sqlite', config.DB_FILE, create_db=True)
translator = basehash.base(constants.ALPHABET)


class Mapping(db.Entity):
    knowledge_id = orm.PrimaryKey(str)
    fact_id = orm.Required(str)


db.generate_mapping(create_tables=True)


@orm.db_session
def get(knowledge_id):
    mapping = Mapping.get(knowledge_id=knowledge_id)

    # If mapping not found in the local database, raise an exception
    if mapping is None:
        raise errors.MappingNotFoundException(knowledge_id)

    return mapping.fact_id
Beispiel #3
0
from nose.tools import raises

from basehash import base

basex = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr')


def test_custom_encode_with_1234567890_SKvdr0U():
    assert basex.encode(1234567890) == 'SKvdr0U'


def test_custom_decode_with_SKvdr0U_1234567890():
    assert basex.decode('SKvdr0U') == 1234567890


def test_custom_hash_with_1234567890_10_4QxzxC4CtG():
    assert basex.hash(1234567890, 10) == '4QxzxC4CtG'


def test_custom_unhash_with_4QxzxC4CtG_1234567890():
    assert basex.unhash('4QxzxC4CtG') == 1234567890


def test_custom_maximum_value_with_6_887503680():
    assert basex.maximum_value(6) == 887503680


@raises(ValueError)
def test_custom_hash_fail_with_887503681_6():
    assert basex.hash(887503681, 6)
from nose.tools import raises

from basehash import base


basex = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr')


def test_custom_encode_with_1234567890_SKvdr0U():
    assert basex.encode(1234567890) == 'SKvdr0U'


def test_custom_decode_with_SKvdr0U_1234567890():
    assert basex.decode('SKvdr0U') == 1234567890


def test_custom_hash_with_1234567890_10_4QxzxC4CtG():
    assert basex.hash(1234567890, 10) == '4QxzxC4CtG'


def test_custom_unhash_with_4QxzxC4CtG_1234567890():
    assert basex.unhash('4QxzxC4CtG') == 1234567890


def test_custom_maximum_value_with_6_887503680():
    assert basex.maximum_value(6) == 887503680


@raises(ValueError)
def test_custom_hash_fail_with_887503681_6():
    assert basex.hash(887503681, 6)
Beispiel #5
0
from nose.tools import raises

from basehash import base

bhx = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr')
bh6 = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr', 6)
bh10 = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr', 10)


def test_custom_encode_with_1234567890_SKvdr0U():
    assert bhx.encode(1234567890) == 'SKvdr0U'


def test_custom_decode_with_SKvdr0U_1234567890():
    assert bhx.decode('SKvdr0U') == 1234567890


def test_custom_hash_with_1234567890_10_4QxzxC4CtG():
    assert bh10.hash(1234567890) == '4QxzxC4CtG'


def test_custom_unhash_with_4QxzxC4CtG_1234567890():
    assert bh10.unhash('4QxzxC4CtG') == 1234567890


def test_custom_maximum_value_with_6_887503680():
    assert bh6.maximum == 887503680


@raises(ValueError)
def test_custom_hash_fail_with_887503681_6():
Beispiel #6
0
from nose.tools import raises

from basehash import base


bhx = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr')
bh6 = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr', 6)
bh10 = base('0SYv2xCbfhzGK4AW8E6QUpnjdtMOIlr', 10)


def test_custom_encode_with_1234567890_SKvdr0U():
    assert bhx.encode(1234567890) == 'SKvdr0U'


def test_custom_decode_with_SKvdr0U_1234567890():
    assert bhx.decode('SKvdr0U') == 1234567890


def test_custom_hash_with_1234567890_10_4QxzxC4CtG():
    assert bh10.hash(1234567890) == '4QxzxC4CtG'


def test_custom_unhash_with_4QxzxC4CtG_1234567890():
    assert bh10.unhash('4QxzxC4CtG') == 1234567890


def test_custom_maximum_value_with_6_887503680():
    assert bh6.maximum == 887503680


@raises(ValueError)