From 4580dfc31ee836dd4a985f5b07130e43de7aef20 Mon Sep 17 00:00:00 2001 From: Gunther Cox Date: Wed, 10 Apr 2019 09:50:14 -0400 Subject: [PATCH] Read version from config instead of using import --- .gitignore | 1 - MANIFEST.in | 1 + chatterbot/__init__.py | 4 ---- chatterbot/__main__.py | 14 +++++++++++--- docs/conf.py | 31 +++++++++++++++++++------------ setup.cfg | 8 +++++++- setup.py | 22 ++++++++++++++-------- tests/test_cli.py | 6 ++++-- 8 files changed, 56 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index afd260435..e757fccdc 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,6 @@ venv examples/settings.py examples/ubuntu_dialogs* -sentence_tokenizer.pickle .env .out diff --git a/MANIFEST.in b/MANIFEST.in index c13f042ab..3a460343d 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,6 +1,7 @@ include LICENSE include README.md include requirements.txt +include setup.cfg global-exclude __pycache__ global-exclude *.py[co] diff --git a/chatterbot/__init__.py b/chatterbot/__init__.py index 3979f9b1c..cd586411f 100644 --- a/chatterbot/__init__.py +++ b/chatterbot/__init__.py @@ -3,10 +3,6 @@ """ from .chatterbot import ChatBot -__version__ = '1.0.5' -__author__ = 'Gunther Cox' -__email__ = 'gunthercx@gmail.com' -__url__ = 'https://github.com/gunthercox/ChatterBot' __all__ = ( 'ChatBot', diff --git a/chatterbot/__main__.py b/chatterbot/__main__.py index d6c03a735..7cc6fe68f 100644 --- a/chatterbot/__main__.py +++ b/chatterbot/__main__.py @@ -1,10 +1,18 @@ -import importlib +import configparser import sys +import os def get_chatterbot_version(): - chatterbot = importlib.import_module('chatterbot') - return chatterbot.__version__ + config = configparser.ConfigParser() + + current_directory = os.path.dirname(os.path.abspath(__file__)) + parent_directory = os.path.abspath(os.path.join(current_directory, os.pardir)) + config_file_path = os.path.join(parent_directory, 'setup.cfg') + + config.read(config_file_path) + + return config['chatterbot']['version'] if __name__ == '__main__': diff --git a/docs/conf.py b/docs/conf.py index a55032631..e4157883b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,18 +1,22 @@ -import sys import os +import sys +import configparser from datetime import datetime import sphinx_rtd_theme -# Insert the project root dir as the first element in the PYTHONPATH. -# This lets us ensure that the source package is imported, and that its version is used. +config = configparser.ConfigParser() + current_directory = os.path.dirname(os.path.abspath(__file__)) parent_directory = os.path.abspath(os.path.join(current_directory, os.pardir)) -sys.path.insert(0, parent_directory) +config_file_path = os.path.join(parent_directory, 'setup.cfg') + +config.read(config_file_path) -import chatterbot # NOQA +# Insert the project root dir as the first element in the PYTHONPATH. +# This lets us ensure that the source package is imported, and used to generate the documentation. +sys.path.insert(0, parent_directory) -# -- General configuration ------------------------------------------------ # Sphinx extension modules extensions = [ @@ -41,14 +45,17 @@ # General information about the project project = 'ChatterBot' -copyright = '{}, {}'.format(datetime.now().year, chatterbot.__author__) -author = chatterbot.__author__ - -# The short X.Y version -version = chatterbot.__version__ +author = config['chatterbot']['author'] +copyright = '{}, {}'.format( + datetime.now().year, + author +) # The full version, including alpha/beta/rc tags -release = chatterbot.__version__ +release = config['chatterbot']['version'] + +# The short X.Y version +version = config['chatterbot']['version'].rsplit('.', 1)[0] language = 'en' diff --git a/setup.cfg b/setup.cfg index 6e0ffb183..dbc91524f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -20,4 +20,10 @@ cover-min-percentage = 40 # H306: imports not in alphabetical order (time, os) ignore = H306 max_line_length = 175 -exclude = .eggs, .git, .tox, build, \ No newline at end of file +exclude = .eggs, .git, .tox, build, + +[chatterbot] +version = 1.0.5 +author = Gunther Cox +email = gunthercx@gmail.com +url = https://github.com/gunthercox/ChatterBot diff --git a/setup.py b/setup.py index 527bfc42f..e4039b30e 100644 --- a/setup.py +++ b/setup.py @@ -2,8 +2,10 @@ """ ChatterBot setup file. """ +import os import sys import platform +import configparser from setuptools import setup @@ -15,13 +17,17 @@ ) ) -# Dynamically retrieve the version information from the chatterbot module -CHATTERBOT = __import__('chatterbot') -VERSION = CHATTERBOT.__version__ -AUTHOR = CHATTERBOT.__author__ -AUTHOR_EMAIL = CHATTERBOT.__email__ -URL = CHATTERBOT.__url__ -DESCRIPTION = CHATTERBOT.__doc__ +config = configparser.ConfigParser() + +current_directory = os.path.dirname(os.path.abspath(__file__)) +config_file_path = os.path.join(current_directory, 'setup.cfg') + +config.read(config_file_path) + +VERSION = config['chatterbot']['version'] +AUTHOR = config['chatterbot']['author'] +AUTHOR_EMAIL = config['chatterbot']['email'] +URL = config['chatterbot']['url'] with open('README.md') as f: LONG_DESCRIPTION = f.read() @@ -45,7 +51,7 @@ project_urls={ 'Documentation': 'https://chatterbot.readthedocs.io', }, - description=DESCRIPTION, + description='ChatterBot is a machine learning, conversational dialog engine.', long_description=LONG_DESCRIPTION, long_description_content_type='text/markdown', author=AUTHOR, diff --git a/tests/test_cli.py b/tests/test_cli.py index b6720be3e..c18a9ef85 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,5 +1,4 @@ from unittest import TestCase -from chatterbot import __version__ from chatterbot import __main__ as main @@ -10,4 +9,7 @@ class CommandLineInterfaceTests(TestCase): def test_get_chatterbot_version(self): version = main.get_chatterbot_version() - self.assertEqual(version, __version__) + version_parts = version.split('.') + self.assertEqual(len(version_parts), 3) + self.assertTrue(version_parts[0].isdigit()) + self.assertTrue(version_parts[1].isdigit())