-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
[automation] Added automation script StartLevels #2222
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...penhab/core/automation/module/script/rulesupport/internal/loader/ScriptFileReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.automation.module.script.rulesupport.internal.loader; | ||
|
||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.openhab.core.service.StartLevelService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Script File wrapper offering various methods to inspect the script | ||
* | ||
* @author Jonathan Gilbert - initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class ScriptFileReference implements Comparable<ScriptFileReference> { | ||
|
||
private static final Set<String> EXCLUDED_FILE_EXTENSIONS = new HashSet<>( | ||
Arrays.asList("txt", "old", "example", "backup", "md", "swp", "tmp", "bak")); | ||
|
||
private static final Pattern[] startLevelPatterns = new Pattern[] { Pattern.compile(".*/sl(\\d{2})/[^/]+"), // script | ||
// in | ||
// immediate | ||
// slXX | ||
// directory | ||
Pattern.compile(".*/[^/]+\\.sl(\\d{2})\\.[^/.]+") // script named <name>.slXX.<ext> | ||
}; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ScriptFileReference.class); | ||
|
||
private final URL scriptFileURL; | ||
|
||
public ScriptFileReference(URL scriptFileURL) { | ||
this.scriptFileURL = scriptFileURL; | ||
} | ||
|
||
public URL getScriptFileURL() { | ||
return scriptFileURL; | ||
} | ||
|
||
public int getStartLevel() { | ||
for (Pattern p : startLevelPatterns) { | ||
Matcher m = p.matcher(scriptFileURL.getPath()); | ||
if (m.find() && m.groupCount() > 0) { | ||
try { | ||
return Integer.parseInt(m.group(1)); | ||
} catch (NumberFormatException nfe) { | ||
logger.warn("Extracted start level {} from {}, but it's not an integer. Ignoring.", m.group(1), | ||
scriptFileURL.getPath()); | ||
} | ||
} | ||
} | ||
|
||
return StartLevelService.STARTLEVEL_RULEENGINE; | ||
} | ||
|
||
public Optional<String> getScriptType() { | ||
String fileName = scriptFileURL.getPath(); | ||
int index = fileName.lastIndexOf("."); | ||
if (index == -1) { | ||
return Optional.empty(); | ||
} | ||
String fileExtension = fileName.substring(index + 1); | ||
|
||
// ignore known file extensions for "temp" files | ||
if (EXCLUDED_FILE_EXTENSIONS.contains(fileExtension) || fileExtension.endsWith("~")) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(fileExtension); | ||
} | ||
|
||
public String getScriptIdentifier() { | ||
return scriptFileURL.toString(); | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull ScriptFileReference other) { | ||
try { | ||
Path path1 = Paths.get(scriptFileURL.toURI()); | ||
String name1 = path1.getFileName().toString(); | ||
logger.trace("o1 [{}], path1 [{}], name1 [{}]", scriptFileURL, path1, name1); | ||
|
||
Path path2 = Paths.get(other.scriptFileURL.toURI()); | ||
String name2 = path2.getFileName().toString(); | ||
logger.trace("o2 [{}], path2 [{}], name2 [{}]", other.scriptFileURL, path2, name2); | ||
|
||
int nameCompare = name1.compareToIgnoreCase(name2); | ||
if (nameCompare != 0) { | ||
return nameCompare; | ||
} else { | ||
return path1.getParent().toString().compareToIgnoreCase(path2.getParent().toString()); | ||
} | ||
} catch (URISyntaxException e) { | ||
logger.error("URI syntax exception", e); | ||
return 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't use jetbrains null annotations.
See https://www.openhab.org/docs/developer/guidelines.html#null-annotations
Can you do a follow up PR with adding proper null annotations?
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.
Sorry about that! IDEA can be very keen on adding these sometimes; I hadn't noticed. I'll create a follow up PR to remove them.
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.
Thanks!