<commit_before>from django.db import models
from django.db.models import BLANK_CHOICE_DASH
from django.core.exceptions import ValidationError
from django.apps import apps
from django.utils.module_loading import import_string
from contextlib import suppress

__all__ = ['EnumField']

class DBType(str):
    """ Create a db_type string that can represent paramatised values """
    params = []
    db_type = None

    def __new__(cls, value, params=None, *args, **kwargs):
        print("Making DBType {} with params {}".format(value, params))
        if not params:
            return super().__new__(cls, value)
        self = super().__new__(cls, cls.render(value, params))
        self.db_type = value
        self.params = params
        return self

    @property
    def paramatized(self):
        return self.db_type or self, params

    @staticmethod
    def render(db_type, params):
        return db_type % tuple(params)

class EnumField(models.Field):
    description = 'Enumeration field using python PEP435 and database implementations'
    case_sensitive = None
    enum_type = None

    def __init__(self, enum, enum_type=None, case_sensitive=None, **kwargs):
        # TODO Add case insensitive enum
        if case_sensitive is not None:
            self.case_sensitive = case_sensitive

        if isinstance(enum, str):
            # Used for migrations only. Cannot specify enum by lazy class name
            self.enum = import_string(enum)
        else:
            self.enum = enum

        app_config = apps.get_containing_app_config(self.enum.__module__)
        if app_config is None:
            raise RuntimeError(
                "Enum class doesn't declare an explicit app_label, and isn't"
                " in an application in INSTALLED_APPS")
        self.enum_app = app_config.label

        if enum_type:
            self.enum_type = enum_type
        else:
            if 'Meta' in self.enum and 'db_type' in self.enum.Meta:
                self.enum_type = self.enum.Meta.db_type
            else:
                self.enum_type = '{al}_enum_{en}'.format(
                    al=self.enum_app,
                    en=self.enum.__qualname__.lower())
        super().__init__(**kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        kwargs['enum'] = '{}.{}'.format(self.enum.__module__, self.enum.__name__)
        kwargs['enum_type'] = self.enum_type
        if self.case_sensitive is not None:
            kwargs['case_sensitive'] = self.case_sensitive
        return name, path, args, kwargs

    def from_db_value(self, value, expression, connection, context):
        if value is None:
            return None
        with suppress(KeyError):
            return self.enum[value]
        raise ValueError('Invalid enumeration value returned from database')

    def to_python(self, value):
        if value is None or isinstance(value, self.enum):
            return value
        with suppress(ValueError):
            # Key miss suppressed with .get()
            return self.enum.__members__.get(value) or self.enum(value)
        raise ValidationError('Invalid enumeration value')

    def get_prep_value(self, value):
        if not value:
            return value
        if not isinstance(value, self.enum):
            value = self.to_python(value)
        return value.value

    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to=None):
        return [e.value for e in self.enum]

    # db_type method not needed if parameters set
    def db_type_parameters(self, connection):
        paras = super().db_type_parameters(connection)
        if connection.features.has_enum:
            paras['enum_type'] = self.enum_type
            paras['values'] = ', '.join('%s' * len(self.get_choices()))
        return paras

    def db_type(self, connection):
        type_string = super().db_type(connection)
        choices = None
        if connection.features.has_enum and not connection.features.requires_enum_declaration:
            choices = self.get_choices()

        return DBType(
            type_string,
            choices)
<commit_msg>Corrected clone vs deconstruct behaviour<commit_after>from django.db import models
from django.db.models import BLANK_CHOICE_DASH
from django.core.exceptions import ValidationError
from django.apps import apps
from django.utils.module_loading import import_string
from contextlib import suppress

__all__ = ['EnumField']

class DBType(str):
    """ Create a db_type string that can represent paramatised values """
    params = []
    db_type = None

    def __new__(cls, value, params=None, *args, **kwargs):
        print("Making DBType {} with params {}".format(value, params))
        if not params:
            return super().__new__(cls, value)
        self = super().__new__(cls, cls.render(value, params))
        self.db_type = value
        self.params = params
        return self

    @property
    def paramatized(self):
        return self.db_type or self, params

    @staticmethod
    def render(db_type, params):
        return db_type % tuple(params)

class EnumField(models.Field):
    description = 'Enumeration field using python PEP435 and database implementations'
    case_sensitive = None
    enum_type = None
    enum = None
    enum_app = None

    def __init__(self, enum=None, enum_type=None, case_sensitive=None, **kwargs):
        # TODO Add case insensitive enum
        if case_sensitive is not None:
            self.case_sensitive = case_sensitive

        if isinstance(enum, str):
            with suppress(ImportError):
                self.enum = import_string(enum)
        else:
            self.enum = enum

        if self.enum:
            # Determine app_label of enum being used
            app_config = apps.get_containing_app_config(self.enum.__module__)
            if app_config is None:
                raise RuntimeError(
                    "Enum class doesn't declare an explicit app_label, and isn't"
                    " in an application in INSTALLED_APPS")
            self.enum_app = app_config.label

        # Respect the db_type declared on the enum, else generate
        if enum_type:
            self.enum_type = enum_type
        else:
            if 'Meta' in self.enum and 'db_type' in self.enum.Meta:
                self.enum_type = self.enum.Meta.db_type
            else:
                self.enum_type = '{al}_enum_{en}'.format(
                    al=self.enum_app,
                    en=self.enum.__qualname__.lower())
        super().__init__(**kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        kwargs['enum_type'] = self.enum_type
        if self.case_sensitive is not None:
            kwargs['case_sensitive'] = self.case_sensitive
        return name, path, args, kwargs

    def clone(self):
        # Override deconstruct behaviour to include live enum
        obj = super().clone()
        if self.enum:
            obj.enum = self.enum
        return obj

    def from_db_value(self, value, expression, connection, context):
        if value is None:
            return None
        with suppress(KeyError):
            return self.enum[value]
        raise ValueError('Invalid enumeration value returned from database')

    def to_python(self, value):
        if value is None or isinstance(value, self.enum):
            return value
        with suppress(ValueError):
            # Key miss suppressed with .get()
            return self.enum.__members__.get(value) or self.enum(value)
        raise ValidationError('Invalid enumeration value')

    def get_prep_value(self, value):
        if not value:
            return value
        if not isinstance(value, self.enum):
            value = self.to_python(value)
        return value.value

    def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to=None):
        return [e.value for e in self.enum]

    # db_type method not needed if parameters set
    def db_type_parameters(self, connection):
        paras = super().db_type_parameters(connection)
        if connection.features.has_enum:
            paras['enum_type'] = self.enum_type
            paras['values'] = ', '.join('%s' * len(self.get_choices()))
        return paras

    def db_type(self, connection):
        type_string = super().db_type(connection)
        choices = None
        if connection.features.has_enum and not connection.features.requires_enum_declaration:
            choices = self.get_choices()

        return DBType(
            type_string,
            choices)
<|endoftext|>


<commit_before>methods = 'AdmAddNode AdmAddPerson AdmAddPersonToSite AdmAddSite AdmAuthCheck AdmDeleteNode AdmDeletePerson AdmDeleteSite AdmGetAllRoles AdmGetNodes AdmGetPersonRoles AdmGetPersonSites AdmGetPersons AdmGetSites AdmGrantRoleToPerson AdmIsPersonInRole AdmRemovePersonFromSite AdmRevokeRoleFromPerson AdmSetPersonEnabled AdmSetPersonPrimarySite AdmUpdateNode AdmUpdatePerson AuthenticatePrincipal  system.listMethods  system.methodHelp  system.methodSignature  system.multicall'.split()
<commit_msg>remove AuthenticatePrincipal<commit_after>methods = 'AdmAddNode AdmAddPerson AdmAddPersonToSite AdmAddSite AdmAuthCheck AdmDeleteNode AdmDeletePerson AdmDeleteSite AdmGetAllRoles AdmGetNodes AdmGetPersonRoles AdmGetPersonSites AdmGetPersons AdmGetSites AdmGrantRoleToPerson AdmIsPersonInRole AdmRemovePersonFromSite AdmRevokeRoleFromPerson AdmSetPersonEnabled AdmSetPersonPrimarySite AdmUpdateNode AdmUpdatePerson  system.listMethods  system.methodHelp  system.methodSignature  system.multicall'.split()
<|endoftext|>


<commit_before><commit_msg>Fix missing import<commit_after><|endoftext|>


<commit_before>#!/usr/bin/python
#
# CLI compiler for bcmd's new model description language
#

import sys
import argparse
import bcmd_yacc
import os
import decimal
import string
import pprint
import logger
import ast
import codegen
import info

# default compiler configuration
# (this is effectively a template whose details
# may be adapted by command line args)
CONFIG = {'modelpath': ['.', 'models'],
          'outdir': '.',
          'outfile': None,
          'treefile': None,
          'name': None,
          'unused': True,
          'graph': None,
          'graph-exclude-unused': False,
          'graph-exclude-init': False,
          'graph-exclude-self': True,
          'graph-exclude-clusters': False,
          'graph-exclude-params': False,
          'independent': 't',
          'input-makes-intermed': True}

# these are effectively constants
VERSION = 0.6
MODELDEF_EXT = '.modeldef'
CODE_EXT = '.c'
MODEL_EXT = '.model'
TREE_EXT = '.tree'
COMPILE_EXT = '.bcmpl'
GRAPHVIZ_EXT = '.gv'

DUMMY_SOURCE = '##\n'

# parse a chosen model definition file and return the AST

def parse_file(filename):
    try:
        f = open(filename)
        data = f.read()
        f.close()
    except IOError as e:
        logger.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        return None

    logger.message("Processing file: " + filename)

    bcmd_yacc.currentFile = filename
    errsBefore = len(bcmd_yacc.compilationInfo['errors'])

    lp = bcmd_yacc.get_lexer_parser()
    result = lp[1].parse(data, lexer=lp[0])

    fileErrs = len(bcmd_yacc.compilationInfo['errors']) - errsBefore
    bcmd_yacc.currentFile = None

    if fileErrs == 1:
        logger.error('Compilation failed with 1 syntax error')
    elif fileErrs > 1:
        logger.error('Compilation failed with %d syntax errors' % fileErrs)

    return fileErrs, result

def print_errors():
    logger.error('*** Summary of model compilation errors ***')
    errs = bcmd_yacc.compilationInfo
    for ii in range(len(errs['errors'])):
        logger.error(errs['messages'][ii]
                     + ' (' + errs['files'][ii]
                     + ', line ' + str(errs['lines'][ii]) + ')')

# find a file on the search path

def search_file(filename, search_path):
    for path in search_path:
        candidate = os.path.join(path, filename)
        if os.path.isfile(candidate):
            return os.path.abspath(candidate)
    return None

# process arguments

def process_args():
    config = CONFIG

    ap = argparse.ArgumentParser(
        description="Model compiler for the BCMD modelling system.")
    ap.add_argument('--version', action='version',
                    version='bcmd version %.1fa' % VERSION)
    ap.add_argument(
        '-i', help='append to default model search path', metavar='PATH')
    ap.add_argument(
        '-I', help='replace default model search path', metavar='PATH')
    ap.add_argument(
        '-n', '--name', help='specify model name (default: <file1>)', metavar='NAME')
    ap.add_argument(
        '-o', help='specify output file name (default: <modelname>.model)', metavar='FILE')
    ap.add_argument(
        '-d', help='specify output directory (default: .)', metavar='DIR')
    ap.add_argument(
        '-u', '--unused', help='omit apparently unused intermediates', action='store_false')
    ap.add_argument(
        '-g', '--debug', help='include debug outputs in generated model code', action='store_true')
    ap.add_argument('-t', '--tree', help='write parse tree to file (default: <modelname>.tree)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-p', '--processed', help='write compilation data to file (default: <modelname>.bcmpl)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-G', '--graph', help='write dependency structure in GraphViz format (default: <modelname>.gv)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-U', '--graphxunused',
                    help='exclude apparently unused elements from graph output', action='store_true')
    ap.add_argument('-N', '--graphxinit',
                    help='exclude initialisation dependencies from graph output', action='store_true')
    ap.add_argument('-C', '--graphxclust',
                    help='exclude clustering from graph output', action='store_true')
    ap.add_argument('-S', '--graphself',
                    help='include direct circular dependencies in graph output', action='store_false')
    ap.add_argument('-v', '--verbose',
                    help='set level of detail logged to stderr (0-7, default: 3)', metavar='LEVEL', type=int)
    ap.add_argument(
        '-Y', '--yacc', help='run a dummy parse to rebuild parse tables', action='store_true')
    # ... add further options here as needed ...

    ap.add_argument('file', nargs='+',
                    help='one or more model description files to be compiled')

    args = ap.parse_args()

    if args.yacc:
        lp = bcmd_yacc.get_lexer_parser()
        result = lp[1].parse(DUMMY_SOURCE, lexer=lp[0])
        return False

    if not (args.I is None):
        config['modelpath'] = args.I.split(os.pathsep)
    elif not (args.i is None):
        config['modelpath'] = config['modelpath'] + \
            args.i.split(os.pathsep)

    if not (args.name is None):
        config['name'] = args.name
    else:
        srcname, srcext = os.path.splitext(args.file[0])
        config['name'] = srcname

    if not (args.o is None):
        config['outfile'] = args.o
    else:
        config['outfile'] = config['name'] + MODEL_EXT

    if args.d is not None:
        if not os.path.isdir(args.d):
            os.makedirs(args.d)
        config['outdir'] = args.d

    config['treefile'] = args.tree
    config['compfile'] = args.processed
    config['sources'] = args.file
    config['unused'] = args.unused
    config['debug'] = args.debug
    config['graph'] = args.graph
    config['graph-exclude-unused'] = args.graphxunused
    config['graph-exclude-init'] = args.graphxinit
    config['graph-exclude-self'] = args.graphself
    config['graph-exclude-clusters'] = args.graphxclust

    if args.verbose is not None:
        logger.verbosity = args.verbose
    return config

# load and parse source files named on the command line, plus imports
# note failures and return a structure including those details and
# the resulting merged item list

def load_sources(config):
    sources = config['sources']
    srcIndex = 0
    parsedSources = []
    failedSources = []
    merged = []

    while srcIndex < len(sources):
        logger.message("Searching for source file: " + sources[srcIndex])
        src = search_file(sources[srcIndex], config['modelpath'])
        if (src is None) and (not sources[srcIndex].endswith(MODELDEF_EXT)):
            logger.message(
                "Not found, trying with added extension: " + sources[srcIndex] + MODELDEF_EXT)
            src = search_file(sources[srcIndex] +
                              MODELDEF_EXT, config['modelpath'])

        if src is None:
            logger.warn("File not found: " + sources[srcIndex])
            failedSources.append(sources[srcIndex])
        else:
            nErrs, ast = parse_file(src)
            if nErrs > 0 or ast is None:
                failedSources.append(src)
            else:
                ast = list(ast)

                # add imports that are not already in the source list to it
                for imp in list(sum([x[1:] for x in ast if x[0] == 'import'], ())):
                    if imp not in sources and imp + MODELDEF_EXT not in sources:
                        sources.append(imp)

                logger.detail(ast, prettify=True)
                parsedSources.append((sources[srcIndex], src))

                merged = merged + ast

        srcIndex = srcIndex + 1

    logger.message("Total number of attempted source files: %d" % srcIndex)
    logger.message("%d parsed, %d failed" %
                   (len(parsedSources), len(failedSources)))
    for failed in failedSources:
        logger.message("  ->  %s" % failed)

    return {'sources': sources, 'parsed': parsedSources, 'failed': failedSources, 'merged': merged}

# write the loaded merged item list to a file, if so specified
def write_tree(config, work):
    if not config['treefile'] is None:
        if config['treefile'] == '':
            config['treefile'] = config['name'] + TREE_EXT
        treePath = os.path.join(config['outdir'], config['treefile'])
        logger.message("Attempting to write parse tree to " + treePath)
        try:
            treeStream = open(treePath, 'w')
            pprint.pprint(work['merged'], stream=treeStream)
            treeStream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

# write the processed model structure to a file, if so specified

def write_comp(config, processed):
    if not config['compfile'] is None:
        if config['compfile'] == '':
            config['compfile'] = config['name'] + COMPILE_EXT
        compPath = os.path.join(config['outdir'], config['compfile'])
        logger.message(
            "Attempting to write compilation structure to " + compPath)
        try:
            compStream = open(compPath, 'w')
            pprint.pprint(processed, stream=compStream)
            compStream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

# write the model dependencies to a graph, if so specified

def write_graph(config, model):
    if not config['graph'] is None:
        if config['graph'] == '':
            config['graph'] = config['name'] + GRAPHVIZ_EXT
        graphPath = os.path.join(config['outdir'], config['graph'])
        logger.message("Attempting to write dependency graph to " + graphPath)
        try:
            stream = open(graphPath, 'w')
            print(info.generateGraphViz(model, config), file=stream)
            stream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

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

# main entry point of this compiler script
if __name__ == '__main__':
    config = process_args()
    if not config:
        sys.exit(2)

    work = load_sources(config)
    if len(work['failed']) > 0:
        print_errors()
        sys.exit(1)

    write_tree(config, work)

    processed = ast.process(work['merged'], work[
                            'parsed'], config['independent'])
    info.logModelInfo(processed, config)
    write_comp(config, processed)
    write_graph(config, processed)

    source = codegen.generateSource(processed, config)

    codepath = os.path.join(config['outdir'], config['name'] + CODE_EXT)
    logger.message("Attempting to write C code to " + codepath)

    try:
        cfile = open(codepath, 'w')
        cfile.write(source)
        print("C file written to {}".format(codepath))
        cfile.close()
    except IOError as e:
        logger.error("Error writing file ({0}): {1}\nAttempted to write to\n\t{2}.".format(
            e.errno, e.strerror, codepath))
        sys.exit(1)
<commit_msg>Cleane dup debugging code<commit_after>#!/usr/bin/python
#
# CLI compiler for bcmd's new model description language
#

import sys
import argparse
import bcmd_yacc
import os
import decimal
import string
import pprint
import logger
import ast
import codegen
import info

# default compiler configuration
# (this is effectively a template whose details
# may be adapted by command line args)
CONFIG = {'modelpath': ['.', 'models'],
          'outdir': '.',
          'outfile': None,
          'treefile': None,
          'name': None,
          'unused': True,
          'graph': None,
          'graph-exclude-unused': False,
          'graph-exclude-init': False,
          'graph-exclude-self': True,
          'graph-exclude-clusters': False,
          'graph-exclude-params': False,
          'independent': 't',
          'input-makes-intermed': True}

# these are effectively constants
VERSION = 0.6
MODELDEF_EXT = '.modeldef'
CODE_EXT = '.c'
MODEL_EXT = '.model'
TREE_EXT = '.tree'
COMPILE_EXT = '.bcmpl'
GRAPHVIZ_EXT = '.gv'

DUMMY_SOURCE = '##\n'

# parse a chosen model definition file and return the AST

def parse_file(filename):
    try:
        f = open(filename)
        data = f.read()
        f.close()
    except IOError as e:
        logger.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        return None

    logger.message("Processing file: " + filename)

    bcmd_yacc.currentFile = filename
    errsBefore = len(bcmd_yacc.compilationInfo['errors'])

    lp = bcmd_yacc.get_lexer_parser()
    result = lp[1].parse(data, lexer=lp[0])

    fileErrs = len(bcmd_yacc.compilationInfo['errors']) - errsBefore
    bcmd_yacc.currentFile = None

    if fileErrs == 1:
        logger.error('Compilation failed with 1 syntax error')
    elif fileErrs > 1:
        logger.error('Compilation failed with %d syntax errors' % fileErrs)

    return fileErrs, result

def print_errors():
    logger.error('*** Summary of model compilation errors ***')
    errs = bcmd_yacc.compilationInfo
    for ii in range(len(errs['errors'])):
        logger.error(errs['messages'][ii]
                     + ' (' + errs['files'][ii]
                     + ', line ' + str(errs['lines'][ii]) + ')')

# find a file on the search path

def search_file(filename, search_path):
    for path in search_path:
        candidate = os.path.join(path, filename)
        if os.path.isfile(candidate):
            return os.path.abspath(candidate)
    return None

# process arguments

def process_args():
    config = CONFIG

    ap = argparse.ArgumentParser(
        description="Model compiler for the BCMD modelling system.")
    ap.add_argument('--version', action='version',
                    version='bcmd version %.1fa' % VERSION)
    ap.add_argument(
        '-i', help='append to default model search path', metavar='PATH')
    ap.add_argument(
        '-I', help='replace default model search path', metavar='PATH')
    ap.add_argument(
        '-n', '--name', help='specify model name (default: <file1>)', metavar='NAME')
    ap.add_argument(
        '-o', help='specify output file name (default: <modelname>.model)', metavar='FILE')
    ap.add_argument(
        '-d', help='specify output directory (default: .)', metavar='DIR')
    ap.add_argument(
        '-u', '--unused', help='omit apparently unused intermediates', action='store_false')
    ap.add_argument(
        '-g', '--debug', help='include debug outputs in generated model code', action='store_true')
    ap.add_argument('-t', '--tree', help='write parse tree to file (default: <modelname>.tree)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-p', '--processed', help='write compilation data to file (default: <modelname>.bcmpl)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-G', '--graph', help='write dependency structure in GraphViz format (default: <modelname>.gv)',
                    nargs='?', default=None, const='', metavar='FILE')
    ap.add_argument('-U', '--graphxunused',
                    help='exclude apparently unused elements from graph output', action='store_true')
    ap.add_argument('-N', '--graphxinit',
                    help='exclude initialisation dependencies from graph output', action='store_true')
    ap.add_argument('-C', '--graphxclust',
                    help='exclude clustering from graph output', action='store_true')
    ap.add_argument('-S', '--graphself',
                    help='include direct circular dependencies in graph output', action='store_false')
    ap.add_argument('-v', '--verbose',
                    help='set level of detail logged to stderr (0-7, default: 3)', metavar='LEVEL', type=int)
    ap.add_argument(
        '-Y', '--yacc', help='run a dummy parse to rebuild parse tables', action='store_true')
    # ... add further options here as needed ...

    ap.add_argument('file', nargs='+',
                    help='one or more model description files to be compiled')

    args = ap.parse_args()

    if args.yacc:
        lp = bcmd_yacc.get_lexer_parser()
        result = lp[1].parse(DUMMY_SOURCE, lexer=lp[0])
        return False

    if not (args.I is None):
        config['modelpath'] = args.I.split(os.pathsep)
    elif not (args.i is None):
        config['modelpath'] = config['modelpath'] + \
            args.i.split(os.pathsep)

    if not (args.name is None):
        config['name'] = args.name
    else:
        srcname, srcext = os.path.splitext(args.file[0])
        config['name'] = srcname

    if not (args.o is None):
        config['outfile'] = args.o
    else:
        config['outfile'] = config['name'] + MODEL_EXT

    if args.d is not None:
        if not os.path.isdir(args.d):
            os.makedirs(args.d)
        config['outdir'] = args.d

    config['treefile'] = args.tree
    config['compfile'] = args.processed
    config['sources'] = args.file
    config['unused'] = args.unused
    config['debug'] = args.debug
    config['graph'] = args.graph
    config['graph-exclude-unused'] = args.graphxunused
    config['graph-exclude-init'] = args.graphxinit
    config['graph-exclude-self'] = args.graphself
    config['graph-exclude-clusters'] = args.graphxclust

    if args.verbose is not None:
        logger.verbosity = args.verbose
    return config

# load and parse source files named on the command line, plus imports
# note failures and return a structure including those details and
# the resulting merged item list

def load_sources(config):
    sources = config['sources']
    srcIndex = 0
    parsedSources = []
    failedSources = []
    merged = []

    while srcIndex < len(sources):
        logger.message("Searching for source file: " + sources[srcIndex])
        src = search_file(sources[srcIndex], config['modelpath'])
        if (src is None) and (not sources[srcIndex].endswith(MODELDEF_EXT)):
            logger.message(
                "Not found, trying with added extension: " + sources[srcIndex] + MODELDEF_EXT)
            src = search_file(sources[srcIndex] +
                              MODELDEF_EXT, config['modelpath'])

        if src is None:
            logger.warn("File not found: " + sources[srcIndex])
            failedSources.append(sources[srcIndex])
        else:
            nErrs, ast = parse_file(src)
            if nErrs > 0 or ast is None:
                failedSources.append(src)
            else:
                ast = list(ast)

                # add imports that are not already in the source list to it
                for imp in list(sum([x[1:] for x in ast if x[0] == 'import'], ())):
                    if imp not in sources and imp + MODELDEF_EXT not in sources:
                        sources.append(imp)

                logger.detail(ast, prettify=True)
                parsedSources.append((sources[srcIndex], src))

                merged = merged + ast

        srcIndex = srcIndex + 1

    logger.message("Total number of attempted source files: %d" % srcIndex)
    logger.message("%d parsed, %d failed" %
                   (len(parsedSources), len(failedSources)))
    for failed in failedSources:
        logger.message("  ->  %s" % failed)

    return {'sources': sources, 'parsed': parsedSources, 'failed': failedSources, 'merged': merged}

# write the loaded merged item list to a file, if so specified
def write_tree(config, work):
    if not config['treefile'] is None:
        if config['treefile'] == '':
            config['treefile'] = config['name'] + TREE_EXT
        treePath = os.path.join(config['outdir'], config['treefile'])
        logger.message("Attempting to write parse tree to " + treePath)
        try:
            treeStream = open(treePath, 'w')
            pprint.pprint(work['merged'], stream=treeStream)
            treeStream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

# write the processed model structure to a file, if so specified

def write_comp(config, processed):
    if not config['compfile'] is None:
        if config['compfile'] == '':
            config['compfile'] = config['name'] + COMPILE_EXT
        compPath = os.path.join(config['outdir'], config['compfile'])
        logger.message(
            "Attempting to write compilation structure to " + compPath)
        try:
            compStream = open(compPath, 'w')
            pprint.pprint(processed, stream=compStream)
            compStream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

# write the model dependencies to a graph, if so specified

def write_graph(config, model):
    if not config['graph'] is None:
        if config['graph'] == '':
            config['graph'] = config['name'] + GRAPHVIZ_EXT
        graphPath = os.path.join(config['outdir'], config['graph'])
        logger.message("Attempting to write dependency graph to " + graphPath)
        try:
            stream = open(graphPath, 'w')
            print(info.generateGraphViz(model, config), file=stream)
            stream.close()
        except IOError as e:
            logger.error("Error writing file ({0}): {1}".format(
                e.errno, e.strerror))

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

# main entry point of this compiler script
if __name__ == '__main__':
    config = process_args()
    if not config:
        sys.exit(2)

    work = load_sources(config)
    if len(work['failed']) > 0:
        print_errors()
        sys.exit(1)

    write_tree(config, work)

    processed = ast.process(work['merged'], work[
                            'parsed'], config['independent'])
    info.logModelInfo(processed, config)
    write_comp(config, processed)
    write_graph(config, processed)

    source = codegen.generateSource(processed, config)

    codepath = os.path.join(config['outdir'], config['name'] + CODE_EXT)
    logger.message("Attempting to write C code to " + codepath)

    try:
        cfile = open(codepath, 'w')
        cfile.write(source)
        cfile.close()
    except IOError as e:
        logger.error("Error writing file ({0}): {1}\nAttempted to write to\n\t{2}.".format(
            e.errno, e.strerror, codepath))
        sys.exit(1)
<|endoftext|>


<commit_before>import vagrant
from gkeepcore.shell_command import run_command, run_command_in_directory
from robot.api import logger

class VagrantControl:

    def __init__(self):
        self.v = vagrant.Vagrant()
        self.up_verified = False

    def make_boxes_if_missing(self):
        names = [box.name for box in self.v.box_list()]

        if 'gkserver' not in names:
            logger.console('Making gkserver.box.  This can take up to 30 minutes but only executes once per system.')
            run_command_in_directory('gkserver_base', 'bash -c ./make_box.sh')
            self.v.box_add('gkserver', 'gkserver_base/gkserver.box')

        if 'gkclient' not in names:
            logger.console('Making gkclient.box.  This can take up to 30 minutes but only executes once per system.')
            run_command_in_directory('gkclient_base', 'bash -c ./make_box.sh')
            self.v.box_add('gkclient', 'gkclient_base/gkclient.box')

    def start_vagrant(self, vagrant_start):
        if vagrant_start == 'Yes':
            if self.is_server_running():
                raise AssertionError('gkserver already running!')
            if self.is_client_running():
                raise AssertionError('gkclient already running!')
            logger.console('Starting gkserver and gkclient VMs...')
            self.v.up()
        else:
            self.check_status()
            logger.console('Using existing gkserver and gkclient VMS...')

    def start_gkeepd(self):
        self.run_on_server('keeper', 'screen -S gkserver -d -m gkeepd')

    def stop_gkeepd(self):
        self.run_on_server('keeper', 'screen -S gkserver -X quit')

    def stop_vagrant(self, vagrant_stop):
        if vagrant_stop == 'Yes':
            logger.console('Destroying gkserver and gkclient VMs...')
            self.v.destroy()
        else:
            logger.console('Leaving gkserver and gkclient VMs running...')

    def expect_email(self, to_user, contains):
        result = self.run_server_script('keeper', 'email_check.py', to_user, contains)
        assert result == 'True'

    def user_exists(self, username):
        result = self.run_server_script('keeper', 'user_exists.py', username)
        assert result == 'True'

    def remove_user(self, username):
        self.run_on_server('keeper', 'sudo userdel -r {} || :'.format(username))

    def remove_file(self, username, filename):
        self.run_on_server(username, 'rm {}'.format(filename))

    def clear_email(self):
        self.run_on_server('keeper', 'sudo find /email ! -name "mysmtpd.py" -type f -exec rm -f {} +')

    def run_on_server(self, username, cmd):
        return self.run_on(username, cmd, self.get_server_port())

    def run_on_client(self, username, cmd):
        return self.run_on(username, cmd, self.get_client_port())

    def run_server_script(self, username, script, *args):
        base = 'python3 /vagrant/server_scripts/' + script
        cmd = ' '.join([base] + list(args))
        return self.run_on_server(username, cmd).strip()

    def run_on(self, username, cmd, port):
        self.check_status()
        base = 'ssh localhost'
        port = '-p {}'.format(port)
        user_and_key = '-l {} -i ssh_keys/{}_rsa'.format(username, username)
        suppress_warnings = '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR'

        full_cmd = ' '.join([base, port, user_and_key, suppress_warnings, cmd])
        return run_command(full_cmd)

    def is_server_running(self):
        result = self.v.status(vm_name='gkserver')
        return result[0].state == 'running'

    def is_client_running(self):
        result = self.v.status(vm_name='gkclient')
        return result[0].state == 'running'

    def check_status(self):
        if self.up_verified:
            return
        if not self.is_server_running():
            raise AssertionError('gkserver not running')
        if not self.is_client_running():
            raise AssertionError('gkclient not running')
        self.up_verified = True

    def get_client_port(self):
        self.check_status()
        return self.v.port(vm_name='gkclient')

    def get_server_port(self):
        self.check_status()
        return self.v.port(vm_name='gkserver')
<commit_msg>Improve logging<commit_after>import vagrant
from gkeepcore.shell_command import run_command, run_command_in_directory
from robot.api import logger

class VagrantControl:

    def __init__(self):
        self.v = vagrant.Vagrant()
        self.up_verified = False

    def make_boxes_if_missing(self):
        names = [box.name for box in self.v.box_list()]

        if 'gkserver' not in names:
            logger.console('Making gkserver.box.  This can take up to 30 minutes.')
            run_command_in_directory('gkserver_base', 'bash -c ./make_box.sh')
            self.v.box_add('gkserver', 'gkserver_base/gkserver.box')

        if 'gkclient' not in names:
            logger.console('Making gkclient.box.  This can take up to 30 minutes.')
            run_command_in_directory('gkclient_base', 'bash -c ./make_box.sh')
            self.v.box_add('gkclient', 'gkclient_base/gkclient.box')

    def start_vagrant(self, vagrant_start):
        if vagrant_start == 'Yes':
            if self.is_server_running():
                raise AssertionError('gkserver already running!')
            if self.is_client_running():
                raise AssertionError('gkclient already running!')
            logger.console('Starting gkserver and gkclient VMs...')
            self.v.up()
        else:
            self.check_status()
            logger.console('Using existing gkserver and gkclient VMS...')

    def start_gkeepd(self):
        self.run_on_server('keeper', 'screen -S gkserver -d -m gkeepd')

    def stop_gkeepd(self):
        self.run_on_server('keeper', 'screen -S gkserver -X quit')

    def stop_vagrant(self, vagrant_stop):
        if vagrant_stop == 'Yes':
            logger.console('Destroying gkserver and gkclient VMs...')
            self.v.destroy()
        else:
            logger.console('Leaving gkserver and gkclient VMs running...')

    def expect_email(self, to_user, contains):
        result = self.run_server_script('keeper', 'email_check.py', to_user, contains)
        assert result == 'True'

    def user_exists(self, username):
        result = self.run_server_script('keeper', 'user_exists.py', username)
        assert result == 'True'

    def remove_user(self, username):
        self.run_on_server('keeper', 'sudo userdel -r {} || :'.format(username))

    def remove_file(self, username, filename):
        self.run_on_server(username, 'rm {}'.format(filename))

    def clear_email(self):
        self.run_on_server('keeper', 'sudo find /email ! -name "mysmtpd.py" -type f -exec rm -f {} +')

    def run_on_server(self, username, cmd):
        return self.run_on(username, cmd, self.get_server_port())

    def run_on_client(self, username, cmd):
        return self.run_on(username, cmd, self.get_client_port())

    def run_server_script(self, username, script, *args):
        base = 'python3 /vagrant/server_scripts/' + script
        cmd = ' '.join([base] + list(args))
        return self.run_on_server(username, cmd).strip()

    def run_on(self, username, cmd, port):
        self.check_status()
        base = 'ssh localhost'
        port = '-p {}'.format(port)
        user_and_key = '-l {} -i ssh_keys/{}_rsa'.format(username, username)
        suppress_warnings = '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR'

        full_cmd = ' '.join([base, port, user_and_key, suppress_warnings, cmd])
        return run_command(full_cmd)

    def is_server_running(self):
        result = self.v.status(vm_name='gkserver')
        return result[0].state == 'running'

    def is_client_running(self):
        result = self.v.status(vm_name='gkclient')
        return result[0].state == 'running'

    def check_status(self):
        if self.up_verified:
            return
        if not self.is_server_running():
            raise AssertionError('gkserver not running')
        if not self.is_client_running():
            raise AssertionError('gkclient not running')
        self.up_verified = True

    def get_client_port(self):
        self.check_status()
        return self.v.port(vm_name='gkclient')

    def get_server_port(self):
        self.check_status()
        return self.v.port(vm_name='gkserver')
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import time
import unittest

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import EchoWebSocket

class FakeSocket(object):
    def settimeout(self, timeout):
        pass

    def shutdown(self, flag):
        pass

    def close(self):
        pass

    def sendall(self, bytes):
        return len(bytes)

    def setblocking(self, flag):
        pass

    def fileno(self):
        return 1

    def recv(self, bufsize=0):
        pass

    def getsockname(self):
        return '127.0.0.1', 0

    def getpeername(self):
        return '127.0.0.1', 8091

class App(object):
    @cherrypy.expose
    def ws(self):
        assert cherrypy.request.ws_handler != None

def setup_engine():
    # we don't need a HTTP server for this test
    cherrypy.server.unsubscribe()

    cherrypy.config.update({'log.screen': False})

    cherrypy.engine.websocket = WebSocketPlugin(cherrypy.engine)
    cherrypy.engine.websocket.subscribe()

    cherrypy.tools.websocket = WebSocketTool()

    config={'/ws': {'tools.websocket.on': True,
                    'tools.websocket.handler_cls': EchoWebSocket}}
    cherrypy.tree.mount(App(), '/', config)
    cherrypy.engine.start()

def teardown_engine():
    cherrypy.engine.exit()

class CherryPyTest(unittest.TestCase):
    def setUp(self):
        setup_engine()

    def tearDown(self):
        teardown_engine()

    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEquals(len(manager), 0)

        s = FakeSocket()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEquals(len(manager), 1)
        self.assertIn(h, manager)

        h.close() # initiate closing handshake
        h.client_terminated = True # we aren't actually connected so pretend the client shutdown

        # TODO: Implement a fake poller so that works...
        self.assertEquals(len(manager), 0)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for testcase in [CherryPyTest]:
        tests = loader.loadTestsFromTestCase(testcase)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)
<commit_msg>updated test to match new internal manager<commit_after># -*- coding: utf-8 -*-
import time
import unittest

import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import EchoWebSocket

class FakeSocket(object):
    def settimeout(self, timeout):
        pass

    def shutdown(self, flag):
        pass

    def close(self):
        pass

    def sendall(self, bytes):
        return len(bytes)

    def setblocking(self, flag):
        pass

    def fileno(self):
        return 1

    def recv(self, bufsize=0):
        pass

    def getsockname(self):
        return '127.0.0.1', 0

    def getpeername(self):
        return '127.0.0.1', 8091

class FakePoller(object):
    def __init__(self, timeout=0.1):
        self._fds = []

    def release(self):
        self._fds = []

    def register(self, fd):
        if fd not in self._fds:
            self._fds.append(fd)

    def unregister(self, fd):
        if fd in self._fds:
            self._fds.remove(fd)

    def poll(self):
        return self._fds

class App(object):
    @cherrypy.expose
    def ws(self):
        assert cherrypy.request.ws_handler != None

def setup_engine():
    # we don't need a HTTP server for this test
    cherrypy.server.unsubscribe()

    cherrypy.config.update({'log.screen': False})

    cherrypy.engine.websocket = WebSocketPlugin(cherrypy.engine)
    cherrypy.engine.websocket.subscribe()

    cherrypy.engine.websocket.manager.poller = FakePoller()

    cherrypy.tools.websocket = WebSocketTool()

    config={'/ws': {'tools.websocket.on': True,
                    'tools.websocket.handler_cls': EchoWebSocket}}
    cherrypy.tree.mount(App(), '/', config)
    cherrypy.engine.start()

def teardown_engine():
    cherrypy.engine.exit()

class CherryPyTest(unittest.TestCase):
    def setUp(self):
        setup_engine()

    def tearDown(self):
        teardown_engine()

    def test_plugin(self):
        manager = cherrypy.engine.websocket.manager
        self.assertEquals(len(manager), 0)

        s = FakeSocket()
        h = EchoWebSocket(s, [], [])
        cherrypy.engine.publish('handle-websocket', h, ('127.0.0.1', 0))
        self.assertEquals(len(manager), 1)
        self.assertIn(h, manager)

        h.close()

        # the poller runs a thread, give it time to get there
        time.sleep(0.5)

        # TODO: Implement a fake poller so that works...
        self.assertEquals(len(manager), 0)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for testcase in [CherryPyTest]:
        tests = loader.loadTestsFromTestCase(testcase)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import csv
import six
from builtins import object, str

import collections
import six
from collections import OrderedDict as SortedDict
from django.http import StreamingHttpResponse
from future import standard_library
from io import StringIO, BytesIO

standard_library.install_aliases()

__all__ = ("formats", )

class FormatsException(Exception):
    pass

class Formats(SortedDict):

    def add(self, format):
        parent = self

        def decorator(func):
            if isinstance(func, collections.Callable):
                parent.update({format: func})
            else:
                raise FormatsException("func is not a function.")

        return decorator

formats = Formats()

# Taken from http://docs.python.org/library/csv.html#csv-examples
class UnicodeWriter:
    """
    A CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, dialect=csv.excel_tab, encoding="utf-8", **kwds):
        # Redirect output to a queue
        self.queue = BytesIO() if six.PY2 else StringIO()
        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)

    def writerow(self, row):
        self.writer.writerow([str(s) for s in row])

    def get_values(self):
        # Fetch UTF-8 output from the queue ...
        ret = self.queue.getvalue()
        # empty queue
        self.queue.truncate(0)
        return ret.encode('utf-8').lstrip(b'\0')

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

def base_export(labels, results, dialect=csv.excel_tab):
    w = UnicodeWriter(dialect=dialect)
    count = 0
    w.writerow(labels)
    for row in results:
        count += 1
        w.writerow(row)

        if count % 200 == 0:
            yield w.get_values()

    yield w.get_values()

def make_attachment(response, ext):
    response['Content-Disposition'] = 'attachment; filename=export.%s' % ext
    return response

@formats.add("csv")
def csv_format(labels, results):
    content_type = "text/csv"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "csv")

@formats.add("ods")
def ods_format(labels, results):
    content_type = "application/vnd.oasis.opendocument.spreadsheet"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "ods")

@formats.add("xls")
def xls_format(labels, results):
    content_type = "application/vnd.ms-excel"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "xls")
<commit_msg>FR-376-FR-377 refactor future away<commit_after># -*- coding: utf-8 -*-
import csv
from collections import OrderedDict, Callable
from io import StringIO, BytesIO

import six
from django.http import StreamingHttpResponse

__all__ = ("formats", )

class FormatsException(Exception):
    pass

class Formats(OrderedDict):

    def add(self, format):
        parent = self

        def decorator(func):
            if isinstance(func, Callable):
                parent.update({format: func})
            else:
                raise FormatsException("func is not a function.")

        return decorator

formats = Formats()

# Taken from http://docs.python.org/library/csv.html#csv-examples
class UnicodeWriter(object):
    """
    A CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, dialect=csv.excel_tab, encoding="utf-8", **kwds):
        # Redirect output to a queue
        self.queue = BytesIO() if six.PY2 else StringIO()
        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)

    def writerow(self, row):
        if six.PY2:
            self.writer.writerow([s.encode('utf-8') for s in row])
        else:
            self.writer.writerow([s for s in row])

    def get_values(self):
        # Fetch UTF-8 output from the queue ...
        ret = self.queue.getvalue()
        # empty queue
        self.queue.truncate(0)
        if six.PY2:
            return ret.lstrip(b'\0')
        return ret.encode('utf-8').lstrip(b'\0')

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

def base_export(labels, results, dialect=csv.excel_tab):
    w = UnicodeWriter(dialect=dialect)
    count = 0
    w.writerow(labels)
    for row in results:
        count += 1
        w.writerow(row)

        if count % 200 == 0:
            yield w.get_values()

    yield w.get_values()

def make_attachment(response, ext):
    response['Content-Disposition'] = 'attachment; filename=export.%s' % ext
    return response

@formats.add("csv")
def csv_format(labels, results):
    content_type = "text/csv"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "csv")

@formats.add("ods")
def ods_format(labels, results):
    content_type = "application/vnd.oasis.opendocument.spreadsheet"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "ods")

@formats.add("xls")
def xls_format(labels, results):
    content_type = "application/vnd.ms-excel"
    return make_attachment(StreamingHttpResponse(base_export(labels, results, dialect=csv.excel), content_type=content_type), "xls")
<|endoftext|>


<commit_before>import redis

<commit_msg>added redis_host<commit_after># builtin
import logging

# config
from config import shared_config

# third party
import redis

logger = logging.getLogger(shared_config.api_log_root_name + __name__)
pipe = redis.StrictRedis(host=shared_config.redis_host).pipeline()

def names_list():
    results = pipe.keys("*").execute()[0]
    try:
        if results:
            logger.debug("returning all names with details")
            for name in results: pipe.object(infotype="encoding", key=name)

            key_types = pipe.execute()

            for r in results: pipe.execute_command("TTL", r)

            k_tt = pipe.execute()

            names = [{"encoding": ktyp, "name": knam, "ttl": ktt} for ktyp, knam, ktt in zip(key_types, results, k_tt)]
        else:
            names = None
        return names
    except Exception as e:
        logger.error("failed to return key list because %s" % e, exc_info=True)
        raise RuntimeError("failed to return key list because %s" % e)<|endoftext|>


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

"""
Test unique stratis instance.
"""

# isort: STDLIB
import os
import subprocess
import unittest

_STRATISD = os.environ["STRATISD"]

class TestUniqueInstance(unittest.TestCase):
    """
    Test that only one instance of stratisd can be running at any given time.
    """

    def setUp(self):
        """
        Start the original stratisd instance. Register a cleanup function to
        terminate it once started.
        """
        self.command_line = [_STRATISD, "--sim", "--debug"]
        self.first_process = subprocess.Popen(
            self.command_line,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            close_fds=True,
            env=os.environ,
        )

        self.addCleanup(self.first_process.terminate)

    def test_unique_instance(self):
        """
        Verify that a second stratisd instance can not be started.
        """
        process = subprocess.Popen(
            self.command_line,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            close_fds=True,
            env=os.environ,
        )
        (_, stderr) = process.communicate()
        self.assertEqual(process.returncode, 1)
        self.assertNotEqual(stderr, "")
<commit_msg>Capture and display the stderr output from the PID file test<commit_after># Copyright 2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Test unique stratis instance.
"""

# isort: STDLIB
import os
import subprocess
import sys
import unittest

_STRATISD = os.environ["STRATISD"]

class TestUniqueInstance(unittest.TestCase):
    """
    Test that only one instance of stratisd can be running at any given time.
    """

    def setUp(self):
        """
        Start the original stratisd instance. Register a cleanup function to
        terminate it once started.
        """
        process = subprocess.Popen(
            [_STRATISD, "--sim", "--debug"],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            close_fds=True,
            env=os.environ,
        )

        def cleanup():
            process.terminate()
            process.wait()

        self.addCleanup(cleanup)

    def test_unique_instance(self):
        """
        Verify that a second stratisd instance can not be started.
        """
        process = subprocess.Popen(
            [_STRATISD, "--sim", "--debug"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            close_fds=True,
            env=os.environ,
        )
        (_, stderr) = process.communicate()
        self.assertEqual(process.returncode, 1)
        self.assertNotEqual(stderr, "")

        print("Stderr from this invocation of stratisd:", file=sys.stdout, flush=True)
        print(stderr, file=sys.stdout, flush=True)
<|endoftext|>


<commit_before>import unittest
import os
from datetime import date, timedelta

import tinydb

import database

class FetchAvailableEpisodesTests(unittest.TestCase):
    """
    Test class to test fetch_available_episodes
    """

    def setUp(self):
        """
        Configure database for test purposes. Remind to close db after data
        insertion.
        """
        database.DEFAULT_DATABASE = 'test.db'
        self.db = database.opendb()

    def tearDown(self):
        os.remove('test.db')

    def test_function_1(self):
        """
        Test function in situation where only one is available
        """
        anime_dict = [{
            'name': 'test bangumi 2',
            'keyword': 'test bangumi 2',
            'translation_team': ['sumisora'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 0,
            'total_ep': 12,
            'offset': 0
        }, {
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'start_date': date.today() - timedelta(days=49),
            'dled_ep': 6,
            'total_ep': 13,
            'offset': 0
        }
        ]
        self.db.insert_multiple(anime_dict)
        self.db.close()
        avail_eps = database.fetch_available_episodes()
        self.assertEqual([{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 7
        }, {
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 8
        }], avail_eps)

    def test_function_with_offset(self):
        """
        Test function if an offset anime is presented
        """
        anime_dict = [{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'start_date': date.today() - timedelta(days=49),
            'dled_ep': 6,
            'total_ep': 13,
            'offset': 1
        }]
        self.db.insert_multiple(anime_dict)
        self.db.close()
        avail_eps = database.fetch_available_episodes()
        self.assertEqual([{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 7
        }], avail_eps)

class UpdateAnimeInfoTests(unittest.TestCase):
    """
    Test cases to test update_anime_info
    """

    def setUp(self):
        """
        Configure database for test purposes. Remind to close db after data
        insertion.
        """
        database.DEFAULT_DATABASE = 'test.db'
        self.db = database.opendb()

    def tearDown(self):
        os.remove('test.db')

    def test_function_1(self):
        """
        Test function under situation with one entry in database
        """
        anime = {
            'name': 'test bangumi 2',
            'translation_team': ['sumisora'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 0,
            'total_ep': 12,
            'offset': 0
        }
        self.db.insert(anime)
        new_info = {
            'new_name': 'another',
            'new_translation_team': ['kmp'],
            'new_dled_ep': 2,
        }
        database.update_anime_info('test bangumi 2', new_info)
        q = tinydb.Query()
        new = self.db.get(q.name == 'another')
        self.assertEqual({
            'name': 'another',
            'translation_team': ['kmp'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 2,
            'total_ep': 12,
            'offset': 0
        }, new)
        self.db.close()
<commit_msg>add test for remove_anime<commit_after>import unittest
import os
from datetime import date, timedelta

import tinydb

import database

class FetchAvailableEpisodesTests(unittest.TestCase):
    """
    Test class to test fetch_available_episodes
    """

    def setUp(self):
        """
        Configure database for test purposes. Remind to close db after data
        insertion.
        """
        database.DEFAULT_DATABASE = 'test.db'
        self.db = database.opendb()

    def tearDown(self):
        os.remove('test.db')

    def test_function_1(self):
        """
        Test function in situation where only one is available
        """
        anime_dict = [{
            'name': 'test bangumi 2',
            'keyword': 'test bangumi 2',
            'translation_team': ['sumisora'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 0,
            'total_ep': 12,
            'offset': 0
        }, {
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'start_date': date.today() - timedelta(days=49),
            'dled_ep': 6,
            'total_ep': 13,
            'offset': 0
        }
        ]
        self.db.insert_multiple(anime_dict)
        self.db.close()
        avail_eps = database.fetch_available_episodes()
        self.assertEqual([{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 7
        }, {
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 8
        }], avail_eps)

    def test_function_with_offset(self):
        """
        Test function if an offset anime is presented
        """
        anime_dict = [{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'start_date': date.today() - timedelta(days=49),
            'dled_ep': 6,
            'total_ep': 13,
            'offset': 1
        }]
        self.db.insert_multiple(anime_dict)
        self.db.close()
        avail_eps = database.fetch_available_episodes()
        self.assertEqual([{
            'name': 'test bangumi 3',
            'keyword': 'test bangumi 3',
            'translation_team': ['sumisora'],
            'ep': 7
        }], avail_eps)

class UpdateAnimeInfoTests(unittest.TestCase):
    """
    Test cases to test update_anime_info
    """

    def setUp(self):
        """
        Configure database for test purposes. Remind to close db after data
        insertion.
        """
        database.DEFAULT_DATABASE = 'test.db'
        self.db = database.opendb()

    def tearDown(self):
        os.remove('test.db')

    def test_function_1(self):
        """
        Test function under situation with one entry in database
        """
        anime = {
            'name': 'test bangumi 2',
            'translation_team': ['sumisora'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 0,
            'total_ep': 12,
            'offset': 0
        }
        self.db.insert(anime)
        new_info = {
            'new_name': 'another',
            'new_translation_team': ['kmp'],
            'new_dled_ep': 2,
        }
        database.update_anime_info('test bangumi 2', new_info)
        q = tinydb.Query()
        new = self.db.get(q.name == 'another')
        self.assertEqual({
            'name': 'another',
            'translation_team': ['kmp'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 2,
            'total_ep': 12,
            'offset': 0
        }, new)
        self.db.close()

class RemoveAnimeTests(unittest.TestCase):
    """
    Test cases to test remove_anime
    """

    def setUp(self):
        """
        Configure database for test purposes. Remind to close db after data
        insertion.
        """
        database.DEFAULT_DATABASE = 'test.db'
        self.db = database.opendb()

    def tearDown(self):
        self.db.close()
        os.remove('test.db')

    def test_function_1(self):
        anime = {
            'name': 'test bangumi 2',
            'translation_team': ['sumisora'],
            'start_date': date.today() + timedelta(days=1),
            'dled_ep': 0,
            'total_ep': 12,
            'offset': 0
        }
        self.db.insert(anime)
        database.remove_anime('test bangumi 2')
        self.assertEqual(0, len(self.db.all()))
<|endoftext|>


<commit_before>VERSION = (1, 2, 0)  # PEP 386
__version__ = ".".join([str(x) for x in VERSION])

default_app_config = 'django_th.apps.DjangoThConfig'
<commit_msg>1.3.0<commit_after>VERSION = (1, 3, 0)  # PEP 386
__version__ = ".".join([str(x) for x in VERSION])

default_app_config = 'django_th.apps.DjangoThConfig'
<|endoftext|>


<commit_before>"""Schema for Grainbin."""
from flask import url_for
from fm_database.models.device import Grainbin, GrainbinUpdate
from marshmallow.fields import Method

from ..base import BaseSchema

class GrainbinSchema(BaseSchema):
    """Marshmallow Grainbin Schema that loads the instance."""

    class Meta:  # pylint: disable=too-few-public-methods
        """Meta class for GrainbinSchema."""

        exclude = ("id", "creation_time", "updates")

        model = Grainbin
        include_relationships = True
        load_instance = True

    url = Method("get_url")

    def get_url(self, obj):  # pylint: disable=no-self-use
        """Return url for Grainbin."""

        return url_for("grainbin.GrainbinById", grainbin_id=obj.id)

class GrainbinUpdateSchema(BaseSchema):
    """Marshmallow GrainbinUpdate Schema that loads the instance."""

    class Meta:  # pylint: disable=too-few-public-methods
        """Meta class for GrainbinUpdateSchema."""

        exclude = ("id",)

        model = GrainbinUpdate
        include_relationships = True
        load_instance = True
<commit_msg>add ids to schema<commit_after>"""Schema for Grainbin."""
from flask import url_for
from fm_database.models.device import Grainbin, GrainbinUpdate
from marshmallow.fields import Method

from ..base import BaseSchema

class GrainbinSchema(BaseSchema):
    """Marshmallow Grainbin Schema that loads the instance."""

    class Meta:  # pylint: disable=too-few-public-methods
        """Meta class for GrainbinSchema."""

        exclude = ("creation_time", "updates")

        model = Grainbin
        include_relationships = True
        load_instance = True

    url = Method("get_url")

    def get_url(self, obj):  # pylint: disable=no-self-use
        """Return url for Grainbin."""

        return url_for("grainbin.GrainbinById", grainbin_id=obj.id)

class GrainbinUpdateSchema(BaseSchema):
    """Marshmallow GrainbinUpdate Schema that loads the instance."""

    class Meta:  # pylint: disable=too-few-public-methods
        """Meta class for GrainbinUpdateSchema."""

        # exclude = ("id",)

        model = GrainbinUpdate
        include_relationships = True
        load_instance = True
<|endoftext|>


<commit_before>import unittest
from plexcleaner.exception import PlexCleanerException
from plexcleaner.database import Database

__author__ = 'Jean-Bernard Ratte - jean.bernard.ratte@unary.ca'

class TestDatabase(unittest.TestCase):
    def get_db(self):
        db = Database(database_override=':memory:')
        db._cursor.execute('CREATE TABLE media_parts(id, file)')
        db._cursor.executemany('INSERT INTO media_parts (id, file) VALUES (?, ?)', [(1, '/test/fail'),
                                                                                (2, '/test/fail'),
                                                                                (3, '/test/fail')])
        return db

    def test_database_exception(self):
        with self.assertRaises(PlexCleanerException) as e:
            db = Database(database_override='./path/to/com.plexapp.plugins.library.db')
        self.assertTrue('Could not connect' in e.exception.message)

    def test_update(self):
        db = self.get_db()
        db.update_row(1, '/test/success')
        row = db._cursor.execute('SELECT file FROM media_parts WHERE id = ?', (1,)).fetchone()
        self.assertEqual(row[0], '/test/success')

    def test_update_many(self):
        db = self.get_db()
        db.update_many_row([('/test/success', 1), ('/test/success', 2), ('/test/success', 3)])
        for row in db._cursor.execute('SELECT file FROM media_parts').fetchall():
            self.assertEqual(row[0], '/test/success')

    def test_get_rows(self):
        db = self.get_db()
        rows = db._cursor.execute('SELECT file FROM media_parts').fetchall()
        self.assertEqual(len(rows), 3)

<commit_msg>fix pep8<commit_after>import unittest
from plexcleaner.exception import PlexCleanerException
from plexcleaner.database import Database

__author__ = 'Jean-Bernard Ratte - jean.bernard.ratte@unary.ca'

class TestDatabase(unittest.TestCase):
    def get_db(self):
        db = Database(database_override=':memory:')
        db._cursor.execute('CREATE TABLE media_parts(id, file)')
        db._cursor.executemany('INSERT INTO media_parts (id, file) VALUES (?, ?)', [(1, '/test/fail'),
                                                                                (2, '/test/fail'),
                                                                                (3, '/test/fail')])
        return db

    def test_database_exception(self):
        with self.assertRaises(PlexCleanerException) as e:
            db = Database(database_override='./path/to/com.plexapp.plugins.library.db')
        self.assertTrue('Could not connect' in e.exception.message)

    def test_update(self):
        db = self.get_db()
        db.update_row(1, '/test/success')
        row = db._cursor.execute('SELECT file FROM media_parts WHERE id = ?', (1,)).fetchone()
        self.assertEqual(row[0], '/test/success')

    def test_update_many(self):
        db = self.get_db()
        db.update_many_row([('/test/success', 1), ('/test/success', 2), ('/test/success', 3)])
        for row in db._cursor.execute('SELECT file FROM media_parts').fetchall():
            self.assertEqual(row[0], '/test/success')

    def test_get_rows(self):
        db = self.get_db()
        rows = db._cursor.execute('SELECT file FROM media_parts').fetchall()
        self.assertEqual(len(rows), 3)
<|endoftext|>


<commit_before>#  Copyright (c) 2021, Manfred Moitzi
#  License: MIT License
import pytest

pytest.importorskip("PyQt5")

from ezdxf.lldxf.tags import Tags
from ezdxf.lldxf.loader import load_dxf_structure

from ezdxf.addons.browser import DXFTagsModel, DXFStructureModel
from PyQt5.QtCore import Qt, QModelIndex

# noinspection PyMissingConstructor
class ModelIndex(QModelIndex):
    """Proxy"""

    def __init__(self, row, col):
        self._row = row
        self._col = col

    def row(self):
        return self._row

    def column(self):
        return self._col

class TestDXFTagsModel:
    def tags(self):
        return Tags.from_text(POINT)

    @pytest.fixture
    def model(self):
        return DXFTagsModel(self.tags())

    def test_fixed_column_count(self, model):
        assert model.columnCount() == 3

    def test_row_count(self, model):
        assert model.rowCount() == len(self.tags())

    def test_dxf_entity_type(self, model):
        assert model.dxftype() == "POINT"

    def test_render_display_role(self, model):
        assert model.data(ModelIndex(0, 0), role=Qt.DisplayRole) == "0"
        assert model.data(ModelIndex(0, 1), role=Qt.DisplayRole) == "<ctrl>"
        assert model.data(ModelIndex(0, 2), role=Qt.DisplayRole) == "POINT"

POINT = """0
POINT
5
0
330
0
100
AcDbEntity
8
0
100
AcDbPoint
10
0.0
20
0.0
30
0.0
"""

def test_setup_dxf_structure_model():
    sections = load_dxf_structure(Tags.from_text(ENTITIES))
    model = DXFStructureModel("ez.dxf", sections)
    parent = model.item(0, 0)
    assert parent.data(Qt.DisplayRole) == "ez.dxf"
    assert parent.child(0, 0).data(Qt.DisplayRole) == "ENTITIES"
    # one level down
    parent = parent.child(0, 0)
    assert parent.child(0, 0).data(Qt.DisplayRole) == "#100 LINE"
    assert parent.child(1, 0).data(Qt.DisplayRole) == "#101 LINE"

ENTITIES = """0
SECTION
2
ENTITIES
0
LINE
5
100
0
LINE
5
101
0
ENDSEC
0
EOF
"""

if __name__ == "__main__":
    pytest.main([__file__])
<commit_msg>fix dxf browser test<commit_after>#  Copyright (c) 2021, Manfred Moitzi
#  License: MIT License
import pytest

pytest.importorskip("PyQt5")

from ezdxf.lldxf.tags import Tags
from ezdxf.lldxf.loader import load_dxf_structure

from ezdxf.addons.browser import DXFTagsModel, DXFStructureModel
from PyQt5.QtCore import Qt, QModelIndex

# noinspection PyMissingConstructor
class ModelIndex(QModelIndex):
    """Proxy"""

    def __init__(self, row, col):
        self._row = row
        self._col = col

    def row(self):
        return self._row

    def column(self):
        return self._col

class TestDXFTagsModel:
    def tags(self):
        return Tags.from_text(POINT)

    @pytest.fixture
    def model(self):
        return DXFTagsModel(self.tags())

    def test_fixed_column_count(self, model):
        assert model.columnCount() == 3

    def test_row_count(self, model):
        assert model.rowCount() == len(self.tags())

    def test_dxf_entity_type(self, model):
        assert model.dxftype() == "POINT"

    def test_render_display_role(self, model):
        assert model.data(ModelIndex(0, 0), role=Qt.DisplayRole) == "0"
        assert model.data(ModelIndex(0, 1), role=Qt.DisplayRole) == "<ctrl>"
        assert model.data(ModelIndex(0, 2), role=Qt.DisplayRole) == "POINT"

POINT = """0
POINT
5
0
330
0
100
AcDbEntity
8
0
100
AcDbPoint
10
0.0
20
0.0
30
0.0
"""

def test_setup_dxf_structure_model():
    sections = load_dxf_structure(Tags.from_text(ENTITIES))
    model = DXFStructureModel("ez.dxf", sections)
    parent = model.item(0, 0)
    assert parent.data(Qt.DisplayRole) == "ez.dxf"
    assert "ENTITIES" in parent.child(0, 0).data(Qt.DisplayRole)
    # one level down
    parent = parent.child(0, 0)
    assert "LINE" in parent.child(0, 0).data(Qt.DisplayRole)
    assert "LINE" in parent.child(1, 0).data(Qt.DisplayRole)

ENTITIES = """0
SECTION
2
ENTITIES
0
LINE
5
100
0
LINE
5
101
0
ENDSEC
0
EOF
"""

if __name__ == "__main__":
    pytest.main([__file__])
<|endoftext|>


<commit_before>import unittest

from nanomongo import Field, BaseDocument
from nanomongo.errors import ValidationError

PYMONGO_OK = False
try:
    import pymongo
    pymongo.MongoClient()
    PYMONGO_OK = True
except:
    PYMONGO_OK = False

class DocumentTestCase(unittest.TestCase):
    def test_document_bad_field(self):

        def bad_field():
            class Doc(BaseDocument):
                foo = Field()

        def bad_field_default():
            class Doc(BaseDocument):
                foo = Field(str, default=1)

        [self.assertRaises(TypeError, func) for func in (bad_field, bad_field_default)]

    def test_document(self):

        class Doc(BaseDocument):
            bool_a, bool_b = Field(bool), Field(bool)
            foo = Field(int, default=42)
            bar = Field(str, default=None, required=False)

        class Doc2(Doc, dot_notation=True):
            foo = Field(dict)
            zoo = Field(list)

        d = Doc(bool_a=True)
        self.assertEqual(d, {'bool_a': True, 'foo': 42, 'bar': None})
        # bool_b not set and required
        self.assertRaises(ValidationError, d.validate_all)
        # dot_notation not active
        d.bool_b = False
        self.assertRaises(ValidationError, d.validate_all)
        d['bool_b'] = True
        d.validate_all()
        # Doc2 has field 'foo' chaged to type dict, can't create new Doc2 from Doc
        self.assertRaises(ValidationError, Doc2, *(d,))
        self.assertRaises(ValidationError, Doc2, **d)
        # this type change will allow Doc2 cast, but fail Doc validation
        d['foo'] = {'L33t': 42}
        self.assertRaises(ValidationError, d.validate_all)
        dd = Doc2(d)
        self.assertTrue(dd == Doc2(**d) ==
                        Doc2(bool_a=True, bool_b=True, foo={'L33t': 42}, bar=None))
        # dd (Doc2) still has required field 'zoo' unset
        self.assertRaises(ValidationError, dd.validate_all)
        # dd has dot_notation
        dd.zoo = [3.14159265]
        dd.validate_all()

class ClientTestCase(unittest.TestCase):
    @unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
    def test_document_client(self):
        client = pymongo.MongoClient()

        class Doc(BaseDocument, dot_notation=True, client=client, db='nanomongotest'):
            foo = Field(str)

        class Doc2(Doc, dot_notation=True, client=client, db='nanomongotest',
                   collection='othercollection'):
            bar = Field(int)

        class Doc3(Doc):
            bar = Field(float)

        d = Doc()
        dd = Doc2()
        ddd = Doc3()
        self.assertEqual(d.nanomongo.client, dd.nanomongo.client)
        self.assertEqual('nanomongotest', d.nanomongo.database)
        self.assertEqual('nanomongotest', dd.nanomongo.database)
        self.assertEqual('doc', d.nanomongo.collection)
        self.assertEqual('othercollection', dd.nanomongo.collection)

        self.assertNotEqual(d.nanomongo.client, ddd.nanomongo.client)

<commit_msg>bad client/db test case added<commit_after>import unittest

from nanomongo import Field, BaseDocument
from nanomongo.errors import ValidationError

PYMONGO_OK = False
try:
    import pymongo
    pymongo.MongoClient()
    PYMONGO_OK = True
except:
    PYMONGO_OK = False

class DocumentTestCase(unittest.TestCase):
    def test_document_bad_field(self):

        def bad_field():
            class Doc(BaseDocument):
                foo = Field()

        def bad_field_default():
            class Doc(BaseDocument):
                foo = Field(str, default=1)

        [self.assertRaises(TypeError, func) for func in (bad_field, bad_field_default)]

    def test_document(self):

        class Doc(BaseDocument):
            bool_a, bool_b = Field(bool), Field(bool)
            foo = Field(int, default=42)
            bar = Field(str, default=None, required=False)

        class Doc2(Doc, dot_notation=True):
            foo = Field(dict)
            zoo = Field(list)

        d = Doc(bool_a=True)
        self.assertEqual(d, {'bool_a': True, 'foo': 42, 'bar': None})
        # bool_b not set and required
        self.assertRaises(ValidationError, d.validate_all)
        # dot_notation not active
        d.bool_b = False
        self.assertRaises(ValidationError, d.validate_all)
        d['bool_b'] = True
        d.validate_all()
        # Doc2 has field 'foo' chaged to type dict, can't create new Doc2 from Doc
        self.assertRaises(ValidationError, Doc2, *(d,))
        self.assertRaises(ValidationError, Doc2, **d)
        # this type change will allow Doc2 cast, but fail Doc validation
        d['foo'] = {'L33t': 42}
        self.assertRaises(ValidationError, d.validate_all)
        dd = Doc2(d)
        self.assertTrue(dd == Doc2(**d) ==
                        Doc2(bool_a=True, bool_b=True, foo={'L33t': 42}, bar=None))
        # dd (Doc2) still has required field 'zoo' unset
        self.assertRaises(ValidationError, dd.validate_all)
        # dd has dot_notation
        dd.zoo = [3.14159265]
        dd.validate_all()

class ClientTestCase(unittest.TestCase):
    def test_document_cient_bad(self):

        def bad_client():
            class Doc(BaseDocument, client=''):
                pass

        def bad_db():
            class Doc(BaseDocument, db=1234):
                pass

        [self.assertRaises(TypeError, func) for func in (bad_client, bad_db)]

    @unittest.skipUnless(PYMONGO_OK, 'pymongo not installed or connection refused')
    def test_document_client(self):
        client = pymongo.MongoClient()

        class Doc(BaseDocument, dot_notation=True, client=client, db='nanomongotest'):
            foo = Field(str)

        class Doc2(Doc, dot_notation=True, client=client, db='nanomongotest',
                   collection='othercollection'):
            bar = Field(int)

        class Doc3(Doc):
            bar = Field(float)

        d = Doc()
        dd = Doc2()
        ddd = Doc3()
        self.assertEqual(d.nanomongo.client, dd.nanomongo.client)
        self.assertEqual('nanomongotest', d.nanomongo.database)
        self.assertEqual('nanomongotest', dd.nanomongo.database)
        self.assertEqual('doc', d.nanomongo.collection)
        self.assertEqual('othercollection', dd.nanomongo.collection)

        self.assertNotEqual(d.nanomongo.client, ddd.nanomongo.client)

<|endoftext|>


<commit_before>import os
import time
from datetime import datetime
from django.utils.translation import ugettext_lazy as _
from . import uwsgi

def get_uwsgi_stats():
    uwsgi_stats = {}
    workers = uwsgi.workers()
    total_load = time.time() - uwsgi.started_on
    for w in workers:
        w['running_time'] = w['running_time'] / 1000
        w['load'] = w['running_time'] / total_load / 10 / len(workers)
        w['last_spawn'] = datetime.fromtimestamp(w['last_spawn'])
    jobs = []
    if uwsgi.opt.get('spooler'):
        spooler_jobs = uwsgi.spooler_jobs()
        for j in spooler_jobs:
            jobs.append({'file': j, 'env': uwsgi.parsefile(j)})

    uwsgi_stats.update({
        'version': uwsgi.version,
        'hostname': uwsgi.hostname,
        'magic': uwsgi.magic_table,
        'os': os.uname(),
        'masterpid': uwsgi.masterpid(),
        'stats': [
            ('loop', uwsgi.loop),
            ('masterpid', str(uwsgi.masterpid())),
            ('started_on', datetime.fromtimestamp(uwsgi.started_on)),
            ('now', datetime.now()),
            ('buffer_size', uwsgi.buffer_size),
            ('total_requests', uwsgi.total_requests()),
            ('numproc', uwsgi.numproc),
            ('cores', uwsgi.cores),
            ('cwd', os.getcwd()),
            ('logsize', uwsgi.logsize()),
            ('cache_exists', uwsgi.cache_exists),
            ('spooler_pid', uwsgi.spooler_pid() if uwsgi.opt.get('spooler') else _('disabled')),
            ('threads', _('enabled') if uwsgi.has_threads else _('disabled'))
        ],
        'options': uwsgi.opt.items(),
        'workers': workers,
        'jobs': jobs,
    })
    return uwsgi_stats
<commit_msg>Fix time output in avg_rt. Closes #11<commit_after>import os
import time
from datetime import datetime
from django.utils.translation import ugettext_lazy as _
from . import uwsgi

def get_uwsgi_stats():
    uwsgi_stats = {}
    workers = uwsgi.workers()
    total_load = time.time() - uwsgi.started_on
    for w in workers:
        w['running_time'] = w['running_time'] / 1000
        w['avg_rt'] = w['avg_rt'] / 1000
        w['load'] = w['running_time'] / total_load / 10 / len(workers)
        w['last_spawn'] = datetime.fromtimestamp(w['last_spawn'])
    jobs = []
    if uwsgi.opt.get('spooler'):
        spooler_jobs = uwsgi.spooler_jobs()
        for j in spooler_jobs:
            jobs.append({'file': j, 'env': uwsgi.parsefile(j)})

    uwsgi_stats.update({
        'version': uwsgi.version,
        'hostname': uwsgi.hostname,
        'magic': uwsgi.magic_table,
        'os': os.uname(),
        'masterpid': uwsgi.masterpid(),
        'stats': [
            ('loop', uwsgi.loop),
            ('masterpid', str(uwsgi.masterpid())),
            ('started_on', datetime.fromtimestamp(uwsgi.started_on)),
            ('now', datetime.now()),
            ('buffer_size', uwsgi.buffer_size),
            ('total_requests', uwsgi.total_requests()),
            ('numproc', uwsgi.numproc),
            ('cores', uwsgi.cores),
            ('cwd', os.getcwd()),
            ('logsize', uwsgi.logsize()),
            ('cache_exists', uwsgi.cache_exists),
            ('spooler_pid', uwsgi.spooler_pid() if uwsgi.opt.get('spooler') else _('disabled')),
            ('threads', _('enabled') if uwsgi.has_threads else _('disabled'))
        ],
        'options': uwsgi.opt.items(),
        'workers': workers,
        'jobs': jobs,
    })
    return uwsgi_stats
<|endoftext|>


<commit_before>import openpnm as op
import openpnm.models.geometry.throat_area as mods
import numpy as np
from numpy.testing import assert_approx_equal

class ThroatSurfaceAreaTest:
    def setup_class(self):
        self.net = op.network.Cubic(shape=[5, 5, 5], spacing=1.0)
        self.geo = op.geometry.GenericGeometry(network=self.net,
                                               pores=self.net.Ps,
                                               throats=self.net.Ts)
        self.air = op.phases.Air(network=self.net)
        self.phys = op.physics.GenericPhysics(network=self.net,
                                              phase=self.air,
                                              geometry=self.geo)
        self.geo['pore.diameter'] = 0.2
        self.geo['throat.length'] = 1
        self.geo['throat.diameter'] = 0.1

    def test_sphere(self):
        self.geo.add_model(propname='throat.area',
                           model=mods.cylinder,
                           regen_mode='normal')
        a = np.array([0.007853981])
        b = np.unique(self.geo['throat.area'])
        assert_approx_equal(a, b)

    def test_cube(self):
        self.geo.add_model(propname='throat.area',
                           model=mods.cuboid,
                           regen_mode='normal')
        a = np.array([0.01])
        b = np.unique(self.geo['throat.area'])
        assert_approx_equal(a, b)

    def test_equivalent_area_spherical_pores(self):
        self.geo['throat.area'] = np.pi * self.geo['throat.diameter']**2 / 4
        self.geo.add_model(propname='throat.equivalent_area',
                           model=mods.equivalent_area_spherical_pores,
                           regen_mode='normal')
        a = np.array([0.008798571])
        b = np.unique(self.geo['throat.equivalent_area'])
        assert_approx_equal(a, b)

    def test_equivalent_area_circular_pores(self):
        self.geo['throat.area'] = self.geo['throat.diameter']
        self.geo.add_model(propname='throat.equivalent_area',
                           model=mods.equivalent_area_circular_pores,
                           regen_mode='normal')
        a = np.array([0.1073520])
        b = np.unique(self.geo['throat.equivalent_area'])
        assert_approx_equal(a, b)

if __name__ == '__main__':

    t = ThroatSurfaceAreaTest()
    self = t
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print('running test: '+item)
            t.__getattribute__(item)()
<commit_msg>Removed tests for equivalent_area models from ThroatAreaTest.py<commit_after>import openpnm as op
import openpnm.models.geometry.throat_area as mods
import numpy as np
from numpy.testing import assert_approx_equal

class ThroatSurfaceAreaTest:
    def setup_class(self):
        self.net = op.network.Cubic(shape=[5, 5, 5], spacing=1.0)
        self.geo = op.geometry.GenericGeometry(network=self.net,
                                               pores=self.net.Ps,
                                               throats=self.net.Ts)
        self.air = op.phases.Air(network=self.net)
        self.phys = op.physics.GenericPhysics(network=self.net,
                                              phase=self.air,
                                              geometry=self.geo)
        self.geo['pore.diameter'] = 0.2
        self.geo['throat.length'] = 1
        self.geo['throat.diameter'] = 0.1

    def test_sphere(self):
        self.geo.add_model(propname='throat.area',
                           model=mods.cylinder,
                           regen_mode='normal')
        a = np.array([0.007853981])
        b = np.unique(self.geo['throat.area'])
        assert_approx_equal(a, b)

    def test_cube(self):
        self.geo.add_model(propname='throat.area',
                           model=mods.cuboid,
                           regen_mode='normal')
        a = np.array([0.01])
        b = np.unique(self.geo['throat.area'])
        assert_approx_equal(a, b)

if __name__ == '__main__':

    t = ThroatSurfaceAreaTest()
    self = t
    t.setup_class()
    for item in t.__dir__():
        if item.startswith('test'):
            print('running test: '+item)
            t.__getattribute__(item)()
<|endoftext|>


<commit_before>def test_dupradar(sample1_se_dupradar):
    assert open(sample1_se_dupradar['dataframe']).readline().startswith('"ID"\t"geneLength"')
<commit_msg>marks dupradar as expeted to fail until we get a good test dataset.<commit_after>import pytest

@pytest.mark.xfail
def test_dupradar(sample1_se_dupradar):
    assert open(sample1_se_dupradar['dataframe']).readline().startswith('"ID"\t"geneLength"')
<|endoftext|>


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

import timeside.api
import timeside.exceptions
import timeside.core
import timeside.component
import timeside.metadata
import timeside.decoder
import timeside.encoder
import timeside.grapher
import timeside.analyzer
from timeside.core import *

__version__ = '0.3.2'
<commit_msg>update version<commit_after># -*- coding: utf-8 -*-

import timeside.api
import timeside.exceptions
import timeside.core
import timeside.component
import timeside.metadata
import timeside.decoder
import timeside.encoder
import timeside.grapher
import timeside.analyzer
from timeside.core import *

__version__ = '0.3.3'
<|endoftext|>