Debian Codegraph @

tasks/data-meta.js

raw symbols SCIP
1 "use strict";
2
3 var path = require('path');
4
5 function parseVersion (grunt, version) {
6 var newsPath = path.join('temp/download', version, 'NEWS'),
7 input = grunt.file.read(newsPath),
8 matches = input.match(/\nRelease (\d{4}[a-z]) /);
9
10 if (matches && matches[1]) {
11 if (version !== 'latest' && version !== matches[1]) {
12 throw new Error("Parsed version " + matches[1] +
13 " differs from specified version " + version)
14 }
15 return matches[1];
16 }
17 throw new Error("Could not find version from " + newsPath);
18 }
19
20 function parseLatLong (input, isLong) {
21 var sign = input[0] === '+' ? 1 : -1,
22 deg = ~~input.substr(1, 2 + isLong) * sign,
23 min = ~~input.substr(3 + isLong, 2),
24 sec = ~~input.substr(5 + isLong, 2);
25
26 min += sec / 60;
27 deg += min / 60;
28
29 return +deg.toFixed(4);
30 }
31
32 function parseCountries (grunt, version) {
33 var countryData = grunt.file.read('temp/download/' + version + '/iso3166.tab');
34 var countries = {};
35
36 countryData.split('\n').map(function (line) {
37 return line.split("#")[0].split(/\t/);
38 }).filter(function (parts) {
39 return parts.length === 2;
40 }).forEach(function (parts) {
41 countries[parts[0]] = {
42 name: parts[1],
43 abbr: parts[0],
44 zones: []
45 };
46 });
47 return countries;
48 }
49
50 function parseZones (grunt, version, countries) {
51 var latestZonesFile = 'temp/download/' + version + '/zone1970.tab';
52 var defaultZoneFile = 'temp/download/' + version + '/zone.tab';
53
54 var zoneData = grunt.file.exists(latestZonesFile)
55 ? grunt.file.read(latestZonesFile)
56 : grunt.file.read(defaultZoneFile);
57 var zones = {};
58
59 zoneData.split('\n').map(function (line) {
60 return line.split("#")[0].split(/\s+/); // split on whitespace before a # comment
61 }).filter(function (parts) {
62 return parts.length > 2;
63 }).forEach(function (parts) {
64 var latlong = parts[1].match(/[+-]\d+/g);
65 var zoneCountries = parts[0].split(",");
66 var zoneName = parts[2];
67
68 zones[zoneName] = {
69 name : zoneName,
70 lat : parseLatLong(latlong[0], 0),
71 long : parseLatLong(latlong[1], 1),
72 countries : zoneCountries,
73 comments : parts.slice(3).join(' ')
74 };
75
76 // add an entry of this zone, to all countries where its used
77 zoneCountries.forEach(function(countryCode) {
78 countries[countryCode].zones.push(zoneName);
79 });
80 });
81
82 return zones;
83 }
84
85 function addZoneCountries (grunt, version, zones, countries) {
86 var defaultZoneFile = 'temp/download/' + version + '/zone.tab';
87 var zoneData = grunt.file.read(defaultZoneFile);
88
89 zoneData.split('\n').map(function (line) {
90 return line.split("#")[0].split(/\s+/); // split on whitespace before a # comment
91 }).filter(function (parts) {
92 return parts.length > 2;
93 }).forEach(function (parts) {
94 var latlong = parts[1].match(/[+-]\d+/g);
95 var zoneCountries = parts[0].split(",");
96 var zoneName = parts[2];
97
98 if (!zones[zoneName]) {
99 zones[zoneName] = {
100 name : zoneName,
101 lat : parseLatLong(latlong[0], 0),
102 long : parseLatLong(latlong[1], 1),
103 countries : zoneCountries,
104 comments : parts.slice(3).join(' ')
105 };
106 }
107
108 zoneCountries.forEach(function (country) {
109 if (zones[zoneName].countries.indexOf(country) === -1) {
110 zones[zoneName].countries.push(country);
111 }
112 });
113
114 // add an entry of this zone, to all countries where its used
115 zoneCountries.forEach(function(countryCode) {
116 if (countries[countryCode].zones.indexOf(zoneName) === -1) {
117 countries[countryCode].zones.push(zoneName);
118 }
119 });
120
121
122 });
123 }
124
125 function filterCountries (allCountries) {
126 var countriesWithZones = {};
127
128 // Filter out the countries which dont have any timezones. eg: 'Bouvet Island', 'Heard Island and McDonald Islands'
129 for (var countryCode in allCountries) {
130 var country = allCountries[countryCode];
131 if(country.zones.length > 0) {
132 countriesWithZones[country.abbr] = country;
133 }
134 }
135 return countriesWithZones;
136 }
137
138 module.exports = function (grunt) {
139 grunt.registerTask('data-meta', '2. Parse metadata from zone1970.tab', function (version) {
140 version = version || 'latest';
141 var countries = parseCountries(grunt, version);
142 var zones = parseZones(grunt, version, countries);
143 addZoneCountries(grunt, version, zones, countries);
144 var validCountries = filterCountries(countries);
145
146 var output = {
147 version: parseVersion(grunt, version),
148 countries: validCountries,
149 zones: zones
150 };
151
152 grunt.file.write('data/meta/' + version + '.json', JSON.stringify(output, null, '\t'));
153 if (version === 'latest') {
154 grunt.file.copy('data/meta/latest.json', 'data/meta/' + output.version + '.json');
155 }
156
157 grunt.log.ok('Added metadata for ' + version + (version === 'latest' ? ': ' + output.version : ''));
158 });
159 };