<commit_before># Author: Julien Coux
# Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, models

class TrialBalanceXslx(models.AbstractModel):
    _name = "report.a_f_r.report_trial_balance_xlsx"
    _description = "Trial Balance XLSX Report"
    _inherit = "report.account_financial_report.abstract_report_xlsx"

    def _get_report_name(self, report, data=False):
        company_id = data.get("company_id", False)
        report_name = _("Trial Balance")
        if company_id:
            company = self.env["res.company"].browse(company_id)
            suffix = " - {} - {}".format(company.name, company.currency_id.name)
            report_name = report_name + suffix
        return report_name

    def _get_report_columns(self, report):
        if not report.show_partner_details:
            res = {
                0: {"header": _("Code"), "field": "code", "width": 10},
                1: {"header": _("Account"), "field": "name", "width": 60},
                2: {
                    "header": _("Initial balance"),
                    "field": "initial_balance",
                    "type": "amount",
                    "width": 14,
                },
                3: {
                    "header": _("Debit"),
                    "field": "debit",
                    "type": "amount",
                    "width": 14,
                },
                4: {
                    "header": _("Credit"),
                    "field": "credit",
                    "type": "amount",
                    "width": 14,
                },
                5: {
                    "header": _("Period balance"),
                    "field": "balance",
                    "type": "amount",
                    "width": 14,
                },
                6: {
                    "header": _("Ending balance"),
                    "field": "ending_balance",
                    "type": "amount",
                    "width": 14,
                },
            }
            if report.foreign_currency:
                foreign_currency = {
                    7: {
                        "header": _("Cur."),
                        "field": "currency_id",
                        "field_currency_balance": "currency_id",
                        "type": "many2one",
                        "width": 7,
                    },
                    8: {
                        "header": _("Initial balance"),
                        "field": "initial_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                    9: {
                        "header": _("Ending balance"),
                        "field": "ending_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                }
                res = {**res, **foreign_currency}
            return res
        else:
            res = {
                0: {"header": _("Partner"), "field": "name", "width": 70},
                1: {
                    "header": _("Initial balance"),
                    "field": "initial_balance",
                    "type": "amount",
                    "width": 14,
                },
                2: {
                    "header": _("Debit"),
                    "field": "debit",
                    "type": "amount",
                    "width": 14,
                },
                3: {
                    "header": _("Credit"),
                    "field": "credit",
                    "type": "amount",
                    "width": 14,
                },
                4: {
                    "header": _("Period balance"),
                    "field": "balance",
                    "type": "amount",
                    "width": 14,
                },
                5: {
                    "header": _("Ending balance"),
                    "field": "ending_balance",
                    "type": "amount",
                    "width": 14,
                },
            }
            if report.foreign_currency:
                foreign_currency = {
                    6: {
                        "header": _("Cur."),
                        "field": "currency_id",
                        "field_currency_balance": "currency_id",
                        "type": "many2one",
                        "width": 7,
                    },
                    7: {
                        "header": _("Initial balance"),
                        "field": "initial_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                    8: {
                        "header": _("Ending balance"),
                        "field": "ending_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                }
                res = {**res, **foreign_currency}
            return res

    def _get_report_filters(self, report):
        return [
            [
                _("Date range filter"),
                _("From: %s To: %s") % (report.date_from, report.date_to),
            ],
            [
                _("Target moves filter"),
                _("All posted entries")
                if report.target_move == "all"
                else _("All entries"),
            ],
            [
                _("Account at 0 filter"),
                _("Hide") if report.hide_account_at_0 else _("Show"),
            ],
            [
                _("Show foreign currency"),
                _("Yes") if report.foreign_currency else _("No"),
            ],
            [
                _("Limit hierarchy levels"),
                _("Level %s" % report.show_hierarchy_level)
                if report.limit_hierarchy_level
                else _("No limit"),
            ],
        ]

    def _get_col_count_filter_name(self):
        return 2

    def _get_col_count_filter_value(self):
        return 3

    def _generate_report_content(self, workbook, report, data):
        res_data = self.env[
            "report.account_financial_report.trial_balance"
        ]._get_report_values(report, data)
        trial_balance = res_data["trial_balance"]
        total_amount = res_data["total_amount"]
        partners_data = res_data["partners_data"]
        accounts_data = res_data["accounts_data"]
        hierarchy_on = res_data["hierarchy_on"]
        show_partner_details = res_data["show_partner_details"]
        show_hierarchy_level = res_data["show_hierarchy_level"]
        foreign_currency = res_data["foreign_currency"]
        limit_hierarchy_level = res_data["limit_hierarchy_level"]
        if not show_partner_details:
            # Display array header for account lines
            self.write_array_header()

        # For each account
        if not show_partner_details:
            for balance in trial_balance:
                if hierarchy_on == "relation":
                    if limit_hierarchy_level:
                        if show_hierarchy_level > balance["level"]:
                            # Display account lines
                            self.write_line_from_dict(balance)
                    else:
                        self.write_line_from_dict(balance)
                elif hierarchy_on == "computed":
                    if balance["type"] == "account_type":
                        if limit_hierarchy_level:
                            if show_hierarchy_level > balance["level"]:
                                # Display account lines
                                self.write_line_from_dict(balance)
                        else:
                            self.write_line_from_dict(balance)
                else:
                    self.write_line_from_dict(balance)
        else:
            for account_id in total_amount:
                # Write account title
                self.write_array_title(
                    accounts_data[account_id]["code"]
                    + "- "
                    + accounts_data[account_id]["name"]
                )
                # Display array header for partner lines
                self.write_array_header()

                # For each partner
                for partner_id in total_amount[account_id]:
                    if isinstance(partner_id, int):
                        # Display partner lines
                        self.write_line_from_dict_order(
                            total_amount[account_id][partner_id],
                            partners_data[partner_id],
                        )

                # Display account footer line
                accounts_data[account_id].update(
                    {
                        "initial_balance": total_amount[account_id]["initial_balance"],
                        "credit": total_amount[account_id]["credit"],
                        "debit": total_amount[account_id]["debit"],
                        "balance": total_amount[account_id]["balance"],
                        "ending_balance": total_amount[account_id]["ending_balance"],
                    }
                )
                if foreign_currency:
                    accounts_data[account_id].update(
                        {
                            "initial_currency_balance": total_amount[account_id][
                                "initial_currency_balance"
                            ],
                            "ending_currency_balance": total_amount[account_id][
                                "ending_currency_balance"
                            ],
                        }
                    )
                self.write_account_footer(
                    accounts_data[account_id],
                    accounts_data[account_id]["code"]
                    + "- "
                    + accounts_data[account_id]["name"],
                )

                # Line break
                self.row_pos += 2

    def write_line_from_dict_order(self, total_amount, partner_data):
        total_amount.update({"name": str(partner_data["name"])})
        self.write_line_from_dict(total_amount)

    def write_line(self, line_object, type_object):
        """Write a line on current line using all defined columns field name.
        Columns are defined with `_get_report_columns` method.
        """
        if type_object == "partner":
            line_object.currency_id = line_object.report_account_id.currency_id
        elif type_object == "account":
            line_object.currency_id = line_object.currency_id
        super(TrialBalanceXslx, self).write_line(line_object)

    def write_account_footer(self, account, name_value):
        """Specific function to write account footer for Trial Balance"""
        format_amt = self._get_currency_amt_header_format_dict(account)
        for col_pos, column in self.columns.items():
            if column["field"] == "name":
                value = name_value
            else:
                value = account[column["field"]]
            cell_type = column.get("type", "string")
            if cell_type == "string":
                self.sheet.write_string(
                    self.row_pos, col_pos, value or "", self.format_header_left
                )
            elif cell_type == "amount":
                self.sheet.write_number(
                    self.row_pos, col_pos, float(value), self.format_header_amount
                )
            elif cell_type == "many2one" and account["currency_id"]:
                self.sheet.write_string(
                    self.row_pos, col_pos, value.name or "", self.format_header_right
                )
            elif cell_type == "amount_currency" and account["currency_id"]:
                self.sheet.write_number(self.row_pos, col_pos, float(value), format_amt)
            else:
                self.sheet.write_string(
                    self.row_pos, col_pos, "", self.format_header_right
                )
        self.row_pos += 1
<commit_msg>[account_financial_report][fix] display correct target_move in Trial Balance XLSX<commit_after># Author: Julien Coux
# Copyright 2016 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, models

class TrialBalanceXslx(models.AbstractModel):
    _name = "report.a_f_r.report_trial_balance_xlsx"
    _description = "Trial Balance XLSX Report"
    _inherit = "report.account_financial_report.abstract_report_xlsx"

    def _get_report_name(self, report, data=False):
        company_id = data.get("company_id", False)
        report_name = _("Trial Balance")
        if company_id:
            company = self.env["res.company"].browse(company_id)
            suffix = " - {} - {}".format(company.name, company.currency_id.name)
            report_name = report_name + suffix
        return report_name

    def _get_report_columns(self, report):
        if not report.show_partner_details:
            res = {
                0: {"header": _("Code"), "field": "code", "width": 10},
                1: {"header": _("Account"), "field": "name", "width": 60},
                2: {
                    "header": _("Initial balance"),
                    "field": "initial_balance",
                    "type": "amount",
                    "width": 14,
                },
                3: {
                    "header": _("Debit"),
                    "field": "debit",
                    "type": "amount",
                    "width": 14,
                },
                4: {
                    "header": _("Credit"),
                    "field": "credit",
                    "type": "amount",
                    "width": 14,
                },
                5: {
                    "header": _("Period balance"),
                    "field": "balance",
                    "type": "amount",
                    "width": 14,
                },
                6: {
                    "header": _("Ending balance"),
                    "field": "ending_balance",
                    "type": "amount",
                    "width": 14,
                },
            }
            if report.foreign_currency:
                foreign_currency = {
                    7: {
                        "header": _("Cur."),
                        "field": "currency_id",
                        "field_currency_balance": "currency_id",
                        "type": "many2one",
                        "width": 7,
                    },
                    8: {
                        "header": _("Initial balance"),
                        "field": "initial_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                    9: {
                        "header": _("Ending balance"),
                        "field": "ending_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                }
                res = {**res, **foreign_currency}
            return res
        else:
            res = {
                0: {"header": _("Partner"), "field": "name", "width": 70},
                1: {
                    "header": _("Initial balance"),
                    "field": "initial_balance",
                    "type": "amount",
                    "width": 14,
                },
                2: {
                    "header": _("Debit"),
                    "field": "debit",
                    "type": "amount",
                    "width": 14,
                },
                3: {
                    "header": _("Credit"),
                    "field": "credit",
                    "type": "amount",
                    "width": 14,
                },
                4: {
                    "header": _("Period balance"),
                    "field": "balance",
                    "type": "amount",
                    "width": 14,
                },
                5: {
                    "header": _("Ending balance"),
                    "field": "ending_balance",
                    "type": "amount",
                    "width": 14,
                },
            }
            if report.foreign_currency:
                foreign_currency = {
                    6: {
                        "header": _("Cur."),
                        "field": "currency_id",
                        "field_currency_balance": "currency_id",
                        "type": "many2one",
                        "width": 7,
                    },
                    7: {
                        "header": _("Initial balance"),
                        "field": "initial_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                    8: {
                        "header": _("Ending balance"),
                        "field": "ending_currency_balance",
                        "type": "amount_currency",
                        "width": 14,
                    },
                }
                res = {**res, **foreign_currency}
            return res

    def _get_report_filters(self, report):
        return [
            [
                _("Date range filter"),
                _("From: %s To: %s") % (report.date_from, report.date_to),
            ],
            [
                _("Target moves filter"),
                _("All posted entries")
                if report.target_move == "posted"
                else _("All entries"),
            ],
            [
                _("Account at 0 filter"),
                _("Hide") if report.hide_account_at_0 else _("Show"),
            ],
            [
                _("Show foreign currency"),
                _("Yes") if report.foreign_currency else _("No"),
            ],
            [
                _("Limit hierarchy levels"),
                _("Level %s" % report.show_hierarchy_level)
                if report.limit_hierarchy_level
                else _("No limit"),
            ],
        ]

    def _get_col_count_filter_name(self):
        return 2

    def _get_col_count_filter_value(self):
        return 3

    def _generate_report_content(self, workbook, report, data):
        res_data = self.env[
            "report.account_financial_report.trial_balance"
        ]._get_report_values(report, data)
        trial_balance = res_data["trial_balance"]
        total_amount = res_data["total_amount"]
        partners_data = res_data["partners_data"]
        accounts_data = res_data["accounts_data"]
        hierarchy_on = res_data["hierarchy_on"]
        show_partner_details = res_data["show_partner_details"]
        show_hierarchy_level = res_data["show_hierarchy_level"]
        foreign_currency = res_data["foreign_currency"]
        limit_hierarchy_level = res_data["limit_hierarchy_level"]
        if not show_partner_details:
            # Display array header for account lines
            self.write_array_header()

        # For each account
        if not show_partner_details:
            for balance in trial_balance:
                if hierarchy_on == "relation":
                    if limit_hierarchy_level:
                        if show_hierarchy_level > balance["level"]:
                            # Display account lines
                            self.write_line_from_dict(balance)
                    else:
                        self.write_line_from_dict(balance)
                elif hierarchy_on == "computed":
                    if balance["type"] == "account_type":
                        if limit_hierarchy_level:
                            if show_hierarchy_level > balance["level"]:
                                # Display account lines
                                self.write_line_from_dict(balance)
                        else:
                            self.write_line_from_dict(balance)
                else:
                    self.write_line_from_dict(balance)
        else:
            for account_id in total_amount:
                # Write account title
                self.write_array_title(
                    accounts_data[account_id]["code"]
                    + "- "
                    + accounts_data[account_id]["name"]
                )
                # Display array header for partner lines
                self.write_array_header()

                # For each partner
                for partner_id in total_amount[account_id]:
                    if isinstance(partner_id, int):
                        # Display partner lines
                        self.write_line_from_dict_order(
                            total_amount[account_id][partner_id],
                            partners_data[partner_id],
                        )

                # Display account footer line
                accounts_data[account_id].update(
                    {
                        "initial_balance": total_amount[account_id]["initial_balance"],
                        "credit": total_amount[account_id]["credit"],
                        "debit": total_amount[account_id]["debit"],
                        "balance": total_amount[account_id]["balance"],
                        "ending_balance": total_amount[account_id]["ending_balance"],
                    }
                )
                if foreign_currency:
                    accounts_data[account_id].update(
                        {
                            "initial_currency_balance": total_amount[account_id][
                                "initial_currency_balance"
                            ],
                            "ending_currency_balance": total_amount[account_id][
                                "ending_currency_balance"
                            ],
                        }
                    )
                self.write_account_footer(
                    accounts_data[account_id],
                    accounts_data[account_id]["code"]
                    + "- "
                    + accounts_data[account_id]["name"],
                )

                # Line break
                self.row_pos += 2

    def write_line_from_dict_order(self, total_amount, partner_data):
        total_amount.update({"name": str(partner_data["name"])})
        self.write_line_from_dict(total_amount)

    def write_line(self, line_object, type_object):
        """Write a line on current line using all defined columns field name.
        Columns are defined with `_get_report_columns` method.
        """
        if type_object == "partner":
            line_object.currency_id = line_object.report_account_id.currency_id
        elif type_object == "account":
            line_object.currency_id = line_object.currency_id
        super(TrialBalanceXslx, self).write_line(line_object)

    def write_account_footer(self, account, name_value):
        """Specific function to write account footer for Trial Balance"""
        format_amt = self._get_currency_amt_header_format_dict(account)
        for col_pos, column in self.columns.items():
            if column["field"] == "name":
                value = name_value
            else:
                value = account[column["field"]]
            cell_type = column.get("type", "string")
            if cell_type == "string":
                self.sheet.write_string(
                    self.row_pos, col_pos, value or "", self.format_header_left
                )
            elif cell_type == "amount":
                self.sheet.write_number(
                    self.row_pos, col_pos, float(value), self.format_header_amount
                )
            elif cell_type == "many2one" and account["currency_id"]:
                self.sheet.write_string(
                    self.row_pos, col_pos, value.name or "", self.format_header_right
                )
            elif cell_type == "amount_currency" and account["currency_id"]:
                self.sheet.write_number(self.row_pos, col_pos, float(value), format_amt)
            else:
                self.sheet.write_string(
                    self.row_pos, col_pos, "", self.format_header_right
                )
        self.row_pos += 1
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wger Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager.  If not, see <http://www.gnu.org/licenses/>.

import datetime
import logging
import json

from django.template import RequestContext
from django.http import HttpResponse
from django.shortcuts import render_to_response

from django.utils.translation import ugettext as _

from wger.nutrition.forms import BmiForm
from wger.weight.models import WeightEntry

logger = logging.getLogger('wger.custom')

'''
BMI views
'''

def view(request):
    '''
    The BMI calculator detail page
    '''

    context = {}
    form_data = {'height': request.user.userprofile.height,
                 'weight': request.user.userprofile.weight}
    context['form'] = BmiForm(initial=form_data)
    return render_to_response('bmi/form.html',
                              context,
                              context_instance=RequestContext(request))

def calculate(request):
    '''
    Calculates the BMI
    '''

    data = []

    form = BmiForm(request.POST, instance=request.user.userprofile)
    if form.is_valid():
        form.save()

        # Create a new weight entry as needed
        if (not WeightEntry.objects.all().exists()
           or (datetime.date.today() - WeightEntry.objects.all().latest().creation_date
               > datetime.timedelta(1))):
            entry = WeightEntry()
            entry.weight = form.cleaned_data['weight']
            entry.user = request.user
            entry.creation_date = datetime.date.today()
            entry.save()

        # Update the last entry
        else:
            entry = WeightEntry.objects.all().latest()
            entry.weight = form.cleaned_data['weight']
            entry.save()

        bmi = request.user.userprofile.calculate_bmi()
        result = {'bmi': '{0:.2f}'.format(bmi),
                  'weight': form.cleaned_data['weight'],
                  'height': request.user.userprofile.height}
        data = json.dumps(result)

    # Return the results to the client
    return HttpResponse(data, 'application/json')

def chart_data(request):
    '''
    Returns the data to render the BMI chart
    '''

    data = json.dumps([
        {'key': 'Group1', 'height':160, 'weight': 40},
        {'key': 'Group1', 'height':200, 'weight': 65},
        {'key': 'Group2', 'height':160, 'weight': 44},
        {'key': 'Group2', 'height':200, 'weight': 68},
        {'key': 'Group3', 'height':160, 'weight': 48},
        {'key': 'Group3', 'height':200, 'weight': 72},
        {'key': 'Group4', 'height':160, 'weight': 63},
        {'key': 'Group4', 'height':200, 'weight': 100},
        {'key': 'Group5', 'height':160, 'weight': 76},
        {'key': 'Group5', 'height':200, 'weight': 120},
        {'key': 'Group6', 'height':160, 'weight': 88},
        {'key': 'Group6', 'height':187, 'weight': 120},
        {'key': 'Group7', 'height':160, 'weight': 102},
        {'key': 'Group7', 'height':175, 'weight': 120}
        ])

    # Return the results to the client
    return HttpResponse(data, 'application/json')<commit_msg>Correctly retrieve the latest weight entry, fix JSON bug<commit_after># -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wger Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager.  If not, see <http://www.gnu.org/licenses/>.

import datetime
import logging
import json

from django.template import RequestContext
from django.http import HttpResponse
from django.shortcuts import render_to_response

from django.utils.translation import ugettext as _

from wger.nutrition.forms import BmiForm
from wger.weight.models import WeightEntry
from wger.utils import helpers

logger = logging.getLogger('wger.custom')

'''
BMI views
'''

def view(request):
    '''
    The BMI calculator detail page
    '''

    context = {}
    form_data = {'height': request.user.userprofile.height,
                 'weight': request.user.userprofile.weight}
    context['form'] = BmiForm(initial=form_data)
    return render_to_response('bmi/form.html',
                              context,
                              context_instance=RequestContext(request))

def calculate(request):
    '''
    Calculates the BMI
    '''

    data = []

    form = BmiForm(request.POST, instance=request.user.userprofile)
    if form.is_valid():
        form.save()

        # Create a new weight entry as needed
        if (not WeightEntry.objects.all().exists()
           or (datetime.date.today() - WeightEntry.objects.filter(user=request.user).latest().creation_date
               > datetime.timedelta(1))):
            entry = WeightEntry()
            entry.weight = form.cleaned_data['weight']
            entry.user = request.user
            entry.creation_date = datetime.date.today()
            entry.save()

        # Update the last entry
        else:
            entry = WeightEntry.objects.filter(user=request.user).latest()
            entry.weight = form.cleaned_data['weight']
            entry.save()

        bmi = request.user.userprofile.calculate_bmi()
        result = {'bmi': '{0:.2f}'.format(bmi),
                  'weight': form.cleaned_data['weight'],
                  'height': request.user.userprofile.height}
        data = json.dumps(result, cls=helpers.DecimalJsonEncoder)

    # Return the results to the client
    return HttpResponse(data, 'application/json')

def chart_data(request):
    '''
    Returns the data to render the BMI chart
    '''

    data = json.dumps([
        {'key': 'Group1', 'height':160, 'weight': 40},
        {'key': 'Group1', 'height':200, 'weight': 65},
        {'key': 'Group2', 'height':160, 'weight': 44},
        {'key': 'Group2', 'height':200, 'weight': 68},
        {'key': 'Group3', 'height':160, 'weight': 48},
        {'key': 'Group3', 'height':200, 'weight': 72},
        {'key': 'Group4', 'height':160, 'weight': 63},
        {'key': 'Group4', 'height':200, 'weight': 100},
        {'key': 'Group5', 'height':160, 'weight': 76},
        {'key': 'Group5', 'height':200, 'weight': 120},
        {'key': 'Group6', 'height':160, 'weight': 88},
        {'key': 'Group6', 'height':187, 'weight': 120},
        {'key': 'Group7', 'height':160, 'weight': 102},
        {'key': 'Group7', 'height':175, 'weight': 120}
        ])

    # Return the results to the client
    return HttpResponse(data, 'application/json')<|endoftext|>


<commit_before>#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import RemoteController, OVSKernelSwitch
from mininet.link import TCLink
from mininet.cli import CLI
# from mininet.term import makeTerm
from mininet.log import setLogLevel

"""
Handorver example (based on proposed scenario)

    Uses addresses:
    UAVs Mesh:  10.0.0.XXX/24
    Hosts: 10.0.1.XXX/24
        - 10.0.1.[1-9] - UAVs
        - 10.0.1.[91-99] - Hosts

"""

# Configurations
OF_CONTROLLER_IP = '192.168.56.1'
OF_CONTROLLER_PORT = 6633

# Number of UAVs
CONF_UAV_NUMBER = 9
CONF_GUARANI_NUMBER = 4

# Nodes placement
POS_SCALE = 35

# Distance between elements (UAV, vehicles...)
POS_GUARANI_DISTANCE = POS_SCALE / 3
POS_GUARANI_X = 100
POS_GUARANI_Y_START = POS_SCALE / 2

POS_UAV_FROM_GUARANI = POS_SCALE / 2
POS_UAV_DISTANCE = int(4.0 / (CONF_UAV_NUMBER - 1) * POS_SCALE)
POS_UAV_MOTION = 5

class OVSKernelSwitch13(OVSKernelSwitch):
    """
    Class for use of OpenFlow 1.3 (Inheritance)
    """

    def __init__(self, *args, **kwargs):
        OVSKernelSwitch.__init__(self, protocols='OpenFlow13', *args, **kwargs)

def uav_get_ybase():
    posy = POS_GUARANI_Y_START + \
        (CONF_GUARANI_NUMBER - 1) * POS_GUARANI_DISTANCE + \
        POS_UAV_FROM_GUARANI
    return int(round(posy))

def uav_get_xbase(uav_index):
    formula = (uav_index - (CONF_UAV_NUMBER + 1) / 2) * POS_UAV_DISTANCE
    posx = POS_GUARANI_X + formula
    return int(round(posx))

def topology():
    """
      Creates the network elements in Mininet
    """

    # List o UAVs (stations)
    uav_list = []
    # List of Switches and APs (Guaranis)
    sw_list = []
    ap_list = []
    print "*** Creates a remote controller"
    c1 = RemoteController('c1', ip=OF_CONTROLLER_IP, port=OF_CONTROLLER_PORT)

    print "*** Creating a network."
    net = Mininet(controller=c1,
                  link=TCLink,
                  switch=OVSKernelSwitch13)

    # # Creating N UAV (configured by CONF_UAV_NUMBER)
    print '*** Creating UAVs (Stations)'
    for i in range(CONF_UAV_NUMBER):
        print '    - Creating UAV {0:2d} of {1:2d}...'.format(i + 1,
                                                              CONF_UAV_NUMBER)
        sta_mac = '00:00:00:00:00:{0:02d}'.format(i + 1)
        sta_name = 'sta{0:d}'.format(i + 1)
        sta_ip = '10.0.0.{0:d}/24'.format(i + 1)
        # node position
        uav_position = '{0:d},{1:d},0'.format(
            uav_get_xbase(i + 1), uav_get_ybase() - 10)
        uav_min_x = uav_get_xbase(i + 1) - POS_UAV_MOTION
        uav_max_x = uav_get_xbase(i + 1) + POS_UAV_MOTION
        uav_min_y = uav_get_ybase() - POS_UAV_MOTION
        uav_max_y = uav_get_ybase() + POS_UAV_MOTION
        uav_list.append(net.addStation(sta_name, mac=sta_mac, ip=sta_ip,
                                       min_x=uav_min_x, max_x=uav_max_x,
                                       min_y=uav_min_y, max_y=uav_max_y,
                                       wlans=2, position=uav_position))

    # print '*** Creating UAVs (Stations)'
    # for uav in uav_list:
    #     print '    - Adding UAV {0:s} to WifiDirect'.format(uav.name)
    #     net.wifiDirect(uav)

    print "*** Creating static Guaranis (Switch + AP)"
    for i in xrange(1, CONF_GUARANI_NUMBER + 1):
        print '    - Creating Guarani (Switch + AP) {0:d} of {1:d}' \
            .format(i, CONF_GUARANI_NUMBER)
        sw_name = 's{0:d}'.format(i)
        sw_dpid = '000000000000000{0:d}'.format(i)
        sw_list.append(net.addSwitch(sw_name, dpid=sw_dpid))

        if i == CONF_GUARANI_NUMBER:
            ap_name = 'ap{0:d}'.format(i)
            ap_dpid = '000000000000100{0:d}'.format(i)
            ap_ssid = 'ssid_ap{0:d}'.format(i)
            print '      - {0:s} -> SSID: {1:s}'.format(ap_name, ap_ssid)
            ap_position = '{0:d},{1:d},0'.format(
                POS_GUARANI_X,
                POS_GUARANI_Y_START + ((i - 1) * POS_GUARANI_DISTANCE))
            ap_channel = [1, 6, 11][i % 3]

            ap_list.append(net.addBaseStation(ap_name, dpid=ap_dpid,
                                              ssid=ap_ssid, mode='g',
                                              channel=ap_channel,
                                              position=ap_position))
            # Link last created AP with last created Switch
            net.addLink(sw_list[-1], ap_list[-1])

    print "*** Creating links between Guaranis"
    for i in xrange(0, CONF_GUARANI_NUMBER - 1):
        print "    - sw{0:s} <=======> sw{1:s}".format(sw_list[i].name,
                                                       sw_list[i + 1].name)
        net.addLink(sw_list[i], sw_list[i + 1])

    h1 = net.addHost('h1', mac='00:00:00:00:01:91', ip='10.0.1.91/24')
    h2 = net.addHost('h2', mac='00:00:00:00:01:91', ip='10.0.1.92/24')
    """ Creating routes on hosts """
    net.addLink(h1, sw_list[0])
    net.addLink(h2, sw_list[-1])

    print '*** Adding Mesh network among Stations'
    for uav in uav_list:
        print '    - Adding UAV {0:s} to mesh network'.format(uav.name)
        net.addMesh(uav, ssid='meshNet')

    print '*** Starting network'
    net.build()
    c1.start()
    # Starting Switches
    for sw in sw_list:
        print '    - Starting {0:s}'.format(sw.name)
        sw.start([c1])
    # Starting APs
    for ap in ap_list:
        print '    - Starting {0:s}'.format(ap.name)
        ap.start([c1])

    print '**** Associating UAV5 to Access Point'
    uav_list[4].cmd('iwconfig sta5-wlan1 essid ssid_ap4')
    print '**** Configuring the IP address for UAV Wireless interface'
    uav_list[4].cmd('ifconfig sta5-wlan1 10.0.1.5/24')

    # Adding routes to hosts
    h1.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.1.5')
    h2.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.1.5')
    # Enabling the routing by node sta5
    uav_list[4].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 dev sta5-wlan1')
    uav_list[4].cmd('echo 1 > /proc/sys/net/ipv4/ip_forward')
    # Configuring staX routes
    uav_list[0].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[1].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[2].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[3].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    # uav_list[4] is the GATEWAY!!!
    uav_list[5].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[6].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[7].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[8].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')

    """uncomment to plot graph"""
    net.plotGraph(max_x=200, max_y=200)

    # Starting Mobility
    # net.seed(20)
    # net.startMobility(startTime=0, model='GaussMarkov', min_v=0.5, max_v=0.8)

    print '*** Running CLI'
    CLI(net)

    print '*** Stopping network'
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    topology()
<commit_msg>Adding video player and ffserver<commit_after>#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import RemoteController, OVSKernelSwitch
from mininet.link import TCLink
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.term import makeTerm

import time

"""
Handorver example (based on proposed scenario)

    Uses addresses:
    UAVs Mesh:  10.0.0.XXX/24
    Hosts: 10.0.1.XXX/24
        - 10.0.1.[1-9] - UAVs
        - 10.0.1.[91-99] - Hosts

"""

# Configurations
OF_CONTROLLER_IP = '192.168.56.1'
OF_CONTROLLER_PORT = 6633

# Number of UAVs
CONF_UAV_NUMBER = 9
CONF_GUARANI_NUMBER = 4

# Nodes placement
POS_SCALE = 35

# Distance between elements (UAV, vehicles...)
POS_GUARANI_DISTANCE = POS_SCALE / 3
POS_GUARANI_X = 100
POS_GUARANI_Y_START = POS_SCALE / 2

POS_UAV_FROM_GUARANI = POS_SCALE / 2
POS_UAV_DISTANCE = int(4.0 / (CONF_UAV_NUMBER - 1) * POS_SCALE)
POS_UAV_MOTION = 5

class OVSKernelSwitch13(OVSKernelSwitch):
    """
    Class for use of OpenFlow 1.3 (Inheritance)
    """

    def __init__(self, *args, **kwargs):
        OVSKernelSwitch.__init__(self, protocols='OpenFlow13', *args, **kwargs)

def uav_get_ybase():
    posy = POS_GUARANI_Y_START + \
        (CONF_GUARANI_NUMBER - 1) * POS_GUARANI_DISTANCE + \
        POS_UAV_FROM_GUARANI
    return int(round(posy))

def uav_get_xbase(uav_index):
    formula = (uav_index - (CONF_UAV_NUMBER + 1) / 2) * POS_UAV_DISTANCE
    posx = POS_GUARANI_X + formula
    return int(round(posx))

def runFFServer(net, host):

    pass

def topology():
    """
      Creates the network elements in Mininet
    """

    # List o UAVs (stations)
    uav_list = []
    # List of Switches and APs (Guaranis)
    sw_list = []
    ap_list = []
    print "*** Creates a remote controller"
    c1 = RemoteController('c1', ip=OF_CONTROLLER_IP, port=OF_CONTROLLER_PORT)

    print "*** Creating a network."
    net = Mininet(controller=c1,
                  link=TCLink,
                  switch=OVSKernelSwitch13)

    # # Creating N UAV (configured by CONF_UAV_NUMBER)
    print '*** Creating UAVs (Stations)'
    for i in range(CONF_UAV_NUMBER):
        print '    - Creating UAV {0:2d} of {1:2d}...'.format(i + 1,
                                                              CONF_UAV_NUMBER)
        sta_mac = '00:00:00:00:00:{0:02d}'.format(i + 1)
        sta_name = 'sta{0:d}'.format(i + 1)
        sta_ip = '10.0.0.{0:d}/24'.format(i + 1)
        # node position
        uav_position = '{0:d},{1:d},0'.format(
            uav_get_xbase(i + 1), uav_get_ybase() - 10)
        uav_min_x = uav_get_xbase(i + 1) - POS_UAV_MOTION
        uav_max_x = uav_get_xbase(i + 1) + POS_UAV_MOTION
        uav_min_y = uav_get_ybase() - POS_UAV_MOTION
        uav_max_y = uav_get_ybase() + POS_UAV_MOTION
        uav_list.append(net.addStation(sta_name, mac=sta_mac, ip=sta_ip,
                                       min_x=uav_min_x, max_x=uav_max_x,
                                       min_y=uav_min_y, max_y=uav_max_y,
                                       wlans=2, position=uav_position))

    # print '*** Creating UAVs (Stations)'
    # for uav in uav_list:
    #     print '    - Adding UAV {0:s} to WifiDirect'.format(uav.name)
    #     net.wifiDirect(uav)

    print "*** Creating static Guaranis (Switch + AP)"
    for i in xrange(1, CONF_GUARANI_NUMBER + 1):
        print '    - Creating Guarani (Switch + AP) {0:d} of {1:d}' \
            .format(i, CONF_GUARANI_NUMBER)
        sw_name = 's{0:d}'.format(i)
        sw_dpid = '000000000000000{0:d}'.format(i)
        sw_list.append(net.addSwitch(sw_name, dpid=sw_dpid))

        if i == CONF_GUARANI_NUMBER:
            ap_name = 'ap{0:d}'.format(i)
            ap_dpid = '000000000000100{0:d}'.format(i)
            ap_ssid = 'ssid_ap{0:d}'.format(i)
            print '      - {0:s} -> SSID: {1:s}'.format(ap_name, ap_ssid)
            ap_position = '{0:d},{1:d},0'.format(
                POS_GUARANI_X,
                POS_GUARANI_Y_START + ((i - 1) * POS_GUARANI_DISTANCE))
            ap_channel = [1, 6, 11][i % 3]

            ap_list.append(net.addBaseStation(ap_name, dpid=ap_dpid,
                                              ssid=ap_ssid, mode='g',
                                              channel=ap_channel,
                                              position=ap_position))
            # Link last created AP with last created Switch
            net.addLink(sw_list[-1], ap_list[-1])

    print "*** Creating links between Guaranis"
    for i in xrange(0, CONF_GUARANI_NUMBER - 1):
        print "    - sw{0:s} <=======> sw{1:s}".format(sw_list[i].name,
                                                       sw_list[i + 1].name)
        net.addLink(sw_list[i], sw_list[i + 1])

    h1 = net.addHost('h1', mac='00:00:00:00:01:91', ip='10.0.1.91/24')
    h2 = net.addHost('h2', mac='00:00:00:00:01:91', ip='10.0.1.92/24')
    """ Creating routes on hosts """
    net.addLink(h1, sw_list[0])
    net.addLink(h2, sw_list[-1])

    print '*** Adding Mesh network among Stations'
    for uav in uav_list:
        print '    - Adding UAV {0:s} to mesh network'.format(uav.name)
        net.addMesh(uav, ssid='meshNet')

    print '*** Starting network'
    net.build()
    c1.start()
    # Starting Switches
    for sw in sw_list:
        print '    - Starting {0:s}'.format(sw.name)
        sw.start([c1])
    # Starting APs
    for ap in ap_list:
        print '    - Starting {0:s}'.format(ap.name)
        ap.start([c1])

    print '**** Associating UAV5 to Access Point'
    uav_list[4].cmd('iwconfig sta5-wlan1 essid ssid_ap4')
    print '**** Configuring the IP address for UAV Wireless interface'
    uav_list[4].cmd('ifconfig sta5-wlan1 10.0.1.5/24')

    # Adding routes to hosts
    h1.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.1.5')
    h2.cmd('route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.1.5')
    # Enabling the routing by node sta5
    uav_list[4].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 dev sta5-wlan1')
    uav_list[4].cmd('echo 1 > /proc/sys/net/ipv4/ip_forward')
    # Configuring staX routes
    uav_list[0].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[1].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[2].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[3].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    # uav_list[4] is the GATEWAY!!!
    uav_list[5].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[6].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[7].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')
    uav_list[8].cmd(
        'route add -net 10.0.1.0 netmask 255.255.255.0 gw 10.0.0.5')

    # """uncomment to plot graph"""
    # net.plotGraph(max_x=200, max_y=200)

    # Starting Mobility
    # net.seed(20)
    # net.startMobility(startTime=0, model='GaussMarkov', min_v=0.5, max_v=0.8)

    # Run FFServer and FFPlay on hosts
    # uav_list[7].popen("iperf -s -u")
    makeTerm(uav_list[7], title='FFServer', cmd='ffserver -d')
    # Wait for ffserver to initialize
    time.sleep(1)
    # Running ffplay clients with loglevel trace
    # makeTerm(h1, title='FFPlay running the video',
    #    cmd='ffplay -loglevel trace http://10.0.0.8:8090/movie480.ogg > log_h1.log 2>&1')
    # makeTerm(h2, title='Iperf client', cmd='')

    print '*** Running CLI'
    CLI(net)

    print '*** Stopping network'
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    topology()
<|endoftext|>


<commit_before>import re
import json
import struct
import ctypes
from queue import Queue
from functools import lru_cache
from collections import namedtuple, OrderedDict

base = (
    'D:\\stats_data\\cache\\saau\\sections\\'
    'geology\\48006_shp\\globalmap2001\\Raster\\elevation\\'
    'elscnf'
)
NUM_RE = re.compile(r'(\d*)')
STR_RE = re.compile(r'([A-Za-z0-9_ -]*)')
PUNC = set('{},:')

FORMATS = json.load(open('formats.json'))

class AUXFile(namedtuple('AUXFile', 'version,rootEntry,mifDictionary')):
    def __hash__(self):
        return hash(
            (
                self.rootEntry,
                self.mifDictionary
            )
        )

    def __eq__(self, other):
        return hash(self) == hash(other)

MIF_DATA_TYPES = {
    '1': 1,
    '2': 1,
    '4': 1,
    'C': 1,
    'c': 1,
    'e': 1,
    'S': 2,
    's': 2,
    't': 4,
    'L': 4,
    'l': 4,
    'i': 4,
    'f': 4,
    'd': 8,
    'm': 8,
    'M': 16,
    'b': 'dynamic',
    'o': 'dynamic',
    'x': 'dynamic'
}

class BadFormatString(Exception):
    pass

def nc(tokens):
    name = tokens.pop(0)
    comma = tokens.pop(0)
    assert comma == ','
    return name

def parse_to_struct(tokens):
    while tokens:
        token = tokens.pop(0)
        if token == '{':
            block = list(parse_to_struct(tokens))
            name = nc(tokens)

            yield (name, block)

        elif token == '}':
            return

        elif isinstance(token, int):
            length = token
            assert tokens.pop(0) == ':'
            name = tokens.pop(0)
            comma = tokens.pop(0)

            typ = determine_type(name)

            if typ == 'o':
                # okay, custom type
                type_name = name[1:]

                if type_name == 'Emif_String':
                    typ = '16c'
                else:
                    raise Exception

                yield (1, {'type': typ, 'name': nc(tokens)})

            elif typ == 'e':
                # okay, enum

                n_elems = int(name[1:])
                yield (
                    length,
                    {
                        "type": "c",
                        "values": [nc(tokens) for _ in range(n_elems)],
                        "name": nc(tokens)
                    }
                )

            else:
                assert comma == ','

                yield (length, {'type': typ, 'name': name[1:]})

        else:
            raise BadFormatString(token)

class Entry:
    def __init__(self, nxt, prev, parent, child, data, dataSize, name, typ,
                 modTime):
        self.nxt = nxt
        self.prev = prev
        self.parent = parent
        self.child = child
        self.data = data
        self.dataSize = dataSize
        self.name = name
        self.typ = typ
        self.modTime = modTime

    def __hash__(self):
        return hash(self.ptr)
        # return hash((
        #     self.ptr,
        #     self.next,
        #     self.prev,
        #     self.parent,
        #     self.child,
        #     self.data,
        #     self.dataSize,
        #     self.name,
        #     self.typ,
        #     self.modTime
        # ))

    def __eq__(self, other):
        return hash(self) == hash(other)

    __str__ = __repr__ = lambda self: '<Entry {} of type {}>'.format(
        self.name, self.typ
    )

def clean_string(string):
    return ctypes.create_string_buffer(b''.join(string)).value.decode()

class EntryParser:
    def __init__(self, fh, root_ptr):
        self.fh = fh
        self.root_ptr = root_ptr
        self.queue = Queue()
        self.seen = {}

    def parse(self):
        root = type('', (), {'child': None})()
        self.queue.put((root, 'child', self.root_ptr))
        while not self.queue.empty():
            dest, name, ptr = self.queue.get()
            setattr(dest, name, self.parse_entry(ptr, dest))
            if name == 'child':
                setattr(
                    getattr(dest, name),
                    'parent',
                    dest
                )
        root.child.parent = None
        return root.child

    def parse_entry(self, ptr, parent):
        if ptr in self.seen:
            return self.seen[ptr]

        self.fh.seek(ptr)

        (
            nxt, prev, _, child, data, dataSize,
            name, typ, modTime
        ) = struct.unpack('IIIIIl64s32sL', self.fh.read(124))

        self.fh.seek(data)
        data = self.fh.read(dataSize)

        name = self.clean_string(name)
        typ = self.clean_string(typ)

        ent = Entry(
            nxt=nxt or None,
            prev=prev or None,
            parent=parent,
            child=child or None,
            data=data,
            dataSize=dataSize,
            name=name,
            typ=typ,
            modTime=modTime,
        )
        self.seen[ptr] = ent

        self.enqueue(ent, 'next')
        self.enqueue(ent, 'prev')
        self.enqueue(ent, 'child')

        return ent

    def enqueue(self, ent, label):
        if getattr(ent, label):
            self.queue.put((ent, label, getattr(ent, label)))

class MIF:
    def __init__(self, struct_def, spec):
        self.struct_def = struct_def
        self.spec = spec

    def _from_file(self, fh):
        data = self.struct_def.unpack_from(fh.read(self.struct_def.size))

        idx = 0
        for length, spec in self.spec:
            name = spec['name']
            val = data[idx:idx+length]
            if isinstance(val, tuple) and len(val) == 1:
                val = val[0]
            yield name, val
            idx += length

    def from_file(self, fh):
        return dict(self._from_file(fh))

def determine_type(typ):
    return 'L' if typ[0] == 't' else typ[0]

@lru_cache()
def compile_mif(definition):
    tokens = list(to_tokens(definition))
    bits = OrderedDict(list(parse_to_struct(tokens)))

    bits = list(bits.values())[0]

    parts = [
        '{}{}'.format(
            '' if length == 1 else length,
            spec['type']
        )
        for length, spec in bits
    ]

    return MIF(
        struct.Struct(''.join(parts)),
        bits
    )

def parse(filename):
    with open(filename, 'rb') as fh:
        label, ptr = struct.unpack('15sxcxxx', fh.read(20))
        label = label.decode()
        ptr = ord(ptr)
        print(label, ptr)

        fh.seek(ptr)

        version = struct.unpack('l', fh.read(4))[0]
        assert version == 1, version

        freeList, rootEntryPtr, entryHeaderLength, dictionaryPtr = (
            struct.unpack('<IIhI', fh.read(14))
        )

        root = EntryParser(fh, rootEntryPtr).parse()
    return root

def to_tokens(string):
    while string:
        if string[0] in PUNC:
            yield string[0]
            string = string[1:]

        elif string[0].isnumeric():
            m = NUM_RE.match(string)

            yield int(m.group())

            string = string[len(m.group()):]

        else:
            m = STR_RE.match(string)

            yield m.group()

            string = string[len(m.group()):]

if __name__ == '__main__':
    # from pprint import pprint
    # tokens = list(to_tokens(multiple))
    __import__('ipdb').set_trace()
    # pprint(OrderedDict(list(parse_to_struct(tokens))))
    main()
<commit_msg>Rename AUXFile to IMGFile<commit_after>import re
import json
import struct
import ctypes
from queue import Queue
from functools import lru_cache
from collections import namedtuple, OrderedDict

base = (
    'D:\\stats_data\\cache\\saau\\sections\\'
    'geology\\48006_shp\\globalmap2001\\Raster\\elevation\\'
    'elscnf'
)
NUM_RE = re.compile(r'(\d*)')
STR_RE = re.compile(r'([A-Za-z0-9_ -]*)')
PUNC = set('{},:')

FORMATS = json.load(open('formats.json'))

class IMGFile(namedtuple('IMGFile', 'version,rootEntry,mifDictionary')):
    def __hash__(self):
        return hash(
            (
                self.rootEntry,
                self.mifDictionary
            )
        )

    def __eq__(self, other):
        return hash(self) == hash(other)

MIF_DATA_TYPES = {
    '1': 1,
    '2': 1,
    '4': 1,
    'C': 1,
    'c': 1,
    'e': 1,
    'S': 2,
    's': 2,
    't': 4,
    'L': 4,
    'l': 4,
    'i': 4,
    'f': 4,
    'd': 8,
    'm': 8,
    'M': 16,
    'b': 'dynamic',
    'o': 'dynamic',
    'x': 'dynamic'
}

class BadFormatString(Exception):
    pass

def nc(tokens):
    name = tokens.pop(0)
    comma = tokens.pop(0)
    assert comma == ','
    return name

def parse_to_struct(tokens):
    while tokens:
        token = tokens.pop(0)
        if token == '{':
            block = list(parse_to_struct(tokens))
            name = nc(tokens)

            yield (name, block)

        elif token == '}':
            return

        elif isinstance(token, int):
            length = token
            assert tokens.pop(0) == ':'
            name = tokens.pop(0)
            comma = tokens.pop(0)

            typ = determine_type(name)

            if typ == 'o':
                # okay, custom type
                type_name = name[1:]

                if type_name == 'Emif_String':
                    typ = '16c'
                else:
                    raise Exception

                yield (1, {'type': typ, 'name': nc(tokens)})

            elif typ == 'e':
                # okay, enum

                n_elems = int(name[1:])
                yield (
                    length,
                    {
                        "type": "c",
                        "values": [nc(tokens) for _ in range(n_elems)],
                        "name": nc(tokens)
                    }
                )

            else:
                assert comma == ','

                yield (length, {'type': typ, 'name': name[1:]})

        else:
            raise BadFormatString(token)

class Entry:
    def __init__(self, nxt, prev, parent, child, data, dataSize, name, typ,
                 modTime):
        self.nxt = nxt
        self.prev = prev
        self.parent = parent
        self.child = child
        self.data = data
        self.dataSize = dataSize
        self.name = name
        self.typ = typ
        self.modTime = modTime

    def __hash__(self):
        return hash(self.ptr)
        # return hash((
        #     self.ptr,
        #     self.next,
        #     self.prev,
        #     self.parent,
        #     self.child,
        #     self.data,
        #     self.dataSize,
        #     self.name,
        #     self.typ,
        #     self.modTime
        # ))

    def __eq__(self, other):
        return hash(self) == hash(other)

    __str__ = __repr__ = lambda self: '<Entry {} of type {}>'.format(
        self.name, self.typ
    )

def clean_string(string):
    return ctypes.create_string_buffer(b''.join(string)).value.decode()

class EntryParser:
    def __init__(self, fh, root_ptr):
        self.fh = fh
        self.root_ptr = root_ptr
        self.queue = Queue()
        self.seen = {}

    def parse(self):
        root = type('', (), {'child': None})()
        self.queue.put((root, 'child', self.root_ptr))
        while not self.queue.empty():
            dest, name, ptr = self.queue.get()
            setattr(dest, name, self.parse_entry(ptr, dest))
            if name == 'child':
                setattr(
                    getattr(dest, name),
                    'parent',
                    dest
                )
        root.child.parent = None
        return root.child

    def parse_entry(self, ptr, parent):
        if ptr in self.seen:
            return self.seen[ptr]

        self.fh.seek(ptr)

        (
            nxt, prev, _, child, data, dataSize,
            name, typ, modTime
        ) = struct.unpack('IIIIIl64s32sL', self.fh.read(124))

        self.fh.seek(data)
        data = self.fh.read(dataSize)

        name = self.clean_string(name)
        typ = self.clean_string(typ)

        ent = Entry(
            nxt=nxt or None,
            prev=prev or None,
            parent=parent,
            child=child or None,
            data=data,
            dataSize=dataSize,
            name=name,
            typ=typ,
            modTime=modTime,
        )
        self.seen[ptr] = ent

        self.enqueue(ent, 'next')
        self.enqueue(ent, 'prev')
        self.enqueue(ent, 'child')

        return ent

    def enqueue(self, ent, label):
        if getattr(ent, label):
            self.queue.put((ent, label, getattr(ent, label)))

class MIF:
    def __init__(self, struct_def, spec):
        self.struct_def = struct_def
        self.spec = spec

    def _from_file(self, fh):
        data = self.struct_def.unpack_from(fh.read(self.struct_def.size))

        idx = 0
        for length, spec in self.spec:
            name = spec['name']
            val = data[idx:idx+length]
            if isinstance(val, tuple) and len(val) == 1:
                val = val[0]
            yield name, val
            idx += length

    def from_file(self, fh):
        return dict(self._from_file(fh))

def determine_type(typ):
    return 'L' if typ[0] == 't' else typ[0]

@lru_cache()
def compile_mif(definition):
    tokens = list(to_tokens(definition))
    bits = OrderedDict(list(parse_to_struct(tokens)))

    bits = list(bits.values())[0]

    parts = [
        '{}{}'.format(
            '' if length == 1 else length,
            spec['type']
        )
        for length, spec in bits
    ]

    return MIF(
        struct.Struct(''.join(parts)),
        bits
    )

def parse(filename):
    with open(filename, 'rb') as fh:
        label, ptr = struct.unpack('15sxcxxx', fh.read(20))
        label = label.decode()
        ptr = ord(ptr)
        print(label, ptr)

        fh.seek(ptr)

        version = struct.unpack('l', fh.read(4))[0]
        assert version == 1, version

        freeList, rootEntryPtr, entryHeaderLength, dictionaryPtr = (
            struct.unpack('<IIhI', fh.read(14))
        )

        root = EntryParser(fh, rootEntryPtr).parse()
    return root

def to_tokens(string):
    while string:
        if string[0] in PUNC:
            yield string[0]
            string = string[1:]

        elif string[0].isnumeric():
            m = NUM_RE.match(string)

            yield int(m.group())

            string = string[len(m.group()):]

        else:
            m = STR_RE.match(string)

            yield m.group()

            string = string[len(m.group()):]

if __name__ == '__main__':
    # from pprint import pprint
    # tokens = list(to_tokens(multiple))
    __import__('ipdb').set_trace()
    # pprint(OrderedDict(list(parse_to_struct(tokens))))
    main()
<|endoftext|>


<commit_before># Django settings for gonzo project.
import os

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'UTC'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, '../media/')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '%%wx$yiag9%t+ncj!ozqqid4b=wjlx2ra*=(p284j&uezh@@mi'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'gonzo.utils.middleware.debug.DebugFooter',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware'
)

ROOT_URLCONF = 'gonzo.urls'

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, '../templates/'),
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'gonzo.hunt',
    'gonzo.api',
    'gonzo.account',
    'gonzo.oauth',
    'gonzo.webapp'
)
AUTH_PROFILE_MODULE='account.Profile'
LOGIN_REDIRECT_URL='/'
SIGNUP_EMAIL_WHITELIST=()

import sys

local_settings_name = os.environ.get('GONZO_LOCAL_SETTINGS_MODULE', 'local_settings')
try:
    __import__(local_settings_name)

    local = sys.modules[local_settings_name]
    this = sys.modules[__name__]

    # This mimics 'from local_settings import *'
    for name in dir(local):
        if not name.startswith('_'):
            print name
            setattr(this, name, getattr(local, name))

except ImportError:
    pass

<commit_msg>Remove print<commit_after># Django settings for gonzo project.
import os

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'UTC'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, '../media/')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '%%wx$yiag9%t+ncj!ozqqid4b=wjlx2ra*=(p284j&uezh@@mi'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GZipMiddleware',
    'gonzo.utils.middleware.debug.DebugFooter',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware'
)

ROOT_URLCONF = 'gonzo.urls'

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, '../templates/'),
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'gonzo.hunt',
    'gonzo.api',
    'gonzo.account',
    'gonzo.oauth',
    'gonzo.webapp'
)
AUTH_PROFILE_MODULE='account.Profile'
LOGIN_REDIRECT_URL='/'
SIGNUP_EMAIL_WHITELIST=()

import sys

local_settings_name = os.environ.get('GONZO_LOCAL_SETTINGS_MODULE', 'local_settings')
try:
    __import__(local_settings_name)

    local = sys.modules[local_settings_name]
    this = sys.modules[__name__]

    # This mimics 'from local_settings import *'
    for name in dir(local):
        if not name.startswith('_'):
            setattr(this, name, getattr(local, name))

except ImportError:
    pass

<|endoftext|>


<commit_before>def countDistinctParams(fname):
    with open(fname) as f:
        parameters=[]
        for line in f:
            if line != '\n':
                parameters.append(line.split()[1][:-1])
    return len(set(parameters))

def countDistinctParamsInMorseGraph(fname,MGN):
    with open(fname) as f:
        parameters=[]
        for line in f:
            if line != '\n':
                words = line.split()
                if int(words[4][:-1]) == MGN:
                    parameters.append(words[1][:-1])
    return len(set(parameters))

if __name__=='__main__':
    fname = '/share/data/bcummins/parameterresults/5D_2016_01_05_C_MG2658_allresults.txt'
    MGN = 2658
    print countDistinctParams(fname)
    print countDistinctParamsInMorseGraph(fname,MGN)
<commit_msg>commented out lines<commit_after>def countDistinctParams(fname):
    with open(fname) as f:
        parameters=[]
        for line in f:
            if line != '\n':
                parameters.append(line.split()[1][:-1])
    return len(set(parameters))

def countDistinctParamsInMorseGraph(fname,MGN):
    with open(fname) as f:
        parameters=[]
        for line in f:
            if line != '\n':
                words = line.split()
                if int(words[4][:-1]) == MGN:
                    parameters.append(words[1][:-1])
    return len(set(parameters))

if __name__=='__main__':
    fname = '/share/data/bcummins/parameterresults/5D_2016_01_16_A_MG4578_allresults.txt'
    # MGN = 4578
    print countDistinctParams(fname)
    # print countDistinctParamsInMorseGraph(fname,MGN)
<|endoftext|>


<commit_before>import time
try:
    import lcd
    lcd = lcd.lcd()
    # Change this to your i2c address
    lcd.set_addr(0x23)
except:
    lcd = False

def log(string, color = 'white'):
    colorHex = {
        'red': '91m',
        'green': '92m',
        'yellow': '93m',
        'blue': '94m'
    }
    if color not in colorHex:
        print('[' + time.strftime("%Y-%m-%d %H:%M:%S") + '] '+ string)
    else:
        print(u'\033['+ colorHex[color] + '[' + time.strftime("%Y-%m-%d %H:%M:%S") + '] ' + string.decode('utf-8') + '\033[0m')
    if lcd:
        if(string):
            lcd.message(string)
<commit_msg>Making logs easier to read and smaller - removing unnecessary date<commit_after>import time
try:
    import lcd
    lcd = lcd.lcd()
    # Change this to your i2c address
    lcd.set_addr(0x23)
except:
    lcd = False

def log(string, color = 'white'):
    colorHex = {
        'red': '91m',
        'green': '92m',
        'yellow': '93m',
        'blue': '94m'
    }
    if color not in colorHex:
        print('[' + time.strftime("%%H:%M:%S") + '] '+ string)
    else:
        print(u'\033['+ colorHex[color] + '[' + time.strftime("%H:%M:%S") + '] ' + string.decode('utf-8') + '\033[0m')
    if lcd:
        if(string):
            lcd.message(string)
<|endoftext|>


<commit_before>import re
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer, StaticHTMLRenderer

class JSONRendererWithESISupport(JSONRenderer):
    format = 'json'
    media_type = 'application/json'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        #  TODO: There should be a way to do this that is conditional on esi being requested and
        #  TODO: In such a way that it doesn't use regex unless there's absolutely no other way.
        initial_rendering = super(JSONRendererWithESISupport, self).render(data, accepted_media_type, renderer_context)
        augmented_rendering = re.sub(r'"<esi:include src=\\"(.*?)\\"\/>"', r'<esi:include src="\1"/>', initial_rendering)
        return augmented_rendering

class JSONAPIRenderer(JSONRendererWithESISupport):
    format = 'jsonapi'
    media_type = 'application/vnd.api+json'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        # Allow adding a top-level `meta` object to the response by including it in renderer_context
        # See JSON-API documentation on meta information: http://jsonapi.org/format/#document-meta
        data_type = type(data)
        if renderer_context is not None and data_type != str and data is not None:
            meta_dict = renderer_context.get('meta')
            version = getattr(renderer_context['request'], 'version', None)
            if meta_dict is not None:
                if version:
                    meta_dict['version'] = renderer_context['request'].version
                    data.setdefault('meta', {}).update(meta_dict)
            elif version:
                meta_dict = {'version': renderer_context['request'].version}
                data.setdefault('meta', {}).update(meta_dict)
        return super(JSONAPIRenderer, self).render(data, accepted_media_type, renderer_context)

class BrowsableAPIRendererNoForms(BrowsableAPIRenderer):
    """
    Renders browsable API but omits HTML forms
    """

    def show_form_for_method(self, view, method, request, obj):
        return False

    def show_form_for_method(self, view, method, request, obj):
        #  This keeps the forms from being rendered, which we never want to do.
        return False

class PlainTextRenderer(StaticHTMLRenderer):
    """
    Renders plain text.

    Inherits from StaticHTMLRenderer for error handling.
    """

    media_type = 'text/markdown'
    format = 'txt'
<commit_msg>Removing renderer line<commit_after>import re
from rest_framework.renderers import JSONRenderer, BrowsableAPIRenderer, StaticHTMLRenderer

class JSONRendererWithESISupport(JSONRenderer):
    format = 'json'
    media_type = 'application/json'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        #  TODO: There should be a way to do this that is conditional on esi being requested and
        #  TODO: In such a way that it doesn't use regex unless there's absolutely no other way.
        initial_rendering = super(JSONRendererWithESISupport, self).render(data, accepted_media_type, renderer_context)
        augmented_rendering = re.sub(r'"<esi:include src=\\"(.*?)\\"\/>"', r'<esi:include src="\1"/>', initial_rendering)
        return augmented_rendering

class JSONAPIRenderer(JSONRendererWithESISupport):
    format = 'jsonapi'
    media_type = 'application/vnd.api+json'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        # Allow adding a top-level `meta` object to the response by including it in renderer_context
        # See JSON-API documentation on meta information: http://jsonapi.org/format/#document-meta
        data_type = type(data)
        if renderer_context is not None and data_type != str and data is not None:
            meta_dict = renderer_context.get('meta')
            version = getattr(renderer_context['request'], 'version', None)
            if meta_dict is not None:
                if version:
                    meta_dict['version'] = renderer_context['request'].version
                    data.setdefault('meta', {}).update(meta_dict)
            elif version:
                meta_dict = {'version': renderer_context['request'].version}
                data.setdefault('meta', {}).update(meta_dict)
        return super(JSONAPIRenderer, self).render(data, accepted_media_type, renderer_context)

class BrowsableAPIRendererNoForms(BrowsableAPIRenderer):
    """
    Renders browsable API but omits HTML forms
    """

    def show_form_for_method(self, view, method, request, obj):
        return False

class PlainTextRenderer(StaticHTMLRenderer):
    """
    Renders plain text.

    Inherits from StaticHTMLRenderer for error handling.
    """

    media_type = 'text/markdown'
    format = 'txt'
<|endoftext|>


<commit_before># written in python 3.7

from time import sleep
import time
import string
import os
import re
import sys
import logging
from datetime import datetime

history_list = []
f = open("versions.txt","r")
for x in f:
  if x.strip() == "":
  	continue
  elif "current" in x.strip():
  	continue
  else:
  	history_list.append(x.strip())
  	print(x.strip())

history_thismonth = []
for item in history_list:
    #text= str(item)
    print(item)
    if "October" in item:
       history_thismonth.append(item)
       #print(item)

for t in history_thismonth:
    t = t + ", 2019"
    datetime_object = datetime.strptime(t, "%B %d, %I:%M %p, %Y")
    print(datetime_object)<commit_msg>M: time comparision.<commit_after># written in python 3.7

from time import sleep
import time
import string
import os
import re
import sys
import logging
from datetime import datetime

history_list = []
f = open("versions.txt","r")
for x in f:
  if x.strip() == "":
  	continue
  elif "current" in x.strip():
  	continue
  else:
  	history_list.append(x.strip())
  	#print(x.strip())

history_thismonth = []
for item in history_list:
    #text= str(item)
    #print(item)
    if "October" in item:
       history_thismonth.append(item)
       #print(item)

history_thismonth_redundant = list(dict.fromkeys(history_thismonth))

arr_datetime = []
prev_datetime = ""
for t in history_thismonth_redundant:
    t = t + ", 2019"
    prev_datetime
    datetime_object = datetime.strptime(t, "%B %d, %I:%M %p, %Y")
    #print(datetime_object)
    arr_datetime.append(datetime_object)

for i in range(len(arr_datetime)-1, 0, -1):
	test= arr_datetime[i-1] - arr_datetime[i]
	print(str(arr_datetime[i-1]) + " - " + str(arr_datetime[i]) + " = " + str(test))<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals
import argparse
from datetime import datetime
import hashlib
import json
import logging
import re

from popong_models import Base
from popong_data_utils import guess_person
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

from pokr.database import db_session, transaction
from pokr.models import Meeting, Person, Statement
from utils.command import Command

attendance_re = re.compile(r'(출|참)석\s*(감사위|의|위)원')

class MeetingCommand(Command):
    __command__ = 'meeting'

class InsertMeetingCommand(Command):
    __command__ = 'insert'
    __parent__ = MeetingCommand

    @classmethod
    def init_parser_options(cls):
        cls.parser.add_argument('files', type=argparse.FileType('r'), nargs='+')
        cls.parser.add_argument('-r', dest='region_id', required=True)

    @classmethod
    def run(cls, files, region_id, **kwargs):
        Base.query = db_session.query_property()
        for file_ in files:
            obj = json.load(file_)
            insert_meetings(region_id, obj)

def insert_meetings(region_id, obj):
    if isinstance(obj, dict):
        insert_meeting(obj)

    elif isinstance(obj, list):
        for o in obj:
            insert_meeting(region_id, o)

    else:
        raise Exception()

def insert_meeting(region_id, obj):
    date = datetime.strptime(obj['date'], '%Y-%m-%d').date()
    dialogue = obj['dialogue']
    attendee_names = get_attendee_names(obj)
    session_id = int(obj['session_id']) if obj['session_id'].isdigit() else None
    meeting_id = int(obj['meeting_id']) if obj['meeting_id'].isdigit() else None
    id = int('{region_id}{assembly_id}{session_id}{meeting_id}{md5}'.format(
             region_id=region_id,
             assembly_id=obj['assembly_id'],
             session_id=session_id or '',
             meeting_id=meeting_id or '',
             md5=int(hashlib.md5(obj['committee'].encode('utf-8')).hexdigest()[:4], 16)))

    meeting = Meeting(
        id=id,
        region_id=region_id,
        committee=obj['committee'],
        parliament_id=obj['assembly_id'],
        session_id=session_id,
        sitting_id=meeting_id,
        date=date,
        issues=obj['issues'],
        url=obj['issues_url'],
        pdf_url=obj['pdf'],
    )
    with transaction() as session:
        session.add(meeting)

        # 'meeting_attendee' table
        attendees = list(get_attendees(meeting, attendee_names, session))
        meeting.attendees = attendees

        # 'statement' table
        statements = (stmt for stmt in dialogue
                           if stmt['type'] == 'statement')
        for seq, statement in enumerate(statements):
            item = create_statement(meeting, seq, statement, attendees)
            statement['person_id'] = item.person_id
            meeting.statements.append(item)

        # Updated dialog field of meeting table
        meeting.dialogue = dialogue

        # TODO: votes = obj['votes']

def get_attendee_names(obj):
    for key, val in obj['attendance'].iteritems():
        if attendance_re.match(key):
            return val['names']
    logging.warning('Attendance not found {date}-{committee}'.format(**obj))
    return []

def get_attendees(meeting, names, session=None):
    for name in names:
        try:
            person = guess_person(name=name, assembly_id=meeting.parliament_id,
                                  is_elected=True)
            # FIXME: workaround different session binding problem
            yield session.query(Person).filter_by(id=person.id).one()
        except MultipleResultsFound as e:
            logging.warning('Multiple result found for: %s' % name)
        except NoResultFound as e:
            logging.warning('No result found for: %s' % name)

def create_statement(meeting, seq, statement, attendees):
    person = guess_attendee(attendees, statement['person'])
    item = Statement(
        meeting_id=meeting.id,
        person_id=person.id if person else None,
        sequence=seq,
        speaker=statement['person'],
        content=statement['content'],
    )
    return item

def guess_attendee(attendees, name):
    for attendee in attendees:
        if attendee.name in name:
            return attendee
    return None

<commit_msg>In case that there is no attendee<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals
import argparse
from datetime import datetime
import hashlib
import json
import logging
import re

from popong_models import Base
from popong_data_utils import guess_person
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

from pokr.database import db_session, transaction
from pokr.models import Meeting, Person, Statement
from utils.command import Command

attendance_re = re.compile(r'(출|참)석\s*(감사위|의|위)원')

class MeetingCommand(Command):
    __command__ = 'meeting'

class InsertMeetingCommand(Command):
    __command__ = 'insert'
    __parent__ = MeetingCommand

    @classmethod
    def init_parser_options(cls):
        cls.parser.add_argument('files', type=argparse.FileType('r'), nargs='+')
        cls.parser.add_argument('-r', dest='region_id', required=True)

    @classmethod
    def run(cls, files, region_id, **kwargs):
        Base.query = db_session.query_property()
        for file_ in files:
            obj = json.load(file_)
            insert_meetings(region_id, obj)

def insert_meetings(region_id, obj):
    if isinstance(obj, dict):
        insert_meeting(obj)

    elif isinstance(obj, list):
        for o in obj:
            insert_meeting(region_id, o)

    else:
        raise Exception()

def insert_meeting(region_id, obj):
    date = datetime.strptime(obj['date'], '%Y-%m-%d').date()
    dialogue = obj['dialogue']
    attendee_names = get_attendee_names(obj)
    session_id = int(obj['session_id']) if obj['session_id'].isdigit() else None
    meeting_id = int(obj['meeting_id']) if obj['meeting_id'].isdigit() else None
    id = int('{region_id}{assembly_id}{session_id}{meeting_id}{md5}'.format(
             region_id=region_id,
             assembly_id=obj['assembly_id'],
             session_id=session_id or '',
             meeting_id=meeting_id or '',
             md5=int(hashlib.md5(obj['committee'].encode('utf-8')).hexdigest()[:4], 16)))

    meeting = Meeting(
        id=id,
        region_id=region_id,
        committee=obj['committee'],
        parliament_id=obj['assembly_id'],
        session_id=session_id,
        sitting_id=meeting_id,
        date=date,
        issues=obj['issues'],
        url=obj['issues_url'],
        pdf_url=obj['pdf'],
    )
    with transaction() as session:
        session.add(meeting)

        # 'meeting_attendee' table
        attendees = list(get_attendees(meeting, attendee_names, session))
        meeting.attendees = attendees

        # 'statement' table
        statements = (stmt for stmt in dialogue
                           if stmt['type'] == 'statement')
        for seq, statement in enumerate(statements):
            item = create_statement(meeting, seq, statement, attendees)
            statement['person_id'] = item.person_id
            meeting.statements.append(item)

        # Updated dialog field of meeting table
        meeting.dialogue = dialogue

        # TODO: votes = obj['votes']

def get_attendee_names(obj):
    for key, val in obj.get('attendance', {}).iteritems():
        if attendance_re.match(key):
            return val['names']
    logging.warning('Attendance not found {date}-{committee}'.format(**obj))
    return []

def get_attendees(meeting, names, session=None):
    for name in names:
        try:
            person = guess_person(name=name, assembly_id=meeting.parliament_id,
                                  is_elected=True)
            # FIXME: workaround different session binding problem
            yield session.query(Person).filter_by(id=person.id).one()
        except MultipleResultsFound as e:
            logging.warning('Multiple result found for: %s' % name)
        except NoResultFound as e:
            logging.warning('No result found for: %s' % name)

def create_statement(meeting, seq, statement, attendees):
    person = guess_attendee(attendees, statement['person'])
    item = Statement(
        meeting_id=meeting.id,
        person_id=person.id if person else None,
        sequence=seq,
        speaker=statement['person'],
        content=statement['content'],
    )
    return item

def guess_attendee(attendees, name):
    for attendee in attendees:
        if attendee.name in name:
            return attendee
    return None

<|endoftext|>


<commit_before>from graphql_relay.node.node import from_global_id, to_global_id
from database.operation import Operation
import api_utils
import batch_schema
import graphene
import logging

# Load logging configuration
log = logging.getLogger(__name__)

class CreateBatchOwnerInput(graphene.InputObjectType):
    """Input to create batch owner."""
    name = graphene.String(required=True)

class CreateBatchOwner(graphene.Mutation):
    """Create batch owner."""
    class Arguments:
        input = CreateBatchOwnerInput(required=True)

    # Class attributes
    batch_owner = graphene.Field(batch_schema.BatchOwner)

    @staticmethod
    def mutate(root, info, input=None):
        """Method to create batch owner."""
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch_owner = api_utils.create('BatchOwner', record)
        batch_owner = batch_schema.BatchOwner(**batch_owner)
        batch_owner.id = to_global_id('BatchOwner', batch_owner.id)
        return CreateBatchOwner(batch_owner=batch_owner)

class UpdateBatchOwnerInput(graphene.InputObjectType):
    """Input to update batch owner."""
    id = graphene.ID(required=True)
    name = graphene.String()

class UpdateBatchOwner(graphene.Mutation):
    """Update batch owner."""
    class Arguments:
        input = UpdateBatchOwnerInput(required=True)

    # Class attributes
    batch_owner = graphene.Field(batch_schema.BatchOwner)

    @staticmethod
    def mutate(self, info, input):
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch_owner = api_utils.update('BatchOwner', record)
        batch_owner = batch_schema.BatchOwner(**batch_owner)
        return UpdateBatchOwner(batch_owner)

class CreateBatchInput(graphene.InputObjectType):
    """Input to create batch."""
    batchOwnerId = graphene.String(required=True)
    statusId = graphene.String(required=True)

class CreateBatch(graphene.Mutation):
    """Create batch."""
    class Arguments:
        input = CreateBatchInput(required=True)

    # Class attributes
    batch = graphene.Field(batch_schema.Batch)

    @staticmethod
    def mutate(root, info, input=None):
        """Method to create batch."""
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch = api_utils.create('Batch', record)
        batch = batch_schema.Batch(**batch)
        batch.id = to_global_id('Batch', batch.id)
        return CreateBatch(batch=batch)

class UpdateBatchInput(graphene.InputObjectType):
    """Input to update batch."""
    id = graphene.ID(required=True)
    batchOwnerId = graphene.String()
    statusId = graphene.String()

class UpdateBatch(graphene.Mutation):
    """Update batch."""
    class Arguments:
        input = UpdateBatchInput(required=True)

    # Class attributes
    batch = graphene.Field(batch_schema.Batch)

    @staticmethod
    def mutate(self, info, input):
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        print(record)
        batch = api_utils.update('Batch', record)
        batch = batch_schema.Batch(**batch)
        return UpdateBatch(batch)

class BatchMethod:
    """Functions called by the API for batch objects."""

    def execute(self, indicator_id=None):
        batch_record = self.start()

        # Get indicators for the batch owner
        if indicator_id is not None:
            indicator_list = Operation('Indicator').read(id=indicator_id, batchOwnerId=self.batch_owner_id)
        else:
            indicator_list = Operation('Indicator').read(batchOwnerId=self.batch_owner_id)

        for indicator_record in indicator_list:
            IndicatorMethod(indicator_record.id).execute(batch_record.id)

        self.stop(batch_record.id)
        self.error_message['message'] = 'Batch with Id {} completed successfully'.format(batch_record.id)
        log.info(self.error_message['message'])
        return self.error_message
<commit_msg>Removed print<commit_after>from graphql_relay.node.node import from_global_id, to_global_id
from database.operation import Operation
import api_utils
import batch_schema
import graphene
import logging

# Load logging configuration
log = logging.getLogger(__name__)

class CreateBatchOwnerInput(graphene.InputObjectType):
    """Input to create batch owner."""
    name = graphene.String(required=True)

class CreateBatchOwner(graphene.Mutation):
    """Create batch owner."""
    class Arguments:
        input = CreateBatchOwnerInput(required=True)

    # Class attributes
    batch_owner = graphene.Field(batch_schema.BatchOwner)

    @staticmethod
    def mutate(root, info, input=None):
        """Method to create batch owner."""
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch_owner = api_utils.create('BatchOwner', record)
        batch_owner = batch_schema.BatchOwner(**batch_owner)
        batch_owner.id = to_global_id('BatchOwner', batch_owner.id)
        return CreateBatchOwner(batch_owner=batch_owner)

class UpdateBatchOwnerInput(graphene.InputObjectType):
    """Input to update batch owner."""
    id = graphene.ID(required=True)
    name = graphene.String()

class UpdateBatchOwner(graphene.Mutation):
    """Update batch owner."""
    class Arguments:
        input = UpdateBatchOwnerInput(required=True)

    # Class attributes
    batch_owner = graphene.Field(batch_schema.BatchOwner)

    @staticmethod
    def mutate(self, info, input):
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch_owner = api_utils.update('BatchOwner', record)
        batch_owner = batch_schema.BatchOwner(**batch_owner)
        return UpdateBatchOwner(batch_owner)

class CreateBatchInput(graphene.InputObjectType):
    """Input to create batch."""
    batchOwnerId = graphene.String(required=True)
    statusId = graphene.String(required=True)

class CreateBatch(graphene.Mutation):
    """Create batch."""
    class Arguments:
        input = CreateBatchInput(required=True)

    # Class attributes
    batch = graphene.Field(batch_schema.Batch)

    @staticmethod
    def mutate(root, info, input=None):
        """Method to create batch."""
        # Convert input to dictionary
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch = api_utils.create('Batch', record)
        batch = batch_schema.Batch(**batch)
        batch.id = to_global_id('Batch', batch.id)
        return CreateBatch(batch=batch)

class UpdateBatchInput(graphene.InputObjectType):
    """Input to update batch."""
    id = graphene.ID(required=True)
    batchOwnerId = graphene.String()
    statusId = graphene.String()

class UpdateBatch(graphene.Mutation):
    """Update batch."""
    class Arguments:
        input = UpdateBatchInput(required=True)

    # Class attributes
    batch = graphene.Field(batch_schema.Batch)

    @staticmethod
    def mutate(self, info, input):
        record = {}
        for key in input:
            if key[-2:].lower() == 'id':
                input[key] = from_global_id(input[key])[1]  # Convert global id to database id
            record[key] = input[key]
        batch = api_utils.update('Batch', record)
        batch = batch_schema.Batch(**batch)
        return UpdateBatch(batch)

class BatchMethod:
    """Functions called by the API for batch objects."""

    def execute(self, indicator_id=None):
        batch_record = self.start()

        # Get indicators for the batch owner
        if indicator_id is not None:
            indicator_list = Operation('Indicator').read(id=indicator_id, batchOwnerId=self.batch_owner_id)
        else:
            indicator_list = Operation('Indicator').read(batchOwnerId=self.batch_owner_id)

        for indicator_record in indicator_list:
            IndicatorMethod(indicator_record.id).execute(batch_record.id)

        self.stop(batch_record.id)
        self.error_message['message'] = 'Batch with Id {} completed successfully'.format(batch_record.id)
        log.info(self.error_message['message'])
        return self.error_message
<|endoftext|>