The SSH host key has changed on 8 April, 2022 to this one: SHA256:573uTBSeh74kvOo0HJXi5ijdzRm8me27suzNEDlGyrQ

Add very basic Share handling

Gergely Polonkai 3 months ago
parent b585814ab8
commit d602d9c3ba
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
  1. 61
  2. 113

@ -0,0 +1,61 @@
"""Share handling"""
from random import choice
import re
from typing import Optional
from .exc import ValidationError
class Share:
"""Class to handle a share (space or workspace in older terminology)"""
def __init__(self, name: str, suffix: Optional[str] = None) -> None:
suffix = suffix or self.generate_random_suffix()
if not re.match(f'^{self._NAME_PATTERN}$', name):
raise ValidationError(f'Invalid name "{name}"')
if not re.match(f'^{self._SUFFIX_PATTERN}$', suffix):
raise ValidationError(f'Invalid suffix "{suffix}"') = name
self.suffix = suffix
def validate_address(cls, share_address: str) -> None:
"""Check if share_address is a valid share address"""
if not re.match(cls.PATTERN, share_address):
raise ValidationError(f'Invalid share address {share_address}')
def from_address(cls, share_address: str) -> 'Share':
"""Create a Share object from a share address"""
name, suffix = share_address[1:].split('.')
return cls(name, suffix)
def generate_random_suffix(length: int = 53) -> str:
"""Generate a random share suffix of the desired length"""
assert 54 > length > 0
return choice(ALPHA_LOWER) + ''.join(
choice(ALPHA_LOWER_OR_DIGIT) for _ in range(length - 1)
def __str__(self) -> str:
return f'+{}.{self.suffix}'
def __repr__(self) -> str:
return f'<Share {self}>'

@ -0,0 +1,113 @@
"""Tests for the Share class"""
import pytest
from earthsnake.types import ALPHA_LOWER, ALPHA_LOWER_OR_DIGIT
from earthsnake.exc import ValidationError
from earthsnake.share import Share
pytest.param('80smusic', 'suffix', 'Invalid name "80smusic"', id='name_digitstart'),
pytest.param('a.b', 'suffix', 'Invalid name "a.b"', id='name_period'),
pytest.param('PARTY', 'suffix', 'Invalid name "PARTY"', id='name_uppercase'),
pytest.param('test', 'b.c', 'Invalid suffix "b.c"', id='suffix_period'),
pytest.param('test', '4ever', 'Invalid suffix "4ever"', id='suffix_digitstart'),
pytest.param('test', 'TIME', 'Invalid suffix "TIME"', id='suffix_uppercase'),
def test_create_invalid(name: str, suffix: str, error: str) -> None:
"""Test if share creation fails with invalid values"""
with pytest.raises(ValidationError) as ctx:
Share(name, suffix=suffix)
assert str(ctx.value) == error
def test_create() -> None:
"""Test if creating a share with a name and a suffix succeeds"""
share = Share('test', suffix='suffix')
assert == 'test'
assert share.suffix == 'suffix'
def test_create_no_suffix() -> None:
"""Test if creating succeeds if no suffix is given, and the suffix is randomly chosen"""
share = Share('test')
assert == 'test'
assert len(share.suffix) == 53
def test_str() -> None:
"""Test the __str__ method"""
share = Share('test', 'suffix')
assert str(share) == '+test.suffix'
def test_repr() -> None:
"""Test the __repr__ method"""
share = Share('test', 'suffix')
assert repr(share) == '<Share +test.suffix>'
pytest.param('test.suffix', id='no_prefix'),
pytest.param('', id='invalid_name_char'),
def test_validate_invalid(address: str) -> None:
"""Test if validate_address fails if the address is invalid"""
with pytest.raises(ValidationError) as ctx:
assert str(ctx.value) == f'Invalid share address {address}'
pytest.param('test.suffix', id='no_prefix'),
pytest.param('', id='invalid_name_char'),
def test_from_address_invalid(address: str) -> None:
"""Test if from_address fails if the address is invalid"""
with pytest.raises(ValidationError) as ctx:
assert str(ctx.value) == f'Invalid share address {address}'
def test_from_address() -> None:
"""Test constructing a share from a string address"""
share = Share.from_address('+test.suffix')
assert == 'test'
assert share.suffix == 'suffix'
@pytest.mark.parametrize('length', range(1, 54))
def test_generate_suffix(length: int) -> None:
"""Test the random suffix generator"""
suffix = Share.generate_random_suffix(length=length)
assert suffix[0] in ALPHA_LOWER
assert all(char in ALPHA_LOWER_OR_DIGIT for char in suffix)
assert len(suffix) == length