Repository: spyder-ide/spyder
Pull request: PR: Preserve selected working directory in Preferences after a restart
<!--- Make sure to read the Contributing Guidelines:                   --->
<!--- https://github.com/spyder-ide/spyder/blob/master/CONTRIBUTING.md --->
<!--- and follow PEP 8, PEP 257 and Spyder's code style:               --->
<!--- https://github.com/spyder-ide/spyder/wiki/Dev:-Coding-Style      --->

## Description of Changes


* [ ] Wrote at least one-line docstrings (for any new functions)
* [ ] Added unit test(s) covering the changes (if testable)
<!--- Remember that an image/animation is worth a thousand words! --->
* [x] Included a screenshot or animation (if affecting the UI, see [Licecap](https://www.cockos.com/licecap/))


<!--- Explain what you've done and why --->
Add options to configurations file and check the working directory preferences.


### Issue(s) Resolved

<!--- List the issue(s) below, in the form "Fixes #1234"; one per line --->

Fixes #9561 


### Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the [Developer Certificate of Origin](https://developercertificate.org)
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

<!--- TYPE YOUR USER/NAME AFTER THE FOLLOWING: --->
I certify the above statement is true and correct: steff456

<!--- Thanks for your help making Spyder better for everyone! --->


Problem statement:
Selected working directory in Preferences is not preserved after a restart
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

under preferance i chose a working directory as a start up directrory when starting spyder again reverts back to c:\users\user directory



## Versions

* Spyder version: 4.0.0b2 
* Python version: 3.7.3
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```
pygments >=2.0     :  2.3.1 (OK)
qdarkstyle >=2.6.4 :  2.6.8 (OK)
sphinx >=0.6.6     :  1.8.5 (OK)
pyls >=0.19.0;<0.25:  0.24.0 (OK)
nbconvert >=4.0    :  5.4.1 (OK)
pandas >=0.13.1    :  0.24.2 (OK)
numpy >=1.7        :  1.16.2 (OK)
sympy >=0.7.3      :  1.3 (OK)
cython >=0.21      :  0.29.6 (OK)
qtconsole >=4.2.0  :  4.4.3 (OK)
IPython >=4.0      :  7.4.0 (OK)
matplotlib >=2.0.0 :  3.0.3 (OK)
pylint >=0.25      :  2.3.1 (OK)
```

Hints from issue discussion:
@dalthviz, could you verify if this is happening to you on Windows or not?
yes windows 10
@ccordoba12 yes, testing it, it happens to me too. In the preferences, the selected woking directory shows the change but the working directory toolbar shows the home directory (after restarting Spyder manually this is what I see):

![imagen](https://user-images.githubusercontent.com/16781833/59482860-4218b280-8e30-11e9-927c-3fe4e4f1ab49.png)



Thanks for the confirmation @dalthviz. Could you also test what happens on Linux?
Testing on Linux (Ubuntu) the same behaviour happens:

![linux](https://user-images.githubusercontent.com/16781833/59528122-079c2d80-8ea3-11e9-9f3f-9ac953b9e18f.PNG)


Ok, thanks @dalthviz. I thought it was a Windows issue only.
Who could work on this @ccordoba12 ?
Let's give this one to @steff456 to this one.

Commit message: fix pep8 issues

Patch to review:
diff --git a/spyder/config/main.py b/spyder/config/main.py
index fa7231020fe..4cad80a1f28 100755
--- a/spyder/config/main.py
+++ b/spyder/config/main.py
@@ -307,6 +307,9 @@
               'console/use_project_or_home_directory': False,
               'console/use_cwd': True,
               'console/use_fixed_directory': False,
+              'console/fixed_directory': '',
+              'startup/fixed_directory': '',
+              'startup/use_fixed_directory': False,
               }),
             ('shortcuts',
              {
diff --git a/spyder/plugins/ipythonconsole/plugin.py b/spyder/plugins/ipythonconsole/plugin.py
index b4e6b8dd69f..0bcc6fa5bc2 100644
--- a/spyder/plugins/ipythonconsole/plugin.py
+++ b/spyder/plugins/ipythonconsole/plugin.py
@@ -896,6 +896,8 @@ def register_client(self, client, give_focus=True):
             if (self.main.projects is not None and
                     self.main.projects.get_active_project() is not None):
                 cwd_path = self.main.projects.get_active_project_path()
+        elif CONF.get('workingdir', 'startup/use_fixed_directory'):
+            cwd_path = CONF.get('workingdir', 'startup/fixed_directory')
         elif CONF.get('workingdir', 'console/use_fixed_directory'):
             cwd_path = CONF.get('workingdir', 'console/fixed_directory')
 
diff --git a/spyder/plugins/workingdirectory/plugin.py b/spyder/plugins/workingdirectory/plugin.py
index 840757b15a1..8b29a4f8450 100644
--- a/spyder/plugins/workingdirectory/plugin.py
+++ b/spyder/plugins/workingdirectory/plugin.py
@@ -88,12 +88,7 @@ def __init__(self, parent, workdir=None, **kwds):
         self.pathedit.setMaxCount(self.get_option('working_dir_history'))
         wdhistory = self.load_wdhistory(workdir)
         if workdir is None:
-            if self.get_option('console/use_project_or_home_directory'):
-                workdir = get_home_dir()
-            else:
-                workdir = self.get_option('console/fixed_directory', default='')
-                if not osp.isdir(workdir):
-                    workdir = get_home_dir()
+            workdir = self.get_workdir()
         self.chdir(workdir)
         self.pathedit.addItems(wdhistory)
         self.pathedit.selected_text = self.pathedit.currentText()
@@ -113,7 +108,21 @@ def __init__(self, parent, workdir=None, **kwds):
                                       _('Change to parent directory'),
                                       triggered=self.parent_directory)
         self.toolbar.addAction(parent_action)
-                
+
+    def get_workdir(self):
+        """Get current workdir from the CONF file."""
+        if self.get_option('startup/use_fixed_directory'):
+            workdir = self.get_option('startup/fixed_directory',
+                                      default='')
+        elif self.get_option('console/use_project_or_home_directory'):
+            workdir = get_home_dir()
+        else:
+            workdir = self.get_option('console/fixed_directory',
+                                      default='')
+        if not osp.isdir(workdir):
+            workdir = get_home_dir()
+        return workdir
+
     #------ SpyderPluginWidget API ---------------------------------------------    
     def get_plugin_title(self):
         """Return widget title"""
@@ -149,7 +158,7 @@ def load_wdhistory(self, workdir=None):
             wdhistory = [name for name in wdhistory if os.path.isdir(name)]
         else:
             if workdir is None:
-                workdir = get_home_dir()
+                workdir = self.get_workdir()
             wdhistory = [ workdir ]
         return wdhistory
 


Code review comments:
diff_hunk: @@ -307,6 +307,9 @@
               'console/use_project_or_home_directory': False,
               'console/use_cwd': True,
               'console/use_fixed_directory': False,
+              'console/fixed_directory': '',
+              'startup/fixed_directory': '',
path: spyder/config/main.py
text: @user1:
@author since these values are empty we probably do not need to add them here
```
              'console/fixed_directory': '',
              'startup/fixed_directory': '',

```

diff_hunk: @@ -896,6 +896,8 @@ def register_client(self, client, give_focus=True):
             if (self.main.projects is not None and
                     self.main.projects.get_active_project() is not None):
                 cwd_path = self.main.projects.get_active_project_path()
+        elif CONF.get('workingdir', 'startup/use_fixed_directory'):
+            cwd_path = CONF.get('workingdir', 'startup/fixed_directory')
path: spyder/plugins/ipythonconsole/plugin.py
text: @user1:
Since this value may not exist this call should probably include a default

`CONF.get('workingdir', 'startup/fixed_directory', default=somethig?)`