Changeset 9627


Ignore:
Timestamp:
Aug 30, 2011, 6:34:39 PM (13 years ago)
Author:
rick
Message:

Allow importing multiple times.

File:
1 edited

Legend:

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

    r9626 r9627  
    2929logger.setLevel(logging.INFO)
    3030
    31 # Open files for reading
    3231def open_file(file):
    33  if file.endswith('.gz'):
    34    return gzip.open(file,'rb')
    35  else:
    36   return open(file,'rb')
    37 
    38 # Prefix/Suffix removal
     32  """ Open files for reading, unzip if needed """
     33  if file.endswith('.gz'):
     34    return gzip.open(file,'rb')
     35  else:
     36   return open(file,'rb')
     37
     38
    3939valid_prefixes = ['DroidStumbler-', 'Kismet-','ScanResult-']
    4040def strip_prefix(filename):
     41  """ Prefix removal """
    4142  for prefix in valid_prefixes:
    4243    if filename.startswith(prefix):
    4344      filename = filename[len(prefix):]
    4445  return filename
     46
     47
    4548valid_suffixes = ['.gz', '.gpsxml', '.netxml', '.csv', '.ns1']
    4649def strip_suffix(filename):
     50  """ Suffix removal """
    4751  for suffix in valid_suffixes:
    4852    if filename.endswith(suffix):
    4953      filename = filename[:-len(suffix)]
    5054  return filename
     55
     56
    5157def strip_file(filename):
     58  """ Prefix and suffix removal """
    5259  return strip_suffix(strip_prefix(filename))
    5360
     61
     62#Kismet-20110805-15-37-30-1
     63#ScanResult-2011-05-09-201117
     64strptime_choices = ['%Y%m%d-%H-%M-%S-1', '%Y-%m-%d-%H%M%S']
     65def process_date(datestr):
     66  for strptime in strptime_choices:
     67    try: return datetime.datetime.strptime(datestr,strptime)
     68    except ValueError: pass
     69  # Start nagging we cannot parse the entries
     70  raise CommandError("Invalid date '%s', options: %s" % (datestr, strptime_options))
    5471
    5572def import_accespoints(ap_pool, counters):
     
    146163
    147164  def handle(self, *args, **options):
    148     if options['verbosity'] > 1:
     165    if options['verbosity'] > 2:
    149166      logger.setLevel(logging.DEBUG)
    150167    if len(args) == 0:
     
    167184
    168185    def get_date(filename):
    169       def process_date(datestr):
    170         try:
    171            # Kismet-20110805-15-37-30-1
    172            return datetime.datetime.strptime(datestr,'%Y%m%d-%H-%M-%S-1')
    173         except ValueError:
    174           raise CommandError("Invalid date '%s'" % datestr)
    175186      if options['datum'] == None:
    176187         datestr = strip_file(os.path.basename(filename))
Note: See TracChangeset for help on using the changeset viewer.