<commit_before>"""Core models used by the CMS."""

import datetime

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core import urlresolvers
from django.db import models
from django.db.models import Q

from cms.core import sitemaps
from cms.core.models.base import PageBase
from cms.core.models.managers import publication_manager
from cms.core.optimizations import CachedProperty
from cms.apps import historylinks
from cms.apps.pages.models.managers import PageManager
from cms.apps.pages.models.fields import PageField

class Page(PageBase):

    """A page within the site."""

    objects = PageManager()

    @classmethod
    def select_published(cls, queryset):
        """Selects only published pages."""
        queryset = super(Page, cls).select_published(queryset)
        now = datetime.datetime.now()
        queryset = queryset.filter(Q(publication_date=None) | Q(publication_date__lte=now))
        queryset = queryset.filter(Q(expiry_date=None) | Q(expiry_date__gt=now))
        return queryset

    # Base fields.

    def __init__(self, *args, **kwargs):
        """"Initializes the Page."""
        super(Page, self).__init__(*args, **kwargs)
        if self.id:
            self.__class__.objects.cache.put(self)

    # Hierarchy fields.

    parent = PageField(
        blank = True,
        null = True
    )

    order = models.PositiveIntegerField(editable=False)

    @cached_property
    @publication_manager.getter
    def children(self):
        """The children of this page."""
        return list(Page.objects.filter(parent=self).order_by("order").iterator())

    @property
    def navigation(self):
        """The sub-navigation of this page."""
        return [child for child in super(Page, self).navigation if child.in_navigation]

    # Publication fields.

    publication_date = models.DateTimeField(blank=True,
                                            null=True,
                                            help_text="The date that this page will appear on the website.  Leave this blank to immediately publish this page.")

    expiry_date = models.DateTimeField(blank=True,
                                       null=True,
                                       help_text="The date that this page will be removed from the website.  Leave this blank to never expire this page.")

    # Navigation fields.

    in_navigation = models.BooleanField("add to navigation",
                                        default=True,
                                        help_text="Uncheck this box to remove this content from the site navigation.")

    permalink = models.SlugField(blank=True,
                                 help_text="A unique identifier for this page.  This will be set by your design team in order to link to this page from any custom templates they write.")

    # Content fields.

    content_type = models.ForeignKey(
        ContentType,
        editable = False,
        help_text="The type of page content.",
    )

    @cached_property
    def content(self):
        """The associated content model for this page."""
        content_cls = ContentType.objects.get_for_id(self.content_type_id).model_class()
        content = content_cls._default_manager.get(page=self)
        content.page = self
        return content

    # Standard model methods.

    def get_absolute_url(self):
        """Generates the absolute url of the page."""
        if self.parent:
            return self.parent.get_absolute_url() + self.url_title + "/"
        return urlresolvers.get_script_prefix()

    def reverse(self, view_func, args, kwargs):
        """Performs a reverse URL lookup."""
        urlconf = ContentType.objects.get_for_id(self.content_type_id).model_class().urlconf
        return self.get_absolute_url() + urlresolvers.reverse(view_func, args=args, kwargs=kwargs, urlconf=urlconf, prefix="")

    def save(self, force_insert=False, force_update=False, using=None, user=None):
        """Saves the page."""
        super(Page, self).save(force_insert, force_update, using, user)
        self.__class__.objects.cache.put(self)

    def delete(self, using=None):
        """Deletes the page."""
        super(Page, self).delete(using)
        self.__class__.objects.cache.remove(self)

    class Meta:
        unique_together = (("parent", "url_title",),)
        ordering = ("order",)

historylinks.register(Page)

sitemaps.register(Page)

# Base content class.

def get_registered_content():
    """Returns a list of all registered content objects."""
    return [
        model for model in models.get_models()
        if issubclass(model, ContentBase) and not model._meta.abstract
    ]

class ContentBase(models.Model):

    """Base class for page content."""

    # This must be a 64 x 64 pixel image.
    icon = settings.STATIC_URL + "cms/img/content-types/content.png"

    # The heading that the admin places this content under.
    classifier = "content"

    # The urlconf used to power this content's views.
    urlconf = "cms.apps.pages.urls"

    # A fieldset definition. If blank, one will be generated.
    fieldsets = None

    page = models.OneToOneField(
        Page,
        primary_key = True,
        editable = False,
        related_name = "+",
    )

    def __unicode__(self):
        """Returns a unicode representation."""
        return u"Content for {0}.".format(self.page)

    class Meta:
        abstract = True<commit_msg>Adding in a fix for a stray import<commit_after>"""Core models used by the CMS."""

import datetime

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core import urlresolvers
from django.db import models
from django.db.models import Q

from cms.core import sitemaps
from cms.core.models.base import PageBase
from cms.core.models.managers import publication_manager
from cms.core.optimizations import cached_property
from cms.apps import historylinks
from cms.apps.pages.models.managers import PageManager
from cms.apps.pages.models.fields import PageField

class Page(PageBase):

    """A page within the site."""

    objects = PageManager()

    @classmethod
    def select_published(cls, queryset):
        """Selects only published pages."""
        queryset = super(Page, cls).select_published(queryset)
        now = datetime.datetime.now()
        queryset = queryset.filter(Q(publication_date=None) | Q(publication_date__lte=now))
        queryset = queryset.filter(Q(expiry_date=None) | Q(expiry_date__gt=now))
        return queryset

    # Base fields.

    def __init__(self, *args, **kwargs):
        """"Initializes the Page."""
        super(Page, self).__init__(*args, **kwargs)
        if self.id:
            self.__class__.objects.cache.put(self)

    # Hierarchy fields.

    parent = PageField(
        blank = True,
        null = True
    )

    order = models.PositiveIntegerField(editable=False)

    @cached_property
    @publication_manager.getter
    def children(self):
        """The children of this page."""
        return list(Page.objects.filter(parent=self).order_by("order").iterator())

    @property
    def navigation(self):
        """The sub-navigation of this page."""
        return [child for child in super(Page, self).navigation if child.in_navigation]

    # Publication fields.

    publication_date = models.DateTimeField(blank=True,
                                            null=True,
                                            help_text="The date that this page will appear on the website.  Leave this blank to immediately publish this page.")

    expiry_date = models.DateTimeField(blank=True,
                                       null=True,
                                       help_text="The date that this page will be removed from the website.  Leave this blank to never expire this page.")

    # Navigation fields.

    in_navigation = models.BooleanField("add to navigation",
                                        default=True,
                                        help_text="Uncheck this box to remove this content from the site navigation.")

    permalink = models.SlugField(blank=True,
                                 help_text="A unique identifier for this page.  This will be set by your design team in order to link to this page from any custom templates they write.")

    # Content fields.

    content_type = models.ForeignKey(
        ContentType,
        editable = False,
        help_text="The type of page content.",
    )

    @cached_property
    def content(self):
        """The associated content model for this page."""
        content_cls = ContentType.objects.get_for_id(self.content_type_id).model_class()
        content = content_cls._default_manager.get(page=self)
        content.page = self
        return content

    # Standard model methods.

    def get_absolute_url(self):
        """Generates the absolute url of the page."""
        if self.parent:
            return self.parent.get_absolute_url() + self.url_title + "/"
        return urlresolvers.get_script_prefix()

    def reverse(self, view_func, args, kwargs):
        """Performs a reverse URL lookup."""
        urlconf = ContentType.objects.get_for_id(self.content_type_id).model_class().urlconf
        return self.get_absolute_url() + urlresolvers.reverse(view_func, args=args, kwargs=kwargs, urlconf=urlconf, prefix="")

    def save(self, force_insert=False, force_update=False, using=None, user=None):
        """Saves the page."""
        super(Page, self).save(force_insert, force_update, using, user)
        self.__class__.objects.cache.put(self)

    def delete(self, using=None):
        """Deletes the page."""
        super(Page, self).delete(using)
        self.__class__.objects.cache.remove(self)

    class Meta:
        unique_together = (("parent", "url_title",),)
        ordering = ("order",)

historylinks.register(Page)

sitemaps.register(Page)

# Base content class.

def get_registered_content():
    """Returns a list of all registered content objects."""
    return [
        model for model in models.get_models()
        if issubclass(model, ContentBase) and not model._meta.abstract
    ]

class ContentBase(models.Model):

    """Base class for page content."""

    # This must be a 64 x 64 pixel image.
    icon = settings.STATIC_URL + "cms/img/content-types/content.png"

    # The heading that the admin places this content under.
    classifier = "content"

    # The urlconf used to power this content's views.
    urlconf = "cms.apps.pages.urls"

    # A fieldset definition. If blank, one will be generated.
    fieldsets = None

    page = models.OneToOneField(
        Page,
        primary_key = True,
        editable = False,
        related_name = "+",
    )

    def __unicode__(self):
        """Returns a unicode representation."""
        return u"Content for {0}.".format(self.page)

    class Meta:
        abstract = True<|endoftext|>


<commit_before>import argparse
import os
import logging
import collections
from .version import __version__
from .tylogger import logger
from .strings import Strings
from .translator import Translator
from tabulate import tabulate

def parent_parser():
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-v', '--verbose', action="store_true", dest="verbose", help="show more debugging information")
    parser.add_argument('--utf8', action="store_true", dest="utf8", help="use encoding UTF-8")
    return parser

def extant_file(x):
    if not os.path.exists(x):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: x does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(x))
    elif os.path.isdir(x):
        raise argparse.ArgumentTypeError("{0} is a directory".format(x))
    return x

def arg_parser():
    description = r"""
  _______     _____ _        _
 |__   __|   / ____| |      (_)
    | |_   _| (___ | |_ _ __ _ _ __   __ _ ___
    | | | | |\___ \| __| '__| | '_ \ / _` / __|
    | | |_| |____) | |_| |  | | | | | (_| \__ \
    |_|\__, |_____/ \__|_|  |_|_| |_|\__, |___/
        __/ |                         __/ |
       |___/                         |___/
    """
    parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=description)
    parser.add_argument('--version', action='version', version=__version__)
    subparsers = parser.add_subparsers(title='subcommands', dest='action')

    generate_parser = subparsers.add_parser('generate', parents=[parent_parser()],
                                            help='generate `.strings` file from source code files.')
    generate_parser.add_argument('files', metavar='file', nargs='+', help='source file .[mc]', type=extant_file)
    generate_parser.add_argument('-a', '--aliases', nargs='+', help='aliases')
    generate_parser.add_argument('-o', '--output', nargs='+', dest='destinations', help='place output files in dirs')

    translate_parser = subparsers.add_parser('translate', parents=[parent_parser()],
                                             help='using Baidu Translate Service to translate `.strings` file.')
    translate_parser.add_argument('source', help='source `.strings` file')
    translate_parser.add_argument('destination', help='destination, a file or directory')
    translate_parser.add_argument('--dst-lang', required=True, help='destination language')
    translate_parser.add_argument('-s', '--src-lang', help='source language')

    lint_parser = subparsers.add_parser('lint', parents=[parent_parser()],
                                        help='Validates a `.strings` file.')
    lint_parser.add_argument('file', help='`.strings` file', type=extant_file)

    diff_parser = subparsers.add_parser('diff', parents=[parent_parser()],
                                        help='Compare `.strings` files line by line.')
    diff_parser.add_argument('file1', type=extant_file)
    diff_parser.add_argument('file2', type=extant_file)
    diff_parser.add_argument('-k', '--only-key', action="store_true", help="Only keys are compared")
    return parser

def main(argv=None):
    parser = arg_parser()
    args = parser.parse_args(argv)

    if args.verbose:
        logger.setLevel(logging.DEBUG)

    if args.action == 'generate':
        generate(args=args)
    elif args.action == 'translate':
        translate(args=args)
    elif args.action == 'lint':
        lint(args=args)
    elif args.action == 'diff':
        diff(args=args)

    exit(0)

def generate(args):
    dsts = args.destinations if args.destinations else ["."]

    strings = Strings(aliases=args.aliases, encoding=args.utf8)
    for dst in dsts:
        strings.generate(files=args.files, dst=dst)
    logger.success('have fun!')

def translate(args):
    translator = Translator(args.source, lang=args.src_lang)
    translateds = translator.translate(args.destination, dst_lang=args.dst_lang)
    rows = []
    for k in translateds.keys():
        (s, d) = translateds[k]
        rows.append([k, s, d])
    if rows:
        logger.done('Finished')
        logger.info(tabulate(rows, headers=['Key', 'Value', 'Result']))
    logger.success('have fun!')

def lint(args):
    logger.process('Parsing Source Reference...')
    elems = Strings.parsing_elems(filename=args.file, encoding='utf8' if args.utf8 else None)
    logger.process('Check Duplicate Keys...')
    duplicates = []
    for item, count in collections.Counter([e[0] for e in elems]).items():
        if count > 1:
            duplicates.append((item, count))

    if duplicates:
        table = []
        table_file = []
        logger.info('Find the following:')
        for key, count in duplicates:
            table.append([key, count])
            for elem in elems:
                if elem[0] == key:
                    table_file.append([elem[2], elem[0], elem[1]])
        logger.info(tabulate(table, headers=['Key', 'Count'], showindex='always', tablefmt="orgtbl"))
        logger.debug('Detail:')
        logger.debug(tabulate(table_file, headers=['Line', 'Key', 'Value'], tablefmt="orgtbl"))
        logger.error('Duplicate Keys')
        exit(1)

    logger.success('lint success')

def diff(args):
    def __generator(elems1, elems2, only_key, prefix):
        for elem in elems1:
            if not next((e for e in elems2 if e[0] == elem[0] and (only_key or e[1] == elem[1])), False):
                if prefix == '+':
                    yield (prefix, elem[2], '', elem[0], elem[1])
                else:
                    yield (prefix, '', elem[2], elem[0], elem[1])

    encoding = 'utf8' if args.utf8 else None
    logger.process('Parsing File1 Reference...')
    file1_elems = Strings.parsing_elems(filename=args.file1, encoding=encoding)
    logger.process('Parsing File2 Reference...')
    file2_elems = Strings.parsing_elems(filename=args.file2, encoding=encoding)
    logger.process('Comparing...')

    diff_adds = list(__generator(file1_elems, file2_elems, args.only_key, '+'))
    diff_subs = list(__generator(file2_elems, file1_elems, args.only_key, '-'))
    diffs = diff_adds + diff_subs
    diffs.sort(key=lambda obj: obj[1] if obj[1] else obj[2])
    logger.diffs(diffs)

if __name__ == '__main__':
    main()<commit_msg>cli translate add "--appid" and "--secret"<commit_after>import argparse
import os
import logging
import collections
from .version import __version__
from .tylogger import logger
from .strings import Strings
from .translator import Translator
from tabulate import tabulate

def parent_parser():
    parser = argparse.ArgumentParser(add_help=False)
    parser.add_argument('-v', '--verbose', action="store_true", dest="verbose", help="show more debugging information")
    parser.add_argument('--utf8', action="store_true", dest="utf8", help="use encoding UTF-8")
    return parser

def extant_file(x):
    if not os.path.exists(x):
        # Argparse uses the ArgumentTypeError to give a rejection message like:
        # error: argument input: x does not exist
        raise argparse.ArgumentTypeError("{0} does not exist".format(x))
    elif os.path.isdir(x):
        raise argparse.ArgumentTypeError("{0} is a directory".format(x))
    return x

def arg_parser():
    description = r"""
  _______     _____ _        _
 |__   __|   / ____| |      (_)
    | |_   _| (___ | |_ _ __ _ _ __   __ _ ___
    | | | | |\___ \| __| '__| | '_ \ / _` / __|
    | | |_| |____) | |_| |  | | | | | (_| \__ \
    |_|\__, |_____/ \__|_|  |_|_| |_|\__, |___/
        __/ |                         __/ |
       |___/                         |___/
    """
    parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=description)
    parser.add_argument('--version', action='version', version=__version__)
    subparsers = parser.add_subparsers(title='subcommands', dest='action')

    generate_parser = subparsers.add_parser('generate', parents=[parent_parser()],
                                            help='generate `.strings` file from source code files.')
    generate_parser.add_argument('files', metavar='file', nargs='+', help='source file .[mc]', type=extant_file)
    generate_parser.add_argument('-a', '--aliases', nargs='+', help='aliases')
    generate_parser.add_argument('-o', '--output', nargs='+', dest='destinations', help='place output files in dirs')

    translate_parser = subparsers.add_parser('translate', parents=[parent_parser()],
                                             help='using Baidu Translate Service to translate `.strings` file.')
    translate_parser.add_argument('source', help='source `.strings` file')
    translate_parser.add_argument('destination', help='destination, a file or directory')
    translate_parser.add_argument('--appid', required=True, dest='app_id', help='Baidu Translation AppId. '
                                                               'http://api.fanyi.baidu.com/api/trans/product/apidoc')
    translate_parser.add_argument('--secret', required=True, dest='secret_key', help='Baidu Translation SecretKey. '
                                                               'http://api.fanyi.baidu.com/api/trans/product/apidoc')
    translate_parser.add_argument('--dst-lang', required=True, help='destination language')
    translate_parser.add_argument('-s', '--src-lang', help='source language')

    lint_parser = subparsers.add_parser('lint', parents=[parent_parser()],
                                        help='Validates a `.strings` file.')
    lint_parser.add_argument('file', help='`.strings` file', type=extant_file)

    diff_parser = subparsers.add_parser('diff', parents=[parent_parser()],
                                        help='Compare `.strings` files line by line.')
    diff_parser.add_argument('file1', type=extant_file)
    diff_parser.add_argument('file2', type=extant_file)
    diff_parser.add_argument('-k', '--only-key', action="store_true", help="Only keys are compared")
    return parser

def main(argv=None):
    parser = arg_parser()
    args = parser.parse_args(argv)

    if args.verbose:
        logger.setLevel(logging.DEBUG)

    if args.action == 'generate':
        generate(args=args)
    elif args.action == 'translate':
        translate(args=args)
    elif args.action == 'lint':
        lint(args=args)
    elif args.action == 'diff':
        diff(args=args)

    exit(0)

def generate(args):
    dsts = args.destinations if args.destinations else ["."]

    strings = Strings(aliases=args.aliases, encoding=args.utf8)
    for dst in dsts:
        strings.generate(files=args.files, dst=dst)
    logger.success('have fun!')

def translate(args):
    translator = Translator(args.source, app_id=args.app_id, secret_key=args.secret_key, lang=args.src_lang)
    translateds = translator.translate(args.destination, dst_lang=args.dst_lang)
    rows = []
    for k in translateds.keys():
        (s, d) = translateds[k]
        rows.append([k, s, d])
    if rows:
        logger.done('Finished')
        logger.info(tabulate(rows, headers=['Key', 'Value', 'Result']))
    logger.success('have fun!')

def lint(args):
    logger.process('Parsing Source Reference...')
    elems = Strings.parsing_elems(filename=args.file, encoding='utf8' if args.utf8 else None)
    logger.process('Check Duplicate Keys...')
    duplicates = []
    for item, count in collections.Counter([e[0] for e in elems]).items():
        if count > 1:
            duplicates.append((item, count))

    if duplicates:
        table = []
        table_file = []
        logger.info('Find the following:')
        for key, count in duplicates:
            table.append([key, count])
            for elem in elems:
                if elem[0] == key:
                    table_file.append([elem[2], elem[0], elem[1]])
        logger.info(tabulate(table, headers=['Key', 'Count'], showindex='always', tablefmt="orgtbl"))
        logger.debug('Detail:')
        logger.debug(tabulate(table_file, headers=['Line', 'Key', 'Value'], tablefmt="orgtbl"))
        logger.error('Duplicate Keys')
        exit(1)

    logger.success('lint success')

def diff(args):
    def __generator(elems1, elems2, only_key, prefix):
        for elem in elems1:
            if not next((e for e in elems2 if e[0] == elem[0] and (only_key or e[1] == elem[1])), False):
                if prefix == '+':
                    yield (prefix, elem[2], '', elem[0], elem[1])
                else:
                    yield (prefix, '', elem[2], elem[0], elem[1])

    encoding = 'utf8' if args.utf8 else None
    logger.process('Parsing File1 Reference...')
    file1_elems = Strings.parsing_elems(filename=args.file1, encoding=encoding)
    logger.process('Parsing File2 Reference...')
    file2_elems = Strings.parsing_elems(filename=args.file2, encoding=encoding)
    logger.process('Comparing...')

    diff_adds = list(__generator(file1_elems, file2_elems, args.only_key, '+'))
    diff_subs = list(__generator(file2_elems, file1_elems, args.only_key, '-'))
    diffs = diff_adds + diff_subs
    diffs.sort(key=lambda obj: obj[1] if obj[1] else obj[2])
    logger.diffs(diffs)

if __name__ == '__main__':
    main()<|endoftext|>


<commit_before>import matplotlib.pyplot as plt
import numpy as np
import os
from fiber_properties import ImageAnalysis, load_image_object, image_list

plt.rc('font', size=16, family='serif')
plt.rc('figure', figsize=[20, 12.36])
plt.rc('xtick', labelsize=16)
plt.rc('ytick', labelsize=16)
plt.rc('lines', lw=4)

NUM_IMAGES = 500
CAMS = ['in']
FOLDER = '../data/stability/2017-03-19 Stability Test/circular_200um/'
NF_METHOD = 'radius'
FF_METHOD = 'gaussian'
BIN_SIZE = 10

def plot_stability(data, cam):
    plt.figure()
    plt.title(cam + 'stability')
    plt.subplot(311)
    plt.plot(data.time, data.x_diff)
    plt.ylabel('X Centroid Shift [um]')
    plt.title('std: ' + str(np.std(data.x_diff)))

    plt.subplot(312)
    plt.plot(data.time, data.y_diff)
    plt.ylabel('Y Centroid Shift [um]')
    plt.title('std: ' + str(np.std(data.y_diff)))

    plt.subplot(313)
    plt.plot(data.time, data.diameter)
    plt.xlabel('Time [min]')
    plt.ylabel('Diameter Shift [um]')
    plt.title('std: ' + str(np.std(data.diameter)))

    plt.suptitle(cam + ' stability')

def plot_stability_binned(data, cam, bin_size):
    plot_stability(data, cam)

    time = data.time[:len(data.time)-bin_size]
    x_diff = []
    y_diff = []
    diameter = []
    for i in xrange(0, len(data.time)-bin_size):
        x_diff.append(np.array(data.x_diff[i:i+bin_size]).mean())
        y_diff.append(np.array(data.y_diff[i:i+bin_size]).mean())
        diameter.append(np.array(data.diameter[i:i+bin_size]).mean())

    plt.subplot(311)
    plt.plot(time, x_diff, c='red')
    plt.title('std: ' + str(np.array(x_diff).std()))

    plt.subplot(312)
    plt.plot(time, y_diff, c='red')
    plt.title('std: ' + str(np.array(y_diff).std()))

    plt.subplot(313)
    plt.plot(time, diameter, c='red')
    plt.title('std:  ' + str(np.array(diameter).std()))

class StabilityInfo(object):
    def __init__(self):
        self.centroid = []
        self.center = []
        self.x_diff = []
        self.y_diff  = []
        self.diameter = []
        self.time = []

if __name__ == "__main__":
    data = {}
    data['spot'] = StabilityInfo()

    for cam in CAMS:
        data[cam] = StabilityInfo()
        if cam == 'in' or cam == 'nf':
            method = NF_METHOD
        else:
            method = FF_METHOD

        for i in xrange(NUM_IMAGES):
            obj_file = cam + '_obj_' + str(i).zfill(3) + '.pkl'
            if obj_file not in os.listdir(FOLDER):
                print 'saving ' + cam + '_' + str(i)
                im_file = FOLDER + cam + '_' + str(i).zfill(3) + '.fit'
                ImageAnalysis(im_file, threshold=1000).save_object(FOLDER + obj_file)

            print 'loading ' + cam + '_' + str(i)
            obj = load_image_object(FOLDER + obj_file)
            data[cam].center.append(np.array(obj.get_fiber_center(method=method, units='microns')))
            data[cam].centroid.append(np.array(obj.get_fiber_centroid(method=method, units='microns')))
            data[cam].x_diff.append(data[cam].centroid[-1][1] - data[cam].center[-1][1])
            data[cam].y_diff.append(data[cam].centroid[-1][0] - data[cam].center[-1][0])
            # data[cam].x_diff.append(data[cam].center[-1][1])
            # data[cam].y_diff.append(data[cam].center[-1][0])
            data[cam].diameter.append(obj.get_fiber_diameter(method=method, units='microns'))
            data[cam].time.append(obj.get_image_info('date_time'))
            obj.save_object()
            if cam == 'in':
                data['spot'].center.append(np.array(obj.get_fiber_center(method='gaussian', units='microns')))
                data['spot'].centroid.append(np.array(obj.get_fiber_centroid(method='gaussian', units='microns')))
                data['spot'].x_diff.append(data['spot'].center[-1][1] - data[cam].center[-1][1])
                data['spot'].y_diff.append(data['spot'].center[-1][0] - data[cam].center[-1][0])
                # data['spot'].x_diff.append(data['spot'].center[i][1])
                # data['spot'].y_diff.append(data['spot'].center[i][0])
                data['spot'].diameter.append(obj.get_fiber_diameter(method='gaussian', units='microns'))
                data['spot'].time.append(obj.get_image_info('date_time'))
                obj.save_object()

    if 'in' in CAMS:
        CAMS += ['spot']

    for cam in CAMS:
        init_center = np.copy(data[cam].center[0])
        init_centroid = np.copy(data[cam].centroid[0])
        init_x_diff = np.copy(data[cam].x_diff[0])
        init_y_diff = np.copy(data[cam].y_diff[0])
        init_diameter = np.copy(data[cam].diameter[0])
        init_time = np.copy(data[cam].time[0])
        for i in xrange(NUM_IMAGES):
            data[cam].center[i] -= init_center
            data[cam].centroid[i] -= init_centroid
            data[cam].x_diff[i] -= init_x_diff
            data[cam].y_diff[i] -= init_y_diff
            data[cam].diameter[i] -= init_diameter
            data[cam].time[i] -= init_time
            data[cam].time[i] = data[cam].time[i].total_seconds() / 60.0
        plot_stability(data[cam], cam)
        plt.savefig(FOLDER + cam + '_stability.png')
        plot_stability_binned(data[cam], cam, BIN_SIZE)
        plt.savefig(FOLDER + cam + '_stability_binned.png')
<commit_msg>save to linux<commit_after>import matplotlib.pyplot as plt
import numpy as np
import os
from fiber_properties import ImageAnalysis, load_image_object, image_list

plt.rc('font', size=16, family='serif')
plt.rc('figure', figsize=[20, 12.36])
plt.rc('xtick', labelsize=16)
plt.rc('ytick', labelsize=16)
plt.rc('lines', lw=4)

NUM_IMAGES = 500
CAMS = ['nf']
FOLDER = '../data/stability/2017-03-19 Stability Test/circular_200um/'
NF_METHOD = 'radius'
FF_METHOD = 'gaussian'
BIN_SIZE = 10

def plot_stability(data, cam):
    plt.figure()
    plt.title(cam + 'stability')
    plt.subplot(311)
    plt.plot(data.time, data.x_diff)
    plt.ylabel('X Centroid Shift [um]')
    plt.title('std: ' + str(np.std(data.x_diff)))

    plt.subplot(312)
    plt.plot(data.time, data.y_diff)
    plt.ylabel('Y Centroid Shift [um]')
    plt.title('std: ' + str(np.std(data.y_diff)))

    plt.subplot(313)
    plt.plot(data.time, data.diameter)
    plt.xlabel('Time [min]')
    plt.ylabel('Diameter Shift [um]')
    plt.title('std: ' + str(np.std(data.diameter)))

    plt.suptitle(cam + ' stability')

def plot_stability_binned(data, cam, bin_size):
    plot_stability(data, cam)

    time = data.time[:len(data.time)-bin_size]
    x_diff = []
    y_diff = []
    diameter = []
    for i in xrange(0, len(data.time)-bin_size):
        x_diff.append(np.array(data.x_diff[i:i+bin_size]).mean())
        y_diff.append(np.array(data.y_diff[i:i+bin_size]).mean())
        diameter.append(np.array(data.diameter[i:i+bin_size]).mean())

    plt.subplot(311)
    plt.plot(time, x_diff, c='red')
    plt.title('std: ' + str(np.array(x_diff).std()))

    plt.subplot(312)
    plt.plot(time, y_diff, c='red')
    plt.title('std: ' + str(np.array(y_diff).std()))

    plt.subplot(313)
    plt.plot(time, diameter, c='red')
    plt.title('std:  ' + str(np.array(diameter).std()))

class StabilityInfo(object):
    def __init__(self):
        self.centroid = []
        self.center = []
        self.x_diff = []
        self.y_diff  = []
        self.diameter = []
        self.time = []

if __name__ == "__main__":
    data = {}
    data['spot'] = StabilityInfo()

    for cam in CAMS:
        data[cam] = StabilityInfo()
        if cam == 'in' or cam == 'nf':
            method = NF_METHOD
        else:
            method = FF_METHOD

        for i in xrange(NUM_IMAGES):
            obj_file = cam + '_obj_' + str(i).zfill(3) + '.pkl'
            if obj_file not in os.listdir(FOLDER):
                print 'saving ' + cam + '_' + str(i)
                im_file = FOLDER + cam + '_' + str(i).zfill(3) + '.fit'
                ImageAnalysis(im_file, threshold=1000).save_object(FOLDER + obj_file)

            print 'loading ' + cam + '_' + str(i)
            obj = load_image_object(FOLDER + obj_file)
            data[cam].center.append(np.array(obj.get_fiber_center(method=method, units='microns')))
            data[cam].centroid.append(np.array(obj.get_fiber_centroid(method=method, units='microns')))
            data[cam].x_diff.append(data[cam].centroid[-1][1] - data[cam].center[-1][1])
            data[cam].y_diff.append(data[cam].centroid[-1][0] - data[cam].center[-1][0])
            # data[cam].x_diff.append(data[cam].center[-1][1])
            # data[cam].y_diff.append(data[cam].center[-1][0])
            data[cam].diameter.append(obj.get_fiber_diameter(method=method, units='microns'))
            data[cam].time.append(obj.get_image_info('date_time'))
            obj.save_object()
            if cam == 'in':
                data['spot'].center.append(np.array(obj.get_fiber_center(method='gaussian', units='microns')))
                data['spot'].centroid.append(np.array(obj.get_fiber_centroid(method='gaussian', units='microns')))
                data['spot'].x_diff.append(data['spot'].center[-1][1] - data[cam].center[-1][1])
                data['spot'].y_diff.append(data['spot'].center[-1][0] - data[cam].center[-1][0])
                # data['spot'].x_diff.append(data['spot'].center[i][1])
                # data['spot'].y_diff.append(data['spot'].center[i][0])
                data['spot'].diameter.append(obj.get_fiber_diameter(method='gaussian', units='microns'))
                data['spot'].time.append(obj.get_image_info('date_time'))
                obj.save_object()

    if 'in' in CAMS:
        CAMS += ['spot']

    for cam in CAMS:
        init_center = np.copy(data[cam].center[0])
        init_centroid = np.copy(data[cam].centroid[0])
        init_x_diff = np.copy(data[cam].x_diff[0])
        init_y_diff = np.copy(data[cam].y_diff[0])
        init_diameter = np.copy(data[cam].diameter[0])
        init_time = np.copy(data[cam].time[0])
        for i in xrange(NUM_IMAGES):
            data[cam].center[i] -= init_center
            data[cam].centroid[i] -= init_centroid
            data[cam].x_diff[i] -= init_x_diff
            data[cam].y_diff[i] -= init_y_diff
            data[cam].diameter[i] -= init_diameter
            data[cam].time[i] -= init_time
            data[cam].time[i] = data[cam].time[i].total_seconds() / 60.0
        plot_stability(data[cam], cam)
        plt.savefig(FOLDER + cam + '_stability.png')
        plot_stability_binned(data[cam], cam, BIN_SIZE)
        plt.savefig(FOLDER + cam + '_stability_binned.png')
<|endoftext|>


<commit_before>from pytest import raises
import numpy as np
import timeseries.TimeSeries as TS
from timeseries.lazy import *
import collections

# Test data
ts0 = TS.TimeSeries([],[])
ts1 = TS.TimeSeries(range(0,4),range(1,5))
ts2 = TS.TimeSeries(range(0,4),[10,20,30,40,50])
ts3 = TS.TimeSeries([0,5,10], [1,2,3])
ts4 = TS.TimeSeries([2.5,7.5], [100, -100])

# Lab 07
def test_len():
    assert len(ts1) == 4

def test_getitem():
    assert ts1[0] == 1

def test_setitem():
    tmpTestSeries = TS.TimeSeries(range(0,4),range(1,5))
    tmpTestSeries[0]=5
    assert tmpTestSeries[0] == 5

# Lab 08 same as Lab 07

# Lab 10
def test_contains():
    assert (2 in ts1) == True

def test_values():
    assert np.array_equal(ts1.values(), range(1,5)) == True

def test_times():
    assert np.array_equal(ts1.times(), range(0,4)) == True

def test_items():
    assert ts1.items()==list((t,v) for t,v in zip(range(0,4), range(1,5)))

def test_interpolation():
    # Simple cases
    c = TS.TimeSeries([1.],[1.2])
    d = ts3.interpolate([1.])
    assert c == d
    assert ts3.interpolate(ts4.times()) == TS.TimeSeries([2.5,7.5], [1.5, 2.5])
    # Boundary conditions
    assert ts3.interpolate([-100,100]) == TS.TimeSeries([-100,100],[1,3])

@lazy
def check_length(a,b):
    return len(a)==len(b)

def test_check_length():
    thunk = check_length(TS.TimeSeries(range(0,4),range(1,5)).lazy, TS.TimeSeries(range(1,5),range(2,6)))

# Lab 11
def test_median():
    assert ts1.median() == 2.5
    with raises(ValueError):
         ts0.median()

# Lab 12
def test_itertimes():
    ti=ts1.itertimes()
    assert next(ti)==0

def test_itervalue():
    vi=ts1.itervalues()
    assert next(vi)==1

def test_iteritems():
    iti=ts1.iteritems()
    assert next(iti)==(0,1)

# Lab 15
def test_equal():
    tmpTestSeries = TS.TimeSeries(range(0,4),range(1,5))
    assert tmpTestSeries == ts1

def test_add():
    c = 100
    d = TS.TimeSeries([0,1,2], [1,2,3])
    assert ts1+ts2 == TS.TimeSeries(range(0,4),[11,22,33,44,55])
    assert ts1+c == TS.TimeSeries(range(0,4),[101,102,103,104,105])
    with raises(ValueError):
        ts1+d
    assert ts1+ts2 == ts2+ts1
    assert c+ts1 == ts1+c
    with raises(ValueError):
        d+ts1

def test_sub():
    a = TS.TimeSeries([0,5,10], [1,2,3])
    b = TS.TimeSeries([0,5,10], [10,20,30])
    c = 100
    d = TS.TimeSeries([0,1,2], [1,2,3])
    assert b-a == TS.TimeSeries([0,5,10],[9,18,27])
    assert a-c == TS.TimeSeries([0,5,10],[-99,-98,-97])
    with raises(ValueError):
        a-d

def test_neg():
    a = TS.TimeSeries([0,5,10], [1,2,3])
    b = TS.TimeSeries([0,5,10], [10,20,30])
    assert -a == TS.TimeSeries([0,5,10],[-1,-2,-3])

def test_pos():
    a = TS.TimeSeries([0,5,10], [-1,-2,3])
    assert +a == a

def test_mul():
    a = TS.TimeSeries([0,5,10], [1,2,3])
    b = TS.TimeSeries([0,5,10], [10,20,30])
    c = 100
    d = TS.TimeSeries([0,1,2], [1,2,3])
    assert a*b == TS.TimeSeries([0,5,10],[10,40,90])
    assert a*c == TS.TimeSeries([0,5,10],[100,200,300])
    with raises(ValueError):
        a*d
    assert a*b == b*a
    assert c*a == a*c
    with raises(ValueError):
        d*a

def test_abs():
    a = TS.TimeSeries([0,5], [3,4])
    assert abs(a)==5.

def test_bool():
    a = TS.TimeSeries([0],[0])
    assert abs(a)==False

def test_neg():
    a = TS.TimeSeries([0,5,10], [1,2,3])
    b = TS.TimeSeries([0,5,10], [10,20,30])
    assert -a == TS.TimeSeries([0,5,10],[-1,-2,-3])

def test_pos():
    a = TS.TimeSeries([0,5,10], [-1,-2,3])
    assert +a == a

# Lab 19
def test_std():
    assert ts1.std() == 1.1180339887498949
    with raises(ValueError):
        ts0.std()

def test_mean():
    assert ts1.mean() == 2.5
    with raises(ValueError):
        ts0.mean()
<commit_msg>Updated import in test_TimeSeries<commit_after>from pytest import raises
import numpy as np
from timeseries.TimeSeries import TimeSeries
from timeseries.lazy import *
import collections

# Test data
ts0 = TimeSeries([],[])
ts1 = TimeSeries(range(0,4),range(1,5))
ts2 = TimeSeries(range(0,4),[10,20,30,40,50])
ts3 = TimeSeries([0,5,10], [1,2,3])
ts4 = TimeSeries([2.5,7.5], [100, -100])

# Lab 07
def test_len():
    assert len(ts1) == 4

def test_getitem():
    assert ts1[0] == 1

def test_setitem():
    tmpTestSeries = TimeSeries(range(0,4),range(1,5))
    tmpTestSeries[0]=5
    assert tmpTestSeries[0] == 5

# Lab 08 same as Lab 07

# Lab 10
def test_contains():
    assert (2 in ts1) == True

def test_values():
    assert np.array_equal(ts1.values(), range(1,5)) == True

def test_times():
    assert np.array_equal(ts1.times(), range(0,4)) == True

def test_items():
    assert ts1.items()==list((t,v) for t,v in zip(range(0,4), range(1,5)))

def test_interpolation():
    # Simple cases
    c = TimeSeries([1.],[1.2])
    d = ts3.interpolate([1.])
    assert c == d
    assert ts3.interpolate(ts4.times()) == TimeSeries([2.5,7.5], [1.5, 2.5])
    # Boundary conditions
    assert ts3.interpolate([-100,100]) == TimeSeries([-100,100],[1,3])

@lazy
def check_length(a,b):
    return len(a)==len(b)

def test_check_length():
    thunk = check_length(TimeSeries(range(0,4),range(1,5)).lazy, TimeSeries(range(1,5),range(2,6)))

# Lab 11
def test_median():
    assert ts1.median() == 2.5
    with raises(ValueError):
         ts0.median()

# Lab 12
def test_itertimes():
    ti=ts1.itertimes()
    assert next(ti)==0

def test_itervalue():
    vi=ts1.itervalues()
    assert next(vi)==1

def test_iteritems():
    iti=ts1.iteritems()
    assert next(iti)==(0,1)

# Lab 15
def test_equal():
    tmpTestSeries = TimeSeries(range(0,4),range(1,5))
    assert tmpTestSeries == ts1

def test_add():
    c = 100
    d = TimeSeries([0,1,2], [1,2,3])
    assert ts1+ts2 == TimeSeries(range(0,4),[11,22,33,44,55])
    assert ts1+c == TimeSeries(range(0,4),[101,102,103,104,105])
    with raises(ValueError):
        ts1+d
    assert ts1+ts2 == ts2+ts1
    assert c+ts1 == ts1+c
    with raises(ValueError):
        d+ts1

def test_sub():
    a = TimeSeries([0,5,10], [1,2,3])
    b = TimeSeries([0,5,10], [10,20,30])
    c = 100
    d = TimeSeries([0,1,2], [1,2,3])
    assert b-a == TimeSeries([0,5,10],[9,18,27])
    assert a-c == TimeSeries([0,5,10],[-99,-98,-97])
    with raises(ValueError):
        a-d

def test_neg():
    a = TimeSeries([0,5,10], [1,2,3])
    b = TimeSeries([0,5,10], [10,20,30])
    assert -a == TimeSeries([0,5,10],[-1,-2,-3])

def test_pos():
    a = TimeSeries([0,5,10], [-1,-2,3])
    assert +a == a

def test_mul():
    a = TimeSeries([0,5,10], [1,2,3])
    b = TimeSeries([0,5,10], [10,20,30])
    c = 100
    d = TimeSeries([0,1,2], [1,2,3])
    assert a*b == TimeSeries([0,5,10],[10,40,90])
    assert a*c == TimeSeries([0,5,10],[100,200,300])
    with raises(ValueError):
        a*d
    assert a*b == b*a
    assert c*a == a*c
    with raises(ValueError):
        d*a

def test_abs():
    a = TimeSeries([0,5], [3,4])
    assert abs(a)==5.

def test_bool():
    a = TimeSeries([0],[0])
    assert abs(a)==False

def test_neg():
    a = TimeSeries([0,5,10], [1,2,3])
    b = TimeSeries([0,5,10], [10,20,30])
    assert -a == TimeSeries([0,5,10],[-1,-2,-3])

def test_pos():
    a = TimeSeries([0,5,10], [-1,-2,3])
    assert +a == a

# Lab 19
def test_std():
    assert ts1.std() == 1.1180339887498949
    with raises(ValueError):
        ts0.std()

def test_mean():
    assert ts1.mean() == 2.5
    with raises(ValueError):
        ts0.mean()
<|endoftext|>


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

"""
Views for disks module.
"""

import logging
from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import ugettext as _
from plinth.modules import disks as disks_module
from plinth.utils import format_lazy

logger = logging.getLogger(__name__)

def index(request):
    """Show connection list."""
    disks = disks_module.get_disks()
    root_device = disks_module.get_root_device(disks)
    expandable_root_size = disks_module.is_expandable(root_device)
    expandable_root_size = _format_bytes(expandable_root_size)

    warn_about_low_disk_space(request)

    return TemplateResponse(request, 'disks.html',
                            {'title': _('Disks'),
                             'disks': disks,
                             'expandable_root_size': expandable_root_size})

def expand(request):
    """Warn and expand the root partition."""
    disks = disks_module.get_disks()
    root_device = disks_module.get_root_device(disks)

    if request.method == 'POST':
        expand_partition(request, root_device)
        return redirect(reverse('disks:index'))

    expandable_root_size = disks_module.is_expandable(root_device)
    expandable_root_size = _format_bytes(expandable_root_size)
    return TemplateResponse(request, 'disks_expand.html',
                            {'title': _('Expand Root Partition'),
                             'expandable_root_size': expandable_root_size})

def expand_partition(request, device):
    """Expand the partition."""
    try:
        disks_module.expand_partition(device)
    except Exception as exception:
        messages.error(request, _('Error expanding partition: {exception}')
                       .format(exception=exception))
    else:
        messages.success(request, _('Partition expanded successfully.'))

def warn_about_low_disk_space(request):
    """Warn about insufficient space on root partition."""
    disks = disks_module.get_disks()
    list_root = [disk for disk in disks if disk['mountpoint'] == '/']
    perc_used = list_root[0]['percentage_used'] if list_root else -1
    size_str = list_root[0]['size'] if list_root else '-1'
    size_Bytes = _interpret_size_string(size_str)
    free_Bytes = size_Bytes * (100 - perc_used) / 100
    free_GiB = free_Bytes / 1024 ** 3
    free_str = _format_bytes(free_Bytes)

    if perc_used < 0 or free_GiB < 0:
        logger.exception('Error getting information about root partition.')
        return

    msg_str = format_lazy(
        _('Warning: Low space on system partition ({percent_used}% used, '
          '{free_space} free). Check the Disks Configuration to resolve '
          'this problem.'),
        percent_used=perc_used, free_space=free_str)

    if perc_used > 90 or free_GiB < 1:
        messages.error(request, msg_str)

    elif perc_used > 75 or free_GiB < 2:
        messages.warning(request, msg_str)

def _interpret_size_string(size_str):
    """Convert size string to number of bytes."""
    if size_str is None or not size_str:
        return -1

    if size_str[-1] in '-10123456789':
        return float(size_str[:-1])

    if size_str[-1] == 'K':
        return float(size_str[:-1]) * 1024

    if size_str[-1] == 'M':
        return float(size_str[:-1]) * 1024 ** 2

    if size_str[-1] == 'G':
        return float(size_str[:-1]) * 1024 ** 3

    if size_str[-1] == 'T':
        return float(size_str[:-1]) * 1024 ** 4

    return -1

def _format_bytes(size):
    """Return human readable disk size from bytes."""
    if not size:
        return size

    if size < 1024:
        return _('{disk_size:.1f} bytes').format(disk_size=size)

    if size < 1024 ** 2:
        size /= 1024
        return _('{disk_size:.1f} KiB').format(disk_size=size)

    if size < 1024 ** 3:
        size /= 1024 ** 2
        return _('{disk_size:.1f} MiB').format(disk_size=size)

    if size < 1024 ** 4:
        size /= 1024 ** 3
        return _('{disk_size:.1f} GiB').format(disk_size=size)

    size /= 1024 ** 4
    return _('{disk_size:.1f} TiB').format(disk_size=size)
<commit_msg>disks: warning about low space only for users in admin group<commit_after>#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Views for disks module.
"""

import logging
from django.contrib import messages
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import ugettext as _
from plinth.modules import disks as disks_module
from plinth.utils import format_lazy, is_user_admin

logger = logging.getLogger(__name__)

def index(request):
    """Show connection list."""
    disks = disks_module.get_disks()
    root_device = disks_module.get_root_device(disks)
    expandable_root_size = disks_module.is_expandable(root_device)
    expandable_root_size = _format_bytes(expandable_root_size)

    warn_about_low_disk_space(request)

    return TemplateResponse(request, 'disks.html',
                            {'title': _('Disks'),
                             'disks': disks,
                             'expandable_root_size': expandable_root_size})

def expand(request):
    """Warn and expand the root partition."""
    disks = disks_module.get_disks()
    root_device = disks_module.get_root_device(disks)

    if request.method == 'POST':
        expand_partition(request, root_device)
        return redirect(reverse('disks:index'))

    expandable_root_size = disks_module.is_expandable(root_device)
    expandable_root_size = _format_bytes(expandable_root_size)
    return TemplateResponse(request, 'disks_expand.html',
                            {'title': _('Expand Root Partition'),
                             'expandable_root_size': expandable_root_size})

def expand_partition(request, device):
    """Expand the partition."""
    try:
        disks_module.expand_partition(device)
    except Exception as exception:
        messages.error(request, _('Error expanding partition: {exception}')
                       .format(exception=exception))
    else:
        messages.success(request, _('Partition expanded successfully.'))

def warn_about_low_disk_space(request):
    """Warn about insufficient space on root partition."""
    if not is_user_admin(request, cached=False):
        return

    disks = disks_module.get_disks()
    list_root = [disk for disk in disks if disk['mountpoint'] == '/']
    perc_used = list_root[0]['percentage_used'] if list_root else -1
    size_str = list_root[0]['size'] if list_root else '-1'
    size_Bytes = _interpret_size_string(size_str)
    free_Bytes = size_Bytes * (100 - perc_used) / 100
    free_GiB = free_Bytes / 1024 ** 3
    free_str = _format_bytes(free_Bytes)

    if perc_used < 0 or free_GiB < 0:
        logger.exception('Error getting information about root partition.')
        return

    msg_str = format_lazy(
        _('Warning: Low space on system partition ({percent_used}% used, '
          '{free_space} free). Check the Disks Configuration to resolve '
          'this problem.'),
        percent_used=perc_used, free_space=free_str)

    if perc_used > 90 or free_GiB < 1:
        messages.error(request, msg_str)

    elif perc_used > 75 or free_GiB < 2:
        messages.warning(request, msg_str)

def _interpret_size_string(size_str):
    """Convert size string to number of bytes."""
    if size_str is None or not size_str:
        return -1

    if size_str[-1] in '-10123456789':
        return float(size_str[:-1])

    if size_str[-1] == 'K':
        return float(size_str[:-1]) * 1024

    if size_str[-1] == 'M':
        return float(size_str[:-1]) * 1024 ** 2

    if size_str[-1] == 'G':
        return float(size_str[:-1]) * 1024 ** 3

    if size_str[-1] == 'T':
        return float(size_str[:-1]) * 1024 ** 4

    return -1

def _format_bytes(size):
    """Return human readable disk size from bytes."""
    if not size:
        return size

    if size < 1024:
        return _('{disk_size:.1f} bytes').format(disk_size=size)

    if size < 1024 ** 2:
        size /= 1024
        return _('{disk_size:.1f} KiB').format(disk_size=size)

    if size < 1024 ** 3:
        size /= 1024 ** 2
        return _('{disk_size:.1f} MiB').format(disk_size=size)

    if size < 1024 ** 4:
        size /= 1024 ** 3
        return _('{disk_size:.1f} GiB').format(disk_size=size)

    size /= 1024 ** 4
    return _('{disk_size:.1f} TiB').format(disk_size=size)
<|endoftext|>


<commit_before>import sys
from tabulate import tabulate
import requests
from collections import OrderedDict

class metric_api:

    def __init__(self):
        self.from_date = None
        self.to_date = None
        self.period = None
        self.timetype = None
        self.metric = None
        self.host = None
        self.iaas = None
        self.userid = None

        self.set_default()
        self.load_server_info()

    def __str__(self):
        result = ""
        result += "from_date: %s\n" % self.from_date
        result += "to_date:   %s\n" % self.to_date
        result += "period:    %s\n" % self.period
        result += "timetype:    %s\n" % self.timetype
        result += "metric:    %s\n" % self.metric
        result += "host:   %s\n" % self.host
        result += "iaas:      %s\n" % self.iaas
        result += "userid:      %s\n" % self.userid
        return result

    def set_default(self):
        self.timetype = "hour"
        self.metric = "vmcount"

    def load_server_info(self):
        # cloudmesh_server.yaml contains the api server info
        # this is only for test.
        self.api_server = "156.56.93.202"
        self.port = 5001
        self.doc_url = "metric"

    def connect(self):
        try:
            r = requests.get(self.get_uri())
            return r.json()
        except requests.ConnectionError:
            print "Connection failed to %s" % self.get_uri()
        except requests.HTTPError as e:
            print "Invalid HTTP response ({0},{1})".format(e.errno, e.strerror)
        except requests.Timeout:
            print "HTTP times out"
        except requests.TooManyRedirects:
            print "Exceeds the configured number of maximum redirections"
        except:
            print "Unexpected error:", sys.exc_info()[0]
            raise

    def get_uri(self):
        # /metric/<cloudname>/<hostname>/<username>/<metric>/<timestart>/<timeend>/<period>
        uri = ["http:/"]
        uri.append(self.api_server + ":" + str(self.port))
        uri.append(self.doc_url)
        uri.append(self.iaas)
        uri.append(self.host)
        uri.append(self.userid)
        uri.append(self.metric)
        uri.append(self.from_date)
        uri.append(self.to_date)
        uri.append(self.period)
        self.uri = "/".join(map(str,uri))
        return self.uri

    def set_date(self, from_date, to_date):
        self.from_date = from_date
        self.to_date = to_date

    def set_period(self, period):
        # NOT IMPLEMENTED MESSAGE
        if period:
            self.na_message(sys._getframe().f_code.co_name, period)

        self.period = period

    def set_metric(self, metric):
        if metric:
            self.metric = metric

    def set_host(self, host):
        self.host = host

    def set_iaas(self, cloud):
        self.iaas = cloud

    def set_cloud(self, cloud):
        ''' link to set_iaas '''
        self.set_iaas(cloud)

    def set_user(self, userid):
        self.userid = userid

    def test_raw_data(self):
        # for test, dummy data is returned
        res = [["HOST", "PROJECT", "cpu", "memory_mb", "disk_gb"],
               ["india","(total)",2, 4003,157],
               ["india","(used_now)", 3, 5120, 40],
               ["india","(used_max)",3,4608, 40],
               ["india","b70d90d65e464582b6b2161cf3603ced",1,512,0],
               ["india","66265572db174a7aa66eba661f58eb9e",2,4096,40]]
        return res

    def get_raw_data(self):
        # expect list variable
        res = self.connect()
        # res is dict
        dictlist = []
        i = 0
        for row in res["message"]:
            if i == 0:
                dictlist.append(row.keys())
            i=1
            dictlist.append(row.values())
        self.raw_data = dictlist

        self.comparison_table(res)

    def translate_naming(self, name):
        name = name.upper()

        if name in ["WALLTIME", "WALLCLOCK", "RUNTIME"]:
            timetype = self.translate_naming(self.timetype)
            return "WallTime ("+timetype+")"
        elif name == "HOUR":
            return "Hrs"
        else:
            return name

    def comparison_table(self, res):
        # Let's provide comparison table if there is no search option defined
        if self.iaas or self.host:
            return

        # New table for the comparison of IaaS
        # ====================================
        distlist_comparison = []
        index = "YEAR MONTH"
        column = "CLOUDNAME"
        value = self.translate_naming(name = self.metric)#"VMCOUNT")

        iaas = []
        dates = []
        for row in res["message"]:
            try:
                iaas.index(row[column])
            except ValueError:
                iaas.append(row[column])
            try:
                dates.index(row[index])
            except ValueError:
                dates.append(row[index])

        # header for the new table
        distlist_comparison.append(["DATE"] + iaas)
        complist = OrderedDict()
        for row in res["message"]:
            # date | openstack | eucalyptus | nimbus
            pos = iaas.index(row[column])
            try:
                complist[row[index]][pos] = row[value]
            except KeyError:
                complist[row[index]] = [0]*len(iaas)
                complist[row[index]][pos] = row[value]

        for k, v in complist.iteritems():
            distlist_comparison.append([k]+v)

        self.raw_data = distlist_comparison

    def display(self, table_format="orgtbl"):

        # display uri
        print "uri: " + self.uri

        # display search options
        print "metric: " + self.metric
        if self.iaas:
            print "iaas: " + self.iaas
        if self.host:
            print "host: " + self.host
        if self.userid:
            print "userid: " + self.userid

        # display table of contents
        # table_format =
        # plain,
        # simple,
        # grid,
        # pipe,
        # orgtbl,
        # rst,
        # mediawiki,
        # latex
        self.table = tabulate (self.raw_data, headers="firstrow",
                               tablefmt=table_format)
        try:
            seperator = self.table.split("\n")[1].replace("|", "+")
            print seperator
            print self.table
            print seperator
        except IndexError:
            print "No results."

    def get_stats(self):
        #print vars(self)
        self.get_raw_data()
        self.display()
        return

    def stats(self):
        ''' link to get_stats '''
        return self.get_stats()

    def na_message(self, func_name, *args):
        args = map(str,args)
        msg = "%s is not implemented, '%s' ignored." % \
                ( func_name, ', '.join(args))
        print "=" * len(msg)
        print msg
        print "=" * len(msg)
        print

<commit_msg>test ip for metric<commit_after>import sys
from tabulate import tabulate
import requests
from collections import OrderedDict

class metric_api:

    def __init__(self):
        self.from_date = None
        self.to_date = None
        self.period = None
        self.timetype = None
        self.metric = None
        self.host = None
        self.iaas = None
        self.userid = None

        self.set_default()
        self.load_server_info()

    def __str__(self):
        result = ""
        result += "from_date: %s\n" % self.from_date
        result += "to_date:   %s\n" % self.to_date
        result += "period:    %s\n" % self.period
        result += "timetype:    %s\n" % self.timetype
        result += "metric:    %s\n" % self.metric
        result += "host:   %s\n" % self.host
        result += "iaas:      %s\n" % self.iaas
        result += "userid:      %s\n" % self.userid
        return result

    def set_default(self):
        self.timetype = "hour"
        self.metric = "vmcount"

    def load_server_info(self):
        # cloudmesh_server.yaml contains the api server info
        # this is only for test.
        self.api_server = "129.79.135.80"
        self.port = 5001
        self.doc_url = "metric"

    def connect(self):
        try:
            r = requests.get(self.get_uri())
            return r.json()
        except requests.ConnectionError:
            print "Connection failed to %s" % self.get_uri()
        except requests.HTTPError as e:
            print "Invalid HTTP response ({0},{1})".format(e.errno, e.strerror)
        except requests.Timeout:
            print "HTTP times out"
        except requests.TooManyRedirects:
            print "Exceeds the configured number of maximum redirections"
        except:
            print "Unexpected error:", sys.exc_info()[0]
            raise

    def get_uri(self):
        # /metric/<cloudname>/<hostname>/<username>/<metric>/<timestart>/<timeend>/<period>
        uri = ["http:/"]
        uri.append(self.api_server + ":" + str(self.port))
        uri.append(self.doc_url)
        uri.append(self.iaas)
        uri.append(self.host)
        uri.append(self.userid)
        uri.append(self.metric)
        uri.append(self.from_date)
        uri.append(self.to_date)
        uri.append(self.period)
        self.uri = "/".join(map(str,uri))
        return self.uri

    def set_date(self, from_date, to_date):
        self.from_date = from_date
        self.to_date = to_date

    def set_period(self, period):
        # NOT IMPLEMENTED MESSAGE
        if period:
            self.na_message(sys._getframe().f_code.co_name, period)

        self.period = period

    def set_metric(self, metric):
        if metric:
            self.metric = metric

    def set_host(self, host):
        self.host = host

    def set_iaas(self, cloud):
        self.iaas = cloud

    def set_cloud(self, cloud):
        ''' link to set_iaas '''
        self.set_iaas(cloud)

    def set_user(self, userid):
        self.userid = userid

    def test_raw_data(self):
        # for test, dummy data is returned
        res = [["HOST", "PROJECT", "cpu", "memory_mb", "disk_gb"],
               ["india","(total)",2, 4003,157],
               ["india","(used_now)", 3, 5120, 40],
               ["india","(used_max)",3,4608, 40],
               ["india","b70d90d65e464582b6b2161cf3603ced",1,512,0],
               ["india","66265572db174a7aa66eba661f58eb9e",2,4096,40]]
        return res

    def get_raw_data(self):
        # expect list variable
        res = self.connect()
        # res is dict
        dictlist = []
        i = 0
        for row in res["message"]:
            if i == 0:
                dictlist.append(row.keys())
            i=1
            dictlist.append(row.values())
        self.raw_data = dictlist

        self.comparison_table(res)

    def translate_naming(self, name):
        name = name.upper()

        if name in ["WALLTIME", "WALLCLOCK", "RUNTIME"]:
            timetype = self.translate_naming(self.timetype)
            return "WallTime ("+timetype+")"
        elif name == "HOUR":
            return "Hrs"
        else:
            return name

    def comparison_table(self, res):
        # Let's provide comparison table if there is no search option defined
        if self.iaas or self.host:
            return

        # New table for the comparison of IaaS
        # ====================================
        distlist_comparison = []
        index = "YEAR MONTH"
        column = "CLOUDNAME"
        value = self.translate_naming(name = self.metric)#"VMCOUNT")

        iaas = []
        dates = []
        for row in res["message"]:
            try:
                iaas.index(row[column])
            except ValueError:
                iaas.append(row[column])
            try:
                dates.index(row[index])
            except ValueError:
                dates.append(row[index])

        # header for the new table
        distlist_comparison.append(["DATE"] + iaas)
        complist = OrderedDict()
        for row in res["message"]:
            # date | openstack | eucalyptus | nimbus
            pos = iaas.index(row[column])
            try:
                complist[row[index]][pos] = row[value]
            except KeyError:
                complist[row[index]] = [0]*len(iaas)
                complist[row[index]][pos] = row[value]

        for k, v in complist.iteritems():
            distlist_comparison.append([k]+v)

        self.raw_data = distlist_comparison

    def display(self, table_format="orgtbl"):

        # display uri
        print "uri: " + self.uri

        # display search options
        print "metric: " + self.metric
        if self.iaas:
            print "iaas: " + self.iaas
        if self.host:
            print "host: " + self.host
        if self.userid:
            print "userid: " + self.userid

        # display table of contents
        # table_format =
        # plain,
        # simple,
        # grid,
        # pipe,
        # orgtbl,
        # rst,
        # mediawiki,
        # latex
        self.table = tabulate (self.raw_data, headers="firstrow",
                               tablefmt=table_format)
        try:
            seperator = self.table.split("\n")[1].replace("|", "+")
            print seperator
            print self.table
            print seperator
        except IndexError:
            print "No results."

    def get_stats(self):
        #print vars(self)
        self.get_raw_data()
        self.display()
        return

    def stats(self):
        ''' link to get_stats '''
        return self.get_stats()

    def na_message(self, func_name, *args):
        args = map(str,args)
        msg = "%s is not implemented, '%s' ignored." % \
                ( func_name, ', '.join(args))
        print "=" * len(msg)
        print msg
        print "=" * len(msg)
        print

<|endoftext|>


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

import sys
import os
import random
import time
import yaml

from twisted.internet import defer, reactor, task
from twisted.application import app
from twisted.python import usage, failure
from twisted.python.util import spewer

from ooni import nettest, runner, reporter, config

from ooni.inputunit import InputUnitFactory

from ooni.utils import net
from ooni.utils import checkForRoot, NotRootError
from ooni.utils import log

class Options(usage.Options):
    synopsis = """%s [options] [path to test].py
    """ % (os.path.basename(sys.argv[0]),)

    longdesc = ("ooniprobe loads and executes a suite or a set of suites of"
                " network tests. These are loaded from modules, packages and"
                " files listed on the command line")

    optFlags = [["help", "h"],
                ["resume", "r"]]

    optParameters = [["reportfile", "o", None, "report file name"],
                     ["testdeck", "i", None,
                         "Specify as input a test deck: a yaml file containig the tests to run an their arguments"],
                     ["collector", "c", None,
                         "Address of the collector of test results. (example: http://127.0.0.1:8888)"],
                     ["logfile", "l", None, "log file name"],
                     ["pcapfile", "p", None, "pcap file name"]]

    compData = usage.Completions(
        extraActions=[usage.CompleteFiles(
                "*.py", descr="file | module | package | TestCase | testMethod",
                repeat=True)],)

    tracer = None

    def __init__(self):
        self['test'] = None
        usage.Options.__init__(self)

    def opt_asciilulz(self):
        from ooni.utils import logo
        print logo.getlogo()

    def opt_spew(self):
        """
        Print an insanely verbose log of everything that happens.  Useful
        when debugging freezes or locks in complex code.
        """
        sys.settrace(spewer)

    def parseArgs(self, *args):
        if self['testdeck']:
            return
        try:
            self['test'] = args[0]
            self['subargs'] = args[1:]
        except:
            raise usage.UsageError("No test filename specified!")

def updateStatusBar():
    for test_filename in config.state.keys():
        # The ETA is not updated so we we will not print it out for the
        # moment.
        eta = config.state[test_filename].eta()
        progress = config.state[test_filename].progress()
        progress_bar_frmt = "[%s] %s%%" % (test_filename, progress)
        print progress_bar_frmt

def testsEnded(*arg, **kw):
    """
    You can place here all the post shutdown tasks.
    """
    log.debug("testsEnded: Finished running all tests")
    config.start_reactor = False
    try: reactor.stop()
    except: pass

def startSniffing():
    from ooni.utils.txscapy import ScapyFactory, ScapySniffer
    try:
        checkForRoot()
    except NotRootError:
        print "[!] Includepcap options requires root priviledges to run"
        print "    you should run ooniprobe as root or disable the options in ooniprobe.conf"
        sys.exit(1)

    print "Starting sniffer"
    config.scapyFactory = ScapyFactory(config.advanced.interface)

    sniffer = ScapySniffer(config.reports.pcap)
    config.scapyFactory.registerProtocol(sniffer)

def runTestList(none, test_list):
    """
    none: is always None.

    test_list (list): a list of tuples containing (test_cases, options,
        cmd_line_options)
    """
    deck_dl = []

    for test in test_list:
        test_cases, options, cmd_line_options = test
        d1 = runner.runTestCases(test_cases, options, cmd_line_options)
        deck_dl.append(d1)

    d2 = defer.DeferredList(deck_dl)
    d2.addBoth(testsEnded)

    # Print every 5 second the list of current tests running
    l = task.LoopingCall(updateStatusBar)
    l.start(5.0)
    return d2

def errorRunningTests(failure):
    failure.printTraceback()

def run():
    """
    Parses command line arguments of test.
    """
    cmd_line_options = Options()
    if len(sys.argv) == 1:
        cmd_line_options.getUsage()
    try:
        cmd_line_options.parseOptions()
    except usage.UsageError, ue:
        raise SystemExit, "%s: %s" % (sys.argv[0], ue)

    log.start(cmd_line_options['logfile'])

    if config.privacy.includepcap:
        log.msg("Starting")
        runner.startSniffing()

    resume = cmd_line_options['resume']

    # contains (test_cases, options, cmd_line_options)
    test_list = []

    if cmd_line_options['testdeck']:
        test_deck = yaml.safe_load(open(cmd_line_options['testdeck']))
        for test in test_deck:
            del cmd_line_options
            cmd_line_options = test['options']
            if resume:
                cmd_line_options['resume'] = True
            else:
                cmd_line_options['resume'] = False
            test_list.append(runner.loadTest(cmd_line_options))
    else:
        log.msg("No test deck detected")
        del cmd_line_options['testdeck']
        test_list.append(runner.loadTest(cmd_line_options))

    if config.advanced.start_tor:
        log.msg("Starting Tor...")
        d = runner.startTor()
        d.addCallback(runTestList, test_list)
        d.addErrback(errorRunningTests)
    else:
        # We need to pass None as first argument because when the callback is
        # fired it will pass it's result to runTestCase.
        d = runTestList(None, test_list)
        d.addErrback(errorRunningTests)

    reactor.run()
<commit_msg>Removes duplicated startSniffer code<commit_after>#-*- coding: utf-8 -*-

import sys
import os
import random
import time
import yaml

from twisted.internet import defer, reactor, task
from twisted.application import app
from twisted.python import usage, failure
from twisted.python.util import spewer

from ooni import nettest, runner, reporter, config

from ooni.inputunit import InputUnitFactory

from ooni.utils import net
from ooni.utils import checkForRoot, NotRootError
from ooni.utils import log

class Options(usage.Options):
    synopsis = """%s [options] [path to test].py
    """ % (os.path.basename(sys.argv[0]),)

    longdesc = ("ooniprobe loads and executes a suite or a set of suites of"
                " network tests. These are loaded from modules, packages and"
                " files listed on the command line")

    optFlags = [["help", "h"],
                ["resume", "r"]]

    optParameters = [["reportfile", "o", None, "report file name"],
                     ["testdeck", "i", None,
                         "Specify as input a test deck: a yaml file containig the tests to run an their arguments"],
                     ["collector", "c", None,
                         "Address of the collector of test results. (example: http://127.0.0.1:8888)"],
                     ["logfile", "l", None, "log file name"],
                     ["pcapfile", "p", None, "pcap file name"]]

    compData = usage.Completions(
        extraActions=[usage.CompleteFiles(
                "*.py", descr="file | module | package | TestCase | testMethod",
                repeat=True)],)

    tracer = None

    def __init__(self):
        self['test'] = None
        usage.Options.__init__(self)

    def opt_asciilulz(self):
        from ooni.utils import logo
        print logo.getlogo()

    def opt_spew(self):
        """
        Print an insanely verbose log of everything that happens.  Useful
        when debugging freezes or locks in complex code.
        """
        sys.settrace(spewer)

    def parseArgs(self, *args):
        if self['testdeck']:
            return
        try:
            self['test'] = args[0]
            self['subargs'] = args[1:]
        except:
            raise usage.UsageError("No test filename specified!")

def updateStatusBar():
    for test_filename in config.state.keys():
        # The ETA is not updated so we we will not print it out for the
        # moment.
        eta = config.state[test_filename].eta()
        progress = config.state[test_filename].progress()
        progress_bar_frmt = "[%s] %s%%" % (test_filename, progress)
        print progress_bar_frmt

def testsEnded(*arg, **kw):
    """
    You can place here all the post shutdown tasks.
    """
    log.debug("testsEnded: Finished running all tests")
    config.start_reactor = False
    try: reactor.stop()
    except: pass

def runTestList(none, test_list):
    """
    none: is always None.

    test_list (list): a list of tuples containing (test_cases, options,
        cmd_line_options)
    """
    deck_dl = []

    for test in test_list:
        test_cases, options, cmd_line_options = test
        d1 = runner.runTestCases(test_cases, options, cmd_line_options)
        deck_dl.append(d1)

    d2 = defer.DeferredList(deck_dl)
    d2.addBoth(testsEnded)

    # Print every 5 second the list of current tests running
    l = task.LoopingCall(updateStatusBar)
    l.start(5.0)
    return d2

def errorRunningTests(failure):
    failure.printTraceback()

def run():
    """
    Parses command line arguments of test.
    """
    cmd_line_options = Options()
    if len(sys.argv) == 1:
        cmd_line_options.getUsage()
    try:
        cmd_line_options.parseOptions()
    except usage.UsageError, ue:
        raise SystemExit, "%s: %s" % (sys.argv[0], ue)

    log.start(cmd_line_options['logfile'])

    if config.privacy.includepcap:
        log.msg("Starting")
        runner.startSniffing()

    resume = cmd_line_options['resume']

    # contains (test_cases, options, cmd_line_options)
    test_list = []

    if cmd_line_options['testdeck']:
        test_deck = yaml.safe_load(open(cmd_line_options['testdeck']))
        for test in test_deck:
            del cmd_line_options
            cmd_line_options = test['options']
            if resume:
                cmd_line_options['resume'] = True
            else:
                cmd_line_options['resume'] = False
            test_list.append(runner.loadTest(cmd_line_options))
    else:
        log.msg("No test deck detected")
        del cmd_line_options['testdeck']
        test_list.append(runner.loadTest(cmd_line_options))

    if config.advanced.start_tor:
        log.msg("Starting Tor...")
        d = runner.startTor()
        d.addCallback(runTestList, test_list)
        d.addErrback(errorRunningTests)
    else:
        # We need to pass None as first argument because when the callback is
        # fired it will pass it's result to runTestCase.
        d = runTestList(None, test_list)
        d.addErrback(errorRunningTests)

    reactor.run()
<|endoftext|>


<commit_before>#!/usr/bin/env python
'''whatlastgenre version'''

__version__ = '0.1.24'
<commit_msg>version bump to 0.1.25<commit_after>#!/usr/bin/env python
'''whatlastgenre version'''

__version__ = '0.1.25'
<|endoftext|>


<commit_before>from statblock.character import Character
from statblock.character import Size
from statblock.weapon import Longsword
from statblock.dice import d4, d8
from statblock.feat import WeaponFocus
from statblock.weapon import Dagger
from statblock.ability import Strength

import py
import os.path

def test_strength_repr():
    s = Strength(12)
    assert str(s) == "Strength: 12"

def test_strength_affected():
    guard = Character()

    guard.abilities.strength = 16
    assert guard.abilities.strength.value == 16
    assert guard.attack.melee.value == 3

    guard.abilities.strength.initial -= 4
    assert guard.abilities.strength.value == 12
    assert guard.attack.melee.value == 1

def test_dexterity_affected():
    guard = Character()
    guard.abilities.dexterity = 8
    assert guard.armor_class.value == 9
    assert guard.attack.ranged.value == -1
    assert guard.saving_throws.reflex.value == -1

def test_constitution_affected():
    guard = Character()
    guard.abilities.constitution = 13
    assert guard.hit_points.value == 9
    assert guard.saving_throws.fortitude.value == 1

    guard.abilities.constitution = 5
    assert guard.hit_points.value == 5

def test_wisdom_affected():
    guard = Character()
    guard.abilities.wisdom = 14
    assert guard.saving_throws.will.value == +2

def test_base_attack():
    guard = Character()
    guard.attack.base.value += 1
    guard.abilities.strength = 15
    guard.abilities.dexterity = 12

    assert guard.attack.melee.value == 3
    assert guard.attack.ranged.value == 2

def test_size_effects():
    guard = Character()
    guard.size = Size.LARGE
    assert guard.attack.base.value == -1
    assert guard.armor_class.value == 9

    guard.size = Size.MEDIUM
    assert guard.attack.base.value == 0
    assert guard.armor_class.value == 10

def test_adding_a_weapon():
    guard = Character()
    guard.abilities.strength.value = 16
    guard.abilities.dexterity.value = 13
    guard.registry.actions.clear()

    sword = Longsword()
    dagger = Dagger()
    guard.weapons.add(sword)
    guard.weapons.add(dagger)
    guard.add(WeaponFocus())

    assert sword.melee.attack.value == 4
    assert sword.melee.damage.default == d8
    assert sword.melee.damage.get_combined() == d8+3
    assert sword.is_melee()
    assert not sword.is_ranged()

    assert dagger.melee.attack.value == 3
    assert dagger.is_ranged()
    assert dagger.ranged.attack.value == 1
    assert dagger.ranged.damage.get_combined() == d4

if __name__ == '__main__':
    py.cmdline.pytest(["-s", os.path.basename(__file__)])#, "-k", "sword"])<commit_msg>Look up test file to from file's basename<commit_after>from statblock.character import Character
from statblock.character import Size
from statblock.weapon import Longsword
from statblock.dice import d4, d8
from statblock.feat import WeaponFocus
from statblock.weapon import Dagger
from statblock.ability import Strength

import py
import os.path

def test_strength_repr():
    s = Strength(12)
    assert str(s) == "Strength: 12"

def test_strength_affected():
    guard = Character()

    guard.abilities.strength = 16
    assert guard.abilities.strength.value == 16
    assert guard.attack.melee.value == 3

    guard.abilities.strength.initial -= 4
    assert guard.abilities.strength.value == 12
    assert guard.attack.melee.value == 1

def test_dexterity_affected():
    guard = Character()
    guard.abilities.dexterity = 8
    assert guard.armor_class.value == 9
    assert guard.attack.ranged.value == -1
    assert guard.saving_throws.reflex.value == -1

def test_constitution_affected():
    guard = Character()
    guard.abilities.constitution = 13
    assert guard.hit_points.value == 9
    assert guard.saving_throws.fortitude.value == 1

    guard.abilities.constitution = 5
    assert guard.hit_points.value == 5

def test_wisdom_affected():
    guard = Character()
    guard.abilities.wisdom = 14
    assert guard.saving_throws.will.value == +2

def test_base_attack():
    guard = Character()
    guard.attack.base.value += 1
    guard.abilities.strength = 15
    guard.abilities.dexterity = 12

    assert guard.attack.melee.value == 3
    assert guard.attack.ranged.value == 2

def test_size_effects():
    guard = Character()
    guard.size = Size.LARGE
    assert guard.attack.base.value == -1
    assert guard.armor_class.value == 9

    guard.size = Size.MEDIUM
    assert guard.attack.base.value == 0
    assert guard.armor_class.value == 10

def test_adding_a_weapon():
    guard = Character()
    guard.abilities.strength.value = 16
    guard.abilities.dexterity.value = 13
    guard.registry.actions.clear()

    sword = Longsword()
    dagger = Dagger()
    guard.weapons.add(sword)
    guard.weapons.add(dagger)
    guard.add(WeaponFocus())

    assert sword.melee.attack.value == 4
    assert sword.melee.damage.default == d8
    assert sword.melee.damage.get_combined() == d8+3
    assert sword.is_melee()
    assert not sword.is_ranged()

    assert dagger.melee.attack.value == 3
    assert dagger.is_ranged()
    assert dagger.ranged.attack.value == 1
    assert dagger.ranged.damage.get_combined() == d4

if __name__ == '__main__':
    py.cmdline.pytest(["-s", os.path.basename(__file__)])<|endoftext|>


<commit_before># coding=utf-8

"""
Collect stats via MX4J from Kafka

#### Dependencies

 * urllib2
 * xml.etree
"""
import urllib2

from urllib import urlencode

try:
    from xml.etree import ElementTree
except ImportError:
    ElementTree = None

try:
    from ElementTree import ParseError as ETParseError
except ImportError:
    ETParseError = Exception

import diamond.collector

class KafkaCollector(diamond.collector.Collector):
    ATTRIBUTE_TYPES = {
        'double': float,
        'float': float,
        'int': int,
        'long': long,
    }

    def get_default_config_help(self):
        config_help = super(KafkaCollector, self).get_default_config_help()
        config_help.update({
            'host': "",
            'port': "",
        })
        return config_help

    def get_default_config(self):
        """
        Returns the default collector settings
        """
        config = super(KafkaCollector, self).get_default_config()
        config.update({
            'host': '127.0.0.1',
            'port': 8082,
            'path': 'kafka',
        })
        return config

    def _get(self, path, query_args=None):
        if not path.startswith('/'):
            path = '/' + path

        qargs = {'template': 'identity'}

        if query_args:
            qargs.update(query_args)

        url = 'http://%s:%i%s?%s' % (
            self.config['host'], int(self.config['port']),
            path, urlencode(qargs))

        try:
            response = urllib2.urlopen(url)
        except urllib2.URLError, err:
            self.log.error("%s: %s", url, err)
            return None

        try:
            return ElementTree.fromstring(response.read())
        except ETParseError:
            self.log.error("Unable to parse response from mx4j")
            return None

    def get_mbeans(self, pattern):
        query_args = {'querynames': pattern}

        mbeans = self._get('/serverbydomain', query_args)
        if mbeans is None:
            return

        found_beans = set()

        for mbean in mbeans.getiterator(tag='MBean'):
            objectname = mbean.get('objectname')

            if objectname:
                found_beans.add(objectname)

        return found_beans

    def query_mbean(self, objectname, key_prefix=None):
        query_args = {
            'objectname': objectname,
            'operations': False,
            'constructors': False,
            'notifications': False,
        }

        attributes = self._get('/mbean', query_args)
        if attributes is None:
            return

        if key_prefix is None:
            # Could be 1 or 2 = in the string
            # java.lang:type=Threading
            # "kafka.controller":type="ControllerStats",
            # name="LeaderElectionRateAndTimeMs"
            split_num = objectname.count('=')
            for i in range(split_num):
                if i == 0:
                    key_prefix = objectname.split('=')[1]
                    if '"' in key_prefix:
                        key_prefix = key_prefix.split('"')[1]
                    if "," in key_prefix:
                        key_prefix = key_prefix.split(',')[0]
                elif i > 0:
                    key = objectname.split('=')[2]
                    if key:
                        if '"' in key:
                            key = key.split('"')[1]
                        key_prefix = key_prefix + '.' + key

        metrics = {}

        for attrib in attributes.getiterator(tag='Attribute'):
            atype = attrib.get('type')

            ptype = self.ATTRIBUTE_TYPES.get(atype)
            if not ptype:
                continue

            value = ptype(attrib.get('value'))

            name = '.'.join([key_prefix, attrib.get('name')])
            # Some prefixes and attributes could have spaces, thus we must
            # sanitize them
            name = name.replace(' ', '')

            metrics[name] = value

        return metrics

    def collect(self):
        if ElementTree is None:
            self.log.error('Failed to import xml.etree.ElementTree')
            return

        # Get list of gatherable stats
        query_list = [
            '*kafka*:*',
            'java.lang:type=GarbageCollector,name=*',
            'java.lang:type=Threading'
        ]
        mbeans = set()
        for pattern in query_list:
            match = self.get_mbeans(pattern)
            mbeans.update(match)

        metrics = {}

        # Query each one for stats
        for mbean in mbeans:
            stats = self.query_mbean(mbean)
            metrics.update(stats)

        # Publish stats
        for metric, value in metrics.iteritems():
            self.publish(metric, value)
<commit_msg>Revert to f4a523fb92526659798e61cc334cc8e0ee8d635a<commit_after># coding=utf-8

"""
Collect stats via MX4J from Kafka

#### Dependencies

 * urllib2
 * xml.etree
"""
import urllib2

from urllib import urlencode

try:
    from xml.etree import ElementTree
except ImportError:
    ElementTree = None

try:
    from ElementTree import ParseError as ETParseError
except ImportError:
    ETParseError = Exception

import diamond.collector

class KafkaCollector(diamond.collector.Collector):
    ATTRIBUTE_TYPES = {
        'double': float,
        'float': float,
        'int': int,
        'long': long,
    }

    def get_default_config_help(self):
        config_help = super(KafkaCollector, self).get_default_config_help()
        config_help.update({
            'host': "",
            'port': "",
        })
        return config_help

    def get_default_config(self):
        """
        Returns the default collector settings
        """
        config = super(KafkaCollector, self).get_default_config()
        config.update({
            'host': '127.0.0.1',
            'port': 8082,
            'path': 'kafka',
        })
        return config

    def _get(self, path, query_args=None):
        if not path.startswith('/'):
            path = '/' + path

        qargs = {'template': 'identity'}

        if query_args:
            qargs.update(query_args)

        url = 'http://%s:%i%s?%s' % (
            self.config['host'], int(self.config['port']),
            path, urlencode(qargs))

        try:
            response = urllib2.urlopen(url)
        except urllib2.URLError, err:
            self.log.error("%s: %s", url, err)
            return None

        try:
            return ElementTree.fromstring(response.read())
        except ETParseError:
            self.log.error("Unable to parse response from mx4j")
            return None

    def get_mbeans(self, pattern):
        query_args = {'querynames': pattern}

        mbeans = self._get('/serverbydomain', query_args)
        if mbeans is None:
            return

        found_beans = set()

        for mbean in mbeans.getiterator(tag='MBean'):
            objectname = mbean.get('objectname')

            if objectname:
                found_beans.add(objectname)

        return found_beans

    def query_mbean(self, objectname, key_prefix=None):
        query_args = {
            'objectname': objectname,
            'operations': False,
            'constructors': False,
            'notifications': False,
        }

        attributes = self._get('/mbean', query_args)
        if attributes is None:
            return

        if key_prefix is None:
            # Could be 1 or 2 = in the string
            # java.lang:type=Threading
            # "kafka.controller":type="ControllerStats",
            # name="LeaderElectionRateAndTimeMs"
            split_num = objectname.count('=')
            for i in range(split_num):
                if i == 0:
                    key_prefix = objectname.split('=')[1]
                    if '"' in key_prefix:
                        key_prefix = key_prefix.split('"')[1]
                    if "," in key_prefix:
                        key_prefix = key_prefix.split(',')[0]
                elif i > 0:
                    key = objectname.split('=')[i + 1]
                    if key:
                        if '"' in key:
                            key = key.split('"')[i]
                        key_prefix = key_prefix + '.' + key.replace(",", ".")

        metrics = {}

        for attrib in attributes.getiterator(tag='Attribute'):
            atype = attrib.get('type')

            ptype = self.ATTRIBUTE_TYPES.get(atype)
            if not ptype:
                try:
                    int(attrib.get('value'))
                    ptype = int
                except Exception:
                    self.log.info('Failed to guess type %s for %s.%si value %s', atype, key_prefix, attrib.get('name'), attrib.get('value'))
                    continue

            value = ptype(attrib.get('value'))

            name = '.'.join([key_prefix, attrib.get('name')])
            # Some prefixes and attributes could have spaces, thus we must
            # sanitize them
            name = name.replace(' ', '')

            metrics[name] = value

        return metrics

    def collect(self):
        if ElementTree is None:
            self.log.error('Failed to import xml.etree.ElementTree')
            return

        # Get list of gatherable stats
        query_list = [
            '*kafka*:*',
            'java.lang:type=GarbageCollector,name=*',
            'java.lang:type=Threading'
        ]
        mbeans = set()
        for pattern in query_list:
            match = self.get_mbeans(pattern)
            mbeans.update(match)

        metrics = {}

        # Query each one for stats
        for mbean in mbeans:
            stats = self.query_mbean(mbean)
            metrics.update(stats)

        # Publish stats
        for metric, value in metrics.iteritems():
            self.publish(metric, value)
<|endoftext|>