<commit_before>from operator import attrgetter
from django.utils.translation import ugettext_noop
from corehq.apps.groups.models import Group

ASHA_ROLE = ugettext_noop('ASHA')
AWW_ROLE = ugettext_noop('AWW')

def get_team_members(group):
        """
        Get any commcare users that are either "asha" or "aww".
        """
        users = group.get_users(only_commcare=True)

        def is_team_member(user):
            role = user.user_data.get('role', '')
            return role == ASHA_ROLE or role == AWW_ROLE

        return sorted([u for u in users if is_team_member(u)],
                      key=lambda u: u.user_data['role'])

def get_all_owner_ids(user_ids):
    all_group_ids = [
        row['id']
        for row in Group.get_db().view(
            'groups/by_user',
            keys=user_ids,
            include_docs=False
        )
    ]
    return set(user_ids).union(set(all_group_ids))

def get_all_owner_ids_from_group(group):
    return get_all_owner_ids([user.get_id for user in get_team_members(group)])

def get_calculation(owner_ids, slug):
    from bihar.models import CareBiharFluff
    r = CareBiharFluff.aggregate_results(slug, (
        ['care-bihar', owner_id] for owner_id in owner_ids
    ), reduce=True)
    num = r.get('numerator')
    total = r.get('total')
    r = CareBiharFluff.aggregate_results(slug, (
        ['care-bihar', owner_id] for owner_id in owner_ids
    ), reduce=False)
    num_cases = ', '.join(r.get('numerator', ()))
    total_cases = ', '.join(r.get('total', ()))
    return num, total, num_cases, total_cases

def get_all_calculations(owner_ids):

    from bihar.reports.indicators.indicators import IndicatorConfig, INDICATOR_SETS

    config = IndicatorConfig(INDICATOR_SETS)
    for indicator_set in config.indicator_sets:
        print indicator_set.name
        for indicator in indicator_set.get_indicators():
            slug = indicator.slug()
            yield (indicator.name,) + get_calculation(owner_ids, slug)

def get_groups_for_group(group):
    """This is a helper function only called locally"""
    owner_ids = list(get_all_owner_ids_from_group(group))
    db = Group.get_db()
    rows = db.view('_all_docs', keys=owner_ids, include_docs=True)
    groups = []
    for row in rows:
        doc = row['doc']
        if doc['doc_type'] == 'Group':
            groups.append(Group.wrap(doc))

    groups.sort(key=attrgetter('name'))
    return groups<commit_msg>fix previous change<commit_after>from operator import attrgetter
from django.utils.translation import ugettext_noop
from corehq.apps.groups.models import Group

ASHA_ROLE = ugettext_noop('ASHA')
AWW_ROLE = ugettext_noop('AWW')

def get_team_members(group):
        """
        Get any commcare users that are either "asha" or "aww".
        """
        users = group.get_users(only_commcare=True)

        def is_team_member(user):
            role = user.user_data.get('role', '')
            return role == ASHA_ROLE or role == AWW_ROLE

        return sorted([u for u in users if is_team_member(u)],
                      key=lambda u: u.user_data['role'])

def get_all_owner_ids(user_ids):
    all_group_ids = [
        row['id']
        for row in Group.get_db().view(
            'groups/by_user',
            keys=user_ids,
            include_docs=False
        )
    ]
    return set(user_ids).union(set(all_group_ids))

def get_all_owner_ids_from_group(group):
    return get_all_owner_ids([user.get_id for user in get_team_members(group)])

def get_calculation(owner_ids, slug):
    from bihar.models import CareBiharFluff
    r = CareBiharFluff.aggregate_results(slug, (
        ['care-bihar', owner_id] for owner_id in owner_ids
    ), reduce=True)
    num = r.get('numerator')
    total = r.get('total')
    r = CareBiharFluff.aggregate_results(slug, (
        ['care-bihar', owner_id] for owner_id in owner_ids
    ), reduce=False)
    num_cases = ', '.join(r.get('numerator', ()))
    total_cases = ', '.join(r.get('total', ()))
    return num or '', total, num_cases, total_cases

def get_all_calculations(owner_ids):

    from bihar.reports.indicators.indicators import IndicatorConfig, INDICATOR_SETS

    config = IndicatorConfig(INDICATOR_SETS)
    for indicator_set in config.indicator_sets:
        print indicator_set.name
        for indicator in indicator_set.get_indicators():
            slug = indicator.slug
            yield (indicator.name,) + get_calculation(owner_ids, slug)

def get_groups_for_group(group):
    """This is a helper function only called locally"""
    owner_ids = list(get_all_owner_ids_from_group(group))
    db = Group.get_db()
    rows = db.view('_all_docs', keys=owner_ids, include_docs=True)
    groups = []
    for row in rows:
        doc = row['doc']
        if doc['doc_type'] == 'Group':
            groups.append(Group.wrap(doc))

    groups.sort(key=attrgetter('name'))
    return groups<|endoftext|>


<commit_before>import os
import copy

from os.path import relpath, join, exists, dirname, basename
from os import makedirs
from json import load

from tools.export.exporters import Exporter, apply_supported_whitelist
from tools.targets import TARGET_MAP
from tools.utils import NotSupportedException
from tools.build_api import prepare_toolchain

POST_BINARY_WHITELIST = set([
    "TEENSY3_1Code.binary_hook",
    "MCU_NRF51Code.binary_hook",
    "LPCTargetCode.lpc_patch",
    "LPC4088Code.binary_hook"
])

class GNUARMNetbeans(Exporter):
    NAME = 'GNU ARM Netbeans'
    TOOLCHAIN = 'GCC_ARM'

    @classmethod
    def is_target_supported(cls, target_name):
        target = TARGET_MAP[target_name]
        return apply_supported_whitelist(
            cls.TOOLCHAIN, POST_BINARY_WHITELIST, target)

    @staticmethod
    def prepare_sys_lib(libname):
        return "-l" + libname

    def toolchain_flags(self, toolchain):
        """Returns a dictionary of toolchain flags.
        Keys of the dictionary are:
        cxx_flags    - c++ flags
        c_flags      - c flags
        ld_flags     - linker flags
        asm_flags    - assembler flags
        common_flags - common options

        The difference from the above is that it takes a parameter.
        """

        # Note: use the config options from the currently selected toolchain.
        config_header = self.toolchain.get_config_header()

        flags = {key + "_flags": copy.deepcopy(value) for key, value
                 in toolchain.flags.iteritems()}
        if config_header:
            config_header = relpath(config_header,
                                    self.resources.file_basepath[config_header])
            header_options = self.toolchain.get_config_option(config_header)
            flags['c_flags'] += header_options
            flags['cxx_flags'] += header_options
        return flags

    @staticmethod
    def get_defines_and_remove_from_flags(flags_in, str_key):
        defines = []
        flags_temp = copy.deepcopy(flags_in)
        for f in flags_temp[str_key]:
            f = f.strip()
            if f.startswith('-D'):
                defines.append(f[2:])
                flags_in[str_key].remove(f)

        return defines

    @staticmethod
    def get_includes_and_remove_from_flags(flags_in, str_key):
        includes = []
        flags_temp = copy.deepcopy(flags_in)
        next_is_include = False
        for f in flags_temp[str_key]:
            f = f.strip()
            if next_is_include:
                includes.append(f)
                flags_in[str_key].remove(f)
                next_is_include = False
                continue
            if f == "-include":
                flags_in[str_key].remove(f)
                next_is_include = True

        return includes

    @staticmethod
    def get_c_std_and_remove_from_flag(flags_in, str_key):
        comp_std = ''
        c_std = {
            'c90': 'c90', 'c89': 'c90', 'gnu90': 'gnu90', 'gnu89': 'gnu90',
            'c99': 'c99', 'c9x': 'c99', 'gnu99': 'gnu99', 'gnu9x': 'gnu98',
            'c11': 'c11', 'c1x': 'c11', 'gnu11': 'gnu11', 'gnu1x': 'gnu11'
        }
        cpp_std = {
            'c++98': 'cpp98', 'c++03': 'cpp98',
            'gnu++98': 'gnucpp98', 'gnu++03': 'gnucpp98',
            'c++0x': 'cpp0x', 'gnu++0x': 'gnucpp0x',
            'c++11': 'cpp11', 'gnu++11': 'gnucpp11',
            'c++1y': 'cpp1y', 'gnu++1y': 'gnucpp1y',
            'c++14': 'cpp14', 'gnu++14': 'gnucpp14',
            'c++1z': 'cpp1z', 'gnu++1z': 'gnucpp1z',
        }

        flags_temp = copy.deepcopy(flags_in)
        for f in flags_temp[str_key]:
            f = f.strip()
            if f.startswith('-std='):
                comp_std = f[len('-std='):]
                flags_in[str_key].remove(f)
            elif f.startswith('-'):
                std = f[len('-'):]
                if std in c_std or std in cpp_std:
                    comp_std = std
                    flags_in[str_key].remove(f)
        return comp_std

    def validate_resources(self):
        if not self.resources.linker_script:
            raise NotSupportedException("No linker script found.")

    def create_jinja_ctx(self):
        self.options = {}
        flags = {}
        self.validate_resources()
        # Convert all Backslashes to Forward Slashes
        self.resources.win_to_unix()

        print 'Include folders: {0}'.format(len(self.resources.inc_dirs))

        print 'Symbols: {0}'.format(len(self.toolchain.get_symbols()))

        self.ld_script = self.filter_dot(
            self.resources.linker_script)
        print 'Linker script: {0}'.format(self.ld_script)

        # Read in all profiles, we'll extract compiler options.
        profiles = self.get_all_profiles()

        profile_ids = [s.lower() for s in profiles]
        profile_ids.sort()
        for prof_id in profile_ids:
            # There are 4 categories of options, a category common too
            # all tools and a specific category for each of the tools.
            opts = {}
            opts['defines'] = {}
            opts['common'] = {}
            opts['as'] = {}
            opts['c'] = {}
            opts['cpp'] = {}
            opts['ld'] = {}

            opts['id'] = prof_id
            opts['name'] = opts['id'].capitalize()

            print
            print 'Build configuration: {0}'.format(opts['name'])

            profile = profiles[prof_id]

            # A small hack, do not bother with src_path again,
            # pass an empty string to avoid crashing.
            src_paths = ['']
            target_name = self.toolchain.target.name

            toolchain = prepare_toolchain(
                src_paths, "", target_name, self.TOOLCHAIN, build_profile=[profile])

            flags = self.toolchain_flags(toolchain)

            print 'Common flags:', ' '.join(flags['common_flags'])
            print 'C++ flags:', ' '.join(flags['cxx_flags'])
            print 'C flags:', ' '.join(flags['c_flags'])
            print 'ASM flags:', ' '.join(flags['asm_flags'])
            print 'Linker flags:', ' '.join(flags['ld_flags'])

            opts['defines'] = self.get_defines_and_remove_from_flags(flags, 'common_flags')
            opts['forced_includes'] = self.get_includes_and_remove_from_flags(flags, 'common_flags')
            opts['common'] = flags['common_flags']
            opts['as'] = flags['asm_flags']
            opts['c'] = flags['c_flags']
            opts['cpp'] = flags['cxx_flags']
            opts['ld'] = flags['ld_flags']

            self.options[prof_id] = opts

        sources = []  # list of strings

        forced_includes = self.get_includes_and_remove_from_flags(flags, 'c_flags')
        forced_includes += self.get_includes_and_remove_from_flags(flags, 'cxx_flags')

        # Remove Duplicates
        forced_includes = list(set(forced_includes))

        c_std = self.get_c_std_and_remove_from_flag(flags, 'c_flags')
        cpp_std = self.get_c_std_and_remove_from_flag(flags, 'cxx_flags')

        # Make one list of all resources
        for r_type in ['c_sources', 's_sources', 'cpp_sources']:
            sources.extend(getattr(self.resources, r_type))

        # Remove all leading './'
        c_sources = [self.filter_dot(field) for field in self.resources.c_sources]
        cpp_sources = [self.filter_dot(field) for field in self.resources.cpp_sources]
        s_sources = [self.filter_dot(field) for field in self.resources.s_sources]
        headers = [self.filter_dot(field) for field in self.resources.headers]
        sources = [self.filter_dot(field) for field in sources]
        include_paths = [self.filter_dot(field) for field in self.resources.inc_dirs]

        sys_libs = [self.prepare_sys_lib(lib) for lib
                    in self.toolchain.sys_libs]
        preproc = " ".join([basename(self.toolchain.preproc[0])] +
                           self.toolchain.preproc[1:] +
                           self.toolchain.ld[1:])

        if 'nbproject' in include_paths:
            include_paths.remove('nbproject')

        jinja_ctx = {
            'name': self.project_name,
            'target': self.toolchain.target.name,
            'elf_location': join('BUILD', self.project_name) + '.elf',
            'c_symbols': self.toolchain.get_symbols(),
            'asm_symbols': self.toolchain.get_symbols(True),
            'c_flags': flags['c_flags'],
            'cxx_flags': flags['cxx_flags'],
            'ld_flags': self.flags['ld_flags'],
            'asm_flags': self.flags['asm_flags'],
            'common_flags': self.flags['common_flags'],
            'include_paths': include_paths,
            'forced_includes': forced_includes,
            'c_sources': c_sources,
            'cpp_sources': cpp_sources,
            's_sources': s_sources,
            'headers': headers,
            'headers_folder': self.get_netbeans_file_list(sorted(headers)),
            'sources_folder': self.get_netbeans_file_list(sorted(sources)),
            'options': self.options,
            'c_std': self.get_netbeans_c_std(c_std),
            'cpp_std': self.get_netbeans_cpp_std(cpp_std),
            'linker_script': self.ld_script,
            'linker_libs': sys_libs,
            'pp_cmd': preproc,
            'cc_cmd': self.toolchain.cc[0],
            'cppc_cmd': self.toolchain.cppc[0],
            'asm_cmd': self.toolchain.asm[0],
            'ld_cmd': self.toolchain.ld[0],
            'elf2bin_cmd': self.toolchain.elf2bin
        }
        return jinja_ctx

    def generate(self):
        """Generate Makefile, configurations.xml & project.xml Netbeans project file
        """
        jinja_ctx = self.create_jinja_ctx()

        print
        print 'Create a GNU ARM Netbeans C++ managed project'
        print 'Project name: {0}'.format(self.project_name)
        print 'Target: {0}'.format(self.toolchain.target.name)
        print 'Toolchain: {0}'.format(self.TOOLCHAIN)

        if not exists(join(self.export_dir, 'nbproject')):
            makedirs(join(self.export_dir, 'nbproject'))

        self.gen_file('nb/configurations.tmpl', jinja_ctx, 'nbproject/configurations.xml')
        self.gen_file('nb/project.tmpl', jinja_ctx, 'nbproject/project.xml')
        self.gen_file('nb/mbedignore.tmpl', jinja_ctx, '.mbedignore')
        self.gen_file('nb/Makefile.tmpl', jinja_ctx, 'Makefile')

        print
        print 'Done. Import the \'{0}\' project in Netbeans.'.format(self.project_name)

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

    @staticmethod
    def filter_dot(str_in):
        """
        Remove the './' prefix, if present.
        This function assumes that resources.win_to_unix()
        replaced all windows backslashes with slashes.
        """
        if str_in is None:
            return None
        if str_in[:2] == './':
            return str_in[2:]
        return str_in

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

    @staticmethod
    def get_all_profiles():
        tools_path = dirname(dirname(dirname(__file__)))
        file_names = [join(tools_path, "profiles", fn) for fn in os.listdir(
            join(tools_path, "profiles")) if fn.endswith(".json")]

        profiles = {}

        for fn in file_names:
            content = load(open(fn))
            profile_name = basename(fn).replace(".json", "")
            profiles[profile_name] = content

        return profiles

    @staticmethod
    def get_netbeans_file_list(file_list):
        cur_dir = ''
        prev_dir = ''
        output = []
        folder_count = 1
        dir_depth = 0
        for item in file_list:
            cur_dir = os.path.dirname(item)
            dir_temp = os.path.normpath(cur_dir)
            prev_dir_temp = os.path.normpath(prev_dir)
            dir_list = dir_temp.split(os.sep)
            prev_dir_list = prev_dir_temp.split(os.sep)
            dir_depth = len(dir_list)

            # Current File is in Directory: Compare the given dir with previous Dir
            if cur_dir and prev_dir != cur_dir:
                # evaluate all matched items (from current and previous list)
                matched = []
                # Compare the Element in Previous Dir with the Elements in Current Dir
                # and add the Differences to the match-List
                if len(dir_list) <= len(prev_dir_list):
                    for idx, element in enumerate(dir_list):
                        if element == prev_dir_list[idx]:
                            matched.append(element)
                else:
                    for idx, element in enumerate(prev_dir_list):
                        if element == dir_list[idx]:
                            matched.append(element)

                # calculate difference between matched and length
                diff = dir_depth - len(matched)

                # if previous dir was not root
                if prev_dir != '':
                    # if the elements count is not equal we calculate the difference
                    if len(dir_list) != len(prev_dir_list):
                        dir_depth_prev = len(prev_dir_list)
                        delta = dir_depth_prev - len(matched)

                        for i in range(dir_depth_prev - delta, dir_depth_prev):
                            output.append('</logicalFolder>')

                    # if the elements count is equal, we subtract the matched length from the total length
                    else:
                        for i in range(len(matched), len(dir_list)):
                            output.append('</logicalFolder>')

                for i in range(dir_depth - diff, dir_depth):
                    output.append('<logicalFolder name="f' + str(folder_count) + '" displayName="' + str(
                        dir_list[i]) + '" projectFiles="true">')
                    folder_count += 1

            # Current File is in root
            else:
                # Close Tag if we are in root and the previous dir wasn't
                if cur_dir == '' and prev_dir != '':
                    for i in range(0, len(prev_dir_list)):
                        output.append('</logicalFolder>')

            # Save the Current Dir
            prev_dir = cur_dir
            output.append('<itemPath>' + str(item) + '</itemPath>')

        if cur_dir != '':
            # close all open tags
            output.append('</logicalFolder>' * dir_depth)

        return output

    @staticmethod
    def get_netbeans_c_std(c_std):
        c_std_netbeans = 0
        if '89' in c_std:
            c_std_netbeans = 2
        elif '99' in c_std:
            c_std_netbeans = 3
        elif '11' in c_std:
            c_std_netbeans = 10
        return c_std_netbeans

    @staticmethod
    def get_netbeans_cpp_std(cpp_std):
        cpp_std_netbeans = 0
        if '98' in cpp_std:
            cpp_std_netbeans = 4
        elif '11' in cpp_std:
            cpp_std_netbeans = 8
        elif '14' in cpp_std:
            cpp_std_netbeans = 11
        return cpp_std_netbeans
<commit_msg>Updated List comparision with shorter implementaion using python build-in function zip()<commit_after>import os
import copy

from os.path import relpath, join, exists, dirname, basename
from os import makedirs
from json import load

from tools.export.exporters import Exporter, apply_supported_whitelist
from tools.targets import TARGET_MAP
from tools.utils import NotSupportedException
from tools.build_api import prepare_toolchain

POST_BINARY_WHITELIST = set([
    "TEENSY3_1Code.binary_hook",
    "MCU_NRF51Code.binary_hook",
    "LPCTargetCode.lpc_patch",
    "LPC4088Code.binary_hook"
])

class GNUARMNetbeans(Exporter):
    NAME = 'GNU ARM Netbeans'
    TOOLCHAIN = 'GCC_ARM'

    @classmethod
    def is_target_supported(cls, target_name):
        target = TARGET_MAP[target_name]
        return apply_supported_whitelist(
            cls.TOOLCHAIN, POST_BINARY_WHITELIST, target)

    @staticmethod
    def prepare_sys_lib(libname):
        return "-l" + libname

    def toolchain_flags(self, toolchain):
        """Returns a dictionary of toolchain flags.
        Keys of the dictionary are:
        cxx_flags    - c++ flags
        c_flags      - c flags
        ld_flags     - linker flags
        asm_flags    - assembler flags
        common_flags - common options

        The difference from the above is that it takes a parameter.
        """

        # Note: use the config options from the currently selected toolchain.
        config_header = self.toolchain.get_config_header()

        flags = {key + "_flags": copy.deepcopy(value) for key, value
                 in toolchain.flags.iteritems()}
        if config_header:
            config_header = relpath(config_header,
                                    self.resources.file_basepath[config_header])
            header_options = self.toolchain.get_config_option(config_header)
            flags['c_flags'] += header_options
            flags['cxx_flags'] += header_options
        return flags

    @staticmethod
    def get_defines_and_remove_from_flags(flags_in, str_key):
        defines = []
        flags_temp = copy.deepcopy(flags_in)
        for f in flags_temp[str_key]:
            f = f.strip()
            if f.startswith('-D'):
                defines.append(f[2:])
                flags_in[str_key].remove(f)

        return defines

    @staticmethod
    def get_includes_and_remove_from_flags(flags_in, str_key):
        includes = []
        flags_temp = copy.deepcopy(flags_in)
        next_is_include = False
        for f in flags_temp[str_key]:
            f = f.strip()
            if next_is_include:
                includes.append(f)
                flags_in[str_key].remove(f)
                next_is_include = False
                continue
            if f == "-include":
                flags_in[str_key].remove(f)
                next_is_include = True

        return includes

    @staticmethod
    def get_c_std_and_remove_from_flag(flags_in, str_key):
        comp_std = ''
        c_std = {
            'c90': 'c90', 'c89': 'c90', 'gnu90': 'gnu90', 'gnu89': 'gnu90',
            'c99': 'c99', 'c9x': 'c99', 'gnu99': 'gnu99', 'gnu9x': 'gnu98',
            'c11': 'c11', 'c1x': 'c11', 'gnu11': 'gnu11', 'gnu1x': 'gnu11'
        }
        cpp_std = {
            'c++98': 'cpp98', 'c++03': 'cpp98',
            'gnu++98': 'gnucpp98', 'gnu++03': 'gnucpp98',
            'c++0x': 'cpp0x', 'gnu++0x': 'gnucpp0x',
            'c++11': 'cpp11', 'gnu++11': 'gnucpp11',
            'c++1y': 'cpp1y', 'gnu++1y': 'gnucpp1y',
            'c++14': 'cpp14', 'gnu++14': 'gnucpp14',
            'c++1z': 'cpp1z', 'gnu++1z': 'gnucpp1z',
        }

        flags_temp = copy.deepcopy(flags_in)
        for f in flags_temp[str_key]:
            f = f.strip()
            if f.startswith('-std='):
                comp_std = f[len('-std='):]
                flags_in[str_key].remove(f)
            elif f.startswith('-'):
                std = f[len('-'):]
                if std in c_std or std in cpp_std:
                    comp_std = std
                    flags_in[str_key].remove(f)
        return comp_std

    def validate_resources(self):
        if not self.resources.linker_script:
            raise NotSupportedException("No linker script found.")

    def create_jinja_ctx(self):
        self.options = {}
        flags = {}
        self.validate_resources()
        # Convert all Backslashes to Forward Slashes
        self.resources.win_to_unix()

        print 'Include folders: {0}'.format(len(self.resources.inc_dirs))

        print 'Symbols: {0}'.format(len(self.toolchain.get_symbols()))

        self.ld_script = self.filter_dot(
            self.resources.linker_script)
        print 'Linker script: {0}'.format(self.ld_script)

        # Read in all profiles, we'll extract compiler options.
        profiles = self.get_all_profiles()

        profile_ids = [s.lower() for s in profiles]
        profile_ids.sort()
        for prof_id in profile_ids:
            # There are 4 categories of options, a category common too
            # all tools and a specific category for each of the tools.
            opts = {}
            opts['defines'] = {}
            opts['common'] = {}
            opts['as'] = {}
            opts['c'] = {}
            opts['cpp'] = {}
            opts['ld'] = {}

            opts['id'] = prof_id
            opts['name'] = opts['id'].capitalize()

            print
            print 'Build configuration: {0}'.format(opts['name'])

            profile = profiles[prof_id]

            # A small hack, do not bother with src_path again,
            # pass an empty string to avoid crashing.
            src_paths = ['']
            target_name = self.toolchain.target.name

            toolchain = prepare_toolchain(
                src_paths, "", target_name, self.TOOLCHAIN, build_profile=[profile])

            flags = self.toolchain_flags(toolchain)

            print 'Common flags:', ' '.join(flags['common_flags'])
            print 'C++ flags:', ' '.join(flags['cxx_flags'])
            print 'C flags:', ' '.join(flags['c_flags'])
            print 'ASM flags:', ' '.join(flags['asm_flags'])
            print 'Linker flags:', ' '.join(flags['ld_flags'])

            opts['defines'] = self.get_defines_and_remove_from_flags(flags, 'common_flags')
            opts['forced_includes'] = self.get_includes_and_remove_from_flags(flags, 'common_flags')
            opts['common'] = flags['common_flags']
            opts['as'] = flags['asm_flags']
            opts['c'] = flags['c_flags']
            opts['cpp'] = flags['cxx_flags']
            opts['ld'] = flags['ld_flags']

            self.options[prof_id] = opts

        sources = []  # list of strings

        forced_includes = self.get_includes_and_remove_from_flags(flags, 'c_flags')
        forced_includes += self.get_includes_and_remove_from_flags(flags, 'cxx_flags')

        # Remove Duplicates
        forced_includes = list(set(forced_includes))

        c_std = self.get_c_std_and_remove_from_flag(flags, 'c_flags')
        cpp_std = self.get_c_std_and_remove_from_flag(flags, 'cxx_flags')

        # Make one list of all resources
        for r_type in ['c_sources', 's_sources', 'cpp_sources']:
            sources.extend(getattr(self.resources, r_type))

        # Remove all leading './'
        c_sources = [self.filter_dot(field) for field in self.resources.c_sources]
        cpp_sources = [self.filter_dot(field) for field in self.resources.cpp_sources]
        s_sources = [self.filter_dot(field) for field in self.resources.s_sources]
        headers = [self.filter_dot(field) for field in self.resources.headers]
        sources = [self.filter_dot(field) for field in sources]
        include_paths = [self.filter_dot(field) for field in self.resources.inc_dirs]

        sys_libs = [self.prepare_sys_lib(lib) for lib
                    in self.toolchain.sys_libs]
        preproc = " ".join([basename(self.toolchain.preproc[0])] +
                           self.toolchain.preproc[1:] +
                           self.toolchain.ld[1:])

        if 'nbproject' in include_paths:
            include_paths.remove('nbproject')

        jinja_ctx = {
            'name': self.project_name,
            'target': self.toolchain.target.name,
            'elf_location': join('BUILD', self.project_name) + '.elf',
            'c_symbols': self.toolchain.get_symbols(),
            'asm_symbols': self.toolchain.get_symbols(True),
            'c_flags': flags['c_flags'],
            'cxx_flags': flags['cxx_flags'],
            'ld_flags': self.flags['ld_flags'],
            'asm_flags': self.flags['asm_flags'],
            'common_flags': self.flags['common_flags'],
            'include_paths': include_paths,
            'forced_includes': forced_includes,
            'c_sources': c_sources,
            'cpp_sources': cpp_sources,
            's_sources': s_sources,
            'headers': headers,
            'headers_folder': self.get_netbeans_file_list(sorted(headers)),
            'sources_folder': self.get_netbeans_file_list(sorted(sources)),
            'options': self.options,
            'c_std': self.get_netbeans_c_std(c_std),
            'cpp_std': self.get_netbeans_cpp_std(cpp_std),
            'linker_script': self.ld_script,
            'linker_libs': sys_libs,
            'pp_cmd': preproc,
            'cc_cmd': self.toolchain.cc[0],
            'cppc_cmd': self.toolchain.cppc[0],
            'asm_cmd': self.toolchain.asm[0],
            'ld_cmd': self.toolchain.ld[0],
            'elf2bin_cmd': self.toolchain.elf2bin
        }
        return jinja_ctx

    def generate(self):
        """Generate Makefile, configurations.xml & project.xml Netbeans project file
        """
        jinja_ctx = self.create_jinja_ctx()

        print
        print 'Create a GNU ARM Netbeans C++ managed project'
        print 'Project name: {0}'.format(self.project_name)
        print 'Target: {0}'.format(self.toolchain.target.name)
        print 'Toolchain: {0}'.format(self.TOOLCHAIN)

        if not exists(join(self.export_dir, 'nbproject')):
            makedirs(join(self.export_dir, 'nbproject'))

        self.gen_file('nb/configurations.tmpl', jinja_ctx, 'nbproject/configurations.xml')
        self.gen_file('nb/project.tmpl', jinja_ctx, 'nbproject/project.xml')
        self.gen_file('nb/mbedignore.tmpl', jinja_ctx, '.mbedignore')
        self.gen_file('nb/Makefile.tmpl', jinja_ctx, 'Makefile')

        print
        print 'Done. Import the \'{0}\' project in Netbeans.'.format(self.project_name)

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

    @staticmethod
    def filter_dot(str_in):
        """
        Remove the './' prefix, if present.
        This function assumes that resources.win_to_unix()
        replaced all windows backslashes with slashes.
        """
        if str_in is None:
            return None
        if str_in[:2] == './':
            return str_in[2:]
        return str_in

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

    @staticmethod
    def get_all_profiles():
        tools_path = dirname(dirname(dirname(__file__)))
        file_names = [join(tools_path, "profiles", fn) for fn in os.listdir(
            join(tools_path, "profiles")) if fn.endswith(".json")]

        profiles = {}

        for fn in file_names:
            content = load(open(fn))
            profile_name = basename(fn).replace(".json", "")
            profiles[profile_name] = content

        return profiles

    @staticmethod
    def get_netbeans_file_list(file_list):
        cur_dir = ''
        prev_dir = ''
        output = []
        folder_count = 1
        dir_depth = 0
        for item in file_list:
            cur_dir = os.path.dirname(item)
            dir_temp = os.path.normpath(cur_dir)
            prev_dir_temp = os.path.normpath(prev_dir)
            dir_list = dir_temp.split(os.sep)
            prev_dir_list = prev_dir_temp.split(os.sep)
            dir_depth = len(dir_list)

            # Current File is in Directory: Compare the given dir with previous Dir
            if cur_dir and prev_dir != cur_dir:
                # evaluate all matched items (from current and previous list)
                matched = []
                # Compare the Element in Previous Dir with the Elements in Current Dir
                # and add the equal Elements to the match-List
                for elem_prev_dir, elem_cur_dir in zip(prev_dir_list, dir_list):
                    if elem_prev_dir == elem_cur_dir:
                        matched.append(elem_cur_dir)

                # calculate difference between matched and length
                diff = dir_depth - len(matched)

                # if previous dir was not root
                if prev_dir != '':
                    # if the elements count is not equal we calculate the difference
                    if len(dir_list) != len(prev_dir_list):
                        dir_depth_prev = len(prev_dir_list)
                        delta = dir_depth_prev - len(matched)

                        for i in range(dir_depth_prev - delta, dir_depth_prev):
                            output.append('</logicalFolder>')

                    # if the elements count is equal, we subtract the matched length from the total length
                    else:
                        for i in range(len(matched), len(dir_list)):
                            output.append('</logicalFolder>')

                for i in range(dir_depth - diff, dir_depth):
                    output.append('<logicalFolder name="f' + str(folder_count) + '" displayName="' + str(
                        dir_list[i]) + '" projectFiles="true">')
                    folder_count += 1

            # Current File is in root
            else:
                # Close Tag if we are in root and the previous dir wasn't
                if cur_dir == '' and prev_dir != '':
                    for i in range(0, len(prev_dir_list)):
                        output.append('</logicalFolder>')

            # Save the Current Dir
            prev_dir = cur_dir
            output.append('<itemPath>' + str(item) + '</itemPath>')

        if cur_dir != '':
            # close all open tags
            output.append('</logicalFolder>' * dir_depth)

        return output

    @staticmethod
    def get_netbeans_c_std(c_std):
        c_std_netbeans = 0
        if '89' in c_std:
            c_std_netbeans = 2
        elif '99' in c_std:
            c_std_netbeans = 3
        elif '11' in c_std:
            c_std_netbeans = 10
        return c_std_netbeans

    @staticmethod
    def get_netbeans_cpp_std(cpp_std):
        cpp_std_netbeans = 0
        if '98' in cpp_std:
            cpp_std_netbeans = 4
        elif '11' in cpp_std:
            cpp_std_netbeans = 8
        elif '14' in cpp_std:
            cpp_std_netbeans = 11
        return cpp_std_netbeans
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Commands:
.autoop add channel nick!ident@example.org         adds hostmask to channels auto-op list
.autoop remove channel nick!ident@example.org      removes hostmask from channels auto-op list
.autoop list <channel>                             lists ops in current channel or channel
.autoop status                                      lists channels where user is op
.op
"""

from __future__ import unicode_literals, print_function, division
import sqlite3
import re

COMMANDS = ['add', 'remove', 'list', 'status']
CHANNEL_PREFIXES = '&#!+'

def init(botconfig):
    open_DB(True)

def open_DB(createTable=False):
    conn = sqlite3.connect('module_autoop.db')
    c = conn.cursor()
    if createTable:
        c.execute('CREATE TABLE IF NOT EXISTS autoops (channel, hostmask, modes);')
        conn.commit()
    return conn, c

def add_op(channel, hostmask, modes='o'):
    if not get_op_status(channel, hostmask):
        conn, c = open_DB()
        c.execute('INSERT INTO autoops VALUES (?, ?, ?);', (channel, hostmask, modes))
        conn.commit()
        conn.close()
        return True

def remove_op(channel, hostmask):
    if get_op_status(channel, hostmask):
        conn, c = open_DB()
        c.execute('DELETE FROM autoops WHERE channel = ? AND hostmask = ?;', (channel, hostmask))
        conn.commit()
        conn.close()
        return True

def get_op_status(channel, hostmask):
    conn, c = open_DB()
    c.execute('SELECT * FROM autoops WHERE channel = ? AND hostmask = ? LIMIT 1;', (channel, hostmask))
    if c.fetchone():
        retval = True
    else:
        retval = False
    conn.close()
    return retval

def get_user_channels(hostmask):
    conn, c = open_DB()
    c.execute('SELECT channel FROM autoops WHERE hostmask = ?;', (hostmask,))
    rows = c.fetchall()
    conn.close()
    return rows

def get_ops(channel):
    conn, c = open_DB()
    c.execute('SELECT hostmask FROM autoops WHERE channel = ?;', (channel,))
    rows = c.fetchall()
    conn.close()
    return rows

def check_hostmask(hostmask):
    rg = re.compile('.*?(!).*?(@).*?', re.IGNORECASE | re.DOTALL)  # regex for checking hostmask format
    if re.match(rg, hostmask):
        return True
    return False

def op_user(bot, user, channel):
    nick = getNick(user)
    bot.log('auto-opping %s on %s' % (user, channel))
    bot.mode(channel, True, 'o', user=nick)

def handle_userJoined(bot, user, channel):
    if isAdmin(user) or get_op_status(channel, user):
        op_user(bot, user, channel)

def command_autoop(bot, user, channel, args):
    args = args.split()
    command = args[0]
    if command not in COMMANDS:
        return bot.say(channel, 'Invalid command, valid commands are %s' % ', '.join(map(str, COMMANDS)))

    if command == 'list':
        if len(args) > 1:
            list_channel = args[1]
        else:
            list_channel = channel
        if list_channel[0] not in CHANNEL_PREFIXES:
            return bot.say(channel, 'Channel name must start with one of "%s"' % CHANNEL_PREFIXES)

        ops = get_ops(list_channel)
        if ops:
            return bot.say(channel, '%s ops: %s' % (list_channel, ', '.join(str(i[0]) for i in ops)))
        return bot.say(channel, "%s doesn't have any ops." % list_channel)

    elif command == 'status':
        if channel[0] not in CHANNEL_PREFIXES:
            channel_list = get_user_channels(user)
            if channel_list:
                return bot.say(channel, "You're auto-opped on %s" % ', '.join(str(i[0]) for i in channel_list))
            return bot.say(channel, "You're not auto-opped on any channel.")
        else:
            if get_op_status(channel, user):
                return bot.say(channel, "You're op in %s." % channel)
            return bot.say(channel, "You're not an op in %s." % channel)

    else:
        if isAdmin(user):
            if len(args) < 3:
                return bot.say(channel, 'Command must have channel and hostmask as arguments.')

            if not (args[1][0] in CHANNEL_PREFIXES and check_hostmask(args[2])):
                return bot.say(channel, 'Channel name must start with one of "%s" and hostmask must be in format nick!ident@hostma.sk' % CHANNEL_PREFIXES)

            if command == 'add':
                if add_op(args[1], args[2]):
                    return bot.say(channel, 'OP added.')
                return bot.say(channel, 'Adding OP failed, is user already auto-opped?')
            elif command == 'remove':
                if remove_op(args[1], args[2]):
                    return bot.say(channel, 'OP removed.')
                return bot.say(channel, 'Removing OP failed, is user the user auto-opped?')
        else:
            return bot.say(channel, 'You must be bot admin to use this feature.')

def command_op(bot, user, channel, args):
    if isAdmin(user) or get_op_status(channel, user):
        op_user(bot, user, channel)
<commit_msg>Clean up autoop module a bit<commit_after># -*- coding: utf-8 -*-
"""
Commands:
.autoop add channel nick!ident@example.org         adds hostmask to channels auto-op list
.autoop remove channel nick!ident@example.org      removes hostmask from channels auto-op list
.autoop list <channel>                             lists ops in current channel or channel
.autoop status                                      lists channels where user is op
.op
"""

from __future__ import unicode_literals, print_function, division
import sqlite3
import re

COMMANDS = ['add', 'remove', 'list', 'status']
CHANNEL_PREFIXES = '&#!+'

def init(botconfig):
    open_DB(True)

def open_DB(createTable=False):
    conn = sqlite3.connect('module_autoop.db')
    c = conn.cursor()
    if createTable:
        c.execute('CREATE TABLE IF NOT EXISTS autoops (channel, hostmask, modes);')
        conn.commit()
    return conn, c

def add_op(channel, hostmask, modes='o'):
    if not get_op_status(channel, hostmask):
        conn, c = open_DB()
        c.execute('INSERT INTO autoops VALUES (?, ?, ?);', (channel, hostmask, modes))
        conn.commit()
        conn.close()
        return True

def remove_op(channel, hostmask):
    if get_op_status(channel, hostmask):
        conn, c = open_DB()
        c.execute('DELETE FROM autoops WHERE channel = ? AND hostmask = ?;', (channel, hostmask))
        conn.commit()
        conn.close()
        return True

def get_op_status(channel, hostmask):
    conn, c = open_DB()
    c.execute('SELECT * FROM autoops WHERE channel = ? AND hostmask = ? LIMIT 1;', (channel, hostmask))
    if c.fetchone():
        retval = True
    else:
        retval = False
    conn.close()
    return retval

def get_user_channels(hostmask):
    conn, c = open_DB()
    c.execute('SELECT channel FROM autoops WHERE hostmask = ?;', (hostmask,))
    rows = c.fetchall()
    conn.close()
    return rows

def get_ops(channel):
    conn, c = open_DB()
    c.execute('SELECT hostmask FROM autoops WHERE channel = ?;', (channel,))
    rows = c.fetchall()
    conn.close()
    return rows

def check_hostmask(hostmask):
    rg = re.compile('.*?(!).*?(@).*?', re.IGNORECASE | re.DOTALL)  # regex for checking hostmask format
    if re.match(rg, hostmask):
        return True
    return False

def op_user(bot, user, channel):
    nick = getNick(user)
    bot.log('auto-opping %s on %s' % (user, channel))
    bot.mode(channel, True, 'o', user=nick)

def handle_userJoined(bot, user, channel):
    if isAdmin(user) or get_op_status(channel, user):
        op_user(bot, user, channel)

def command_autoop(bot, user, channel, args):
    """Usage: .autoop [add|remove|list|status]"""

    if not args:
        return bot.say(channel, 'Valid commands are %s' % ', '.join(map(str, COMMANDS)))

    args = args.split()
    command = args[0]
    if command not in COMMANDS:
        return bot.say(channel, 'Invalid command, valid commands are %s' % ', '.join(map(str, COMMANDS)))

    if command == 'list':
        if len(args) > 1:
            list_channel = args[1]
        else:
            list_channel = channel
        if list_channel[0] not in CHANNEL_PREFIXES:
            return bot.say(channel, 'Channel name must start with one of "%s"' % CHANNEL_PREFIXES)

        ops = get_ops(list_channel)
        if ops:
            return bot.say(channel, '%s ops: %s' % (list_channel, ', '.join(str(i[0]) for i in ops)))
        return bot.say(channel, "%s doesn't have any ops." % list_channel)

    elif command == 'status':
        if channel[0] not in CHANNEL_PREFIXES:
            channel_list = get_user_channels(user)
            if channel_list:
                return bot.say(channel, "You're auto-opped on %s" % ', '.join(str(i[0]) for i in channel_list))
            return bot.say(channel, "You're not auto-opped on any channel.")
        else:
            if get_op_status(channel, user):
                return bot.say(channel, "You're op in %s." % channel)
            return bot.say(channel, "You're not an op in %s." % channel)

    else:
        if isAdmin(user):
            if len(args) < 3:
                return bot.say(channel, 'Command must have channel and hostmask as arguments.')

            if not (args[1][0] in CHANNEL_PREFIXES and check_hostmask(args[2])):
                return bot.say(channel, 'Channel name must start with one of "%s" and hostmask must be in format nick!ident@hostma.sk' % CHANNEL_PREFIXES)

            if command == 'add':
                if add_op(args[1], args[2]):
                    return bot.say(channel, 'OP added.')
                return bot.say(channel, 'Adding OP failed, is user already auto-opped?')
            elif command == 'remove':
                if remove_op(args[1], args[2]):
                    return bot.say(channel, 'OP removed.')
                return bot.say(channel, 'Removing OP failed, is user the user auto-opped?')
        else:
            return bot.say(channel, 'You must be bot admin to use this feature.')

def command_op(bot, user, channel, args):
    """Get op status from bot if you are authorized"""
    if isAdmin(user) or get_op_status(channel, user):
        op_user(bot, user, channel)
<|endoftext|>


<commit_before>from _collections import defaultdict
import logging
from optparse import make_option

from django.core.management.base import BaseCommand
from six import iteritems

from ... import Bungiesearch
from ...utils import update_index

class Command(BaseCommand):
    args = ''
    help = 'Manage search index.'

    option_list = BaseCommand.option_list + (
        make_option('--create',
            action='store_const',
            dest='action',
            const='create',
            help='Create the index specified in the settings with the mapping generating from the search indices.'),
        make_option('--update',
            action='store_const',
            dest='action',
            const='update',
            help='Update the index specified in the settings with the mapping generating from the search indices.'),
        make_option('--update-mapping',
            action='store_const',
            dest='action',
            const='update-mapping',
            help='Update the mapping of specified models (or all models) on the index specified in the settings.'),
        make_option('--delete',
            action='store_const',
            dest='action',
            const='delete',
            help='Delete the index specified in the settings. Requires the "--guilty-as-charged" flag.'),
        make_option('--delete-mapping',
            action='store_const',
            dest='action',
            const='delete-mapping',
            help='Delete the mapping of specified models (or all models) on the index specified in the settings. Requires the "--guilty-as-charged" flag.'),
        make_option('--guilty-as-charged',
            action='store_true',
            dest='confirmed',
            default=False,
            help='Flag needed to delete an index.'),
        make_option('--models',
            action='store',
            dest='models',
            default=None,
            help='Models to be updated, separated by commas. If none are specified, then all models defined in the index will be updated.'),
        make_option('--index',
            action='store',
            dest='index',
            default=None,
            help='Specify the index for which to apply the action, as defined in BUNGIESEARCH.INDEXES of settings. Defaults to using all indices.'),
        make_option('--bulk-size',
            action='store',
            dest='bulk_size',
            default=100,
            type='int',
            help='Specify the number of items to be updated together.'),
        make_option('--num-docs',
            action='store',
            dest='num_docs',
            default=-1,
            type='int',
            help='Specify the maximum number of items to be indexed. By default will index the whole model.'),
        make_option('--start',
            action='store',
            dest='start_date',
            default=None,
            type='str',
            help='Specify the start date and time of documents to be indexed.'),
        make_option('--end',
            action='store',
            dest='end_date',
            default=None,
            type='str',
            help='Specify the end date and time of documents to be indexed.'),
        )

    def handle(self, *args, **options):
        del args
        logging.basicConfig(level='INFO')

        src = Bungiesearch()
        es = src.get_es_instance()

        if not options['action']:
            raise ValueError('No action specified. Must be one of "create", "update" or "delete".')

        if options['action'].startswith('delete'):
            if not options['confirmed']:
                raise ValueError('If you know what a delete operation does (on index or mapping), add the --guilty-as-charged flag.')
            if options['action'] == 'delete':
                if options['index']:
                    indices = [options['index']]
                else:
                    indices = src.get_indices()

                for index in indices:
                    logging.warning('Deleting elastic search index {}.'.format(index))
                    es.indices.delete(index=index, ignore=404)

            else:
                index_to_doctypes = defaultdict(list)
                if options['models']:
                    logging.info('Deleting mapping for models {} on index {}.'.format(options['models'], index))
                    for model_name in options['models'].split():
                        for index in src.get_index(model_name):
                            index_to_doctypes[index].append(model_name)
                elif options['index']:
                    index = options['index']
                    logging.info('Deleting mapping for all models on index {}.'.format(index))
                    index_to_doctypes[index] = src.get_models(index)
                else:
                    for index in src.get_indices():
                        index_to_doctypes[index] = src.get_models(index)
                    logging.info('Deleting mapping for all models ({}) on all indices ({}).'.format(index_to_doctypes.values(), index_to_doctypes.keys()))

                for index, doctype_list in iteritems(index_to_doctypes):
                    es.indices.delete_mapping(index, ','.join(doctype_list), params=None)

        elif options['action'] == 'create':
            if options['index']:
                indices = [options['index']]
            else:
                indices = src.get_indices()
            for index in indices:
                mapping = {}
                for mdl_idx in src.get_model_indices(index):
                    mapping[mdl_idx.get_model().__name__] = mdl_idx.get_mapping()

                analysis = mdl_idx.collect_analysis()

                logging.info('Creating index {} with {} doctypes.'.format(index, len(mapping)))
                es.indices.create(index=index, body={'mappings': mapping, 'settings': {'analysis': analysis}})

        elif options['action'] == 'update-mapping':
            if options['index']:
                indices = [options['index']]
            else:
                indices = src.get_indices()

            if options['models']:
                models = options['models'].split(',')
            else:
                models = []

            for index in indices:
                for model_name in src._idx_name_to_mdl_to_mdlidx[index]:
                    if models and model_name not in models:
                        continue
                    logging.info('Updating mapping of model/doctype {} on index {}.'.format(model_name, index))
                    try:
                        es.indices.put_mapping(model_name, src._idx_name_to_mdl_to_mdlidx[index][model_name].get_mapping(), index=index)
                    except Exception as e:
                        print(e)
                        if raw_input('Something terrible happened! Type "abort" to stop updating the mappings: ') == 'abort':
                            raise e
                        print('Continuing.')

        else:
            if options['models']:
                logging.info('Updating models {}.'.format(options['models']))
                model_names = options['models'].split(',')
            elif options['index']:
                index = options['index']
                logging.info('Updating all models on index {}.'.format(options['index']))
                model_names = src.get_models(index)
            else:
                model_names = [model for index in src.get_indices() for model in src.get_models(index)]
            # Update index.
            for model_name in model_names:
                if src.get_model_index(model_name).indexing_query is not None:
                    update_index(src.get_model_index(model_name).indexing_query, model_name, bulk_size=options['bulk_size'], num_docs=options['num_docs'], start_date=options['start_date'], end_date=options['end_date'])
                else:
                    update_index(src.get_model_index(model_name).get_model().objects.all(), model_name, bulk_size=options['bulk_size'], num_docs=options['num_docs'], start_date=options['start_date'], end_date=options['end_date'])
<commit_msg>Fixes #109.<commit_after>from _collections import defaultdict
import logging
from optparse import make_option

from django.core.management.base import BaseCommand
from six import iteritems

from ... import Bungiesearch
from ...utils import update_index

class Command(BaseCommand):
    args = ''
    help = 'Manage search index.'

    option_list = BaseCommand.option_list + (
        make_option('--create',
            action='store_const',
            dest='action',
            const='create',
            help='Create the index specified in the settings with the mapping generating from the search indices.'),
        make_option('--update',
            action='store_const',
            dest='action',
            const='update',
            help='Update the index specified in the settings with the mapping generating from the search indices.'),
        make_option('--update-mapping',
            action='store_const',
            dest='action',
            const='update-mapping',
            help='Update the mapping of specified models (or all models) on the index specified in the settings.'),
        make_option('--delete',
            action='store_const',
            dest='action',
            const='delete',
            help='Delete the index specified in the settings. Requires the "--guilty-as-charged" flag.'),
        make_option('--delete-mapping',
            action='store_const',
            dest='action',
            const='delete-mapping',
            help='Delete the mapping of specified models (or all models) on the index specified in the settings. Requires the "--guilty-as-charged" flag.'),
        make_option('--guilty-as-charged',
            action='store_true',
            dest='confirmed',
            default=False,
            help='Flag needed to delete an index.'),
        make_option('--models',
            action='store',
            dest='models',
            default=None,
            help='Models to be updated, separated by commas. If none are specified, then all models defined in the index will be updated.'),
        make_option('--index',
            action='store',
            dest='index',
            default=None,
            help='Specify the index for which to apply the action, as defined in BUNGIESEARCH.INDEXES of settings. Defaults to using all indices.'),
        make_option('--bulk-size',
            action='store',
            dest='bulk_size',
            default=100,
            type='int',
            help='Specify the number of items to be updated together.'),
        make_option('--num-docs',
            action='store',
            dest='num_docs',
            default=-1,
            type='int',
            help='Specify the maximum number of items to be indexed. By default will index the whole model.'),
        make_option('--start',
            action='store',
            dest='start_date',
            default=None,
            type='str',
            help='Specify the start date and time of documents to be indexed.'),
        make_option('--end',
            action='store',
            dest='end_date',
            default=None,
            type='str',
            help='Specify the end date and time of documents to be indexed.'),
        )

    def handle(self, *args, **options):
        del args
        logging.basicConfig(level='INFO')

        src = Bungiesearch()
        es = src.get_es_instance()

        if not options['action']:
            raise ValueError('No action specified. Must be one of "create", "update" or "delete".')

        if options['action'].startswith('delete'):
            if not options['confirmed']:
                raise ValueError('If you know what a delete operation does (on index or mapping), add the --guilty-as-charged flag.')
            if options['action'] == 'delete':
                if options['index']:
                    indices = [options['index']]
                else:
                    indices = src.get_indices()

                for index in indices:
                    logging.warning('Deleting elastic search index {}.'.format(index))
                    es.indices.delete(index=index, ignore=404)

            else:
                index_to_doctypes = defaultdict(list)
                if options['models']:
                    logging.info('Deleting mapping for models {} on index {}.'.format(options['models'], index))
                    for model_name in options['models'].split():
                        for index in src.get_index(model_name):
                            index_to_doctypes[index].append(model_name)
                elif options['index']:
                    index = options['index']
                    logging.info('Deleting mapping for all models on index {}.'.format(index))
                    index_to_doctypes[index] = src.get_models(index)
                else:
                    for index in src.get_indices():
                        index_to_doctypes[index] = src.get_models(index)
                    logging.info('Deleting mapping for all models ({}) on all indices ({}).'.format(index_to_doctypes.values(), index_to_doctypes.keys()))

                for index, doctype_list in iteritems(index_to_doctypes):
                    es.indices.delete_mapping(index, ','.join(doctype_list), params=None)

        elif options['action'] == 'create':
            if options['index']:
                indices = [options['index']]
            else:
                indices = src.get_indices()
            for index in indices:
                mapping = {}
                for mdl_idx in src.get_model_indices(index):
                    mapping[mdl_idx.get_model().__name__] = mdl_idx.get_mapping()

                analysis = mdl_idx.collect_analysis()

                logging.info('Creating index {} with {} doctypes.'.format(index, len(mapping)))
                es.indices.create(index=index, body={'mappings': mapping, 'settings': {'analysis': analysis}})

        elif options['action'] == 'update-mapping':
            if options['index']:
                indices = [options['index']]
            else:
                indices = src.get_indices()

            if options['models']:
                models = options['models'].split(',')
            else:
                models = []

            for index in indices:
                for model_name in src._idx_name_to_mdl_to_mdlidx[index]:
                    if models and model_name not in models:
                        continue
                    logging.info('Updating mapping of model/doctype {} on index {}.'.format(model_name, index))
                    try:
                        es.indices.put_mapping(model_name, src._idx_name_to_mdl_to_mdlidx[index][model_name].get_mapping(), index=index)
                    except Exception as e:
                        print(e)
                        if raw_input('Something terrible happened! Type "abort" to stop updating the mappings: ') == 'abort':
                            raise e
                        print('Continuing.')

        else:
            if options['index']:
                indices = options['index']
            else:
                indices = src.get_indices()
            if options['models']:
                model_names = options['models'].split(',')
            else:
                model_names = [model for index in indices for model in src.get_models(index)]

            logging.info('Updating models {} on indices {}.'.format(model_names, indices))

            # Update index.
            for model_name in model_names:
                if src.get_model_index(model_name).indexing_query is not None:
                    update_index(src.get_model_index(model_name).indexing_query, model_name, bulk_size=options['bulk_size'], num_docs=options['num_docs'], start_date=options['start_date'], end_date=options['end_date'])
                else:
                    update_index(src.get_model_index(model_name).get_model().objects.all(), model_name, bulk_size=options['bulk_size'], num_docs=options['num_docs'], start_date=options['start_date'], end_date=options['end_date'])
<|endoftext|>


<commit_before>from collections import namedtuple
import logging
from pathlib import Path

import pytest

from package_reviewer.runner import CheckRunner
from package_reviewer.check import file as file_c

def _collect_test_packages():
    this_dir = Path(__file__).with_name("packages")
    for package_path in this_dir.iterdir():
        if not package_path.is_dir():
            continue
        yield package_path

test_packages = list(_collect_test_packages())

@pytest.fixture(scope='function', params=test_packages)
def package_path(request):
    """Path to a package to be tested."""
    return request.param

@pytest.fixture(scope='function')
def check_runner():
    """Return an initialized CheckRunner with all file checkers."""
    checkers = file_c.get_checkers()
    return CheckRunner(checkers)

def config_logging():
    # Ensure we see debug output if tests fail
    logger = logging.getLogger("package_reviewer")
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.DEBUG)

config_logging()

# import after logging is configured

##############################################################################

CheckAssert = namedtuple("CheckAssert", "message details")

def _read_check_asserts(file_path):
    """Read CheckAsserts from file."""
    asserts = set()
    if file_path.is_file():
        with file_path.open('r') as f:
            message = None
            details = []
            line_iter = iter(f)

            line = next(line_iter)
            while line:
                assert line.startswith('- ')
                message = line[2:].strip()
                details = []

                for line in line_iter:
                    if not line.startswith(' '):
                        break
                    details.append(line.strip())
                else:
                    line = None

                asserts.add(CheckAssert(message, tuple(details)))

    return asserts

def test_reviewer_integration(package_path, check_runner):
    """Run checks over a package and check if specified failures or warnings were emitted.

    Test packages can specify the minimum required failures (or warnings)
    in files named "failures" and "warnings" respectively.
    If all failures or warnings should be compared,
    specify them in "all_failures" and "all_warnings".
    """

    # Run checks first and report them to stdout,
    # so we have something to inspect when the test fails.
    check_runner.run(package_path)
    check_runner.report()

    failure_asserts = _read_check_asserts(Path(package_path, "failures"))
    all_failure_asserts = _read_check_asserts(Path(package_path, "all_failures"))
    assert not (failure_asserts and all_failure_asserts), \
        "Only one failures meta file is allowed"

    warning_asserts = _read_check_asserts(Path(package_path, "warnings"))
    all_warning_asserts = _read_check_asserts(Path(package_path, "all_warnings"))
    assert not (warning_asserts and all_warning_asserts), \
        "Only one warnings meta file is allowed"

    assert_none = not (failure_asserts or all_failure_asserts
                       or warning_asserts or all_warning_asserts)

    failures = {CheckAssert(failure.message, failure.to_details())
                for failure in check_runner.failures}
    assert len(failures) == len(check_runner.failures), "TODO: Revisit tests"
    warnings = {CheckAssert(warning.message, warning.to_details())
                for warning in check_runner.warnings}
    assert len(warnings) == len(check_runner.warnings), "TODO: Revisit tests"

    assert assert_none or (failures or warnings), \
        "No asserts found for package but there were reports"

    if all_failure_asserts or assert_none:
        assert failures == all_failure_asserts
    elif failures:
        assert failures >= failure_asserts

    if all_warning_asserts or assert_none:
        assert warnings == all_warning_asserts
    elif warnings:
        assert warnings >= warning_asserts
<commit_msg>Set proper fixture id name for 'package_path'<commit_after>from collections import namedtuple
import logging
from pathlib import Path

import pytest

from package_reviewer.runner import CheckRunner
from package_reviewer.check import file as file_c

def _collect_test_packages():
    this_dir = Path(__file__).with_name("packages")
    for package_path in this_dir.iterdir():
        if not package_path.is_dir():
            continue
        yield package_path

test_packages = list(_collect_test_packages())

@pytest.fixture(scope='function', params=test_packages, ids=[p.name for p in test_packages])
def package_path(request):
    """Path to a package to be tested."""
    return request.param

@pytest.fixture(scope='function')
def check_runner():
    """Return an initialized CheckRunner with all file checkers."""
    checkers = file_c.get_checkers()
    return CheckRunner(checkers)

def config_logging():
    # Ensure we see debug output if tests fail
    logger = logging.getLogger("package_reviewer")
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.DEBUG)

config_logging()

# import after logging is configured

##############################################################################

CheckAssert = namedtuple("CheckAssert", "message details")

def _read_check_asserts(file_path):
    """Read CheckAsserts from file."""
    asserts = set()
    if file_path.is_file():
        with file_path.open('r') as f:
            message = None
            details = []
            line_iter = iter(f)

            line = next(line_iter)
            while line:
                assert line.startswith('- ')
                message = line[2:].strip()
                details = []

                for line in line_iter:
                    if not line.startswith(' '):
                        break
                    details.append(line.strip())
                else:
                    line = None

                asserts.add(CheckAssert(message, tuple(details)))

    return asserts

def test_reviewer_integration(package_path, check_runner):
    """Run checks over a package and check if specified failures or warnings were emitted.

    Test packages can specify the minimum required failures (or warnings)
    in files named "failures" and "warnings" respectively.
    If all failures or warnings should be compared,
    specify them in "all_failures" and "all_warnings".
    """

    # Run checks first and report them to stdout,
    # so we have something to inspect when the test fails.
    check_runner.run(package_path)
    check_runner.report()

    failure_asserts = _read_check_asserts(Path(package_path, "failures"))
    all_failure_asserts = _read_check_asserts(Path(package_path, "all_failures"))
    assert not (failure_asserts and all_failure_asserts), \
        "Only one failures meta file is allowed"

    warning_asserts = _read_check_asserts(Path(package_path, "warnings"))
    all_warning_asserts = _read_check_asserts(Path(package_path, "all_warnings"))
    assert not (warning_asserts and all_warning_asserts), \
        "Only one warnings meta file is allowed"

    assert_none = not (failure_asserts or all_failure_asserts
                       or warning_asserts or all_warning_asserts)

    failures = {CheckAssert(failure.message, failure.to_details())
                for failure in check_runner.failures}
    assert len(failures) == len(check_runner.failures), "TODO: Revisit tests"
    warnings = {CheckAssert(warning.message, warning.to_details())
                for warning in check_runner.warnings}
    assert len(warnings) == len(check_runner.warnings), "TODO: Revisit tests"

    assert assert_none or (failures or warnings), \
        "No asserts found for package but there were reports"

    if all_failure_asserts or assert_none:
        assert failures == all_failure_asserts
    elif failures:
        assert failures >= failure_asserts

    if all_warning_asserts or assert_none:
        assert warnings == all_warning_asserts
    elif warnings:
        assert warnings >= warning_asserts
<|endoftext|>


<commit_before>import numpy as nm
import matplotlib.pyplot as plt

from sfepy.discrete.fem.geometry_element import GeometryElement
from sfepy.mesh.mesh_generators import get_tensor_product_conn
import sfepy.postprocess.plot_dofs as pd
from sfepy.postprocess.plot_dofs import _get_axes

from sfepy.discrete.iga.iga import _get_knots_tuple

def plot_parametric_mesh(ax, knots):
    """
    Plot the parametric mesh of a NURBS given by its knots.
    """
    knots = _get_knots_tuple(knots)
    dim = len(knots)

    ax = _get_axes(ax, dim)

    uknots = [nm.unique(ii) for ii in knots]
    shape = [len(ii) for ii in uknots]

    ngrid = nm.mgrid[[slice(ii) for ii in shape]]
    coors = nm.r_[[uknots[ii][ig].ravel() for ii, ig in enumerate(ngrid)]].T

    conn, desc = get_tensor_product_conn(nm.array(shape))
    gel = GeometryElement(desc)

    ax = pd.plot_mesh(ax, coors, conn, gel.edges)
    pd.plot_points(ax, coors)

    return ax

def plot_control_mesh(ax, control_points, label=False):
    """
    Plot the control mesh of a NURBS given by its control points.
    """
    dim = control_points.shape[-1]
    ax = _get_axes(ax, dim)

    shape = control_points.shape

    conn, desc = get_tensor_product_conn(nm.array(shape[:-1]))
    gel = GeometryElement(desc)

    coors = control_points.reshape((-1, dim))

    ax = pd.plot_mesh(ax, coors, conn, gel.edges)
    pd.plot_points(ax, coors)

    if label:
        for ii, cc in enumerate(coors):
            if dim == 3:
                cx, cy, cz = cc
                ax.text(cx, cy, cz, '%d' % ii,
                        color='g', fontsize=12, weight='bold')

            else:
                cx, cy = cc
                ax.text(cx, cy, '%d' % ii,
                        color='g', fontsize=12, weight='bold')

    return ax

def _get_edges(n_ep, shape):
    dim = len(shape)
    aux = nm.arange(n_ep).reshape(shape)

    edges = []
    if dim == 3:
        for ii in xrange(shape[2] - 1):
            edges.append(aux[0, 0, ii:ii+2])
            edges.append(aux[-1, 0, ii:ii+2])
            edges.append(aux[0, -1, ii:ii+2])
            edges.append(aux[-1, -1, ii:ii+2])

        for ii in xrange(shape[1] - 1):
            edges.append(aux[0, ii:ii+2, 0])
            edges.append(aux[-1, ii:ii+2, 0])
            edges.append(aux[0, ii:ii+2, -1])
            edges.append(aux[-1, ii:ii+2, -1])

        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2, 0, 0])
            edges.append(aux[ii:ii+2, -1, 0])
            edges.append(aux[ii:ii+2, 0, -1])
            edges.append(aux[ii:ii+2, -1, -1])

    elif dim == 2:
        for ii in xrange(shape[1] - 1):
            edges.append(aux[0, ii:ii+2])
            edges.append(aux[-1, ii:ii+2])

        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2, 0])
            edges.append(aux[ii:ii+2, -1])

    else:
        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2])

    return nm.array(edges)

def plot_bezier_mesh(ax, control_points, conn, degrees, label=False):
    """
    Plot the Bezier mesh of a NURBS given by its control points and
    connectivity.
    """
    dim = control_points.shape[-1]
    ax = _get_axes(ax, dim)

    edges = _get_edges(conn.shape[1], nm.asarray(degrees) + 1)
    ax = pd.plot_mesh(ax, control_points, conn, edges)
    pd.plot_points(ax, control_points)

    if label:
        ax = pd.plot_global_dofs(ax, control_points, conn)

    return ax

def plot_iso_lines(ax, nurbs, color='b', n_points=100):
    """
    Plot the NURBS object using iso-lines in Greville abscissae coordinates.
    """
    dim = nurbs.dim
    ax = _get_axes(ax, dim)

    gas = nurbs.greville()

    if dim == 1:
        ga = gas[0]
        line = nm.linspace(ga[0], ga[-1], n_points)

        vals = nurbs(line)[:, 0]
        ax.plot(line, vals, color)

    elif dim == 2:
        ga0 = gas[0]
        ga1 = gas[1]

        x1 = nm.linspace(ga1[0], ga1[-1], n_points)
        for x0 in ga0:
            vals = nurbs(x0, x1)
            ax.plot(vals[:, 0], vals[:, 1], color)

        x0 = nm.linspace(ga0[0], ga0[-1], n_points)
        for x1 in ga0:
            vals = nurbs(x0, x1)
            ax.plot(vals[:, 0], vals[:, 1], color)

    else:
        ga0 = gas[0]
        ga1 = gas[1]
        ga2 = gas[2]

        x2 = nm.linspace(ga2[0], ga2[-1], n_points)
        for x0 in ga0:
            for x1 in ga1:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

        x1 = nm.linspace(ga1[0], ga1[-1], n_points)
        for x0 in ga0:
            for x2 in ga2:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

        x0 = nm.linspace(ga0[0], ga0[-1], n_points)
        for x1 in ga1:
            for x2 in ga2:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

    return ax

def plot_nurbs_basis_1d(ax, nurbs, n_points=100, x_axis='parametric',
                        legend=False):
    """
    Plot a 1D NURBS basis.
    """
    ax = _get_axes(ax, 2)

    ga = nurbs.greville()[0]

    n_fun = nurbs.weights.shape[0]
    line = nm.linspace(ga[0], ga[-1], n_points)
    for ii in xrange(n_fun):
        field = nm.zeros(n_fun)
        field[ii] = 1.0

        vals = nurbs.evaluate(fields=field, u=line)
        if x_axis == 'parametric':
            ax.plot(line, vals, label='%d' % ii)

        else:
            coors = nurbs(u=line)[:, x_axis]
            ax.plot(coors, vals, label='%d' % ii)

    if legend: ax.legend()

    return ax

def plot_bezier_nurbs_basis_1d(ax, control_points, weights, degrees, cs, conn,
                               n_points=20):
    """
    Plot a 1D NURBS basis using the Bezier extraction and local Bernstein
    basis.
    """
    from sfepy.discrete.iga.iga import eval_variable_in_qp
    ax = _get_axes(ax, 2)

    n_fun = weights.shape[0]
    line = nm.linspace(0, 1, n_points)[:, None]
    for ii in xrange(n_fun):
        variable = nm.zeros((n_fun, 1))
        variable[ii] = 1.0

        coors, vals, dets = eval_variable_in_qp(variable, line,
                                                control_points,
                                                weights, degrees,
                                                cs, conn)
        plt.plot(coors[:, 0], vals)

    return ax
<commit_msg>update plot_iso_lines() for curves in 2D<commit_after>import numpy as nm
import matplotlib.pyplot as plt

from sfepy.discrete.fem.geometry_element import GeometryElement
from sfepy.mesh.mesh_generators import get_tensor_product_conn
import sfepy.postprocess.plot_dofs as pd
from sfepy.postprocess.plot_dofs import _get_axes

from sfepy.discrete.iga.iga import _get_knots_tuple

def plot_parametric_mesh(ax, knots):
    """
    Plot the parametric mesh of a NURBS given by its knots.
    """
    knots = _get_knots_tuple(knots)
    dim = len(knots)

    ax = _get_axes(ax, dim)

    uknots = [nm.unique(ii) for ii in knots]
    shape = [len(ii) for ii in uknots]

    ngrid = nm.mgrid[[slice(ii) for ii in shape]]
    coors = nm.r_[[uknots[ii][ig].ravel() for ii, ig in enumerate(ngrid)]].T

    conn, desc = get_tensor_product_conn(nm.array(shape))
    gel = GeometryElement(desc)

    ax = pd.plot_mesh(ax, coors, conn, gel.edges)
    pd.plot_points(ax, coors)

    return ax

def plot_control_mesh(ax, control_points, label=False):
    """
    Plot the control mesh of a NURBS given by its control points.
    """
    dim = control_points.shape[-1]
    ax = _get_axes(ax, dim)

    shape = control_points.shape

    conn, desc = get_tensor_product_conn(nm.array(shape[:-1]))
    gel = GeometryElement(desc)

    coors = control_points.reshape((-1, dim))

    ax = pd.plot_mesh(ax, coors, conn, gel.edges)
    pd.plot_points(ax, coors)

    if label:
        for ii, cc in enumerate(coors):
            if dim == 3:
                cx, cy, cz = cc
                ax.text(cx, cy, cz, '%d' % ii,
                        color='g', fontsize=12, weight='bold')

            else:
                cx, cy = cc
                ax.text(cx, cy, '%d' % ii,
                        color='g', fontsize=12, weight='bold')

    return ax

def _get_edges(n_ep, shape):
    dim = len(shape)
    aux = nm.arange(n_ep).reshape(shape)

    edges = []
    if dim == 3:
        for ii in xrange(shape[2] - 1):
            edges.append(aux[0, 0, ii:ii+2])
            edges.append(aux[-1, 0, ii:ii+2])
            edges.append(aux[0, -1, ii:ii+2])
            edges.append(aux[-1, -1, ii:ii+2])

        for ii in xrange(shape[1] - 1):
            edges.append(aux[0, ii:ii+2, 0])
            edges.append(aux[-1, ii:ii+2, 0])
            edges.append(aux[0, ii:ii+2, -1])
            edges.append(aux[-1, ii:ii+2, -1])

        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2, 0, 0])
            edges.append(aux[ii:ii+2, -1, 0])
            edges.append(aux[ii:ii+2, 0, -1])
            edges.append(aux[ii:ii+2, -1, -1])

    elif dim == 2:
        for ii in xrange(shape[1] - 1):
            edges.append(aux[0, ii:ii+2])
            edges.append(aux[-1, ii:ii+2])

        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2, 0])
            edges.append(aux[ii:ii+2, -1])

    else:
        for ii in xrange(shape[0] - 1):
            edges.append(aux[ii:ii+2])

    return nm.array(edges)

def plot_bezier_mesh(ax, control_points, conn, degrees, label=False):
    """
    Plot the Bezier mesh of a NURBS given by its control points and
    connectivity.
    """
    dim = control_points.shape[-1]
    ax = _get_axes(ax, dim)

    edges = _get_edges(conn.shape[1], nm.asarray(degrees) + 1)
    ax = pd.plot_mesh(ax, control_points, conn, edges)
    pd.plot_points(ax, control_points)

    if label:
        ax = pd.plot_global_dofs(ax, control_points, conn)

    return ax

def plot_iso_lines(ax, nurbs, color='b', n_points=100):
    """
    Plot the NURBS object using iso-lines in Greville abscissae coordinates.
    """
    dim = nurbs.dim
    ax = _get_axes(ax, dim)

    gas = nurbs.greville()

    if dim == 1:
        ga = gas[0]

        x0 = nm.linspace(ga[0], ga[-1], n_points)
        vals = nurbs(x0)

        if vals.shape[1] == 1:
            ax.plot(x0, vals[:, 0], color)

        else: # Assume curve in 2D.
            ax.plot(vals[:, 0], vals[:, 1], color)

    elif dim == 2:
        ga0 = gas[0]
        ga1 = gas[1]

        x1 = nm.linspace(ga1[0], ga1[-1], n_points)
        for x0 in ga0:
            vals = nurbs(x0, x1)
            ax.plot(vals[:, 0], vals[:, 1], color)

        x0 = nm.linspace(ga0[0], ga0[-1], n_points)
        for x1 in ga0:
            vals = nurbs(x0, x1)
            ax.plot(vals[:, 0], vals[:, 1], color)

    else:
        ga0 = gas[0]
        ga1 = gas[1]
        ga2 = gas[2]

        x2 = nm.linspace(ga2[0], ga2[-1], n_points)
        for x0 in ga0:
            for x1 in ga1:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

        x1 = nm.linspace(ga1[0], ga1[-1], n_points)
        for x0 in ga0:
            for x2 in ga2:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

        x0 = nm.linspace(ga0[0], ga0[-1], n_points)
        for x1 in ga1:
            for x2 in ga2:
                vals = nurbs(x0, x1, x2)
                ax.plot(vals[:, 0], vals[:, 1], vals[:, 2], color)

    return ax

def plot_nurbs_basis_1d(ax, nurbs, n_points=100, x_axis='parametric',
                        legend=False):
    """
    Plot a 1D NURBS basis.
    """
    ax = _get_axes(ax, 2)

    ga = nurbs.greville()[0]

    n_fun = nurbs.weights.shape[0]
    line = nm.linspace(ga[0], ga[-1], n_points)
    for ii in xrange(n_fun):
        field = nm.zeros(n_fun)
        field[ii] = 1.0

        vals = nurbs.evaluate(fields=field, u=line)
        if x_axis == 'parametric':
            ax.plot(line, vals, label='%d' % ii)

        else:
            coors = nurbs(u=line)[:, x_axis]
            ax.plot(coors, vals, label='%d' % ii)

    if legend: ax.legend()

    return ax

def plot_bezier_nurbs_basis_1d(ax, control_points, weights, degrees, cs, conn,
                               n_points=20):
    """
    Plot a 1D NURBS basis using the Bezier extraction and local Bernstein
    basis.
    """
    from sfepy.discrete.iga.iga import eval_variable_in_qp
    ax = _get_axes(ax, 2)

    n_fun = weights.shape[0]
    line = nm.linspace(0, 1, n_points)[:, None]
    for ii in xrange(n_fun):
        variable = nm.zeros((n_fun, 1))
        variable[ii] = 1.0

        coors, vals, dets = eval_variable_in_qp(variable, line,
                                                control_points,
                                                weights, degrees,
                                                cs, conn)
        plt.plot(coors[:, 0], vals)

    return ax
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2016 KMEE (http://www.kmee.com.br)
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import logging
from openerp import api, fields, models

_logger = logging.getLogger(__name__)

try:
    from pybrasil.valor.decimal import Decimal as D
    from pybrasil.valor import formata_valor

except ImportError:
    _logger.info('Cannot import pybrasil')

class HrPayslipeLine(models.Model):
    _inherit = "hr.payslip.line"

    @api.model
    def _valores(self):
        for linha in self:
            linha.quantity_fmt = valor.formata_valor(linha.quantity)
            if linha.salary_rule_id.category_id.code == "PROVENTO":
                linha.valor_provento = linha.total
                linha.valor_provento_fmt = \
                    valor.formata_valor(linha.valor_provento)
            else:
                linha.valor_provento = 0.00
                linha.valor_provento_fmt = ''

    @api.model
    def '_compute_valores'(self):
        for linha in self:
            if linha.salary_rule_id.category_id.code in ["DEDUCAO"] \
                    or linha.salary_rule_id.code == "INSS" \
                    or linha.salary_rule_id.code == "IRPF":
                linha.valor_deducao = linha.total
                linha.valor_deducao_fmt = \
                    valor.formata_valor(linha.valor_deducao)
            else:
                linha.valor_deducao = 0.00
                linha.valor_deducao_fmt = ''

    quantity_fmt = fields.Char(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    valor_provento = fields.Float(
        string=u'Provento',
        compute='_compute_valores',
    )
    valor_provento_fmt = fields.Char(
        string=u'Provento',
        compute='_compute_valores',
    )
    valor_deducao = fields.Float(
        string=u'Dedução',
        compute='_compute_valores',
    )
    valor_deducao_fmt = fields.Char(
        string=u'Dedução',
        compute='_compute_valores',
    )
    quantidade = fields.Float(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    quantidade_fmt = fields.Char(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    base = fields.Float(
        string=u'Base',
        compute='_compute_valores',
    )
    base_fmt = fields.Char(
        string=u'Base',
        compute='_compute_valores',
    )
    percentual = fields.Float(
        string=u'Percentual',
        compute='_compute_valores',
    )
    percentual_fmt = fields.Char(
        string=u'Percentual',
        compute='_compute_valores',
    )
    valor = fields.Float(
        string=u'Valor',
        compute='_compute_valores',
    )
    valor_fmt = fields.Char(
        string=u'Valor',
        compute='_compute_valores',
    )

    @api.model
    def _compute_valore(self):
        for linha in self:
            linha.quantidade = D(linha.quantity or 0)
            linha.base = D(linha.amount or 0)
            linha.percentual = D(linha.rate or 0)
            linha.valor = D(linha.total or 0)

            linha.quantidade_fmt = formata_valor(linha.quantidade)
            linha.base_fmt = formata_valor(linha.base)
            linha.percentual_fmt = formata_valor(linha.percentual)
            linha.valor_fmt = formata_valor(linha.valor)

            linha.quantity_fmt = linha.quantidade_fmt
            linha.quantity_fmt = linha.quantidade_fmt

            linha.valor_provento = D(0)
            linha.valor_provento_fmt = ''
            linha.valor_deducao = D(0)
            linha.valor_deducao_fmt = ''

            if linha.salary_rule_id.category_id.code == "PROVENTO":
                linha.valor_provento = linha.valor
                linha.valor_provento_fmt = linha.valor_fmt

            elif linha.salary_rule_id.category_id.code in ["DEDUCAO"] \
                    or linha.salary_rule_id.code == "INSS" \
                    or linha.salary_rule_id.code == "IRPF":
                linha.valor_deducao = linha.valor
                linha.valor_deducao_fmt = linha.valor_fmt
<commit_msg>[FIX] Typos<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2016 KMEE (http://www.kmee.com.br)
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

import logging
from openerp import api, fields, models

_logger = logging.getLogger(__name__)

try:
    from pybrasil.valor.decimal import Decimal as D
    from pybrasil.valor import formata_valor

except ImportError:
    _logger.info('Cannot import pybrasil')

class HrPayslipeLine(models.Model):
    _inherit = "hr.payslip.line"

    quantity_fmt = fields.Char(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    valor_provento = fields.Float(
        string=u'Provento',
        compute='_compute_valores',
    )
    valor_provento_fmt = fields.Char(
        string=u'Provento',
        compute='_compute_valores',
    )
    valor_deducao = fields.Float(
        string=u'Dedução',
        compute='_compute_valores',
    )
    valor_deducao_fmt = fields.Char(
        string=u'Dedução',
        compute='_compute_valores',
    )
    quantidade = fields.Float(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    quantidade_fmt = fields.Char(
        string=u'Quantidade',
        compute='_compute_valores',
    )
    base = fields.Float(
        string=u'Base',
        compute='_compute_valores',
    )
    base_fmt = fields.Char(
        string=u'Base',
        compute='_compute_valores',
    )
    percentual = fields.Float(
        string=u'Percentual',
        compute='_compute_valores',
    )
    percentual_fmt = fields.Char(
        string=u'Percentual',
        compute='_compute_valores',
    )
    valor = fields.Float(
        string=u'Valor',
        compute='_compute_valores',
    )
    valor_fmt = fields.Char(
        string=u'Valor',
        compute='_compute_valores',
    )

    @api.model
    def _compute_valores(self):
        for linha in self:
            linha.quantidade = D(linha.quantity or 0)
            linha.base = D(linha.amount or 0)
            linha.percentual = D(linha.rate or 0)
            linha.valor = D(linha.total or 0)

            linha.quantidade_fmt = formata_valor(linha.quantidade)
            linha.base_fmt = formata_valor(linha.base)
            linha.percentual_fmt = formata_valor(linha.percentual)
            linha.valor_fmt = formata_valor(linha.valor)

            linha.quantity_fmt = linha.quantidade_fmt
            linha.quantity_fmt = linha.quantidade_fmt

            linha.valor_provento = D(0)
            linha.valor_provento_fmt = ''
            linha.valor_deducao = D(0)
            linha.valor_deducao_fmt = ''

            if linha.salary_rule_id.category_id.code == "PROVENTO":
                linha.valor_provento = linha.valor
                linha.valor_provento_fmt = linha.valor_fmt

            elif linha.salary_rule_id.category_id.code in ["DEDUCAO"] \
                    or linha.salary_rule_id.code == "INSS" \
                    or linha.salary_rule_id.code == "IRPF":
                linha.valor_deducao = linha.valor
                linha.valor_deducao_fmt = linha.valor_fmt
<|endoftext|>


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

"""
Canon C-Log Colourspace
=======================

Defines the *C-Log* colourspace:

-   :attr:`C_LOG_COLOURSPACE`.

See Also
--------
`RGB Colourspaces IPython Notebook
<http://nbviewer.ipython.org/github/colour-science/colour-ipython/blob/master/notebooks/models/rgb.ipynb>`_  # noqa

References
----------
.. [1]  `Canon-Log_Transfer_Characteristic
        <http://downloads.canon.com/CDLC/Canon-Log_Transfer_Characteristic_6-20-2012.pdf>`_  # noqa
        (Last accessed 18 April 2014)
"""

from __future__ import division, unicode_literals

import math
import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models import RGB_Colourspace, normalised_primary_matrix

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2014 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['C_LOG_PRIMARIES',
           'C_LOG_WHITEPOINT',
           'C_LOG_TO_XYZ_MATRIX',
           'XYZ_TO_C_LOG_MATRIX',
           'C_LOG_TRANSFER_FUNCTION',
           'C_LOG_INVERSE_TRANSFER_FUNCTION',
           'C_LOG_COLOURSPACE']

C_LOG_PRIMARIES = np.array(
    [[0.6400, 0.3300],
     [0.3000, 0.6000],
     [0.1500, 0.0600]])
"""
*C-Log* colourspace primaries,

C_LOG_PRIMARIES : ndarray, (3, 2)
"""

C_LOG_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get('D65')
"""
*C-Log* colourspace whitepoint.

C_LOG_WHITEPOINT : tuple
"""

C_LOG_TO_XYZ_MATRIX = normalised_primary_matrix(C_LOG_PRIMARIES,
                                                C_LOG_WHITEPOINT)
"""
*C-Log* colourspace to *CIE XYZ* colourspace matrix.

C_LOG_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_C_LOG_MATRIX = np.linalg.inv(C_LOG_TO_XYZ_MATRIX)
"""
*CIE XYZ* colourspace to *C-Log* colourspace matrix.

XYZ_TO_C_LOG_MATRIX : array_like, (3, 3)
"""

C_LOG_TRANSFER_FUNCTION = lambda x: (
    0.529136 * math.log10(10.1596 * x + 1) + 0.0730597)
"""
Transfer function from linear to *C-Log* colourspace.

C_LOG_TRANSFER_FUNCTION : object
"""

C_LOG_INVERSE_TRANSFER_FUNCTION = lambda x: (
    -0.0716226 * (1.37427 - math.exp(1) ** (4.35159 * x)))
"""
Inverse transfer function from *C-Log* colourspace to linear.

C_LOG_INVERSE_TRANSFER_FUNCTION : object
"""

C_LOG_COLOURSPACE = RGB_Colourspace(
    'C-Log',
    C_LOG_PRIMARIES,
    C_LOG_WHITEPOINT,
    C_LOG_TO_XYZ_MATRIX,
    XYZ_TO_C_LOG_MATRIX,
    C_LOG_TRANSFER_FUNCTION,
    C_LOG_INVERSE_TRANSFER_FUNCTION)
"""
*C-Log* colourspace.

C_LOG_COLOURSPACE : RGB_Colourspace
"""
<commit_msg>Improve "C-Log *" colourspace models inverse OECF precision.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Canon C-Log Colourspace
=======================

Defines the *C-Log* colourspace:

-   :attr:`C_LOG_COLOURSPACE`.

See Also
--------
`RGB Colourspaces IPython Notebook
<http://nbviewer.ipython.org/github/colour-science/colour-ipython/blob/master/notebooks/models/rgb.ipynb>`_  # noqa

References
----------
.. [1]  `Canon-Log_Transfer_Characteristic
        <http://downloads.canon.com/CDLC/Canon-Log_Transfer_Characteristic_6-20-2012.pdf>`_  # noqa
        (Last accessed 18 April 2014)
"""

from __future__ import division, unicode_literals

import math
import numpy as np

from colour.colorimetry import ILLUMINANTS
from colour.models import RGB_Colourspace, normalised_primary_matrix

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2014 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = ['C_LOG_PRIMARIES',
           'C_LOG_WHITEPOINT',
           'C_LOG_TO_XYZ_MATRIX',
           'XYZ_TO_C_LOG_MATRIX',
           'C_LOG_TRANSFER_FUNCTION',
           'C_LOG_INVERSE_TRANSFER_FUNCTION',
           'C_LOG_COLOURSPACE']

C_LOG_PRIMARIES = np.array(
    [[0.6400, 0.3300],
     [0.3000, 0.6000],
     [0.1500, 0.0600]])
"""
*C-Log* colourspace primaries,

C_LOG_PRIMARIES : ndarray, (3, 2)
"""

C_LOG_WHITEPOINT = ILLUMINANTS.get(
    'CIE 1931 2 Degree Standard Observer').get('D65')
"""
*C-Log* colourspace whitepoint.

C_LOG_WHITEPOINT : tuple
"""

C_LOG_TO_XYZ_MATRIX = normalised_primary_matrix(C_LOG_PRIMARIES,
                                                C_LOG_WHITEPOINT)
"""
*C-Log* colourspace to *CIE XYZ* colourspace matrix.

C_LOG_TO_XYZ_MATRIX : array_like, (3, 3)
"""

XYZ_TO_C_LOG_MATRIX = np.linalg.inv(C_LOG_TO_XYZ_MATRIX)
"""
*CIE XYZ* colourspace to *C-Log* colourspace matrix.

XYZ_TO_C_LOG_MATRIX : array_like, (3, 3)
"""

C_LOG_TRANSFER_FUNCTION = lambda x: (
    0.529136 * math.log10(10.1596 * x + 1) + 0.0730597)
"""
Transfer function from linear to *C-Log* colourspace.

C_LOG_TRANSFER_FUNCTION : object
"""

C_LOG_INVERSE_TRANSFER_FUNCTION = lambda x: (
    -0.071622555735168 * (
    1.3742747797867 - math.exp(1) ** (4.3515940948906 * x)))
"""
Inverse transfer function from *C-Log* colourspace to linear.

C_LOG_INVERSE_TRANSFER_FUNCTION : object
"""

C_LOG_COLOURSPACE = RGB_Colourspace(
    'C-Log',
    C_LOG_PRIMARIES,
    C_LOG_WHITEPOINT,
    C_LOG_TO_XYZ_MATRIX,
    XYZ_TO_C_LOG_MATRIX,
    C_LOG_TRANSFER_FUNCTION,
    C_LOG_INVERSE_TRANSFER_FUNCTION)
"""
*C-Log* colourspace.

C_LOG_COLOURSPACE : RGB_Colourspace
"""
<|endoftext|>