def test_xcall__xerror_response(monkeypatch): mock_Popen = create_mock_Popen('', 'x-error response') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) with pytest.raises(xcall.XCallbackError) as excinfo: xcall.xcall('scheme', 'action') assert ("x-error callback: 'x-error response' (in response to url: " "'scheme://x-callback-url/action')" in excinfo.value)
def test_speed_or_urlcall(): t_start = time.time() # Run once to ensure ulysses is open xcall.xcall('ulysses', 'get-version') n = 10 for i in range(n): # @UnusedVariable xcall.xcall('ulysses', 'get-version') dt = time.time() - t_start time_per_run = dt / n assert time_per_run < 0.15
def test_xcall__no_parameters(monkeypatch): mock_Popen = create_mock_Popen('"ignored success response"', '') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) xcall.xcall('scheme', 'action') popen_args = mock_Popen.call_args[0][0] assert popen_args == [ xcall.XCALL_PATH, '-url', '"scheme://x-callback-url/action"' ]
def test_xcall__with_unicode_and_unsafe_html_parameters(monkeypatch): mock_Popen = create_mock_Popen('"ignored success response"', '') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) xcall.xcall('scheme', 'action', {'key1': TEST_STRING}) popen_args = mock_Popen.call_args[0][0] encoded_test_string = urllib.quote(TEST_STRING.encode('utf8')) assert popen_args == [ xcall.XCALL_PATH, '-url', u'"scheme://x-callback-url/action?key1=%s"' % encoded_test_string ]
def test_xcall__with_parameters(monkeypatch): mock_Popen = create_mock_Popen('"ignored success response"', '') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) action_parameters = OrderedDict([('key1', 'val1'), ('key2', 'val2')]) xcall.xcall('scheme', 'action', action_parameters) popen_args = mock_Popen.call_args[0][0] assert popen_args == [ xcall.XCALL_PATH, '-url', '"scheme://x-callback-url/action?key1=val1&key2=val2"' ]
def test_get_pid_of_running_xcall_processes(): # There is 20-30ms delay before xcall is started (hence the sleep) # The solid way to prevent more than one running at once would be with # a persistent lock on disk. for _ in range(10): assert len(xcall.get_pid_of_running_xcall_processes()) == 0 t = Thread(target=xcall.xcall, args=('ulysses', 'get-version')) t.start() time.sleep(.03) assert len(xcall.get_pid_of_running_xcall_processes()) == 1 with pytest.raises(AssertionError): xcall.xcall('ulysses', 'get-version') t.join()
def get_existing_notes(): search_term = datetime.now().strftime(TITLE_DAY_FORMAT) contents = xcall.xcall('bear', 'search', {"tag": PLANNER_TAG, "token": BEAR_API_TOKEN, "show_window": False, "term": search_term}) notes = json.loads(contents['notes']) if len(notes) > 0: return contents['notes'][0] return None
def open_note_for_edit(note_id: str): _ = xcall('bear', 'open-note', { 'id': note_id, 'show_window': 'yes', 'open_note': 'yes', 'edit': 'yes', })
def get_last_note(): contents = xcall.xcall('bear', 'search', {"tag": PLANNER_TAG, "token": BEAR_API_TOKEN, "show_window": "no"}) notes = json.loads(contents['notes']) last_note = None last_note_creation_date = None for note in notes: creation_date = parser.parse(note['creationDate']) if not last_note: last_note = note last_note_creation_date = creation_date else: if creation_date > last_note_creation_date: last_note = note last_note_creation_date = creation_date note = xcall.xcall('bear', 'open-note', {"id": last_note['identifier'], "new_window": "no", "show_window": "no"}) return note
def search_term(term: str) -> SearchResults: """ Search Bear for the given term. See Also: https://bear.app/faq/X-callback-url%20Scheme%20documentation/#search Args: term: Term to search for. Returns: SearchResults """ return SearchResults( xcall_dict=xcall('bear', 'search', { 'term': term, 'token': get_bear_api_token(), 'show_window': 'no', }))
def test_xcall_to_ulysses_error(): with pytest.raises(xcall.XCallbackError): xcall.xcall('ulysses', 'not-a-valid-action')
def test_xcall_to_ulysses(): d = xcall.xcall('ulysses', 'get-version') assert d['apiVersion'] >= 2
def test_xcall__success_with_unicode_and_unsafe_html_parameters(monkeypatch): mock_Popen = create_mock_Popen(ENCODED_TEST_STRING, '') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) assert xcall.xcall('scheme', 'action') == TEST_STRING
def run_xcall(scheme, action, action_parameters, arguments=None): if arguments and arguments.dryrun: params = ', '.join(f'{k}={v}' for k, v in action_parameters.items()) print(f'run_xcall({scheme}, {action}, {params})') return xcall.xcall(scheme, action, action_parameters)
def get_note(note_id: str) -> Note: return Note(xcall_dict=xcall('bear', 'open-note', { 'id': note_id, 'show_window': 'no', 'open_note': 'no', }))
def test_xcall__success(monkeypatch): mock_Popen = create_mock_Popen('"success response"', '') monkeypatch.setattr(subprocess, 'Popen', mock_Popen) assert xcall.xcall('scheme', 'action') == 'success response'
def create_daily_note(): parameters = format_note() contents = xcall.xcall('bear', 'create', parameters) return contents