2
0
mirror of https://github.com/mhinz/neovim-remote synced 2024-11-11 13:10:34 +00:00

Refactoring: sanitize address

This commit is contained in:
Marco Hinz 2017-12-03 14:57:26 +01:00
parent 83b9d014f7
commit df89d17f56
No known key found for this signature in database
GPG Key ID: 1C980A1B657B4A4F

View File

@ -61,17 +61,9 @@ class Neovim():
if self.server:
return True
address = self.address
if get_address_type(address) == 'socket' and os.path.exists(address):
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(address)
except:
with tempfile.NamedTemporaryFile(dir='/tmp', prefix='nvimsocket_') as f:
self.address = f.name
if not silent and not self._msg_shown:
self._show_msg(address)
self._show_msg(self.address)
self._msg_shown = True
return False
@ -170,6 +162,23 @@ class Neovim():
""".format(o, o, o, o, self.address)))
def sanitize_address(address, env):
if not address:
address = env.get('NVIM_LISTEN_ADDRESS')
if not address:
address = '/tmp/nvimsocket'
if get_address_type(address) == 'socket' and os.path.exists(address):
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect(address)
except:
with tempfile.NamedTemporaryFile(dir='/tmp', prefix='nvimsocket_') as f:
address = f.name
return address
def parse_args(argv):
form_class = argparse.RawDescriptionHelpFormatter
usage = '{} [arguments]'.format(argv[0])
@ -363,6 +372,8 @@ def main(argv=sys.argv, env=os.environ):
print_sockaddrs()
return
address = sanitize_address(options.servername, env)
nvim = Neovim(address, options.s)
nvim.attach()