Skip to content

Commit

Permalink
Merge pull request #27 from mundialis/expand_open_geodata_functions
Browse files Browse the repository at this point in the history
Expand functions from open-geodata-germany
  • Loading branch information
juleshaas authored Apr 24, 2024
2 parents ff40f41 + db4e8c6 commit 143ede8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
29 changes: 21 additions & 8 deletions How-to-create-a-GRASS-GIS-addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ A GRASS python module consists of
### Structure (here `r.blend` as example)

1. shebang (first line)
2. header (author, purpose, license)
3. `# % ` comments are important (ignored by python but important for parser)

1. header (author, purpose, license)

1. `# % ` comments are important (ignored by python but important for parser)

- See https://grass.osgeo.org/grass-devel/manuals/g.parser.html

Expand All @@ -77,18 +79,21 @@ A GRASS python module consists of
- key is key in command line (e.g. 'first')
- answer is default values
- access them in main function like `options['first']`
- there are also [standard options]("https://grass.osgeo.org/grass84/manuals/parser_standard_options.html) which can be extended
- there are also [standard options](%22https://grass.osgeo.org/grass84/manuals/parser_standard_options.html) which can be extended

###### `# % flag`

###### `# % rules`

- define dependencies between options, required options and more. See official [docs](https://grass.osgeo.org/grass84/manuals/g.parser.html#conditional-parameters)

4. `def main():` reads in all variables (`options['first']`)
1. `def main():` reads in all variables (`options['first']`)

- a main function is required
5. indefinite additional functions are possible
6. include parser at the end before calling main function:

1. indefinite additional functions are possible

1. include parser at the end before calling main function:

```python
if __name__ == "__main__":
Expand All @@ -108,13 +113,19 @@ A GRASS python module consists of
## Best practises

- python style guide
- [PEP 8 – Style Guide for Python Code](https://peps.python.org/pep-0008/])

- [PEP 8 – Style Guide for Python Code](https://peps.python.org/pep-0008/%5D)
- https://trac.osgeo.org/grass/wiki/Submitting
- https://trac.osgeo.org/grass/wiki/Submitting/Python

- html documentation (no full html please, parts are auto-generated at compile time)

- https://trac.osgeo.org/grass/wiki/Submitting/Docs

- to support i18n, import following module and use macro '\_' before strings. The text after will be replaced with existing lookup tables for other languages

- use existing functions: esp. from PyGRASS

- API description: https://grass.osgeo.org/grass-devel/manuals/libpython/pygrass_index.html
- PyGRASS paper: An Object Oriented Python Application Programming Interface (API) for GRASS: https://www.mdpi.com/2220-9964/2/1/201/htm

Expand Down Expand Up @@ -204,7 +215,9 @@ Choose a name depending on the "family":
```

- Fix lint errors which black couldn't fix or add exception to `.flake8` file

- Black does not fix GRASS GIS parameter `#%`. This can be batch changed with `sed -i 's|^#%|# %|g' foo.py`

- There may not be any linting issues left as the pipeline would fail

#### General steps Part 2
Expand Down Expand Up @@ -242,4 +255,4 @@ For more information on standardized messages see [here](https://trac.osgeo.org/
- Delete "old" code from internal repository
- Add hint to internal repository README that the addon was moved and where to find it
- Adjust README.md in actinia-assets/grass-gis-addons
- Adjust deployments which use the addon, if applicable
- Adjust deployments which use the addon, if applicable
15 changes: 11 additions & 4 deletions src/grass_gis_helpers/open_geodata_germany/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@


def check_download_dir(download_dir):
"""Checks if download directory is set. If yes, checks if folder exists or
creates it. If not set, a temporary directory will be used.
"""Check if download directory is set. If yes, check if folder exists or
create it. If not set, a temporary directory will be used.
Args:
download_dir (str): download directory module parameter
download_dir (str): Download directory module parameter
Returns:
(str): Path to download directory
"""
Expand All @@ -51,6 +51,13 @@ def check_download_dir(download_dir):
)
)
os.makedirs(download_dir)
elif os.path.exists(download_dir) and os.listdir(download_dir):
grass.warning(
_(
f"Download folder {download_dir} exists and is not empty. "
"Folder will NOT be deleted."
)
)
grass.message(f"Download directory: {download_dir}")
return download_dir

Expand All @@ -59,7 +66,7 @@ def url_response(url):
"""URL response function which is used by download_data_using_threadpool
Args:
url (str): data download url
url (str): Data download url
Return:
url (str): Return the url for printing
"""
Expand Down
12 changes: 8 additions & 4 deletions src/grass_gis_helpers/open_geodata_germany/federal_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,26 @@ def get_federal_states(federal_state, federal_state_file):
list with federal state abbreviations
Args:
federal_state (str): federal state module parameter
federal_state_file (str): federal state file module parameter
federal_state (str): Federal state module parameter
federal_state_file (str): Federal state file module parameter
Returns:
(list): list with federale state abbreviations
(list): List with federal state abbreviations
"""
if federal_state_file:
if not os.path.isfile(federal_state_file):
grass.fatal(
_(
"Federal state file is given, but file "
f"<{federal_state_file}> does not exists."
f"<{federal_state_file}> does not exist."
)
)
with open(federal_state_file) as fs_file:
fs_list_str = fs_file.read().strip()
if fs_list_str == "":
grass.fatal(
_("Federal state in <federal_state_file> is empty string!")
)
elif federal_state:
fs_list_str = federal_state.strip()
else:
Expand Down

0 comments on commit 143ede8

Please sign in to comment.