Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate using libappstream to parse AppStream #2565

Open
barthalion opened this issue Feb 8, 2024 · 1 comment
Open

Investigate using libappstream to parse AppStream #2565

barthalion opened this issue Feb 8, 2024 · 1 comment

Comments

@barthalion
Copy link
Member

          > Website is likely also fine https://github.com/flathub-infra/website/blob/main/backend/app/utils.py#L216

Hmm... A bit OT, but did you know that you could get appstream2dict relatively efficiently via AppStream directly?

import gi
import yaml

gi.require_version('AppStream', '1.0')
from gi.repository import AppStream

def appstream2dict(xml_fname) -> dict[str, dict]:
    mdata = AppStream.Metadata()
    mdata.set_locale('ALL')
    mdata.set_format_style(AppStream.FormatStyle.CATALOG)
    mdata.set_parse_flags(AppStream.ParseFlags.IGNORE_MEDIABASEURL)

    with lzma.open(xml_fname, 'r') as f:
        xml_catalog_data = str(f.read(), 'utf-8')

    mdata.clear_components()
    mdata.parse_data(xml_catalog_data, AppStream.FormatKind.YAML)

    return yaml.safe_load(mdata.components_to_catalog(AppStream.FormatKind.YAML))

You might not want that for various reasons, but it would be dramatically less code ^^

Originally posted by @ximion in flathub-infra/flat-manager-hooks#101 (comment)

@ximion
Copy link

ximion commented Feb 25, 2024

Advantages

  • Always parse with the latest AppStream version and have any differences between specification revisions automatically normalized into one canonical form
  • Very fast XML parser
  • Lots of utility functions readily available to validate, transform markup or query additional information
  • Extremely well tested
  • Could reduce the amount of custom code drastically

Disadvantages

  • PyGObject dependency, GIR and dependency on a C library is annoying to handle and could make upgrading a bit harder
  • Some porting effort
  • Drags in a few dependencies (xmlb, libyaml, curl, potentially snowball)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants