Wutag tags are currently stored in a way that makes them unique to wutag. They might get picked up by other programs, but they won’t look the same. The reason why is that tags get serialized in cbor and prefixed with wutag. for easier future extraction as can be seen here:

fn hash(&self) -> Result<String> { 
         .map(|tag| format!("{}.{}", WUTAG_NAMESPACE, base64::encode(tag))) 

How can I serialize the tags in python?

import base64
import pytest
import xattr

WUTAG_NAMESPACE = "user.wutag"

def hash(tag: Dict) -> str:
    return f"{WUTAG_NAMESPACE}.{pickle.dumps(tag).encode('base64', 'strict')}"
    # AttributeError: 'bytes' object has no attribute 'encode'
    #return f"{WUTAG_NAMESPACE}.{base64.b64encode(json.dumps(tag))}"
    # TypeError: a bytes-like object is required, not 'str'

def test_hash():
    tag = {'name': 'tag1', 'color': '#000000'}
    assert hash('tag') == 'user.wutag.dGFn'

def hash_tags(tags: List[Dict]) -> List[str]:
    return [hash(tag) for tag in tags]

def import_tags_wutag(path: str, tags: List[str]) -> List[str]:
    xattr.setxattr(path, WUTAG_NAMESPACE, hash_tags(tags))

Here is another try that doesn’t seem to work either:

def write_wutag_tags(parent_folder: str, tags: List[str]) -> None:
    for format in FORMAT_LIST:
        filepath = get_filepath(parent_folder, format)
        subprocess.run(['wutag', 'set', filepath, *tags])

This last one fails with this error if any of the tags is empty.

error: The argument '<tags>...' requires a value but none was supplied

    wutag set <pattern> <tags>...

For more information try --help

Otherwise it works fine.

import cbor2
def hash(tag: Dict) -> str:
    return f"{WUTAG_NAMESPACE}.{base64.b64encode(cbor2.dumps(tag))}"

def test_hash():
    tag = {'name': 'tag1', 'color': '#000000'}
    assert hash('tag') == "user.wutag.b'Y3RhZw=='"

Still need a test for the rust code to make sure they are the same.

Ask specific questions about how to code something in python

Python docs (tutorial)
Intro to programming - University of Helsinki

General python discussion on lemmy.ml

Create Post From:

  • 0 users online
  • 1 user / day
  • 1 user / week
  • 5 users / month
  • 7 users / 6 months
  • 22 subscribers
  • 13 Posts
  • Modlog