-
-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wxgui: Create grassdata automatically on the first GUI startup #705
wxgui: Create grassdata automatically on the first GUI startup #705
Conversation
… platform was created.
gui/wxpython/startup/utils.py
Outdated
|
|
||
| try: | ||
| # here goes everything which has potential unicode issues | ||
| candidates.append(os.path.join(home, _("Documents"), "grassdata")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why _() in the path? Localized Windows should understand Documents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes from dac6d4a - see the comment there. I would not bother with that for this function and just go with Documents only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, here in this new function, I would not bother with translations here as this is more future oriented and we hope that every new system will be set in a way that there is "Documents". So, I'm saying, delete the "candidates.append(os.path.join(home, _("Documents"), "grassdata"))" part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gui/wxpython/startup/utils.py
Outdated
| # If still doesn't exist, create "grassdata_username" dir in temp | ||
| if path is None: | ||
| os.mkdir(tmp) | ||
| path = tmp | ||
| return path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This goes against "If even [the temporary directory fallback] fails, GRASS should not fail, but simply let the user make that choice which is the current behavior." from #682.
gui/wxpython/startup/utils.py
Outdated
| tmp = os.path.join(tempfile.gettempdir(), | ||
| "grassdata_{}".format(getpass.getuser())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be created only once it is really needed. (Otherwise, the tmp dir will be created and not deleted every time thing function runs.)
gui/wxpython/startup/utils.py
Outdated
| try: | ||
| path = os.path.join(home, "grassdata") | ||
| os.mkdir(path) | ||
| except OSError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path is already set at this point even when exception was raised, so your line if path is None: will have path which is not None and thus the functions will just return this path.
gui/wxpython/startup/utils.py
Outdated
| path = os.path.join(home, "grassdata") | ||
| os.mkdir(path) | ||
| except OSError: | ||
| print ("Creation of the directory {} failed".format(path)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may decide to refine it and give more info to the user through GUI, but at this point and in this function, there is no need to report the failure in any way. This function has a fallback which is the tmp dir and the original calling code has its own fallback solution.
gui/wxpython/startup/utils.py
Outdated
|
|
||
| try: | ||
| # here goes everything which has potential unicode issues | ||
| candidates.append(os.path.join(home, _("Documents"), "grassdata")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes from dac6d4a - see the comment there. I would not bother with that for this function and just go with Documents only.
gui/wxpython/gis_set.py
Outdated
| else: | ||
| # nothing found | ||
| # TODO: should it be warning, hint or message? | ||
| self._showWarning(_( | ||
| 'GRASS needs a directory (GRASS database) ' | ||
| 'in which to store its data. ' | ||
| 'Create one now if you have not already done so. ' | ||
| 'A popular choice is "grassdata", located in ' | ||
| 'your home directory. ' | ||
| 'Press Browse button to select the directory.')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still should be there. (#682: "If even [the temporary directory fallback] fails, [...] simply let the user make that choice which is the current behavior.")
gui/wxpython/startup/utils.py
Outdated
| def create_possible_database_path(): | ||
| """Create the directory to what is possibly a GRASS Database. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possible -> standard, fix wording of "directory to what is"
gui/wxpython/startup/utils.py
Outdated
| def create_possible_database_path(): | ||
| """Create the directory to what is possibly a GRASS Database. | ||
|
|
||
| Create directory named grassdata in the usual locations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like "...in the standard location according to the platform." And also mention the fallback.
…ture was changed a bit.
gui/wxpython/gis_set.py
Outdated
| @@ -530,6 +535,7 @@ def SuggestDatabase(self): | |||
| 'your home directory. ' | |||
| 'Press Browse button to select the directory.')) | |||
|
|
|||
|
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this extra change. Also in PEP8, there is only one empty line between methods.
gui/wxpython/gis_set.py
Outdated
| @@ -507,6 +508,10 @@ def SuggestDatabase(self): | |||
| if self.GetRCValue("LOCATION_NAME") != "<UNKNOWN>": | |||
| return | |||
| path = get_possible_database_path() | |||
| # If nothing found, create GRASS directory | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on what the function does and what the context is, the comment should be "...try to create..."
gui/wxpython/startup/utils.py
Outdated
| home = os.path.expanduser('~') | ||
| candidates = [] | ||
|
|
||
| # Candidate for independent "grassdata" in for Linux and macOS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What the word "independent" refers to here?
gui/wxpython/startup/utils.py
Outdated
|
|
||
| try: | ||
| # here goes everything which has potential unicode issues | ||
| candidates.append(os.path.join(home, _("Documents"), "grassdata")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, here in this new function, I would not bother with translations here as this is more future oriented and we hope that every new system will be set in a way that there is "Documents". So, I'm saying, delete the "candidates.append(os.path.join(home, _("Documents"), "grassdata"))" part.
gui/wxpython/startup/utils.py
Outdated
| path = candidate | ||
| return path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, you don't have to assign to a variable here, just return candidate right away.
gui/wxpython/startup/utils.py
Outdated
| tmp = os.path.join(tempfile.gettempdir(), | ||
| "grassdata_{}".format(getpass.getuser())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to PEP8, t from tempfile and " from "grassdata... should be aligned. Another option is to use Black style which puts this into 4 lines if it is longer than 88 characters:
tmp = os.path.join(
tempfile.gettempdir(),
"grassdata_{}".format(getpass.getuser())
)
You can run Flake8 on any file you edit to see these things. Unfortunately, you will get a lot of warning and errors from other lines you did not edit and you need to ignore those (this will get better in the future as we make more files PEP8, Flake8, and Black compliant).
gui/wxpython/startup/utils.py
Outdated
| else: | ||
| try: | ||
| os.mkdir(tmp) | ||
| path = tmp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, just return here is enough.
gui/wxpython/startup/utils.py
Outdated
| path = tmp | ||
| return path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only return is needed here, no assignment.
gui/wxpython/startup/utils.py
Outdated
| tmp = os.path.join(tempfile.gettempdir(), | ||
| "grassdata_{}".format(getpass.getuser())) | ||
|
|
||
| # Control if exists, if does not, create it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Control -> Check
gui/wxpython/startup/utils.py
Outdated
| @@ -55,6 +58,58 @@ def get_possible_database_path(): | |||
| return path | |||
|
|
|||
|
|
|||
| def create_possible_database_path(): | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_possible_database_path() -> create_database_directory()
The GRASS terminology here is all over the place here (grassdata, gisbase, ...), but "possible" does not make sense here and we are not creating a path, but the actual directory, so that's way "...database_directory".
gui/wxpython/startup/utils.py
Outdated
| else: | ||
| try: | ||
| os.mkdir(tmp) | ||
| return tmp | ||
| except OSError: | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the true branch of the if statement has return, you don't need else here and can move the try-except block one level left. This is does not change anything, but is makes the code simpler and the same logic is already used above: The good case returns, so the other case is just whatever follows after that.
gui/wxpython/startup/utils.py
Outdated
| # Candidate for case independent "grassdata" for Linux and macOS | ||
| if sys.platform.startswith('linux') or sys.platform.startswith('darwin'): | ||
| candidates.append(os.path.join(home, "grassdata")) | ||
| # Candidates for case independent "grassdata" in other dirs (Windows) | ||
| else: | ||
| candidates.append(os.path.join(home, "Documents", "grassdata")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple candidate = os.path... will suffice here in both cases. It is either or and at one point you have only one candidate. This removes the need for the loop over the candidates list below.
…ive)" This reverts commit ee63c49.
…ndidate=os.path which removes the for loop over candidates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minor comments to make the code more readable.
gui/wxpython/startup/utils.py
Outdated
| # Candidate for case independent "grassdata" for Linux and macOS | ||
| if sys.platform.startswith('linux') or sys.platform.startswith('darwin'): | ||
| candidate = os.path.join(home, "grassdata") | ||
| # Candidates for case independent "grassdata" in other dirs (Windows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "case independent" does not make sense here. Simplify further, by merging the two comments together and saying something like "Determine the standard path according to a platform."
While you are at it, you can drop the "candidate" terminology from the variable name too. What candidate refers to here? It really makes sense only in the other function. Simple path will be probably more descriptive here. And going further, the tmp variable can be called path too because in a way it is no different from the path before, it replaces it for all intents and purposes.
gui/wxpython/startup/utils.py
Outdated
| "grassdata_{}".format(getpass.getuser()) | ||
| ) | ||
|
|
||
| # Check if exists, if does not, create it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intended behavior may not be clear from the code, so add another comment saying something like "The created tmp is not cleaned by GRASS, so we are relying on the system to do it at some point. The positive outcome is that another GRASS instance will find the data created by the first one which is desired in the "try out GRASS" use case we are aiming towards." See the original ticket for alternative formulation of this.
gui/wxpython/startup/utils.py
Outdated
| except OSError: | ||
| pass | ||
|
|
||
| # Temporary "grassdata" directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Say something like "Create a temporary "grassdata" directory if GRASS is running in some special environment and the standard directories cannot be created which might be the case in some "try out GRASS" use cases."
(Note that a line should be 88 characters or less.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching trailing whitespace.
Use flake8 myfile.py to check the contents of files you edit. Ignore what is reported for lines you did not touch. Fix what is reported for lines you are changing. Use the https://www.flake8rules.com/ website for explanations. Ask if something is not clear. Not all the rules can be applied to the whole GRASS code base, but most of them should.
gui/wxpython/gis_set.py
Outdated
| @@ -38,7 +38,8 @@ | |||
| from core.gcmd import GMessage, GError, DecodeString, RunCommand | |||
| from core.utils import GetListOfLocations, GetListOfMapsets | |||
| from startup.utils import ( | |||
| get_lockfile_if_present, get_possible_database_path, create_mapset) | |||
| get_lockfile_if_present, get_possible_database_path, | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
| Creates database directory named grassdata in the standard location | ||
| according to the platform. | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
|
|
||
| Returns the new path as a string or None if nothing was found or created. | ||
| """ | ||
| home = os.path.expanduser('~') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
| if sys.platform.startswith('linux') or sys.platform.startswith('darwin'): | ||
| candidate = os.path.join(home, "grassdata") | ||
| # Candidates for case independent "grassdata" in other dirs (Windows) | ||
| else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
| else: | ||
| candidate = os.path.join(home, "Documents", "grassdata") | ||
|
|
||
| # Create "grassdata" directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
| except OSError: | ||
| pass | ||
|
|
||
| # Temporary "grassdata" directory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
gui/wxpython/startup/utils.py
Outdated
| tmp = os.path.join( | ||
| tempfile.gettempdir(), | ||
| "grassdata_{}".format(getpass.getuser()) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace (Flake8 W291)
… 'path', edited description related to the temporary file.
gui/wxpython/startup/utils.py
Outdated
| home = os.path.expanduser('~') | ||
|
|
||
| # Determine the standard path according to the platform | ||
| if sys.platform.startswith('linux') or sys.platform.startswith('darwin'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully the last change, sorry I missed this before: there are other platforms (see https://stackoverflow.com/questions/446209/possible-values-from-sys-platform) so we should probably have it more like this (and treat windows as special case):
if sys.platform == 'win32':
path = os.path.join(home, "Documents", "grassdata")
else:
path = os.path.join(home, "grassdata")
After starting GRASS GIS with GUI, the GUI now searches for existing grassdata (e.g., dac6d4a and #644 with #664). If nothing is found, GRASS GIS (GUI) should automatically create directory named grassdata as a subdirectory of a platfrom-dependent directory. This platfrom-dependent directory would be:
If that fails (GRASS is running in some special environment and the standard directories cannot be created which might be the case in some "try out GRASS" use cases) it should use a temporary directory (/tmp/... etc.) as a fallback.
The created tmp is not cleaned by GRASS, so we are relying on the system to do it at some point.