ud-mailgate: remove exception for münchen.debian.net
[mirror/userdir-ldap.git] / ud-xearth
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # Generate an xearth database from the LDAP entries
4 # LDAP entires for lat/long can be in one of 3 different formats
5 #    1) Decimal Degrees
6 #        +-DDD.DDDDDDDDDDDDDDD
7 #    2) Degrees Minutes (DGM), common output from GPS units
8 #        +-DDDMM.MMMMMMMMMMMMM
9 #    3) Degrees Minutes Seconds (DGMS)
10 #        +-DDDMMSS.SSSSSSSSSSS
11 # Decimal Degrees is the most basic format, but to have good accuracy it
12 # needs a large number of decimals. The other formats are all derived from it:
13 #  DGM -> DD   DDD + (MM.MMMMMMMM)/60
14 #  DGMS -> DD  DDD + (MM + (SS.SSSSSS)/60)/60
15 # For Latitude + is North, for Longitude + is East
16
17 import re, time, ldap, getopt, sys, pwd, os, posix;
18 from userdir_ldap import *;
19
20 Anon = 0;
21
22 # Main program starts here
23 User = pwd.getpwuid(posix.getuid())[0];
24 BindUser = User;
25 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
26 for (switch, val) in options:
27    if (switch == '-u'):
28       User = val;
29    if (switch == '-a'):
30       Anon = 1;
31
32 # Connect to the ldap server
33 l = passwdAccessLDAP(BaseDn, User)
34
35 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
36          ["uid","cn","mn","sn","latitude","longitude"]);
37
38 Attrs.sort();
39
40 print "Markers file will be written to markers.dat,",
41 sys.stdout.flush();
42 F = open("markers.dat","w");
43 Count = 0;
44 Failed = 0;
45 for x in Attrs:
46    if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
47       continue;
48    Count = Count + 1;
49    try:
50       if Anon != 0:
51          F.write("%8s %8s \"\"\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon)));
52       else:
53          F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon),GetAttr(x,"uid"),EmailAddress(x)));
54    except:
55       Failed = Failed + 1;
56       if Anon == 0:
57          F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
58       else:
59          F.write("# Failed => %s: %s\n" %(sys.exc_type,sys.exc_value));
60 F.close();
61 print Count,"entries,",Failed,"failures.";