# Path: prefixes/models.py
# class Prefix(db.Expando):
#     """
#     Count all domains in the datastore that start with the same
#     prefix. The key name is the prefix, with a leading dot and
#     optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Prefix.name_cut('123456789')
#         '123456789'
#         >>> Prefix.name_cut('123456789', 3)
#         '123'
#         """
#         if length is None:
#             return name
#         else:
#             return name[:length]
# 
# class Suffix(db.Expando):
#     """
#     Count all domains in the datastore that end with the same suffix.
#     The key name is the suffix backwards, with a leading dot
#     and optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Suffix.name_cut('123456789')
#         '987654321'
#         >>> Suffix.name_cut('123456789', 3)
#         '987'
#         """
#         if length is None:
#             return name[::-1]
#         else:
#             return name[::-1][:length]


import os
import sys
from common.appenginepatch.aecmd import setup_env
from prefixes.models import Prefix, Suffix
LENGTHS = range(2, 7)


def auth_func():
    return open('.passwd').read().split(':')


def most_popular(model, variable, length, maximums, totals):
    query = model.all()
    query.filter('length', length),
    query.order('-count')
    total = maximum = 0
    counters = []
    prefixes = retry(query.fetch, 1000)
    for prefix in prefixes:
        if prefix.count < 10:
            break
        counters.append((prefix.key().name(), prefix.count))
        if prefix.count > maximum:
            maximum = prefix.count
        total += prefix.count
    counters.sort(key=lambda pair: -pair[1])
    print '%s[%d] = {' % (variable, length)
    for pair in counters:
        print "'%s': %d," % pair
    print '}'
    totals[length] = int(total)
    maximums[length] = int(maximum)



# Path: prefixes/models.py
# class Prefix(db.Expando):
#     """
#     Count all domains in the datastore that start with the same
#     prefix. The key name is the prefix, with a leading dot and
#     optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Prefix.name_cut('123456789')
#         '123456789'
#         >>> Prefix.name_cut('123456789', 3)
#         '123'
#         """
#         if length is None:
#             return name
#         else:
#             return name[:length]
# 
# class Suffix(db.Expando):
#     """
#     Count all domains in the datastore that end with the same suffix.
#     The key name is the suffix backwards, with a leading dot
#     and optional property 'resume' if incomplete.
#     """
#     length = db.IntegerProperty(required=True)
#     count = db.IntegerProperty(required=True, default=0)
#     com = db.IntegerProperty()
#     percentage = db.FloatProperty()
#     timestamp = db.DateTimeProperty()
# 
#     @classmethod
#     def name_cut(cls, name, length=None):
#         """
#         >>> Suffix.name_cut('123456789')
#         '987654321'
#         >>> Suffix.name_cut('123456789', 3)
#         '987'
#         """
#         if length is None:
#             return name[::-1]
#         else:
#             return name[::-1][:length]


# Path: tools/prefixes_popular.py
import os
import sys
from common.appenginepatch.aecmd import setup_env
from prefixes.models import Prefix, Suffix

LENGTHS = range(2, 7)


def auth_func():
    return open('.passwd').read().split(':')


def most_popular(model, variable, length, maximums, totals):
    query = model.all()
    query.filter('length', length),
    query.order('-count')
    total = maximum = 0
    counters = []
    prefixes = retry(query.fetch, 1000)
    for prefix in prefixes:
        if prefix.count < 10:
            break
        counters.append((prefix.key().name(), prefix.count))
        if prefix.count > maximum:
            maximum = prefix.count
        total += prefix.count
    counters.sort(key=lambda pair: -pair[1])
    print '%s[%d] = {' % (variable, length)
    for pair in counters:
        print "'%s': %d," % pair
    print '}'
    totals[length] = int(total)
    maximums[length] = int(maximum)



def sort_prefixes(filename):