<commit_before>import os
import xlrd

from django.core.management.base import BaseCommand, CommandError

from olcc.models import Product

class Command(BaseCommand):
    """
    :todo: Use optparse to add a --quiet option to supress all output except errors.
    :todo: Write a separate management command to fetch the latest price document.
    """
    args = "<filename>"
    help = "Parses an excel document of OLCC price data."

    def handle(self, *args, **options):
        try:
            filename = args[0]
            if not filename:
                # Get latest file and hash for update
                # ...
                pass
            self.stdout.write("Importing from \"%s\"...\n" % filename)

            # Import workbook
            wb = xlrd.open_workbook(filename)
            self.stdout.write("Sheet Names:\n%s\n" % wb.sheet_names())

            # Get the first sheet
            sheet = wb.sheet_by_index(0)

            # Loop over worksheet
            for rownum in range(sheet.nrows):
                if rownum < 3:
                    continue
                values = sheet.row_values(rownum)
                if len(values) > 0:
                    if values[0][0] and values[0][0].isdigit():
                        try:
                            # TODO: Updating products
                            Product.from_row(values)
                        except IndexError:
                            pass
        except IOError as (errno, strerror):
            raise CommandError("%s" % strerror)
        except IndexError:
            raise CommandError("You must specify a filename!")<commit_msg>Update the olccimport management command to be more verbose. Include a --quiet command to silence all output. Better error handling.<commit_after>import os
import xlrd

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from olcc.models import Product

class Command(BaseCommand):
    """
    This command parses an Excel spreadsheet containing OLCC product
    and price data and imports it into the database.

    A typical price list can be found here: http://www.olcc.state.or.us/pdfs/Numeric_Price_List_Next_Month.xls
    """
    args = "<filename>"
    help = "Parses an excel document of OLCC price data."

    option_list = BaseCommand.option_list + (
        make_option('--quiet', action='store_true', dest='quiet', default=False,
            help='Suppress all output except errors'),
    )

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

        # Get our filename
        try:
            filename = args[0]
            if not os.path.exists(filename):
                raise CommandError("The file %s does not exist!" % filename)
        except IndexError:
            raise CommandError("You must specify a filename!")

        if not quiet:
            self.stdout.write("Importing prices from %s\n" % filename)
            self.stdout.flush()

        # Import workbook
        wb = xlrd.open_workbook(filename)

        # Get the first sheet
        sheet = wb.sheet_by_index(0)

        # Loop over worksheet
        count = 0
        for rownum in range(sheet.nrows):
            # Get the row
            values = sheet.row_values(rownum)
            if len(values) > 0:
                # Make sure the first column contains a valid product code
                if values[0] and values[0][0].isdigit():
                    if not quiet:
                        self.stdout.write(str(values))
                        self.stdout.write("\n")
                        self.stdout.flush()

                    # Import the product and price data
                    Product.from_row(values)
                    count += 1

        if not quiet:
            self.stdout.write("\nImported %d rows of price data\n" % count)
{'fraction_non_alphanumeric': 0.08657335406946605, 'fraction_numerical': 0.00414722654224987, 'mean_word_length': 1.7446657183499288, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '820160', 'n_tokens_mistral': 1032, 'n_tokens_neox': 976, 'n_words': 324}