Top

telemeta.management.commands.telemeta-export-item-revisions-plot module

from optparse import make_option
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from telemeta.models import *
import datetime, time, calendar, itertools
import numpy as np

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.backends.backend_pdf import PdfPages


SECOND = 1
MINUTE = SECOND * 60
HOUR = MINUTE * 60
DAY = HOUR * 24
MONTH = DAY * 30


class Command(BaseCommand):
    help = "export MediaItem revisions vs. dates to a matplotlib PDF file"
    """ info :
    http://www.geophysique.be/2012/06/14/matplotlib-datetimes-tutorial-03-grouping-sparse-data/
    http://matplotlib.org/examples/pylab_examples/date_demo2.html
    http://matplotlib.org/examples/api/date_demo.html
    """
    args = 'year month day'
    binning = 7*DAY

    option_list = BaseCommand.option_list + (
          make_option('-y', '--year',
            dest='year',
            help='year of the first revision'),
          make_option('-m', '--month',
            dest='month',
            help='month of the first revision'),
          make_option('-d', '--day',
            dest='day',
            help='day of the first revision'),
    )
    def group(self, di):
        return int(calendar.timegm(di.timetuple()))/self.binning

    def handle(self, *args, **kwargs):
        limit_date = datetime.datetime(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))
        years    = mdates.YearLocator()
        months    = mdates.MonthLocator(range(1,13), bymonthday=1, interval=3)
        mondays   = mdates.WeekdayLocator(mdates.MONDAY)
        monthsFmt = mdates.DateFormatter("%b '%y")
        yearsFmt = mdates.DateFormatter('%Y')

        revisions = Revision.objects.filter(time__gte=limit_date)
        list_of_dates = [r.time for r in revisions]
        grouped_dates = [[datetime.datetime(*time.gmtime(d*self.binning)[:6]), len(list(g))] \
                            for d,g in itertools.groupby(list_of_dates, self.group)]
        grouped_dates = zip(*grouped_dates)
        
        revs = np.array(grouped_dates[1])
        revs_mean = np.mean(revs)

        fig = plt.figure()
        ax = fig.add_subplot(111, ylabel='Revisions by week (mean='+str(np.round(revs_mean, 1))+')')
        ax.plot_date(grouped_dates[0], grouped_dates[1] , '-')
        ax.xaxis.set_major_locator(months)
        ax.xaxis.set_major_formatter(monthsFmt)
        ax.xaxis.set_minor_locator(mondays)
        ax.autoscale_view()
        ax.grid(True)
        fig.autofmt_xdate()

        plt.savefig('/tmp/telemeta-revisions.png')
        plt.savefig('/tmp/telemeta-revisions.pdf')

        #plt.show()

Module variables

var DAY

var HOUR

var ITEM_PUBLIC_ACCESS_CHOICES

var ITEM_TRANSODING_STATUS

var MINUTE

var MONTH

var PUBLIC_ACCESS_CHOICES

var SCOPE_CHOICES

var SECOND

var TYPE_CHOICES

var app_name

var code_linesep

var collection_code_regex

var collection_published_code_regex

var collection_unpublished_code_regex

var default_decoding

var default_encoding

var engine

var eol

var ext

var item_code_regex

var item_published_code_regex

var item_unpublished_code_regex

var mime_type

var private_extra_types

var public_extra_types

var resource_code_regex

var strict_code

Classes

class Command

class Command(BaseCommand):
    help = "export MediaItem revisions vs. dates to a matplotlib PDF file"
    """ info :
    http://www.geophysique.be/2012/06/14/matplotlib-datetimes-tutorial-03-grouping-sparse-data/
    http://matplotlib.org/examples/pylab_examples/date_demo2.html
    http://matplotlib.org/examples/api/date_demo.html
    """
    args = 'year month day'
    binning = 7*DAY

    option_list = BaseCommand.option_list + (
          make_option('-y', '--year',
            dest='year',
            help='year of the first revision'),
          make_option('-m', '--month',
            dest='month',
            help='month of the first revision'),
          make_option('-d', '--day',
            dest='day',
            help='day of the first revision'),
    )
    def group(self, di):
        return int(calendar.timegm(di.timetuple()))/self.binning

    def handle(self, *args, **kwargs):
        limit_date = datetime.datetime(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))
        years    = mdates.YearLocator()
        months    = mdates.MonthLocator(range(1,13), bymonthday=1, interval=3)
        mondays   = mdates.WeekdayLocator(mdates.MONDAY)
        monthsFmt = mdates.DateFormatter("%b '%y")
        yearsFmt = mdates.DateFormatter('%Y')

        revisions = Revision.objects.filter(time__gte=limit_date)
        list_of_dates = [r.time for r in revisions]
        grouped_dates = [[datetime.datetime(*time.gmtime(d*self.binning)[:6]), len(list(g))] \
                            for d,g in itertools.groupby(list_of_dates, self.group)]
        grouped_dates = zip(*grouped_dates)
        
        revs = np.array(grouped_dates[1])
        revs_mean = np.mean(revs)

        fig = plt.figure()
        ax = fig.add_subplot(111, ylabel='Revisions by week (mean='+str(np.round(revs_mean, 1))+')')
        ax.plot_date(grouped_dates[0], grouped_dates[1] , '-')
        ax.xaxis.set_major_locator(months)
        ax.xaxis.set_major_formatter(monthsFmt)
        ax.xaxis.set_minor_locator(mondays)
        ax.autoscale_view()
        ax.grid(True)
        fig.autofmt_xdate()

        plt.savefig('/tmp/telemeta-revisions.png')
        plt.savefig('/tmp/telemeta-revisions.pdf')

Ancestors (in MRO)

  • Command
  • django.core.management.base.BaseCommand
  • __builtin__.object

Class variables

var args

var binning

var can_import_settings

var help

info : http://www.geophysique.be/2012/06/14/matplotlib-datetimes-tutorial-03-grouping-sparse-data/ http://matplotlib.org/examples/pylab_examples/date_demo2.html http://matplotlib.org/examples/api/date_demo.html

var leave_locale_alone

var option_list

var output_transaction

var requires_model_validation

Methods

def __init__(

self)

def __init__(self):
    self.style = color_style()

def create_parser(

self, prog_name, subcommand)

Create and return the OptionParser which will be used to parse the arguments to this command.

def create_parser(self, prog_name, subcommand):
    """
    Create and return the ``OptionParser`` which will be used to
    parse the arguments to this command.
    """
    return OptionParser(prog=prog_name,
                        usage=self.usage(subcommand),
                        version=self.get_version(),
                        option_list=self.option_list)

def execute(

self, *args, **options)

Try to execute this command, performing model validation if needed (as controlled by the attribute self.requires_model_validation, except if force-skipped).

def execute(self, *args, **options):
    """
    Try to execute this command, performing model validation if
    needed (as controlled by the attribute
    ``self.requires_model_validation``, except if force-skipped).
    """
    self.stdout = OutputWrapper(options.get('stdout', sys.stdout))
    self.stderr = OutputWrapper(options.get('stderr', sys.stderr), self.style.ERROR)
    if self.can_import_settings:
        from django.conf import settings
    saved_locale = None
    if not self.leave_locale_alone:
        # Only mess with locales if we can assume we have a working
        # settings file, because django.utils.translation requires settings
        # (The final saying about whether the i18n machinery is active will be
        # found in the value of the USE_I18N setting)
        if not self.can_import_settings:
            raise CommandError("Incompatible values of 'leave_locale_alone' "
                               "(%s) and 'can_import_settings' (%s) command "
                               "options." % (self.leave_locale_alone,
                                             self.can_import_settings))
        # Switch to US English, because django-admin.py creates database
        # content like permissions, and those shouldn't contain any
        # translations.
        from django.utils import translation
        saved_locale = translation.get_language()
        translation.activate('en-us')
    try:
        if self.requires_model_validation and not options.get('skip_validation'):
            self.validate()
        output = self.handle(*args, **options)
        if output:
            if self.output_transaction:
                # This needs to be imported here, because it relies on
                # settings.
                from django.db import connections, DEFAULT_DB_ALIAS
                connection = connections[options.get('database', DEFAULT_DB_ALIAS)]
                if connection.ops.start_transaction_sql():
                    self.stdout.write(self.style.SQL_KEYWORD(connection.ops.start_transaction_sql()))
            self.stdout.write(output)
            if self.output_transaction:
                self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;"))
    finally:
        if saved_locale is not None:
            translation.activate(saved_locale)

def get_version(

self)

Return the Django version, which should be correct for all built-in Django commands. User-supplied commands should override this method.

def get_version(self):
    """
    Return the Django version, which should be correct for all
    built-in Django commands. User-supplied commands should
    override this method.
    """
    return django.get_version()

def group(

self, di)

def group(self, di):
    return int(calendar.timegm(di.timetuple()))/self.binning

def handle(

self, *args, **kwargs)

def handle(self, *args, **kwargs):
    limit_date = datetime.datetime(int(kwargs.get('year')), int(kwargs.get('month')), int(kwargs.get('day')))
    years    = mdates.YearLocator()
    months    = mdates.MonthLocator(range(1,13), bymonthday=1, interval=3)
    mondays   = mdates.WeekdayLocator(mdates.MONDAY)
    monthsFmt = mdates.DateFormatter("%b '%y")
    yearsFmt = mdates.DateFormatter('%Y')
    revisions = Revision.objects.filter(time__gte=limit_date)
    list_of_dates = [r.time for r in revisions]
    grouped_dates = [[datetime.datetime(*time.gmtime(d*self.binning)[:6]), len(list(g))] \
                        for d,g in itertools.groupby(list_of_dates, self.group)]
    grouped_dates = zip(*grouped_dates)
    
    revs = np.array(grouped_dates[1])
    revs_mean = np.mean(revs)
    fig = plt.figure()
    ax = fig.add_subplot(111, ylabel='Revisions by week (mean='+str(np.round(revs_mean, 1))+')')
    ax.plot_date(grouped_dates[0], grouped_dates[1] , '-')
    ax.xaxis.set_major_locator(months)
    ax.xaxis.set_major_formatter(monthsFmt)
    ax.xaxis.set_minor_locator(mondays)
    ax.autoscale_view()
    ax.grid(True)
    fig.autofmt_xdate()
    plt.savefig('/tmp/telemeta-revisions.png')
    plt.savefig('/tmp/telemeta-revisions.pdf')

def print_help(

self, prog_name, subcommand)

Print the help message for this command, derived from self.usage().

def print_help(self, prog_name, subcommand):
    """
    Print the help message for this command, derived from
    ``self.usage()``.
    """
    parser = self.create_parser(prog_name, subcommand)
    parser.print_help()

def run_from_argv(

self, argv)

Set up any environment changes requested (e.g., Python path and Django settings), then run this command. If the command raises a CommandError, intercept it and print it sensibly to stderr. If the --traceback option is present or the raised Exception is not CommandError, raise it.

def run_from_argv(self, argv):
    """
    Set up any environment changes requested (e.g., Python path
    and Django settings), then run this command. If the
    command raises a ``CommandError``, intercept it and print it sensibly
    to stderr. If the ``--traceback`` option is present or the raised
    ``Exception`` is not ``CommandError``, raise it.
    """
    parser = self.create_parser(argv[0], argv[1])
    options, args = parser.parse_args(argv[2:])
    handle_default_options(options)
    try:
        self.execute(*args, **options.__dict__)
    except Exception as e:
        if options.traceback or not isinstance(e, CommandError):
            raise
        # self.stderr is not guaranteed to be set here
        stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
        stderr.write('%s: %s' % (e.__class__.__name__, e))
        sys.exit(1)

def usage(

self, subcommand)

Return a brief description of how to use this command, by default from the attribute self.help.

def usage(self, subcommand):
    """
    Return a brief description of how to use this command, by
    default from the attribute ``self.help``.
    """
    usage = '%%prog %s [options] %s' % (subcommand, self.args)
    if self.help:
        return '%s\n\n%s' % (usage, self.help)
    else:
        return usage

def validate(

self, app=None, display_num_errors=False)

Validates the given app, raising CommandError for any errors.

If app is None, then this will validate all installed apps.

def validate(self, app=None, display_num_errors=False):
    """
    Validates the given app, raising CommandError for any errors.
    If app is None, then this will validate all installed apps.
    """
    from django.core.management.validation import get_validation_errors
    s = StringIO()
    num_errors = get_validation_errors(s, app)
    if num_errors:
        s.seek(0)
        error_text = s.read()
        raise CommandError("One or more models did not validate:\n%s" % error_text)
    if display_num_errors:
        self.stdout.write("%s error%s found" % (num_errors, '' if num_errors == 1 else 's'))