ericmandel / pyds9

Python connection to SAOimage DS9 via XPA
76 stars 41 forks source link

Look for xpans and ds9 executables or Aqua app at the start #29

Closed montefra closed 8 years ago

montefra commented 8 years ago

When importing, pyds9 searches for the xpans and ds9 executable. On OSX, if ds9 is not found, search for SAOImage DS9.app in \Applications, $HOME, $HOME\Applications.

If no xpans and/or ds9 is found a warning is issued.

The calls to the two executable are streamlined, as we now already have they absolute path.

montefra commented 8 years ago

Can anyone, on OSX with the Aqua DS9 version only, check if this version works?

It's possible to check if the correct ds9 version is picked before running it with

import pyds9
pyds9.get_xpans_ds9()
cdeil commented 8 years ago

Using this branch, I get this on OS X with only Aqua DS9:

$ python3
>>> import pyds9
>>> pyds9.get_xpans_ds9()
('/Users/deil/Library/Python/3.4/lib/python/site-packages/xpans', ['open', '-a', '/Applications/SAOImage DS9.app'])

Executing the same code with -c gives consistently this error though:

$ python -c "import pyds9; pyds9.get_xpans_ds9()"

An instance of ds9 was found to be running before we could
start the 'xpans' name server. You will need to perform a
bit of manual intervention in order to connect this
existing ds9 to Python.

For ds9 version 5.7 and beyond, simply register the
existing ds9 with the xpans name server by selecting the
ds9 File->XPA->Connect menu option. Your ds9 will now be
fully accessible to pyds9 (e.g., it appear in the list
returned by the ds9_targets() routine).

For ds9 versions prior to 5.7, you cannot (easily) register
with xpans, but you can view ds9's File->XPA Information
menu option and pass the value associated with XPA_METHOD
directly to the Python DS9() constructor, e.g.:

    d = DS9('a000101:12345')

The good news is that new instances of ds9 will be
registered with xpans, and will be known to ds9_targets()
and the DS9() constructor.

And I also get this:

$ python -c "import pyds9; pyds9.DS9()"

An instance of ds9 was found to be running before we could
start the 'xpans' name server. You will need to perform a
bit of manual intervention in order to connect this
existing ds9 to Python.

For ds9 version 5.7 and beyond, simply register the
existing ds9 with the xpans name server by selecting the
ds9 File->XPA->Connect menu option. Your ds9 will now be
fully accessible to pyds9 (e.g., it appear in the list
returned by the ds9_targets() routine).

For ds9 versions prior to 5.7, you cannot (easily) register
with xpans, but you can view ds9's File->XPA Information
menu option and pass the value associated with XPA_METHOD
directly to the Python DS9() constructor, e.g.:

    d = DS9('a000101:12345')

The good news is that new instances of ds9 will be
registered with xpans, and will be known to ds9_targets()
and the DS9() constructor.

open: invalid option -- i
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
      By default, opens each file using the default application for that file.  
      If the file is in the form of a URL, the file will be opened as a URL.
Options: 
      -a                Opens with the specified application.
      -b                Opens with the specified application bundle identifier.
      -e                Opens with TextEdit.
      -t                Opens with default text editor.
      -f                Reads input from standard input and opens with TextEdit.
      -F  --fresh       Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
      -R, --reveal      Selects in the Finder instead of opening.
      -W, --wait-apps   Blocks until the used applications are closed (even if they were already running).
          --args        All remaining arguments are passed in argv to the application's main() function instead of opened.
      -n, --new         Open a new instance of the application even if one is already running.
      -j, --hide        Launches the app hidden.
      -g, --background  Does not bring the application to the foreground.
      -h, --header      Searches header file locations for headers matching the given filenames, and opens them.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/deil/Library/Python/3.4/lib/python/site-packages/pyds9.py", line 449, in __init__
    raise ValueError('no active ds9 running for target: %s' % target)
ValueError: no active ds9 running for target: ds9

I don't have ds9 running ... seems like it's not starting one up correctly?

montefra commented 8 years ago

First: thank you for checking.

When importing pyds9, without the environment variable PYDS9_NOXPANS, the function ds9_xplans is called, and this is the one writing the long thing starting with "An instance of ds9 was found".

About the second part: I'll try to figure out how "open -a" works in a python subprocess. For sure I forgot [--args arguments].

I'll try to fix it as soon as possible.

cdeil commented 8 years ago

Is it possible to execute these things on a Mac on travis-ci to test if it works and make sure it keeps working?

montefra commented 8 years ago

@cdeil probably. However I don't know how to test xpa/ds9 in an automatic way. However should be possible to at least test installation and a few things.

Adding some proper test and travis is in the list of todos on the devel branch. As I'm heavily changing the whole structure, I would rather do it there.

ericmandel commented 8 years ago

@montefra Happy New Year! I am back from a few weeks away from computers and will look into this next week. Thanks for doing the initial work!

montefra commented 8 years ago

@cdeil @ericmandel : happy new year. I'm back to work too.

@cdeil: I'll try to figure out how to run open from python subprocess. I think that it doesn't like the space before DS9 so much.

cdeil commented 8 years ago

@montefra - Concerning tests on travis-ci, I saw this: https://github.com/sherpa/sherpa/pull/140

There's some magic setup incantation with DISPLAY xvfb that might be helpful to get ds9 behaving properly on travis-ci: https://github.com/sherpa/sherpa/pull/140/files#diff-354f30a63fb0907d4ad57269548329e3R63

montefra commented 8 years ago

@cdeil thank you very much for the link. When I'll begin to work on the tests, I'll look into sherpa.

ericmandel commented 8 years ago

@montefra Your new code works fine on my Mac with two simple changes, which I list below:

eric% diff pyds9.py{~,}
79c79,80
<                   os.path.join(user_dir, 'Applications')]:
---
>                   os.path.join(user_dir, 'Applications'),
>                   os.path.join(user_dir, 'Desktop')]:
439c440
<             self.pid = subprocess.Popen(ds9Globals["progs"][1] +
---
>             self.pid = subprocess.Popen(ds9Globals["progs"][1] + ['--args'] +

The first change allows users to install the DS9 app in their Desktop directory, which I have seen done frequently. The second change tells the Mac open command that the arguments that follow go with the DS9 program. (As an aside, I never understand whether one should use single or double quotes in Python, but I recoil against having both in the same line of code!)

Nicely done!

montefra commented 8 years ago

@ericmandel : thank you for investigating the issue on Mac.

(As an aside, I never understand whether one should use single or double quotes in Python, but I recoil against having both in the same line of code!)

they are the same. I keep mixing them, although this is not stylistically the best. I'll try to be consistent.

ericmandel commented 8 years ago

they are the same. I keep mixing them, although this is not stylistically the best. I'll try to be consistent.

But my question is about Python best practice: do Python programmers care about not mixing single and double quotes? Because if mixing is standard practice, I would not have you change it ...

cdeil commented 8 years ago

There is no preference for single or double quotes for strings in the official Python style guide: https://www.python.org/dev/peps/pep-0008/#string-quotes

do Python programmers care about not mixing single and double quotes?

Probably some do and some don't care. I've been using Python for years and don't remember discussions on this. Maybe people that just to Python use single-quotes, and people that use other languages that use double quotes (like C / HTML / JSON) use double quotes.

ericmandel commented 8 years ago

Interesting ... the doc you referenced says:

In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it.

Well, this is not a big deal and should not detract from recognizing the really good work that @montefra is doing!

montefra commented 8 years ago

@ericmandel :blush: Thank you for the support

Tomorrow I try to test on a Mac if the latest commit works. If there are no objection I'll merge the changes into master in the next few days.

ericmandel commented 8 years ago

Yes, that will be fine with me. It will be an improvement over the current situation in which you need the X11 version.

montefra commented 8 years ago

Yesterday I successfully tried on a machine with OSX10.8.x. I managed to find and open ds9 from the path, for the app in ~/Applications and ~/Desktop.

I'll merge this branch into master next week and tag it as a bugfix: 1.8.1.

But first I have a question about languages. If the language is not English, the directories to look for the app are not called Applications and, maybe, Desktop. On my old Snow Leopard, with Italian as language, the former appears as Applicazioni in Finder. However IIRC, the directories had the English name on the console: i.e. there was no localisation there.

If this is still the case and subprocess uses the original names in English, than it's fine. Otherwise we'll have to find out how to localize get_xpans_ds9

ericmandel commented 8 years ago

It appears that the OS-level uses the English names, leaving it to the Finder to localize at the GUI level:

http://stackoverflow.com/questions/28546799/get-special-folder-localized-name-in-os-x

Nice work on this, it will be a big help for DS9 Aqua users.

montefra commented 8 years ago

@ericmandel: thanks for the answer.

I'll merge the branch and tag the master as 1.8.1 release by tonight.