# Path: montanha/models.py
# class Institution(models.Model):
# 
#     class Meta:
#         verbose_name = _("Institution")
#         verbose_name_plural = _("Institutions")
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Name"))
# 
#     siglum = models.CharField(max_length=10,
#                               verbose_name=_("Siglum"),
#                               unique=True)
# 
#     logo = ThumbnailerImageField(
#         verbose_name=_('Logo'),
#         upload_to='institution',
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Legislature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislature")
#         verbose_name_plural = _("Legislatures")
# 
#     institution = models.ForeignKey("Institution")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     date_start = models.DateField(verbose_name=_("Date started"),
#                                   help_text=_("""Date in which this legislature started."""),
#                                   db_index=True)
# 
#     date_end = models.DateField(blank=True, null=True,
#                                 verbose_name=_("Date ended"),
#                                 help_text=_("""Date in which this legislature ended."""),
#                                 db_index=True)
# 
#     def __unicode__(self):
#         return u"%s's legislature starting at %s, ending at %s" % (self.institution.siglum,
#                                                                    str(self.date_start),
#                                                                    str(self.date_end))
# 
# class ExpenseNature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Expense nature")
#         verbose_name_plural = _("Expense natures")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     name = models.CharField(max_length=512,
#                             verbose_name=_("Expense nature name"))
# 
#     def __unicode__(self):
#         return u"%s" % self.name
# 
# class Legislator(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislator")
#         verbose_name_plural = _("Legislators")
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Full name"))
# 
#     alternative_names = models.ManyToManyField('AlternativeLegislatorName')
# 
#     picture = ThumbnailerImageField(
#         verbose_name=_('Picture'),
#         upload_to='legislator',
#         max_length=256,
#         blank=True,
#         null=True)
# 
#     site = models.URLField(
#         verbose_name=_('Site'),
#         blank=True,
#         null=True)
# 
#     email = models.EmailField(
#         verbose_name=_('Email'),
#         blank=True,
#         null=True)
# 
#     about = models.TextField(
#         verbose_name=_('About'),
#         blank=True,
#         null=True)
# 
#     gender = models.CharField(
#         verbose_name=_('Gender'),
#         choices=(
#             ('F', _('Female')),
#             ('M', _('Male'))),
#         blank=True,
#         null=True,
#         max_length=1)
# 
#     date_of_birth = models.DateField(
#         verbose_name=_('Date of Birth'),
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"Legislator %s" % (self.name)
# 
#     @property
#     def party(self):
#         party = self.mandate_set.order_by("-date_start")[0].party
#         if party and party.siglum:
#             return party.siglum
#         return u"Partido não informado"
# 
# class ArchivedExpense(AbstractExpense):
# 
#     class Meta:
#         verbose_name = _("Archived expense")
#         verbose_name_plural = _("Archived expenses")
# 
#     collection_run = models.ForeignKey("CollectionRun")


import threading
import time
from datetime import datetime, date
from basecollector import BaseCollector
from montanha.models import (
    Institution, Legislature, ExpenseNature, Legislator, ArchivedExpense
)


class CMBH(BaseCollector):
    def __init__(self, collection_runs, debug_enabled=False):
        super(CMBH, self).__init__(collection_runs, debug_enabled)

        self.institution, _ = Institution.objects.get_or_create(
            siglum='CMBH', name=u'Câmara Municipal de Belo Horizonte'
        )

        self.legislature, _ = Legislature.objects.get_or_create(
            institution=self.institution,
            date_start=datetime(2013, 1, 1),
            date_end=datetime(2016, 12, 31)
        )

        self.download_threads = []
        for x in range(NUM_THREADS):
            thread = threading.Thread(target=self._download_thread)
            thread.daemon = True
            self.download_threads.append(thread)

        self.download_queue = []
        self.download_lock = threading.Lock()

        self.processing_queue = []
        self.processing_condition = threading.Condition()

    def _download_thread(self):
        throttle = False

# Path: montanha/models.py
# class Institution(models.Model):
# 
#     class Meta:
#         verbose_name = _("Institution")
#         verbose_name_plural = _("Institutions")
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Name"))
# 
#     siglum = models.CharField(max_length=10,
#                               verbose_name=_("Siglum"),
#                               unique=True)
# 
#     logo = ThumbnailerImageField(
#         verbose_name=_('Logo'),
#         upload_to='institution',
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class Legislature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislature")
#         verbose_name_plural = _("Legislatures")
# 
#     institution = models.ForeignKey("Institution")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     date_start = models.DateField(verbose_name=_("Date started"),
#                                   help_text=_("""Date in which this legislature started."""),
#                                   db_index=True)
# 
#     date_end = models.DateField(blank=True, null=True,
#                                 verbose_name=_("Date ended"),
#                                 help_text=_("""Date in which this legislature ended."""),
#                                 db_index=True)
# 
#     def __unicode__(self):
#         return u"%s's legislature starting at %s, ending at %s" % (self.institution.siglum,
#                                                                    str(self.date_start),
#                                                                    str(self.date_end))
# 
# class ExpenseNature(models.Model):
# 
#     class Meta:
#         verbose_name = _("Expense nature")
#         verbose_name_plural = _("Expense natures")
# 
#     original_id = models.CharField(blank=True, null=True,
#                                    max_length=512,
#                                    verbose_name=_("Original ID"))
# 
#     name = models.CharField(max_length=512,
#                             verbose_name=_("Expense nature name"))
# 
#     def __unicode__(self):
#         return u"%s" % self.name
# 
# class Legislator(models.Model):
# 
#     class Meta:
#         verbose_name = _("Legislator")
#         verbose_name_plural = _("Legislators")
# 
#     name = models.CharField(max_length=2048,
#                             verbose_name=_("Full name"))
# 
#     alternative_names = models.ManyToManyField('AlternativeLegislatorName')
# 
#     picture = ThumbnailerImageField(
#         verbose_name=_('Picture'),
#         upload_to='legislator',
#         max_length=256,
#         blank=True,
#         null=True)
# 
#     site = models.URLField(
#         verbose_name=_('Site'),
#         blank=True,
#         null=True)
# 
#     email = models.EmailField(
#         verbose_name=_('Email'),
#         blank=True,
#         null=True)
# 
#     about = models.TextField(
#         verbose_name=_('About'),
#         blank=True,
#         null=True)
# 
#     gender = models.CharField(
#         verbose_name=_('Gender'),
#         choices=(
#             ('F', _('Female')),
#             ('M', _('Male'))),
#         blank=True,
#         null=True,
#         max_length=1)
# 
#     date_of_birth = models.DateField(
#         verbose_name=_('Date of Birth'),
#         blank=True,
#         null=True)
# 
#     def __unicode__(self):
#         return u"Legislator %s" % (self.name)
# 
#     @property
#     def party(self):
#         party = self.mandate_set.order_by("-date_start")[0].party
#         if party and party.siglum:
#             return party.siglum
#         return u"Partido não informado"
# 
# class ArchivedExpense(AbstractExpense):
# 
#     class Meta:
#         verbose_name = _("Archived expense")
#         verbose_name_plural = _("Archived expenses")
# 
#     collection_run = models.ForeignKey("CollectionRun")


# Path: montanha/management/commands/collectors/cmbh.py
import threading
import time
from datetime import datetime, date
from basecollector import BaseCollector
from montanha.models import (
    Institution, Legislature, ExpenseNature, Legislator, ArchivedExpense
)



class CMBH(BaseCollector):
    def __init__(self, collection_runs, debug_enabled=False):
        super(CMBH, self).__init__(collection_runs, debug_enabled)

        self.institution, _ = Institution.objects.get_or_create(
            siglum='CMBH', name=u'Câmara Municipal de Belo Horizonte'
        )

        self.legislature, _ = Legislature.objects.get_or_create(
            institution=self.institution,
            date_start=datetime(2013, 1, 1),
            date_end=datetime(2016, 12, 31)
        )

        self.download_threads = []
        for x in range(NUM_THREADS):
            thread = threading.Thread(target=self._download_thread)
            thread.daemon = True
            self.download_threads.append(thread)

        self.download_queue = []
        self.download_lock = threading.Lock()

        self.processing_queue = []
        self.processing_condition = threading.Condition()

    def _download_thread(self):
        throttle = False

        while True: