From 08e4564a1b0c97e32797434c4f243e0c6a079e4b Mon Sep 17 00:00:00 2001 From: Andre Filho Date: Fri, 14 Jun 2019 12:54:01 -0300 Subject: [PATCH 1/3] add(documentation): added docstrings to all functions --- commit_helper/__main__.py | 8 +++-- commit_helper/conventions/atom.py | 6 ++++ .../conventions/convention_help_handler.py | 12 ++++--- .../conventions/custom_convention.py | 5 ++- commit_helper/conventions/karma_angular.py | 6 ++++ commit_helper/conventions/no_convention.py | 3 ++ commit_helper/conventions/symphony_cmf.py | 6 ++++ commit_helper/conventions/tagged.py | 6 ++++ commit_helper/utils/file_handler.py | 12 +++++-- commit_helper/utils/flag_commit_handler.py | 8 +++-- commit_helper/utils/text_utils.py | 25 +++++++++++++ commit_helper/utils/utils.py | 35 +++++++++++++------ 12 files changed, 108 insertions(+), 24 deletions(-) diff --git a/commit_helper/__main__.py b/commit_helper/__main__.py index 3246115..686fbc2 100755 --- a/commit_helper/__main__.py +++ b/commit_helper/__main__.py @@ -1,16 +1,18 @@ -# dependencies imports from pathlib import Path -# utils imports + from .utils.utils import parser_cli from .utils.text_utils import debug from .utils.file_handler import handle_file_based_commit from .utils.file_handler import get_file_path from .utils.flag_commit_handler import convention_flag_handler -# convention imports + from .conventions.convention_help_handler import convention_help_handler def main(): + """ + Main function. Called by CLI. + """ parser = parser_cli() args = parser.parse_args() debug_mode = args.debug diff --git a/commit_helper/conventions/atom.py b/commit_helper/conventions/atom.py index d4c51b4..7137336 100644 --- a/commit_helper/conventions/atom.py +++ b/commit_helper/conventions/atom.py @@ -1,4 +1,10 @@ def atom_convention(tag, msg): + """ + Formats the commit following the atom convention format: + The atom convention: + + :: + """ tag = tag.lower() msg = msg.capitalize() composed_message = ":%s: %s\n" % (tag, msg) diff --git a/commit_helper/conventions/convention_help_handler.py b/commit_helper/conventions/convention_help_handler.py index a1b2de3..3085c74 100644 --- a/commit_helper/conventions/convention_help_handler.py +++ b/commit_helper/conventions/convention_help_handler.py @@ -1,14 +1,12 @@ -# dependencies imports from yaml import safe_load from yaml import YAMLError -# convention imports + from .atom import atom_convention_help from .tagged import tagged_convention_help from .karma_angular import karma_convention_help from .karma_angular import angular_convention_help from .symphony_cmf import symphony_convention_help -# utils imports -from commit_helper.utils.colors import HELP + from commit_helper.utils.colors import RESET from commit_helper.utils.colors import MIN_ERROR from commit_helper.utils.text_utils import debug @@ -18,6 +16,9 @@ # TODO: test def convention_help_handler(file_path, args, debug_mode): + """ + Handles the user's help request. + """ if file_path.is_file() and args.convention is '': debug('Found file for help', str(file_path), debug_mode) with open(str(file_path)) as target: @@ -42,6 +43,9 @@ def convention_help_handler(file_path, args, debug_mode): # TODO: test def get_help_to_defined_convention(convention, debug_mode): + """ + Prints the help menu to the base conventions. + """ debug('recieved convention for help catch', convention, debug_mode) if convention == 'angular': print_help(angular_convention_help) diff --git a/commit_helper/conventions/custom_convention.py b/commit_helper/conventions/custom_convention.py index b78f87f..ff3be03 100644 --- a/commit_helper/conventions/custom_convention.py +++ b/commit_helper/conventions/custom_convention.py @@ -1,9 +1,12 @@ -# custom commits are only acceptable when you have a config file from commit_helper.utils.text_utils import debug from commit_helper.utils.text_utils import get_context +# custom commits are only acceptable when you have a config file def custom_convention(tag, message, config_file, debug_mode): + """ + Formats the given arguments to make a custom commit message. + """ debug('tag', tag, debug_mode) debug('message', message, debug_mode) debug('pattern from file', config_file['commit_pattern'], debug_mode) diff --git a/commit_helper/conventions/karma_angular.py b/commit_helper/conventions/karma_angular.py index a72aaa8..9148954 100644 --- a/commit_helper/conventions/karma_angular.py +++ b/commit_helper/conventions/karma_angular.py @@ -1,4 +1,10 @@ def karma_angular_convention(tag, msg, context): + """ + Handles the formatting for both karma and angular conventions. + + Convention structure: + (): + """ tag = tag.lower() if context == '': composed_message = "%s: %s\n" % (tag, msg) diff --git a/commit_helper/conventions/no_convention.py b/commit_helper/conventions/no_convention.py index 26404f7..177d217 100644 --- a/commit_helper/conventions/no_convention.py +++ b/commit_helper/conventions/no_convention.py @@ -3,6 +3,9 @@ def just_message(msg=''): + """ + Formats the message to capitalize. + """ if msg == '': message = str(input(INPUT_COLOR + "commit message: " + RESET)) else: diff --git a/commit_helper/conventions/symphony_cmf.py b/commit_helper/conventions/symphony_cmf.py index ff1d8a4..de2a749 100644 --- a/commit_helper/conventions/symphony_cmf.py +++ b/commit_helper/conventions/symphony_cmf.py @@ -1,4 +1,10 @@ def symphony_convention(tag, msg): + """ + Formats the commit following the symphony convention. + + The symphony CMF convention: + [] + """ tag = tag.capitalize() composed = "[%s] %s\n" % (tag, msg) return composed diff --git a/commit_helper/conventions/tagged.py b/commit_helper/conventions/tagged.py index ba84497..053a6ce 100644 --- a/commit_helper/conventions/tagged.py +++ b/commit_helper/conventions/tagged.py @@ -1,4 +1,10 @@ def tagged_convention(tag, msg): + """ + Formats the convention following the tag convention. + + The tagged convention: + : + """ tag = tag.upper() composed_message = "%s: %s\n" % (tag, msg) return composed_message diff --git a/commit_helper/utils/file_handler.py b/commit_helper/utils/file_handler.py index 036ba88..b7c1d57 100644 --- a/commit_helper/utils/file_handler.py +++ b/commit_helper/utils/file_handler.py @@ -2,7 +2,7 @@ from pathlib import Path from yaml import safe_load from yaml import YAMLError -# utils imports + from .text_utils import debug from .text_utils import notify from .text_utils import get_text @@ -10,12 +10,16 @@ from .utils import dump_convention from .utils import validate_commiter_file from .utils import handle_conventioned_commit -# conventions imports + from commit_helper.conventions.no_convention import just_message from commit_helper.conventions.custom_convention import custom_convention def handle_file_based_commit(file_path, debug_mode, args): + """ + Function that handles all the logic involved in commits with previous + configuration file. + """ with open(str(file_path), 'r') as stream: try: config = safe_load(stream) @@ -48,6 +52,10 @@ def handle_file_based_commit(file_path, debug_mode, args): def get_file_path(): # pragma: no cover + """ + Searchs on the folder the program was called if there is a commiter.yml or a + commit-helper.yml file and returns it's path if exists. + """ commiter = Path('commiter.yml') commit_h = Path('commit-helper.yml') diff --git a/commit_helper/utils/flag_commit_handler.py b/commit_helper/utils/flag_commit_handler.py index 368793f..e0d00fd 100644 --- a/commit_helper/utils/flag_commit_handler.py +++ b/commit_helper/utils/flag_commit_handler.py @@ -1,15 +1,17 @@ -# dependencies imports from os import system -# utils imports + from .text_utils import debug from .utils import create_file from .utils import gen_co_author from .utils import handle_conventioned_commit -# conventions imports + from commit_helper.conventions.no_convention import just_message def convention_flag_handler(args, debug_mode): + """ + Handles the commit configuration and creation through flags. + """ convention = str(args.convention) debug('convention flag', convention, debug_mode) commit_message = '' diff --git a/commit_helper/utils/text_utils.py b/commit_helper/utils/text_utils.py index e94c860..029dbe8 100644 --- a/commit_helper/utils/text_utils.py +++ b/commit_helper/utils/text_utils.py @@ -6,44 +6,69 @@ def get_text(): + """ + Gets the input for both tag and message. + """ tag = str(input(INPUT_COLOR + 'type the tag: ' + RESET)) msg = str(input(INPUT_COLOR + 'type the commit message: ' + RESET)).lower() return tag, msg def get_context(): + """ + Gets the input for the commit context and formats it to lowercase. + """ context = str(input(INPUT_COLOR + 'type the context: ' + RESET) or '') context.lower() return context def sanitize_as_empty_string(string): + """ + Checks if arg is None and returns it as an empty string. + """ if string is None: return '' return string def notify(message): + """ + Colored formatted print for notifications. + """ print(NOTIFY_COLOR + str(message) + RESET) def debug(message, value, show=False): + """ + Colored formatted print for debugging. + """ if show: mid = 'DEBUG: ' + str(message) + ' ~> ' + str(value) print(DEBUG_COLOR + mid + RESET) def print_help(message): + """ + Colored formatted print for help menus. + """ print(HELP + str(message) + RESET) def handle_tag_message_args(tag='', message=''): + """ + Checks if args are empty strings, if so it calls the get_text function. + """ if tag + message is not '': return tag, message return get_text() def handle_context_arg(context=''): + """ + Checks if context is empty and if it is calls a input function to get the + context. + """ if context is not '': return context return get_context() diff --git a/commit_helper/utils/utils.py b/commit_helper/utils/utils.py index 4952652..51f5d40 100644 --- a/commit_helper/utils/utils.py +++ b/commit_helper/utils/utils.py @@ -21,6 +21,10 @@ def gen_co_author(co_author): + """ + Formats the co-author for putting into the commit body if it is not an empty + string. + """ if co_author is '': return '' return '\nCo-authored-by: %s' % co_author @@ -28,17 +32,23 @@ def gen_co_author(co_author): # TEST def create_file(convention_name, dont_create=False): # pragma: no cover + """ + Creates a commit-helper.yml file with the given convention. + """ if not dont_create: data = dict( convention=convention_name ) - with open('commiter.yml', 'w') as output_file: + with open('commit-helper.yml', 'w') as output_file: output_file.write(dump(data, stream=None, default_flow_style=False)) notify('Successfully created the commiter file.') def parser_cli(): + """ + Calls argparser's parser to handle the CLI arguments. + """ desc = 'A commit formatter tool to help you follow commit conventions.' help_convention = \ """ @@ -48,35 +58,30 @@ def parser_cli(): parser = argparse.ArgumentParser(description=desc) parser.add_argument('-t', '--tag', dest='tag', default='', help='Pass your commit tag directly') - parser.add_argument('-m', '--message', dest='message', default='', help='Pass your commit message directly') - parser.add_argument('-ct', '--context', dest='context', default='', help='Pass your commit context directly') - parser.add_argument('-ca', '--co-author', - help='Make your friend an co-author to the commit', + help='Make your friend a co-author for the commit', dest='co_author', default='') - parser.add_argument('-nf', '--no-file', dest='no_file', help='Disables the creation of a commiter.yml file', action='store_true') - parser.add_argument('-c', '--convention', choices=supported_conventions, dest='convention', default='', help=help_convention) - parser.add_argument('-d', '--debug', action='store_true', dest='debug', help='Toggles debug option') - parser.add_argument('-s', '--show', dest='show_convention_tags', default='False', action='store_true', help='Shows the rules of a given convention') - return parser def dump_convention(config_file): + """ + Gets the convention's name from file. + """ if config_file['convention'] is None: return 'none' return str(config_file['convention']).lower() @@ -84,12 +89,20 @@ def dump_convention(config_file): # this function forces the program to quit if commiter file is invalid def validate_commiter_file(stream_file): # pragma: no cover + """ + Checks if commit-helper file with custom convention has the required fields. + """ if stream_file['commit_pattern'] is None or stream_file['context'] is None: - print("Error: Your commiter file lacks a commit_pattern or context!") + print( + "Error: Your commit-helper file lacks a commit_pattern or context!") sys.exit(0) +# REFACT: use function dict to keep code clean def handle_conventioned_commit(convention, args): + """ + Handler for conventioned commits. + """ tag, message = handle_tag_message_args(args.tag, args.message) commit_message = '' From a56592caf22c45dbcc3c9ded6ce3fe5949af6754 Mon Sep 17 00:00:00 2001 From: Andre Filho Date: Mon, 12 Aug 2019 09:09:52 -0300 Subject: [PATCH 2/3] fix(codestyle): pycodestyle --- commit_helper/utils/file_handler.py | 4 +-- commit_helper/utils/utils.py | 38 ++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/commit_helper/utils/file_handler.py b/commit_helper/utils/file_handler.py index b7c1d57..519715e 100644 --- a/commit_helper/utils/file_handler.py +++ b/commit_helper/utils/file_handler.py @@ -53,8 +53,8 @@ def handle_file_based_commit(file_path, debug_mode, args): def get_file_path(): # pragma: no cover """ - Searchs on the folder the program was called if there is a commiter.yml or a - commit-helper.yml file and returns it's path if exists. + Searchs on the folder the program was called if there is a commiter.yml + or a commit-helper.yml file and returns it's path if exists. """ commiter = Path('commiter.yml') commit_h = Path('commit-helper.yml') diff --git a/commit_helper/utils/utils.py b/commit_helper/utils/utils.py index 51f5d40..acad045 100644 --- a/commit_helper/utils/utils.py +++ b/commit_helper/utils/utils.py @@ -22,8 +22,8 @@ def gen_co_author(co_author): """ - Formats the co-author for putting into the commit body if it is not an empty - string. + Formats the co-author for putting into the commit body if it is not an + empty string. """ if co_author is '': return '' @@ -90,15 +90,18 @@ def dump_convention(config_file): # this function forces the program to quit if commiter file is invalid def validate_commiter_file(stream_file): # pragma: no cover """ - Checks if commit-helper file with custom convention has the required fields. + Checks if commit-helper file with custom convention has the + required fields. """ if stream_file['commit_pattern'] is None or stream_file['context'] is None: print( - "Error: Your commit-helper file lacks a commit_pattern or context!") + "Error: Your commit-helper file lacks a commit_pattern or context!" + ) sys.exit(0) - # REFACT: use function dict to keep code clean + + def handle_conventioned_commit(convention, args): """ Handler for conventioned commits. @@ -106,17 +109,28 @@ def handle_conventioned_commit(convention, args): tag, message = handle_tag_message_args(args.tag, args.message) commit_message = '' + create_commit = dict( + { + # 'angular': handle_karma_angular, + # 'karma': handle_karma_angular, + 'tagged': tagged_convention, + 'symphony': symphony_convention, + 'atom': atom_convention + }) + if convention == 'angular' or convention == 'karma': context = handle_context_arg(args.context) - commit_message = karma_angular_convention(tag, message, context) + return karma_angular_convention(tag, message, context) + + commit_message = create_commit[convention](tag, message) - elif convention == 'tagged': - commit_message = tagged_convention(tag, message) + # elif convention == 'tagged': + # commit_message = tagged_convention(tag, message) - elif convention == 'symphony': - commit_message = symphony_convention(tag, message) + # elif convention == 'symphony': + # commit_message = symphony_convention(tag, message) - elif convention == 'atom': - commit_message = atom_convention(tag, message) + # elif convention == 'atom': + # commit_message = atom_convention(tag, message) return commit_message From 10f810daca8db5d0e3ae7e8cd1d472b1c5858dfb Mon Sep 17 00:00:00 2001 From: Andre Filho Date: Fri, 4 Oct 2019 08:19:27 -0300 Subject: [PATCH 3/3] ci(codeclimate): fixes the coverage reporter version --- dev-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index e7e1fc0..3329698 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -7,7 +7,7 @@ codacy-coverage~=1.3.11 codeclimate-test-reporter~=0.2.3 colorama~=0.4.1 colored~=1.3.93 -coverage~=4.5.3 +coverage~=4.0 docopt~=0.6.2 idna~=2.8 more-itertools~=7.0.0