Changeset 9178
- Timestamp:
- May 12, 2011, 7:00:37 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/management/commands/import_droidstumbler.py
r9170 r9178 15 15 import datetime 16 16 import gzip 17 import logging 17 18 import os 18 19 import sys 19 20 20 def import_droidstumbler(location, meetrondje, gebruiker, email ):21 def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False): 21 22 22 23 g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email) 23 24 a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone') 24 mr = MeetRondje.objects.create(datum=dat etime.datetime.now(), naam=meetrondje , gebruiker=g , apparatuur=a)25 mr = MeetRondje.objects.create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a) 25 26 26 27 if location.endswith('.gz'): … … 31 32 count = 0 32 33 ap_cache = {} 33 print "#INFO: Importing points"34 34 for row in csvfile: 35 35 try: 36 36 epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row 37 37 except ValueError: 38 print "# WARNING: Unable to parse line:%i '%s'" % (csvfile.line_num, row)38 logging.error("Unable to parse line:%i '%s'" % (csvfile.line_num, row)) 39 39 continue 40 40 if msg_type == "data" and lat and lon: … … 45 45 # Give some feedback to the user 46 46 count += 1 47 if (count % 1000) == 0: 48 sys.stdout.write(str(count)) 49 elif (count % 100) == 0: 50 sys.stdout.write(".") 51 sys.stdout.flush() 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() 52 53 53 sys.stdout.write("%s\n" % count)54 print "#INFO: All done, goodbye"54 if show_progres: 55 sys.stdout.write("%s\n" % count) 55 56 56 57 … … 58 59 args = '<csvfile>[.gz]' 59 60 option_list = BaseCommand.option_list + ( 60 make_option('-m', '--meetrondje', dest='meetrondje', default='rondje'), 61 make_option('-g', '--gebruiker', dest='gebruiker', default='username'), 62 make_option('-e', '--email', dest='email', default='foo@bar.org'), 63 ) 61 make_option('-m', '--meetrondje', dest='meetrondje', default=None), 62 make_option('-g', '--gebruiker', dest='gebruiker', default=os.environ['USER']), 63 make_option('-e', '--email', dest='email', default=os.environ['USER'] + '@example.org'), 64 make_option('-d', '--datum', dest='datum', default=None, help="Provide date \ 65 in following format: %Y-%m-%d-%H%M%S, by default it will be generated from \ 66 the filename"),) 64 67 65 68 def handle(self, *args, **options): … … 71 74 if not os.path.isfile(csv_file): 72 75 raise CommandError("csv file '%s' does not exists" % csv_file) 73 import_droidstumbler(csv_file,options['meetrondje'],options['gebruiker'],options['email']) 76 77 # Meetrondje from filename if needed 78 if options['meetrondje'] == None: 79 meetrondje = os.path.basename(csv_file).rstrip('.gz').rstrip('.csv') 80 else: 81 meetrondje = options['meetrondje'] 82 # Date from filename if needed 83 if options['datum'] == None: 84 datum = os.path.basename(csv_file).lstrip('ScanResult-').rstrip('.csv.gz') 85 else: 86 datum = options['datum'] 87 try: 88 datum = datetime.datetime.strptime(datum,'%Y-%m-%d-%H%M%S') 89 except ValueError: 90 logging.error("Invalid date '%s'" % options['datum']) 91 sys.exit(1) 92 93 self.stdout.write("Going to import '%s' for gebruiker '%s <%s>'\n" % (os.path.basename(csv_file), options['gebruiker'], options['email'])) 94 import_droidstumbler(csv_file,meetrondje,options['gebruiker'],options['email'], datum, True) 95 self.stdout.writeln("All done, goodbye")
Note:
See TracChangeset
for help on using the changeset viewer.