Hello,

In the current form, the multivalued fields are returned like a list. Search
within them is much easier if they are dictionary alike objects.

My use case is the Release file and the checksums multivalued fields. To
search for the md5 of a file I must run over release['MD5Sum'] checking
every name field in its dictionary elements, instead of just use
release['MD5Sum'][name].

So, I use deb822.py patched to return this multivalued dictionaries.
Although I send it attached, I'm not proposing it to inclusion as is, but
only to show, because it will probably break most of the existing code
(requires a '.values()' to return the old list). Mi proposal is to add a
_multivalued_dict class with this behaviour, so allowing a non hardcoded
value for the dictionary key. Using an optional argument on instantiation
indicating the name of the dictionary key might be a better approach,
although I'm not able to see how to implement with the current __init__
prototype.

Javier Palacios
From 0fbb48574d48688123e95facfe0ea2fd86d18293 Mon Sep 17 00:00:00 2001
From: Javier Palacios <[email protected]>
Date: Fri, 2 Apr 2010 11:15:33 +0200
Subject: [PATCH] If a name is present, produce dictionaries instead of lists for multivalued fields

---
 lib/debian/deb822.py |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/lib/debian/deb822.py b/lib/debian/deb822.py
index 68af3d2..0fb9382 100644
--- a/lib/debian/deb822.py
+++ b/lib/debian/deb822.py
@@ -864,6 +864,10 @@ class _multivalued(Deb822):
                 continue
 
             if self.is_multi_line(contents):
+              if "name" in fields :
+                self[field] = Deb822Dict()
+                updater_method = self[field].update
+              else :
                 self[field] = []
                 updater_method = self[field].append
             else:
@@ -871,7 +875,12 @@ class _multivalued(Deb822):
                 updater_method = self[field].update
 
             for line in filter(None, contents.splitlines()):
-                updater_method(Deb822Dict(zip(fields, line.split())))
+                _dict = Deb822Dict(zip(fields, line.split()))
+                if "name" in fields :
+                    key = str(_dict['name'])
+                    updater_method({key:_dict})
+                else :
+                    updater_method(_dict)
 
     def get_as_string(self, key):
         keyl = key.lower()
-- 
1.5.6.5

--
http://lists.alioth.debian.org/mailman/listinfo/pkg-python-debian-discuss

Reply via email to