# This file is part of OpenHatch.
# Copyright (C) 2012 OpenHatch, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.core.management.base import BaseCommand
import mysite.customs.core_bugimporters
import yaml
from django.utils import simplejson

class Command(BaseCommand):
    args = '<yaml/json_file yaml/json_file ...>'
    help = "Call this command with YAML/JSON files to load into the Bug table"

    def handle(self, *args, **options):
        for filename in args:
            with open(filename) as f:
                if filename.endswith('.json'):
                    bug_dicts = simplejson.load(f)
                else:
                    # assume YAML
                    s = f.read()
                    bug_dicts = yaml.loads(s)
            for bug_dict in bug_dicts:
                mysite.customs.core_bugimporters.import_one_bug_item(bug_dict)

@@ -861,16 +861,348 @@
 mplejson
+%0Aimport logging%0A%0A%0Adef jsonlines_decoder(f):%0A    for line in f:%0A        if line.endswith('%5Cn'):%0A            line = line %5B:-1%5D%0A        try:%0A            yield simplejson.loads(line)%0A        except:%0A            logging.exception(%22simplejson decode failed%22)%0A            logging.info(%22repr(line) was: %25s%22, repr(line))%0A            continue
 %0A%0Aclass 
@@ -1558,16 +1558,123 @@
 load(f)%0A
+                elif filename.endswith('.jsonlines'):%0A                    bug_dicts = jsonlines_decoder(f)%0A
         
@@ -1808,24 +1808,28 @@
             
+    
 for bug_dict
@@ -1843,16 +1843,20 @@
 _dicts:%0A
+    
         

Support jsonlines file format