~mapperr/trl

dcc85249eafb097da0ed5d0795ddd0a11be6811c — mapperr 2 years ago 65a3ac8 0.3.0
Change matching algorithm

Now matching substrings in normalized names
M trullo.py => trullo.py +23 -17
@@ 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>']:
            board_shortcut = args['<board_shortcut>']
            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['<list_shortcut>']
            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['<card_shortcut>']
            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['<list_shortcut>']

                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

M trullo/printer.py => trullo/printer.py +14 -34
@@ 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

A trullo/shortcuttable.py => trullo/shortcuttable.py +10 -0
@@ 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

M trullo/shortener.py => trullo/shortener.py +16 -6
@@ 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()]

M trullo/trl_board.py => trullo/trl_board.py +2 -1
@@ 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]

M trullo/trl_card.py => trullo/trl_card.py +2 -1
@@ 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

M trullo/trl_list.py => trullo/trl_list.py +2 -1
@@ 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