DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
0 votes

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))
edited by

What does a "wutag" tag look like?


It looks much different than what I had gotten. Should I remove the answer?

❯ wutag set test tag1

        + tag1
❯ getfattr --dump test
# file: test

I can always call wutag from python, it may not be slower than what I'm doing.

1 Answer

0 votes
Best answer

Can be done directly using wutag. Since the set subcommand takes a pattern and not a file path as a parameter you can either change to the file directory with, cwd=parent_folder) or increase the max depth to search for the provided pattern. I think the second approach would be more efficient.

def write_wutag_tags(parent_folder: str, tags: List[str]) -> None:
    for format in FORMAT_LIST:
        filepath = get_filepath(parent_folder, format)
        filepath = os.path.relpath(filepath)
        depth = len(Path(filepath).parents)['wutag', '-m', str(depth), 'set', filepath, *tags])

for format in FORMAT_LIST:
    filename = os.path.basename(parent_folder) + '.' + format['wutag', 'set', filename, *tags], cwd=parent_folder)
edited by
Contributions licensed under CC0