<commit_before>from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from dal import autocomplete
from django import forms
from django.forms import ModelForm

from profiles.models import Profile
from register.models import Alumno, Apoderado, Personal, Promotor, Director, Cajero, Tesorero, Proveedor, Colegio, Sistemas, Administrativo, \
    Direccion, Docente
from utils.forms import ValidProfileFormMixin
from utils.models import TipoDocumento, TipoSexo, Departamento, Provincia, Distrito

class PersonaForm(ModelForm):

    direccion = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '13', 'class': 'form-control'}), label="Direccion")
    referencia = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '14', 'class': 'form-control'}), label="Referencia")
    departamento = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Departamento")
    provincia = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Provincia", required=False)
    distrito = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Distrito", required=False)
    tipo_cel = forms.ChoiceField(widget=forms.Select(attrs={'tabindex': '15', 'class': 'form-control'}), label="Tipo Movil",
                                 required=False)
    celular = forms.CharField(widget=forms.NumberInput(attrs={'tabindex': '16', 'class': 'form-control'}), label="Celular",
                              required=False)
    celulares = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'tabindex': '17', 'class': 'form-control'}), label="Números",
                                          required=False)

    @property
    def ChoiceTipoDocumento(self):
        choices = [(tipo.id_tipo, tipo.descripcion) for tipo in TipoDocumento.objects.all()]
        return choices

    @property
    def ChoiceTipoSexo(self):
        choices = [(sex.id_sexo, sex.descripcion) for sex in TipoSexo.objects.all()]
        return choices

    @property
    def ChoiceDepartamento(self):

        choices = []

        choices.append(('-1', 'Seleccione'))

        for d in Departamento.objects.all():
            choices.append((d.id_departamento, d.descripcion))

        return choices

    # @property
    def ChoiceProvincia(self):
        # choices = [(p.id_provincia, p.descripcion) for p in Provincia.objects.filter(departamento__id_departamento=dpto)]
        choices = [(p.id_provincia, p.descripcion) for p in
                   Provincia.objects.all()]
        return choices

    # @property
    def ChoiceDistrito(self):
        # choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.filter(provincia__id_provincia=prov)]
        choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.all()]
        return choices

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['tipo_documento'] = forms.ChoiceField(choices=self.ChoiceTipoDocumento,
                                                          widget=forms.Select(attrs={'tabindex': '5', 'class': 'form-control', 'onchange': 'dni()'}))
        self.fields['sexo'] = forms.ChoiceField(choices=self.ChoiceTipoSexo,
                                                widget=forms.Select(attrs={'tabindex': '7', 'class': 'form-control'}))
        self.fields['departamento'] = forms.ChoiceField(choices = self.ChoiceDepartamento, initial='-1',
                                                        widget=forms.Select(attrs={'tabindex': '10', 'class': 'form-control'}))
        self.fields['nombre'].widget.attrs = {'tabindex': '1', 'class': 'form-control', 'maxlength': '50'}
        self.fields['segundo_nombre'].widget.attrs = {'tabindex': '2', 'class': 'form-control', 'maxlength': '200'}
        self.fields['apellido_pa'].widget.attrs = {'tabindex': '3', 'class': 'form-control', 'maxlength': '50'}
        self.fields['apellido_ma'].widget.attrs = {'tabindex': '4', 'class': 'form-control', 'maxlength': '50'}
        self.fields['numero_documento'].widget.attrs = {'tabindex': '6', 'class': 'form-control', 'maxlength': '15'}
        self.fields['correo'].widget.attrs = {'tabindex': '7', 'class': 'form-control'}
        self.fields['fecha_nac'] = forms.DateField(widget=forms.DateInput, input_formats=['%Y-%m-%d'])
        self.fields['fecha_nac'].widget.attrs = {'tabindex': '8', 'class': 'form-control', 'onChange': 'validarFecNac()'}

        try:
            #cargar los valores guardados en la dirección
            if kwargs['instance'].pk is not None:
                direc = Direccion.objects.get(persona__id_persona=kwargs['instance'].pk)
                self.fields['departamento'].initial = direc.dpto

                opciones_provincias = self.ChoiceProvincia()
                opciones_distritos = self.ChoiceDistrito()

                self.fields['provincia'] = forms.ChoiceField(choices=opciones_provincias,
                    widget=forms.Select(attrs={'tabindex': '11', 'class': 'form-control'}))
                self.fields['distrito'] = forms.ChoiceField(choices=opciones_distritos,
                    widget=forms.Select(attrs={'tabindex': '12', 'class': 'form-control'}))

                self.fields['provincia'].initial = direc.provincia
                self.fields['distrito'].initial = direc.distrito
                self.fields['direccion'].initial = direc.calle
                self.fields['referencia'].initial = direc.referencia
        except:
            self.fields['provincia'] = forms.ChoiceField(choices=self.ChoiceProvincia, initial='-1',
                                                            widget=forms.Select(
                                                                attrs={'tabindex': '10', 'class': 'form-control'}))
            self.fields['distrito'] = forms.ChoiceField(choices=self.ChoiceDistrito, initial='-1',
                                                            widget=forms.Select(
                                                                attrs={'tabindex': '10', 'class': 'form-control'}))

    class Meta:
        model = Profile
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class AlumnoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Alumno", required=False)

    class Meta:
        model = Alumno
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class ApoderadoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Apoderado", required=False)

    #alu = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '23', 'class': 'form-control'}), label="Direccion")

    alumno = forms.ModelMultipleChoiceField(queryset=Alumno.objects.all(),required= False,
                                 widget=autocomplete.ModelSelect2Multiple(url='registers:alumno_autocomplete',attrs={'tabindex': '27', 'class': 'form-control'}))

    #parentesco = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'class' : 'form-control'}))

    def ChoiceParentesco(self):
        MY_CHOICES = (
            ('Padre', 'Padre'),
            ('Madre', 'Madre'),
            ('Tio', 'Tio'),
            ('Hermano', 'Hermano')
        )
        return MY_CHOICES

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['parentesco'] = forms.ChoiceField(choices=self.ChoiceParentesco())
        self.fields['parentesco'].widget.attrs.update({'tabindex': '10', 'class': 'form-control'})

    class Meta:
        model = Apoderado
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento',
                  'numero_documento', 'sexo', 'correo', 'fecha_nac']

class PersonalForm(ValidProfileFormMixin, PersonaForm):

    class Meta:
        model = Personal
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class SistemasForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Sistemas", required=False)

    class Meta:
        model = Sistemas
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class PromotorForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Promotor", required=False)

    class Meta:
        model = Promotor
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class DirectorForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Director", required=False)

    class Meta:
        model = Director
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class CajeroForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Cajero", required=False)

    class Meta:
        model = Cajero
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class TesoreroForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Tesorero", required=False)

    class Meta:
        model = Tesorero
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class AdministrativoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Administrativo", required=False)

    class Meta:
        model = Administrativo
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class DocenteForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Docente", required=False)

    class Meta:
        model = Docente
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class ProveedorForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['razon_social'].label = "Razón Social"
        self.fields['ruc'].label = "RUC"
        self.helper = FormHelper()
        self.helper.form_id = "idproveedor"
        self.helper.form_method = "post"

        self.helper.add_input(Submit('submit', 'Grabar', css_class="btn btn-primary btn-block btn-flat"))

    class Meta:
        model = Proveedor
        fields = ['razon_social', 'ruc']

class ColegioForm(ModelForm):

    direccion = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="Direccion")
    referencia = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="Referencia")
    departamento = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Departamento")
    provincia = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Provincia")
    distrito = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Distrito")
    tipo_cel = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Tipo Movil",
                                 required=False)
    celular = forms.CharField(widget=forms.NumberInput(attrs={'class': 'form-control'}), label="Celular",
                              required=False)
    celulares = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'class': 'form-control'}), label="Números",
                                          required=False)

    @property
    def ChoiceTipoDocumento(self):
        choices = [(tipo.id_tipo, tipo.descripcion) for tipo in TipoDocumento.objects.all()]
        return choices

    @property
    def ChoiceTipoSexo(self):
        choices = [(sex.id_sexo, sex.descripcion) for sex in TipoSexo.objects.all()]
        return choices

    @property
    def ChoiceDepartamento(self):
        choices = [(d.id_departamento, d.descripcion) for d in Departamento.objects.all()]
        return choices

    @property
    def ChoiceProvincia(self):
        choices = [(p.id_provincia, p.descripcion) for p in Provincia.objects.all()]
        return choices

    @property
    def ChoiceDistrito(self):
        choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.all()]
        return choices

    class Meta:
        model = Colegio
        fields = [
            'nombre',
            'ruc',
            'ugel',
            'departamento',
            'provincia',
            'distrito',
            'referencia',
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['nombre'].widget.attrs.update({'class': 'form-control'})
        self.fields['ruc'].widget.attrs.update({'class': 'form-control'})
        self.fields['ugel'].widget.attrs.update({'class': 'form-control'})
        self.fields['departamento'] = forms.ChoiceField(choices=self.ChoiceDepartamento,
                                                        widget=forms.Select(attrs={'class': 'form-control'}))
        self.fields['provincia'] = forms.ChoiceField(choices=self.ChoiceProvincia,
                                                     widget=forms.Select(attrs={'class': 'form-control'}))
        self.fields['distrito'] = forms.ChoiceField(choices=self.ChoiceDistrito,
                                                    widget=forms.Select(attrs={'class': 'form-control'}))

class DocenteForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Docente", required=False)

    class Meta:
        model = Docente
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']
<commit_msg>modificando parentesco<commit_after>from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from dal import autocomplete
from django import forms
from django.forms import ModelForm

from profiles.models import Profile
from register.models import Alumno, Apoderado, Personal, Promotor, Director, Cajero, Tesorero, Proveedor, Colegio, Sistemas, Administrativo, \
    Direccion, Docente
from utils.forms import ValidProfileFormMixin
from utils.models import TipoDocumento, TipoSexo, Departamento, Provincia, Distrito

class PersonaForm(ModelForm):

    direccion = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '13', 'class': 'form-control'}), label="Direccion")
    referencia = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '14', 'class': 'form-control'}), label="Referencia")
    departamento = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Departamento")
    provincia = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Provincia", required=False)
    distrito = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Distrito", required=False)
    tipo_cel = forms.ChoiceField(widget=forms.Select(attrs={'tabindex': '15', 'class': 'form-control'}), label="Tipo Movil",
                                 required=False)
    celular = forms.CharField(widget=forms.NumberInput(attrs={'tabindex': '16', 'class': 'form-control'}), label="Celular",
                              required=False)
    celulares = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'tabindex': '17', 'class': 'form-control'}), label="Números",
                                          required=False)

    @property
    def ChoiceTipoDocumento(self):
        choices = [(tipo.id_tipo, tipo.descripcion) for tipo in TipoDocumento.objects.all()]
        return choices

    @property
    def ChoiceTipoSexo(self):
        choices = [(sex.id_sexo, sex.descripcion) for sex in TipoSexo.objects.all()]
        return choices

    @property
    def ChoiceDepartamento(self):

        choices = []

        choices.append(('-1', 'Seleccione'))

        for d in Departamento.objects.all():
            choices.append((d.id_departamento, d.descripcion))

        return choices

    # @property
    def ChoiceProvincia(self):
        # choices = [(p.id_provincia, p.descripcion) for p in Provincia.objects.filter(departamento__id_departamento=dpto)]
        choices = [(p.id_provincia, p.descripcion) for p in
                   Provincia.objects.all()]
        return choices

    # @property
    def ChoiceDistrito(self):
        # choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.filter(provincia__id_provincia=prov)]
        choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.all()]
        return choices

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['tipo_documento'] = forms.ChoiceField(choices=self.ChoiceTipoDocumento,
                                                          widget=forms.Select(attrs={'tabindex': '5', 'class': 'form-control', 'onchange': 'dni()'}))
        self.fields['sexo'] = forms.ChoiceField(choices=self.ChoiceTipoSexo,
                                                widget=forms.Select(attrs={'tabindex': '7', 'class': 'form-control'}))
        self.fields['departamento'] = forms.ChoiceField(choices = self.ChoiceDepartamento, initial='-1',
                                                        widget=forms.Select(attrs={'tabindex': '10', 'class': 'form-control'}))
        self.fields['nombre'].widget.attrs = {'tabindex': '1', 'class': 'form-control', 'maxlength': '50'}
        self.fields['segundo_nombre'].widget.attrs = {'tabindex': '2', 'class': 'form-control', 'maxlength': '200'}
        self.fields['apellido_pa'].widget.attrs = {'tabindex': '3', 'class': 'form-control', 'maxlength': '50'}
        self.fields['apellido_ma'].widget.attrs = {'tabindex': '4', 'class': 'form-control', 'maxlength': '50'}
        self.fields['numero_documento'].widget.attrs = {'tabindex': '6', 'class': 'form-control', 'maxlength': '15'}
        self.fields['correo'].widget.attrs = {'tabindex': '7', 'class': 'form-control'}
        self.fields['fecha_nac'] = forms.DateField(widget=forms.DateInput, input_formats=['%Y-%m-%d'])
        self.fields['fecha_nac'].widget.attrs = {'tabindex': '8', 'class': 'form-control', 'onChange': 'validarFecNac()'}

        try:
            #cargar los valores guardados en la dirección
            if kwargs['instance'].pk is not None:
                direc = Direccion.objects.get(persona__id_persona=kwargs['instance'].pk)
                self.fields['departamento'].initial = direc.dpto

                opciones_provincias = self.ChoiceProvincia()
                opciones_distritos = self.ChoiceDistrito()

                self.fields['provincia'] = forms.ChoiceField(choices=opciones_provincias,
                    widget=forms.Select(attrs={'tabindex': '11', 'class': 'form-control'}))
                self.fields['distrito'] = forms.ChoiceField(choices=opciones_distritos,
                    widget=forms.Select(attrs={'tabindex': '12', 'class': 'form-control'}))

                self.fields['provincia'].initial = direc.provincia
                self.fields['distrito'].initial = direc.distrito
                self.fields['direccion'].initial = direc.calle
                self.fields['referencia'].initial = direc.referencia
        except:
            self.fields['provincia'] = forms.ChoiceField(choices=self.ChoiceProvincia, initial='-1',
                                                            widget=forms.Select(
                                                                attrs={'tabindex': '10', 'class': 'form-control'}))
            self.fields['distrito'] = forms.ChoiceField(choices=self.ChoiceDistrito, initial='-1',
                                                            widget=forms.Select(
                                                                attrs={'tabindex': '10', 'class': 'form-control'}))

    class Meta:
        model = Profile
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class AlumnoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Alumno", required=False)

    class Meta:
        model = Alumno
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class ApoderadoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Apoderado", required=False)

    #alu = forms.CharField(widget=forms.TextInput(attrs={'tabindex': '23', 'class': 'form-control'}), label="Direccion")

    alumno = forms.ModelMultipleChoiceField(queryset=Alumno.objects.all(),required= False,
                                 widget=autocomplete.ModelSelect2Multiple(url='registers:alumno_autocomplete',attrs={'tabindex': '27', 'class': 'form-control'}))

    #parentesco = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'class' : 'form-control'}))

    def ChoiceParentesco(self):
        MY_CHOICES = (
            ('Padre', 'Padre'),
            ('Madre', 'Madre'),
            ('Tio', 'Tio'),
            ('Hermano', 'Hermano'),
            ('Pariente', 'Pariente')
        )
        return MY_CHOICES

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['parentesco'] = forms.ChoiceField(choices=self.ChoiceParentesco())
        self.fields['parentesco'].widget.attrs.update({'tabindex': '10', 'class': 'form-control'})

    class Meta:
        model = Apoderado
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento',
                  'numero_documento', 'sexo', 'correo', 'fecha_nac']

class PersonalForm(ValidProfileFormMixin, PersonaForm):

    class Meta:
        model = Personal
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class SistemasForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Sistemas", required=False)

    class Meta:
        model = Sistemas
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class PromotorForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Promotor", required=False)

    class Meta:
        model = Promotor
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class DirectorForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Director", required=False)

    class Meta:
        model = Director
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class CajeroForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Cajero", required=False)

    class Meta:
        model = Cajero
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class TesoreroForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Tesorero", required=False)

    class Meta:
        model = Tesorero
        fields = [ 'nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class AdministrativoForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Administrativo", required=False)

    class Meta:
        model = Administrativo
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class DocenteForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Docente", required=False)

    class Meta:
        model = Docente
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']

class ProveedorForm(ModelForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.fields['razon_social'].label = "Razón Social"
        self.fields['ruc'].label = "RUC"
        self.helper = FormHelper()
        self.helper.form_id = "idproveedor"
        self.helper.form_method = "post"

        self.helper.add_input(Submit('submit', 'Grabar', css_class="btn btn-primary btn-block btn-flat"))

    class Meta:
        model = Proveedor
        fields = ['razon_social', 'ruc']

class ColegioForm(ModelForm):

    direccion = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="Direccion")
    referencia = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), label="Referencia")
    departamento = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Departamento")
    provincia = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Provincia")
    distrito = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Distrito")
    tipo_cel = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label="Tipo Movil",
                                 required=False)
    celular = forms.CharField(widget=forms.NumberInput(attrs={'class': 'form-control'}), label="Celular",
                              required=False)
    celulares = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'class': 'form-control'}), label="Números",
                                          required=False)

    @property
    def ChoiceTipoDocumento(self):
        choices = [(tipo.id_tipo, tipo.descripcion) for tipo in TipoDocumento.objects.all()]
        return choices

    @property
    def ChoiceTipoSexo(self):
        choices = [(sex.id_sexo, sex.descripcion) for sex in TipoSexo.objects.all()]
        return choices

    @property
    def ChoiceDepartamento(self):
        choices = [(d.id_departamento, d.descripcion) for d in Departamento.objects.all()]
        return choices

    @property
    def ChoiceProvincia(self):
        choices = [(p.id_provincia, p.descripcion) for p in Provincia.objects.all()]
        return choices

    @property
    def ChoiceDistrito(self):
        choices = [(d.id_distrito, d.descripcion) for d in Distrito.objects.all()]
        return choices

    class Meta:
        model = Colegio
        fields = [
            'nombre',
            'ruc',
            'ugel',
            'departamento',
            'provincia',
            'distrito',
            'referencia',
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['nombre'].widget.attrs.update({'class': 'form-control'})
        self.fields['ruc'].widget.attrs.update({'class': 'form-control'})
        self.fields['ugel'].widget.attrs.update({'class': 'form-control'})
        self.fields['departamento'] = forms.ChoiceField(choices=self.ChoiceDepartamento,
                                                        widget=forms.Select(attrs={'class': 'form-control'}))
        self.fields['provincia'] = forms.ChoiceField(choices=self.ChoiceProvincia,
                                                     widget=forms.Select(attrs={'class': 'form-control'}))
        self.fields['distrito'] = forms.ChoiceField(choices=self.ChoiceDistrito,
                                                    widget=forms.Select(attrs={'class': 'form-control'}))

class DocenteForm(ValidProfileFormMixin, PersonaForm):

    title = forms.CharField(label="Registrar Docente", required=False)

    class Meta:
        model = Docente
        fields = ['nombre', 'segundo_nombre', 'apellido_pa', 'apellido_ma', 'tipo_documento', 'numero_documento',
                  'sexo', 'correo', 'fecha_nac']
<|endoftext|>


<commit_before>import pyjd # dummy in pyjs

from pyjamas.ui.TabBar import TabBar
from pyjamas.ui.TabPanel import TabPanel
from pyjamas.ui import HasAlignment
from pyjamas.ui.Image import Image
from pyjamas.ui.DockPanel import DockPanel
from pyjamas.ui import HasAlignment
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Composite import Composite
#from pyjamas.ui import DecoratorPanel
from pyjamas.ui import MouseListener
from pyjamas.ui import Event
from pyjamas import Window
from pyjamas.ui.DecoratorPanel import DecoratedTabPanel, DecoratorPanel
from pyjamas.ui.DecoratorPanel import DecoratorTitledPanel
from pyjamas.HTTPRequest import HTTPRequest
from PageLoader import PageListLoader, PageLoader
from pyjamas.ui.HTMLLinkPanel import HTMLLinkPanel
from pyjamas import History

#class PrettyTab(DecoratorPanel):
class PrettyTab(Composite):

    def __init__(self, text, imageUrl):

        DecoratorPanel.__init__(self, DecoratorPanel.DECORATE_ALL)

        p = HorizontalPanel()
        p.setSpacing(3)
        self.img = Image(imageUrl)
        self.txt = HTML(text)
        p.add(self.img)
        p.add(self.txt)

        self.add(p)

    def addClickListener(self, listener):

        self.img.addClickListener(listener)
        self.txt.addClickListener(listener)

class Tabs:

    def onModuleLoad(self):

        dock = DockPanel()
        self.header = HTML(Width="100%")
        self.footer = HTML(Width="100%")
        self.sidebar = HTML(Width="200px", Height="100%", StyleName="sidebar")
        self.fTabs = DecoratedTabPanel(Size=("100%", "100%"),
                                       StyleName="tabs")

        #dp = DecoratorTitledPanel("Tabs", "bluetitle", "bluetitleicon",
        #              ["bluetop", "bluetop2", "bluemiddle", "bluebottom"])
        #dp.add(self.fTabs)

        dock.add(self.header, DockPanel.NORTH)
        dock.add(self.footer, DockPanel.SOUTH)
        dock.add(self.sidebar, DockPanel.EAST)
        dock.add(self.fTabs, DockPanel.CENTER)
        dock.setCellVerticalAlignment(self.fTabs, HasAlignment.ALIGN_TOP)
        #dock.setCellHorizontalAlignment(self.fTabs, HasAlignment.ALIGN_CENTER)
        dock.setCellWidth(self.header, "100%")
        dock.setCellWidth(self.sidebar, "200px")

        RootPanel().add(dock)

        self.loadPageList()
        History.addHistoryListener(self)

    def createPage(self, title, purpose, text):

        if purpose == 'faq':
            self.faq_pages[title] = text
            if len(self.faq_pages) != len(self.faq_list):
                return
            faq = self.page_widgets['FAQ']
            for l in self.faq_list:
                question = l[0]
                answer = self.faq_pages[question]
                html = faq.getHTML()
                html += "
%s
\n" % question
                html += "\n%s\n\n" % answer
                faq.setHTML(html)
            html = "
\n%s
\n" % html
            faq.setHTML(html)
            faq.replaceLinks(use_page_href=False)
            return

        if title == 'header':
            self.header.setHTML(text)
            return
        elif title == 'footer':
            self.footer.setHTML(text)
            return
        elif title == 'sidebar':
            self.sidebar.setHTML(text)
            return

        self.pages[title] = text
        if len(self.pages) != len(self.page_list):
            return
        self.page_widgets = {}
        self.tab_index = {}
        for [idx, l] in enumerate(self.page_list):
            title = l[0]
            text = self.pages[title]
            self.tab_index[title] = idx
            widget = HTMLLinkPanel(text)
            self.fTabs.add(widget, title, True)
            self.page_widgets[title] = widget
            if title == 'FAQ':
                HTTPRequest().asyncGet("faq/questions.txt",
                                       PageListLoader(self, "faq"))
            else:
                widget.replaceLinks(use_page_href=False)
        self.fTabs.selectTab(0)

    def createImage(self, imageUrl):
        image = Image(imageUrl)
        image.setStyleName("ks-images-Image")

        p = VerticalPanel()
        p.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)
        p.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
        p.add(image)

        return p

    def onHistoryChanged(self, token):
        print "onHistoryChanged", token, self.pages.keys()
        if self.pages.has_key(token):
            idx = self.tab_index(token)
            self.fTabs.selectTab(idx)

    def loadPageList(self):
        HTTPRequest().asyncGet("sidebar.html",
                                PageLoader(self, "sidebar", "contents"))
        HTTPRequest().asyncGet("header.html",
                                PageLoader(self, "header", "contents"))
        HTTPRequest().asyncGet("footer.html",
                                PageLoader(self, "footer", "contents"))
        HTTPRequest().asyncGet("contents.txt",
                                PageListLoader(self, "contents"))

    def loadPages(self, pages, purpose):
        if purpose == 'contents':
            self.pages = {}
            self.page_list = pages
        elif purpose == 'faq':
            self.faq_pages = {}
            self.faq_list = pages

        for l in pages:
            title = l[0]
            desc = l[1]
            HTTPRequest().asyncGet(desc, PageLoader(self, title, purpose))

if __name__ == '__main__':
    pyjd.setup("./public/index.html")
    app = Tabs()
    app.onModuleLoad()
    pyjd.run()

<commit_msg>oops must use (i, idx) not [i, idx] in for enumerate<commit_after>import pyjd # dummy in pyjs

from pyjamas.ui.TabBar import TabBar
from pyjamas.ui.TabPanel import TabPanel
from pyjamas.ui import HasAlignment
from pyjamas.ui.Image import Image
from pyjamas.ui.DockPanel import DockPanel
from pyjamas.ui import HasAlignment
from pyjamas.ui.VerticalPanel import VerticalPanel
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Composite import Composite
#from pyjamas.ui import DecoratorPanel
from pyjamas.ui import MouseListener
from pyjamas.ui import Event
from pyjamas import Window
from pyjamas.ui.DecoratorPanel import DecoratedTabPanel, DecoratorPanel
from pyjamas.ui.DecoratorPanel import DecoratorTitledPanel
from pyjamas.HTTPRequest import HTTPRequest
from PageLoader import PageListLoader, PageLoader
from pyjamas.ui.HTMLLinkPanel import HTMLLinkPanel
from pyjamas import History

#class PrettyTab(DecoratorPanel):
class PrettyTab(Composite):

    def __init__(self, text, imageUrl):

        DecoratorPanel.__init__(self, DecoratorPanel.DECORATE_ALL)

        p = HorizontalPanel()
        p.setSpacing(3)
        self.img = Image(imageUrl)
        self.txt = HTML(text)
        p.add(self.img)
        p.add(self.txt)

        self.add(p)

    def addClickListener(self, listener):

        self.img.addClickListener(listener)
        self.txt.addClickListener(listener)

class Tabs:

    def onModuleLoad(self):

        dock = DockPanel()
        self.header = HTML(Width="100%")
        self.footer = HTML(Width="100%")
        self.sidebar = HTML(Width="200px", Height="100%", StyleName="sidebar")
        self.fTabs = DecoratedTabPanel(Size=("100%", "100%"),
                                       StyleName="tabs")

        #dp = DecoratorTitledPanel("Tabs", "bluetitle", "bluetitleicon",
        #              ["bluetop", "bluetop2", "bluemiddle", "bluebottom"])
        #dp.add(self.fTabs)

        dock.add(self.header, DockPanel.NORTH)
        dock.add(self.footer, DockPanel.SOUTH)
        dock.add(self.sidebar, DockPanel.EAST)
        dock.add(self.fTabs, DockPanel.CENTER)
        dock.setCellVerticalAlignment(self.fTabs, HasAlignment.ALIGN_TOP)
        #dock.setCellHorizontalAlignment(self.fTabs, HasAlignment.ALIGN_CENTER)
        dock.setCellWidth(self.header, "100%")
        dock.setCellWidth(self.sidebar, "200px")

        RootPanel().add(dock)

        self.loadPageList()
        History.addHistoryListener(self)

    def createPage(self, title, purpose, text):

        if purpose == 'faq':
            self.faq_pages[title] = text
            if len(self.faq_pages) != len(self.faq_list):
                return
            faq = self.page_widgets['FAQ']
            for l in self.faq_list:
                question = l[0]
                answer = self.faq_pages[question]
                html = faq.getHTML()
                html += "
%s
\n" % question
                html += "\n%s\n\n" % answer
                faq.setHTML(html)
            html = "
\n%s
\n" % html
            faq.setHTML(html)
            faq.replaceLinks(use_page_href=False)
            return

        if title == 'header':
            self.header.setHTML(text)
            return
        elif title == 'footer':
            self.footer.setHTML(text)
            return
        elif title == 'sidebar':
            self.sidebar.setHTML(text)
            return

        self.pages[title] = text
        if len(self.pages) != len(self.page_list):
            return
        self.page_widgets = {}
        self.tab_index = {}
        for (idx, l) in enumerate(self.page_list):
            title = l[0]
            text = self.pages[title]
            self.tab_index[title] = idx
            widget = HTMLLinkPanel(text)
            self.fTabs.add(widget, title, True)
            self.page_widgets[title] = widget
            if title == 'FAQ':
                HTTPRequest().asyncGet("faq/questions.txt",
                                       PageListLoader(self, "faq"))
            else:
                widget.replaceLinks(use_page_href=False)
        self.fTabs.selectTab(0)

    def createImage(self, imageUrl):
        image = Image(imageUrl)
        image.setStyleName("ks-images-Image")

        p = VerticalPanel()
        p.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)
        p.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
        p.add(image)

        return p

    def onHistoryChanged(self, token):
        print "onHistoryChanged", token, self.pages.keys()
        if self.pages.has_key(token):
            idx = self.tab_index(token)
            self.fTabs.selectTab(idx)

    def loadPageList(self):
        HTTPRequest().asyncGet("sidebar.html",
                                PageLoader(self, "sidebar", "contents"))
        HTTPRequest().asyncGet("header.html",
                                PageLoader(self, "header", "contents"))
        HTTPRequest().asyncGet("footer.html",
                                PageLoader(self, "footer", "contents"))
        HTTPRequest().asyncGet("contents.txt",
                                PageListLoader(self, "contents"))

    def loadPages(self, pages, purpose):
        if purpose == 'contents':
            self.pages = {}
            self.page_list = pages
        elif purpose == 'faq':
            self.faq_pages = {}
            self.faq_list = pages

        for l in pages:
            title = l[0]
            desc = l[1]
            HTTPRequest().asyncGet(desc, PageLoader(self, title, purpose))

if __name__ == '__main__':
    pyjd.setup("./public/index.html")
    app = Tabs()
    app.onModuleLoad()
    pyjd.run()

<|endoftext|>


<commit_before>"""
Investigate data in various ways
"""

import sys
from itertools import cycle

import numpy as np

import matplotlib as mpl
import matplotlib.pylab as plt

from tqdm import tqdm, trange

from utils import extract_sig_entries, compute_correlation_matrix
from plotter import plot_histogram, plot_system, save_figure
from setup import generate_basic_system
from solver import solve_system
from nm_data_generator import add_node_to_system

def plot_correlation_hist(data):
    """ Plot histogram of all correlations
    """
    # gather data
    corrs = []
    for raw_res, enh_list in data:
        _, raw_mat, _ = raw_res

        if not raw_mat is None:
            raw_vec = extract_sig_entries(raw_mat)
            corrs.extend(raw_vec)

        for enh_res in enh_list:
            _, enh_mat, _ = enh_res

            if not enh_mat is None:
                enh_vec = extract_sig_entries(enh_mat)
                corrs.extend(enh_vec)

    # plot result
    fig = plt.figure()

    plot_histogram(corrs, plt.gca())
    plt.xlabel('simulated correlations')

    fig.savefig('images/all_sim_corrs.pdf')

def check_ergodicity(reps=500):
    """ Check whether simulated systems are ergodic
    """
    def get_matrices(syst, entry_num=100):
        """ Get correlation matrices for both cases
        """
        # multiple entries from single run
        single_run_matrices = []
        for _ in range(entry_num):
            sol = solve_system(syst)

            extract = sol.T[-entry_num:]
            single_run_mat = compute_correlation_matrix(np.array([extract]))

            single_run_matrices.append(single_run_mat)
        avg_single_mat = np.mean(single_run_matrices, axis=0)

        # one entry from multiple runs
        multiple_runs = []
        for _ in range(entry_num):
            sol = solve_system(syst)

            extract = sol.T[-1].T
            multiple_runs.append(extract)
        multiple_mat = compute_correlation_matrix(np.array([multiple_runs]))

        return avg_single_mat, multiple_mat

    syst = generate_basic_system()

    single_runs = []
    multiple_runs = []
    for _ in trange(reps):
        sm, rm = get_matrices(syst)

        single_runs.append(sm)
        multiple_runs.append(rm)
    single_runs = np.array(single_runs)
    multiple_runs = np.array(multiple_runs)

    # plot result
    dim = syst.jacobian.shape[1]

    plt.figure(figsize=(6, 14))
    gs = mpl.gridspec.GridSpec(int((dim**2-dim)/2), 1)

    axc = 0
    for i in range(dim):
        for j in range(dim):
            if i == j: break
            ax = plt.subplot(gs[axc])

            plot_histogram(
                single_runs[:,i,j], ax,
                alpha=0.5,
                label='Multiple entries from single run')
            plot_histogram(multiple_runs[:,i,j], ax,
                facecolor='mediumturquoise', alpha=0.5,
                label='One entry from multiple runs')

            ax.set_title('Nodes {}, {}'.format(i, j))
            ax.set_xlabel('correlation')
            ax.legend(loc='best')

            axc += 1

    plt.tight_layout()
    plt.savefig('images/ergodicity_check.pdf')

def single_corr_coeff_hist(reps=5000):
    """ Plot distribution of single correlation coefficients
    """
    def do(syst, ax):
        # data
        single_run_matrices = []
        for _ in trange(reps):
            sol = solve_system(syst)

            sol_extract = sol.T[int(len(sol.T)*3/4):]
            single_run_mat = compute_correlation_matrix(np.array([sol_extract]))

            if single_run_mat.shape == (4, 4):
                single_run_mat = single_run_mat[:-1,:-1]
            assert single_run_mat.shape == (3, 3)

            single_run_matrices.append(single_run_mat)
        single_run_matrices = np.asarray(single_run_matrices)

        # plotting
        cols = cycle(['b', 'r', 'g', 'c', 'm', 'y', 'k'])
        for i, row in enumerate(single_run_matrices.T):
            for j, series in enumerate(row):
                if i == j: break
                plot_histogram(
                    series[series!=1], ax,
                    label=r'$c_{{{},{}}}$'.format(i,j),
                    facecolor=next(cols), alpha=0.5,
                    bins=100)
        ax.legend(loc='best')

    # data
    syst = generate_basic_system()
    more = add_node_to_system(syst)[::10]
    print('#more', len(more))

    # plot
    f, axes = plt.subplots(1+len(more), 2, figsize=(9,20))

    for i, m in tqdm(enumerate([syst]+more), total=1+len(more)):
        plot_system(m, axes[i,0])
        do(m, axes[i,1])

    plt.tight_layout()
    save_figure('images/correlation_distribution.pdf', bbox_inches='tight')

def main(data):
    """ Analyse data
    """
    #if data is None:
    #    check_ergodicity()
    #else:
    #    plot_correlation_hist(data)
    single_corr_coeff_hist()

if __name__ == '__main__':
    main(np.load(sys.argv[1])['data'] if len(sys.argv) == 2 else None)
<commit_msg>Show correlation distributions for varying parameters<commit_after>"""
Investigate data in various ways
"""

import sys
from itertools import cycle

import numpy as np

import seaborn as sns
import matplotlib as mpl
import matplotlib.pylab as plt

from tqdm import tqdm, trange

from utils import extract_sig_entries, compute_correlation_matrix
from plotter import plot_histogram, plot_system, save_figure, plot_system_evolution
from setup import generate_basic_system
from main import analyze_system
from nm_data_generator import add_node_to_system
from filters import filter_steady_state

def plot_correlation_hist(data):
    """ Plot histogram of all correlations
    """
    # gather data
    corrs = []
    for raw_res, enh_list in data:
        _, raw_mat, _ = raw_res

        if not raw_mat is None:
            raw_vec = extract_sig_entries(raw_mat)
            corrs.extend(raw_vec)

        for enh_res in enh_list:
            _, enh_mat, _ = enh_res

            if not enh_mat is None:
                enh_vec = extract_sig_entries(enh_mat)
                corrs.extend(enh_vec)

    # plot result
    fig = plt.figure()

    plot_histogram(corrs, plt.gca())
    plt.xlabel('simulated correlations')

    fig.savefig('images/all_sim_corrs.pdf')

def check_ergodicity(reps=500):
    """ Check whether simulated systems are ergodic
    """
    def get_matrices(syst, entry_num=100):
        """ Get correlation matrices for both cases
        """
        # multiple entries from single run
        single_run_matrices = []
        for _ in range(entry_num):
            sol = solve_system(syst)

            extract = sol.T[-entry_num:]
            single_run_mat = compute_correlation_matrix(np.array([extract]))

            single_run_matrices.append(single_run_mat)
        avg_single_mat = np.mean(single_run_matrices, axis=0)

        # one entry from multiple runs
        multiple_runs = []
        for _ in range(entry_num):
            sol = solve_system(syst)

            extract = sol.T[-1].T
            multiple_runs.append(extract)
        multiple_mat = compute_correlation_matrix(np.array([multiple_runs]))

        return avg_single_mat, multiple_mat

    syst = generate_basic_system()

    single_runs = []
    multiple_runs = []
    for _ in trange(reps):
        sm, rm = get_matrices(syst)

        single_runs.append(sm)
        multiple_runs.append(rm)
    single_runs = np.array(single_runs)
    multiple_runs = np.array(multiple_runs)

    # plot result
    dim = syst.jacobian.shape[1]

    plt.figure(figsize=(6, 14))
    gs = mpl.gridspec.GridSpec(int((dim**2-dim)/2), 1)

    axc = 0
    for i in range(dim):
        for j in range(dim):
            if i == j: break
            ax = plt.subplot(gs[axc])

            plot_histogram(
                single_runs[:,i,j], ax,
                alpha=0.5,
                label='Multiple entries from single run')
            plot_histogram(multiple_runs[:,i,j], ax,
                facecolor='mediumturquoise', alpha=0.5,
                label='One entry from multiple runs')

            ax.set_title('Nodes {}, {}'.format(i, j))
            ax.set_xlabel('correlation')
            ax.legend(loc='best')

            axc += 1

    plt.tight_layout()
    plt.savefig('images/ergodicity_check.pdf')

def single_corr_coeff_hist(reps=200):
    """ Plot distribution of single correlation coefficients for various parameters
    """
    def do(gs, res):
        param_range = np.linspace(.1, 5, res)
        currow = 0
        for k_m in tqdm(param_range):
            for k_23 in tqdm(param_range):
                syst = generate_basic_system(k_m=k_m, k_23=k_23)

                single_run_matrices = []
                for r in trange(reps):
                    _,mat,sol = analyze_system(syst, repetition_num=1)
                    if mat is None:
                        continue

                    sol_extract = sol.T[int(len(sol.T)*3/4):]

                    if r == 0:
                        plot_system_evolution(
                            sol_extract.T,
                            plt.subplot(gs[currow,2]), show_legend=False)

                    single_run_mat = compute_correlation_matrix(np.array([sol_extract]))

                    if single_run_mat.shape == (4, 4):
                        single_run_mat = single_run_mat[:-1,:-1]
                    assert single_run_mat.shape == (3, 3)

                    single_run_matrices.append(single_run_mat)

                plot_system(syst, plt.subplot(gs[currow,0]))

                single_run_matrices = np.asarray(single_run_matrices)
                for i, row in enumerate(single_run_matrices.T):
                    for j, series in enumerate(row):
                        if i == j: break

                        ax = plt.subplot(gs[currow,1])
                        sns.distplot(series, ax=ax, label=r'$c_{{{},{}}}$'.format(i,j))
                        ax.set_xlim((-1,1))

                currow += 1

    # generate plots
    res = 3

    plt.figure(figsize=(20,30))
    gs = mpl.gridspec.GridSpec(res**2, 3, width_ratios=[1,1,2])

    sns.set_style('white')
    plt.style.use('seaborn-poster')

    do(gs, res)

    plt.tight_layout()
    save_figure('images/correlation_distribution.pdf', bbox_inches='tight')

def main(data):
    """ Analyse data
    """
    #if data is None:
    #    check_ergodicity()
    #else:
    #    plot_correlation_hist(data)
    single_corr_coeff_hist()

if __name__ == '__main__':
    main(np.load(sys.argv[1])['data'] if len(sys.argv) == 2 else None)
<|endoftext|>


<commit_before>import csv
import numpy as np
import datetime
import matplotlib.pyplot as plt

metacols = 3
color_scheme = ['#00B2A5', '#D9661F', '#00B0DA', '#FDB515', '#ED4E33',
                '#2D637F', '#9DAD33', '#53626F', '#EE1F60', '#6C3302',
                '#C2B9A7', '#CFDD45', '#003262']
color_map = {'k40': 8, 'bi214': 5, 'tl208': 7, 'cs137': 10, 'cs134': 9}
isotope_key = ['k40', 'bi214', 'tl208', 'cs137', 'cs134']
color_scheme = np.asarray([color_scheme[color_map[key]]
                           for key in isotope_key])

def parse_time(date):
    if ('-' in date):
        return ''
    date_list = date.split('/')
    return_list = []
    for item in date_list:
        return_list.append(int(item))
    return datetime.datetime(year=2000+return_list[2], month=return_list[0],
                             day=return_list[1])

def create_barerror_plot(csv_file, title, log=True):
    sample_list = []
    name_list = []
    date_list = []
    header = []
    with open(csv_file) as csvfile:
        parser = csv.reader(csvfile)
        header = parser.__next__()
        dictparser = csv.DictReader(csvfile, header)
        for row in dictparser:
            tmp_list = []
            name_list.append(row[header[0]])
            date_list.append(row[header[1]])
            for ind in range(metacols, 2 * len(isotope_key) + 2, 2):
                if float(row[header[ind]]) < float(row[header[ind+1]]):
                    tmp_list.extend([0, float(row[header[ind+1]])])
                else:
                    tmp_list.extend([float(row[header[ind]]),
                                     float(row[header[ind+1]])])
            sample_list.append(tmp_list)
    sample_list = np.asarray(sample_list)
    legend_key = []
    data = np.zeros((len(name_list), int(sample_list.shape[1] / 2.)))
    error = np.zeros((len(name_list), int(sample_list.shape[1] / 2.)))
    loop = 0
    for item in range(0, sample_list.shape[1], 2):
        legend_key.append(header[metacols + item])
        data[:, loop] = sample_list[:, item]
        error[:, loop] = sample_list[:, item + 1]
        loop += 1
    ax, fig = generate_barerror_logy(sample_names=name_list, data=data,
                                     error=error, legend_key=legend_key,
                                     title=title, log=log)

def generate_barerror_logy(sample_names, data, error, legend_key, title,
                           log=True):
    number_samples = len(sample_names)
    index = np.arange(0.5, number_samples, dtype=np.float64)
    width = float(0.15)
    fig, ax = plt.subplots()

    axis = []
    mins = np.amin(data[np.nonzero(data)])
    for sample in range(0, len(legend_key)):
        args = np.zeros((0))
        left_edge = index + float(width) * float(sample)
        if np.amin(data[:, sample]) == 0:
            args = np.where(data[:, sample] == 0)
            data[args, sample] += 1e-9
            draw_arrows(axes=ax, xlocs=(left_edge + 0.5 * float(width))[args],
                        ylocs=error[args, sample], color=color_scheme[sample])
        axis.append(ax.bar(left=left_edge, height=tuple(data[:, sample]),
                    width=width, color=color_scheme[sample],
                    yerr=tuple(error[:, sample]), ecolor=color_scheme[sample],
                    edgecolor="none", log=log))

    ylims = ax.get_ylim()
    upper_mult = 1
    if log:
        upper_mult = 10
    ax.set_ylim([mins / 10, upper_mult * ylims[1]])
    ax.set_xticks(index + float(len(legend_key)) / 2. * width)
    ax.set_xticklabels(sample_names)
    ax.tick_params(axis='x', color='w')
    ax.legend([a[0] for a in axis], legend_key, loc='upper left')
    ax.annotate('', xy=(0.88, 0.8999), xycoords='axes fraction',
                xytext=(0.88, 0.9), textcoords='axes fraction',
                arrowprops=dict(edgecolor='k', facecolor='k',
                arrowstyle='-|>'))
    ax.annotate('Detection Limit', xy=(0.888, 0.905), xytext=(0.888, 0.905),
                textcoords='axes fraction', ha='left', va='center')
    ax.set_title(title)
    ax.set_ylabel('Specific Activity' + legend_key[0].split(' ')[1])
    plt.gcf().subplots_adjust(bottom=0.15, left=0.05, right=0.95)
    plt.show()
    return ax, fig

def draw_arrows(axes, xlocs, ylocs, color):
    num_els = len(xlocs)
    if num_els == 0:
        return
    if len(ylocs.shape) > 1:
        ylocs = np.squeeze(ylocs, axis=(0,))
    for index in range(0, num_els):
        dy = 1e-10
        axes.annotate("", xy=(xlocs[index], ylocs[index] - dy),
                      xycoords='data', xytext=(xlocs[index], ylocs[index]),
                      textcoords='data', arrowprops=dict(edgecolor=color,
                      facecolor=color, arrowstyle="-|>"))
    return

create_barerror_plot('Sampling_Table.csv', 'Cyrax')
<commit_msg>Removed UCB number and recal from sample names.<commit_after>import csv
import numpy as np
import datetime
import matplotlib.pyplot as plt

metacols = 3
color_scheme = ['#00B2A5', '#D9661F', '#00B0DA', '#FDB515', '#ED4E33',
                '#2D637F', '#9DAD33', '#53626F', '#EE1F60', '#6C3302',
                '#C2B9A7', '#CFDD45', '#003262']
color_map = {'k40': 8, 'bi214': 5, 'tl208': 7, 'cs137': 10, 'cs134': 9}
isotope_key = ['k40', 'bi214', 'tl208', 'cs137', 'cs134']
color_scheme = np.asarray([color_scheme[color_map[key]]
                           for key in isotope_key])

def parse_time(date):
    if ('-' in date):
        return ''
    date_list = date.split('/')
    return_list = []
    for item in date_list:
        return_list.append(int(item))
    return datetime.datetime(year=2000+return_list[2], month=return_list[0],
                             day=return_list[1])

def create_barerror_plot(csv_file, title, log=True):
    sample_list = []
    name_list = []
    date_list = []
    header = []
    with open(csv_file) as csvfile:
        parser = csv.reader(csvfile)
        header = parser.__next__()
        dictparser = csv.DictReader(csvfile, header)
        for row in dictparser:
            tmp_list = []
            if 'recal' in row[header[0]]:
                name_list.append(row[header[0]][7:-5])
            else:
                name_list.append(row[header[0]][7:])
            date_list.append(row[header[1]])
            for ind in range(metacols, 2 * len(isotope_key) + 2, 2):
                if float(row[header[ind]]) < float(row[header[ind+1]]):
                    tmp_list.extend([0, float(row[header[ind+1]])])
                else:
                    tmp_list.extend([float(row[header[ind]]),
                                     float(row[header[ind+1]])])
            sample_list.append(tmp_list)
    sample_list = np.asarray(sample_list)
    legend_key = []
    data = np.zeros((len(name_list), int(sample_list.shape[1] / 2.)))
    error = np.zeros((len(name_list), int(sample_list.shape[1] / 2.)))
    loop = 0
    for item in range(0, sample_list.shape[1], 2):
        legend_key.append(header[metacols + item])
        data[:, loop] = sample_list[:, item]
        error[:, loop] = sample_list[:, item + 1]
        loop += 1
    ax, fig = generate_barerror_logy(sample_names=name_list, data=data,
                                     error=error, legend_key=legend_key,
                                     title=title, log=log)

def generate_barerror_logy(sample_names, data, error, legend_key, title,
                           log=True):
    number_samples = len(sample_names)
    index = np.arange(0.5, number_samples, dtype=np.float64)
    width = float(0.15)
    fig, ax = plt.subplots()

    axis = []
    mins = np.amin(data[np.nonzero(data)])
    for sample in range(0, len(legend_key)):
        args = np.zeros((0))
        left_edge = index + float(width) * float(sample)
        if np.amin(data[:, sample]) == 0:
            args = np.where(data[:, sample] == 0)
            data[args, sample] += 1e-9
            draw_arrows(axes=ax, xlocs=(left_edge + 0.5 * float(width))[args],
                        ylocs=error[args, sample], color=color_scheme[sample])
        axis.append(ax.bar(left=left_edge, height=tuple(data[:, sample]),
                    width=width, color=color_scheme[sample],
                    yerr=tuple(error[:, sample]), ecolor=color_scheme[sample],
                    edgecolor="none", log=log))

    ylims = ax.get_ylim()
    upper_mult = 1
    if log:
        upper_mult = 10
    ax.set_ylim([mins / 10, upper_mult * ylims[1]])
    ax.set_xticks(index + float(len(legend_key)) / 2. * width)
    ax.set_xticklabels(sample_names)
    ax.tick_params(axis='x', color='w')
    ax.legend([a[0] for a in axis], legend_key, loc='upper left')
    ax.annotate('', xy=(0.88, 0.8999), xycoords='axes fraction',
                xytext=(0.88, 0.9), textcoords='axes fraction',
                arrowprops=dict(edgecolor='k', facecolor='k',
                arrowstyle='-|>'))
    ax.annotate('Detection Limit', xy=(0.888, 0.905), xytext=(0.888, 0.905),
                textcoords='axes fraction', ha='left', va='center')
    ax.set_title(title)
    ax.set_ylabel('Specific Activity' + legend_key[0].split(' ')[1])
    plt.gcf().subplots_adjust(bottom=0.15, left=0.05, right=0.95)
    plt.show()
    return ax, fig

def draw_arrows(axes, xlocs, ylocs, color):
    num_els = len(xlocs)
    if num_els == 0:
        return
    if len(ylocs.shape) > 1:
        ylocs = np.squeeze(ylocs, axis=(0,))
    for index in range(0, num_els):
        dy = 1e-10
        axes.annotate("", xy=(xlocs[index], ylocs[index] - dy),
                      xycoords='data', xytext=(xlocs[index], ylocs[index]),
                      textcoords='data', arrowprops=dict(edgecolor=color,
                      facecolor=color, arrowstyle="-|>"))
    return

create_barerror_plot('Sampling_Table.csv', 'Cyrax')
<|endoftext|>


<commit_before>from yamltools import resolver
from os import listdir
from os.path import isfile, join
import sys
import logging
import os
import glob
import yaml
import re
import json
from collections import OrderedDict

logger = logging.getLogger(__name__)

def _read_file_safe(file_path):
    try:
        return open(file_path).read()
    except OSError as e:
        logger.warn("Couldn't open file at path: %s" % file_path)
        logger.warn("This might be okay if you didn't expect that profile to exist")
    return None

__settings_cache = None

def settings_cache(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local"):
    if __settings_cache is None:
        __settings_cache = settings(spinnaker_config_dir, spring_profiles_active)
    return __settings_cache

def settings(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local"):
    return named_settings(spinnaker_config_dir, spring_profiles_active)

def named_settings(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local", config_name="spinnaker"):
    # make some assumptions about the environment
    spring_profiles_active = os.environ.get("SPRING_PROFILES_ACTIVE", spring_profiles_active)

    # order them the right way so we overwrite properties properly
    spring_profiles = reversed(spring_profiles_active.split(","))

    # remove extra spaces if any
    profiles_clean = map(lambda p: p.strip(), spring_profiles)
    profile_suffixes = list(map(lambda ps: "-%s" % ps, profiles_clean))

    # we need to add the default profiles, i.e spinnaker.yml
    profile_suffixes.append("")
    active_yaml_filenames = map(lambda p: "%s/%s%s.yml" % (spinnaker_config_dir, config_name, p), profile_suffixes)

    yaml_content = map(_read_file_safe, active_yaml_filenames)
    nonempty_yamls = filter(lambda f: f is not None, yaml_content)
    loaded_yaml = map(yaml.load, nonempty_yamls)
    resolved_settings = resolver.resolve_yamls(list(loaded_yaml))
    return resolved_settings

def render_deck_settings(deck_settings_txt, spkr_settings):
    keys_to_resolve = re.findall("\$\{(.*?)\}", deck_settings_txt)
    rendered_settings = deck_settings_txt
    for key in keys_to_resolve:
        value = spkr_settings.get(key, '')
        value_is_string = isinstance(value, str)

        if value == False or (value_is_string and value.lower() == 'false'):
            print("yml false for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, "false")
        elif value == True or (value_is_string and value.lower() == 'true'):
            print("yml true for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, "true")
        elif value is None or value == '':
            print("yml empty string for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, '')
        else:
            print("yml value for:", "${%s}" % key, str(value))
            rendered_settings = rendered_settings.replace("${%s}" % key, str(value))

    return rendered_settings

def deck_configure():
    deck_dir = os.environ.get("DECK_OPT_DIR", "/opt/deck/html")
    spinnaker_config_dir = os.environ.get("SPINNAKER_CONFIG_DIR", "/opt/spinnaker/config")

    settings_template_path = "%s/settings-template.js" % deck_dir

    logger.info("Path to settings.js template: %s" % settings_template_path)

    deck_settings_content = open(settings_template_path).read()
    spkr_settings = settings(spinnaker_config_dir)
    rendered_settings = render_deck_settings(deck_settings_content, spkr_settings)

    settings_js_rendered = "%s/settings.js" % deck_dir
    logger.info("Writing rendered settings.js to: %s" % settings_js_rendered)

    settings_js_file = open(settings_js_rendered, "w+")
    settings_js_file.write(rendered_settings)
    logger.warn("Completed rendering of deck settings")
<commit_msg>Despite having utf-8 as the default encoding, sometimes these functions try to read ascii instead and choke on valid javascript. Just always force it to explicit utf-8 encoding.<commit_after>from yamltools import resolver
from os import listdir
from os.path import isfile, join
import sys
import logging
import os
import glob
import yaml
import re
import json
from collections import OrderedDict

logger = logging.getLogger(__name__)

def _read_file_safe(file_path):
    try:
        return open(file_path).read()
    except OSError as e:
        logger.warn("Couldn't open file at path: %s" % file_path)
        logger.warn("This might be okay if you didn't expect that profile to exist")
    return None

__settings_cache = None

def settings_cache(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local"):
    if __settings_cache is None:
        __settings_cache = settings(spinnaker_config_dir, spring_profiles_active)
    return __settings_cache

def settings(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local"):
    return named_settings(spinnaker_config_dir, spring_profiles_active)

def named_settings(spinnaker_config_dir="/opt/spinnaker/config", spring_profiles_active="local", config_name="spinnaker"):
    # make some assumptions about the environment
    spring_profiles_active = os.environ.get("SPRING_PROFILES_ACTIVE", spring_profiles_active)

    # order them the right way so we overwrite properties properly
    spring_profiles = reversed(spring_profiles_active.split(","))

    # remove extra spaces if any
    profiles_clean = map(lambda p: p.strip(), spring_profiles)
    profile_suffixes = list(map(lambda ps: "-%s" % ps, profiles_clean))

    # we need to add the default profiles, i.e spinnaker.yml
    profile_suffixes.append("")
    active_yaml_filenames = map(lambda p: "%s/%s%s.yml" % (spinnaker_config_dir, config_name, p), profile_suffixes)

    yaml_content = map(_read_file_safe, active_yaml_filenames)
    nonempty_yamls = filter(lambda f: f is not None, yaml_content)
    loaded_yaml = map(yaml.load, nonempty_yamls)
    resolved_settings = resolver.resolve_yamls(list(loaded_yaml))
    return resolved_settings

def render_deck_settings(deck_settings_txt, spkr_settings):
    keys_to_resolve = re.findall("\$\{(.*?)\}", deck_settings_txt)
    rendered_settings = deck_settings_txt
    for key in keys_to_resolve:
        value = spkr_settings.get(key, '')
        value_is_string = isinstance(value, str)

        if value == False or (value_is_string and value.lower() == 'false'):
            print("yml false for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, "false")
        elif value == True or (value_is_string and value.lower() == 'true'):
            print("yml true for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, "true")
        elif value is None or value == '':
            print("yml empty string for:", "${%s}" % key)
            rendered_settings = rendered_settings.replace("${%s}" % key, '')
        else:
            print("yml value for:", "${%s}" % key, str(value))
            rendered_settings = rendered_settings.replace("${%s}" % key, str(value))

    return rendered_settings

def deck_configure():
    deck_dir = os.environ.get("DECK_OPT_DIR", "/opt/deck/html")
    spinnaker_config_dir = os.environ.get("SPINNAKER_CONFIG_DIR", "/opt/spinnaker/config")

    settings_template_path = "%s/settings-template.js" % deck_dir

    logger.info("Path to settings.js template: %s" % settings_template_path)

    deck_settings_content = open(settings_template_path, encoding="utf-8").read()
    spkr_settings = settings(spinnaker_config_dir)
    rendered_settings = render_deck_settings(deck_settings_content, spkr_settings)

    settings_js_rendered = "%s/settings.js" % deck_dir
    logger.info("Writing rendered settings.js to: %s" % settings_js_rendered)

    settings_js_file = open(settings_js_rendered, "w+", encoding="utf-8")
    settings_js_file.write(rendered_settings)
    logger.warn("Completed rendering of deck settings")
<|endoftext|>


<commit_before>import mistune
import schema
import json
import yaml
import os
import copy
import re
import sys
import StringIO
import logging
import urlparse
from aslist import aslist
import re
import argparse

_logger = logging.getLogger("salad")

def has_types(items):
    r = []
    if isinstance(items, dict):
        if items["type"] == "https://w3id.org/cwl/salad#record":
            return [items["name"]]
        for n in ("type", "items", "values"):
            if n in items:
                r.extend(has_types(items[n]))
        return r
    if isinstance(items, list):
        for i in items:
            r.extend(has_types(i))
        return r
    if isinstance(items, basestring):
        return [items]
    return []

def linkto(item):
    _, frg = urlparse.urldefrag(item)
    return "[%s](#%s)" % (frg, to_id(frg))

class MyRenderer(mistune.Renderer):
    def header(self, text, level, raw=None):
        return """<h%i id="%s">%s
""" % (level, to_id(text), text)

def to_id(text):
    textid = text
    if text[0] in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"):
        try:
            textid = text[text.index(" ")+1:]
        except ValueError:
            pass
    textid = textid.replace(" ", "_")
    return textid

class ToC(object):
    def __init__(self):
        self.first_toc_entry = True
        self.numbering = [0]
        self.toc = ""
        self.start_numbering = True

    def add_entry(self, thisdepth, title):
        depth = len(self.numbering)
        if thisdepth < depth:
            self.toc += "
"
            for n in range(0, depth-thisdepth):
                self.numbering.pop()
                self.toc += "

"
            self.numbering[-1] += 1
        elif thisdepth == depth:
            if not self.first_toc_entry:
                self.toc += "
"
            else:
                self.first_toc_entry = False
            self.numbering[-1] += 1
        elif thisdepth > depth:
            self.numbering.append(1)

        if self.start_numbering:
            num = "%i.%s" % (self.numbering[0], ".".join([str(n) for n in self.numbering[1:]]))
        else:
            num = ""
        self.toc += """
%s %s
\n""" %(to_id(title),
            num, title)
        return num

    def contents(self, id):
        c = """
Table of contents

%s""" % (id, self.toc)
        c += "
"
        for i in range(0, len(self.numbering)):
            c += "

"
        c += """"""
        return c

basicTypes = ("https://w3id.org/cwl/salad#null",
              "http://www.w3.org/2001/XMLSchema#boolean",
              "http://www.w3.org/2001/XMLSchema#int",
              "http://www.w3.org/2001/XMLSchema#long",
              "http://www.w3.org/2001/XMLSchema#float",
              "http://www.w3.org/2001/XMLSchema#double",
              "http://www.w3.org/2001/XMLSchema#string",
              "https://w3id.org/cwl/salad#record",
              "https://w3id.org/cwl/salad#enum",
              "https://w3id.org/cwl/salad#array")

def typefmt(tp, redirects, nbsp=False):
    if isinstance(tp, list):
        if nbsp and len(tp) <= 3:
            return " | ".join([typefmt(n, redirects) for n in tp])
        else:
            return " | ".join([typefmt(n, redirects) for n in tp])
    if isinstance(tp, dict):
        if tp["type"] == "https://w3id.org/cwl/salad#array":
            return "array<%s>" % (typefmt(tp["items"], redirects, nbsp=True))
        if tp["type"] in ("https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum"):
            frg = schema.avro_name(tp["name"])
            if tp["name"] in redirects:
                return """%s""" % (redirects[tp["name"]], frg)
            else:
                return """%s""" % (to_id(frg), frg)
        if isinstance(tp["type"], dict):
            return typefmt(tp["type"], redirects)
    else:
        if str(tp) in redirects:
            return """%s""" % (redirects[tp], redirects[tp])
        elif str(tp) in basicTypes:
            return """%s""" % schema.avro_name(str(tp))
        else:
            _, frg = urlparse.urldefrag(tp)
            if frg:
                tp = frg
            return """%s""" % (to_id(tp), tp)

def add_dictlist(di, key, val):
    if key not in di:
        di[key] = []
    di[key].append(val)

def number_headings(toc, maindoc):
    mdlines = []
    skip = False
    for line in maindoc.splitlines():
        if line.strip() == "# Introduction":
            toc.start_numbering = True
            toc.numbering = [0]

        if line == "```":
            skip = not skip

        if not skip:
            m = re.match(r'^(#+) (.*)', line)
            if m:
                num = toc.add_entry(len(m.group(1)), m.group(2))
                line = "%s %s %s" % (m.group(1), num, m.group(2))
            line = re.sub(r'^(https?://\S+)', r'[\1](\1)', line)
        mdlines.append(line)

    maindoc = '\n'.join(mdlines)
    return maindoc

def fix_doc(doc):
    if isinstance(doc, list):
        doc = "".join(doc)
    return "\n".join([re.sub(r"<([^>@]+@[^>]+)>", r"[\1](mailto:\1)", d) for d in doc.splitlines()])

class RenderType(object):
    def __init__(self, toc, j, renderlist, redirects):
        self.typedoc = StringIO.StringIO()
        self.toc = toc
        self.subs = {}
        self.docParent = {}
        self.docAfter = {}
        self.rendered = set()
        self.redirects = redirects
        self.title = None

        for t in j:
            if "extends" in t:
                for e in aslist(t["extends"]):
                    add_dictlist(self.subs, e, t["name"])
                    #if "docParent" not in t and "docAfter" not in t:
                    #    add_dictlist(self.docParent, e, t["name"])

            if t.get("docParent"):
                add_dictlist(self.docParent, t["docParent"], t["name"])

            if t.get("docChild"):
                for c in aslist(t["docChild"]):
                    add_dictlist(self.docParent, t["name"], c)

            if t.get("docAfter"):
                add_dictlist(self.docAfter, t["docAfter"], t["name"])

        _, _, metaschema_loader = schema.get_metaschema()
        alltypes = schema.extend_and_specialize(j, metaschema_loader)

        self.typemap = {}
        self.uses = {}
        self.record_refs = {}
        for t in alltypes:
            self.typemap[t["name"]] = t
            try:
                if t["type"] == "record":
                    self.record_refs[t["name"]] = []
                    for f in t.get("fields", []):
                        p = has_types(f)
                        for tp in p:
                            if tp not in self.uses:
                                self.uses[tp] = []
                            if (t["name"], f["name"]) not in self.uses[tp]:
                                _, frg1 = urlparse.urldefrag(t["name"])
                                _, frg2 = urlparse.urldefrag(f["name"])
                                self.uses[tp].append((frg1, frg2))
                            if tp not in basicTypes and tp not in self.record_refs[t["name"]]:
                                    self.record_refs[t["name"]].append(tp)
            except KeyError as e:
                _logger.error("Did not find 'type' in %s", t)
                raise

        for f in alltypes:
            if (f["name"] in renderlist or
                ((not renderlist) and
                 ("extends" not in f) and
                 ("docParent" not in f) and
                 ("docAfter" not in f))):
                self.render_type(f, 1)

    def render_type(self, f, depth):
        if f["name"] in self.rendered or f["name"] in self.redirects:
            return
        self.rendered.add(f["name"])

        if "doc" not in f:
            f["doc"] = ""

        f["type"] = copy.deepcopy(f)
        f["doc"] = ""
        f = f["type"]

        if "doc" not in f:
            f["doc"] = ""

        f["doc"] = fix_doc(f["doc"])

        if f["type"] == "record":
            for field in f.get("fields", []):
                if "doc" not in field:
                    field["doc"] = ""

        if f["type"] != "documentation":
            lines = []
            for l in f["doc"].splitlines():
                if len(l) > 0 and l[0] == "#":
                    l = ("#" * depth) + l
                lines.append(l)
            f["doc"] = "\n".join(lines)

            _, frg = urlparse.urldefrag(f["name"])
            num = self.toc.add_entry(depth, frg)
            doc = "## %s %s\n" % (num, frg)
        else:
            doc = ""

        if self.title is None:
            self.title = f["doc"][0:f["doc"].index("\n")][2:]

        if f["type"] == "documentation":
            f["doc"] = number_headings(self.toc, f["doc"])

        #if "extends" in f:
        #    doc += "\n\nExtends "
        #    doc += ", ".join([" %s" % linkto(ex) for ex in aslist(f["extends"])])
        #if f["name"] in self.subs:
        #    doc += "\n\nExtended by"
        #    doc += ", ".join([" %s" % linkto(s) for s in self.subs[f["name"]]])
        #if f["name"] in self.uses:
        #    doc += "\n\nReferenced by"
        #    doc += ", ".join([" [%s.%s](#%s)" % (s[0], s[1], to_id(s[0])) for s in self.uses[f["name"]]])

        doc = doc + "\n\n" + f["doc"]

        doc = mistune.markdown(doc, renderer=MyRenderer())

        if f["type"] == "record":
            doc += "<h3>Fields</h3>"
            doc += """<table class="table table-striped">"""
            doc += "<tr><th>field</th><th>type</th><th>required</th><th>description</th></tr>"
            for i in f.get("fields", []):
                doc += "<tr>"
                tp = i["type"]
                if isinstance(tp, list) and tp[0] == "null":
                    opt = False
                    tp = tp[1:]
                else:
                    opt = True

                desc = i["doc"]
                #if "inherited_from" in i:
                #    desc = "%s _Inherited from %s_" % (desc, linkto(i["inherited_from"]))

                frg = schema.avro_name(i["name"])
                doc += "<td><code>%s</code></td><td>%s</td><td>%s</td><td>%s</td>" % (frg, typefmt(tp, self.redirects), opt, mistune.markdown(desc))
                doc += "</tr>"
            doc += """</table>"""
        f["doc"] = doc

        self.typedoc.write(f["doc"])

        subs = self.docParent.get(f["name"], []) + self.record_refs.get(f["name"], [])
        if len(subs) == 1:
            self.render_type(self.typemap[subs[0]], depth)
        else:
            for s in subs:
                self.render_type(self.typemap[s], depth+1)

        for s in self.docAfter.get(f["name"], []):
            self.render_type(self.typemap[s], depth)

def avrold_doc(j, outdoc, renderlist, redirects, brand, brandlink):
    toc = ToC()
    toc.start_numbering = False

    rt = RenderType(toc, j, renderlist, redirects)
    content = rt.typedoc.getvalue()

    outdoc.write("""
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    """)

    outdoc.write("<title>%s</title>" % (rt.title))

    outdoc.write("""
    <style>
    :target {
      padding-top: 61px;
      margin-top: -61px;
    }
    body {
      padding-top: 61px;
    }
    .tocnav ol {
      list-style: none
    }
    </style>
    </head>
    <body>
    """)

    outdoc.write("""
      <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
          <div class="navbar-header">
            <a class="navbar-brand" href="%s">%s</a>
    """ % (brandlink, brand))

    if u"<!--ToC-->" in content:
        content = content.replace(u"<!--ToC-->", toc.contents("toc"))
        outdoc.write("""
                <ul class="nav navbar-nav">
                  <li><a href="#toc">Table of contents</a></li>
                </ul>
        """)

    outdoc.write("""
          </div>
        </div>
      </nav>
    """)

    outdoc.write("""
    <div class="container">
    """)

    outdoc.write("""
    <div class="row">
    """)

    outdoc.write("""
    <div class="col-md-12" role="main" id="main">""")

    outdoc.write(content.encode("utf-8"))

    outdoc.write("""</div>""")

    outdoc.write("""
    </div>
    </div>
    </body>
    </html>""")

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("schema")
    parser.add_argument('--only', action='append')
    parser.add_argument('--redirect', action='append')
    parser.add_argument('--brand')
    parser.add_argument('--brandlink')

    args = parser.parse_args()

    s = []
    a = args.schema
    with open(a) as f:
        if a.endswith("md"):
            s.append({"name": os.path.splitext(os.path.basename(a))[0],
                  "type": "documentation",
                  "doc": f.read().decode("utf-8")
              })
        else:
            uri = "file://" + os.path.abspath(a)
            _, _, metaschema_loader = schema.get_metaschema()
            j, schema_metadata = metaschema_loader.resolve_ref(uri, "")
            if isinstance(j, list):
                s.extend(j)
            else:
                s.append(j)

    redirect = {r.split("=")[0]:r.split("=")[1] for r in args.redirect} if args.redirect else {}
    renderlist = args.only if args.only else []
    avrold_doc(s, sys.stdout, renderlist, redirect, args.brand, args.brandlink)
<commit_msg>Better rendering for enums.<commit_after>import mistune
import schema
import json
import yaml
import os
import copy
import re
import sys
import StringIO
import logging
import urlparse
from aslist import aslist
import re
import argparse

_logger = logging.getLogger("salad")

def has_types(items):
    r = []
    if isinstance(items, dict):
        if items["type"] == "https://w3id.org/cwl/salad#record":
            return [items["name"]]
        for n in ("type", "items", "values"):
            if n in items:
                r.extend(has_types(items[n]))
        return r
    if isinstance(items, list):
        for i in items:
            r.extend(has_types(i))
        return r
    if isinstance(items, basestring):
        return [items]
    return []

def linkto(item):
    _, frg = urlparse.urldefrag(item)
    return "[%s](#%s)" % (frg, to_id(frg))

class MyRenderer(mistune.Renderer):
    def header(self, text, level, raw=None):
        return """<h%i id="%s">%s</h1>""" % (level, to_id(text), text)

def to_id(text):
    textid = text
    if text[0] in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"):
        try:
            textid = text[text.index(" ")+1:]
        except ValueError:
            pass
    textid = textid.replace(" ", "_")
    return textid

class ToC(object):
    def __init__(self):
        self.first_toc_entry = True
        self.numbering = [0]
        self.toc = ""
        self.start_numbering = True

    def add_entry(self, thisdepth, title):
        depth = len(self.numbering)
        if thisdepth < depth:
            self.toc += "</ol>"
            for n in range(0, depth-thisdepth):
                self.numbering.pop()
                self.toc += "</li></ol>"
            self.numbering[-1] += 1
        elif thisdepth == depth:
            if not self.first_toc_entry:
                self.toc += "</ol>"
            else:
                self.first_toc_entry = False
            self.numbering[-1] += 1
        elif thisdepth > depth:
            self.numbering.append(1)

        if self.start_numbering:
            num = "%i.%s" % (self.numbering[0], ".".join([str(n) for n in self.numbering[1:]]))
        else:
            num = ""
        self.toc += """<li><a href="#%s">%s %s</a><ol>\n""" %(to_id(title),
            num, title)
        return num

    def contents(self, id):
        c = """<h1 id="%s">Table of contents</h1>
               <nav class="tocnav"><ol>%s""" % (id, self.toc)
        c += "</ol>"
        for i in range(0, len(self.numbering)):
            c += "</li></ol>"
        c += """</nav>"""
        return c

basicTypes = ("https://w3id.org/cwl/salad#null",
              "http://www.w3.org/2001/XMLSchema#boolean",
              "http://www.w3.org/2001/XMLSchema#int",
              "http://www.w3.org/2001/XMLSchema#long",
              "http://www.w3.org/2001/XMLSchema#float",
              "http://www.w3.org/2001/XMLSchema#double",
              "http://www.w3.org/2001/XMLSchema#string",
              "https://w3id.org/cwl/salad#record",
              "https://w3id.org/cwl/salad#enum",
              "https://w3id.org/cwl/salad#array")

def typefmt(tp, redirects, nbsp=False):
    if isinstance(tp, list):
        if nbsp and len(tp) <= 3:
            return " | ".join([typefmt(n, redirects) for n in tp])
        else:
            return " | ".join([typefmt(n, redirects) for n in tp])
    if isinstance(tp, dict):
        if tp["type"] == "https://w3id.org/cwl/salad#array":
            return "array<%s>" % (typefmt(tp["items"], redirects, nbsp=True))
        if tp["type"] in ("https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum"):
            frg = schema.avro_name(tp["name"])
            if tp["name"] in redirects:
                return """<a href="%s">%s</a>""" % (redirects[tp["name"]], frg)
            else:
                return """<a href="#%s">%s</a>""" % (to_id(frg), frg)
        if isinstance(tp["type"], dict):
            return typefmt(tp["type"], redirects)
    else:
        if str(tp) in redirects:
            return """<a href="%s">%s</a>""" % (redirects[tp], redirects[tp])
        elif str(tp) in basicTypes:
            return """<a href="#CWLType">%s</a>""" % schema.avro_name(str(tp))
        else:
            _, frg = urlparse.urldefrag(tp)
            if frg:
                tp = frg
            return """<a href="#%s">%s</a>""" % (to_id(tp), tp)

def add_dictlist(di, key, val):
    if key not in di:
        di[key] = []
    di[key].append(val)

def number_headings(toc, maindoc):
    mdlines = []
    skip = False
    for line in maindoc.splitlines():
        if line.strip() == "# Introduction":
            toc.start_numbering = True
            toc.numbering = [0]

        if line == "```":
            skip = not skip

        if not skip:
            m = re.match(r'^(#+) (.*)', line)
            if m:
                num = toc.add_entry(len(m.group(1)), m.group(2))
                line = "%s %s %s" % (m.group(1), num, m.group(2))
            line = re.sub(r'^(https?://\S+)', r'[\1](\1)', line)
        mdlines.append(line)

    maindoc = '\n'.join(mdlines)
    return maindoc

def fix_doc(doc):
    if isinstance(doc, list):
        doc = "".join(doc)
    return "\n".join([re.sub(r"<([^>@]+@[^>]+)>", r"[\1](mailto:\1)", d) for d in doc.splitlines()])

class RenderType(object):
    def __init__(self, toc, j, renderlist, redirects):
        self.typedoc = StringIO.StringIO()
        self.toc = toc
        self.subs = {}
        self.docParent = {}
        self.docAfter = {}
        self.rendered = set()
        self.redirects = redirects
        self.title = None

        for t in j:
            if "extends" in t:
                for e in aslist(t["extends"]):
                    add_dictlist(self.subs, e, t["name"])
                    #if "docParent" not in t and "docAfter" not in t:
                    #    add_dictlist(self.docParent, e, t["name"])

            if t.get("docParent"):
                add_dictlist(self.docParent, t["docParent"], t["name"])

            if t.get("docChild"):
                for c in aslist(t["docChild"]):
                    add_dictlist(self.docParent, t["name"], c)

            if t.get("docAfter"):
                add_dictlist(self.docAfter, t["docAfter"], t["name"])

        _, _, metaschema_loader = schema.get_metaschema()
        alltypes = schema.extend_and_specialize(j, metaschema_loader)

        self.typemap = {}
        self.uses = {}
        self.record_refs = {}
        for t in alltypes:
            self.typemap[t["name"]] = t
            try:
                if t["type"] == "record":
                    self.record_refs[t["name"]] = []
                    for f in t.get("fields", []):
                        p = has_types(f)
                        for tp in p:
                            if tp not in self.uses:
                                self.uses[tp] = []
                            if (t["name"], f["name"]) not in self.uses[tp]:
                                _, frg1 = urlparse.urldefrag(t["name"])
                                _, frg2 = urlparse.urldefrag(f["name"])
                                self.uses[tp].append((frg1, frg2))
                            if tp not in basicTypes and tp not in self.record_refs[t["name"]]:
                                    self.record_refs[t["name"]].append(tp)
            except KeyError as e:
                _logger.error("Did not find 'type' in %s", t)
                raise

        for f in alltypes:
            if (f["name"] in renderlist or
                ((not renderlist) and
                 ("extends" not in f) and
                 ("docParent" not in f) and
                 ("docAfter" not in f))):
                self.render_type(f, 1)

    def render_type(self, f, depth):
        if f["name"] in self.rendered or f["name"] in self.redirects:
            return
        self.rendered.add(f["name"])

        if "doc" not in f:
            f["doc"] = ""

        f["type"] = copy.deepcopy(f)
        f["doc"] = ""
        f = f["type"]

        if "doc" not in f:
            f["doc"] = ""

        enumDesc = {}
        if f["type"] == "enum" and isinstance(f["doc"], list):
            for i in f["doc"]:
                idx = i.find(":")
                if idx > -1:
                    enumDesc[i[:idx]] = i[idx+1:]
            f["doc"] = [i for i in f["doc"] if i.find(":") == -1 or i.find(" ")  0 and l[0] == "#":
                    l = ("#" * depth) + l
                lines.append(l)
            f["doc"] = "\n".join(lines)

            _, frg = urlparse.urldefrag(f["name"])
            num = self.toc.add_entry(depth, frg)
            doc = "## %s %s\n" % (num, frg)
        else:
            doc = ""

        if self.title is None:
            self.title = f["doc"][0:f["doc"].index("\n")][2:]

        if f["type"] == "documentation":
            f["doc"] = number_headings(self.toc, f["doc"])

        #if "extends" in f:
        #    doc += "\n\nExtends "
        #    doc += ", ".join([" %s" % linkto(ex) for ex in aslist(f["extends"])])
        #if f["name"] in self.subs:
        #    doc += "\n\nExtended by"
        #    doc += ", ".join([" %s" % linkto(s) for s in self.subs[f["name"]]])
        #if f["name"] in self.uses:
        #    doc += "\n\nReferenced by"
        #    doc += ", ".join([" [%s.%s](#%s)" % (s[0], s[1], to_id(s[0])) for s in self.uses[f["name"]]])

        doc = doc + "\n\n" + f["doc"]

        doc = mistune.markdown(doc, renderer=MyRenderer())

        if f["type"] == "record":
            doc += "
Fields
"
            doc += """
"""
            doc += "
field	type	required	description
"
            for i in f.get("fields", []):
                doc += "
"
                tp = i["type"]
                if isinstance(tp, list) and tp[0] == "null":
                    opt = False
                    tp = tp[1:]
                else:
                    opt = True

                desc = i["doc"]
                #if "inherited_from" in i:
                #    desc = "%s _Inherited from %s_" % (desc, linkto(i["inherited_from"]))

                frg = schema.avro_name(i["name"])
                doc += "%s	%s	%s	%s	" % (frg, typefmt(tp, self.redirects), opt, mistune.markdown(desc))
                doc += "
"
            doc += """
"""
        elif f["type"] == "enum":
            doc += "
Symbols
"
            doc += """
"""
            doc += "
symbol	description
"
            for i in f.get("symbols", []):
                doc += "
"
                frg = schema.avro_name(i)
                doc += "%s	%s	" % (frg, enumDesc.get(frg, ""))
                doc += "
"
            doc += """
"""
        f["doc"] = doc

        self.typedoc.write(f["doc"])

        subs = self.docParent.get(f["name"], []) + self.record_refs.get(f["name"], [])
        if len(subs) == 1:
            self.render_type(self.typemap[subs[0]], depth)
        else:
            for s in subs:
                self.render_type(self.typemap[s], depth+1)

        for s in self.docAfter.get(f["name"], []):
            self.render_type(self.typemap[s], depth)

def avrold_doc(j, outdoc, renderlist, redirects, brand, brandlink):
    toc = ToC()
    toc.start_numbering = False

    rt = RenderType(toc, j, renderlist, redirects)
    content = rt.typedoc.getvalue()

    outdoc.write("""
    <!DOCTYPE html>

    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    """)

    outdoc.write("<title>%s</title>" % (rt.title))

    outdoc.write("""

    """)

    outdoc.write("""

            %s
    """ % (brandlink, brand))

    if u"" in content:
        content = content.replace(u"", toc.contents("toc"))
        outdoc.write("""

Table of contents

        """)

    outdoc.write("""

    """)

    outdoc.write("""

    """)

    outdoc.write("""

    """)

    outdoc.write("""

""")

    outdoc.write(content.encode("utf-8"))

    outdoc.write("""
""")

    outdoc.write("""

    """)

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("schema")
    parser.add_argument('--only', action='append')
    parser.add_argument('--redirect', action='append')
    parser.add_argument('--brand')
    parser.add_argument('--brandlink')

    args = parser.parse_args()

    s = []
    a = args.schema
    with open(a) as f:
        if a.endswith("md"):
            s.append({"name": os.path.splitext(os.path.basename(a))[0],
                  "type": "documentation",
                  "doc": f.read().decode("utf-8")
              })
        else:
            uri = "file://" + os.path.abspath(a)
            _, _, metaschema_loader = schema.get_metaschema()
            j, schema_metadata = metaschema_loader.resolve_ref(uri, "")
            if isinstance(j, list):
                s.extend(j)
            else:
                s.append(j)

    redirect = {r.split("=")[0]:r.split("=")[1] for r in args.redirect} if args.redirect else {}
    renderlist = args.only if args.only else []
    avrold_doc(s, sys.stdout, renderlist, redirect, args.brand, args.brandlink)
<|endoftext|>