<commit_before>#!/usr/bin/python
import sys
import os
import time
import ConfigParser
import tempfile
import codecs
from StringIO import StringIO
from sfa.util.xml import XML

default_config = \
"""
"""

class Config:

    def __init__(self, config_file='/etc/sfa/sfa_config'):
        self._files = []
        self.config_path = os.path.dirname(config_file)
        self.config = ConfigParser.ConfigParser()
        self.filename = config_file
        if not os.path.isfile(self.filename):
            self.create(self.filename)
        self.load(self.filename)

    def _header(self):
        header = """
DO NOT EDIT. This file was automatically generated at
%s from:

%s
""" % (time.asctime(), os.linesep.join(self._files))

        # Get rid of the surrounding newlines
        return header.strip().split(os.linesep)

    def create(self, filename):
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
        configfile = open(filename, 'w')
        configfile.write(default_config)
        configfile.close()

    def load(self, filename):
        if filename:
            try:
                self.config.read(filename)
            except:
                if filename.endswith('.xml'):
                    self.load_xml(filename)
                else:
                    self.load_shell(filename)
            self._files.append(filename)
            self.set_attributes()

    def load_xml(self, filename):
        xml = XML(filename)
        categories = xml.xpath('//configuration/variables/category')
        for category in categories:
            section_name = category.get('id')
            if not self.config.has_section(section_name):
                self.config.add_section(section_name)
            options = category.xpath('./variablelist/variable')
            for option in options:
                option_name = option.get('id')
                value = option.xpath('./value')[0].text
                if not value:
                    value = ""
                self.config.set(section_name, option_name, value)

    def load_shell(self, filename):
        f = open(filename, 'r')
        for line in f:
            try:
                if line.startswith('#'):
                    continue
                parts = line.strip().split("=")
                if len(parts) < 2:
                    continue
                option = parts[0]
                value = parts[1].replace('"', '').replace("'","")
                section, var = self.locate_varname(option, strict=False)
                if section and var:
                    self.set(section, var, value)
            except:
                pass
        f.close()

    def locate_varname(self, varname, strict=True):
        varname = varname.lower()
        sections = self.config.sections()
        section_name = ""
        var_name = ""
        for section in sections:
            if varname.startswith(section.lower()) and len(section) > len(section_name):
                section_name = section.lower()
                var_name = varname.replace(section_name, "")[1:]
        if strict and not self.config.has_option(section_name, var_name):
            raise ConfigParser.NoOptionError(var_name, section_name)
        return (section_name, var_name)

    def set_attributes(self):
        sections = self.config.sections()
        for section in sections:
            for item in self.config.items(section):
                name = "%s_%s" % (section, item[0])
                value = item[1]
                setattr(self, name, value)
                setattr(self, name.upper(), value)

    def verify(self, config1, config2, validate_method):
        return True

    def validate_type(self, var_type, value):
        return True

    def dump(self, sections = []):
        sys.stdout.write(output_python())

    def output_python(self, encoding = "utf-8"):
        buf = codecs.lookup(encoding)[3](StringIO())
        buf.writelines(["# " + line + os.linesep for line in self._header()])

        for section in self.sections():
            buf.write("[%s]%s" % (section, os.linesep))
            for (name,value) in self.items(section):
                buf.write("%s=%s%s" % (name,value,os.linesep))
            buf.write(os.linesep)
        return buf.getvalue()

    def output_shell(self, show_comments = True, encoding = "utf-8"):
        """
        Return variables as a shell script.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        buf.writelines(["# " + line + os.linesep for line in self._header()])

        for section in self.sections():
            for (name,value) in self.items(section):
                # bash does not have the concept of NULL
                if value:
                    option = "%s_%s" % (section.upper(), name.upper())
                    if not value.isdigit() and not bool(value):
                        value = "'%s'" % value
                    buf.write(option + "=" + value + os.linesep)
        return buf.getvalue()

    def output_php(selfi, encoding = "utf-8"):
        """
        Return variables as a PHP script.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        buf.write("<?php" + os.linesep)
        buf.writelines(["// " + line + os.linesep for line in self._header()])

        for section in self.sections():
            for (name,value) in self.items(section):
                option = "%s_%s" % (section, name)
                buf.write(os.linesep)
                buf.write("// " + option + os.linesep)
                if value is None:
                    value = 'NULL'
                buf.write("define('%s', %s);" % (option, value) + os.linesep)

        buf.write("?>" + os.linesep)

        return buf.getvalue()

    def output_xml(self, encoding = "utf-8"):
        pass

    def output_variables(self):
        """
        Return list of all variable names.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        for section in self.sections():
            for (name,value) in self.items(section):
                option = "%s_%s" % (section,name)
                buf.write(option + os.linesep)

        return buf.getvalue()
        pass

    def write(self, filename=None):
        if not filename:
            filename = self.filename
        configfile = open(filename, 'w')
        self.config.write(configfile)

    def save(self, filename=None):
        self.write(filename)

    def get_trustedroots_dir(self):
        return self.config_path + os.sep + 'trusted_roots'

    def get_openflow_aggrMgr_info(self):
        aggr_mgr_ip = 'localhost'
        if (hasattr(self,'openflow_aggregate_manager_ip')):
            aggr_mgr_ip = self.OPENFLOW_AGGREGATE_MANAGER_IP

        aggr_mgr_port = 2603
        if (hasattr(self,'openflow_aggregate_manager_port')):
            aggr_mgr_port = self.OPENFLOW_AGGREGATE_MANAGER_PORT

        return (aggr_mgr_ip,aggr_mgr_port)

    def get_interface_hrn(self):
        if (hasattr(self,'sfa_interface_hrn')):
            return self.SFA_INTERFACE_HRN
        else:
            return "plc"

    def __getattr__(self, attr):
        return getattr(self.config, attr)

if __name__ == '__main__':
    filename = None
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        config = Config(filename)
    else:
        config = Config()
    config.dump()

<commit_msg>added is_ini() and is_xml()<commit_after>#!/usr/bin/python
import sys
import os
import time
import ConfigParser
import tempfile
import codecs
from StringIO import StringIO
from sfa.util.xml import XML

default_config = \
"""
"""

class Config:

    def __init__(self, config_file='/etc/sfa/sfa_config'):
        self._files = []
        self.config_path = os.path.dirname(config_file)
        self.config = ConfigParser.ConfigParser()
        self.filename = config_file
        if not os.path.isfile(self.filename):
            self.create(self.filename)
        self.load(self.filename)

    def _header(self):
        header = """
DO NOT EDIT. This file was automatically generated at
%s from:

%s
""" % (time.asctime(), os.linesep.join(self._files))

        # Get rid of the surrounding newlines
        return header.strip().split(os.linesep)

    def create(self, filename):
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))
        configfile = open(filename, 'w')
        configfile.write(default_config)
        configfile.close()

    def load(self, filename):
        if filename:
            try:
                self.config.read(filename)
            except:
                print "normal read failed"
                if filename.endswith('.xml'):
                    self.load_xml(filename)
                else:
                    print "loading shell"
                    self.load_shell(filename)
            self._files.append(filename)
            self.set_attributes()

    def load_xml(self, filename):
        xml = XML(filename)
        categories = xml.xpath('//configuration/variables/category')
        for category in categories:
            section_name = category.get('id')
            if not self.config.has_section(section_name):
                self.config.add_section(section_name)
            options = category.xpath('./variablelist/variable')
            for option in options:
                option_name = option.get('id')
                value = option.xpath('./value')[0].text
                if not value:
                    value = ""
                self.config.set(section_name, option_name, value)

    def load_shell(self, filename):
        f = open(filename, 'r')
        for line in f:
            try:
                if line.startswith('#'):
                    continue
                parts = line.strip().split("=")
                if len(parts) < 2:
                    continue
                option = parts[0]
                value = parts[1].replace('"', '').replace("'","")
                section, var = self.locate_varname(option, strict=False)
                if section and var:
                    self.set(section, var, value)
            except:
                pass
        f.close()

    def locate_varname(self, varname, strict=True):
        varname = varname.lower()
        sections = self.config.sections()
        section_name = ""
        var_name = ""
        for section in sections:
            if varname.startswith(section.lower()) and len(section) > len(section_name):
                section_name = section.lower()
                var_name = varname.replace(section_name, "")[1:]
        if strict and not self.config.has_option(section_name, var_name):
            raise ConfigParser.NoOptionError(var_name, section_name)
        return (section_name, var_name)

    def set_attributes(self):
        sections = self.config.sections()
        for section in sections:
            for item in self.config.items(section):
                name = "%s_%s" % (section, item[0])
                value = item[1]
                setattr(self, name, value)
                setattr(self, name.upper(), value)

    def verify(self, config1, config2, validate_method):
        return True

    def validate_type(self, var_type, value):
        return True

    @staticmethod
    def is_xml(config_file):
        try:
            x = Xml(config_file)
            return True
        except:
            return False

    @staticmethod
    def is_ini(config_file):
        try:
            c = ConfigParser.ConfigParser()
            c.read(config_file)
            return True
        except:
            return False

    def dump(self, sections = []):
        sys.stdout.write(output_python())

    def output_python(self, encoding = "utf-8"):
        buf = codecs.lookup(encoding)[3](StringIO())
        buf.writelines(["# " + line + os.linesep for line in self._header()])

        for section in self.sections():
            buf.write("[%s]%s" % (section, os.linesep))
            for (name,value) in self.items(section):
                buf.write("%s=%s%s" % (name,value,os.linesep))
            buf.write(os.linesep)
        return buf.getvalue()

    def output_shell(self, show_comments = True, encoding = "utf-8"):
        """
        Return variables as a shell script.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        buf.writelines(["# " + line + os.linesep for line in self._header()])

        for section in self.sections():
            for (name,value) in self.items(section):
                # bash does not have the concept of NULL
                if value:
                    option = "%s_%s" % (section.upper(), name.upper())
                    if not value.isdigit() and not bool(value):
                        value = "'%s'" % value
                    buf.write(option + "=" + value + os.linesep)
        return buf.getvalue()

    def output_php(selfi, encoding = "utf-8"):
        """
        Return variables as a PHP script.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        buf.write("<?php" + os.linesep)
        buf.writelines(["// " + line + os.linesep for line in self._header()])

        for section in self.sections():
            for (name,value) in self.items(section):
                option = "%s_%s" % (section, name)
                buf.write(os.linesep)
                buf.write("// " + option + os.linesep)
                if value is None:
                    value = 'NULL'
                buf.write("define('%s', %s);" % (option, value) + os.linesep)

        buf.write("?>" + os.linesep)

        return buf.getvalue()

    def output_xml(self, encoding = "utf-8"):
        pass

    def output_variables(self):
        """
        Return list of all variable names.
        """

        buf = codecs.lookup(encoding)[3](StringIO())
        for section in self.sections():
            for (name,value) in self.items(section):
                option = "%s_%s" % (section,name)
                buf.write(option + os.linesep)

        return buf.getvalue()
        pass

    def write(self, filename=None):
        if not filename:
            filename = self.filename
        configfile = open(filename, 'w')
        self.config.write(configfile)

    def save(self, filename=None):
        self.write(filename)

    def get_trustedroots_dir(self):
        return self.config_path + os.sep + 'trusted_roots'

    def get_openflow_aggrMgr_info(self):
        aggr_mgr_ip = 'localhost'
        if (hasattr(self,'openflow_aggregate_manager_ip')):
            aggr_mgr_ip = self.OPENFLOW_AGGREGATE_MANAGER_IP

        aggr_mgr_port = 2603
        if (hasattr(self,'openflow_aggregate_manager_port')):
            aggr_mgr_port = self.OPENFLOW_AGGREGATE_MANAGER_PORT

        return (aggr_mgr_ip,aggr_mgr_port)

    def get_interface_hrn(self):
        if (hasattr(self,'sfa_interface_hrn')):
            return self.SFA_INTERFACE_HRN
        else:
            return "plc"

    def __getattr__(self, attr):
        return getattr(self.config, attr)

if __name__ == '__main__':
    filename = None
    if len(sys.argv) > 1:
        filename = sys.argv[1]
        config = Config(filename)
    else:
        config = Config()
    config.dump()

<|endoftext|>


<commit_before># -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2014-2015 Didotech SRL (info at didotech.com)
#                          All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _

class res_partner(orm.Model):
    _inherit = 'res.partner'

    _columns = {
        'property_supplier_ref': fields.char('Supplier Ref.', size=16, help="The reference attributed to supplier, leave blank for automatic allocation"),
        'property_customer_ref': fields.char('Customer Ref.', size=16, help="The reference attributed to customer, leave blank for automatic allocation"),
        'block_ref_customer': fields.boolean('Block Customer Reference'),
        'block_ref_supplier': fields.boolean('Block Supplier Reference'),
        'selection_account_receivable': fields.many2one(
            'account.account', 'Parent account', domain="[('type','=','view'),\
            ('user_type.code','=','account_type_view_assets')]",
            help='Used for create sub account'),
        'selection_account_payable': fields.many2one(
            'account.account', 'Parent account', domain="[('type','=','view'),\
            ('user_type.code','=','account_type_view_liability')]",
            help='Used for create sub account'),
    }

    _sql_constraints = [
        ('property_supplier_ref', 'unique(property_supplier_ref)',
            'Codice Fornitore Univoco'),
        ('property_customer_ref', 'unique(property_customer_ref)',
            'Codice Cliente Univoco'),
    ]

    def _get_chart_template_property(self, cr, uid, property_chart=None, context=None):
        chart_obj = self.pool['account.chart.template']
        # We need Administrator rights to read account.chart.template properties
        chart_templates_ids = chart_obj.search(cr, 1, [], context=context)
        if len(chart_templates_ids) > 0 and property_chart:
            # We need Administrator rights to read account.chart.template properties
            chart_templates = chart_obj.browse(cr, 1, chart_templates_ids, context)
            for chart_template in chart_templates:
                # if it's not a view type code, it's another branch without partner_subaccount
                account_template = getattr(chart_template, property_chart)
                if account_template.type == 'view':
                    account_account_ids = self.pool['account.account'].search(cr, uid, [('code', '=', account_template.code)])
                    if account_account_ids:
                        return account_account_ids[0]
                    else:
                        continue
                else:
                    raise orm.except_orm('Warning!', "Parent Account Type is not of type 'view'")
        else:
            return []

    def get_create_partner_account(self, cr, uid, vals, account_type, context):
        account_obj = self.pool['account.account']
        account_type_obj = self.pool['account.account.type']

        if account_type == 'customer':
            property_account = 'property_account_receivable'
            type_account = 'receivable'
            property_ref = 'property_customer_ref'
        elif account_type == 'supplier':
            property_account = 'property_account_payable'
            type_account = 'payable'
            property_ref = 'property_supplier_ref'
        else:
            # Unknown account type
            return False

        if not vals.get(property_account, False):
            vals[property_account] = self._get_chart_template_property(
                cr, uid, property_account, context)

        property_account_id = vals.get(property_account, False)
        if property_account_id:
            property_account = account_obj.browse(cr, uid, property_account_id, context)

            account_ids = account_obj.search(cr, uid, [('code', '=', '{0}{1}'.format(property_account.code, vals[property_ref]))])
            if account_ids:
                return account_ids[0]
            else:
                account_type_id = account_type_obj.search(
                    cr, uid, [('code', '=', type_account)], context=context)[0]

                return account_obj.create(cr, uid, {
                    'name': vals.get('name', ''),
                    'code': '{0}{1}'.format(property_account.code, vals[property_ref]),
                    'user_type': account_type_id,
                    'type': type_account,
                    'parent_id': property_account_id,
                    'active': True,
                    'reconcile': True,
                    'currency_mode': property_account.currency_mode,
                }, context)
        else:
            return False

    def create(self, cr, uid, vals, context=None):
        if not context:
            context = self.pool['res.users'].context_get(cr, uid)
        company = self.pool['res.users'].browse(cr, uid, uid, context).company_id

        enable_partner_subaccount = company.enable_partner_subaccount
        # if not company.enable_partner_subaccount:
        #     return super(res_partner, self).create(cr, uid, vals, context=context)

        # 1 se marcato come cliente - inserire se non esiste
        if vals.get('customer', False):
            vals['block_ref_customer'] = True
            if not vals.get('property_customer_ref', False):
                vals['property_customer_ref'] = self.pool['ir.sequence'].get(
                    cr, uid, 'SEQ_CUSTOMER_REF') or ''
            if enable_partner_subaccount:
                if vals.get('selection_account_receivable', False):
                    vals['property_account_receivable'] = vals['selection_account_receivable']
                vals['property_account_receivable'] = self.get_create_partner_account(cr, uid, vals, 'customer', context)

        # 2 se marcato come fornitore - inserire se non esiste

        if vals.get('supplier', False):
            vals['block_ref_supplier'] = True
            if not vals.get('property_supplier_ref', False):
                vals['property_supplier_ref'] = self.pool['ir.sequence'].get(
                    cr, uid, 'SEQ_SUPPLIER_REF') or ''
            if enable_partner_subaccount:
                if vals.get('selection_account_payable', False):
                    vals['property_account_payable'] = vals['selection_account_payable']
                vals['property_account_payable'] = self.get_create_partner_account(cr, uid, vals, 'supplier', context)

        return super(res_partner, self).create(cr, uid, vals, context=context)

    def unlink(self, cr, uid, ids, context=None):
        if not context:
            context = self.pool['res.users'].context_get(cr, uid)
        ids_account_payable = []
        ids_account_receivable = []
        for partner in self.browse(cr, uid, ids, context):

            if partner.property_account_payable and partner.property_account_payable.type != 'view':
                if partner.property_account_payable.balance == 0.0:
                    ids_account_payable.append(partner.property_account_payable.id)
                else:
                    ids.remove(partner.id)
            if partner.property_account_receivable and partner.property_account_receivable.type != 'view':
                if partner.property_account_receivable.balance == 0.0:
                    ids_account_receivable.append(partner.property_account_receivable.id)
                else:
                    ids.remove(partner.id)

        res = super(res_partner, self).unlink(cr, uid, ids, context)
        ids_account = list(set(ids_account_payable + ids_account_receivable))

        if res and ids_account:
            self.pool['account.account'].unlink(cr, 1, ids_account, context) # for unlink force superuser
        return res

    def write(self, cr, uid, ids, vals, context=None):

        if context is None or vals == {}:
            context = self.pool['res.users'].context_get(cr, uid)
            return super(res_partner, self).write(cr, uid, ids, vals, context=context)

        company = self.pool['res.users'].browse(
            cr, uid, uid, context).company_id

        enable_partner_subaccount = company.enable_partner_subaccount
        # if not company.enable_partner_subaccount:
        #     return super(res_partner, self).write(
        #         cr, uid, ids, vals, context=context)
        if isinstance(ids, (int, long)):
            ids = [ids]

        for partner in self.browse(cr, uid, ids, context):

            # 1 se marcato come cliente - inserire se non esiste
            if vals.get('customer', False):
                vals['block_ref_customer'] = True
                if not (vals.get('property_customer_ref', False) or partner.property_customer_ref):
                    vals['property_customer_ref'] = self.pool['ir.sequence'].get(
                        cr, uid, 'SEQ_CUSTOMER_REF') or ''
                if enable_partner_subaccount:
                    if vals.get('selection_account_receivable', False):
                        vals['property_account_receivable'] = vals['selection_account_receivable']
                    if partner.property_account_receivable and partner.property_account_receivable.type == 'view':
                        vals['property_account_receivable'] = self.get_create_partner_account(cr, uid, vals, 'customer', context)

            # 2 se marcato come fornitore - inserire se non esiste

            if vals.get('supplier', False):
                vals['block_ref_supplier'] = True
                if not (vals.get('property_supplier_ref', False) or partner.property_supplier_ref):
                    vals['property_supplier_ref'] = self.pool['ir.sequence'].get(
                        cr, uid, 'SEQ_SUPPLIER_REF') or ''
                if enable_partner_subaccount:
                    if vals.get('selection_account_payable', False):
                        vals['property_account_payable'] = vals['selection_account_payable']
                    if partner.selection_account_payable and partner.selection_account_payable.type == 'view':
                        vals['property_account_payable'] = self.get_create_partner_account(cr, uid, vals, 'supplier', context)

        return super(res_partner, self).write(cr, uid, ids, vals, context=context)

    def copy(self, cr, uid, partner_id, defaults, context=None):
        raise orm.except_orm('Warning', _('Duplication of a partner \
            is not allowed'))
<commit_msg>fix creation from opportunity<commit_after># -*- encoding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2014-2015 Didotech SRL (info at didotech.com)
#                          All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from openerp.osv import orm, fields
from openerp.tools.translate import _

class res_partner(orm.Model):
    _inherit = 'res.partner'

    _columns = {
        'property_supplier_ref': fields.char('Supplier Ref.', size=16, help="The reference attributed to supplier, leave blank for automatic allocation"),
        'property_customer_ref': fields.char('Customer Ref.', size=16, help="The reference attributed to customer, leave blank for automatic allocation"),
        'block_ref_customer': fields.boolean('Block Customer Reference'),
        'block_ref_supplier': fields.boolean('Block Supplier Reference'),
        'selection_account_receivable': fields.many2one(
            'account.account', 'Parent account', domain="[('type','=','view'),\
            ('user_type.code','=','account_type_view_assets')]",
            help='Used for create sub account'),
        'selection_account_payable': fields.many2one(
            'account.account', 'Parent account', domain="[('type','=','view'),\
            ('user_type.code','=','account_type_view_liability')]",
            help='Used for create sub account'),
    }

    _sql_constraints = [
        ('property_supplier_ref', 'unique(property_supplier_ref)',
            'Codice Fornitore Univoco'),
        ('property_customer_ref', 'unique(property_customer_ref)',
            'Codice Cliente Univoco'),
    ]

    def _get_chart_template_property(self, cr, uid, property_chart=None, context=None):
        chart_obj = self.pool['account.chart.template']
        # We need Administrator rights to read account.chart.template properties
        chart_templates_ids = chart_obj.search(cr, 1, [], context=context)
        if len(chart_templates_ids) > 0 and property_chart:
            # We need Administrator rights to read account.chart.template properties
            chart_templates = chart_obj.browse(cr, 1, chart_templates_ids, context)
            for chart_template in chart_templates:
                # if it's not a view type code, it's another branch without partner_subaccount
                account_template = getattr(chart_template, property_chart)
                if account_template.type == 'view':
                    account_account_ids = self.pool['account.account'].search(cr, uid, [('code', '=', account_template.code)])
                    if account_account_ids:
                        return account_account_ids[0]
                    else:
                        continue
                else:
                    raise orm.except_orm('Warning!', "Parent Account Type is not of type 'view'")
        else:
            return []

    def get_create_partner_account(self, cr, uid, vals, account_type, context):
        account_obj = self.pool['account.account']
        account_type_obj = self.pool['account.account.type']

        if account_type == 'customer':
            property_account = 'property_account_receivable'
            type_account = 'receivable'
            property_ref = 'property_customer_ref'
        elif account_type == 'supplier':
            property_account = 'property_account_payable'
            type_account = 'payable'
            property_ref = 'property_supplier_ref'
        else:
            # Unknown account type
            return False

        if not vals.get(property_account, False):
            vals[property_account] = self._get_chart_template_property(
                cr, uid, property_account, context)

        property_account_id = vals.get(property_account, False)
        if property_account_id:
            property_account = account_obj.browse(cr, uid, property_account_id, context)
            account_ids = account_obj.search(cr, uid, [('code', '=', '{0}{1}'.format(property_account.code, vals.get(property_ref, '')))])
            if account_ids:
                return account_ids[0]
            else:
                account_type_id = account_type_obj.search(
                    cr, uid, [('code', '=', type_account)], context=context)[0]

                return account_obj.create(cr, uid, {
                    'name': vals.get('name', ''),
                    'code': '{0}{1}'.format(property_account.code, vals[property_ref]),
                    'user_type': account_type_id,
                    'type': type_account,
                    'parent_id': property_account_id,
                    'active': True,
                    'reconcile': True,
                    'currency_mode': property_account.currency_mode,
                }, context)
        else:
            return False

    def create(self, cr, uid, vals, context=None):
        if not context:
            context = self.pool['res.users'].context_get(cr, uid)
        company = self.pool['res.users'].browse(cr, uid, uid, context).company_id

        enable_partner_subaccount = company.enable_partner_subaccount
        # if not company.enable_partner_subaccount:
        #     return super(res_partner, self).create(cr, uid, vals, context=context)

        # 1 se marcato come cliente - inserire se non esiste
        if vals.get('customer', False):
            vals['block_ref_customer'] = True
            if not vals.get('property_customer_ref', False):
                vals['property_customer_ref'] = self.pool['ir.sequence'].get(
                    cr, uid, 'SEQ_CUSTOMER_REF') or ''
            if enable_partner_subaccount:
                if vals.get('selection_account_receivable', False):
                    vals['property_account_receivable'] = vals['selection_account_receivable']
                vals['property_account_receivable'] = self.get_create_partner_account(cr, uid, vals, 'customer', context)

        # 2 se marcato come fornitore - inserire se non esiste

        if vals.get('supplier', False):
            vals['block_ref_supplier'] = True
            if not vals.get('property_supplier_ref', False):
                vals['property_supplier_ref'] = self.pool['ir.sequence'].get(
                    cr, uid, 'SEQ_SUPPLIER_REF') or ''
            if enable_partner_subaccount:
                if vals.get('selection_account_payable', False):
                    vals['property_account_payable'] = vals['selection_account_payable']
                vals['property_account_payable'] = self.get_create_partner_account(cr, uid, vals, 'supplier', context)

        return super(res_partner, self).create(cr, uid, vals, context=context)

    def unlink(self, cr, uid, ids, context=None):
        if not context:
            context = self.pool['res.users'].context_get(cr, uid)
        ids_account_payable = []
        ids_account_receivable = []
        for partner in self.browse(cr, uid, ids, context):

            if partner.property_account_payable and partner.property_account_payable.type != 'view':
                if partner.property_account_payable.balance == 0.0:
                    ids_account_payable.append(partner.property_account_payable.id)
                else:
                    ids.remove(partner.id)
            if partner.property_account_receivable and partner.property_account_receivable.type != 'view':
                if partner.property_account_receivable.balance == 0.0:
                    ids_account_receivable.append(partner.property_account_receivable.id)
                else:
                    ids.remove(partner.id)

        res = super(res_partner, self).unlink(cr, uid, ids, context)
        ids_account = list(set(ids_account_payable + ids_account_receivable))

        if res and ids_account:
            self.pool['account.account'].unlink(cr, 1, ids_account, context) # for unlink force superuser
        return res

    def write(self, cr, uid, ids, vals, context=None):

        if context is None or vals == {}:
            context = self.pool['res.users'].context_get(cr, uid)
            return super(res_partner, self).write(cr, uid, ids, vals, context=context)

        company = self.pool['res.users'].browse(
            cr, uid, uid, context).company_id

        enable_partner_subaccount = company.enable_partner_subaccount
        # if not company.enable_partner_subaccount:
        #     return super(res_partner, self).write(
        #         cr, uid, ids, vals, context=context)
        if isinstance(ids, (int, long)):
            ids = [ids]

        for partner in self.browse(cr, uid, ids, context):

            # 1 se marcato come cliente - inserire se non esiste
            if vals.get('customer', False):
                vals['block_ref_customer'] = True
                if not (vals.get('property_customer_ref', False) or partner.property_customer_ref):
                    vals['property_customer_ref'] = self.pool['ir.sequence'].get(
                        cr, uid, 'SEQ_CUSTOMER_REF') or ''
                if enable_partner_subaccount:
                    if vals.get('selection_account_receivable', False):
                        vals['property_account_receivable'] = vals['selection_account_receivable']
                    if partner.property_account_receivable and partner.property_account_receivable.type == 'view':
                        vals['property_account_receivable'] = self.get_create_partner_account(cr, uid, vals, 'customer', context)

            # 2 se marcato come fornitore - inserire se non esiste

            if vals.get('supplier', False):
                vals['block_ref_supplier'] = True
                if not (vals.get('property_supplier_ref', False) or partner.property_supplier_ref):
                    vals['property_supplier_ref'] = self.pool['ir.sequence'].get(
                        cr, uid, 'SEQ_SUPPLIER_REF') or ''
                if enable_partner_subaccount:
                    if vals.get('selection_account_payable', False):
                        vals['property_account_payable'] = vals['selection_account_payable']
                    if partner.selection_account_payable and partner.selection_account_payable.type == 'view':
                        vals['property_account_payable'] = self.get_create_partner_account(cr, uid, vals, 'supplier', context)

        return super(res_partner, self).write(cr, uid, ids, vals, context=context)

    def copy(self, cr, uid, partner_id, defaults, context=None):
        raise orm.except_orm('Warning', _('Duplication of a partner \
            is not allowed'))
<|endoftext|>


<commit_before># Copyright 2019 The vt-py authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import bz2
import datetime
import io
import json

import pytest
import pytest_httpserver

from vt import Client
from vt import FeedType
from vt import Object

def new_client(httpserver):
  return Client('dummy_api_key',
      host='http://' + httpserver.host + ':' + str(httpserver.port))

def test_object_from_dict():

  obj = Object.from_dict({
      'type': 'dummy_type',
      'id': 'dummy_id',
      'attributes': {
          'attr1': 'foo',
          'attr2': 1,
      }})

  assert obj.id == 'dummy_id'
  assert obj.type == 'dummy_type'
  assert obj.attr1 == 'foo'
  assert obj.attr2 == 1

  with pytest.raises(ValueError, match=r"Expecting dictionary, got: int"):
    Object.from_dict(1)

  with pytest.raises(ValueError, match=r"Object type not found"):
    Object.from_dict({})

  with pytest.raises(ValueError, match=r"Object id not found"):
    Object.from_dict({'type': 'dummy_type'})

  with pytest.raises(ValueError, match=r'Object attributes must be a dictionary'):
    Object.from_dict({'type': 'dummy_type', 'id': 'dummy_id', 'attributes': 1})

def test_object_date_attrs():

  obj = Object('dummy_type')
  obj.foo_date = 0

  assert obj.foo_date == datetime.datetime(1970, 1, 1, 0, 0, 0)

def test_object_modified_attrs():

  obj = Object.from_dict({
      'type': 'dummy_type',
      'id': 'dummy_id',
      'attributes': {
          'attr1': 'foo',
          'attr2': 1,
          'attr3': {
              'subattr1': 'bar'
          },
          'attr4': {
              'subattr1': 'baz'
          }
      }})

  # No changes, attributes shouldn't appear in the dictionary.
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert 'attributes' not in obj_dict

  # attr1 set to its previous value, no changes yet.
  obj.attr1 = 'foo'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert 'attributes' not in obj_dict

  # attr1 changed to 'bar', this should be the only attribute in the dictionary.
  obj.attr1 = 'bar'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 1
  assert obj_dict['attributes']['attr1'] == 'bar'

  obj.attr3['subattr1'] = 'foo'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 2
  assert obj_dict['attributes']['attr1'] == 'bar'
  assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'}

  del obj.attr4['subattr1']
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 3
  assert obj_dict['attributes']['attr1'] == 'bar'
  assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'}
  assert obj_dict['attributes']['attr4'] == {}

def test_get(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    response = client.get('/foo')

  assert response.status == 200

def test_get_data(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    data = client.get_data('/foo')

  assert data == 'dummy_data'

def test_get_object(httpserver):

  httpserver.expect_request(
      '/api/v3/dummy_types/dummy_id',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 'foo',
              'bar': 'bar'}
  }})

  with new_client(httpserver) as client:
    obj = client.get_object('/dummy_types/dummy_id')

  assert obj.id == 'dummy_id'
  assert obj.type == 'dummy_type'
  assert obj.foo == 'foo'
  assert obj.bar == 'bar'

def test_patch_object(httpserver):

  obj = Object('dummy_type', 'dummy_id', {'foo': 1, 'bar': 2})
  obj.foo = 2

  httpserver.expect_request(
      '/api/v3/dummy_types/dummy_id',
      method='PATCH',
      headers={'X-Apikey': 'dummy_api_key'},
      data=json.dumps({'data': obj.to_dict(modified_attributes_only=True)}),
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 2,
          }
      }
  })

  with new_client(httpserver) as client:
    client.patch_object('/dummy_types/dummy_id', obj=obj)

def test_post_object(httpserver):

  obj = Object('dummy_type')
  obj.foo = 'foo'

  httpserver.expect_request(
      '/api/v3/dummy_types',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'},
      data=json.dumps({'data': obj.to_dict()}),
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    obj = client.post_object('/dummy_types', obj=obj)

  assert obj.id == 'dummy_id'

def test_delete(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='DELETE',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    response = client.delete('/foo')

  assert response.status == 200

def test_iterator(httpserver):

  httpserver.expect_request(
      '/api/v3/dummy_collection/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': [{
          'id': 'dummy_id_1',
          'type': 'dummy_type',
          'attributes': {'order': 0}
          }]
  })

  with new_client(httpserver) as client:
    it = client.iterator('/dummy_collection/{}', 'foo')
    for i, obj in enumerate(it):
      assert 0 == i

def test_download_file(httpserver):

  httpserver.expect_request(
      '/api/v3/files/01020304050607080900a0b0c0d0e0f/download',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data('filecontent')

  with new_client(httpserver) as client:
    with io.BytesIO() as f:
      client.download_file('01020304050607080900a0b0c0d0e0f', f)
      f.seek(0)
      assert f.read() == b'filecontent'

def test_scan_file(httpserver):

  upload_url = (
      'http://' + httpserver.host + ':' + str(httpserver.port) + '/upload')

  httpserver.expect_oneshot_request(
      '/api/v3/files/upload_url',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': upload_url
  })

  httpserver.expect_oneshot_request(
      '/upload',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'analysis',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    f = io.StringIO("dummy file")
    analysis = client.scan_file(f)

  assert analysis.type == 'analysis'

def test_scan_url(httpserver):

  httpserver.expect_request(
      '/api/v3/urls',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'analysis',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    analysis = client.scan_url('https://www.dummy.url')

  assert analysis.type == 'analysis'

def test_feed(httpserver):

  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030405',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data(
    bz2.compress(b'{\"type\": \"file\", \"id\": \"dummy_file_id_1\"}'))

  # The feed iterator should tolerate missing feed packages, so let's return
  # a NotFoundError for package 200102030406.
  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030406',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'error': {
          'code': 'NotFoundError'
    }}, status=404)

  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030407',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data(
    bz2.compress(b'{\"type\": \"file\", \"id\": \"dummy_file_id_2\"}'))

  with new_client(httpserver) as client:
    feed = client.feed(FeedType.FILES, cursor='200102030405')
    feed_iterator = feed.__iter__()
    obj = next(feed_iterator)
    assert obj.type == 'file'
    assert obj.id == 'dummy_file_id_1'
    obj = next(feed_iterator)
    assert obj.type == 'file'
    assert obj.id == 'dummy_file_id_2'
<commit_msg>Add test cases for the `get` method.<commit_after># Copyright 2019 The vt-py authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import bz2
import datetime
import io
import json

import pytest
import pytest_httpserver

from vt import Client
from vt import FeedType
from vt import Object

def new_client(httpserver):
  return Client('dummy_api_key',
      host='http://' + httpserver.host + ':' + str(httpserver.port))

def test_object_from_dict():

  obj = Object.from_dict({
      'type': 'dummy_type',
      'id': 'dummy_id',
      'attributes': {
          'attr1': 'foo',
          'attr2': 1,
      }})

  assert obj.id == 'dummy_id'
  assert obj.type == 'dummy_type'
  assert obj.attr1 == 'foo'
  assert obj.attr2 == 1

  with pytest.raises(ValueError, match=r"Expecting dictionary, got: int"):
    Object.from_dict(1)

  with pytest.raises(ValueError, match=r"Object type not found"):
    Object.from_dict({})

  with pytest.raises(ValueError, match=r"Object id not found"):
    Object.from_dict({'type': 'dummy_type'})

  with pytest.raises(ValueError, match=r'Object attributes must be a dictionary'):
    Object.from_dict({'type': 'dummy_type', 'id': 'dummy_id', 'attributes': 1})

def test_object_date_attrs():

  obj = Object('dummy_type')
  obj.foo_date = 0

  assert obj.foo_date == datetime.datetime(1970, 1, 1, 0, 0, 0)

def test_object_modified_attrs():

  obj = Object.from_dict({
      'type': 'dummy_type',
      'id': 'dummy_id',
      'attributes': {
          'attr1': 'foo',
          'attr2': 1,
          'attr3': {
              'subattr1': 'bar'
          },
          'attr4': {
              'subattr1': 'baz'
          }
      }})

  # No changes, attributes shouldn't appear in the dictionary.
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert 'attributes' not in obj_dict

  # attr1 set to its previous value, no changes yet.
  obj.attr1 = 'foo'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert 'attributes' not in obj_dict

  # attr1 changed to 'bar', this should be the only attribute in the dictionary.
  obj.attr1 = 'bar'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 1
  assert obj_dict['attributes']['attr1'] == 'bar'

  obj.attr3['subattr1'] = 'foo'
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 2
  assert obj_dict['attributes']['attr1'] == 'bar'
  assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'}

  del obj.attr4['subattr1']
  obj_dict = obj.to_dict(modified_attributes_only=True)
  assert len(obj_dict['attributes']) == 3
  assert obj_dict['attributes']['attr1'] == 'bar'
  assert obj_dict['attributes']['attr3'] == {'subattr1': 'foo'}
  assert obj_dict['attributes']['attr4'] == {}

def test_get(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    response = client.get('/foo')

  assert response.status == 200

def test_get_data(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    data = client.get_data('/foo')

  assert data == 'dummy_data'

def test_get_object(httpserver):

  httpserver.expect_request(
      '/api/v3/dummy_types/dummy_id',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 'foo',
              'bar': 'bar'}
  }})

  with new_client(httpserver) as client:
    obj = client.get_object('/dummy_types/dummy_id')

  assert obj.id == 'dummy_id'
  assert obj.type == 'dummy_type'
  assert obj.foo == 'foo'
  assert obj.bar == 'bar'

  assert obj.get('foo') == 'foo'
  assert obj.get('bar') == 'bar'
  assert obj.get('baz') is None

def test_patch_object(httpserver):

  obj = Object('dummy_type', 'dummy_id', {'foo': 1, 'bar': 2})
  obj.foo = 2

  httpserver.expect_request(
      '/api/v3/dummy_types/dummy_id',
      method='PATCH',
      headers={'X-Apikey': 'dummy_api_key'},
      data=json.dumps({'data': obj.to_dict(modified_attributes_only=True)}),
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 2,
          }
      }
  })

  with new_client(httpserver) as client:
    client.patch_object('/dummy_types/dummy_id', obj=obj)

def test_post_object(httpserver):

  obj = Object('dummy_type')
  obj.foo = 'foo'

  httpserver.expect_request(
      '/api/v3/dummy_types',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'},
      data=json.dumps({'data': obj.to_dict()}),
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'dummy_type',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    obj = client.post_object('/dummy_types', obj=obj)

  assert obj.id == 'dummy_id'

def test_delete(httpserver):

  httpserver.expect_request(
      '/api/v3/foo',
      method='DELETE',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': 'dummy_data'
  })

  with new_client(httpserver) as client:
    response = client.delete('/foo')

  assert response.status == 200

def test_iterator(httpserver):

  httpserver.expect_request(
      '/api/v3/dummy_collection/foo',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': [{
          'id': 'dummy_id_1',
          'type': 'dummy_type',
          'attributes': {'order': 0}
          }]
  })

  with new_client(httpserver) as client:
    it = client.iterator('/dummy_collection/{}', 'foo')
    for i, obj in enumerate(it):
      assert 0 == i

def test_download_file(httpserver):

  httpserver.expect_request(
      '/api/v3/files/01020304050607080900a0b0c0d0e0f/download',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data('filecontent')

  with new_client(httpserver) as client:
    with io.BytesIO() as f:
      client.download_file('01020304050607080900a0b0c0d0e0f', f)
      f.seek(0)
      assert f.read() == b'filecontent'

def test_scan_file(httpserver):

  upload_url = (
      'http://' + httpserver.host + ':' + str(httpserver.port) + '/upload')

  httpserver.expect_oneshot_request(
      '/api/v3/files/upload_url',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': upload_url
  })

  httpserver.expect_oneshot_request(
      '/upload',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'analysis',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    f = io.StringIO("dummy file")
    analysis = client.scan_file(f)

  assert analysis.type == 'analysis'

def test_scan_url(httpserver):

  httpserver.expect_request(
      '/api/v3/urls',
      method='POST',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'data': {
          'id': 'dummy_id',
          'type': 'analysis',
          'attributes': {
              'foo': 'foo',
          }
      }
  })

  with new_client(httpserver) as client:
    analysis = client.scan_url('https://www.dummy.url')

  assert analysis.type == 'analysis'

def test_feed(httpserver):

  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030405',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data(
    bz2.compress(b'{\"type\": \"file\", \"id\": \"dummy_file_id_1\"}'))

  # The feed iterator should tolerate missing feed packages, so let's return
  # a NotFoundError for package 200102030406.
  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030406',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_json({
      'error': {
          'code': 'NotFoundError'
    }}, status=404)

  httpserver.expect_ordered_request(
      '/api/v3/feeds/files/200102030407',
      method='GET',
      headers={'X-Apikey': 'dummy_api_key'}
  ).respond_with_data(
    bz2.compress(b'{\"type\": \"file\", \"id\": \"dummy_file_id_2\"}'))

  with new_client(httpserver) as client:
    feed = client.feed(FeedType.FILES, cursor='200102030405')
    feed_iterator = feed.__iter__()
    obj = next(feed_iterator)
    assert obj.type == 'file'
    assert obj.id == 'dummy_file_id_1'
    obj = next(feed_iterator)
    assert obj.type == 'file'
    assert obj.id == 'dummy_file_id_2'
<|endoftext|>


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

# Copyright(C) 2010-2011  Romain Bignon
#
# This file is part of weboob.
#
# weboob 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.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo

class DailymotionTest(BackendTest):
    BACKEND = 'dailymotion'

    def test_search(self):
        l = list(self.backend.search_videos('chirac'))
        self.assertTrue(len(l) > 0)
        v = l[0]
        self.backend.fillobj(v, ('url',))
        self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
        self.backend.browser.openurl(v.url)

    def test_latest(self):
        l = list(self.backend.iter_resources([BaseVideo], [u'latest']))
        assert len(l)
        v = l[0]
        self.backend.fillobj(v, ('url',))
        self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
<commit_msg>dailymotion: Chose a random video to test<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2010-2011  Romain Bignon
#
# This file is part of weboob.
#
# weboob 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.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.

from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo

from random import choice

class DailymotionTest(BackendTest):
    BACKEND = 'dailymotion'

    def test_search(self):
        l = list(self.backend.search_videos('chirac'))
        self.assertTrue(len(l) > 0)
        v = choice(l)
        self.backend.fillobj(v, ('url',))
        self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
        self.backend.browser.openurl(v.url)

    def test_latest(self):
        l = list(self.backend.iter_resources([BaseVideo], [u'latest']))
        assert len(l)
        v = choice(l)
        self.backend.fillobj(v, ('url',))
        self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
<|endoftext|>


<commit_before>"""
Created on 4 Oct 2017

@author: Bruno Beloff (bruno.beloff@southcoastscience.com)

https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
"""

import socket

from abc import ABC, abstractmethod
from collections import OrderedDict
from subprocess import Popen, DEVNULL, TimeoutExpired

from scs_core.sys.ipv4_address import IPv4Address

# --------------------------------------------------------------------------------------------------------------------

class Node(ABC):
    """
    classdocs
    """

    @classmethod
    def scan_accessible_subnets(cls, start=1, end=254, timeout=10.0):
        for dot_decimal in cls.scan_subnet(cls.server_ipv4_address(), start=start, end=end, timeout=timeout):
            yield dot_decimal

        for dot_decimal in cls.scan_subnet(cls.ipv4_address(), start=start, end=end, timeout=timeout):
            yield dot_decimal

    @classmethod
    def scan_subnet(cls, ipv4_address, start=1, end=254, timeout=10.0):
        if ipv4_address is None:
            return

        pings = OrderedDict()

        # start...
        for addr in ipv4_address.lso_range(start, end):
            dot_decimal = addr.dot_decimal()
            pings[dot_decimal] = Popen(['ping', '-n', '-q', '-c', '1', '-t', str(timeout), dot_decimal],
                                       stdout=DEVNULL, stderr=DEVNULL)
        # report...
        for dot_decimal in pings:
            try:
                pings[dot_decimal].wait(timeout=timeout * 2.0)
            except (TimeoutExpired, TimeoutError):
                continue

            if pings[dot_decimal].returncode == 0:
                yield dot_decimal

    @staticmethod
    def ping(host, timeout=1.0):
        p = Popen(['ping', '-q', '-c', '1', '-t', str(timeout), host],
                  stdout=DEVNULL, stderr=DEVNULL)
        p.wait()

        return p.returncode == 0

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    @abstractmethod
    def name(cls):
        pass

    @staticmethod
    def ipv4_address():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        try:
            s.connect(('192.168.0.1', 1))               # host does not need to be reachable
            dot_decimal = s.getsockname()[0]

        except OSError:
            dot_decimal = '127.0.0.1'

        finally:
            s.close()

        return IPv4Address.construct(dot_decimal)

    @classmethod
    @abstractmethod
    def server_ipv4_address(cls):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def ndir_spi_bus(self):
        pass

    @abstractmethod
    def ndir_spi_device(self):
        pass

    @abstractmethod
    def opc_spi_bus(self):
        pass

    @abstractmethod
    def opc_spi_device(self):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def disk_usage(self, volume):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def time_is_synchronized(self):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def home_dir(self):
        pass

    @abstractmethod
    def lock_dir(self):
        pass

    @abstractmethod
    def tmp_dir(self):
        pass

    @abstractmethod
    def command_dir(self):
        pass

    @abstractmethod
    def scs_dir(self):
        pass

    @abstractmethod
    def conf_dir(self):
        pass

    @abstractmethod
    def aws_dir(self):
        pass

    @abstractmethod
    def osio_dir(self):
        pass

    @abstractmethod
    def eep_image(self):
        pass

    @abstractmethod
    def software_update_report(self):
        pass

<commit_msg>Added disk_volume.py top-level script<commit_after>"""
Created on 4 Oct 2017

@author: Bruno Beloff (bruno.beloff@southcoastscience.com)

https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
"""

import socket

from abc import ABC, abstractmethod
from collections import OrderedDict
from subprocess import Popen, DEVNULL, TimeoutExpired

from scs_core.sys.ipv4_address import IPv4Address

# --------------------------------------------------------------------------------------------------------------------

class Node(ABC):
    """
    classdocs
    """

    @classmethod
    def scan_accessible_subnets(cls, start=1, end=254, timeout=10.0):
        for dot_decimal in cls.scan_subnet(cls.server_ipv4_address(), start=start, end=end, timeout=timeout):
            yield dot_decimal

        for dot_decimal in cls.scan_subnet(cls.ipv4_address(), start=start, end=end, timeout=timeout):
            yield dot_decimal

    @classmethod
    def scan_subnet(cls, ipv4_address, start=1, end=254, timeout=10.0):
        if ipv4_address is None:
            return

        pings = OrderedDict()

        # start...
        for addr in ipv4_address.lso_range(start, end):
            dot_decimal = addr.dot_decimal()
            pings[dot_decimal] = Popen(['ping', '-n', '-q', '-c', '1', '-t', str(timeout), dot_decimal],
                                       stdout=DEVNULL, stderr=DEVNULL)
        # report...
        for dot_decimal in pings:
            try:
                pings[dot_decimal].wait(timeout=timeout * 2.0)
            except (TimeoutExpired, TimeoutError):
                continue

            if pings[dot_decimal].returncode == 0:
                yield dot_decimal

    @staticmethod
    def ping(host, timeout=1.0):
        p = Popen(['ping', '-q', '-c', '1', '-t', str(timeout), host],
                  stdout=DEVNULL, stderr=DEVNULL)
        p.wait()

        return p.returncode == 0

    # ----------------------------------------------------------------------------------------------------------------

    @classmethod
    @abstractmethod
    def name(cls):
        pass

    @staticmethod
    def ipv4_address():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        try:
            s.connect(('192.168.0.1', 1))               # host does not need to be reachable
            dot_decimal = s.getsockname()[0]

        except OSError:
            dot_decimal = '127.0.0.1'

        finally:
            s.close()

        return IPv4Address.construct(dot_decimal)

    @classmethod
    @abstractmethod
    def server_ipv4_address(cls):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def ndir_spi_bus(self):
        pass

    @abstractmethod
    def ndir_spi_device(self):
        pass

    @abstractmethod
    def opc_spi_bus(self):
        pass

    @abstractmethod
    def opc_spi_device(self):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def disk_volume(self, volume):
        pass

    @abstractmethod
    def disk_usage(self, volume):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def time_is_synchronized(self):
        pass

    # ----------------------------------------------------------------------------------------------------------------

    @abstractmethod
    def home_dir(self):
        pass

    @abstractmethod
    def lock_dir(self):
        pass

    @abstractmethod
    def tmp_dir(self):
        pass

    @abstractmethod
    def command_dir(self):
        pass

    @abstractmethod
    def scs_dir(self):
        pass

    @abstractmethod
    def conf_dir(self):
        pass

    @abstractmethod
    def aws_dir(self):
        pass

    @abstractmethod
    def osio_dir(self):
        pass

    @abstractmethod
    def eep_image(self):
        pass

    @abstractmethod
    def software_update_report(self):
        pass

<|endoftext|>


<commit_before>#!/usr/bin/env python
import numpy as np
import pandas as pd
from StringIO import StringIO
import json, sys, re, string, logging
import matplotlib.pyplot as plt
import collections
pd.options.display.mpl_style = 'default'

class RecursiveDict(dict):
    """https://gist.github.com/Xjs/114831"""
    def rec_update(self, other):
        for key,value in other.iteritems():
            if key in self and \
               isinstance(self[key], dict) and \
               isinstance(value, dict):
                self[key] = RecursiveDict(self[key])
                self[key].rec_update(value)
            else:
                self[key] = value

class RecursiveParser:
    def __init__(self):
        self.symbol = '>'
        self.min_level = 3 # minimum level to avoid collision w/ '>>'
        self.max_level = 6 # maximum section-nesting supported
        self.level = self.max_level # level counter
        self.section_titles = [None] * (self.max_level-self.min_level+1)
        self.document = RecursiveDict({})
        # TODO better organize read_csv options -> config file?
        self.default_options = { 'sep': ',', 'header': 0 } # csv data
        self.colon_key_value_list = { 'sep': ':', 'header': None, 'index_col': 0 }
        self.special_options = {
            'general': self.colon_key_value_list,
            'plot': self.colon_key_value_list
        }

    def separator_regex(self):
        """get separator regex for section depth/level"""
        return r'\n*%s{%d}(.+)\n*' % (self.symbol, self.level)

    def clean_title(self, title):
        """strip in-line comments & spaces, make lower-case"""
        return re.split(r'#*', title)[0].strip().lower()

    def read_csv(self, title, body):
        """run pandas.read_csv on (sub)section body"""
        return pd.read_csv(
            StringIO(body), comment='#', skipinitialspace=True, squeeze=True,
            **self.special_options.get(title, self.default_options)
        )

    def to_dict(self, pandas_object):
        """convert pandas object to dict"""
        if isinstance(pandas_object, pd.Series):
            return pandas_object.to_dict()
        all_columns_numeric = True
        for col in pandas_object.columns:
            if ( pandas_object[col].dtype != np.float64 and \
                pandas_object[col].dtype != np.int64 ):
                all_columns_numeric = False
                break
        return pandas_object.to_dict(
            outtype = 'list' if all_columns_numeric else 'records'
        )

    def recursive_parse(self, file_string):
        """recursively parse sections according to number of separators"""
        logging.info('-> new level: %d', self.level)
        # return if below minimum section level
        if self.level < self.min_level:
            section_titles = filter(None, self.section_titles)
            pd_obj = self.read_csv(section_titles[-1], file_string)
            logging.info(pd_obj)
            nested_dict = self.to_dict(pd_obj)
            for key in reversed(section_titles):
                nested_dict = {key: nested_dict}
            self.document.rec_update(nested_dict)
            logging.info('=========')
            return
        # split into section title line (even) and section body (odd entries)
        sections = re.split(self.separator_regex(), file_string)
        if len(sections) > 1:
            logging.info('separator %s found', self.symbol*self.level)
            sections = sections[1:] # https://docs.python.org/2/library/re.html#re.split
            for section_index,section_body in enumerate(sections[1::2]):
                clean_title = self.clean_title(sections[2*section_index])
                level_index = self.max_level - self.level
                self.section_titles[level_index] = clean_title
                logging.info(self.section_titles)
                self.level -= 1
                self.recursive_parse(section_body)
                self.level += 1
                self.section_titles[level_index] = None
        else:
            # separator not found -> file_string = sections[0]
            self.level -= 1
            self.recursive_parse(file_string)
            self.level += 1

def plot(filename):
    """plot all data based on output.json (-> plot.ly in future?)"""
    doc = json.load(open(filename,'r'))
    for key,value in doc.iteritems():
        if key == 'general': continue
        value_is_dict = isinstance(value, dict)
        data = value.get('data') if value_is_dict else value
        fig, ax = plt.subplots(1, 1)
        plotopts = value.get('plot', {}) if value_is_dict else {}
        #if plotopts.get('table'): ax.get_xaxis().set_visible(False)
        pd.DataFrame.from_dict(data).plot(ax=ax, **plotopts)
        plt.savefig('png/%s' % key.replace(' ','_'), dpi=300, bbox_inches='tight')

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("infile", help="mp-formatted csv file")
    parser.add_argument("--log", help="show log output", action="store_true")
    args = parser.parse_args()
    loglevel = 'DEBUG' if args.log else 'WARNING'
    logging.basicConfig(
        format='%(message)s', level=getattr(logging, loglevel)
    )
    filestr = open(args.infile,'r').read()
    csv_parser = RecursiveParser()
    csv_parser.recursive_parse(filestr)
    json.dump(
        csv_parser.document, open('output.json','wb'),
        indent=2, sort_keys=True
    )
    plot('output.json')
<commit_msg>add tsv support<commit_after>#!/usr/bin/env python
import numpy as np
import pandas as pd
from StringIO import StringIO
import json, sys, re, string, logging
import matplotlib.pyplot as plt
import collections
pd.options.display.mpl_style = 'default'

class RecursiveDict(dict):
    """https://gist.github.com/Xjs/114831"""
    def rec_update(self, other):
        for key,value in other.iteritems():
            if key in self and \
               isinstance(self[key], dict) and \
               isinstance(value, dict):
                self[key] = RecursiveDict(self[key])
                self[key].rec_update(value)
            else:
                self[key] = value

class RecursiveParser:
    def __init__(self, fileExt='csv'):
        self.symbol = '>'
        self.min_level = 3 # minimum level to avoid collision w/ '>>'
        self.max_level = 6 # maximum section-nesting supported
        self.level = self.max_level # level counter
        self.section_titles = [None] * (self.max_level-self.min_level+1)
        self.document = RecursiveDict({})
        # TODO better organize read_csv options -> config file?
        self.default_options = {
            'sep': '\t' if fileExt == 'tsv' else ',', 'header': 0 # data
        }
        self.colon_key_value_list = { 'sep': ':', 'header': None, 'index_col': 0 }
        self.special_options = {
            'general': self.colon_key_value_list,
            'plot': self.colon_key_value_list
        }

    def separator_regex(self):
        """get separator regex for section depth/level"""
        return r'\n*%s{%d}(.+)\n*' % (self.symbol, self.level)

    def clean_title(self, title):
        """strip in-line comments & spaces, make lower-case"""
        return re.split(r'#*', title)[0].strip().lower()

    def read_csv(self, title, body):
        """run pandas.read_csv on (sub)section body"""
        return pd.read_csv(
            StringIO(body), comment='#', skipinitialspace=True, squeeze=True,
            **self.special_options.get(title, self.default_options)
        )

    def to_dict(self, pandas_object):
        """convert pandas object to dict"""
        if isinstance(pandas_object, pd.Series):
            return pandas_object.to_dict()
        all_columns_numeric = True
        for col in pandas_object.columns:
            if ( pandas_object[col].dtype != np.float64 and \
                pandas_object[col].dtype != np.int64 ):
                all_columns_numeric = False
                break
        return pandas_object.to_dict(
            outtype = 'list' if all_columns_numeric else 'records'
        )

    def recursive_parse(self, file_string):
        """recursively parse sections according to number of separators"""
        logging.info('-> new level: %d', self.level)
        # return if below minimum section level
        if self.level < self.min_level:
            section_titles = filter(None, self.section_titles)
            pd_obj = self.read_csv(section_titles[-1], file_string)
            logging.info(pd_obj)
            nested_dict = self.to_dict(pd_obj)
            for key in reversed(section_titles):
                nested_dict = {key: nested_dict}
            self.document.rec_update(nested_dict)
            logging.info('=========')
            return
        # split into section title line (even) and section body (odd entries)
        sections = re.split(self.separator_regex(), file_string)
        if len(sections) > 1:
            logging.info('separator %s found', self.symbol*self.level)
            sections = sections[1:] # https://docs.python.org/2/library/re.html#re.split
            for section_index,section_body in enumerate(sections[1::2]):
                clean_title = self.clean_title(sections[2*section_index])
                level_index = self.max_level - self.level
                self.section_titles[level_index] = clean_title
                logging.info(self.section_titles)
                self.level -= 1
                self.recursive_parse(section_body)
                self.level += 1
                self.section_titles[level_index] = None
        else:
            # separator not found -> file_string = sections[0]
            self.level -= 1
            self.recursive_parse(file_string)
            self.level += 1

def plot(filename):
    """plot all data based on output.json (-> plot.ly in future?)"""
    doc = json.load(open(filename,'r'))
    for key,value in doc.iteritems():
        if key == 'general': continue
        value_is_dict = isinstance(value, dict)
        data = value.get('data') if value_is_dict else value
        fig, ax = plt.subplots(1, 1)
        plotopts = value.get('plot', {}) if value_is_dict else {}
        #if plotopts.get('table'): ax.get_xaxis().set_visible(False)
        pd.DataFrame.from_dict(data).plot(ax=ax, **plotopts)
        plt.savefig('png/%s' % key.replace(' ','_'), dpi=300, bbox_inches='tight')

if __name__ == '__main__':
    import argparse, os
    parser = argparse.ArgumentParser()
    parser.add_argument("infile", help="mp-formatted csv/tsv file")
    parser.add_argument("--log", help="show log output", action="store_true")
    args = parser.parse_args()
    loglevel = 'DEBUG' if args.log else 'WARNING'
    logging.basicConfig(
        format='%(message)s', level=getattr(logging, loglevel)
    )
    filestr = open(args.infile,'r').read()
    # init RecursiveParser with file extension to identify data column separator
    csv_parser = RecursiveParser(os.path.splitext(args.infile)[1][1:])
    csv_parser.recursive_parse(filestr)
    json.dump(
        csv_parser.document, open('output.json','wb'),
        indent=2, sort_keys=True
    )
    plot('output.json')
<|endoftext|>


<commit_before>#! /usr/bin/env python
#  -*- coding:  UTF-8 -*-
__author__ = 'Sean Yu'
__mail__ = 'try.dash.now@gmail.com'
'''
created 2015/10/7
'''

import os, sys
from pprint import pprint
pardir =os.path.dirname(os.path.realpath(os.getcwd()))
subfolder = ['lib', 'dut']
for sub in subfolder:
    libpath = os.path.sep.join([pardir,sub])
    if libpath not in sys.path:
        sys.path.insert(0,libpath)
if __name__ == "__main__":
    #sr.py suite_file range [arg1 arg2 ...]
    import re,datetime
    from Parser import suiteParser
    from runner import createLogDir

    suitefile =sys.argv[1]
    name = '-'.join(sys.argv[1:])
    name = re.sub('[^\w\-_]','-',name)[:60]+' '+datetime.datetime.now().isoformat(':').replace(':','')
    name = re.sub('-+', '-', name)
    name = re.sub('^-*', '', name)
    def  GetRange(caserange='all'):
        if str(caserange).strip().lower()=='all':
            caserange = 'all'
        else:
            caserange = str(caserange).strip()
            caserange = str(caserange).split(',')
            drange = []
            for i in caserange:
                if str(i).find('-')!=-1:
                    s,e =str(i).split('-')
                    i = list(range(int(s)-1,int(e)))
                else:
                    i =[int(i)-1]
                drange =drange+i
            caserange= sorted(drange)
        CaseRange=caserange
        return CaseRange
    suitelogdir = '../../log'
    rangelist = sys.argv[2]
    arglist = sys.argv[3:]
    if not os.path.exists(suitelogdir):
        os.mkdir(suitelogdir)

    suitelogdir = createLogDir(name, suitelogdir)
    st = suiteParser(name, suitelogdir)
    lstRange = GetRange(rangelist )
    statsTotalCase, suite= st.load(suitefile, arglist, lstRange)

    benchfile =''
    benchinfo ={}
    from runner import createLogger
    logger = createLogger('suite.txt', suitelogdir)
    logger.info('suite name\t%s'%(sys.argv[1]))
    logger.info('suite range\t%s'%(sys.argv[2]))

    index = 1
    for i in sys.argv[3:]:
        logger.info('arg%d\t%s'%(index, i))
        index+=1
    report = []
    dut_pool={}
#    3, break, run_case_in_suite, rc.py2 home_case.csv home.csv full
#	9, concurrent, loop, 2 ,no_stop ,rc.py5 home_case.csv home.csv full
#	9, concurren,FailAction,FuncName,cmd =case, run_case_in_suite, rc.py7 home_case.csv home.csv full
    index = 1
    from runner import run_case_in_suite , releaseDUTs , initDUT,case_runner, createLogDir, array2html, run1case

    import time, re
    returncode =1
    errormessage =''
    suiteStartTime = time.time()
    suiteEndTime = time.time()
    statsPass =0
    statsFail =0
    lstFailCase = []

    htmlstring = array2html(suitefile,rangelist,','.join(arglist), statsTotalCase,statsFail+statsPass,statsPass,statsFail, statsTotalCase-statsFail-statsPass,report, suiteStartTime, suiteEndTime)
    reportfilename = '../../log/%s.html'%(name)
    with open(reportfilename, 'wb') as f:
        f.write(htmlstring.encode(encoding='utf_8', errors='strict'))
    for caseline in suite:
        breakFlag=False
        caseStartTime = time.time()
        LineNo,FailAction,[FuncName,cmd ]=caseline
        logger.info('to run case: index %d, name: %s, failAction: %s'%(LineNo,cmd, FailAction))
        from runner import concurrent
        print cmd
        if FuncName == run_case_in_suite:
            casename ='%d-%s'%(caseline[0], re.sub('[^\w\-_.]','-',cmd[:80]))#index

        elif FuncName == concurrent:
            casename ='%d-%s'%(caseline[0], re.sub('[^\w\-_.]','-',cmd[0][4][:80]))#index
        casename = re.sub('-+','-',casename)
        casename = re.sub('^-*','',casename)
        index+=1
        import os
        logpath = suitelogdir#+"/%s"%casename
        logger.info('creating logdir: %s'%logpath)
        if not os.path.exists(logpath):
            os.mkdir(logpath)

        shareData ={}
        if FuncName == run_case_in_suite:
            suite_dir_name = os.path.basename(logpath)
            logdir = '%s/%s'%(logpath,casename)
            if not os.path.exists(logdir):
                os.mkdir(logdir)
            #logdir =createLogDir(casename, logpath)# logpath#

            returncode = 0
            logger.info('running case: %s'%cmd)

            returncode , errormessage ,benchfile, benchinfo, dut_pool = run1case(casename, cmd, benchfile, benchinfo, dut_pool, logdir, logger ,shareData)
            caseEndTime = time.time()
            ExecutionDuration = caseEndTime-caseStartTime
            caseResult = 'PASS'

            if returncode:
                logger.error('FAIL\t%s'%cmd)
                logger.error(errormessage)
                caseResult ='FAIL'
                lstFailCase.append(caseline)
                statsFail+=1
            else:
                logger.info('PASS\t%s'%cmd)
                statsPass+=1
            logdir = '%s/%s'%(suite_dir_name, casename)
            NewRecord = [index-1,caseResult,caseline[2][1], errormessage,logdir, LineNo,ExecutionDuration,caseStartTime,caseEndTime ]
            pprint("RESULT:", NewRecord)

            #reportname, ArgStr, CaseRangeStr, TOTAL,CASERUN, CASEPASS,CASEFAIL, CASENOTRUN, Report,htmllogdir
            report.append(NewRecord)
            if returncode:
                if FailAction.strip().lower()=='break':
                    breakFlag=True

        elif FuncName == concurrent:
            conCaseTotal, conCasePass, conCaseFail, conReport, conLstFailCase , breakFlag=concurrent(index-1, logpath, caseline[2][1],report, logger, shareData)
            statsPass+=conCasePass
            statsFail+=conCaseFail
            for x in conLstFailCase:
                lstFailCase.append(x)

        print('Pass:',statsPass, 'Fail', statsFail)
        suiteEndTime = time.time()
        htmlstring = array2html(suitefile,rangelist,','.join(arglist), statsTotalCase,statsFail+statsPass,statsPass,statsFail, statsTotalCase-statsFail-statsPass,report, suiteStartTime, suiteEndTime)
        reportfilename = '../../log/%s.html'%(name)
        with open(reportfilename, 'wb') as f:
            f.write(htmlstring.encode(encoding='utf_8', errors='strict'))

        if breakFlag:
            break
        casename='%d'%index
        logdir ='../../log/'+suitefile+'/'+casename

    #if dut_pool.__len__()!={}:
    releaseDUTs(dut_pool, logger)

    print('#'*80)
    print('Pass:',statsPass, 'Fail', statsFail)
    os._exit(0)

<commit_msg>sr.py: change print out format<commit_after>#! /usr/bin/env python
#  -*- coding:  UTF-8 -*-
__author__ = 'Sean Yu'
__mail__ = 'try.dash.now@gmail.com'
'''
created 2015/10/7
'''

import os, sys
from pprint import pprint
pardir =os.path.dirname(os.path.realpath(os.getcwd()))
subfolder = ['lib', 'dut']
for sub in subfolder:
    libpath = os.path.sep.join([pardir,sub])
    if libpath not in sys.path:
        sys.path.insert(0,libpath)
if __name__ == "__main__":
    #sr.py suite_file range [arg1 arg2 ...]
    import re,datetime
    from Parser import suiteParser
    from runner import createLogDir

    suitefile =sys.argv[1]
    name = '-'.join(sys.argv[1:])
    name = re.sub('[^\w\-_]','-',name)[:60]+' '+datetime.datetime.now().isoformat(':').replace(':','')
    name = re.sub('-+', '-', name)
    name = re.sub('^-*', '', name)
    def  GetRange(caserange='all'):
        if str(caserange).strip().lower()=='all':
            caserange = 'all'
        else:
            caserange = str(caserange).strip()
            caserange = str(caserange).split(',')
            drange = []
            for i in caserange:
                if str(i).find('-')!=-1:
                    s,e =str(i).split('-')
                    i = list(range(int(s)-1,int(e)))
                else:
                    i =[int(i)-1]
                drange =drange+i
            caserange= sorted(drange)
        CaseRange=caserange
        return CaseRange
    suitelogdir = '../../log'
    rangelist = sys.argv[2]
    arglist = sys.argv[3:]
    if not os.path.exists(suitelogdir):
        os.mkdir(suitelogdir)

    suitelogdir = createLogDir(name, suitelogdir)
    st = suiteParser(name, suitelogdir)
    lstRange = GetRange(rangelist )
    statsTotalCase, suite= st.load(suitefile, arglist, lstRange)

    benchfile =''
    benchinfo ={}
    from runner import createLogger
    logger = createLogger('suite.txt', suitelogdir)
    logger.info('suite name\t%s'%(sys.argv[1]))
    logger.info('suite range\t%s'%(sys.argv[2]))

    index = 1
    for i in sys.argv[3:]:
        logger.info('arg%d\t%s'%(index, i))
        index+=1
    report = []
    dut_pool={}
#    3, break, run_case_in_suite, rc.py2 home_case.csv home.csv full
#	9, concurrent, loop, 2 ,no_stop ,rc.py5 home_case.csv home.csv full
#	9, concurren,FailAction,FuncName,cmd =case, run_case_in_suite, rc.py7 home_case.csv home.csv full
    index = 1
    from runner import run_case_in_suite , releaseDUTs , initDUT,case_runner, createLogDir, array2html, run1case

    import time, re
    returncode =1
    errormessage =''
    suiteStartTime = time.time()
    suiteEndTime = time.time()
    statsPass =0
    statsFail =0
    lstFailCase = []

    htmlstring = array2html(suitefile,rangelist,','.join(arglist), statsTotalCase,statsFail+statsPass,statsPass,statsFail, statsTotalCase-statsFail-statsPass,report, suiteStartTime, suiteEndTime)
    reportfilename = '../../log/%s.html'%(name)
    with open(reportfilename, 'wb') as f:
        f.write(htmlstring.encode(encoding='utf_8', errors='strict'))
    for caseline in suite:
        breakFlag=False
        caseStartTime = time.time()
        LineNo,FailAction,[FuncName,cmd ]=caseline
        logger.info('to run case: index %d, name: %s, failAction: %s'%(LineNo,cmd, FailAction))
        from runner import concurrent
        print cmd
        if FuncName == run_case_in_suite:
            casename ='%d-%s'%(caseline[0], re.sub('[^\w\-_.]','-',cmd[:80]))#index

        elif FuncName == concurrent:
            casename ='%d-%s'%(caseline[0], re.sub('[^\w\-_.]','-',cmd[0][4][:80]))#index
        casename = re.sub('-+','-',casename)
        casename = re.sub('^-*','',casename)
        index+=1
        import os
        logpath = suitelogdir#+"/%s"%casename
        logger.info('creating logdir: %s'%logpath)
        if not os.path.exists(logpath):
            os.mkdir(logpath)

        shareData ={}
        if FuncName == run_case_in_suite:
            suite_dir_name = os.path.basename(logpath)
            logdir = '%s/%s'%(logpath,casename)
            if not os.path.exists(logdir):
                os.mkdir(logdir)
            #logdir =createLogDir(casename, logpath)# logpath#

            returncode = 0
            logger.info('running case: %s'%cmd)

            returncode , errormessage ,benchfile, benchinfo, dut_pool = run1case(casename, cmd, benchfile, benchinfo, dut_pool, logdir, logger ,shareData)
            caseEndTime = time.time()
            ExecutionDuration = caseEndTime-caseStartTime
            caseResult = 'PASS'

            if returncode:
                logger.error('FAIL\t%s'%cmd)
                logger.error(errormessage)
                caseResult ='FAIL'
                lstFailCase.append(caseline)
                statsFail+=1
            else:
                logger.info('PASS\t%s'%cmd)
                statsPass+=1
            logdir = '%s/%s'%(suite_dir_name, casename)
            NewRecord = [index-1,caseResult,caseline[2][1], errormessage,logdir, LineNo,ExecutionDuration,caseStartTime,caseEndTime ]
            print("RESULT:")
            pprint(NewRecord)

            #reportname, ArgStr, CaseRangeStr, TOTAL,CASERUN, CASEPASS,CASEFAIL, CASENOTRUN, Report,htmllogdir
            report.append(NewRecord)
            if returncode:
                if FailAction.strip().lower()=='break':
                    breakFlag=True

        elif FuncName == concurrent:
            conCaseTotal, conCasePass, conCaseFail, conReport, conLstFailCase , breakFlag=concurrent(index-1, logpath, caseline[2][1],report, logger, shareData)
            statsPass+=conCasePass
            statsFail+=conCaseFail
            for x in conLstFailCase:
                lstFailCase.append(x)

        print('Pass:',statsPass, 'Fail', statsFail)
        suiteEndTime = time.time()
        htmlstring = array2html(suitefile,rangelist,','.join(arglist), statsTotalCase,statsFail+statsPass,statsPass,statsFail, statsTotalCase-statsFail-statsPass,report, suiteStartTime, suiteEndTime)
        reportfilename = '../../log/%s.html'%(name)
        with open(reportfilename, 'wb') as f:
            f.write(htmlstring.encode(encoding='utf_8', errors='strict'))

        if breakFlag:
            break
        casename='%d'%index
        logdir ='../../log/'+suitefile+'/'+casename

    #if dut_pool.__len__()!={}:
    releaseDUTs(dut_pool, logger)

    print('#'*80)
    print('Pass:',statsPass, 'Fail', statsFail)
    os._exit(0)

<|endoftext|>


<commit_before>import nifti
<commit_msg>Again, update pynifti package doc.<commit_after>from info import __doc__

import nifti
<|endoftext|>