Changeset 9179 for src/django_gheat/gheat/management
- Timestamp:
- May 12, 2011, 7:34:26 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/management/commands/import_droidstumbler.py
r9178 r9179 2 2 # -*- coding: utf-8 -*- 3 3 # 4 # Script for importing DroidStumbler .csv files 4 # Script for importing DroidStumbler .csv files, which takes the best value of 5 # each measurement point. 5 6 # 6 7 # Original by Dennis Wagenaar <d.wagenaar@gmail.com> … … 10 11 from django.core.management import setup_environ 11 12 from django.core.management.base import BaseCommand, CommandError 13 from django.db.utils import IntegrityError 12 14 from optparse import OptionParser, make_option 13 15 from gheat.models import * … … 19 21 import sys 20 22 23 def user_feedback(count, dot_step, summary_report): 24 if (count % summary_report) == 0: 25 sys.stdout.write(str(count)) 26 elif (count % dot_step) == 0: 27 sys.stdout.write(".") 28 sys.stdout.flush() 29 return count + 1 30 31 21 32 def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False): 22 33 23 34 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email) 24 35 a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone') 25 mr = MeetRondje.objects.create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a)36 mr, created = MeetRondje.objects.get_or_create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a) 26 37 38 ap_cache = {} 39 meting_pool = {} 40 41 # Process file 27 42 if location.endswith('.gz'): 28 43 fh = gzip.open(location,'rb') … … 30 45 fh = open(location,'rb') 31 46 csvfile = csv.reader(fh, delimiter=',') 32 count = 033 ap_cache = {}34 47 for row in csvfile: 35 48 try: … … 42 55 ap_cache[bssid], created = Accespoint.objects.get_or_create(mac=bssid, ssid=ssid, encryptie=capabilities) 43 56 44 m = Meting.objects.create(meetrondje=mr, accespoint=ap_cache[bssid], latitude=lat, longitude=lon, signaal=(100 + int(level))) 45 # Give some feedback to the user 46 count += 1 47 if show_progres: 48 if (count % 1000) == 0: 49 sys.stdout.write(str(count)) 50 elif (count % 100) == 0: 51 sys.stdout.write(".") 52 sys.stdout.flush() 57 # We store the best value found 58 key = (ap_cache[bssid], lat, lon) 59 signaal=(100 + int(level)) 60 if meting_pool.has_key(key): 61 meting_pool[key] = max(meting_pool[key], signaal) 62 else: 63 meting_pool[key] = signaal 64 65 66 # Import the data into the database 67 count = 0 68 for (ap,lat,lon),signal in meting_pool.iteritems(): 69 try: 70 m = Meting.objects.create(meetrondje=mr, accespoint=ap, latitude=lat, longitude=lon, signaal=signaal) 71 except IntegrityError, e: 72 logging.error("Unable to import - %s" % e) 73 continue 74 75 # Give some feedback to the user 76 if show_progres: 77 count = user_feedback(count, 100, 1000) 53 78 54 79 if show_progres: … … 91 116 sys.exit(1) 92 117 118 self.stdout.write('Meetrondje: %s @ %s\n' % (meetrondje, datum)) 93 119 self.stdout.write("Going to import '%s' for gebruiker '%s <%s>'\n" % (os.path.basename(csv_file), options['gebruiker'], options['email'])) 94 120 import_droidstumbler(csv_file,meetrondje,options['gebruiker'],options['email'], datum, True) 95 self.stdout.write ln("All done, goodbye")121 self.stdout.write("All done, goodbye")
Note:
See TracChangeset
for help on using the changeset viewer.