Skip to content

Instantly share code, notes, and snippets.

@rg3915
Last active March 10, 2023 05:26
Show Gist options
  • Save rg3915/bd5023b6cc9aef78d6b0b485bcc903f8 to your computer and use it in GitHub Desktop.
Save rg3915/bd5023b6cc9aef78d6b0b485bcc903f8 to your computer and use it in GitHub Desktop.
report creator
import uuid
from datetime import datetime
from pprint import pprint
import click
import yaml
from utils import datetime_to_string
FILENAME = 'report_test.yml'
# limit the task_type enum add just add an valid one
COMMANDS = ['create', 'update', 'get', 'add_hour', 'finish_hour']
TASK_TYPES = ['issue', 'pr', 'meeting']
STATUS = ['pending', 'started', 'finished']
def create_item(**kwargs):
'''
Create an item
'''
now = datetime_to_string(datetime.now(), '%Y-%m-%d %H:%M')
short_uuid = uuid.uuid4().hex[:8]
item = {
'short_uuid': short_uuid,
'created_at': now,
'title': kwargs['title'],
'status': kwargs['status'],
'task_type': kwargs['task_type'],
f'{kwargs["task_type"]}_id': kwargs['id'],
'work_hours': [],
'description': kwargs['description'],
'descriptions': [],
}
return item
def add_work_hours(item):
'''
Add a new work hour in work_hours array.
Only if the last work hour has finished or work_hours is empty.
'''
work_hours = item['work_hours']
now = datetime_to_string(datetime.now(), '%Y-%m-%d %H:%M')
work_hour = {'start_time': now, 'end_time': None}
if not work_hours or work_hours[-1]['end_time']:
item['work_hours'].append(work_hour)
def finish_work_hours(item):
'''
Set the end_time for the last work hour in the list.
Only if work_hours is not empty.
'''
work_hours = item['work_hours']
if work_hours:
now = datetime_to_string(datetime.now(), '%Y-%m-%d %H:%M')
work_hours[-1]['end_time'] = now
def update_item(item, **update):
item.update(update)
def search_item(items, **search):
item_obj = None
for item in items:
found_key = True
for search_key, search_value in search.items():
if item[search_key] != search_value:
found_key = False
break
if found_key:
item_obj = item
break
return item_obj
@click.command()
# @click.option('--projeto', '-p', default='core', help='Digite o nome do projeto.')
@click.option('--command', '-c', prompt='command', help='create, update, remove, get, add_hour or finish_hour.') # noqa: E501
@click.option('--task_type', '-tt', prompt='task_type', help='issue, pr or meeting.')
@click.option('-id', help='Type id.')
@click.option('--title', '-t', help='Type the title.')
@click.option('--short_uuid', '-uuid', help='Type the uuid.')
@click.option('--item_description', help='Type the description.')
def main(command, task_type, id=None, title=None, short_uuid=None, item_description=None): # noqa: E501
'''
python report.py -c create -id 145 -tt issue --title 'Issue 145' # create
python report.py -c update --short_uuid aedc8c3e --item_description Um # update
python report.py -c get --short_uuid 13aa3aea # get
python report.py -c add_hour --short_uuid 13aa3aea # add_hour
python report.py -c finish_hour --short_uuid 13aa3aea # finish_hour
''' # noqa: E501
status = 'pending'
description = 'description'
# Load the YAML file
with open(FILENAME, 'r') as f:
data = yaml.safe_load(f)
if command not in COMMANDS:
commands = ', '.join(COMMANDS)
print(f'Type {commands}')
return
match command:
case 'create':
if task_type not in TASK_TYPES:
task_types = ', '.join(TASK_TYPES)
print(f'Type {task_types}')
return
item_init = {
'id': int(id),
'title': title,
'status': status,
'task_type': task_type,
'description': description,
}
item = create_item(**item_init)
data.append(item)
case 'update':
# check how to pass via args
search = {
'short_uuid': short_uuid,
}
item = search_item(data, **search)
# if item is not None:
# # this shoud be given from args
# update = {
# 'description': 'AEHO',
# 'task_type': 'meeting'
# }
# update_item(item, **update)
if item is not None:
# this shoud be given from args
if item_description:
item['descriptions'].append(item_description)
case 'get':
item = search_item(data, **{'short_uuid': short_uuid})
pprint(item)
case 'add_hour':
item = search_item(data, **{'short_uuid': short_uuid})
if item is not None:
add_work_hours(item)
case 'finish_hour':
item = search_item(data, **{'short_uuid': short_uuid})
if item is not None:
finish_work_hours(item)
case _:
print('AEHO')
# Save the updated data back to the YAML file
with open(FILENAME, 'w') as f:
yaml.dump(data, f)
if __name__ == '__main__':
main()
---
data:
- short_uuid: 029a7e13
created_at: '2023-03-09 20:59'
status: pending
task_type: issue
issue_id: 145
name:
first_name: Keanu
last_name: Reeves
work_hours:
- hour_start: '2023-03-09 21:00'
hour_end: '2023-03-09 21:10'
- hour_start: '2023-03-09 22:00'
hour_end: '2023-03-09 22:15'
title: Título
description: Descrição
- short_uuid: 029a7g14
created_at: '2023-03-09 20:59'
status: pending
task_type: pr
pr_id: 100
name:
first_name: Keanu
last_name: Reeves
work_hours:
- hour_start: '2023-03-09 21:00'
hour_end: '2023-03-09 21:10'
- hour_start: '2023-03-09 22:00'
hour_end: '2023-03-09 22:15'
title: Título
description: Descrição
- short_uuid: 029a7f16
created_at: '2023-03-09 20:59'
status: finished
task_type: reuniao
reuniao_id: 101
work_hours:
- hour_start: '2023-03-09 21:00'
hour_end: '2023-03-09 21:10'
title: Reunião
description: Reunião sobre modelagem de dados do Article
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment