Ignore:
Timestamp:
May 10, 2019 3:59:08 PM (4 years ago)
Author:
toby
Message:

slightly better approach to tutorial path

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIctrlGUI.py

    r3961 r3965  
    53595359        directory in a logical place
    53605360        '''
     5361        # has the user set a location and is it valid?
    53615362        if GSASIIpath.GetConfigValue('Tutorial_location'):
    5362             self.tutorialPath = os.path.abspath(GSASIIpath.GetConfigValue('Tutorial_location'))
    5363         elif (sys.platform.lower().startswith('win')):
    5364             for p in ('Documents','My Documents'):
     5363            tutorialPath = os.path.abspath(GSASIIpath.GetConfigValue('Tutorial_location'))
     5364            if os.path.exists(tutorialPath):
     5365                self.tutorialPath = tutorialPath
     5366                return
     5367            try:
     5368                os.makedirs(tutorialPath)
     5369                if os.path.exists(tutorialPath):
     5370                    self.tutorialPath = tutorialPath
     5371                    return
     5372            except:
     5373                print('Unable to use Tutorial_location config setting',
     5374                          tutorialPath)
     5375        # try a system-specific location
     5376        if (sys.platform.lower().startswith('win')):
     5377            for p in ('Documents','My Documents',''):
    53655378                if os.path.exists(os.path.abspath(os.path.expanduser(
    53665379                      os.path.join('~',p)))):
    5367                     self.tutorialPath = os.path.abspath(os.path.expanduser(
     5380                    tutorialPath = os.path.abspath(os.path.expanduser(
    53685381                      os.path.join('~',p,'G2tutorials')))
     5382                    if os.path.exists(tutorialPath):
     5383                        self.tutorialPath = tutorialPath
     5384                        return
     5385                    try:
     5386                        os.makedirs(tutorialPath)
     5387                        if os.path.exists(tutorialPath):
     5388                            self.tutorialPath = tutorialPath
     5389                            return
     5390                    except:
     5391                        pass
     5392        else:
     5393            tutorialPath = os.path.abspath(os.path.expanduser(
     5394                    os.path.join('~','G2tutorials')))
     5395            if os.path.exists(tutorialPath):
     5396                self.tutorialPath = tutorialPath
     5397                return
     5398            try:
     5399                os.makedirs(tutorialPath)
     5400                if os.path.exists(tutorialPath):
     5401                    self.tutorialPath = tutorialPath
    53695402                    return
    5370             self.tutorialPath = '.\G2tutorials'     #would this work as an ultimate default?
    5371         else:
    5372             self.tutorialPath = os.path.abspath(os.path.expanduser(
    5373                     os.path.join('~','G2tutorials')))
     5403            except:
     5404                pass
     5405        # no success so far, use current working directory
     5406        tutorialPath = os.path.abspath(os.path.join(os.getcwd(),'G2tutorials'))
     5407        if os.path.exists(tutorialPath):
     5408            self.tutorialPath = tutorialPath
     5409            return
     5410        try:
     5411            os.makedirs(tutorialPath)
     5412            if os.path.exists(tutorialPath):
     5413                self.tutorialPath = tutorialPath
     5414                return
     5415        except:
     5416            pass
     5417        # nothing worked, set self.tutorialPath with os.getcwd() and hope for the best
     5418        print('Warning: Unable to set a TutorialPath, using',os.getcwd())
     5419        tutorialPath = os.getcwd()
    53745420
    53755421    def SelectAndDownload(self,event):
Note: See TracChangeset for help on using the changeset viewer.