Skip to content

Commit

Permalink
PkgMetaConfig as a mapping, which will be handy in some situations wh…
Browse files Browse the repository at this point in the history
…ere we need to list the repositories by name.
  • Loading branch information
mmulich committed Nov 30, 2011
1 parent e10ae2c commit 42ca625
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions pkgmeta/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections import Mapping
from configparser import ConfigParser
from pkgmeta.exceptions import PkgMetaConfigFileError
from pkgmeta.storage import lookup_storage_by_type
Expand All @@ -23,7 +24,7 @@ def __init__(self, name, type=None, sources=None,
self.storage = storage_factory(self)


class PkgMetaConfig(object):
class PkgMetaConfig(Mapping):
"""Main pkgmeta configuration object"""

def __init__(self, repositories, default=None):
Expand Down Expand Up @@ -66,9 +67,6 @@ def from_file(cls, file):
setattr(inst, '_file', file)
return inst

def __iter__(self):
return iter(self.repositories)

def get_repository_config(self, name=None):
"""Get a RepositoryConfig by name. If name is not given,
the default repository will be returned."""
Expand All @@ -79,3 +77,16 @@ def get_repository_config(self, name=None):
except IndexError:
raise LookupError("Could not find '%s'" % name)
return repo_config

# ############################### #
# Abstract method definitions #
# ############################### #

def __len__(self):
return len(self.repositories)

def __iter__(self):
return iter(self.repositories)

def __getitem__(self, key):
return self.get_repository_config(key)
3 changes: 2 additions & 1 deletion pkgmeta/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def test_get_repository_config_with_name(self):

# Test with a specific repository name
repo_name = repo_names[-1]
repo_config = config.get_repository_config(repo_name)
# __getitem__ uses get_repository_config
repo_config = config[repo_name]
self.assertEqual(repo_config.name, repo_name)


Expand Down

0 comments on commit 42ca625

Please sign in to comment.