~mapperr/trl

ref: f9a6cd5ee6afd98e66b2adee799ee8be0ae18b11 trl/trullo/usecases.py -rw-r--r-- 6.9 KiB
f9a6cd5e — mapperr Refactor and tests 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import subprocess
import tempfile
import urllib
from typing import Optional, Tuple, List

import attr

from trullo.printer import Printer
from trullo.shortener import Shortener
from trullo.tclient import TClient
from trullo.tconfig import TConfig
from trullo.trl_board import TrlBoard
from trullo.trl_card import TrlCard
from trullo.trl_list import TrlList


@attr.s(auto_attribs=True)
class Usecases:
    tconfig: TConfig
    tclient: TClient
    shortener: Shortener
    printer: Printer
    selected_board_id: Optional[str] = attr.ib(default=None)
    selected_board_name: Optional[str] = attr.ib(default=None)

    def get_selected_board(self) -> Optional[Tuple[str, str]]:
        if os.path.exists(self.tconfig.selected_board_filepath):
            with open(self.tconfig.selected_board_filepath, 'r') as fh:
                selected_board_id, selected_board_name = \
                    fh.readline().split(' ', 1)
                self.selected_board_id = selected_board_id
                self.selected_board_name = selected_board_name
                return selected_board_id, selected_board_name

    def print_board_list(self):
        boards = self.tclient.get_boards()
        self.printer.print_boards(boards)
        if os.path.exists(self.tconfig.selected_board_filepath):
            print(f'\ncurrently selected board: {self.selected_board_name}')
        else:
            print(f'\nselect a board with `trl b <board_shortcut>`')

    def select_board(self, board_shortcut: str):
        boards = self.tclient.get_boards()
        matching_boards: List[TrlBoard] = self.shortener.get_matches(
            board_shortcut,
            boards
        )
        if len(matching_boards) > 1:
            matching_names = \
                [board.get_normalized_name() for board in matching_boards]
            print(
                f'shortcut [{board_shortcut}] matches more than one board: '
                f'{matching_names}')
            exit(1)
        board = matching_boards[0]
        print(f'selected board {board.raw_data["name"]}')
        with open(self.tconfig.selected_board_filepath, 'w') as fh:
            fh.write(f'{board.id} {board.raw_data["name"]}')

    def print_board_lists(self):
        board = self.tclient.get_board(self.selected_board_id)
        self.printer.print_board_lists(board)

    def print_lists(self, lists_shortcuts: Optional[List[str]]):
        board = self.tclient.get_board(self.selected_board_id)
        if lists_shortcuts is not None and len(lists_shortcuts) > 0:
            Printer.print_board(board, lists_shortcuts)
        else:
            Printer.print_board(board)

    def print_card(self, card_shortcut: str):
        card = self._get_card(card_shortcut)
        self.printer.print_card(card)

    def open_card_in_browser(self, card_shortcut: str):
        card = self._get_card(card_shortcut)
        subprocess.Popen(['xdg-open', card.raw_data['shortUrl']])

    def create_card(self, target_list_shortcut: str):
        board = self.tclient.get_board(self.selected_board_id)
        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]
            print(
                f'shortcut [{target_list_shortcut}] '
                f'matches more than one list: '
                f'{matching_names}')
            exit(1)

        if len(matching_lists) == 0:
            print(
                f'shortcut [{target_list_shortcut}] '
                f'does not match any list')
            exit(1)

        list_id = matching_lists[0].id

        new_card_name, new_card_desc = self._edit_card()
        self.tclient.new_card(list_id, new_card_name, new_card_desc)

    def update_card(self, card_shortcut: str):
        card = self._get_card(card_shortcut)
        card_new_name, card_new_desc = self._edit_card(card)
        self.tclient.edit_card(card.id, card_new_name, card_new_desc)

    def move_card(self, card_shortcut: str, target_list_shortcut: str):
        board = self.tclient.get_board(self.selected_board_id)
        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]
            print(
                f'shortcut [{target_list_shortcut}] '
                f'matches more than one list: '
                f'{matching_names}')
            exit(1)

        if len(matching_lists) == 0:
            print(
                f'shortcut [{target_list_shortcut}] '
                f'does not match any list')
            exit(1)

        list_id = matching_lists[0].id

        card = self._get_card(card_shortcut)

        self.tclient.move_card(card.id, list_id)

    def _get_card(self, card_shortcut):
        board = self.tclient.get_board(self.selected_board_id)
        matching_cards: List[TrlCard] = Shortener.get_matches(
            card_shortcut,
            board.cards
        )
        if len(matching_cards) > 1:
            matching_names = \
                [matching_card.get_normalized_name()
                 for matching_card in matching_cards]
            print(
                f'shortcut [{card_shortcut}] '
                f'matches more than one card: '
                f'{matching_names}')
            exit(1)

        if len(matching_cards) == 0:
            print(
                f'shortcut [{card_shortcut}] '
                f'does not match any cards')
            exit(1)

        selected_card = matching_cards[0]
        card = self.tclient.get_card(selected_card.id)
        return card

    def _edit_card(self, card_to_edit: TrlCard = None) -> (str, str):
        """
        :param card_to_edit:
        :return: a Tuple with the new name and description of the card
        """
        tempfile_suffix = 'newcard.md'
        clean_card_name = 'New Card Title'
        card_description = 'New Card Description'
        if card_to_edit is not None:
            tempfile_suffix = f'{card_to_edit.id}.md'
            clean_card_name = str(card_to_edit.raw_data['name']).replace('\n',
                                                                         '')
            card_description = card_to_edit.raw_data['desc']

        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, "
                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:
            lines = fd.readlines()
        return urllib.parse.quote(lines[1].replace('\n', ''), safe=''), \
               urllib.parse.quote(str.join('', lines[2:]), safe='')