Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added list of fields to cleanup_value module to leave dot at the end.

  • Loading branch information...
commit e49b777fff738fd140a6f149e1be541b270329ea 1 parent cc760ee
Szymon Guz authored
View
30 lib/akamod/cleanup_value.py
@@ -38,13 +38,22 @@ def cleanup(value, prop):
"""
# Do not remove double quotes from title
dquote = '' if prop == "sourceResource/title" else '"'
+
+ # Remove dot at the end if field name is not in the
+ # DONT_STRIP_DOT_END table.
+ with_dot = '' if prop in DONT_STRIP_DOT_END else "\."
# Tags for stripping at beginning and at the end.
- TAGS_FOR_STRIPPING = '[\.\' \r\t\n;,%s]*' % dquote
+
+ TAGS_FOR_STRIPPING = '[%s\' \r\t\n;,%s]*'
+
+ TAGS_FOR_STRIPPING_AT_BEGIN = TAGS_FOR_STRIPPING % ("\.", dquote)
+ TAGS_FOR_STRIPPING_AT_END = TAGS_FOR_STRIPPING % (with_dot, dquote)
+
REGEXPS = (' *-- *', '--'), \
('[\t ]{2,}', ' '), \
- ('^' + TAGS_FOR_STRIPPING, ''), \
- (TAGS_FOR_STRIPPING + '$', '')
-
+ ('^' + TAGS_FOR_STRIPPING_AT_BEGIN, ''), \
+ (TAGS_FOR_STRIPPING_AT_END + '$', '')
+
if isinstance(value, basestring):
value = value.strip()
for pattern, replace in REGEXPS:
@@ -61,6 +70,16 @@ def cleanup(value, prop):
-- place (may end in an abbreviated state name)
"""
+DONT_STRIP_DOT_END = [
+ "hasView/format",
+ "sourceResource/format",
+ "sourceResource/extent",
+ "sourceResource/description",
+ "sourceResource/rights",
+ "sourceResource/place",
+]
+
+# Below fields should have removed do at the end.
DEFAULT_PROP = [
"sourceResource/language",
"sourceResource/title",
@@ -68,13 +87,12 @@ def cleanup(value, prop):
"sourceResource/relation",
"sourceResource/publisher",
"sourceResource/subject",
- "sourceResource/format",
"sourceResource/date"
]
@simple_service('POST', 'http://purl.org/la/dp/cleanup_value', 'cleanup_value', 'application/json')
-def cleanup_value(body, ctype, action="cleanup_value", prop=",".join(DEFAULT_PROP)):
+def cleanup_value(body, ctype, action="cleanup_value", prop=",".join(DEFAULT_PROP + DONT_STRIP_DOT_END)):
'''
Service that accepts a JSON document and enriches the prop field of that document by:
View
10 test/dict_differ.py
@@ -9,17 +9,17 @@ def pinfo(*data):
print d
-def assert_same_jsons(this, that):
+def assert_same_jsons(expected, current):
"""
Checks if the dictionaries are the same.
It compares the keys and values.
Prints diff if they are not exact and throws exception.
"""
- d = DictDiffer(this, that)
+ d = DictDiffer(expected, current)
if not d.same():
d.print_diff()
- assert this == that
+ assert expected == current
class DictDiffer:
@@ -52,8 +52,8 @@ def _generate_diff(self):
diff[k] = ('KEY NOT FOUND IN SECOND DICT', self.first[k])
elif self.first[k] != self.second[k]:
diff[k] = {"DIFFERENT VALUES":
- {'FIRST DICT ': self.first[k],
- 'SECOND DICT': self.second[k]}
+ {'EXPECTED DICT:': self.first[k],
+ 'CURRENT DICT: ': self.second[k]}
}
for k in self.second.keys():
View
17 test/test_cleanup_value.py
@@ -86,6 +86,7 @@ def test_changing_values():
r, c = _get_server_response(json.dumps(data), 'aaa%2Fbbb')
exp = {}
exp["aaa"] = {"bbb": EXPECTED[i]}
+ print_error_log()
assert_same_jsons(exp, c)
@@ -135,6 +136,7 @@ def test_list_of_properties():
def test_changes_using_default_prop_value():
"""Should process all default values."""
INPUT = {
+ "hasView": {"format": "... format. "},
"aaa": "bbb...",
"sourceResource": {
"aaa": "bbb...",
@@ -142,11 +144,17 @@ def test_changes_using_default_prop_value():
"language": ["...aaa...", "...bbb;;;.;."],
"title": "sss...",
"publisher": ["that's me.."],
- "relation": [".first;", """ second relation..... \n. """, "\r\t\n\t\raaaa\r\n\t ..."]
+ "relation": [".first;", """ second relation..... \n. """, "\r\t\n\t\raaaa\r\n\t ..."],
+ "format": "... format. '''",
+ "extent": "...''',,, extent. ''',,,",
+ "description": ["... desc 1. ", "... desc 2. ''"],
+ "rights": "... rights. ",
+ "place": "... place. ",
},
"bbb": "ccc..."
}
EXPECTED = {
+ "hasView": {"format": "format."},
"aaa": "bbb...",
"sourceResource": {
"aaa": "bbb...",
@@ -154,7 +162,12 @@ def test_changes_using_default_prop_value():
"language": ["aaa", "bbb"],
"title": "sss",
"publisher": ["that's me"],
- "relation": ["first", "second relation", "aaaa"]
+ "relation": ["first", "second relation", "aaaa"],
+ "format": "format.",
+ "extent": "extent.",
+ "description": ["desc 1.", "desc 2."],
+ "rights": "rights.",
+ "place": "place.",
},
"bbb": "ccc..."
}
Please sign in to comment.
Something went wrong with that request. Please try again.