From a28b844e0a446e29859a6f970d4c01dedb55efbf Mon Sep 17 00:00:00 2001 From: Matthias Klumpp Date: Thu, 19 Jan 2017 01:16:00 +0100 Subject: [PATCH] Allow to specify allowed keys If no tags are set to be propagated to the output, we will simply ignore them all. --- docs/asgen-config.md | 1 + src/asgen/config.d | 6 ++++++ src/asgen/result.d | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/asgen-config.md b/docs/asgen-config.md index b49cc51..23deee7 100644 --- a/docs/asgen-config.md +++ b/docs/asgen-config.md @@ -50,6 +50,7 @@ Oldsuites | This key exists to support migration from an alternative appstream g Suites | Suites which should be recognized by the generator. Each suite has the components and architectures which should be searched for metadata as children. See below for more information. Features | Disable or enable selected generator features. For a detailed description see below. CAInfo | Set the CA certificate bundle file to use for SSL peer verification. If this is not set, the generator will use the system default. +AllowedCustomKeys | Set which keys of the tag are allowed to be propagated to the collection metadata output. This key takes a list of custom-key strings as value. ### Suite fields diff --git a/src/asgen/config.d b/src/asgen/config.d index 1161644..e692111 100644 --- a/src/asgen/config.d +++ b/src/asgen/config.d @@ -99,6 +99,8 @@ class Config DataType metadataType; uint enabledFeatures; // bitfield + bool[string] allowedCustomKeys; // set of allowed keys in tags + string workspaceDir; string caInfo; @@ -338,6 +340,10 @@ class Config oldsuites = map!"a.str"(root["Oldsuites"].array).array; } + if ("AllowedCustomKeys" in root.object) + foreach (ref key; root["AllowedCustomKeys"].array) + allowedCustomKeys[key.str] = true; + // Enable features which are default-enabled setFeature (GeneratorFeature.PROCESS_DESKTOP, true); setFeature (GeneratorFeature.VALIDATE, true); diff --git a/src/asgen/result.d b/src/asgen/result.d index 7187592..3f38bef 100644 --- a/src/asgen/result.d +++ b/src/asgen/result.d @@ -29,8 +29,25 @@ import appstream.Component; import asgen.hint; import asgen.utils : buildCptGlobalID; import asgen.backends.interfaces; +import asgen.config : Config; +/** + * Helper function for GeneratorResult.finalize() + */ +extern(C) +int evaluateCustomEntry (void *keyPtr, void *value, void *userData) +{ + auto key = (cast(const(char)*) keyPtr).fromStringz; + auto conf = *cast(Config*) userData; + + if (key in conf.allowedCustomKeys) + return false; // FALSE, do not delete + + // remove invalid key + return true; +} + class GeneratorResult { @@ -208,6 +225,8 @@ public: */ void finalize () { + auto conf = Config.get (); + // we need to duplicate the associative array, because the addHint() function // may remove entries from "cpts", breaking our foreach loop. foreach (cpt; cpts.dup.byValue ()) { @@ -230,8 +249,8 @@ public: addHint (cpt.getId (), "metainfo-no-summary"); } - // inject package descriptions, if needed foreach (cpt; cpts.byValue ()) { + // inject package descriptions, if needed if (cpt.getKind () == ComponentKind.DESKTOP_APP) { auto flags = cpt.getValueFlags; cpt.setValueFlags (flags | AsValueFlags.NO_TRANSLATION_FALLBACK); @@ -250,6 +269,22 @@ public: } } + + // filter custom tags + auto customHashTable = cpt.getCustom (); + auto noCustomKeysAllowed = conf.allowedCustomKeys.length == 0; + if (customHashTable.size () > 0) { + import gi.glibtypes; + + if (noCustomKeysAllowed) { + // if we don't allow any custom keys, we can delete them faster + customHashTable.removeAll (); + continue; + } + + // filter the custom values + customHashTable.foreachRemove (&evaluateCustomEntry, &conf); + } } }