Skip to content
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

Install GitHub #60

Merged
merged 36 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a677e93
Don't generate r script twice
nyalldawson Nov 7, 2019
5c445e8
More stable handling of libPath path
nyalldawson Nov 7, 2019
5173411
Fix setting multiple field input variables
nyalldawson Nov 7, 2019
a71cdc5
Fix compatiblity with older Python versions
nyalldawson Nov 7, 2019
673aa72
Bump to version 2.0.0
nyalldawson Nov 7, 2019
50d91dc
website files
JanCaha Nov 8, 2019
a8f14da
update travis to build and deploy website
JanCaha Nov 8, 2019
e82a1fa
update url to correct github repo
JanCaha Nov 8, 2019
188c25d
fix deploy script
JanCaha Nov 9, 2019
a2727ab
Merge remote-tracking branch 'remotes/upstream/master'
JanCaha Nov 9, 2019
a2f6480
update travis
JanCaha Nov 9, 2019
fe1da68
add link to website
JanCaha Nov 9, 2019
8fa511e
remove verbose from mkdocs
JanCaha Nov 9, 2019
91064a6
remove repo information from config
JanCaha Nov 9, 2019
87a35df
added info about installation
JanCaha Nov 9, 2019
30f8e5f
leave the email while pushing the website to my email
JanCaha Nov 11, 2019
95a16af
Merge remote-tracking branch 'remotes/upstream/master'
JanCaha Nov 15, 2019
c9b80f5
throw error if name of parameter contains whitespace
JanCaha Nov 15, 2019
4608436
remove deploy-key.enc
JanCaha Nov 15, 2019
ac00de9
addition of is_valid_r_variable function
JanCaha Nov 20, 2019
1607ef1
fix pylint issues
JanCaha Nov 20, 2019
c419e3e
simplify into single regex
JanCaha Nov 20, 2019
6e164a7
test github_install metadata line
JanCaha Mar 16, 2020
dec7e88
fix error in process_metadata_line function
JanCaha Mar 16, 2020
9b09262
fix github install code genereration process
JanCaha Mar 16, 2020
6c76ed9
fix documentation
JanCaha Mar 16, 2020
8d2ab0a
fix the most stupid error in python function call :)
JanCaha Mar 16, 2020
93326b0
fix test for github install
JanCaha Mar 16, 2020
fddd1df
fix pylint violations
JanCaha Mar 16, 2020
0db2647
make pycodestyle happy
JanCaha Mar 16, 2020
d681702
change processing order for R script, first necessary packages, then …
JanCaha Mar 16, 2020
b4e0471
move logic of build_script_header_commands from algorithm.py to r_tem…
JanCaha Mar 18, 2020
b5d443c
fix function calls
JanCaha Mar 18, 2020
166db4a
Merge branch 'master' into install_github
JanCaha Mar 18, 2020
3f0ac95
add github_install to website
JanCaha Mar 18, 2020
0904e0e
fix pylint problem with empty lines
JanCaha Mar 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions processing_r/processing/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def parse_script(self, lines):
except StopIteration:
break

def process_metadata_line(self, line):
def process_metadata_line(self, line): # pylint: disable=too-many-return-statements
"""
Processes a "metadata" (##) line
"""
Expand Down Expand Up @@ -244,6 +244,10 @@ def process_metadata_line(self, line):
self._name = self._display_name = value
self._name = RUtils.strip_special_characters(self._name.lower())
return
if type_.lower().strip() == 'github_install':
self.r_templates.install_github = True
self.r_templates.github_dependencies = value
return

self.process_parameter_line(line)

Expand All @@ -262,6 +266,11 @@ def process_parameter_line(self, line):
value, _ = self.split_tokens(line)
description = RUtils.create_descriptive_name(value)

if not RUtils.is_valid_r_variable(value):
self.error = self.tr('This script has a syntax error in variable name.\n'
'"{1}" is not a valid variable name in R.'
'Problem with line: {0}').format(line, value)

output = create_output_from_string(line)
if output is not None:
output.setName(value)
Expand Down Expand Up @@ -456,13 +465,22 @@ def build_script_header_commands(self, _, __, ___):
path_to_use = str(RUtils.r_library_folder()).replace('\\', '/')
commands.append(self.r_templates.change_libPath(path_to_use))

packages = RUtils.get_required_packages(self.script)
packages.extend(self.r_templates.get_necessary_packages())
packages = self.r_templates.get_necessary_packages()

for p in packages:
commands.append(self.r_templates.check_package_availability(p))
commands.append(self.r_templates.load_package(p))

if self.r_templates.install_github:
for dependency in self.r_templates.github_dependencies:
commands.append(self.r_templates.install_package_github(dependency))

packages_script = RUtils.get_required_packages(self.script)

for p in packages_script:
commands.append(self.r_templates.check_package_availability(p))
commands.append(self.r_templates.load_package(p))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this whole block of code could be moved to r_templates -- what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, all the info is contained there anyway! I will fix it later today :)

Btw: Just out of curiosity, is there a way to add the changes to this pull request or do I have to close it and make a new one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you commit them to the same branch and then push, they'll appear here automatically!

return commands

def build_raster_layer_import_command(self, variable_name, layer):
Expand Down
55 changes: 54 additions & 1 deletion processing_r/processing/r_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import List


class RTemplates:
class RTemplates: # pylint: disable=too-many-public-methods
"""
Class for generating R code
"""
Expand All @@ -31,6 +31,47 @@ def __init__(self):
Variable defining that rasters will be read through package `raster` if `TRUE` or through `rgdal` and `sp` if
`FALSE`.
"""
self._install_github = False
"""
Variable defining that there are dependencies from github.
"""
self._github_dependencies = []
"""
Variable that stores dependencies from github to be installed.
"""

@property
def github_dependencies(self):
"""
Getter for class variable github_dependencies.
:return: bool
"""
return self._github_dependencies

@github_dependencies.setter
def github_dependencies(self, dependencies: str):
"""
Setter for class variable github_dependencies.
:param dependencies: str
"""
dependencies = dependencies.strip().replace(" ", "").split(",")
self._github_dependencies = dependencies

@property
def install_github(self):
"""
Getter for class variable install_github.
:return: bool
"""
return self._install_github

@install_github.setter
def install_github(self, use: bool):
"""
Setter for class variable install_github.
:param use: bool
"""
self._install_github = use

@property
def use_sf(self):
Expand Down Expand Up @@ -83,6 +124,9 @@ def get_necessary_packages(self) -> list:
else:
packages.append("rgdal")

if self.install_github:
packages.append("remotes")

return packages

def __set_variable_vector_sf(self, variable: str, path: str, layer: str = None) -> str:
Expand Down Expand Up @@ -355,6 +399,15 @@ def write_csv_output(self, variable: str, path: str) -> str:
"""
return 'write.csv({0}, "{1}")'.format(variable, path)

def install_package_github(self, repo: str) -> str:
"""
Function that produces R code to install

:param repo: string. Name of the repo to be installed.
:return: string. R code to install package from a repo.
"""
return 'remotes::install_github("{0}")'.format(repo)

def check_package_availability(self, package_name: str) -> str:
"""
Function that produces R code to check availability and install missing R packages.
Expand Down
23 changes: 23 additions & 0 deletions processing_r/processing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ def create_descriptive_name(name):
"""
return name.replace('_', ' ')

@staticmethod
def is_valid_r_variable(variable: str) -> bool:
"""
Check if given string is valid R variable name.
:param variable: string
:return: bool
"""

# only letters a-z, A-Z, numbers, dot and underscore
x = re.search("[a-zA-Z0-9\\._]+", variable)

result = True

if variable == x.group():
# cannot start with number or underscore, or start with dot followed by number
x = re.search("^[0-9|_]|^\\.[0-9]", variable)
if x:
result = False
else:
result = False

return result

@staticmethod
def strip_special_characters(name):
"""
Expand Down
12 changes: 12 additions & 0 deletions processing_r/test/test_rutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ def test_r_is_installed(self):
ProcessingConfig.setSettingValue(RUtils.R_FOLDER, None)
self.assertIsNone(RUtils.check_r_is_installed())

def test_is_valid_r_variable(self):
"""
Test for strings to check if they are valid R variables.
"""
self.assertFalse(RUtils.is_valid_r_variable("var_name%"))
self.assertFalse(RUtils.is_valid_r_variable("2var_name"))
self.assertFalse(RUtils.is_valid_r_variable(".2var_name"))
self.assertFalse(RUtils.is_valid_r_variable("_var_name"))
self.assertTrue(RUtils.is_valid_r_variable("var_name2."))
self.assertTrue(RUtils.is_valid_r_variable(".var_name"))
self.assertTrue(RUtils.is_valid_r_variable("var.name"))


if __name__ == "__main__":
suite = unittest.makeSuite(RUtilsTest)
Expand Down
12 changes: 12 additions & 0 deletions processing_r/test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@
class TemplateTest(unittest.TestCase):
"""Test template generation."""

def testGithubInstall(self):
"""
Test github install code generation.
"""
templates = RTemplates()
templates.install_github = True
templates.github_dependencies = "user_1/repo_1, user_2/repo_2"
self.assertEqual(templates.install_package_github(templates.github_dependencies[0]),
'remotes::install_github("user_1/repo_1")')
self.assertEqual(templates.install_package_github(templates.github_dependencies[1]),
'remotes::install_github("user_2/repo_2")')

def testString(self): # pylint: disable=too-many-locals,too-many-statements
"""
Test string variable
Expand Down