Changeset - cf2833ee201e
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-06-16 19:10:19
brettcsmith@brettcsmith.org
plugin: Load user configuration file.
2 files changed with 14 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/__init__.py
Show inline comments
...
 
@@ -6,24 +6,25 @@
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import importlib
 
import logging
 

	
 
import beancount.core.data as bc_data
 

	
 
from typing import (
 
    AbstractSet,
 
    Any,
 
    Dict,
 
    Iterator,
 
    List,
 
    Optional,
 
    Set,
 
    Tuple,
...
 
@@ -33,29 +34,32 @@ from ..beancount_types import (
 
    ALL_DIRECTIVES,
 
    Directive,
 
    Entries,
 
    Errors,
 
    OptionsMap,
 
)
 
from .. import config as configmod
 
from .core import (
 
    Hook,
 
    HookName,
 
)
 
from ..errors import (
 
    ConfigurationError,
 
    Error,
 
)
 

	
 
__plugins__ = ['run']
 

	
 
logger = logging.getLogger('conservancy_beancount.plugin')
 

	
 
class HookRegistry:
 
    INCLUDED_HOOKS: Dict[str, Optional[List[str]]] = {
 
        '.meta_approval': None,
 
        '.meta_entity': None,
 
        '.meta_expense_allocation': None,
 
        '.meta_income_type': None,
 
        '.meta_invoice': None,
 
        # Enforcing this hook would be premature as of May 2020.  --brett
 
        # '.meta_payable_documentation': None,
 
        '.meta_paypal_id': ['MetaPayPalID'],
 
        '.meta_project': None,
 
        '.meta_receipt': None,
...
 
@@ -126,24 +130,33 @@ def run(
 
        config: str='',
 
        hook_registry: Optional[HookRegistry]=None,
 
) -> Tuple[Entries, Errors]:
 
    if hook_registry is None:
 
        hook_registry = HookRegistry()
 
        hook_registry.load_included_hooks()
 
    errors: Errors = []
 
    hooks: Dict[HookName, List[Hook]] = {
 
        # mypy thinks NamedTuples don't have __name__ but they do at runtime.
 
        t.__name__: [] for t in bc_data.ALL_DIRECTIVES  # type:ignore[attr-defined]
 
    }
 
    user_config = configmod.Config()
 
    try:
 
        user_config.load_file()
 
    except OSError as error:
 
        logger.debug("error reading configuration file %s: %s",
 
                     error.filename, error.strerror, exc_info=True)
 
        errors.append(ConfigurationError(
 
            f"error reading configuration file {error.filename}: {error.strerror}",
 
            source={'filename': error.filename},
 
        ))
 
    for key, hook_type in hook_registry.group_by_directive(config):
 
        try:
 
            hook = hook_type(user_config)
 
        except Error as error:
 
            errors.append(error)
 
        else:
 
            hooks[key].append(hook)
 
    for entry in entries:
 
        entry_type = type(entry).__name__
 
        for hook in hooks[entry_type]:
 
            errors.extend(hook.run(entry))
 
    return entries, errors
setup.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
from setuptools import setup
 

	
 
setup(
 
    name='conservancy_beancount',
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.2.2',
 
    version='1.2.3',
 
    author='Software Freedom Conservancy',
 
    author_email='info@sfconservancy.org',
 
    license='GNU AGPLv3+',
 

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
 
        'beancount>=2.2',  # Debian:beancount
 
        # 1.4.1 crashes when trying to save some documents.
 
        'odfpy>=1.4.0,!=1.4.1',  # Debian:python3-odf
 
        'PyYAML>=3.0',  # Debian:python3-yaml
 
        'regex',  # Debian:python3-regex
 
        'rt>=2.0',
0 comments (0 inline, 0 general)