Skip to content

WIP Development #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions commit_helper/__main__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions commit_helper/conventions/atom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def atom_convention(tag, msg):
"""
Formats the commit following the atom convention format:
The atom convention:

:<tag as emoticon>: <Message>
"""
tag = tag.lower()
msg = msg.capitalize()
composed_message = ":%s: %s\n" % (tag, msg)
Expand Down
12 changes: 8 additions & 4 deletions commit_helper/conventions/convention_help_handler.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion commit_helper/conventions/custom_convention.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 6 additions & 0 deletions commit_helper/conventions/karma_angular.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def karma_angular_convention(tag, msg, context):
"""
Handles the formatting for both karma and angular conventions.

Convention structure:
<tag>(<scope>): <message>
"""
tag = tag.lower()
if context == '':
composed_message = "%s: %s\n" % (tag, msg)
Expand Down
3 changes: 3 additions & 0 deletions commit_helper/conventions/no_convention.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@


def just_message(msg=''):
"""
Formats the message to capitalize.
"""
if msg == '':
message = str(input(INPUT_COLOR + "commit message: " + RESET))
else:
Expand Down
6 changes: 6 additions & 0 deletions commit_helper/conventions/symphony_cmf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def symphony_convention(tag, msg):
"""
Formats the commit following the symphony convention.

The symphony CMF convention:
[<Tag>] <message>
"""
tag = tag.capitalize()
composed = "[%s] %s\n" % (tag, msg)
return composed
Expand Down
6 changes: 6 additions & 0 deletions commit_helper/conventions/tagged.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
def tagged_convention(tag, msg):
"""
Formats the convention following the tag convention.

The tagged convention:
<TAG>: <message>
"""
tag = tag.upper()
composed_message = "%s: %s\n" % (tag, msg)
return composed_message
Expand Down
12 changes: 10 additions & 2 deletions commit_helper/utils/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
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
from .utils import gen_co_author
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)
Expand Down Expand Up @@ -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')

Expand Down
8 changes: 5 additions & 3 deletions commit_helper/utils/flag_commit_handler.py
Original file line number Diff line number Diff line change
@@ -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 = ''
Expand Down
25 changes: 25 additions & 0 deletions commit_helper/utils/text_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
63 changes: 45 additions & 18 deletions commit_helper/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@


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


# 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 = \
"""
Expand All @@ -48,62 +58,79 @@ 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()


# 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 = ''

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
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down