Skip to content

Commit

Permalink
Merge branch 'feature/update-docs' into feature/return-config-entry-f…
Browse files Browse the repository at this point in the history
…rom-iter
  • Loading branch information
thecjharries committed Mar 17, 2018
2 parents 8ca94fc + 0c1f2d8 commit 5a4203b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ The Config type
.. method:: Config.__iter__()

The :class:`Config` class has an iterator which can be used to loop
through all the entries in the configuration. Each element is a tuple
containing the name and the value of each configuration variable. Be
through all the entries in the configuration. Each element is a ``ConfigEntry`` object containing the name, level, and value of each configuration variable. Be
aware that this may return multiple versions of each entry if they are
set multiple times in the configuration files.

Expand All @@ -32,3 +31,14 @@ string. In order to apply the git-config parsing rules, you can use

.. automethod:: pygit2.Config.get_bool
.. automethod:: pygit2.Config.get_int


The ConfigEntry type
====================

.. automethod:: pygit2.ConfigEntry.decode_as_string
.. automethod:: pygit2.ConfigEntry._from_c
.. automethod:: pygit2.ConfigEntry.name
.. automethod:: pygit2.ConfigEntry.level
.. automethod:: pygit2.ConfigEntry.value
.. automethod:: pygit2.ConfigEntry.value_string
14 changes: 14 additions & 0 deletions pygit2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ class ConfigEntry(object):

@classmethod
def _from_c(cls, ptr, from_iterator=False):
"""Builds the entry from a ``git_config_entry`` pointer. ``from_iterator``
should be ``True`` when the entry was created during ``git_config_iterator``
actions
"""
entry = cls.__new__(cls)
entry._entry = ptr
entry.from_iterator = from_iterator
Expand All @@ -303,20 +307,30 @@ def __del__(self):

@property
def value(self):
"""The raw ``cData`` entry value
"""
return self._entry.value

@property
def level(self):
"""The entry's ``git_config_level_t`` value
"""
return self._entry.level

@property
def name(self):
"""The entry's name
"""
return self.decode_as_string(self._entry.name)

@property
def value_string(self):
"""The entry's value as a string
"""
return self.decode_as_string(self.value)

@staticmethod
def decode_as_string(value):
"""Returns ``value`` as a decoded ``utf-8`` string.
"""
return ffi.string(value).decode('utf-8')

0 comments on commit 5a4203b

Please sign in to comment.