source: src/django_gheat/gheat/management/commands/import_datafile.py@ 9819

Last change on this file since 9819 was 9819, checked in by rick, 13 years ago

Correct annoying typo in naming of the acces*s* point.

  • Property svn:executable set to *
File size: 5.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Script for importing various stumble files.
5#
6# Rick van der Zwet <info@rickvanderzwet.nl>
7#
8from django.core.files import File
9from django.core.management.base import BaseCommand,CommandError
10from django.db import connection, transaction
11from django.db.utils import IntegrityError
12from gheat.models import *
13from optparse import OptionParser, make_option
14import datetime
15import gzip
16import os
17import sys
18import logging
19
20from collections import defaultdict
21
22from gheat.dataimport import import_file
23
24logger = logging.getLogger(__name__)
25logger.setLevel(logging.INFO)
26
27
28valid_prefixes = ['DroidStumbler-', 'Kismet-EeePC-', 'Kismet-', 'ScanResult-']
29def strip_prefix(filename):
30 """ Prefix removal """
31 for prefix in valid_prefixes:
32 if filename.startswith(prefix):
33 filename = filename[len(prefix):]
34 return filename
35
36
37valid_suffixes = ['.gz', '.gpsxml', '.netxml', '.csv', '.ns1']
38def strip_suffix(filename):
39 """ Suffix removal """
40 for suffix in valid_suffixes:
41 if filename.endswith(suffix):
42 filename = filename[:-len(suffix)]
43 return filename
44
45
46def strip_file(filename):
47 """ Prefix and suffix removal """
48 return strip_suffix(strip_prefix(filename))
49
50
51#Kismet-20110805-15-37-30-1
52#ScanResult-2011-05-09-201117
53strptime_choices = ['%Y%m%d-%H-%M-%S-1', '%Y-%m-%d-%H%M%S']
54def process_date(datestr):
55 for strptime in strptime_choices:
56 try:
57 return datetime.datetime.strptime(datestr,strptime)
58 except ValueError:
59 pass
60 logger.error("Invalid date '%s', options: %s, using: now()", datestr, strptime_choices)
61 return datetime.datetime.now()
62
63
64class Command(BaseCommand):
65 args = '<netstumber.ns1>[.gz] [netstumber2.ns1[.gz] netstumber3.ns1[.gz] ...]'
66 option_list = BaseCommand.option_list + (
67 make_option('-k', '--kaart', dest='kaart', default='onbekend',
68 help="Kaart gebruikt"),
69 make_option('-f', '--force', dest='force', default=False, action="store_true",
70 help="Import anyways, even if the meetrondje is already imported"),
71 make_option('-m', '--meetrondje', dest='meetrondje', default=None),
72 make_option('-g', '--gebruiker', dest='gebruiker', default='username',
73 help='Naam van de persoon die de meting uitgevoerd heeft'),
74 make_option('-e', '--email', dest='email', default='foo@bar.org',
75 help='Email van de persoon die de meting uitgevoerd heeft'),
76 make_option('-d', '--datum', dest='datum', default=None,
77 help="Provide date in following format: '%Y%m%d-%H-%M-%S-1', by \
78 default it will be generated from the filename"),
79 )
80
81 def handle(self, *args, **options):
82 if options['verbosity'] == 2:
83 logger.setLevel(logging.DEBUG)
84 if len(args) == 0:
85 self.print_help(sys.argv[0],sys.argv[1])
86 raise CommandError("Not all arguments are provided")
87
88 # Please first the netxml and the gpsxml files and the rest
89 sorted_args = [x for x in args if "netxml" in x] +\
90 [x for x in args if "gpsxml" in x] +\
91 [x for x in args if "ns1" in x]
92 remainder = list(set(args) - set(sorted_args))
93 args = sorted_args + remainder
94 logger.debug("Parsing files in the following order: %s", args)
95
96 # Make sure the all exists at first
97 for filename in args:
98 if not os.path.isfile(filename):
99 raise CommandError("file '%s' does not exists" % filename)
100
101
102 def get_date(filename):
103 if options['datum'] == None:
104 datestr = strip_file(os.path.basename(filename))
105 datum = process_date(datestr)
106 elif options['datum'] == 'now':
107 datum = datetime.datetime.now()
108 else:
109 datum = process_date(options['datum'])
110 return datum
111
112 def get_meetrondje(meetrondje):
113 # Meetrondje from filename if needed
114 if options['meetrondje'] == None:
115 meetrondje = strip_suffix(os.path.basename(filename))
116 else:
117 meetrondje = options['meetrondje']
118 return meetrondje
119
120 # Get Gheat Objects, pre-req
121 gebruiker, created = Gebruiker.objects.get_or_create(naam=options['gebruiker'],
122 email=options['email'])
123 apparatuur, created = Apparatuur.objects.get_or_create(kaart=options['kaart'])
124
125 # Check if all files are valid
126 for filename in args:
127 logger.info("Processing '%s'" % filename)
128 meetrondje, created = MeetRondje.objects.get_or_create(
129 datum=get_date(filename), naam=get_meetrondje(filename),
130 gebruiker=gebruiker, apparatuur=apparatuur)
131 if not options['force'] and not created:
132 logger.error("Meetrondje '%s' already imported", meetrondje)
133 continue
134
135 logger.info('Meetrondje: %s', meetrondje)
136 meetbestand = MeetBestand(meetrondje=meetrondje,is_imported=True)
137 meetbestand.bestand.save(os.path.basename(filename),File(open(filename)))
138 meetbestand.save()
139
140 counters = import_file(filename,meetrondje)
141 logger.info("summary accesspoints: total:%(ap_total)-6s added:%(ap_added)-6s failed:%(ap_failed)-6s ignored:%(ap_ignored)-6s" % counters)
142 logger.info("summary client : total:%(client_total)-6s added:%(client_added)-6s failed:%(client_failed)-6s ignored:%(client_ignored)-6s" % counters)
143 logger.info("summary metingen : total:%(meting_total)-6s added:%(meting_added)-6s failed:%(meting_failed)-6s ignored:%(meting_ignored)-6s" % counters)
144
145
146
147
Note: See TracBrowser for help on using the repository browser.