Changeset 9639 for src


Ignore:
Timestamp:
Aug 30, 2011, 8:25:13 PM (13 years ago)
Author:
rick
Message:

Pushing the importer logic into its own function...

File:
1 edited

Legend:

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

    r9637 r9639  
    188188
    189189
     190def import_file(filename,meetrondje):
     191  counters = {
     192    'ap_added' : 0, 'ap_total' : 0,
     193    'ap_failed' : 0, 'ap_ignored' : 0,
     194    'client_added' : 0, 'client_total' : 0,
     195    'client_failed' : 0, 'client_ignored' : 0,
     196    'meting_added' : 0, 'meting_total' : 0,
     197    'meting_failed' : 0, 'meting_ignored' : 0
     198    }
     199
     200  fh = open_file(filename)
     201  if 'ns1' in filename:
     202    (counters, ap_pool, client_pool, meting_pool) = netstumbler.process_ns1(fh, counters)
     203  elif 'gpsxml' in filename:
     204    (counters, ap_pool, client_pool, meting_pool) = kismet.process_gpsxml(fh, counters)
     205  elif 'netxml' in filename:
     206    (counters, ap_pool, client_pool, meting_pool) = kismet.process_netxml(fh, counters)
     207  elif 'ScanResult' in filename:
     208    (counters, ap_pool, client_pool, meting_pool) = droidstumbler.process_csv(fh, counters)
     209  else:
     210    raise CommandError("file '%s' format not recognized" % filename)
     211
     212  if ap_pool:
     213    counters = import_accespoints(ap_pool, counters)
     214  if client_pool:
     215    counters = import_clients(client_pool, counters)
     216  if meting_pool:
     217    counters = import_metingen(meetrondje, meting_pool, counters)
     218
     219  logger.info("summary accespoints: total:%(ap_total)-6s added:%(ap_added)-6s failed:%(ap_failed)-6s ignored:%(ap_ignored)-6s" % counters)
     220  logger.info("summary client     : total:%(client_total)-6s added:%(client_added)-6s failed:%(client_failed)-6s ignored:%(client_ignored)-6s" % counters)
     221  logger.info("summary metingen   : total:%(meting_total)-6s added:%(meting_added)-6s failed:%(meting_failed)-6s ignored:%(meting_ignored)-6s" % counters)
     222
    190223
    191224
     
    245278
    246279    # Get Gheat Objects, pre-req
    247     g, created = Gebruiker.objects.get_or_create(naam=options['gebruiker'],
     280    gebruiker, created = Gebruiker.objects.get_or_create(naam=options['gebruiker'],
    248281      email=options['email'])
    249     a, created = Apparatuur.objects.get_or_create(kaart=options['kaart'])
     282    apparatuur, created = Apparatuur.objects.get_or_create(kaart=options['kaart'])
    250283
    251284    # Check if all files are valid
    252285    for filename in args:
    253286      logger.info("Processing '%s'" % filename)
    254       mr, created = MeetRondje.objects.get_or_create(
     287      meetrondje, created = MeetRondje.objects.get_or_create(
    255288        datum=get_date(filename), naam=get_meetrondje(filename),
    256         gebruiker=g, apparatuur=a)
     289        gebruiker=gebruiker, apparatuur=apparatuur)
    257290      if not created:
    258         logger.error("Meetrondje '%s' already imported",  mr)
     291        logger.error("Meetrondje '%s' already imported",  meetrondje)
    259292        continue
    260293
    261       counters = {
    262         'ap_added' : 0, 'ap_total' : 0,
    263         'ap_failed' : 0, 'ap_ignored' : 0,
    264         'client_added' : 0, 'client_total' : 0,
    265         'client_failed' : 0, 'client_ignored' : 0,
    266         'meting_added' : 0, 'meting_total' : 0,
    267         'meting_failed' : 0, 'meting_ignored' : 0
    268         }
    269       logger.info('Meetrondje: %s', mr)
    270       meetbestand = MeetBestand(meetrondje=mr,is_imported=True)
     294      logger.info('Meetrondje: %s', meetrondje)
     295      meetbestand = MeetBestand(meetrondje=meetrondje,is_imported=True)
    271296      meetbestand.bestand.save(os.path.basename(filename),File(open(filename)))
    272297      meetbestand.save()
    273298
    274       fh = open_file(filename)
    275       if 'ns1' in filename:
    276         (counters, ap_pool, client_pool, meting_pool) = netstumbler.process_ns1(fh, counters)
    277       elif 'gpsxml' in filename:
    278         (counters, ap_pool, client_pool, meting_pool) = kismet.process_gpsxml(fh, counters)
    279       elif 'netxml' in filename:
    280         (counters, ap_pool, client_pool, meting_pool) = kismet.process_netxml(fh, counters)
    281       elif 'ScanResult' in filename:
    282         (counters, ap_pool, client_pool, meting_pool) = droidstumbler.process_csv(fh, counters)
    283       else:
    284         raise CommandError("file '%s' format not recognized" % filename)
    285 
    286       if ap_pool:
    287         counters = import_accespoints(ap_pool, counters)
    288       if client_pool:
    289         counters = import_clients(client_pool, counters)
    290       if meting_pool:
    291         counters = import_metingen(mr, meting_pool, counters)
    292 
    293       logger.info("summary accespoints: total:%(ap_total)-6s added:%(ap_added)-6s failed:%(ap_failed)-6s ignored:%(ap_ignored)-6s" % counters)
    294       logger.info("summary client     : total:%(client_total)-6s added:%(client_added)-6s failed:%(client_failed)-6s ignored:%(client_ignored)-6s" % counters)
    295       logger.info("summary metingen   : total:%(meting_total)-6s added:%(meting_added)-6s failed:%(meting_failed)-6s ignored:%(meting_ignored)-6s" % counters)
     299      import_file(filename,meetrondje)
     300
Note: See TracChangeset for help on using the changeset viewer.