3 # Generate an xearth database from the LDAP entries
4 # LDAP entires for lat/long can be in one of 3 different formats
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
17 import string, re, time, ldap, getopt, sys, pwd, posix;
18 from userdir_ldap import *;
22 # This needs to check for leading 0 to disambiguate some things
23 def DecDegree(Attr,Type):
24 Parts = re.match('[+-]?(\d*)\\.?(\d*)?',GetAttr(Attr,Type)).groups();
25 Val = string.atof(GetAttr(Attr,Type));
27 if (abs(Val) >= 1806060.0):
28 raise ValueError,"Too Big";
31 if abs(Val) >= 18060.0 or len(Parts[0]) > 5:
33 Secs = Val - long(Val);
34 Val = long(Val)/100.0;
35 Min = Val - long(Val);
36 Val = long(Val) + (Min*100.0 + Secs*100.0/60.0)/60.0;
39 elif abs(Val) >= 180 or len(Parts[0]) > 3:
41 Min = Val - long(Val);
42 Val = long(Val) + Min*100.0/60.0;
52 # Main program starts here
53 User = pwd.getpwuid(posix.getuid())[0];
55 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
56 for (switch, val) in options:
62 # Connect to the ldap server
63 l = ldap.open(LDAPServer);
64 print "Accessing LDAP directory as '" + User + "'";
65 Password = getpass(User + "'s password: ");
66 UserDn = "uid=" + User + "," + BaseDn;
67 l.simple_bind_s(UserDn,Password);
69 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
70 ["uid","cn","mn","sn","latitude","longitude"]);
74 print "Markers file will be written to markers.dat,",
76 F = open("markers.dat","w");
80 if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
85 F.write("%8s %8s \"\"\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude")));
87 F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(x,"latitude"),DecDegree(x,"longitude"),GetAttr(x,"uid"),EmailAddress(x)));
91 F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
93 F.write("# Failed => %s: %s\n" %(sys.exc_type,sys.exc_value));
95 print Count,"entries,",Failed,"failures.";