Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

#!/usr/bin/python 

# -*- coding: utf-8 -*- 

 

"""Checker methods""" 

 

import pywikibot 

 

import erfgoedbot.common as common 

 

_logger = "update_database" 

 

 

def reportDataError(errorMsg, wikiPage, exceptWord, comment=''): 

"""Report data error to the talk page of the list.""" 

if not comment: 

comment = errorMsg 

 

pywikibot.debug(errorMsg, _logger) 

talkPage = wikiPage.toggleTalkPage() 

try: 

content = talkPage.get() 

except (pywikibot.NoPage, pywikibot.IsRedirectPage): 

content = '' 

if exceptWord and exceptWord not in content: 

content += '\n\n{0} --~~~~\n\n'.format(errorMsg) 

common.save_to_wiki_or_local(talkPage, comment, content) 

return True 

 

return False 

 

 

def is_int(s): 

"""Check if a string is a valid int.""" 

try: 

int(s) 

return True 

except (ValueError, TypeError): 

return False 

 

 

def checkLat(lat, monumentKey, countryconfig, sourcePage): 

"""Check if a latitude has a valid value.""" 

if len(lat): 

try: 

lat = float(lat) 

except ValueError: 

errorMsg = "Invalid latitude value: %s for monument %s" % ( 

lat, monumentKey) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

countryBbox = '' 

if (countryconfig.get('countryBbox')): 

countryBbox = countryconfig.get('countryBbox') 

 

if (lat > 90 or lat < -90): 

errorMsg = "Latitude for monument %s out of range: %s" % ( 

monumentKey, lat) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

elif (countryBbox): 

maxsplit = 3 

(left, bottom, right, top) = countryBbox.split(",", maxsplit) 

bottom = float(bottom) 

top = float(top) 

minLat = min(bottom, top) 

maxLat = max(bottom, top) 

if (lat > maxLat or lat < minLat): 

errorMsg = "Latitude for monument %s out of country area: %s" % ( 

monumentKey, lat) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

else: 

return True 

else: 

return True 

 

 

def checkLon(lon, monumentKey, countryconfig, sourcePage): 

"""Check if a longitude has a valid value.""" 

if len(lon): 

try: 

lon = float(lon) 

except ValueError: 

errorMsg = "Invalid longitude value: %s for monument %s" % ( 

lon, monumentKey) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

countryBbox = '' 

if (countryconfig.get('countryBbox')): 

countryBbox = countryconfig.get('countryBbox') 

 

if (lon > 180 or lon < -180): 

errorMsg = "Longitude for monument %s out of range: %s" % ( 

monumentKey, lon) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

elif (countryBbox): 

maxsplit = 3 

(left, bottom, right, top) = countryBbox.split(",", maxsplit) 

left = float(left) 

right = float(right) 

minLon = min(left, right) 

maxLon = max(left, right) 

if (lon > maxLon or lon < minLon): 

errorMsg = "Longitude for monument %s out of country area: %s" % ( 

monumentKey, lon) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

else: 

return True 

else: 

return True 

 

 

def check_wikidata(wd_item, monumentKey, sourcePage): 

"""Check that a value is a potential wikidata entity.""" 

if len(wd_item): 

if wd_item.startswith('Q') and is_int(wd_item[1:]): 

return True 

else: 

errorMsg = "Invalid wikidata value: %s for monument %s" % ( 

wd_item, monumentKey) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

 

 

def check_lat_with_lon(fieldnames, monumentKey, sourcePage): 

"""Check that lat and lon are always paired.""" 

if 'lat' in fieldnames and 'lon' not in fieldnames: 

errorMsg = "Longitude is not set for monument %s." % ( 

monumentKey, ) 

reportDataError(errorMsg, sourcePage, monumentKey) 

if 'lon' in fieldnames and 'lat' not in fieldnames: 

errorMsg = "Latitude is not set for monument %s." % ( 

monumentKey, ) 

reportDataError(errorMsg, sourcePage, monumentKey) 

 

 

def check_integer(text, monumentKey, sourcePage): 

"""Check that a value is an integer.""" 

if not is_int(text): 

errorMsg = "Invalid integer value: %s for monument %s" % ( 

text, monumentKey) 

reportDataError(errorMsg, sourcePage, monumentKey) 

return False 

return True