Skip to content
Snippets Groups Projects
Commit 7a449c47 authored by Vincent Texier's avatar Vincent Texier
Browse files

issue #52 add type hinting to membership module

parent a9ba5730
Branches
Tags
No related merge requests found
...@@ -4,10 +4,15 @@ Created on 2 déc. 2014 ...@@ -4,10 +4,15 @@ Created on 2 déc. 2014
@author: inso @author: inso
""" """
import re import re
from typing import TypeVar, Type
from .block_uid import BlockUID from .block_uid import BlockUID
from .document import Document, MalformedDocumentError from .document import Document, MalformedDocumentError
from ..constants import BLOCK_UID_REGEX, SIGNATURE_REGEX, PUBKEY_REGEX from ..constants import BLOCK_UID_REGEX, SIGNATURE_REGEX, PUBKEY_REGEX
# required to type hint cls in classmethod
MembershipType = TypeVar('MembershipType', bound='Membership')
class Membership(Document): class Membership(Document):
""" """
...@@ -46,19 +51,19 @@ class Membership(Document): ...@@ -46,19 +51,19 @@ class Membership(Document):
"CertTS": re_certts "CertTS": re_certts
}} }}
def __init__(self, version, currency, issuer, membership_ts, def __init__(self, version: int, currency: str, issuer: str, membership_ts: BlockUID,
membership_type, uid, identity_ts, signature): membership_type: str, uid: str, identity_ts: BlockUID, signature: str) -> None:
""" """
Create a membership document Create a membership document
:param int version: Version of the document :param version: Version of the document
:param currency: Name of the currency :param currency: Name of the currency
:param issuer: Public key of the issuer :param issuer: Public key of the issuer
:param BlockUID membership_ts: BlockUID of this membership :param membership_ts: BlockUID of this membership
:param membership_type: "IN" or "OUT" to enter or quit the community :param membership_type: "IN" or "OUT" to enter or quit the community
:param str uid: Unique identifier of the identity :param uid: Unique identifier of the identity
:param BlockUID identity_ts: BlockUID of the identity :param identity_ts: BlockUID of the identity
:param str|None signature: Signature of the document :param signature: Signature of the document
""" """
super().__init__(version, currency, [signature]) super().__init__(version, currency, [signature])
self.issuer = issuer self.issuer = issuer
...@@ -68,7 +73,17 @@ class Membership(Document): ...@@ -68,7 +73,17 @@ class Membership(Document):
self.identity_ts = identity_ts self.identity_ts = identity_ts
@classmethod @classmethod
def from_inline(cls, version, currency, membership_type, inline): def from_inline(cls: Type[MembershipType], version: int, currency: str, membership_type: str,
inline: str) -> MembershipType:
"""
Return Membership instance from inline format
:param version: Version of the document
:param currency: Name of the currency
:param membership_type: "IN" or "OUT" to enter or exit membership
:param inline: Inline string format
:return:
"""
data = Membership.re_inline.match(inline) data = Membership.re_inline.match(inline)
if data is None: if data is None:
raise MalformedDocumentError("Inline membership ({0})".format(inline)) raise MalformedDocumentError("Inline membership ({0})".format(inline))
...@@ -80,7 +95,13 @@ class Membership(Document): ...@@ -80,7 +95,13 @@ class Membership(Document):
return cls(version, currency, issuer, membership_ts, membership_type, uid, identity_ts, signature) return cls(version, currency, issuer, membership_ts, membership_type, uid, identity_ts, signature)
@classmethod @classmethod
def from_signed_raw(cls, signed_raw): def from_signed_raw(cls: Type[MembershipType], signed_raw: str) -> MembershipType:
"""
Return Membership instance from signed raw format
:param signed_raw: Signed raw format string
:return:
"""
lines = signed_raw.splitlines(True) lines = signed_raw.splitlines(True)
n = 0 n = 0
...@@ -114,7 +135,12 @@ class Membership(Document): ...@@ -114,7 +135,12 @@ class Membership(Document):
return cls(version, currency, issuer, membership_ts, return cls(version, currency, issuer, membership_ts,
membership_type, uid, identity_ts, signature) membership_type, uid, identity_ts, signature)
def raw(self): def raw(self) -> str:
"""
Return signed raw format string of the Membership instance
:return:
"""
return """Version: {0} return """Version: {0}
Type: Membership Type: Membership
Currency: {1} Currency: {1}
...@@ -131,7 +157,11 @@ CertTS: {6} ...@@ -131,7 +157,11 @@ CertTS: {6}
self.uid, self.uid,
self.identity_ts) self.identity_ts)
def inline(self): def inline(self) -> str:
"""
Return inline string format of the Membership instance
:return:
"""
return "{0}:{1}:{2}:{3}:{4}".format(self.issuer, return "{0}:{1}:{2}:{3}:{4}".format(self.issuer,
self.signatures[0], self.signatures[0],
self.membership_ts, self.membership_ts,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment