Skip to content

Commit

Permalink
Update to Python3 to avoid warnings on Big Sur & Monterey
Browse files Browse the repository at this point in the history
  • Loading branch information
isometry committed Nov 17, 2021
1 parent 4e37cee commit 5c11e32
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
._*
*.py[cod]
*~
ssh.alfred3workflow
ssh.alfred*workflow
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
all:
zip -j9 --filesync ssh.alfred3workflow *.{plist,png,py}
zip -j9 --filesync ssh.alfredworkflow *.{plist,png,py}
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ A workflow for [Alfred](http://www.alfredapp.com/) Powerpack users to rapidly op
## Releases

- [v1.3 for Alfred 2.4+](https://github.com/isometry/alfred-ssh/releases/tag/v1.3)
- [v2.x for Alfred 3.1+](https://github.com/isometry/alfred-ssh/releases/latest)
- [v2.3 for Alfred 3.1+](https://github.com/isometry/alfred-ssh/releases/tag/v2.3)
- [v3.x for Alfred 4.0+](https://github.com/isometry/alfred-ssh/releases/latest)

## Prerequisites

- [Alfred](http://www.alfredapp.com/) (version 2.4+/3.1+)
- [Alfred](http://www.alfredapp.com/) (version 2.4+/3.1+/4.0+)
- The [Alfred Powerpack](http://www.alfredapp.com/powerpack/).
- Python3 for v3.x+ (most easily installed/maintained with `sudo xcode-select --install` or [Homebrew](https://brew.sh/))
- (optional) [pybonjour](https://pypi.python.org/pypi/pybonjour)

## Usage

Type `ssh` in Alfred followed by either a literal hostname or by some letters from the hostname of a host referenced in any of `~/.ssh/known_hosts`, `~/.ssh/config`, `/etc/hosts`, or (with `pybonjour` installed) Bonjour.

Alfred 3 only: workflow configuration is available by setting/changing Workflow Environment Variables (accessed via the [𝓍] button within the workflow):
Alfred 3+ only: workflow configuration is available by setting/changing Workflow Environment Variables (accessed via the [𝓍] button within the workflow):

- disable unwanted sources by setting the associated Workflow Environment Variable to 0; e.g. `alfredssh_known_hosts`, `alfredssh_ssh_config`, `alfredssh_hosts`, `alfredssh_bonjour`.
- change the maximum number of returned results by changing `alfredssh_max_results` (default=36).
Expand Down
31 changes: 19 additions & 12 deletions alfredssh.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#!/usr/bin/env python2.7
#-*- coding: utf-8 -*-
# ssh.alfredworkflow, v2.2
# Robin Breathe, 2013-2017
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ssh.alfredworkflow, v3.0
# Robin Breathe, 2013-2021

from __future__ import unicode_literals
from __future__ import print_function

import json, re, sys, os
import json
import re
import sys
import os

from collections import defaultdict
from time import time

DEFAULT_MAX_RESULTS=36
DEFAULT_MAX_RESULTS = 36


class Hosts(defaultdict):
def __init__(self, query, user=None):
super(Hosts, self).__init__(list)
self[query].append('input')
self.query = query
self.user = user
self.user = user

def merge(self, source, hosts=()):
for host in hosts:
Expand All @@ -33,7 +34,7 @@ def _alfred_item(self, host, source):
"title": _uri,
"subtitle": _sub,
"arg": _arg,
"icon": { "path": "icon.png" },
"icon": {"path": "icon.png"},
"autocomplete": _arg
}

Expand All @@ -44,6 +45,7 @@ def alfred_json(self, _filter=(lambda x: True), maxresults=DEFAULT_MAX_RESULTS):
]
return json.dumps({"items": items[:maxresults]})


def cache_file(filename, volatile=True):
parent = os.path.expanduser(
(
Expand All @@ -57,6 +59,7 @@ def cache_file(filename, volatile=True):
raise IOError('No write access: %s' % parent)
return os.path.join(parent, filename)


def fetch_file(file_path, cache_prefix, parser, env_flag):
"""
Parse and cache a file with the named parser
Expand Down Expand Up @@ -91,6 +94,7 @@ def fetch_file(file_path, cache_prefix, parser, env_flag):
# Return results
return (file_path, results)


def parse_file(open_file, parser):
parsers = {
'ssh_config':
Expand Down Expand Up @@ -122,6 +126,7 @@ def parse_file(open_file, parser):
}
return set(parsers[parser])


def fetch_bonjour(_service='_ssh._tcp', alias='Bonjour', timeout=0.1):
if int(os.getenv('alfredssh_bonjour', 1)) != 1:
return (alias, ())
Expand All @@ -132,7 +137,7 @@ def fetch_bonjour(_service='_ssh._tcp', alias='Bonjour', timeout=0.1):
try:
from pybonjour import DNSServiceBrowse, DNSServiceProcessResult
from select import select
bj_callback = lambda s, f, i, e, n, t, d: results.add('{}.{}'.format(n.lower(), d[:-1]))
def bj_callback(s, f, i, e, n, t, d): return results.add('{}.{}'.format(n.lower(), d[:-1]))
bj_browser = DNSServiceBrowse(regtype=_service, callBack=bj_callback)
select([bj_browser], [], [], timeout)
DNSServiceProcessResult(bj_browser)
Expand All @@ -142,6 +147,7 @@ def fetch_bonjour(_service='_ssh._tcp', alias='Bonjour', timeout=0.1):
json.dump(list(results), open(cache, 'w'))
return (alias, results)


def complete():
query = ''.join(sys.argv[1:])
maxresults = int(os.getenv('alfredssh_max_results', DEFAULT_MAX_RESULTS))
Expand Down Expand Up @@ -174,5 +180,6 @@ def complete():

return hosts.alfred_json(pattern.search, maxresults=maxresults)


if __name__ == '__main__':
print(complete())
Loading

0 comments on commit 5c11e32

Please sign in to comment.