Changeset 9640 for src/django_gheat/gheat/dataimport
- Timestamp:
- Aug 30, 2011, 8:36:25 PM (13 years ago)
- Location:
- src/django_gheat/gheat/dataimport
- Files:
-
- 1 added
- 1 copied
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
src/django_gheat/gheat/dataimport/__init__.py
r9639 r9640 10 10 # 11 11 from _mysql_exceptions import OperationalError 12 from django.core.files import File13 from django.core.management.base import BaseCommand,CommandError14 12 from django.db import connection, transaction 15 13 from django.db.utils import IntegrityError 16 14 from gheat.models import * 17 from optparse import OptionParser, make_option18 import datetime19 15 import gzip 20 16 import os … … 29 25 30 26 logger = logging.getLogger(__name__) 31 logger.setLevel(logging.INFO)32 27 33 28 def open_file(file): … … 39 34 40 35 41 valid_prefixes = ['DroidStumbler-', 'Kismet-','ScanResult-']42 def strip_prefix(filename):43 """ Prefix removal """44 for prefix in valid_prefixes:45 if filename.startswith(prefix):46 filename = filename[len(prefix):]47 return filename48 49 50 valid_suffixes = ['.gz', '.gpsxml', '.netxml', '.csv', '.ns1']51 def strip_suffix(filename):52 """ Suffix removal """53 for suffix in valid_suffixes:54 if filename.endswith(suffix):55 filename = filename[:-len(suffix)]56 return filename57 58 59 def strip_file(filename):60 """ Prefix and suffix removal """61 return strip_suffix(strip_prefix(filename))62 63 64 #Kismet-20110805-15-37-30-165 #ScanResult-2011-05-09-20111766 strptime_choices = ['%Y%m%d-%H-%M-%S-1', '%Y-%m-%d-%H%M%S']67 def process_date(datestr):68 for strptime in strptime_choices:69 try:70 return datetime.datetime.strptime(datestr,strptime)71 except ValueError:72 pass73 logger.error("Invalid date '%s', options: %s, using: now()", datestr, strptime_choices)74 return datetime.datetime.now()75 36 76 37 … … 187 148 188 149 189 190 150 def import_file(filename,meetrondje): 151 """ Import a file (on disk) """ 191 152 counters = { 192 153 'ap_added' : 0, 'ap_total' : 0, … … 208 169 (counters, ap_pool, client_pool, meting_pool) = droidstumbler.process_csv(fh, counters) 209 170 else: 210 raise CommandError("file '%s' format not recognized" % filename) 171 (ap_pool, client_pool, meting_pool) = (None, None, None) 172 logger.error("file '%s' format not recognized") 211 173 212 174 if ap_pool: … … 217 179 counters = import_metingen(meetrondje, meting_pool, counters) 218 180 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)181 logger.debug("summary accespoints: total:%(ap_total)-6s added:%(ap_added)-6s failed:%(ap_failed)-6s ignored:%(ap_ignored)-6s" % counters) 182 logger.debug("summary client : total:%(client_total)-6s added:%(client_added)-6s failed:%(client_failed)-6s ignored:%(client_ignored)-6s" % counters) 183 logger.debug("summary metingen : total:%(meting_total)-6s added:%(meting_added)-6s failed:%(meting_failed)-6s ignored:%(meting_ignored)-6s" % counters) 222 184 223 224 225 class Command(BaseCommand): 226 args = '<netstumber.ns1>[.gz] [netstumber2.ns1[.gz] netstumber3.ns1[.gz] ...]' 227 option_list = BaseCommand.option_list + ( 228 make_option('-k', '--kaart', dest='kaart', default='onbekend', 229 help="Kaart gebruikt"), 230 make_option('-m', '--meetrondje', dest='meetrondje', default=None), 231 make_option('-g', '--gebruiker', dest='gebruiker', default='username', 232 help='Naam van de persoon die de meting uitgevoerd heeft'), 233 make_option('-e', '--email', dest='email', default='foo@bar.org', 234 help='Email van de persoon die de meting uitgevoerd heeft'), 235 make_option('-d', '--datum', dest='datum', default=None, 236 help="Provide date in following format: '%Y%m%d-%H-%M-%S-1', by \ 237 default it will be generated from the filename"), 238 ) 239 240 def handle(self, *args, **options): 241 if options['verbosity'] == 2: 242 logger.setLevel(logging.DEBUG) 243 if len(args) == 0: 244 self.print_help(sys.argv[0],sys.argv[1]) 245 raise CommandError("Not all arguments are provided") 246 247 # Please first the netxml and the gpsxml files and the rest 248 sorted_args = [x for x in args if "netxml" in x] +\ 249 [x for x in args if "gpsxml" in x] +\ 250 [x for x in args if "ns1" in x] 251 remainder = list(set(args) - set(sorted_args)) 252 args = sorted_args + remainder 253 logger.debug("Parsing files in the following order: %s", args) 254 255 # Make sure the all exists at first 256 for filename in args: 257 if not os.path.isfile(filename): 258 raise CommandError("file '%s' does not exists" % filename) 259 260 261 def get_date(filename): 262 if options['datum'] == None: 263 datestr = strip_file(os.path.basename(filename)) 264 datum = process_date(datestr) 265 elif options['datum'] == 'now': 266 datum = datetime.datetime.now() 267 else: 268 datum = process_date(options['datum']) 269 return datum 270 271 def get_meetrondje(meetrondje): 272 # Meetrondje from filename if needed 273 if options['meetrondje'] == None: 274 meetrondje = strip_suffix(os.path.basename(filename)) 275 else: 276 meetrondje = options['meetrondje'] 277 return meetrondje 278 279 # Get Gheat Objects, pre-req 280 gebruiker, created = Gebruiker.objects.get_or_create(naam=options['gebruiker'], 281 email=options['email']) 282 apparatuur, created = Apparatuur.objects.get_or_create(kaart=options['kaart']) 283 284 # Check if all files are valid 285 for filename in args: 286 logger.info("Processing '%s'" % filename) 287 meetrondje, created = MeetRondje.objects.get_or_create( 288 datum=get_date(filename), naam=get_meetrondje(filename), 289 gebruiker=gebruiker, apparatuur=apparatuur) 290 if not created: 291 logger.error("Meetrondje '%s' already imported", meetrondje) 292 continue 293 294 logger.info('Meetrondje: %s', meetrondje) 295 meetbestand = MeetBestand(meetrondje=meetrondje,is_imported=True) 296 meetbestand.bestand.save(os.path.basename(filename),File(open(filename))) 297 meetbestand.save() 298 299 import_file(filename,meetrondje) 300 185 return counters
Note:
See TracChangeset
for help on using the changeset viewer.