Skip to main content

DIM Application eXtensions (Python)

License PRs Welcome Platform Issues Repo Size Tags Version

Watchers Forks Stars Followers

Dependencies

  • Latest Versions
Name Version Description
Ming Ke Ming (名可名) Version Decentralized User Identity Authentication
Dao Ke Dao (道可道) Version Universal Message Module
DIMP (去中心化通讯协议) Version Decentralized Instant Messaging Protocol

Extensions

  1. Account
    • Address
      • BTC
      • ETH
    • Meta
      • MKM (Default)
      • BTC
      • ETH
    • Document
      • Visa (User)
      • Profile
      • Bulletin (Group)
  2. Message Contents
    • Text Content
    • File Content
      • Image Content
      • Audio Content
      • Video Content
    • Page Content
    • Name Card
    • Quote Content
    • Money Content
      • Transfer Money
    • Combine Forward
  3. System Commands
    • Meta Command
    • Document Command
    • Receipt Command
    • History Command
    • Group Command
      • Invite
      • Expel
      • Query
      • Quit
      • Join

Examples

Address

from typing import Optional

from dimp import Address, ConstantString

from dimax import BaseAddressFactory
from dimax.mem.ext import address_cache


class CompatibleAddressFactory(BaseAddressFactory):
    """Address factory with a fallback for unsupported address types."""

    # Call it when received 'UIApplicationDidReceiveMemoryWarningNotification',
    # this will remove 50% of cached objects
    #
    # :return number of survivors
    def reduce_memory(self) -> int:
        cache = address_cache()
        return cache.reduce_memory()

    # Override
    def _parse(self, address: str) -> Optional[Address]:
        try:
            res = super()._parse(address=address)
            if res is not None:
                return res
        except AssertionError as error:
            # FIXME:
            raise AssertionError('invalid address: %s, error: %s' % (address, error))
        #
        #  TODO: parse for other types of address
        #
        length = len(address)
        if 4 <= length <= 64:
            return UnknownAddress(string=address)
        raise ValueError('invalid address: %s' % address)


# Unsupported Address
# ~~~~~~~~~~~~~~~~~~~
class UnknownAddress(ConstantString, Address):
    """A fallback Address for strings not recognised by any factory."""

    def __init__(self, string: str):
        super().__init__(string=string)

    @property  # Override
    def network(self) -> int:
        return 0  # EntityType.USER

Meta

from typing import Optional

from dimp import StrMap, Meta

from dimax import BaseMetaFactory, DefaultMeta, BTCMeta, ETHMeta
from dimax.mkm.meta_factory import doc_helper
from dimax.protocol import MetaType


class CompatibleMetaFactory(BaseMetaFactory):
    """Meta factory that dispatches by version string (mkm/btc/eth)."""

    # Override
    def parse_meta(self, meta: StrMap) -> Optional[Meta]:
        helper = doc_helper()
        version = helper.get_meta_type(meta=meta)
        if version in (MetaType.MKM, 'mkm', 'MKM'):
            out = DefaultMeta(meta=meta)
        elif version in (MetaType.BTC, 'btc', 'BTC'):
            out = BTCMeta(meta=meta)
        elif version in (MetaType.ETH, 'eth', 'ETH'):
            out = ETHMeta(meta=meta)
        else:
            # TODO: other types of meta
            raise TypeError('unknown meta type: %s' % version)
        return out if out.is_valid else None

ExtensionLoader

from dimp import Address, Meta, Content, Command
from dimp import ContentType

from dimax import ExtensionLoader, ContentParser, CommandParser
from dimax.protocol import MetaType

from .compat.address import CompatibleAddressFactory
from .compat.meta import CompatibleMetaFactory


# Extensions Loader
# ~~~~~~~~~~~~~~~~~
class CommonExtensionLoader(ExtensionLoader):

    # Override
    def register_address_factory(self):

        Address.set_factory(factory=CompatibleAddressFactory())

    # Override
    def register_meta_factories(self):

        mkm = CompatibleMetaFactory(version=MetaType.MKM)
        btc = CompatibleMetaFactory(version=MetaType.BTC)
        eth = CompatibleMetaFactory(version=MetaType.ETH)

        Meta.set_factory(version=MetaType.MKM, factory=mkm)
        Meta.set_factory(version=MetaType.BTC, factory=btc)
        Meta.set_factory(version=MetaType.ETH, factory=eth)

    # Override
    def register_content_factories(self):
        super().register_content_factories()

        # Application Customized
        factory = ContentParser(content_class=AppCustomizedContent)
        Content.set_factory(msg_type=ContentType.APPLICATION, factory=factory)
        Content.set_factory(msg_type=ContentType.CUSTOMIZED, factory=factory)

    # Override
    def register_command_factories(self):
        super().register_command_factories()

        # Handshake
        Command.set_factory(cmd=HandshakeCommand.HANDSHAKE,
                            factory=CommandParser(command_class=BaseHandshakeCommand))

NOTE: AppCustomizedContent, HandshakeCommand and BaseHandshakeCommand are application-level classes (like the dart protocol/customized.dart and protocol/handshake.dart); they are not shipped in dimax. The registration primitives shown here — ContentParser, CommandParser, Content.set_factory, Command.set_factory and ContentType.APPLICATION / ContentType.CUSTOMIZED — are all real.

You must ensure that every Address you extend has a Meta type that can correspond to it one by one.


Copyright © 2018-2026 Albert Moky Followers

Metadata

Release files for dimax 2.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dimax 2.5.0
File Size Uploaded
dimax-2.5.0.tar.gz 41.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dimax 2.5.0
File Interpreter ABI Platform
dimax-2.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 127.0 kB

Release files / dimax-2.5.0.tar.gz

Download URL dimax-2.5.0.tar.gz
Size 41.7 kB
Tags Source
SHA-256 checksum
How to use checksums
146ddc13f4b5dcd205d4fdbdaa6829412113dd1995aad2b32f3bf471250ee1e5
BLAKE2b-256 checksum
How to use checksums
5b9b119d8cf7fef0f059a9ce6d3f4e4896946922cf10ff111a067f9bbc046817
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

Release files / dimax-2.5.0-py3-none-any.whl

Download URL dimax-2.5.0-py3-none-any.whl
Size 85.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d794df7c45abf7b730b986b0ef70d4122c16df95ead989ff3fa0d1f31db1f42
BLAKE2b-256 checksum
How to use checksums
d7c4268b5e371d70fb93a750aa3367941292e428d957da75fd6a8341bb3ec6b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3

Release history Release notifications | RSS feed

This release

2.5.0 This release

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page