<commit_before># -*- coding: utf-8 -*-
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models

NO_BI_MODELS = [
    'temp.range',
    'account.statement.operation.template',
    'fetchmail.server'
]

NO_BI_FIELDS = [
    'id',
    'create_uid',
    'create_date',
    'write_uid',
    'write_date'
]

NO_BI_TTYPES = [
    'many2many',
    'one2many',
    'html',
    'binary',
    'reference'
]

def dict_for_field(field):
    return {
        'id': field.id,
        'name': field.name,
        'description': field.field_description,
        'type': field.ttype,
        'relation': field.relation,
        'custom': False,
        'model_id': field.model_id.id,
        'model': field.model_id.model,
        'model_name': field.model_id.name
    }

class IrModel(models.Model):
    _inherit = 'ir.model'

    @api.model
    def _filter_bi_fields(self, ir_model_field_obj):
        name = ir_model_field_obj.name
        model = ir_model_field_obj.model_id
        model_name = model.model
        Model = self.env[model_name]
        if name in Model._fields:
            f = Model._fields[name]
            return f.store
        return False

    @api.model
    def _filter_bi_models(self, model):

        def _check_name(model_model):
            if model_model in NO_BI_MODELS:
                return 1
            return 0

        def _check_startswith(model_model):
            if model_model.startswith('workflow') or \
                    model_model.startswith('ir.') or \
                    model_model.startswith('base_'):
                return 1
            return 0

        def _check_contains(model_model):
            if 'mail' in model_model or \
                    '_' in model_model or \
                    'report' in model_model or \
                    'edi.' in model_model:
                return 1
            return 0

        def _check_unknow(model_name):
            if model_name == 'Unknow' or '.' in model_name:
                return 1
            return 0

        model_model = model['model']
        model_name = model['name']
        count_check = 0
        count_check += _check_name(model_model)
        count_check += _check_startswith(model_model)
        count_check += _check_contains(model_model)
        count_check += _check_unknow(model_name)
        if not count_check:
            return self.env['ir.model.access'].check(
                model['model'], 'read', False)
        return False

    @api.model
    def get_related_fields(self, model_ids):
        """ Return list of field dicts for all fields that can be
            joined with models in model_ids
        """
        Model = self.env['ir.model']
        domain = [('id', 'in', model_ids.values())]
        models = Model.sudo().search(domain)
        model_names = {}
        for model in models:
            model_names.update({model.id: model.model})

        related_fields = self._get_related_fields_list(model_ids, model_names)
        return related_fields

    @api.model
    def _get_related_fields_list(self, model_ids, model_names):

        def _get_right_fields(model_ids, model_names):
            Fields = self.env['ir.model.fields']
            rfields = []
            domain = [('model_id', 'in', model_ids.values()),
                      ('ttype', 'in', ['many2one'])]
            for field in filter(
                    self._filter_bi_fields,
                    Fields.sudo().search(domain)):
                for model in model_ids.items():
                    if model[1] == field.model_id.id:
                        rfields.append(
                            dict(dict_for_field(field),
                                 join_node=-1,
                                 table_alias=model[0])
                        )
            return rfields

        def _get_left_fields(model_ids, model_names):
            Fields = self.env['ir.model.fields']
            lfields = []
            domain = [('relation', 'in', model_names.values()),
                      ('ttype', 'in', ['many2one'])]
            for field in filter(
                    self._filter_bi_fields,
                    Fields.sudo().search(domain)):
                for model in model_ids.items():
                    if model_names[model[1]] == field['relation']:
                        lfields.append(
                            dict(dict_for_field(field),
                                 join_node=model[0],
                                 table_alias=-1)
                        )
            return lfields

        def _get_relation_list(model_ids, model_names, lfields):
            relation_list = []
            for model in model_ids.items():
                for field in lfields:
                    if model_names[model[1]] == field['relation']:
                        relation_list.append(
                            dict(field, join_node=model[0])
                        )
            return relation_list

        def _get_model_list(model_ids, rfields):
            model_list = []
            for model in model_ids.items():
                for field in rfields:
                    if model[1] == field['model_id']:
                        model_list.append(
                            dict(field, table_alias=model[0])
                        )
            return model_list

        lfields = _get_left_fields(model_ids, model_names)
        rfields = _get_right_fields(model_ids, model_names)

        relation_list = _get_relation_list(model_ids, model_names, lfields)
        model_list = _get_model_list(model_ids, rfields)

        related_fields = relation_list + model_list
        return related_fields

    @api.model
    def get_related_models(self, model_ids):
        """ Return list of model dicts for all models that can be
            joined with models in model_ids
        """
        def _get_field(fields, orig, target):
            field_list = []
            for f in fields:
                if f[orig] == -1:
                    field_list.append(f[target])
            return field_list

        def _get_list_id(model_ids, fields):
            list_model = model_ids.values()
            list_model += _get_field(fields, 'table_alias', 'model_id')
            return list_model

        def _get_list_relation(fields):
            list_model = _get_field(fields, 'join_node', 'relation')
            return list_model

        models_list = []
        related_fields = self.get_related_fields(model_ids)
        list_id = _get_list_id(model_ids, related_fields)
        list_model = _get_list_relation(related_fields)
        domain = ['|',
                  ('id', 'in', list_id),
                  ('model', 'in', list_model)]
        models = self.env['ir.model'].sudo().search(domain)
        for model in models:
            models_list.append({
                'id': model.id,
                'name': model.name,
                'model': model.model
            })
        return sorted(
            filter(self._filter_bi_models, models_list),
            key=lambda x: x['name']
        )

    @api.model
    def get_models(self):
        """ Return list of model dicts for all available models.
        """
        def dict_for_model(model):
            return {
                'id': model.id,
                'name': model.name,
                'model': model.model
            }

        models_domain = [('transient', '=', False)]
        return sorted(filter(
            self._filter_bi_models,
            [dict_for_model(model)
                for model in self.search(models_domain)]),
            key=lambda x: x['name'])

    @api.model
    def get_join_nodes(self, field_data, new_field):
        """ Return list of field dicts of join nodes

            Return all possible join nodes to add new_field to the query
            containing model_ids.
        """
        def _get_model_ids(field_data):
            model_ids = dict([(field['table_alias'],
                               field['model_id']) for field in field_data])
            return model_ids

        def _get_join_nodes_dict(model_ids, new_field):
            join_nodes = []
            for alias, model_id in model_ids.items():
                if model_id == new_field['model_id']:
                    join_nodes.append({'table_alias': alias})
            for d in self.get_related_fields(model_ids):
                c = [d['join_node'] == -1, d['table_alias'] == -1]
                a = (new_field['model'] == d['relation'] and d['join_node'] == -1)
                b = (new_field['model_id'] == d['model_id'] and d['table_alias'] == -1)
                if (a and c[0]) or (b and c[1]):
                    join_nodes.append(d)
            return join_nodes

        model_ids = _get_model_ids(field_data)
        keys = [(field['table_alias'], field['id'])
                for field in field_data if field.get('join_node', -1) != -1]
        join_nodes = _get_join_nodes_dict(model_ids, new_field)
        return filter(
            lambda x: 'id' not in x or
                      (x['table_alias'], x['id']) not in keys, join_nodes)

    @api.model
    def get_fields(self, model_id):
        bi_field_domain = [
            ('model_id', '=', model_id),
            ('name', 'not in', NO_BI_FIELDS),
            ('ttype', 'not in', NO_BI_TTYPES)
        ]
        Fields = self.env['ir.model.fields']
        fields = filter(
            self._filter_bi_fields,
            Fields.sudo().search(bi_field_domain)
        )
        fields_dict = []
        for field in fields:
            fields_dict.append(
                {'id': field.id,
                 'model_id': model_id,
                 'name': field.name,
                 'description': field.field_description,
                 'type': field.ttype,
                 'custom': False,
                 'model': field.model_id.model,
                 'model_name': field.model_id.name
                 }
            )
        sorted_fields = sorted(
            fields_dict,
            key=lambda x: x['description'],
            reverse=True
        )
        return sorted_fields

    @api.model
    def create(self, vals):
        if self._context and self._context.get('bve'):
            vals['state'] = 'base'
        res = super(IrModel, self).create(vals)

        # this sql update is necessary since a write method here would
        # be not working (an orm constraint is restricting the modification
        # of the state field while updating ir.model)
        q = ("""UPDATE ir_model SET state = 'manual'
               WHERE id = """ + str(res.id))

        self.env.cr.execute(q)

        return res
<commit_msg>Code refactoring of module 'bi_view_editor';<commit_after># -*- coding: utf-8 -*-
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models

NO_BI_MODELS = [
    'temp.range',
    'account.statement.operation.template',
    'fetchmail.server'
]

NO_BI_FIELDS = [
    'id',
    'create_uid',
    'create_date',
    'write_uid',
    'write_date'
]

NO_BI_TTYPES = [
    'many2many',
    'one2many',
    'html',
    'binary',
    'reference'
]

def dict_for_field(field):
    return {
        'id': field.id,
        'name': field.name,
        'description': field.field_description,
        'type': field.ttype,
        'relation': field.relation,
        'custom': False,
        'model_id': field.model_id.id,
        'model': field.model_id.model,
        'model_name': field.model_id.name
    }

class IrModel(models.Model):
    _inherit = 'ir.model'

    @api.model
    def _filter_bi_fields(self, ir_model_field_obj):
        name = ir_model_field_obj.name
        model = ir_model_field_obj.model_id
        model_name = model.model
        Model = self.env[model_name]
        if name in Model._fields:
            f = Model._fields[name]
            return f.store
        return False

    @api.model
    def _filter_bi_models(self, model):

        def _check_name(model_model):
            if model_model in NO_BI_MODELS:
                return 1
            return 0

        def _check_startswith(model_model):
            if model_model.startswith('workflow') or \
                    model_model.startswith('ir.') or \
                    model_model.startswith('base_'):
                return 1
            return 0

        def _check_contains(model_model):
            if 'mail' in model_model or \
                    '_' in model_model or \
                    'report' in model_model or \
                    'edi.' in model_model:
                return 1
            return 0

        def _check_unknow(model_name):
            if model_name == 'Unknow' or '.' in model_name:
                return 1
            return 0

        model_model = model['model']
        model_name = model['name']
        count_check = 0
        count_check += _check_name(model_model)
        count_check += _check_startswith(model_model)
        count_check += _check_contains(model_model)
        count_check += _check_unknow(model_name)
        if not count_check:
            return self.env['ir.model.access'].check(
                model['model'], 'read', False)
        return False

    @api.model
    def get_related_fields(self, model_ids):
        """ Return list of field dicts for all fields that can be
            joined with models in model_ids
        """
        Model = self.env['ir.model']
        domain = [('id', 'in', model_ids.values())]
        models = Model.sudo().search(domain)
        model_names = {}
        for model in models:
            model_names.update({model.id: model.model})

        related_fields = self._get_related_fields_list(model_ids, model_names)
        return related_fields

    @api.model
    def _get_related_fields_list(self, model_ids, model_names):

        def _get_right_fields(model_ids, model_names):
            Fields = self.env['ir.model.fields']
            rfields = []
            domain = [('model_id', 'in', model_ids.values()),
                      ('ttype', 'in', ['many2one'])]
            for field in filter(
                    self._filter_bi_fields,
                    Fields.sudo().search(domain)):
                for model in model_ids.items():
                    if model[1] == field.model_id.id:
                        rfields.append(
                            dict(dict_for_field(field),
                                 join_node=-1,
                                 table_alias=model[0])
                        )
            return rfields

        def _get_left_fields(model_ids, model_names):
            Fields = self.env['ir.model.fields']
            lfields = []
            domain = [('relation', 'in', model_names.values()),
                      ('ttype', 'in', ['many2one'])]
            for field in filter(
                    self._filter_bi_fields,
                    Fields.sudo().search(domain)):
                for model in model_ids.items():
                    if model_names[model[1]] == field['relation']:
                        lfields.append(
                            dict(dict_for_field(field),
                                 join_node=model[0],
                                 table_alias=-1)
                        )
            return lfields

        def _get_relation_list(model_ids, model_names, lfields):
            relation_list = []
            for model in model_ids.items():
                for field in lfields:
                    if model_names[model[1]] == field['relation']:
                        relation_list.append(
                            dict(field, join_node=model[0])
                        )
            return relation_list

        def _get_model_list(model_ids, rfields):
            model_list = []
            for model in model_ids.items():
                for field in rfields:
                    if model[1] == field['model_id']:
                        model_list.append(
                            dict(field, table_alias=model[0])
                        )
            return model_list

        lfields = _get_left_fields(model_ids, model_names)
        rfields = _get_right_fields(model_ids, model_names)

        relation_list = _get_relation_list(model_ids, model_names, lfields)
        model_list = _get_model_list(model_ids, rfields)

        related_fields = relation_list + model_list
        return related_fields

    @api.model
    def get_related_models(self, model_ids):
        """ Return list of model dicts for all models that can be
            joined with models in model_ids
        """
        def _get_field(fields, orig, target):
            field_list = []
            for f in fields:
                if f[orig] == -1:
                    field_list.append(f[target])
            return field_list

        def _get_list_id(model_ids, fields):
            list_model = model_ids.values()
            list_model += _get_field(fields, 'table_alias', 'model_id')
            return list_model

        def _get_list_relation(fields):
            list_model = _get_field(fields, 'join_node', 'relation')
            return list_model

        models_list = []
        related_fields = self.get_related_fields(model_ids)
        list_id = _get_list_id(model_ids, related_fields)
        list_model = _get_list_relation(related_fields)
        domain = ['|',
                  ('id', 'in', list_id),
                  ('model', 'in', list_model)]
        models = self.env['ir.model'].sudo().search(domain)
        for model in models:
            models_list.append({
                'id': model.id,
                'name': model.name,
                'model': model.model
            })
        return sorted(
            filter(self._filter_bi_models, models_list),
            key=lambda x: x['name']
        )

    @api.model
    def get_models(self):
        """ Return list of model dicts for all available models.
        """
        def dict_for_model(model):
            return {
                'id': model.id,
                'name': model.name,
                'model': model.model
            }

        models_domain = [('transient', '=', False)]
        return sorted(filter(
            self._filter_bi_models,
            [dict_for_model(model)
                for model in self.search(models_domain)]),
            key=lambda x: x['name'])

    @api.model
    def get_join_nodes(self, field_data, new_field):
        """ Return list of field dicts of join nodes

            Return all possible join nodes to add new_field to the query
            containing model_ids.
        """
        def _get_model_ids(field_data):
            model_ids = dict([(field['table_alias'],
                               field['model_id']) for field in field_data])
            return model_ids

        def _get_join_nodes_dict(model_ids, new_field):
            join_nodes = []
            for alias, model_id in model_ids.items():
                if model_id == new_field['model_id']:
                    join_nodes.append({'table_alias': alias})
            for d in self.get_related_fields(model_ids):
                c = [d['join_node'] == -1, d['table_alias'] == -1]
                a = (new_field['model'] == d['relation'])
                b = (new_field['model_id'] == d['model_id'])
                if (a and c[0]) or (b and c[1]):
                    join_nodes.append(d)
            return join_nodes

        model_ids = _get_model_ids(field_data)
        keys = [(field['table_alias'], field['id'])
                for field in field_data if field.get('join_node', -1) != -1]
        join_nodes = _get_join_nodes_dict(model_ids, new_field)
        return filter(
            lambda x: 'id' not in x or
                      (x['table_alias'], x['id']) not in keys, join_nodes)

    @api.model
    def get_fields(self, model_id):
        bi_field_domain = [
            ('model_id', '=', model_id),
            ('name', 'not in', NO_BI_FIELDS),
            ('ttype', 'not in', NO_BI_TTYPES)
        ]
        Fields = self.env['ir.model.fields']
        fields = filter(
            self._filter_bi_fields,
            Fields.sudo().search(bi_field_domain)
        )
        fields_dict = []
        for field in fields:
            fields_dict.append(
                {'id': field.id,
                 'model_id': model_id,
                 'name': field.name,
                 'description': field.field_description,
                 'type': field.ttype,
                 'custom': False,
                 'model': field.model_id.model,
                 'model_name': field.model_id.name
                 }
            )
        sorted_fields = sorted(
            fields_dict,
            key=lambda x: x['description'],
            reverse=True
        )
        return sorted_fields

    @api.model
    def create(self, vals):
        if self._context and self._context.get('bve'):
            vals['state'] = 'base'
        res = super(IrModel, self).create(vals)

        # this sql update is necessary since a write method here would
        # be not working (an orm constraint is restricting the modification
        # of the state field while updating ir.model)
        q = ("""UPDATE ir_model SET state = 'manual'
               WHERE id = """ + str(res.id))

        self.env.cr.execute(q)

        return res
<|endoftext|>


<commit_before>"""Test how gradients are drawn."""

import pytest

from ..testing_utils import assert_no_logs

@assert_no_logs
def test_linear_gradients_1(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        BBBBB
        BBBBB
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white, white 3px, blue 0, blue 5px, red 0, red
    )''')

@assert_no_logs
def test_linear_gradients_2(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        BBBBB
        BBBBB
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white 3px, blue 0, blue 5px, red 0
    )''')

@assert_no_logs
def test_linear_gradients_3(assert_pixels):
    assert_pixels('''
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, white 3px, blue 0, blue 5px, red 0
    )''')

@assert_no_logs
def test_linear_gradients_4(assert_pixels):
    assert_pixels('''
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
    ''', '''<style>@page { size: 10px 5px; background: linear-gradient(
      to right, blue 5px, blue 6px, red 6px, red 9px
    )''')

@assert_no_logs
def test_linear_gradients_5(assert_pixels):
    assert_pixels('''
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
    ''', '''
      <style>@page { size: 10px 5px; background: repeating-linear-gradient(
      to right, blue 50%, blue 60%, red 60%, red 90%
    )''')

@assert_no_logs
def test_linear_gradients_6(assert_pixels):
    assert_pixels('''
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, blue 3px, blue 3px, red 3px, red 3px
    )''')

@assert_no_logs
def test_linear_gradients_7(assert_pixels):
    assert_pixels('''
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
    ''', '''<style>@page { size: 9px 5px; background:
    repeating-linear-gradient(
      to right, black 3px, black 3px, #800080 3px, #800080 3px
    )''')

@assert_no_logs
def test_linear_gradients_8(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(to right, blue 3px)''')

@assert_no_logs
def test_linear_gradients_9(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(45deg, blue 3px)''')

@assert_no_logs
def test_linear_gradients_10(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      45deg, blue 3px, red 3px, red 3px, blue 3px
    )''')

@assert_no_logs
def test_linear_gradients_11(assert_pixels):
    assert_pixels('''
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, blue 3px, red 3px, red 4px, blue 4px
    )''')

@assert_no_logs
def test_linear_gradients_12(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(to right, red 3px, blue 3px, blue 4px, red 4px
    )''')

@assert_no_logs
def test_linear_gradients_13(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        SSSSS
        SSSSS
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white, white 3px, rgba(255, 0, 0, 0.751) 0, rgba(255, 0, 0, 0.751) 5px,
      red 0, red
    )''')

@assert_no_logs
def test_radial_gradients_1(assert_pixels):
    assert_pixels('''
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
    ''', '''<style>@page { size: 6px; background:
      radial-gradient(red -30%, blue -10%)''')

@assert_no_logs
def test_radial_gradients_2(assert_pixels):
    assert_pixels('''
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
    ''', '''<style>@page { size: 6px; background:
      radial-gradient(red 110%, blue 130%)''')

@assert_no_logs
def test_radial_gradients_3(assert_pixels):
    assert_pixels('''
        BzzzzzzzzB
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        BzzzzzzzzB
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 20%, blue 80%)''')

@assert_no_logs
def test_radial_gradients_4(assert_pixels):
    assert_pixels('''
        BzzzzzzzzB
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        BzzzzzzzzB
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 50%, blue 50%)''')

@assert_no_logs
def test_radial_gradients_5(assert_pixels):
    assert_pixels('''
        SzzzzzzzzS
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        SzzzzzzzzS
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 50%, rgba(255, 0, 0, 0.751) 50%)''')
<commit_msg>Remove useless import<commit_after>"""Test how gradients are drawn."""

from ..testing_utils import assert_no_logs

@assert_no_logs
def test_linear_gradients_1(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        BBBBB
        BBBBB
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white, white 3px, blue 0, blue 5px, red 0, red
    )''')

@assert_no_logs
def test_linear_gradients_2(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        BBBBB
        BBBBB
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white 3px, blue 0, blue 5px, red 0
    )''')

@assert_no_logs
def test_linear_gradients_3(assert_pixels):
    assert_pixels('''
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
        ___BBrrrr
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, white 3px, blue 0, blue 5px, red 0
    )''')

@assert_no_logs
def test_linear_gradients_4(assert_pixels):
    assert_pixels('''
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
        BBBBBBrrrr
    ''', '''<style>@page { size: 10px 5px; background: linear-gradient(
      to right, blue 5px, blue 6px, red 6px, red 9px
    )''')

@assert_no_logs
def test_linear_gradients_5(assert_pixels):
    assert_pixels('''
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
        rBrrrBrrrB
    ''', '''
      <style>@page { size: 10px 5px; background: repeating-linear-gradient(
      to right, blue 50%, blue 60%, red 60%, red 90%
    )''')

@assert_no_logs
def test_linear_gradients_6(assert_pixels):
    assert_pixels('''
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
        BBBrrrrrr
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, blue 3px, blue 3px, red 3px, red 3px
    )''')

@assert_no_logs
def test_linear_gradients_7(assert_pixels):
    assert_pixels('''
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
        hhhhhhhhh
    ''', '''<style>@page { size: 9px 5px; background:
    repeating-linear-gradient(
      to right, black 3px, black 3px, #800080 3px, #800080 3px
    )''')

@assert_no_logs
def test_linear_gradients_8(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(to right, blue 3px)''')

@assert_no_logs
def test_linear_gradients_9(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(45deg, blue 3px)''')

@assert_no_logs
def test_linear_gradients_10(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      45deg, blue 3px, red 3px, red 3px, blue 3px
    )''')

@assert_no_logs
def test_linear_gradients_11(assert_pixels):
    assert_pixels('''
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
        BBBrBBBBB
    ''', '''<style>@page { size: 9px 5px; background: linear-gradient(
      to right, blue 3px, red 3px, red 4px, blue 4px
    )''')

@assert_no_logs
def test_linear_gradients_12(assert_pixels):
    assert_pixels('''
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
        BBBBBBBBB
    ''', '''<style>@page { size: 9px 5px; background:
      repeating-linear-gradient(to right, red 3px, blue 3px, blue 4px, red 4px
    )''')

@assert_no_logs
def test_linear_gradients_13(assert_pixels):
    assert_pixels('''
        _____
        _____
        _____
        SSSSS
        SSSSS
        RRRRR
        RRRRR
        RRRRR
        RRRRR
    ''', '''<style>@page { size: 5px 9px; background: linear-gradient(
      white, white 3px, rgba(255, 0, 0, 0.751) 0, rgba(255, 0, 0, 0.751) 5px,
      red 0, red
    )''')

@assert_no_logs
def test_radial_gradients_1(assert_pixels):
    assert_pixels('''
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
        BBBBBB
    ''', '''<style>@page { size: 6px; background:
      radial-gradient(red -30%, blue -10%)''')

@assert_no_logs
def test_radial_gradients_2(assert_pixels):
    assert_pixels('''
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
        RRRRRR
    ''', '''<style>@page { size: 6px; background:
      radial-gradient(red 110%, blue 130%)''')

@assert_no_logs
def test_radial_gradients_3(assert_pixels):
    assert_pixels('''
        BzzzzzzzzB
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        BzzzzzzzzB
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 20%, blue 80%)''')

@assert_no_logs
def test_radial_gradients_4(assert_pixels):
    assert_pixels('''
        BzzzzzzzzB
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        BzzzzzzzzB
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 50%, blue 50%)''')

@assert_no_logs
def test_radial_gradients_5(assert_pixels):
    assert_pixels('''
        SzzzzzzzzS
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzRRzzzz
        zzzzRRzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        zzzzzzzzzz
        SzzzzzzzzS
    ''', '''<style>@page { size: 10px 16px; background:
      radial-gradient(red 50%, rgba(255, 0, 0, 0.751) 50%)''')
<|endoftext|>


<commit_before>#!/usr/bin/python

import sys
import os
import base64
import requests
import json

HELP="Usage: python "+sys.argv[0]+" -h|id \n\
\t -h - optional - prints this help message\n\
\t id - id of adapter"

serverAddress = "http://ant-2.fit.vutbr.cz:1338"

# return MAC address of eth0
def getMAC():
	MAC=os.popen("ifconfig -a | grep \"eth[0-9]*\" | grep -o \"HWaddr [0-9A-F:]*\" | grep -o \"[0-9A-F:]*\"").read()
	MAC = MAC.replace('\n', '')

	return MAC

# return security ID of CPU
def getSID():
	SID_1=os.popen("devmem2 0x01c23800 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_2=os.popen("devmem2 0x01c23804 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_3=os.popen("devmem2 0x01c23808 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_4=os.popen("devmem2 0x01c2380c | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()

	SID_1 = SID_1.replace('\n', '')
	SID_2 = SID_2.replace('\n', '')
	SID_3 = SID_3.replace('\n', '')
	SID_4 = SID_4.replace('\n', '')
	SID=SID_1[2:] + SID_2[2:] + SID_3[2:] + SID_4[2:]

	return SID

# saves adapter specified by id, mac and secure_id
def save_adapter(mac, sid):
	headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
	data = {
		"secure_id": sid,
		"lan_mac": mac
	}

	res = requests.post(serverAddress + "/api/adapter/create", data = json.dumps(data), headers = headers)

	return res.json() # return adapter json

if __name__ == '__main__':
	if len(sys.argv) > 1 and sys.argv[1] == "-h":
		print HELP
		sys.exit(0)

	MAC = getMAC()
	SID = getSID()
	adapter = save_adapter(MAC, SID)

	ID = adapter["adapter_id"]
	cert = base64.b64decode(adapter["cert"])
	key = base64.b64decode(adapter["key"])
	pan_id = adapter["pan_id"]  # [3E, 29. C3, 39]

	with open("/etc/openvpn/client.crt", "w") as cert_file:
		cert_file.write(cert)

	with open("/etc/openvpn/client.key", "w") as key_file:
		key_file.write(key)

	with open("/etc/beeeon/fitprotocold.ini", "w") as conf_file:
		edids = "edid0=0x{0}\nedid0=0x{1}\nedid0=0x{2}\nedid0=0x{3}".format(pan_id[0], pan_id[1], pan_id[2], pan_id[3])
		conf_file.write("[net_config]\nchannel=28\n"+edids)

	print "\tID adapteru je:", ID
	print "\tMAC adresa je: ", MAC
	print "\tSID cislo je:  ", SID
	print "\tPAN id je:     ", pan_id

	EEPROM_DATA_VERIFICATION = "ad"
	EEPROM_DATA_VERSION = "01"

	EEPROM_DATA = EEPROM_DATA_VERIFICATION + EEPROM_DATA_VERSION

	coded_adapter_id = "{0:#0{1}x}".format(int(ID), 16)[2:]

	length_adapter_id = len(coded_adapter_id)/2
	if ((len(coded_adapter_id)%2)  == 1) :
		length_adapter_id += 1

	EEPROM_DATA += "01" + str(format(length_adapter_id, '02x')) + coded_adapter_id
	EEPROM_DATA += "020100fe"

	#print "EEPROM data: ",  EEPROM_DATA, "(ID ADA HEX 0x" + coded_adapter_id + ")"
	eeprom = open("/sys/devices/platform/soc@01c00000/1c2b000.i2c/i2c-1/1-0050/eeprom", 'w')

	hex_data = EEPROM_DATA.decode("hex")
	#print "\tEEPROM_DATA_VERIFICATION: ", EEPROM_DATA_VERIFICATION
	#print "\tEEPROM_DATA_VERSION     : ", EEPROM_DATA_VERSION
	#print "\tEEPROM_DATA - ADAPTER_ID: ", (ID + " (0x" + coded_adapter_id + ")")
	#print "\tEEPROM_DATA TOGETHER    : ", EEPROM_DATA
	print "Zapisuji AdapterID + dalsi atributy do EEPROM pameti:"
	print "\tEEPROM data: ",  EEPROM_DATA
	eeprom.write(hex_data)
	eeprom.close()

	print "Povoluji spusteni AdaApp"
	os.system("systemctl enable beeeon-adaapp")

	print "Hotovo, finish!"
<commit_msg>[PAN] Fix edidX key values and add newline at the end of ini file<commit_after>#!/usr/bin/python

import sys
import os
import base64
import requests
import json

HELP="Usage: python "+sys.argv[0]+" -h|id \n\
\t -h - optional - prints this help message\n\
\t id - id of adapter"

serverAddress = "http://ant-2.fit.vutbr.cz:1338"

# return MAC address of eth0
def getMAC():
	MAC=os.popen("ifconfig -a | grep \"eth[0-9]*\" | grep -o \"HWaddr [0-9A-F:]*\" | grep -o \"[0-9A-F:]*\"").read()
	MAC = MAC.replace('\n', '')

	return MAC

# return security ID of CPU
def getSID():
	SID_1=os.popen("devmem2 0x01c23800 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_2=os.popen("devmem2 0x01c23804 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_3=os.popen("devmem2 0x01c23808 | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()
	SID_4=os.popen("devmem2 0x01c2380c | grep \"Read*\" | grep -o \":.*\" | grep -o \"0x[0-9A-Fa-f]*\"").read()

	SID_1 = SID_1.replace('\n', '')
	SID_2 = SID_2.replace('\n', '')
	SID_3 = SID_3.replace('\n', '')
	SID_4 = SID_4.replace('\n', '')
	SID=SID_1[2:] + SID_2[2:] + SID_3[2:] + SID_4[2:]

	return SID

# saves adapter specified by id, mac and secure_id
def save_adapter(mac, sid):
	headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
	data = {
		"secure_id": sid,
		"lan_mac": mac
	}

	res = requests.post(serverAddress + "/api/adapter/create", data = json.dumps(data), headers = headers)

	return res.json() # return adapter json

if __name__ == '__main__':
	if len(sys.argv) > 1 and sys.argv[1] == "-h":
		print HELP
		sys.exit(0)

	MAC = getMAC()
	SID = getSID()
	adapter = save_adapter(MAC, SID)

	ID = adapter["adapter_id"]
	cert = base64.b64decode(adapter["cert"])
	key = base64.b64decode(adapter["key"])
	pan_id = adapter["pan_id"]  # [3E, 29. C3, 39]

	with open("/etc/openvpn/client.crt", "w") as cert_file:
		cert_file.write(cert)

	with open("/etc/openvpn/client.key", "w") as key_file:
		key_file.write(key)

	with open("/etc/beeeon/fitprotocold.ini", "w") as conf_file:
		edids = "edid0=0x{0}\nedid1=0x{1}\nedid2=0x{2}\nedid3=0x{3}\n".format(pan_id[0], pan_id[1], pan_id[2], pan_id[3])
		conf_file.write("[net_config]\nchannel=28\n"+edids)

	print "\tID adapteru je:", ID
	print "\tMAC adresa je: ", MAC
	print "\tSID cislo je:  ", SID
	print "\tPAN id je:     ", pan_id

	EEPROM_DATA_VERIFICATION = "ad"
	EEPROM_DATA_VERSION = "01"

	EEPROM_DATA = EEPROM_DATA_VERIFICATION + EEPROM_DATA_VERSION

	coded_adapter_id = "{0:#0{1}x}".format(int(ID), 16)[2:]

	length_adapter_id = len(coded_adapter_id)/2
	if ((len(coded_adapter_id)%2)  == 1) :
		length_adapter_id += 1

	EEPROM_DATA += "01" + str(format(length_adapter_id, '02x')) + coded_adapter_id
	EEPROM_DATA += "020100fe"

	#print "EEPROM data: ",  EEPROM_DATA, "(ID ADA HEX 0x" + coded_adapter_id + ")"
	eeprom = open("/sys/devices/platform/soc@01c00000/1c2b000.i2c/i2c-1/1-0050/eeprom", 'w')

	hex_data = EEPROM_DATA.decode("hex")
	#print "\tEEPROM_DATA_VERIFICATION: ", EEPROM_DATA_VERIFICATION
	#print "\tEEPROM_DATA_VERSION     : ", EEPROM_DATA_VERSION
	#print "\tEEPROM_DATA - ADAPTER_ID: ", (ID + " (0x" + coded_adapter_id + ")")
	#print "\tEEPROM_DATA TOGETHER    : ", EEPROM_DATA
	print "Zapisuji AdapterID + dalsi atributy do EEPROM pameti:"
	print "\tEEPROM data: ",  EEPROM_DATA
	eeprom.write(hex_data)
	eeprom.close()

	print "Povoluji spusteni AdaApp"
	os.system("systemctl enable beeeon-adaapp")

	print "Hotovo, finish!"
<|endoftext|>


<commit_before>""" Set of interfaces that allow interaction with data. Currently
    available interfaces are:

    DataSource: Generic nifti to named Nifti interface
    DataSink: Generic named output from interfaces to data store

    To come :
    XNATSource, XNATSink

"""
from nipype.interfaces.base import Interface, CommandLine, Bunch, InterfaceResult
from copy import deepcopy
from nipype.utils.filemanip import copyfiles, list_to_filename
from nipype.utils.misc import mktree
import glob
import os

class DataSource(Interface):
    """ Generic datasource module that takes a directory containing a
        list of nifti files and provides a set of structured output
        fields.
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
            Parameters
            --------------------
            (all default to None)

            base_directory : /path/to/dir
                Basedirectory consisting of subject data
            subject_template : string
                Template encoding the subject directory name, indexed
                by subject id.
            file_template : string
                Template used for matching filenames.
                Default = '*-%d-*.nii'
            subject_id: string or int
                Subject identifier
            subject_directory : /path/to/dir
                Path to subject directory
            subject_info : dict
                Provides information about how to map subject run
                numbers to output fields

                subject_id are keys and values are a list of tuples.
                info[subject_id] = [([runnos],fieldname),...]

                Examples
                --------

                info['s1'] = [([4,5],'anat'),([6,7],'bold'),([8],'dti')]
                info[1] = [([3,4],'struct'),([6,7],'bold'),([8],'dti')]

                In the above examples, the output fields of this
                object will be the names 'anat', 'bold', etc.,.
            """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(base_directory=None,
                            subject_template=None,
                            file_template='*-%d-*.nii',
                            subject_id=None,
                            subject_directory=None,
                            subject_info=None)

    def outputs_help(self):
        """
            Parameters
            ----------

            (all default to None)

            subject_id : string
                Subject identifier
            subject_directory: /path/to/dir
                Location of subject directory containing nifti files

            remaining fields are defined by user. See subject_info in
            inputs_help() for description of how to specify output
            fields
            """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch(subject_id=None)
        outputs.subject_id = self.inputs.subject_id
        subjdir = self.inputs.subject_directory
        if subjdir is None:
            #print self.inputs['subj_template'],self.inputs['subj_id']
            if self.inputs.subject_template is not None:
                subjdir = self.inputs.subject_template % self.inputs.subject_id
            else:
                subjdir = self.inputs.subject_id
            subjdir = os.path.join(self.inputs.base_directory,subjdir)
        if subjdir is None:
            raise Exception('Subject directory not provided')
        outputs.subject_directory = subjdir
        if self.inputs.subject_info is None:
            raise Exception('Subject info not provided')
        try:
            info = self.inputs.subject_info[self.inputs.subject_id]
        except KeyError:
            raise KeyError("Key [%s] does not exist in subject_info dictionary"
                           % self.inputs.subject_id)
        for idx, _type in info:
            outputs[_type] = []
            for i in idx:
                files = self.inputs.file_template % i
                path = os.path.abspath(os.path.join(subjdir, files))
                files_found = glob.glob(path)
                if len(files_found) == 0:
                    msg = 'Unable to find file: %s' % path
                    raise IOError(msg)
                outputs[_type].extend(files_found)
            outputs[_type] = list_to_filename(outputs[_type])
        return outputs

    def run(self, cwd=None):
        """Execute this module.

        cwd is just there to make things "work" for now
        """
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

class DataSink(Interface):
    """ Generic datasink module that takes a directory containing a
        list of nifti files and provides a set of structured output
        fields.
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
            Parameters
            ----------
            (all default to None)

            base_directory : /path/to/dir
                Basedirectory consisting of subject data
            subject_id: string or int
                Subject identifier
            subject_directory : /path/to/dir
                Path to subject directory

            Any fields that are set as lists will be copied to a
            directory under subject_directory with the fieldname as a
            new directory.

        """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(base_directory=None,
                            subject_directory=None,
                            subject_template=None,
                            subject_id=None)
        self.input_keys = self.inputs.__dict__.keys()

    def outputs_help(self):
        """
            No outputs
        """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch()
        return Bunch()

    def run(self, cwd=None):
        """Execute this module.

        cwd is just there to make things work for now
        """
        subjdir = self.inputs.subject_directory
        if subjdir is None:
            #print self.inputs['subj_template'],self.inputs['subj_id']
            if self.inputs.subject_template is not None:
                subjdir = self.inputs.subject_template % (self.inputs.subject_id)
            else:
                subjdir = self.inputs.subject_id
            subjdir = os.path.join(self.inputs.base_directory,subjdir)
        if subjdir is None:
            raise Exception('Subject directory not provided')
        outdir = subjdir
        if not os.path.exists(outdir):
            mktree(outdir)
        for k,v in self.inputs.iteritems():
            if k not in self.input_keys:
                if v is not None:
                    tempoutdir = outdir
                    for d in k.split('.'):
                        if d[0] == '@':
                            continue
                        tempoutdir = os.path.join(tempoutdir,d)
                        if not os.path.exists(tempoutdir):
                            os.mkdir(tempoutdir)
                    copyfiles(self.inputs[k],tempoutdir,copy=True)
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

class DataGrabber(Interface):
    """ Generic datagrabber module that wraps around glob in an
        intelligent way for neuroimaging tasks
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
            Parameters
            --------------------
            (all default to None)

            file_template : string
                template for filename
            template_argtuple: tuple of arguments
                arguments that fit into file_template
            template_argnames: list of strings
                provides names of inputs that will be used as
                arguments to the template.
                For example,

                dg.file_template = '%s/%s.nii'

                dg.template_argtuple = ('foo','foo')

                is equivalent to

                dg.inputs.arg1 = 'foo'
                dg.inputs.arg2 = 'foo'
                dg.inputs.template_argnames = ['arg1','arg2']

                however this latter form can be used with iterables
                and iterfield in a pipeline.
            """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(file_template=None,
                            template_argtuple=None,
                            template_argnames=None,
                            )

    def outputs_help(self):
        """
            Parameters
            ----------

            (all default to None)

            file_list : list
                list of files picked up by the grabber
            """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch(file_list=None)
        args = []
        if self.inputs.template_argtuple is not None:
            args.extend(list(self.inputs.template_argtuple))

        if self.inputs.template_argnames is not None:
            for name in self.inputs.template_argnames:
                arg = self.inputs[name]
                print name, arg
                if arg is not None:
                    args.append(arg)
        template = self.inputs.file_template
        if len(args)>0:
            template = template%tuple(args)
        outputs.file_list = list_to_filename(glob.glob(template))
        return outputs

    def run(self, cwd=None):
        """Execute this module.
        """
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

<commit_msg>Some docstring cleanup while learning Datasource class.<commit_after>""" Set of interfaces that allow interaction with data. Currently
    available interfaces are:

    DataSource: Generic nifti to named Nifti interface
    DataSink: Generic named output from interfaces to data store

    To come :
    XNATSource, XNATSink

"""
from nipype.interfaces.base import Interface, CommandLine, Bunch, InterfaceResult
from copy import deepcopy
from nipype.utils.filemanip import copyfiles, list_to_filename
from nipype.utils.misc import mktree
import glob
import os

class DataSource(Interface):
    """ Generic datasource module that takes a directory containing a
        list of nifti files and provides a set of structured output
        fields.
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
        Parameters
        ----------

        base_directory : str
            Path to the base directory consisting of subject data.
        subject_template : str
            Template encoding the subject directory name, indexed
            by subject id.
        file_template : str
            Template used for matching filenames.
            Default = '*-%d-*.nii'
        subject_id: str or int
            The subject identifier.
        subject_directory : str
            Path to the subject directory.
        subject_info : dict
            Provides information about how to map subject run
            numbers to the output fields.

            `subject_id` are keys and the values are a list of tuples.
            info[subject_id] = [([run_identifiers], output_fieldname), ...]

        Examples
        --------

        Here our experiment data is stored in our home directory under
        'data/exp001'.  In the exp001 directory we have a subdirectory
        for our subject named 's1'.  In the 's1' directory we have
        four functional images, 'f3', 'f5', 'f7', 'f10'.  In the
        `info` dictionary we create an entry where the key is the
        subject identifier 's1', and the value is a list of one
        element, a tuple containing a list of the functional image
        names and a field name 'func'.  The 'func' field name is the
        output field name for this datasource object.  So for
        instance, if we were doing motion correction using SPM
        realign, the datasource output 'func' would map to the realign
        input 'infile' in the pipeline.

        >>> from nipype.interfaces.io import DataSource
        >>> info = {}
        >>> info['s1'] = [(['f3', 'f5', 'f7', 'f10'], 'func')]
        >>> datasource = DataSource()
        >>> data_dir = os.path.expanduser('~/data/exp001')
        >>> datasource.inputs.base_directory = data_dir
        >>> datasource.inputs.subject_template = '%s'
        >>> datasource.inputs.file_template = '%s.nii'
        >>> datasource.inputs.subject_info = info
        >>> datasource.inputs.subject_id = 's1'

        """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(base_directory=None,
                            subject_template=None,
                            file_template='*-%d-*.nii',
                            subject_id=None,
                            subject_directory=None,
                            subject_info=None)

    def outputs_help(self):
        """
            Parameters
            ----------

            (all default to None)

            subject_id : string
                Subject identifier
            subject_directory: /path/to/dir
                Location of subject directory containing nifti files

            remaining fields are defined by user. See subject_info in
            inputs_help() for description of how to specify output
            fields
            """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch(subject_id=None)
        outputs.subject_id = self.inputs.subject_id
        subjdir = self.inputs.subject_directory
        if subjdir is None:
            #print self.inputs['subj_template'],self.inputs['subj_id']
            if self.inputs.subject_template is not None:
                subjdir = self.inputs.subject_template % self.inputs.subject_id
            else:
                subjdir = self.inputs.subject_id
            subjdir = os.path.join(self.inputs.base_directory,subjdir)
        if subjdir is None:
            raise Exception('Subject directory not provided')
        outputs.subject_directory = subjdir
        if self.inputs.subject_info is None:
            raise Exception('Subject info not provided')
        try:
            info = self.inputs.subject_info[self.inputs.subject_id]
        except KeyError:
            raise KeyError("Key [%s] does not exist in subject_info dictionary"
                           % self.inputs.subject_id)
        for idx, _type in info:
            outputs[_type] = []
            for i in idx:
                files = self.inputs.file_template % i
                path = os.path.abspath(os.path.join(subjdir, files))
                files_found = glob.glob(path)
                if len(files_found) == 0:
                    msg = 'Unable to find file: %s' % path
                    raise IOError(msg)
                outputs[_type].extend(files_found)
            outputs[_type] = list_to_filename(outputs[_type])
        return outputs

    def run(self, cwd=None):
        """Execute this module.

        cwd is just there to make things "work" for now
        """
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

class DataSink(Interface):
    """ Generic datasink module that takes a directory containing a
        list of nifti files and provides a set of structured output
        fields.
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
            Parameters
            ----------
            (all default to None)

            base_directory : /path/to/dir
                Basedirectory consisting of subject data
            subject_id: string or int
                Subject identifier
            subject_directory : /path/to/dir
                Path to subject directory

            Any fields that are set as lists will be copied to a
            directory under subject_directory with the fieldname as a
            new directory.

        """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(base_directory=None,
                            subject_directory=None,
                            subject_template=None,
                            subject_id=None)
        self.input_keys = self.inputs.__dict__.keys()

    def outputs_help(self):
        """
            No outputs
        """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch()
        return Bunch()

    def run(self, cwd=None):
        """Execute this module.

        cwd is just there to make things work for now
        """
        subjdir = self.inputs.subject_directory
        if subjdir is None:
            #print self.inputs['subj_template'],self.inputs['subj_id']
            if self.inputs.subject_template is not None:
                subjdir = self.inputs.subject_template % (self.inputs.subject_id)
            else:
                subjdir = self.inputs.subject_id
            subjdir = os.path.join(self.inputs.base_directory,subjdir)
        if subjdir is None:
            raise Exception('Subject directory not provided')
        outdir = subjdir
        if not os.path.exists(outdir):
            mktree(outdir)
        for k,v in self.inputs.iteritems():
            if k not in self.input_keys:
                if v is not None:
                    tempoutdir = outdir
                    for d in k.split('.'):
                        if d[0] == '@':
                            continue
                        tempoutdir = os.path.join(tempoutdir,d)
                        if not os.path.exists(tempoutdir):
                            os.mkdir(tempoutdir)
                    copyfiles(self.inputs[k],tempoutdir,copy=True)
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

class DataGrabber(Interface):
    """ Generic datagrabber module that wraps around glob in an
        intelligent way for neuroimaging tasks
    """

    def __init__(self, *args, **inputs):
        self._populate_inputs()
        self.inputs.update(**inputs)

    def inputs_help(self):
        """
            Parameters
            --------------------
            (all default to None)

            file_template : string
                template for filename
            template_argtuple: tuple of arguments
                arguments that fit into file_template
            template_argnames: list of strings
                provides names of inputs that will be used as
                arguments to the template.
                For example,

                dg.file_template = '%s/%s.nii'

                dg.template_argtuple = ('foo','foo')

                is equivalent to

                dg.inputs.arg1 = 'foo'
                dg.inputs.arg2 = 'foo'
                dg.inputs.template_argnames = ['arg1','arg2']

                however this latter form can be used with iterables
                and iterfield in a pipeline.
            """
        print self.inputs_help.__doc__

    def _populate_inputs(self):
        self.inputs = Bunch(file_template=None,
                            template_argtuple=None,
                            template_argnames=None,
                            )

    def outputs_help(self):
        """
            Parameters
            ----------

            (all default to None)

            file_list : list
                list of files picked up by the grabber
            """
        print self.outputs_help.__doc__

    def aggregate_outputs(self):
        outputs = Bunch(file_list=None)
        args = []
        if self.inputs.template_argtuple is not None:
            args.extend(list(self.inputs.template_argtuple))

        if self.inputs.template_argnames is not None:
            for name in self.inputs.template_argnames:
                arg = self.inputs[name]
                print name, arg
                if arg is not None:
                    args.append(arg)
        template = self.inputs.file_template
        if len(args)>0:
            template = template%tuple(args)
        outputs.file_list = list_to_filename(glob.glob(template))
        return outputs

    def run(self, cwd=None):
        """Execute this module.
        """
        runtime = Bunch(returncode=0,
                        stdout=None,
                        stderr=None)
        outputs=self.aggregate_outputs()
        return InterfaceResult(deepcopy(self), runtime, outputs=outputs)

<|endoftext|>


<commit_before># Copyright (c) 2008 Sebastian Sareyko <smoon at nooms dot de>
# See COPYING file for details.

import ConfigParser
import os
import xmmsclient
from albumthing import AlbumThing

class Configuration:
    def __init__(self):
        self.__at = AlbumThing ()

        self.__cfg = ConfigParser.ConfigParser()
        self.__configuration_file = os.path.join(xmmsclient.userconfdir_get(),
                'clients', 'AlbumThing.ini')
        try:
            fd = open (self.__configuration_file, 'r')
            self.__cfg.readfp(fd)
        except IOError:
            self.__cfg.add_section('common')
            self.__cfg.set('common', 'start_xmms2d', '0')
            self.__cfg.add_section('ui')
            self.__cfg.set('ui', 'show_cover_art', '1')
            self.__cfg.set('ui', 'show_alternative_cover_art', '0')
            self.__cfg.set('ui', 'combine_va_albums', '0')
            self.__cfg.add_section('win')
            self.__cfg.set('win', 'width', 500)
            self.__cfg.set('win', 'height', 600)
            self.__cfg.set('win', 'pos_x', 0)
            self.__cfg.set('win', 'pos_y', 0)
            self.__cfg.set('win', 'pos_hpaned', 160)

    def get(self, section, var):
        try:
            ret = self.__cfg.get (section, var)
            if ret == '0':
                ret = 0
        except:
            ret = None

        return ret

    def set(self, section, var, value):
        self.__cfg.set(section, var, value)

    def save(self):
        try:
            self.__cfg.add_section('win')
        except ConfigParser.DuplicateSectionError:
            pass
        self.__cfg.set('win', 'width', self.__at.win.get_size()[0])
        self.__cfg.set('win', 'height', self.__at.win.get_size()[1])
        self.__cfg.set('win', 'pos_x', self.__at.win.get_position()[0])
        self.__cfg.set('win', 'pos_y', self.__at.win.get_position()[1])
        self.__cfg.set('win', 'pos_hpaned', self.__at.win.hpaned.get_position())
        try:
            fd = open (self.__configuration_file, 'w')
            self.__cfg.write(fd)
        except IOError:
            pass
<commit_msg>Configuration stuff fixed<commit_after># Copyright (c) 2008 Sebastian Sareyko <smoon at nooms dot de>
# See COPYING file for details.

import ConfigParser
import os
import xmmsclient
from albumthing import AlbumThing

class Configuration:
    def __init__(self):
        self.__at = AlbumThing ()

        self.__cfg = ConfigParser.ConfigParser()
        self.__configuration_file = os.path.join(xmmsclient.userconfdir_get(),
                'clients', 'AlbumThing.ini')
        try:
            fd = open (self.__configuration_file, 'r')
            self.__cfg.readfp(fd)
        except IOError:
            self.__cfg.add_section('common')
            self.__cfg.set('common', 'start_xmms2d', '0')
            self.__cfg.add_section('ui')
            self.__cfg.set('ui', 'show_cover_art', '1')
            self.__cfg.set('ui', 'show_alternative_cover_art', '0')
            self.__cfg.set('ui', 'combine_va_albums', '0')
            self.__cfg.add_section('win')
            self.__cfg.set('win', 'width', '500')
            self.__cfg.set('win', 'height', '600')
            self.__cfg.set('win', 'pos_x', '0')
            self.__cfg.set('win', 'pos_y', '0')
            self.__cfg.set('win', 'pos_hpaned', '160')

    def get(self, section, var):
        try:
            ret = self.__cfg.get (section, var)
            if ret == '0':
                ret = 0
        except:
            ret = None

        return ret

    def set(self, section, var, value):
        self.__cfg.set(section, var, value)

    def save(self):
        try:
            self.__cfg.add_section('win')
        except ConfigParser.DuplicateSectionError:
            pass
        self.__cfg.set('win', 'width', self.__at.win.get_size()[0])
        self.__cfg.set('win', 'height', self.__at.win.get_size()[1])
        self.__cfg.set('win', 'pos_x', self.__at.win.get_position()[0])
        self.__cfg.set('win', 'pos_y', self.__at.win.get_position()[1])
        self.__cfg.set('win', 'pos_hpaned', self.__at.win.hpaned.get_position())
        try:
            fd = open (self.__configuration_file, 'w')
            self.__cfg.write(fd)
        except IOError:
            pass
<|endoftext|>


<commit_before>#!/usr/bin/env python3

"""This file is part of DING0, the DIstribution Network GeneratOr.
DING0 is a tool to generate synthetic medium and low voltage power
distribution grids based on open data.

It is developed in the project open_eGo: https://openegoproject.wordpress.com

DING0 lives at github: https://github.com/openego/ding0/
The documentation is available on RTD: http://ding0.readthedocs.io

Notes
-----

This example file assumes you have already run the example file
`example_multiple_grid_districts.py` and use the option to save the `nd` object to
disc. If the example script was executed in PWD, do not change `base_path`
below.
"""

__copyright__ = "Reiner Lemoine Institut gGmbH"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/ding0/blob/master/LICENSE"
__author__ = "nesnoj, gplssm"

from ding0.tools import results
from ding0.tools.logger import get_default_home_dir
import os

BASEPATH = get_default_home_dir()

def ding0_exemplary_plots(stats, base_path=BASEPATH):
    """
    Analyze multiple grid district data generated with Ding0

    Parameters
    ----------
    stats : pandas.DataFrame
        Statistics of each MV grid districts
    base_path : str
        Root directory of Ding0 data structure, i.e. '~/.ding0' (which is
        default).
    """

    # make some plot
    plotpath = os.path.join(base_path, 'plots')
    results.plot_cable_length(stats, plotpath)
    results.plot_generation_over_load(stats, plotpath)
    results.plot_km_cable_vs_line(stats, plotpath)

def nd_load_and_stats(filenames, base_path=BASEPATH):
    """
    Load multiple files from disk and generate stats

    Passes the list of files assuming the ding0 data structure as default in
    :code:`~/.ding0`.
    Data will concatenated and key indicators for each grid district are
    returned in table and graphic format

    Parameters
    ----------
    filenames : list of str
        Provide list of files you want to analyze
    base_path : str
        Root directory of Ding0 data structure, i.e. '~/.ding0' (which is
        default).
    Returns
    -------
    stats : pandas.DataFrame
        Statistics of each MV grid districts
    """

    # load Ding0 data
    nds = []
    for filename in filenames:
        try:
            nd_load = results.load_nd_from_pickle(filename=
                                                  os.path.join(base_path,
                                                               'results',
                                                               filename))

            nds.append(nd_load)
        except:
            print("File {mvgd} not found. It was maybe excluded by Ding0 or "
                  "just forgotten to generate by you...".format(mvgd=filename))

    nd = nds[0]

    for n in nds[1:]:
        nd.add_mv_grid_district(n._mv_grid_districts[0])

    # get statistical numbers about grid
    stats = results.calculate_mvgd_stats(nd)

    # TODO: correct LV peak load/ generation capacity. Same in all LV GD
    return stats

if __name__ == '__main__':
    base_path = BASEPATH

    mv_grid_districts = list(range(1729, 1732))

    filenames = ["ding0_grids__{ext}.pkl".format(ext=_)
                 for _ in mv_grid_districts]

    # load files and generate statistical number about each mv grid district
    stats = nd_load_and_stats(filenames)

    # save stats file to disc
    stats.to_csv(os.path.join(base_path, 'results',
                              'ding0_grids_stats_{first}-{last}'.format(
                                  first=mv_grid_districts[0],
                                  last=mv_grid_districts[-1])))

    # load stats from file
    # stats = pd.read_csv(os.path.join(base_path, 'results',
    #                             'ding0_grids_stats_{first}-{last}'.format(
    #                                 first=mv_grid_districts[0],
    #                                 last=mv_grid_districts[-1])))

    # make some plots to compare grid districts
    ding0_exemplary_plots(stats)
<commit_msg>Changed search location of pkl files<commit_after>#!/usr/bin/env python3

"""This file is part of DING0, the DIstribution Network GeneratOr.
DING0 is a tool to generate synthetic medium and low voltage power
distribution grids based on open data.

It is developed in the project open_eGo: https://openegoproject.wordpress.com

DING0 lives at github: https://github.com/openego/ding0/
The documentation is available on RTD: http://ding0.readthedocs.io

Notes
-----

This example file assumes you have already run the example file
`example_multiple_grid_districts.py` and use the option to save the `nd` object to
disc. If the example script was executed in PWD, do not change `base_path`
below.
"""

__copyright__ = "Reiner Lemoine Institut gGmbH"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/ding0/blob/master/LICENSE"
__author__ = "nesnoj, gplssm"

from ding0.tools import results
from ding0.tools.logger import get_default_home_dir
import os

BASEPATH = get_default_home_dir()

def ding0_exemplary_plots(stats, base_path=BASEPATH):
    """
    Analyze multiple grid district data generated with Ding0

    Parameters
    ----------
    stats : pandas.DataFrame
        Statistics of each MV grid districts
    base_path : str
        Root directory of Ding0 data structure, i.e. '~/.ding0' (which is
        default).
    """

    # make some plot
    plotpath = os.path.join(base_path, 'plots')
    results.plot_cable_length(stats, plotpath)
    results.plot_generation_over_load(stats, plotpath)
    results.plot_km_cable_vs_line(stats, plotpath)

def nd_load_and_stats(filenames, base_path=BASEPATH):
    """
    Load multiple files from disk and generate stats

    Passes the list of files assuming the ding0 data structure as default in
    :code:`~/.ding0`.
    Data will concatenated and key indicators for each grid district are
    returned in table and graphic format

    Parameters
    ----------
    filenames : list of str
        Provide list of files you want to analyze
    base_path : str
        Root directory of Ding0 data structure, i.e. '~/.ding0' (which is
        default).
    Returns
    -------
    stats : pandas.DataFrame
        Statistics of each MV grid districts
    """

    # load Ding0 data
    nds = []
    for filename in filenames:
        try:
            nd_load = results.load_nd_from_pickle(filename=
                                                  os.path.join(base_path,
                                                               'grids',
                                                               filename))

            nds.append(nd_load)
        except:
            print("File {mvgd} not found. It was maybe excluded by Ding0 or "
                  "just forgotten to generate by you...".format(mvgd=filename))

    nd = nds[0]

    for n in nds[1:]:
        nd.add_mv_grid_district(n._mv_grid_districts[0])

    # get statistical numbers about grid
    stats = results.calculate_mvgd_stats(nd)

    # TODO: correct LV peak load/ generation capacity. Same in all LV GD
    return stats

if __name__ == '__main__':
    base_path = BASEPATH

    mv_grid_districts = list(range(1729, 1732))

    filenames = ["ding0_grids__{ext}.pkl".format(ext=_)
                 for _ in mv_grid_districts]

    # load files and generate statistical number about each mv grid district
    stats = nd_load_and_stats(filenames)

    # save stats file to disc
    stats.to_csv(os.path.join(base_path, 'results',
                              'ding0_grids_stats_{first}-{last}'.format(
                                  first=mv_grid_districts[0],
                                  last=mv_grid_districts[-1])))

    # load stats from file
    # stats = pd.read_csv(os.path.join(base_path, 'results',
    #                             'ding0_grids_stats_{first}-{last}'.format(
    #                                 first=mv_grid_districts[0],
    #                                 last=mv_grid_districts[-1])))

    # make some plots to compare grid districts
    ding0_exemplary_plots(stats)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function, unicode_literals

"""
Using Classifier and String Features
========================================

This is a famous `shogun` classifier example that predicts family name
of Shogun from his first name.
"""

from jubakit.classifier import Classifier, Schema, Dataset, Config
from jubakit.loader.csv import CSVLoader

# Load the shogun dataset.
train_loader = CSVLoader('shogun.train.csv')
test_loader = CSVLoader('shogun.test.csv')

# Define a Schema that defines types for each columns of the CSV file.
schema = Schema({
  'family_name': Schema.LABEL,
  'first_name': Schema.STRING,
})

# Create a Dataset.
train_dataset = Dataset(train_loader, schema).shuffle()
test_dataset = Dataset(test_loader, schema)

# Create a Classifier Service.
cfg = Config(method='PA', parameter={})
cfg['converter']['string_rules'] = [{'key': 'first_name', 'type': 'unigram', 'sample_weight': 'bin', 'global_weight': 'bin'}]
classifier = Classifier.run(cfg)

# Train the classifier.
for _ in classifier.train(train_dataset): pass

# Classify using the classifier.
for (idx, label, result) in classifier.classify(test_dataset):
  print("label: {0}, estimated: {1}".format(label, result[0][0]).encode('utf-8'))
<commit_msg>fix shogun example to show usage of Dataset.get<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, print_function, unicode_literals

"""
Using Classifier and String Features
========================================

This is a famous `shogun` classifier example that predicts family name
of Shogun from his first name.
"""

from jubakit.classifier import Classifier, Schema, Dataset, Config
from jubakit.loader.csv import CSVLoader

# Load the shogun dataset.
train_loader = CSVLoader('shogun.train.csv')
test_loader = CSVLoader('shogun.test.csv')

# Define a Schema that defines types for each columns of the CSV file.
schema = Schema({
  'family_name': Schema.LABEL,
  'first_name': Schema.STRING,
})

# Create a Dataset.
train_dataset = Dataset(train_loader, schema).shuffle()
test_dataset = Dataset(test_loader, schema)

# Create a Classifier Service.
cfg = Config(method='PA', parameter={})
cfg['converter']['string_rules'] = [{'key': 'first_name', 'type': 'unigram', 'sample_weight': 'bin', 'global_weight': 'bin'}]
classifier = Classifier.run(cfg)

# Train the classifier.
for _ in classifier.train(train_dataset): pass

# Classify using the classifier.
for (idx, label, result) in classifier.classify(test_dataset):
  true_family_name = label
  pred_family_name = result[0][0]
  first_name = test_dataset.get(idx)['first_name']
  print("{0} {1} ({2})".format(
    pred_family_name,
    first_name,
    'correct!' if pred_family_name == true_family_name else 'incorrect'
  ).encode('utf-8'))
<|endoftext|>