Skip to content

Commit

Permalink
Merge pull request #122 from mikapfl/refactor-manipulateConfig
Browse files Browse the repository at this point in the history
Refactor manipulateConfig
  • Loading branch information
mikapfl authored Oct 13, 2022
2 parents 4d0e839 + a50531d commit cd4ac53
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '5898150'
ValidationKey: '5918346'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "lucode2: Code Manipulation and Analysis Tools",
"version": "0.30.6",
"version": "0.30.7",
"description": "<p>A collection of tools which allow to manipulate and analyze code.<\/p>",
"creators": [
{
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: lucode2
Type: Package
Title: Code Manipulation and Analysis Tools
Version: 0.30.6
Date: 2022-10-10
Version: 0.30.7
Date: 2022-10-13
Authors@R: c(person("Jan Philipp", "Dietrich", email = "[email protected]", role = c("aut","cre")),
person("David", "Klein", role = "aut"),
person("Anastasis", "Giannousakis", role = "aut"),
Expand Down
84 changes: 45 additions & 39 deletions R/manipulateConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,67 @@
#'
manipulateConfig <- function(configFile, ...) {
maxchar <- 60
tmp <- list(...)
if (length(tmp) == 1) {
if (is.list(tmp[[1]])) {
tmp <- tmp[[1]]
arguments <- list(...)
if (length(arguments) == 1) {
if (is.list(arguments[[1]])) {
arguments <- arguments[[1]]
}
}
m <- list()
manipulations <- list()
type <- substring(configFile, nchar(configFile) - 2)
if (type %in% c("gms", "inc")) {
for (i in names(tmp)) {
if (length(tmp[[i]]) > 1) {
rpl <- capture.output(cat(paste0(tmp[[i]], ", "), fill = maxchar))
rpl <- paste(rpl, collapse = "\n\t\t")
rpl <- substr(rpl, 1, nchar(rpl) - 2)
} else {
rpl <- tmp[[i]]
for (key in names(arguments)) {
value <- arguments[[key]]
if (length(value) > 1) {
value <- capture.output(cat(paste0(value, ", "), fill = maxchar))
value <- paste(value, collapse = "\n\t\t")
value <- substr(value, 1, nchar(value) - 2)
}

m[[paste(i, "_pattern1", sep = "")]][1] <- paste(
"(\\$[sS][eE][tT][gG][lL][oO][bB][aA][lL][\\t ]*", i, "[\\t ]).*?( *!!|\\n|$)", sep = "")
m[[paste(i, "_pattern1", sep = "")]][2] <- paste(
"\\1 ", rpl, "\\2", sep = "")
m[[paste(i, "_pattern2", sep = "")]][1] <- paste(
"((\\n|^)[\\t ]*(scalar|parameter|set|)s?[\\t ]*", i, "(|\\([^\\)]*\\))(/|[\\t ]+(\"[^\"]*\"|)[^\"/;]*/))[^/]*",
sep = "")
m[[paste(i, "_pattern2", sep = "")]][2] <- paste(
"\\1 ", rpl, " ", sep = "")
m[[paste(i, "_pattern3", sep = "")]][1] <- paste(
"((^|[\\n\\t ])", i, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*", sep = "")
m[[paste(i, "_pattern3", sep = "")]][2] <- paste(
"\\1", rpl, sep = "")
# definitions in the form
# > $setglobal key value
manipulations[[paste0(key, "_pattern1")]][1] <- paste0(
"((\\n|^)[\\t ]*\\$[sS][eE][tT][gG][lL][oO][bB][aA][lL][\\t ]*", key, "[\\t ]).*?( *!!|\\n|$)")
manipulations[[paste0(key, "_pattern1")]][2] <- paste0("\\1 ", value, "\\2")

# definitions in the form
# > parameter key "documentation" / value /;
manipulations[[paste0(key, "_pattern2")]][1] <- paste0(
"((\\n|^)[\\t ]*(scalar|parameter|set|)s?[\\t ]*",
key,
"(|\\([^\\)]*\\))(/|[\\t ]+(\"[^\"]*\"|)[^\"/;]*/))[^/]*")
manipulations[[paste0(key, "_pattern2")]][2] <- paste0("\\1 ", value, " ")

# definitions in the form
# > key = value;
# > key = "value";
manipulations[[paste0(key, "_pattern3")]][1] <- paste0(
"((^|[\\n\\t ])", key, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*")
manipulations[[paste0(key, "_pattern3")]][2] <- paste0("\\1", value)
}
} else if (type %in% c("cfg", "R", "r")) {
for (i in names(tmp)) {
if (is.character(tmp[[i]])) tmp[[i]] <- paste("\"", tmp[[i]], "\"", sep = "")
m[[i]][1] <- paste("((^|[\\t \\$])", i, "[ \\t]*<-[ \\t]*)[\"\']?[^\"\']*[\"\']?", sep = "")
m[[i]][2] <- paste("\\1", tmp[[i]], sep = "")
for (key in names(arguments)) {
if (is.character(arguments[[key]])) arguments[[key]] <- paste0("\"", arguments[[key]], "\"")
manipulations[[key]][1] <- paste0("((^|[\\t \\$])", key, "[ \\t]*<-[ \\t]*)[\"\']?[^\"\']*[\"\']?")
manipulations[[key]][2] <- paste0("\\1", arguments[[key]])
}
} else if (type %in% c("cmd", ".sh")) {
for (i in names(tmp)) {
m[[i]][1] <- paste("((^|[\\t ])", i, "[ \\t]*=[ \\t]*[\"\']?)[^\"\'\\n]*", sep = "")
m[[i]][2] <- paste("\\1", tmp[[i]], sep = "")
for (key in names(arguments)) {
manipulations[[key]][1] <- paste0("((^|[\\t ])", key, "[ \\t]*=[ \\t]*[\"\']?)[^\"\'\\n]*")
manipulations[[key]][2] <- paste0("\\1", arguments[[key]])
}
} else if (type == "php") {
for (i in names(tmp)) {
m[[i]][1] <- paste("((^|[\\t ])\\$", i, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*", sep = "")
m[[i]][2] <- paste("\\1", tmp[[i]], sep = "")
for (key in names(arguments)) {
manipulations[[key]][1] <- paste0("((^|[\\t ])\\$", key, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*")
manipulations[[key]][2] <- paste0("\\1", arguments[[key]])
}
} else if (type == "opt") {
for (i in names(tmp)) {
m[[i]][1] <- paste("((^|[\\t ])", i, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*", sep = "")
m[[i]][2] <- paste("\\1", tmp[[i]], sep = "")
for (key in names(arguments)) {
manipulations[[key]][1] <- paste0("((^|[\\t ])", key, "[ \\t]*=[ \\t]*[\"\']?)[^\"\';]*")
manipulations[[key]][2] <- paste0("\\1", arguments[[key]])
}
} else {
stop(paste("Unknown file type", type))
}
manipulateFile(configFile, m)
manipulateFile(configFile, manipulations)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Code Manipulation and Analysis Tools

R package **lucode2**, version **0.30.6**
R package **lucode2**, version **0.30.7**

[![CRAN status](https://www.r-pkg.org/badges/version/lucode2)](https://cran.r-project.org/package=lucode2) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4389418.svg)](https://doi.org/10.5281/zenodo.4389418) [![R build status](https://github.com/pik-piam/lucode2/workflows/check/badge.svg)](https://github.com/pik-piam/lucode2/actions) [![codecov](https://codecov.io/gh/pik-piam/lucode2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/lucode2) [![r-universe](https://pik-piam.r-universe.dev/badges/lucode2)](https://pik-piam.r-universe.dev/ui#builds)

Expand Down Expand Up @@ -38,7 +38,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **lucode2** in publications use:

Dietrich J, Klein D, Giannousakis A, Bonsch M, Bodirsky B, Baumstark L, Führlich P, Richters O (2022). _lucode2: Code Manipulation and Analysis Tools_. doi:10.5281/zenodo.4389418 <https://doi.org/10.5281/zenodo.4389418>, R package version 0.30.6, <https://github.com/pik-piam/lucode2>.
Dietrich J, Klein D, Giannousakis A, Bonsch M, Bodirsky B, Baumstark L, Führlich P, Richters O (2022). _lucode2: Code Manipulation and Analysis Tools_. doi:10.5281/zenodo.4389418 <https://doi.org/10.5281/zenodo.4389418>, R package version 0.30.7, <https://github.com/pik-piam/lucode2>.

A BibTeX entry for LaTeX users is

Expand All @@ -47,7 +47,7 @@ A BibTeX entry for LaTeX users is
title = {lucode2: Code Manipulation and Analysis Tools},
author = {Jan Philipp Dietrich and David Klein and Anastasis Giannousakis and Markus Bonsch and Benjamin Leon Bodirsky and Lavinia Baumstark and Pascal Führlich and Oliver Richters},
year = {2022},
note = {R package version 0.30.6},
note = {R package version 0.30.7},
doi = {10.5281/zenodo.4389418},
url = {https://github.com/pik-piam/lucode2},
}
Expand Down

0 comments on commit cd4ac53

Please sign in to comment.