-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
260 additions
and
72 deletions.
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
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,49 @@ | ||
LEVEL_ALL = "all" | ||
LEVEL_WARN = "warn" | ||
LEVEL_ERROR = "error" | ||
|
||
|
||
class LintContext(object): | ||
|
||
def __init__(self, level=LEVEL_WARN): | ||
self.level = level | ||
self.found_errors = False | ||
self.found_warns = False | ||
|
||
self.valid_messages = [] | ||
self.info_messages = [] | ||
self.warn_messages = [] | ||
self.error_messages = [] | ||
|
||
def __handle_message(self, message_list, message, *args, **kwds): | ||
if kwds or args: | ||
message = message.format(*args, **kwds) | ||
message_list.append(message) | ||
|
||
def valid(self, message, *args, **kwds): | ||
self.__handle_message(self.valid_messages, message, *args, **kwds) | ||
|
||
def info(self, message, *args, **kwds): | ||
self.__handle_message(self.info_messages, message, *args, **kwds) | ||
|
||
def error(self, message, *args, **kwds): | ||
self.__handle_message(self.error_messages, message, *args, **kwds) | ||
|
||
def warn(self, message, *args, **kwds): | ||
self.__handle_message(self.warn_messages, message, *args, **kwds) | ||
|
||
def print_messages(self): | ||
for message in self.error_messages: | ||
self.found_errors = True | ||
print(".. ERROR: %s" % message) | ||
|
||
if self.level != LEVEL_ERROR: | ||
for message in self.warn_messages: | ||
self.found_warns = True | ||
print(".. WARNING: %s" % message) | ||
|
||
if self.level == LEVEL_ALL: | ||
for message in self.info_messages: | ||
print(".. INFO: %s" % message) | ||
for message in self.valid_messages: | ||
print(".. CHECK: %s" % message) |
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
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
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
52 changes: 52 additions & 0 deletions
52
java/src/main/java/org/galaxyproject/gxformat2/LintContext.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,52 @@ | ||
package org.galaxyproject.gxformat2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LintContext { | ||
private boolean foundErrors = false; | ||
private boolean foundWarns = false; | ||
|
||
private List<String> validMessages = new ArrayList<String>(); | ||
private List<String> infoMessages = new ArrayList<String>(); | ||
private List<String> warnMessages = new ArrayList<String>(); | ||
private List<String> errorMessages = new ArrayList<String>(); | ||
|
||
LintContext() {} | ||
|
||
boolean getFoundErrors() { | ||
return this.foundErrors; | ||
} | ||
|
||
boolean getFoundWarns() { | ||
return this.foundWarns; | ||
} | ||
|
||
void valid(String message, Object... args) { | ||
this.validMessages.add(String.format(message, args)); | ||
} | ||
|
||
void info(String message, Object... args) { | ||
this.infoMessages.add(String.format(message, args)); | ||
} | ||
|
||
void error(String message, Object... args) { | ||
this.errorMessages.add(String.format(message, args)); | ||
} | ||
|
||
void warn(String message, Object... args) { | ||
this.warnMessages.add(String.format(message, args)); | ||
} | ||
|
||
void printMessages() { | ||
for (final String message : this.errorMessages) { | ||
this.foundErrors = true; | ||
System.out.print(".. ERROR " + message); | ||
} | ||
|
||
for (final String message : this.warnMessages) { | ||
this.foundWarns = true; | ||
System.out.print(".. WARNING " + message); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
java/src/main/java/org/galaxyproject/gxformat2/LintUtils.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,51 @@ | ||
package org.galaxyproject.gxformat2; | ||
|
||
import java.util.Map; | ||
|
||
class LintUtils { | ||
static <T> T ensureKey( | ||
LintContext lintContext, Object hasKeys, String key, Class<T> hasClass, Object hasValue) { | ||
if (!(hasKeys instanceof Map)) { | ||
lintContext.error("expected [%s] to be a dictionary type", hasKeys); | ||
return null; | ||
} | ||
final Map<String, Object> map = (Map<String, Object>) hasKeys; | ||
if (!map.containsKey(key)) { | ||
lintContext.error("expected to have key [%s] but absent", key); | ||
} | ||
final Object value = map.get(key); | ||
return ensureKeyHasValue(lintContext, map, key, value, hasClass, hasValue); | ||
} | ||
|
||
static <T> T ensureKeyIfPresent( | ||
LintContext lintContext, Object hasKeys, String key, T defaultValue, Class<T> hasClass) { | ||
if (!(hasKeys instanceof Map)) { | ||
lintContext.error("expected [%s] to be a dictionary type", hasKeys); | ||
return null; | ||
} | ||
final Map<String, Object> map = (Map<String, Object>) hasKeys; | ||
if (!map.containsKey(key)) { | ||
return defaultValue; | ||
} | ||
final Object value = map.get(key); | ||
return ensureKeyHasValue(lintContext, map, key, value, hasClass, null); | ||
} | ||
|
||
static <T> T ensureKeyHasValue( | ||
LintContext lintContext, | ||
Map hasKeys, | ||
String key, | ||
Object value, | ||
Class<T> hasClass, | ||
Object hasValue) { | ||
if (!hasClass.isInstance(value)) { | ||
lintContext.error( | ||
"expected value [%s] with key [%s] to be of class %s", key, value, hasClass); | ||
return null; | ||
} | ||
if (hasValue != null && !hasValue.equals(value)) { | ||
lintContext.error("expected value [%s] with key [%s] to be %s", key, value, hasValue); | ||
} | ||
return (T) value; | ||
} | ||
} |
Oops, something went wrong.