<commit_before>import os
import csv

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from geopy import geocoders

from roaster.models import Roaster, Roast

class Command(BaseCommand):
    help = """\
        Imports a list of roasters from a CSV."""

    fields = (
        'name',
        'order_online',
        'cafe_on_site',
        'open_to_public',
        'address',
        'mon',
        'tue',
        'wed',
        'thu',
        'fri',
        'sat',
        'sun',
        'phone',
        'url',
        'description',
        'photo_url',
        'video_url',
        'video_embed',
        'roasts',
    )

    def uprint(self, msg):
        """
        Unbuffered print.
        """
        if not self.quiet:
            self.stdout.write("%s\n" % msg)
            self.stdout.flush()

    def handle(self, *args, **options):
        self.quiet = False

        try:
            path = os.path.abspath(args[0])

            if os.path.isfile(path):
                self.uprint('Importing from "%s"' % path)

                num_created = 0
                with open(path, 'rb') as f:
                    reader = csv.DictReader(f, fieldnames=self.fields)
                    for row in reader:
                        # Skip the first row
                        name = row.get('name').strip()
                        if row.get('name') and row.get('name') != 'Name':
                            roaster, created = Roaster.objects.get_or_create(
                                    name=name)

                            if created:
                                self.uprint('Importing new roaster: %s' % name)
                                num_created += 1
                            else:
                                self.uprint('Updating roaster: %s' % name)

                            # Geocode our address
                            g = geocoders.Google()

                            try:
                                place, (lat, lng) = g.geocode(row.get('address'))
                                self.uprint('  %s,%s' % (lat, lng))
                                roaster.lat = lat
                                roaster.lng = lng
                            except Exception as e:
                                self.uprint('  Failed to geocode address!')<commit_msg>Add some additional options to the roasterimport command.<commit_after>import os
import csv

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from geopy import geocoders
from optparse import make_option

from roaster.models import Roaster, Roast

IMPORT_TYPES = ('roasters', 'cafes',)

class Command(BaseCommand):
    args = "<filename>"
    help = """\
        Imports a list of roasters from a CSV."""

    option_list = BaseCommand.option_list + (
        make_option('--quiet', action='store_true', dest='quiet',
            default=False, help='Suppress all output except errors'),
        make_option('--import-type', choices=IMPORT_TYPES,
            dest='import_type', default='roasters',
            help='One of the following: %s' % (', '.join(IMPORT_TYPES),)),
        make_option('--geocode', action='store_true', dest='geocode',
            help='Geocode store addresses')
    )

    fields = (
        'name',
        'order_online',
        'cafe_on_site',
        'open_to_public',
        'address',
        'mon',
        'tue',
        'wed',
        'thu',
        'fri',
        'sat',
        'sun',
        'phone',
        'url',
        'description',
        'photo_url',
        'video_url',
        'video_embed',
        'roasts',
    )

    def uprint(self, msg):
        """
        Unbuffered print.
        """
        if not self.quiet:
            self.stdout.write("%s\n" % msg)
            self.stdout.flush()

    def handle(self, *args, **options):
        self.quiet = options.get('quiet', False)
        self.geocode = options.get('geocode', False)
        self.import_type = options.get('import_type')

        try:
            path = os.path.abspath(args[0])

            if os.path.isfile(path):
                self.uprint('Importing from "%s"' % path)

                num_created = 0
                with open(path, 'rb') as f:
                    reader = csv.DictReader(f, fieldnames=self.fields)
                    for row in reader:
                        # Skip the first row
                        name = row.get('name').strip()
                        if row.get('name') and row.get('name') != 'Name':
                            roaster, created = Roaster.objects.get_or_create(
                                    name=name)

                            if created:
                                self.uprint('Importing new roaster: %s' % name)
                                num_created += 1
                            else:
                                self.uprint('Updating roaster: %s' % name)

                            if self.geocode:
                                g = geocoders.Google()

                                try:
                                    # Geocode our address
                                    place, (lat, lng) = g.geocode(row.get('address'))
                                    self.uprint('  %s,%s' % (lat, lng))
                                    roaster.lat = lat
                                    roaster.lng = lng
                                except Exception as e:
                                    self.uprint('  Failed to geocode address!')
{'fraction_non_alphanumeric': 0.11005556551353289, 'fraction_numerical': 0.0010754615522495072, 'mean_word_length': 1.1916732128829537, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033836', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1475, 'n_words': 348}