Skip to content

Commit 144bf1b

Browse files
committed
try to avoid "ImportError: attempted relative import..." in update; address Spyder warnings
1 parent 46c21b0 commit 144bf1b

File tree

2 files changed

+129
-10
lines changed

2 files changed

+129
-10
lines changed

GSASII/GSASIIfiles.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ def FormatSigFigs(val, maxdigits=10, sigfigs=5, treatAsZero=1e-20):
14841484
return str(val)
14851485

14861486
#===========================================================================
1487+
# N.B. duplicated in GSASIIpath
14871488
def openInNewTerm(project=None,g2script=None,pythonapp=sys.executable):
14881489
'''Open a new and independent GSAS-II session in separate terminal
14891490
or console window and as a separate process that will continue

GSASII/GSASIIpath.py

Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def saveGitHubVersion():
368368
'''
369369
try:
370370
import requests
371+
requests
371372
except:
372373
print('Unable to use requests module')
373374
return
@@ -1341,7 +1342,7 @@ def XferConfigIni():
13411342
if i.startswith('__') and i.endswith('__'): continue
13421343
if isinstance(config.__dict__[i],types.ModuleType): continue
13431344
configDict.update({i:str(config.__dict__[i])})
1344-
except ImportError as err:
1345+
except ImportError:
13451346
print("New install: start without a config.py file")
13461347
return
13471348
except Exception as err:
@@ -1360,7 +1361,7 @@ def XferConfigIni():
13601361
XferConfigIni()
13611362
try:
13621363
from . import config_example
1363-
except ImportError as err:
1364+
except ImportError:
13641365
try:
13651366
import GSASII.config_example as config_example
13661367
except ImportError as err:
@@ -1897,6 +1898,122 @@ def postURL(URL,postdict,getcookie=None,usecookie=None,
18971898
else:
18981899
return None
18991900

1901+
#===========================================================================
1902+
# duplicated from GSASIIfiles to avoid using an import below
1903+
# perhaps the references to G2fil.openInNewTerm() should be
1904+
# changed to reference this here.
1905+
def openInNewTerm(project=None,g2script=None,pythonapp=sys.executable):
1906+
'''Open a new and independent GSAS-II session in separate terminal
1907+
or console window and as a separate process that will continue
1908+
even if the calling process exits.
1909+
Intended to work on all platforms.
1910+
1911+
This could be used to run other scripts inside python other than GSAS-II
1912+
1913+
:param str project: the name of an optional parameter to be
1914+
passed to the script (usually a .gpx file to be opened in
1915+
a new GSAS-II session)
1916+
:param str g2script: the script to be run. If None (default)
1917+
the G2.py file in the same directory as this file will
1918+
be used.
1919+
:param str pythonapp: the Python interpreter to be used.
1920+
Defaults to sys.executable which is usually what is wanted.
1921+
:param str terminal: a name for a preferred terminal emulator
1922+
'''
1923+
#import subprocess
1924+
if g2script is None:
1925+
g2script = os.path.join(os.path.dirname(__file__),'G2.py')
1926+
1927+
if sys.platform == "darwin":
1928+
if project:
1929+
script = f'''
1930+
set python to "{pythonapp}"
1931+
set appwithpath to "{g2script}"
1932+
set filename to "{project}"
1933+
set filename to the quoted form of the POSIX path of filename
1934+
1935+
tell application "Terminal"
1936+
activate
1937+
do script python & " " & appwithpath & " " & filename & "; exit"
1938+
end tell
1939+
'''
1940+
else:
1941+
script = f'''
1942+
set python to "{pythonapp}"
1943+
set appwithpath to "{g2script}"
1944+
1945+
tell application "Terminal"
1946+
activate
1947+
do script python & " " & appwithpath & " " & "; exit"
1948+
end tell
1949+
'''
1950+
subprocess.Popen(["osascript","-e",script])
1951+
elif sys.platform.startswith("win"):
1952+
cmds = [pythonapp, g2script]
1953+
if project: cmds += [project]
1954+
subprocess.Popen(cmds,creationflags=subprocess.CREATE_NEW_CONSOLE)
1955+
else:
1956+
import shutil
1957+
script = ''
1958+
# try a bunch of common terminal emulators in Linux
1959+
# there does not appear to be a good way to way to specify this
1960+
# perhaps this should be a GSAS-II config option
1961+
for term in ("lxterminal", "gnome-terminal", 'konsole', "xterm",
1962+
"terminator", "terminology", "tilix"):
1963+
try:
1964+
found = shutil.which(term)
1965+
if not found: continue
1966+
except AttributeError:
1967+
print(f'shutil.which() failed (why?); assuming {term} present')
1968+
found = True
1969+
if term == "gnome-terminal":
1970+
#terminal = 'gnome-terminal -t "GSAS-II console" --'
1971+
cmds = [term,'--title','"GSAS-II console"','--']
1972+
script = "echo; echo Press Enter to close window; read line"
1973+
break
1974+
elif term == "lxterminal":
1975+
#terminal = 'lxterminal -t "GSAS-II console" -e'
1976+
cmds = [term,'-t','"GSAS-II console"','-e']
1977+
script = "echo;echo Press Enter to close window; read line"
1978+
break
1979+
elif term == "xterm":
1980+
#terminal = 'xterm -title "GSAS-II console" -hold -e'
1981+
cmds = [term,'-title','"GSAS-II console"','-hold','-e']
1982+
script = "echo; echo This window can now be closed"
1983+
break
1984+
elif term == "terminator":
1985+
cmds = [term,'-T','"GSAS-II console"','-x']
1986+
script = "echo;echo Press Enter to close window; read line"
1987+
break
1988+
elif term == "konsole":
1989+
cmds = [term,'-p','tabtitle="GSAS-II console"','--hold','-e']
1990+
script = "echo; echo This window can now be closed"
1991+
break
1992+
elif term == "tilix":
1993+
cmds = [term,'-t','"GSAS-II console"','-e']
1994+
script = "echo;echo Press Enter to close window; read line"
1995+
break
1996+
elif term == "terminology":
1997+
cmds = [term,'-T="GSAS-II console"','--hold','-e']
1998+
script = "echo; echo This window can now be closed"
1999+
break
2000+
else:
2001+
print("No known terminal was found to use, Can't start {}")
2002+
return
2003+
2004+
fil = '/tmp/GSAS2-launch.sh'
2005+
cmds += ['/bin/sh',fil]
2006+
fp = open(fil,'w')
2007+
if project:
2008+
fp.write(f"{pythonapp} {g2script} {project}\n")
2009+
else:
2010+
fp.write(f"{pythonapp} {g2script}\n")
2011+
fp.write(f"rm {fil}\n")
2012+
if script:
2013+
fp.write(f"{script}\n")
2014+
fp.close()
2015+
subprocess.Popen(cmds,start_new_session=True)
2016+
19002017
if __name__ == '__main__':
19012018
'''What follows is called to update (or downdate) GSAS-II in a
19022019
separate process.
@@ -2153,20 +2270,21 @@ def postURL(URL,postdict,getcookie=None,usecookie=None,
21532270

21542271
if gitUpdate:
21552272
# path hack for restart, when needed
2156-
import importlib.util
2157-
try:
2158-
importlib.util.find_spec('GSASII.GSASIIGUI')
2159-
except ModuleNotFoundError:
2160-
print('Adding GSAS-II location to Python system path')
2161-
sys.path.insert(0,os.path.dirname(os.path.dirname(__file__)))
2273+
#import importlib.util
2274+
#try:
2275+
# importlib.util.find_spec('GSASII.GSASIIGUI')
2276+
#except ModuleNotFoundError:
2277+
# print('Adding GSAS-II location to Python system path')
2278+
# sys.path.insert(0,os.path.dirname(os.path.dirname(__file__)))
21622279

21632280
# now restart GSAS-II with the new version
21642281
# G2scrpt = os.path.join(path2GSAS2,'G2.py')
21652282
if project:
21662283
print(f"Restart GSAS-II with project file {project!r}")
21672284
else:
21682285
print("Restart GSAS-II without a project file ")
2169-
from . import GSASIIfiles
2170-
GSASIIfiles.openInNewTerm(project)
2286+
#from . import GSASIIfiles
2287+
#GSASIIfiles.openInNewTerm(project)
2288+
openInNewTerm(project)
21712289
print ('exiting update process')
21722290
sys.exit()

0 commit comments

Comments
 (0)