Changeset 9178


Ignore:
Timestamp:
May 12, 2011, 7:00:37 AM (14 years ago)
Author:
rick
Message:

More user-friendly parsing and definitions of meta-variables

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/gheat/management/commands/import_droidstumbler.py

    r9170 r9178  
    1515import datetime
    1616import gzip
     17import logging
    1718import os
    1819import sys
    1920
    20 def import_droidstumbler(location, meetrondje, gebruiker, email):
     21def import_droidstumbler(location, meetrondje, gebruiker, email, datum,show_progres=False):
    2122
    2223  g, created = Gebruiker.objects.get_or_create(naam=gebruiker , email=email)
    2324  a, created = Apparatuur.objects.get_or_create(antenne='buildin' , kaart='mobilePhone')
    24   mr = MeetRondje.objects.create(datum=datetime.datetime.now() , naam=meetrondje , gebruiker=g , apparatuur=a)
     25  mr = MeetRondje.objects.create(datum=datum , naam=meetrondje , gebruiker=g , apparatuur=a)
    2526
    2627  if location.endswith('.gz'):
     
    3132  count = 0
    3233  ap_cache = {}
    33   print "#INFO: Importing points"
    3434  for row in csvfile:
    3535    try:
    3636      epoch, msg_type, lat, lon, accuracy, ssid, bssid, level, frequency, capabilities = row
    3737    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))
    3939      continue
    4040    if msg_type == "data" and lat and lon:
     
    4545      # Give some feedback to the user
    4646      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()
    5253
    53   sys.stdout.write("%s\n" % count)
    54   print "#INFO: All done, goodbye"
     54  if show_progres:
     55    sys.stdout.write("%s\n" % count)
    5556
    5657
     
    5859  args = '<csvfile>[.gz]'
    5960  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"),)
    6467
    6568  def handle(self, *args, **options):
     
    7174    if not os.path.isfile(csv_file):
    7275      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.