Patch-Source: https://github.com/ulif/diceware/pull/62 From 4e11c42940a60518f7b895ca036d461bc2bcbb91 Mon Sep 17 00:00:00 2001 From: Hartmut Goebel Date: Mon, 29 Apr 2019 10:42:12 +0200 Subject: [PATCH 1/4] Get package version from module `__about__`. --- diceware/__about__.py | 1 + diceware/__init__.py | 3 +-- setup.py | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 diceware/__about__.py diff --git a/diceware/__about__.py b/diceware/__about__.py new file mode 100644 index 0000000..2ed0450 --- /dev/null +++ b/diceware/__about__.py @@ -0,0 +1,6 @@ +version = "0.9.7.dev0" +random_sources = { + 'system': 'diceware.random_sources:SystemRandomSource', + 'realdice': 'diceware.random_sources:RealDiceRandomSource', + # add more sources of randomness here... +} diff --git a/diceware/__init__.py b/diceware/__init__.py index 6f00228..b0d0880 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -16,23 +16,20 @@ """diceware -- rememberable passphrases """ import argparse -import pkg_resources import sys import logging from errno import ENOENT from random import SystemRandom +from .__about__ import version as __version__ from diceware.config import get_config_dict from diceware.logger import configure from diceware.wordlist import ( WordList, get_wordlist_path, get_wordlists_dir, get_wordlist_names, ) -__version__ = pkg_resources.get_distribution('diceware').version - #: Special chars inserted on demand SPECIAL_CHARS = r"~!#$%^&*()-=+[]\{}:;" + r'"' + r"'<>?/0123456789" - GPL_TEXT = ( """ This program is free software: you can redistribute it and/or modify @@ -73,10 +70,16 @@ def get_random_sources(): that provides a `choice(sequence)` method that works like the respective method in the standard Python lib `random` module. """ + from .__about__ import random_sources result = dict() - for entry_point in pkg_resources.iter_entry_points( - group="diceware_random_sources"): - result.update({entry_point.name: entry_point.load()}) + for name, spec in random_sources.items(): + module, func = spec.split(":") + module = __import__(module, fromlist=['__name__'], level=0) + try: + func = getattr(module, func) + except AttributeError as exc: + raise ImportError(str(exc)) + result[name] = func return result @@ -84,7 +87,7 @@ def handle_options(args): """Handle commandline options. """ plugins = get_random_sources() - random_sources = plugins.keys() + rnd_sources = plugins.keys() wordlist_names = get_wordlist_names() defaults = get_config_dict() parser = argparse.ArgumentParser( @@ -108,11 +111,11 @@ def handle_options(args): '-d', '--delimiter', default='', help="Separate words by DELIMITER. Empty string by default.") parser.add_argument( - '-r', '--randomsource', default='system', choices=random_sources, + '-r', '--randomsource', default='system', choices=rnd_sources, metavar="SOURCE", help=( "Get randomness from this source. Possible values: `%s'. " - "Default: system" % "', `".join(sorted(random_sources)))) + "Default: system" % "', `".join(sorted(rnd_sources)))) parser.add_argument( '-w', '--wordlist', default=['en_eff'], choices=wordlist_names, metavar="NAME", nargs='*', diff --git a/diceware/__main__.py b/diceware/__main__.py new file mode 100644 index 0000000..031df43 --- /dev/null +++ b/diceware/__main__.py @@ -0,0 +1,2 @@ +from . import main +main() diff --git a/diceware/random_sources.py b/diceware/random_sources.py index 72f69a7..8bc6d35 100644 --- a/diceware/random_sources.py +++ b/diceware/random_sources.py @@ -15,7 +15,7 @@ # along with this program. If not, see . """Sources of randomness. -Please register all sources as entry point in ``setup.py``. Look out for +Please register all sources as entry point in ``__about__.py``. Look out for "SystemRandomSource" for an example. For developers of interfaces to other sources of randomness: Currently, @@ -45,21 +45,15 @@ If you want to manage own commandline options with your plugin, you can implement a `classmethod` called ``update_argparser(parser)`` which gets an `argparse.ArgumentParser` instance as argument (no pun intended). -Finally, to register the source, add some stanza in `setup.py` of your -project that looks like:: +Finally, to register the source, add some stanza in `__about__.py` +that looks like:: # ... - setup( - # ... - entry_points={ - # console scripts and other entry points... - 'diceware_random_sources': [ - 'myrandom = mypkg.mymodule:MyRandomSource', - 'myothersrc = mypkg.mymodule:MyOtherSource', - ], - }, - # ... - ) + random_sources' = { + # ... + 'myrandom': 'mypkg.mymodule:MyRandomSource', + 'myothersrc': 'mypkg.mymodule:MyOtherSource', + } # ... Here the `myrandom` and `myothersrc` lines register random sources that diff --git a/setup.py b/setup.py index 3a7228e..8a82624 100644 --- a/setup.py +++ b/setup.py @@ -2,8 +2,12 @@ import os from setuptools import setup -def read(fname): - return open(os.path.join(os.path.dirname(__file__), fname)).read() +def read(*parts): + with open(os.path.join(os.path.dirname(__file__), *parts)) as fp: + return fp.read() + +version = {} +exec(read("diceware", "__about__.py"), version) setup_requires = [ @@ -27,7 +31,7 @@ docs_require = [ setup( name="diceware", - version="0.10", + version=version["version"], author="Uli Fouquet", author_email="uli@gnufix.de", description=( @@ -75,10 +79,5 @@ setup( 'console_scripts': [ 'diceware = diceware:main', ], - 'diceware_random_sources': [ - 'system = diceware.random_sources:SystemRandomSource', - 'realdice = diceware.random_sources:RealDiceRandomSource', - # add more sources of randomness here... - ], }, ) diff --git a/tests/test_diceware.py b/tests/test_diceware.py index 7867101..c52bb8f 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -180,6 +180,14 @@ class TestDicewareModule(object): assert 'system' in sources_dict assert isinstance(sources_dict['system'], type) + def test_get_random_sources_non_existing(self, monkeypatch): + import diceware + monkeypatch.setattr( + diceware.__about__, 'random_sources', + {'foo': 'diceware.random_sources:NotExistingSource'}) + with pytest.raises(ImportError): + get_random_sources() + def test_insert_special_char(self): # we can insert special chars in words. fake_rnd = FakeRandom() diff --git a/tests/test_random_sources.py b/tests/test_random_sources.py index 91f3b09..a544357 100644 --- a/tests/test_random_sources.py +++ b/tests/test_random_sources.py @@ -1,12 +1,11 @@ from __future__ import unicode_literals -import pkg_resources import pytest import sys import argparse from conftest import InputMock from io import StringIO from itertools import product, chain -from diceware import main +from diceware import main, get_random_sources from diceware.random_sources import ( SystemRandomSource, RealDiceRandomSource, ) @@ -38,10 +37,7 @@ class TestSystemRandomSource(object): def test_registered_as_system(self): # The SystemRandomInstance is registered as entry point with # name 'system' in group 'diceware_random_sources' - sources_dict = dict() - for entry_point in pkg_resources.iter_entry_points( - group="diceware_random_sources"): - sources_dict.update({entry_point.name: entry_point.load()}) + sources_dict = get_random_sources() assert 'system' in sources_dict assert sources_dict['system'] == SystemRandomSource @@ -110,10 +106,7 @@ class TestRealDiceRandomSource(object): def test_registered_as_realdice(self): # The RealDiceRandomSource is registered as entry point with # name 'realdice' in group 'diceware_random_sources' - sources_dict = dict() - for entry_point in pkg_resources.iter_entry_points( - group="diceware_random_sources"): - sources_dict.update({entry_point.name: entry_point.load()}) + sources_dict = get_random_sources() assert 'realdice' in sources_dict assert sources_dict['realdice'] == RealDiceRandomSource