From dcc85249eafb097da0ed5d0795ddd0a11be6811c Mon Sep 17 00:00:00 2001 From: mapperr Date: Sat, 14 Dec 2019 17:58:39 +0100 Subject: [PATCH] Change matching algorithm Now matching substrings in normalized names --- trullo.py | 40 +++++++++++++++++++--------------- trullo/printer.py | 48 ++++++++++++----------------------------- trullo/shortcuttable.py | 10 +++++++++ trullo/shortener.py | 22 +++++++++++++------ trullo/trl_board.py | 3 ++- trullo/trl_card.py | 3 ++- trullo/trl_list.py | 3 ++- 7 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 trullo/shortcuttable.py diff --git a/trullo.py b/trullo.py index 2f7f18d..1768853 100644 --- a/trullo.py +++ b/trullo.py @@ -51,14 +51,18 @@ import pprint import subprocess import tempfile import urllib +from typing import List from docopt import docopt from trullo.printer import Printer +from trullo.shortener import Shortener from trullo.tclient import TClient +from trullo.trl_board import TrlBoard from trullo.trl_card import TrlCard - # logging.basicConfig(level=logging.DEBUG) +from trullo.trl_list import TrlList + logging.basicConfig(level=logging.INFO) logging.getLogger('urllib3.connectionpool').setLevel(logging.INFO) @@ -81,7 +85,8 @@ def edit_card(card_to_edit: TrlCard = None) -> (str, str): tmpfile_path = f'{tempfile.gettempdir()}/.trl-{tempfile_suffix}' with open(tmpfile_path, 'w') as fd: fd.writelines( - f"# The line below is the card title, lines after that are the card description\n" + f"# The line below is the card title, " + f"lines after that are the card description\n" f"{clean_card_name}\n{card_description}") subprocess.Popen([os.environ.get('EDITOR'), tmpfile_path]).wait() with open(tmpfile_path, 'r') as fd: @@ -111,9 +116,10 @@ if __name__ == '__main__': boards = tclient.get_boards() if args['']: board_shortcut = args[''] - matching_boards = [board for board in boards - if board.get_normalized_name() - .startswith(board_shortcut)] + matching_boards: List[TrlBoard] = Shortener.get_matches( + board_shortcut, + boards + ) if len(matching_boards) > 1: matching_names = \ [board.get_normalized_name() for board in matching_boards] @@ -154,9 +160,10 @@ if __name__ == '__main__': new_command = args['n'] if new_command: target_list_shortcut = args[''] - matching_lists = [list_ for list_ in board.lists if - list_.get_normalized_name().startswith( - target_list_shortcut)] + matching_lists: List[TrlList] = Shortener.get_matches( + target_list_shortcut, + board.lists + ) if len(matching_lists) > 1: matching_names = \ [list_.get_normalized_name() for list_ in matching_lists] @@ -171,11 +178,10 @@ if __name__ == '__main__': tclient.new_card(list_id, new_card_name, new_card_desc) else: card_shortcut = args[''] - matching_cards = [ - card - for card in board.cards - if card.get_normalized_name().startswith(card_shortcut) - ] + matching_cards: List[TrlCard] = Shortener.get_matches( + card_shortcut, + board.cards + ) if len(matching_cards) > 1: matching_names = \ @@ -197,10 +203,10 @@ if __name__ == '__main__': subprocess.Popen(['xdg-open', card.raw_data['shortUrl']]) elif move_command: target_list_shortcut = args[''] - - matching_lists = [list_ for list_ in board.lists if - list_.get_normalized_name().startswith( - target_list_shortcut)] + matching_lists: List[TrlList] = Shortener.get_matches( + target_list_shortcut, + board.lists + ) if len(matching_lists) > 1: matching_names = \ [list_.get_normalized_name() for list_ in diff --git a/trullo/printer.py b/trullo/printer.py index a3a31e9..78175c0 100644 --- a/trullo/printer.py +++ b/trullo/printer.py @@ -11,33 +11,12 @@ from trullo.trl_card import TrlCard class Printer: @staticmethod def print_boards(boards: List[TrlBoard]): - symbol_count = Shortener. \ - get_min_symbols_to_uniq([board.get_normalized_name() - for board in boards]) - print(f'symbol count is {symbol_count}') for board in boards: - print( - f"[{board.get_normalized_name()[0:symbol_count].lower()}] " - f"{board.raw_data['name']}") - - @staticmethod - def there_is_a_match(normalized_name: str, shortcuts: List[str]) -> bool: - return len([ - shortcut for shortcut in shortcuts - if normalized_name.startswith(shortcut) - ]) > 0 + print(f"- {board.raw_data['name']}") @staticmethod def print_board(board: TrlBoard, list_shortcuts: Optional[List[str]] = None): - cards_names = [card.get_normalized_name() for card in board.cards] - board_lists_names = \ - [list_.get_normalized_name() for list_ in board.lists] - - symbol_count_cards = Shortener.get_min_symbols_to_uniq(cards_names) - symbol_count_lists = \ - Shortener.get_min_symbols_to_uniq(board_lists_names) - print(f"{board.raw_data['shortUrl']}") print('------------------------------') print(f"{board.raw_data['name']}") @@ -47,19 +26,15 @@ class Printer: if list_shortcuts is not None and len(list_shortcuts) > 0: matching_lists = [ list_ for list_ in board.lists - if Printer.there_is_a_match(list_.get_normalized_name(), - list_shortcuts) + if Printer._there_is_a_match( + list_.get_normalized_name(), + list_shortcuts) ] for list_ in matching_lists: - shortcut = \ - list_.get_normalized_name()[0:symbol_count_lists].lower() - print(f"[{shortcut}] {list_.raw_data['name']}") + print(f"{list_.raw_data['name']}") for card in board.cards: if card.raw_data['idList'] == list_.id: - card_shortcut = card.get_normalized_name()[ - 0:symbol_count_cards].lower() - print(f"\t[{card_shortcut}] " - f"{card.raw_data['name']}") + print(f"\t- {card.raw_data['name']}") print() @staticmethod @@ -72,9 +47,7 @@ class Printer: print() if board.lists is not None: for list_ in board.lists: - list_shortcut = \ - list_.get_normalized_name()[0:symbol_count_lists].lower() - print(f"[{list_shortcut}] {list_.raw_data['name']}") + print(f"- {list_.raw_data['name']}") print() @staticmethod @@ -95,3 +68,10 @@ class Printer: print() print(formatted_desc) print() + + @staticmethod + def _there_is_a_match(normalized_name: str, shortcuts: List[str]) -> bool: + return len([ + shortcut for shortcut in shortcuts + if normalized_name.startswith(shortcut) + ]) > 0 diff --git a/trullo/shortcuttable.py b/trullo/shortcuttable.py new file mode 100644 index 0000000..f93221e --- /dev/null +++ b/trullo/shortcuttable.py @@ -0,0 +1,10 @@ +from abc import abstractmethod + +import attr + + +@attr.s(auto_attribs=True) +class Shortcuttable: + @abstractmethod + def get_normalized_name(self) -> str: + pass diff --git a/trullo/shortener.py b/trullo/shortener.py index 6b12300..ef37fc0 100644 --- a/trullo/shortener.py +++ b/trullo/shortener.py @@ -1,7 +1,10 @@ from typing import List +from typing import Set import attr +from trullo.shortcuttable import Shortcuttable + @attr.s(auto_attribs=True) class Shortener: @@ -32,8 +35,8 @@ class Shortener: return symbol_counter @staticmethod - def are_there_duplicates(list_): - set_ = set() + def are_there_duplicates(list_: List[str]): + set_: Set[str] = set() for elem in list_: if elem in set_: return True @@ -43,9 +46,16 @@ class Shortener: @staticmethod def get_longest_item_length(list_: List[str]) -> int: - longest = 0 + longest_length = 0 for string in list_: length = len(string) - if length > longest: - longest = length - return longest + if length > longest_length: + longest_length = length + return longest_length + + @staticmethod + def get_matches(shortcut: str, + shortcuttables: List[Shortcuttable]) \ + -> List[any]: + return [shortcuttable for shortcuttable in shortcuttables + if shortcut in shortcuttable.get_normalized_name()] diff --git a/trullo/trl_board.py b/trullo/trl_board.py index f313fd2..0a94c85 100644 --- a/trullo/trl_board.py +++ b/trullo/trl_board.py @@ -2,13 +2,14 @@ from typing import Dict, List import attr +from trullo.shortcuttable import Shortcuttable from trullo.shortener import Shortener from trullo.trl_list import TrlList from trullo.trl_card import TrlCard @attr.s(auto_attribs=True) -class TrlBoard: +class TrlBoard(Shortcuttable): id: str short_link: str lists: List[TrlList] diff --git a/trullo/trl_card.py b/trullo/trl_card.py index 6bb7443..2d9879e 100644 --- a/trullo/trl_card.py +++ b/trullo/trl_card.py @@ -2,11 +2,12 @@ from typing import Dict import attr +from trullo.shortcuttable import Shortcuttable from trullo.shortener import Shortener @attr.s(auto_attribs=True) -class TrlCard: +class TrlCard(Shortcuttable): id: str short_link: str raw_data: Dict diff --git a/trullo/trl_list.py b/trullo/trl_list.py index 0a02978..d0a3100 100644 --- a/trullo/trl_list.py +++ b/trullo/trl_list.py @@ -2,11 +2,12 @@ from typing import Dict import attr +from trullo.shortcuttable import Shortcuttable from trullo.shortener import Shortener @attr.s(auto_attribs=True) -class TrlList: +class TrlList(Shortcuttable): id: str raw_data: Dict -- 2.34.2