<commit_before>from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured

from actstream.models import Action, Follow
try:
    from br_pusher import default_pusher
except ImportError:
    default_pusher = None

from . import messages, mixins

class ActionNotification(models.Model):
    action = models.ForeignKey(Action)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, related_name='+')

    is_should_email = models.BooleanField(default=False, db_index=True)
    is_should_email_separately = models.BooleanField(default=False)

    is_read = models.BooleanField(default=False, db_index=True)
    is_emailed = models.BooleanField(default=False, db_index=True)

    do_not_send_before = models.DateTimeField(blank=True, null=True)
    when_emailed = models.DateTimeField(blank=True, null=True)

    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-action__timestamp',)
        unique_together = (
            ('action', 'user',),
        )

    def __unicode__(self):
        return u'{} ({})'.format(
            self.action.__unicode__(),
            'read' if self.is_read else 'unread'
        )

    def _load_message(self):
        if not hasattr(self, '_message'):
            self._message = messages.get_message(self.action, self.user)

    @property
    def message_body(self):
        self._load_message()
        return self._message[0]

    @property
    def message_subject(self):
        self._load_message()
        return self._message[1]

    @property
    def message_from(self):
        self._load_message()
        if len(self._message) > 2:
            return self._message[2]
        return None

    @property
    def message_locale(self):
        self._load_message()
        if len(self._message) > 3:
            return self._message[3]
        return None

class ActionNotificationPreference(models.Model):
    EMAIL_NOTIFICATION_FREQUENCIES = (
        ('* * * * *', 'Immediately',),
        ('*/30 * * * *', 'Every 30 minutes',),
        ('@daily', 'Daily',),
    )

    action_verb = models.CharField(max_length=255, unique=True)

    is_should_notify_actor = models.BooleanField(default=False)
    is_should_notify_actor_when_target = models.BooleanField(default=False)

    # Email preferences
    is_should_email = models.BooleanField(default=False)
    is_should_email_separately = models.BooleanField(default=False)
    use_user_preference = models.BooleanField(
        default=False,
        help_text='Setting this true will cause frequency and is_email_separately to be ignored'
    )
    email_notification_frequency = models.CharField(
        max_length=64,
        choices=EMAIL_NOTIFICATION_FREQUENCIES,
        default='@daily'
    )

    def __unicode__(self):
        return u'notification preference for "{}"'.format(self.action_verb)

@receiver(post_save, sender=Action)
def create_action_notification(sender, instance, **kwargs): # pylint: disable-msg=unused-argument
    action = instance
    actor = action.actor
    target = action.target
    # Bind the verb locally
    _action_verb = action.verb

    # Grab list of user ids following either the actor or the target
    follow_users = Follow.objects.filter(
        Q(content_type=ContentType.objects.get_for_model(actor), object_id=actor.pk) |
        Q(content_type=ContentType.objects.get_for_model(target), object_id=target.pk, actor_only=False)
    ).values_list('user')

    # Create a notification for each user
    for user in get_user_model().objects.filter(pk__in=follow_users):
        notification_preference, _ = ActionNotificationPreference.objects.get_or_create(action_verb=_action_verb)

        if not notification_preference.is_should_notify_actor \
            and action.actor == user \
            and ( \
                not notification_preference.is_should_notify_actor_when_target \
                or action.target != user \
            ):
            # Don't notify the user who did the action under these conditions
            continue

        action_notification = ActionNotification(action=action, user=user)
        action_notification.is_should_email = notification_preference.is_should_email

        if hasattr(settings, 'ACTION_NOTIFICATION_USER_PREFERENCES') \
                and notification_preference.use_user_preference:
            user_preference = getattr(
                user,
                settings.ACTION_NOTIFICATION_USER_PREFERENCE_FIELD_NAME
            )
            if not isinstance(user_preference, mixins.UserPreferenceMixin):
                raise ImproperlyConfigured(
                    'The ACTION_NOTIFICATION_USER_PREFERENCE_FIELD_NAME in your Django setting '
                    'must inherit UserPreferenceMixin found in action_notification.mixins'
                )
            do_not_send_before, is_should_email_separately = user_preference.get_preference_for_action_verb(
                _action_verb,
                action
            )
            action_notification.is_should_email_separately = is_should_email_separately
            action_notification.do_not_send_before = do_not_send_before
        else:
            action_notification.is_should_email_separately = notification_preference.is_should_email_separately
        action_notification.save()

        if default_pusher is not None:
            channel_name = "private-user-notifications-{}".format(user.get_username())
            default_pusher.trigger(channel_name, 'new-notification', {})
<commit_msg>Save some call stacks and database calls by moving the get_or_create out of the user loop (evaluating constant local var).<commit_after>from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured

from actstream.models import Action, Follow
try:
    from br_pusher import default_pusher
except ImportError:
    default_pusher = None

from . import messages, mixins

class ActionNotification(models.Model):
    action = models.ForeignKey(Action)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, related_name='+')

    is_should_email = models.BooleanField(default=False, db_index=True)
    is_should_email_separately = models.BooleanField(default=False)

    is_read = models.BooleanField(default=False, db_index=True)
    is_emailed = models.BooleanField(default=False, db_index=True)

    do_not_send_before = models.DateTimeField(blank=True, null=True)
    when_emailed = models.DateTimeField(blank=True, null=True)

    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-action__timestamp',)
        unique_together = (
            ('action', 'user',),
        )

    def __unicode__(self):
        return u'{} ({})'.format(
            self.action.__unicode__(),
            'read' if self.is_read else 'unread'
        )

    def _load_message(self):
        if not hasattr(self, '_message'):
            self._message = messages.get_message(self.action, self.user)

    @property
    def message_body(self):
        self._load_message()
        return self._message[0]

    @property
    def message_subject(self):
        self._load_message()
        return self._message[1]

    @property
    def message_from(self):
        self._load_message()
        if len(self._message) > 2:
            return self._message[2]
        return None

    @property
    def message_locale(self):
        self._load_message()
        if len(self._message) > 3:
            return self._message[3]
        return None

class ActionNotificationPreference(models.Model):
    EMAIL_NOTIFICATION_FREQUENCIES = (
        ('* * * * *', 'Immediately',),
        ('*/30 * * * *', 'Every 30 minutes',),
        ('@daily', 'Daily',),
    )

    action_verb = models.CharField(max_length=255, unique=True)

    is_should_notify_actor = models.BooleanField(default=False)
    is_should_notify_actor_when_target = models.BooleanField(default=False)

    # Email preferences
    is_should_email = models.BooleanField(default=False)
    is_should_email_separately = models.BooleanField(default=False)
    use_user_preference = models.BooleanField(
        default=False,
        help_text='Setting this true will cause frequency and is_email_separately to be ignored'
    )
    email_notification_frequency = models.CharField(
        max_length=64,
        choices=EMAIL_NOTIFICATION_FREQUENCIES,
        default='@daily'
    )

    def __unicode__(self):
        return u'notification preference for "{}"'.format(self.action_verb)

@receiver(post_save, sender=Action)
def create_action_notification(sender, instance, **kwargs): # pylint: disable-msg=unused-argument
    action = instance
    actor = action.actor
    target = action.target
    # Bind the verb locally
    _action_verb = action.verb

    # Grab list of user ids following either the actor or the target
    follow_users = Follow.objects.filter(
        Q(content_type=ContentType.objects.get_for_model(actor), object_id=actor.pk) |
        Q(content_type=ContentType.objects.get_for_model(target), object_id=target.pk, actor_only=False)
    ).values_list('user')

    notification_preference, _ = ActionNotificationPreference.objects.get_or_create(action_verb=_action_verb)

    # Create a notification for each user
    for user in get_user_model().objects.filter(pk__in=follow_users):
        if not notification_preference.is_should_notify_actor \
            and action.actor == user \
            and ( \
                not notification_preference.is_should_notify_actor_when_target \
                or action.target != user \
            ):
            # Don't notify the user who did the action under these conditions
            continue

        action_notification = ActionNotification(action=action, user=user)
        action_notification.is_should_email = notification_preference.is_should_email

        if hasattr(settings, 'ACTION_NOTIFICATION_USER_PREFERENCES') \
                and notification_preference.use_user_preference:
            user_preference = getattr(
                user,
                settings.ACTION_NOTIFICATION_USER_PREFERENCE_FIELD_NAME
            )
            if not isinstance(user_preference, mixins.UserPreferenceMixin):
                raise ImproperlyConfigured(
                    'The ACTION_NOTIFICATION_USER_PREFERENCE_FIELD_NAME in your Django setting '
                    'must inherit UserPreferenceMixin found in action_notification.mixins'
                )
            do_not_send_before, is_should_email_separately = user_preference.get_preference_for_action_verb(
                _action_verb,
                action
            )
            action_notification.is_should_email_separately = is_should_email_separately
            action_notification.do_not_send_before = do_not_send_before
        else:
            action_notification.is_should_email_separately = notification_preference.is_should_email_separately
        action_notification.save()

        if default_pusher is not None:
            channel_name = "private-user-notifications-{}".format(user.get_username())
            default_pusher.trigger(channel_name, 'new-notification', {})
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    openslides.openslides_settings
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    OpenSlides default settings.

    :copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
    :license: GNU GPL, see LICENSE for more details.
"""

import os
import sys

_fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
def _fs2unicode(s):
    if isinstance(s, unicode):
        return s
    return s.decode(_fs_encoding)

SITE_ROOT = os.path.realpath(os.path.dirname(__file__))

AUTH_PROFILE_MODULE = 'participant.Profile'
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',
    'openslides.utils.auth.AnonymousAuth',)

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

DBPATH = _fs2unicode(os.path.join(os.path.join(SITE_ROOT, '..'), 'database.db'))

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': DBPATH,
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

ugettext = lambda s: s

LANGUAGES = (
    ('de', ugettext('German')),
    ('en', ugettext('English')),
)

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

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

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

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

# Absolute path to the directory that holds static media from ``collectstatic``
# Example: "/home/media/static.lawrence.com/"
STATIC_ROOT = _fs2unicode(os.path.join(SITE_ROOT, '../site-static'))

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

# Additional directories containing static files (not application specific)
# Examples: "/home/media/lawrence.com/extra-static/"
STATICFILES_DIRS = (
    _fs2unicode(os.path.join(SITE_ROOT, 'static')),
)

#XXX: Note this setting (as well as our workaround finder)
#     can be removed again once django-bug-#18404 has been resolved
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'openslides.utils.staticfiles.AppDirectoriesFinder',
)

MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'

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

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

ROOT_URLCONF = 'openslides.urls'

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

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mptt',
    'openslides.utils',
    'openslides.poll',
    'openslides.projector',
    'openslides.agenda',
    'openslides.application',
    'openslides.assignment',
    'openslides.participant',
    'openslides.config',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
    'django.core.context_processors.i18n',
    'django.core.context_processors.static',
    'openslides.utils.utils.revision',
    'openslides.utils.auth.anonymous_context_additions',
)
<commit_msg>#332 use an specific name for the session cookie<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    openslides.openslides_settings
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    OpenSlides default settings.

    :copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
    :license: GNU GPL, see LICENSE for more details.
"""

import os
import sys

_fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
def _fs2unicode(s):
    if isinstance(s, unicode):
        return s
    return s.decode(_fs_encoding)

SITE_ROOT = os.path.realpath(os.path.dirname(__file__))

AUTH_PROFILE_MODULE = 'participant.Profile'
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',
    'openslides.utils.auth.AnonymousAuth',)

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/'

SESSION_COOKIE_NAME = 'OpenSlidesSessionID'

DBPATH = _fs2unicode(os.path.join(os.path.join(SITE_ROOT, '..'), 'database.db'))

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': DBPATH,
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

ugettext = lambda s: s

LANGUAGES = (
    ('de', ugettext('German')),
    ('en', ugettext('English')),
)

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

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

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

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

# Absolute path to the directory that holds static media from ``collectstatic``
# Example: "/home/media/static.lawrence.com/"
STATIC_ROOT = _fs2unicode(os.path.join(SITE_ROOT, '../site-static'))

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

# Additional directories containing static files (not application specific)
# Examples: "/home/media/lawrence.com/extra-static/"
STATICFILES_DIRS = (
    _fs2unicode(os.path.join(SITE_ROOT, 'static')),
)

#XXX: Note this setting (as well as our workaround finder)
#     can be removed again once django-bug-#18404 has been resolved
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'openslides.utils.staticfiles.AppDirectoriesFinder',
)

MESSAGE_STORAGE = 'django.contrib.messages.storage.cookie.CookieStorage'

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

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',
)

ROOT_URLCONF = 'openslides.urls'

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

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mptt',
    'openslides.utils',
    'openslides.poll',
    'openslides.projector',
    'openslides.agenda',
    'openslides.application',
    'openslides.assignment',
    'openslides.participant',
    'openslides.config',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
    'django.core.context_processors.i18n',
    'django.core.context_processors.static',
    'openslides.utils.utils.revision',
    'openslides.utils.auth.anonymous_context_additions',
)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from django.db import models
from django_extensions.db.fields import CreationDateTimeField
from django_extensions.db.fields import ModificationDateTimeField
from jrlib.models import JRModel

# Адрес
class AddressCountry(JRModel):
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Страна"
        verbose_name_plural = "Страны"
    def __str__(self):
        return self.name

class AddressRegion(JRModel):
    country = models.ForeignKey(AddressCountry)
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Регион"
        verbose_name_plural = "Регионы"
    def __str__(self):
        return self.name

class AddressCity(JRModel):
    region = models.ForeignKey(AddressRegion)
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Город"
        verbose_name_plural = "Города"
    def __str__(self):
        return self.name

# Пользователь
class User(JRModel):
    city = models.ForeignKey(AddressCity)

    first_name = models.CharField(max_length = 255)
    second_name =  models.CharField(max_length = 255)
    nick = models.CharField(max_length = 255)
    email = models.EmailField()

# Базовый класс (без создания таблицы) для любых "авторских"
# таблиц, т.е., у записей которых есть автор из Users

class AuthoredModel(JRModel):
    author = models.ForeignKey(User, related_name='+')
    cr_date = CreationDateTimeField()
    up_date = ModificationDateTimeField()

    class Meta:
        abstract = True

# проект — собсно, игра
class Project(AuthoredModel):
    name = models.CharField(max_length = 1023)
    # TODO

# Права доступа к проекту
class ProjectAcl(JRModel):
    project = models.ForeignKey(Project)

# Группы полей заявок в проекте
class ProjectFieldGroup(JRModel):
    project = models.ForeignKey(Project)

# Поля заявки в проекте
class ProjectField(JRModel):
    project = models.ForeignKey(Project)
    project_field_group = models.ForeignKey(ProjectFieldGroup, blank=True, null=True)

# Значения комбобоксов в полях заявок в проекте
class ProjectFieldValue(JRModel):
    project_field = models.ForeignKey(ProjectField)

# Объекты игрового мира — персонажики, группочки персонажиков, локации
class Object(AuthoredModel):
    project = models.ForeignKey(Project)

    name = models.CharField(max_length=1023)

# История изменений в объектах
class ObjectHistory(AuthoredModel):
    obj = models.ForeignKey(Object)

# Подписки пользователей на изменения
class Subscription(AuthoredModel):
    user = models.ForeignKey(User)
    # TODO

# Заявка
class Claim(AuthoredModel):
    obj = models.ForeignKey(Object)

# история изменения заявок
class ClaimHistory(AuthoredModel):
    claim = models.ForeignKey(Claim)

# Комментарии к заявке (переписка)
class ClaimComment(AuthoredModel):
    claim = models.ForeignKey(Claim)

# Загрузы
class StoryPiece(AuthoredModel):
    obj = models.ForeignKey(Object)

# История изменения загрузов
class StoryPieceHistory(AuthoredModel):
    story_piece = models.ForeignKey(StoryPiece)

<commit_msg>Add extra fields for project<commit_after># -*- coding: utf-8 -*-
from django.db import models
from django_extensions.db.fields import CreationDateTimeField
from django_extensions.db.fields import ModificationDateTimeField
from jrlib.models import JRModel, VKField

# Адрес
class AddressCountry(JRModel):
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Страна"
        verbose_name_plural = "Страны"
    def __str__(self):
        return self.name

class AddressRegion(JRModel):
    country = models.ForeignKey(AddressCountry)
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Регион"
        verbose_name_plural = "Регионы"
    def __str__(self):
        return self.name

class AddressCity(JRModel):
    region = models.ForeignKey(AddressRegion)
    name = models.CharField(max_length = 255)
    class Meta:
        verbose_name = "Город"
        verbose_name_plural = "Города"
    def __str__(self):
        return self.name

# Пользователь
class User(JRModel):
    city = models.ForeignKey(AddressCity)

    first_name = models.CharField(max_length = 255)
    second_name =  models.CharField(max_length = 255)
    nick = models.CharField(max_length = 255)
    email = models.EmailField()

# Базовый класс (без создания таблицы) для любых "авторских"
# таблиц, т.е., у записей которых есть автор из Users

class AuthoredModel(JRModel):
    author = models.ForeignKey(User, related_name='+')
    cr_date = CreationDateTimeField()
    up_date = ModificationDateTimeField()

    class Meta:
        abstract = True

# проект — собсно, игра
class Project(AuthoredModel):
    name = models.CharField(max_length = 1023)
    external_uri = models.URLField(max_length = 255)
    description = models.TextField()
    vk_club = VKField()
    # TODO

# Права доступа к проекту
class ProjectAcl(JRModel):
    project = models.ForeignKey(Project)

# Группы полей заявок в проекте
class ProjectFieldGroup(JRModel):
    project = models.ForeignKey(Project)

# Поля заявки в проекте
class ProjectField(JRModel):
    project = models.ForeignKey(Project)
    project_field_group = models.ForeignKey(ProjectFieldGroup, blank=True, null=True)

# Значения комбобоксов в полях заявок в проекте
class ProjectFieldValue(JRModel):
    project_field = models.ForeignKey(ProjectField)

# Объекты игрового мира — персонажики, группочки персонажиков, локации
class Object(AuthoredModel):
    project = models.ForeignKey(Project)

    name = models.CharField(max_length=1023)

# История изменений в объектах
class ObjectHistory(AuthoredModel):
    obj = models.ForeignKey(Object)

# Подписки пользователей на изменения
class Subscription(AuthoredModel):
    user = models.ForeignKey(User)
    # TODO

# Заявка
class Claim(AuthoredModel):
    obj = models.ForeignKey(Object)

# история изменения заявок
class ClaimHistory(AuthoredModel):
    claim = models.ForeignKey(Claim)

# Комментарии к заявке (переписка)
class ClaimComment(AuthoredModel):
    claim = models.ForeignKey(Claim)

# Загрузы
class StoryPiece(AuthoredModel):
    obj = models.ForeignKey(Object)

# История изменения загрузов
class StoryPieceHistory(AuthoredModel):
    story_piece = models.ForeignKey(StoryPiece)

<|endoftext|>


<commit_before>import os
import pathlib
import types

from typing import Union, Iterable, ContextManager
from typing.io import BinaryIO, TextIO

from . import _common

Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike]

def open_binary(package: Package, resource: Resource) -> BinaryIO:
    """Return a file-like object opened for binary reading of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).open('rb')

def read_binary(package: Package, resource: Resource) -> bytes:
    """Return the binary contents of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).read_bytes()

def open_text(
    package: Package,
    resource: Resource,
    encoding: str = 'utf-8',
    errors: str = 'strict',
) -> TextIO:
    """Return a file-like object opened for text reading of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).open(
        'r', encoding=encoding, errors=errors
    )

def read_text(
    package: Package,
    resource: Resource,
    encoding: str = 'utf-8',
    errors: str = 'strict',
) -> str:
    """Return the decoded string of the resource.

    The decoding-related arguments have the same semantics as those of
    bytes.decode().
    """
    with open_text(package, resource, encoding, errors) as fp:
        return fp.read()

def contents(package: Package) -> Iterable[str]:
    """Return an iterable of entries in `package`.

    Note that not all entries are resources.  Specifically, directories are
    not considered resources.  Use `is_resource()` on each entry returned here
    to check if it is a resource or not.
    """
    return [path.name for path in _common.files(package).iterdir()]

def is_resource(package: Package, name: str) -> bool:
    """True if `name` is a resource inside `package`.

    Directories are *not* resources.
    """
    resource = _common.normalize_path(name)
    return any(
        traversable.name == resource and traversable.is_file()
        for traversable in _common.files(package).iterdir()
    )

def path(
    package: Package,
    resource: Resource,
) -> ContextManager[pathlib.Path]:
    """A context manager providing a file path object to the resource.

    If the resource does not already exist on its own on the file system,
    a temporary file will be created. If the file was created, the file
    will be deleted upon exiting the context manager (no exception is
    raised if the file was deleted prior to the context manager
    exiting).
    """
    return _common.as_file(_common.files(package) / _common.normalize_path(resource))
<commit_msg>bpo-38291: DeprecationWarning when importing typing.{io,re}<commit_after>import os
import pathlib
import types

from typing import Union, Iterable, ContextManager, BinaryIO, TextIO

from . import _common

Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike]

def open_binary(package: Package, resource: Resource) -> BinaryIO:
    """Return a file-like object opened for binary reading of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).open('rb')

def read_binary(package: Package, resource: Resource) -> bytes:
    """Return the binary contents of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).read_bytes()

def open_text(
    package: Package,
    resource: Resource,
    encoding: str = 'utf-8',
    errors: str = 'strict',
) -> TextIO:
    """Return a file-like object opened for text reading of the resource."""
    return (_common.files(package) / _common.normalize_path(resource)).open(
        'r', encoding=encoding, errors=errors
    )

def read_text(
    package: Package,
    resource: Resource,
    encoding: str = 'utf-8',
    errors: str = 'strict',
) -> str:
    """Return the decoded string of the resource.

    The decoding-related arguments have the same semantics as those of
    bytes.decode().
    """
    with open_text(package, resource, encoding, errors) as fp:
        return fp.read()

def contents(package: Package) -> Iterable[str]:
    """Return an iterable of entries in `package`.

    Note that not all entries are resources.  Specifically, directories are
    not considered resources.  Use `is_resource()` on each entry returned here
    to check if it is a resource or not.
    """
    return [path.name for path in _common.files(package).iterdir()]

def is_resource(package: Package, name: str) -> bool:
    """True if `name` is a resource inside `package`.

    Directories are *not* resources.
    """
    resource = _common.normalize_path(name)
    return any(
        traversable.name == resource and traversable.is_file()
        for traversable in _common.files(package).iterdir()
    )

def path(
    package: Package,
    resource: Resource,
) -> ContextManager[pathlib.Path]:
    """A context manager providing a file path object to the resource.

    If the resource does not already exist on its own on the file system,
    a temporary file will be created. If the file was created, the file
    will be deleted upon exiting the context manager (no exception is
    raised if the file was deleted prior to the context manager
    exiting).
    """
    return _common.as_file(_common.files(package) / _common.normalize_path(resource))
<|endoftext|>


<commit_before>"""MPF device for a score controller which handles all scoring and bonus tracking."""

import logging
from collections import OrderedDict

from mpf.core.machine import MachineController
from mpf.core.mode import Mode

class ScoreController(object):

    """Base class for the score controller.

    It is responsible for tracking scoring events (or any events configured to score) and adding
    them to the current player's score.
    """

    def __init__(self, machine: MachineController):
        """Initialise Score Controller."""
        self.machine = machine
        self.log = logging.getLogger("Score")
        self.log.debug("Loading the Score Controller")

        self.machine.mode_controller.register_start_method(self.mode_start,
                                                           'scoring')
        self.mode_configs = OrderedDict()
        self.mode_scores = dict()

    @classmethod
    def _validate_entry(cls, entry, mode):
        for value in entry.values():
            if isinstance(value, int):
                continue
            elif isinstance(value, str):
                try:
                    value, block = value.split('|')
                    int(value)
                    if block != "block":
                        raise AssertionError("Invalid action in scoring entry: {} in mode {}".format(
                            entry, mode.name))
                except ValueError:
                    raise AssertionError("Invalid scoring entry: {} in mode {}".format(
                        entry, mode.name))

    def mode_start(self, config: dict, mode: Mode, priority: int, **kwargs):
        """Called when mode is started.

        Args:
            config: Config dict inside mode.
            mode: Mode which was started.
            priority: Priority of mode.
        """
        del kwargs
        self.mode_configs[mode] = config
        self.mode_scores[mode] = dict()
        self.mode_configs = OrderedDict(sorted(iter(self.mode_configs.items()),
                                               key=lambda x: x[0].priority,
                                               reverse=True))

        for event in list(config.keys()):
            self._validate_entry(config[event], mode)

            mode.add_mode_event_handler(event, self._score_event_callback,
                                        priority, event_name=event)

        return self.mode_stop, mode

    def mode_stop(self, mode, **kwargs):
        """Called when mode is stopped.

        Args:
            mode: Mode which was stopped.
        """
        del kwargs
        try:
            del self.mode_configs[mode]
        except KeyError:
            pass

        try:
            del self.mode_scores[mode]
        except KeyError:
            pass

    def _score_event_callback(self, event_name, mode, **kwargs):
        del kwargs
        if not (self.machine.game.player and self.machine.game.balls_in_play):
            return

        blocked_variables = set()

        for entry_mode, settings in self.mode_configs.items():
            if event_name in settings:
                for var_name, value in settings[event_name].items():

                    if (isinstance(value, int) and
                            entry_mode == mode and
                            var_name not in blocked_variables):
                        self.add(value, var_name, mode)

                    elif isinstance(value, str):
                        value, block = value.split('|')
                        if entry_mode == mode and var_name not in blocked_variables:
                            self.add(value, var_name, mode)
                        if block.lower() == 'block':
                            blocked_variables.add(var_name)

    def add(self, value: int, var_name: str='score', mode: Mode=None):
        """Add score to current player.

        Args:
            value: The score to add.
            var_name: Player variable to use for the score.
            mode: Mode in which this was scored. Used track mode scores and post events.
        """
        if not value:
            return

        value = int(value)
        prev_value = value
        self.machine.game.player[var_name] += value

        if mode:
            self.mode_scores[mode][var_name] = (
                self.mode_scores[mode].get(var_name, 0) + value)
            self.machine.events.post('_'.join(('mode', mode.name, var_name,
                                               'score')), value=value,
                                     prev_value=prev_value,
                                     change=value - prev_value)
            '''event: mode_(mode_name)_(var_name)_score

            desc: A scoring event was just processed to add (or remove) value
            from a player variable. (Remember that scoring events can affect
            the value of *any* player variable, not just the *score* player
            variable.

            For example, if a scoring event in the "base" mode added to the
            player variable called *ramps*, the event posted would be
            *mode_base_ramps_score*.

            args:
            value: The new value of the player variable.
            prev_value: The previous value of this player variable before the
                new *value* was added.
            change: The numeric value of the change. (*value* minus
                *prev_value*).
            '''
<commit_msg>simplify<commit_after>"""MPF device for a score controller which handles all scoring and bonus tracking."""

import logging
from collections import OrderedDict

from mpf.core.machine import MachineController
from mpf.core.mode import Mode

class ScoreController(object):

    """Base class for the score controller.

    It is responsible for tracking scoring events (or any events configured to score) and adding
    them to the current player's score.
    """

    def __init__(self, machine: MachineController):
        """Initialise Score Controller."""
        self.machine = machine
        self.log = logging.getLogger("Score")
        self.log.debug("Loading the Score Controller")

        self.machine.mode_controller.register_start_method(self.mode_start,
                                                           'scoring')
        self.mode_configs = OrderedDict()
        self.mode_scores = dict()

    @classmethod
    def _validate_entry(cls, entry, mode):
        for value in entry.values():
            if isinstance(value, int):
                continue
            elif isinstance(value, str):
                try:
                    value, block = value.split('|')
                    int(value)
                    if block != "block":
                        raise AssertionError("Invalid action in scoring entry: {} in mode {}".format(
                            entry, mode.name))
                except ValueError:
                    raise AssertionError("Invalid scoring entry: {} in mode {}".format(
                        entry, mode.name))

    def mode_start(self, config: dict, mode: Mode, priority: int, **kwargs):
        """Called when mode is started.

        Args:
            config: Config dict inside mode.
            mode: Mode which was started.
            priority: Priority of mode.
        """
        del kwargs
        self.mode_configs[mode] = config
        self.mode_scores[mode] = dict()
        self.mode_configs = OrderedDict(sorted(iter(self.mode_configs.items()),
                                               key=lambda x: x[0].priority,
                                               reverse=True))

        for event in config:
            self._validate_entry(config[event], mode)

            mode.add_mode_event_handler(event, self._score_event_callback,
                                        priority, event_name=event)

        return self.mode_stop, mode

    def mode_stop(self, mode, **kwargs):
        """Called when mode is stopped.

        Args:
            mode: Mode which was stopped.
        """
        del kwargs
        try:
            del self.mode_configs[mode]
        except KeyError:
            pass

        try:
            del self.mode_scores[mode]
        except KeyError:
            pass

    def _score_event_callback(self, event_name, mode, **kwargs):
        del kwargs
        if not (self.machine.game.player and self.machine.game.balls_in_play):
            return

        blocked_variables = set()

        for entry_mode, settings in self.mode_configs.items():
            if event_name in settings:
                for var_name, value in settings[event_name].items():

                    if (isinstance(value, int) and
                            entry_mode == mode and
                            var_name not in blocked_variables):
                        self.add(value, var_name, mode)

                    elif isinstance(value, str):
                        value, block = value.split('|')
                        if entry_mode == mode and var_name not in blocked_variables:
                            self.add(value, var_name, mode)
                        if block.lower() == 'block':
                            blocked_variables.add(var_name)

    def add(self, value: int, var_name: str='score', mode: Mode=None):
        """Add score to current player.

        Args:
            value: The score to add.
            var_name: Player variable to use for the score.
            mode: Mode in which this was scored. Used track mode scores and post events.
        """
        if not value:
            return

        value = int(value)
        prev_value = value
        self.machine.game.player[var_name] += value

        if mode:
            self.mode_scores[mode][var_name] = (
                self.mode_scores[mode].get(var_name, 0) + value)
            self.machine.events.post('_'.join(('mode', mode.name, var_name,
                                               'score')), value=value,
                                     prev_value=prev_value,
                                     change=value - prev_value)
            '''event: mode_(mode_name)_(var_name)_score

            desc: A scoring event was just processed to add (or remove) value
            from a player variable. (Remember that scoring events can affect
            the value of *any* player variable, not just the *score* player
            variable.

            For example, if a scoring event in the "base" mode added to the
            player variable called *ramps*, the event posted would be
            *mode_base_ramps_score*.

            args:
            value: The new value of the player variable.
            prev_value: The previous value of this player variable before the
                new *value* was added.
            change: The numeric value of the change. (*value* minus
                *prev_value*).
            '''
<|endoftext|>


<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Copyright (c) 2014 Mozilla Corporation

import netaddr
import os

from mozdef_util.geo_ip import GeoIP

def isIP(ip):
    try:
        netaddr.IPNetwork(ip)
        return True
    except:
        return False

class message(object):
    def __init__(self):
        '''register our criteria for being passed a message
           as a list of lower case strings or values to match with an event's dictionary of keys or values
           set the priority if you have a preference for order of plugins to run.
           0 goes first, 100 is assumed/default if not sent
        '''
        self.registration = ['sourceipaddress', 'destinationipaddress']
        self.priority = 20
        geoip_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../data/GeoLite2-City.mmdb")
        self.geoip = GeoIP(geoip_data_dir)

    def ipLocation(self, ip):
        location = dict()
        try:
            geoDict = self.geoip.lookup_ip(ip)
            if geoDict is not None:
                return geoDict
            else:
                location['location'] = 'unknown'
        except ValueError:
            pass
        return location

    def onMessage(self, message, metadata):
        if 'details' in message:
            if 'sourceipaddress' in message['details']:
                ipText = message['details']['sourceipaddress']
                if isIP(ipText):
                    ip = netaddr.IPNetwork(ipText)[0]
                    if (not ip.is_loopback() and not ip.is_private() and not ip.is_reserved()):
                        '''lookup geoip info'''
                        message['details']['sourceipgeolocation'] = self.ipLocation(ipText)
                        # Add a geo_point coordinates if latitude and longitude exist
                        if 'latitude' in message['details']['sourceipgeolocation'] and 'longitude' in message['details']['sourceipgeolocation']:
                            message['details']['sourceipgeopoint'] = '{0},{1}'.format(
                                message['details']['sourceipgeolocation']['latitude'],
                                message['details']['sourceipgeolocation']['longitude']
                            )

                else:
                    # invalid ip sent in the field
                    # if we send on, elastic search will error, so set it
                    # to a valid, yet meaningless value
                    message['details']['sourceipaddress'] = '0.0.0.0'

            if 'destinationipaddress' in message['details']:
                ipText = message['details']['destinationipaddress']
                if isIP(ipText):
                    ip = netaddr.IPNetwork(ipText)[0]
                    if (not ip.is_loopback() and not ip.is_private() and not ip.is_reserved()):
                        '''lookup geoip info'''
                        message['details']['destinationipgeolocation'] = self.ipLocation(ipText)
                        # Add a geo_point coordinates if latitude and longitude exist
                        if 'latitude' in message['details']['destinationipgeolocation'] and 'longitude' in message['details']['destinationipgeolocation']:
                            message['details']['destinationipgeopoint'] = '{0},{1}'.format(
                                message['details']['destinationipgeolocation']['latitude'],
                                message['details']['destinationipgeolocation']['longitude']
                            )
                else:
                    # invalid ip sent in the field
                    # if we send on, elastic search will error, so set it
                    # to a valid, yet meaningless value
                    message['details']['destinationipaddress'] = '0.0.0.0'
        return (message, metadata)
<commit_msg>Only set geopoint check if lat and long are good<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Copyright (c) 2014 Mozilla Corporation

import netaddr
import os

from mozdef_util.geo_ip import GeoIP

def isIP(ip):
    try:
        netaddr.IPNetwork(ip)
        return True
    except:
        return False

class message(object):
    def __init__(self):
        '''register our criteria for being passed a message
           as a list of lower case strings or values to match with an event's dictionary of keys or values
           set the priority if you have a preference for order of plugins to run.
           0 goes first, 100 is assumed/default if not sent
        '''
        self.registration = ['sourceipaddress', 'destinationipaddress']
        self.priority = 20
        geoip_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../data/GeoLite2-City.mmdb")
        self.geoip = GeoIP(geoip_data_dir)

    def ipLocation(self, ip):
        location = dict()
        try:
            geoDict = self.geoip.lookup_ip(ip)
            if geoDict is not None:
                return geoDict
            else:
                location['location'] = 'unknown'
        except ValueError:
            pass
        return location

    def onMessage(self, message, metadata):
        if 'details' in message:
            keys = ['source', 'destination']
            for key in keys:
                ip_key = '{0}ipaddress'.format(key)
                if ip_key in message['details']:
                    ipText = message['details'][ip_key]
                    if isIP(ipText):
                        ip = netaddr.IPNetwork(ipText)[0]
                        if (not ip.is_loopback() and not ip.is_private() and not ip.is_reserved()):
                            '''lookup geoip info'''
                            geo_key = '{0}ipgeolocation'.format(key)
                            message['details'][geo_key] = self.ipLocation(ipText)
                            # Add a geo_point coordinates if latitude and longitude exist
                            if 'latitude' in message['details'][geo_key] and 'longitude' in message['details'][geo_key]:
                                if message['details'][geo_key]['latitude'] != '' and message['details'][geo_key]['longitude'] != '':
                                    geopoint_key = '{0}ipgeopoint'.format(key)
                                    message['details'][geopoint_key] = '{0},{1}'.format(
                                        message['details'][geo_key]['latitude'],
                                        message['details'][geo_key]['longitude']
                                    )

                    else:
                        # invalid ip sent in the field
                        # if we send on, elastic search will error, so set it
                        # to a valid, yet meaningless value
                        message['details'][ip_key] = '0.0.0.0'
        return (message, metadata)
<|endoftext|>


<commit_before>import argparse
import io
import logging
import os
import sys

from mrfitty import __version__
from mrfitty.fit_task_builder import (
    ConfigurationFileError,
    build_fit_task,
    get_config_parser,
)

def main():
    cli(sys.argv[1:])

def cli(argv):
    print("argv: {}".format(argv))
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        "config_fp", metavar="FILE", help="path to a configuration file"
    )
    argparser.add_argument(
        "--version", action="version", version="MrFitty version {}".format(__version__)
    )

    parsed_args = argparser.parse_args(argv)
    print("parsed args: {}".format(parsed_args))
    fit(parsed_args)

def fit(args):
    config_fp = args.config_fp
    config = get_config_parser()
    print("reading configuration file path: {}".format(config_fp))
    with open(config_fp) as config_file:
        config.read_file(config_file)

    level = config.get("log", "level", fallback="INFO")
    logging.basicConfig(level=logging.getLevelName(level))
    log = logging.getLogger(name=__name__)

    # log the configuration file
    with io.StringIO() as string_buffer:
        config.write(string_buffer)
        log.info("configuration file contents:\n{}".format(string_buffer.getvalue()))

    try:
        fitter = build_fit_task(config)
        plots_pdf_file_dir = os.path.expanduser(
            config.get("output", "plots_pdf_dir", fallback=None)
        )
        if not os.path.exists(plots_pdf_file_dir):
            log.info('output directory "%s" will be created', plots_pdf_file_dir)
            os.makedirs(plots_pdf_file_dir)
        fitter.fit_all(plots_pdf_dp=plots_pdf_file_dir)
    except ConfigurationFileError as e:
        log.error(e)
        log.exception('configuration file error in "%s"', config_fp)
        quit()

    table_file_path = os.path.expanduser(
        config.get("output", "table_fp", fallback=None)
    )
    if table_file_path:
        log.info("writing table output to file path {}".format(table_file_path))
        fitter.write_table(table_file_path)
    else:
        log.warning("No file path specified for table output")

    best_fit_files_dir_path = os.path.expanduser(
        config.get("output", "best_fit_files_dir", fallback=None)
    )
    if best_fit_files_dir_path:
        if not os.path.expanduser(best_fit_files_dir_path):
            log.info("creating directory %s", best_fit_files_dir_path)
            os.makedirs(best_fit_files_dir_path)
        fitter.write_best_fit_arrays(best_fit_files_dir_path)
    else:
        log.warning("No directory specified for best fit files")

if __name__ == "__main__":
    main()
<commit_msg>print absolute file path<commit_after>import argparse
import io
import logging
import os
import sys

from mrfitty import __version__
from mrfitty.fit_task_builder import (
    ConfigurationFileError,
    build_fit_task,
    get_config_parser,
)

def main():
    cli(sys.argv[1:])

def cli(argv):
    print("argv: {}".format(argv))
    argparser = argparse.ArgumentParser()
    argparser.add_argument(
        "config_fp", metavar="FILE", help="path to a configuration file"
    )
    argparser.add_argument(
        "--version", action="version", version="MrFitty version {}".format(__version__)
    )

    parsed_args = argparser.parse_args(argv)
    print("parsed args: {}".format(parsed_args))
    fit(parsed_args)

def fit(args):
    config_fp = args.config_fp
    config = get_config_parser()
    print("reading configuration file path: {}".format(config_fp))
    with open(config_fp) as config_file:
        config.read_file(config_file)

    level = config.get("log", "level", fallback="INFO")
    logging.basicConfig(level=logging.getLevelName(level))
    log = logging.getLogger(name=__name__)

    # log the configuration file
    with io.StringIO() as string_buffer:
        config.write(string_buffer)
        log.info("configuration file contents:\n{}".format(string_buffer.getvalue()))

    try:
        fitter = build_fit_task(config)
        plots_pdf_file_dir = os.path.expanduser(
            config.get("output", "plots_pdf_dir", fallback=None)
        )
        if not os.path.exists(plots_pdf_file_dir):
            log.info('output directory "%s" will be created', os.path.abspath(plots_pdf_file_dir))
            os.makedirs(plots_pdf_file_dir)
        fitter.fit_all(plots_pdf_dp=plots_pdf_file_dir)
    except ConfigurationFileError as e:
        log.error(e)
        log.exception('configuration file error in "%s"', config_fp)
        quit()

    table_file_path = os.path.expanduser(
        config.get("output", "table_fp", fallback=None)
    )
    if table_file_path:
        log.info("writing table output to file path {}".format(table_file_path))
        fitter.write_table(table_file_path)
    else:
        log.warning("No file path specified for table output")

    best_fit_files_dir_path = os.path.expanduser(
        config.get("output", "best_fit_files_dir", fallback=None)
    )
    if best_fit_files_dir_path:
        if not os.path.expanduser(best_fit_files_dir_path):
            log.info("creating directory %s", best_fit_files_dir_path)
            os.makedirs(best_fit_files_dir_path)
        fitter.write_best_fit_arrays(best_fit_files_dir_path)
    else:
        log.warning("No directory specified for best fit files")

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


<commit_before>#This  program will ask you for 5 integer or float values.
#It will calculate the average of all valus from 0 inclusive to 10 exclusive.
#It will print out whether the resulting average is even or odd.

def number0 (N0):
	N0= raw_input ("n0:")
	if N0 < int(0) or N0 > int(10):
		print float({}) "is out of range"
number0 ()

def number1 (N1):
	N1= raw_input ("n1:")
	if N1 < int(0) or N1 > int(10):
		print float({}) "is out of range"
number1 ()

def number2 (N2):
	N2= raw_input ("n2:")
	if N2 < int(0) or N2 > int(10):
		print float({}) "is out of range"
number2 ()

def number3 (N3):
	N3= raw_input ("n3:")
	if N3 < int(0) or N3 > int(10):
		print float({}) "is out of range"
number3 ()

def number4 (N4):
	N4= raw_input ("n4:")
	if N4 < int(0) or N4 > int(10):
		print float({}) "is out of range")
number4 ()

<commit_msg>Commit made changes to file<commit_after>
def main ():
	print (""" This  program will ask you for 5 integer or float values. It will calculate the average of all values from 0 inclusive to 10 exclusive. It will print out whether the resulting average is even or odd.""")
	N0 = float(raw_input("n0: "))
	if N0 < int(0) or N0 >= int(10):
 		print str(N0) +  " is out of range."
	N1 = float(raw_input("n1: "))
	if N1 < int(0) or N1 >= int(10):
		print str(N1) + " is out of range."
	N2 = float(raw_input("n2: "))
	if N2 < int(0) or N2 >= int(10):
		print str(N2) + " is out of range."
	N3 = float(raw_input("n3: "))
	if N3 < int(0) or N3 >= int(10):
 		print str(N3) + " is out of range."
	N4 = float(raw_input("n4: "))
	if N4 < int(0) or N4 >= int(10):
		print str(N4) + " is out of range."
	average= float(N0+N1+N2+N3+N4)/5
	if average== int(0):
		print "The integer part is even"
	elif average== int(1):
		print "The integer part is odd"
	elif average== int(2):
		print "The integer part is even"
	elif average== int(3):
		print "The integer part is odd"
	elif average== int(4):
		print "The integer part is even"
	elif average== int(5):
		print "The integer part is odd"
	elif average== int(6):
		print "The integer part is even"
	elif average== int(7):
		print "The integer part is odd"
	elif average== int(8):
		print "The integer part is even"
	else:
		print "The integer part is odd"
main ()

	total= 0+1+8
count= 0+1+1
<|endoftext|>


<commit_before># Copyright (C) 2010  Chris Lalancette <clalance@redhat.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import Guest
import shutil
import subprocess
import re
import ozutil
import os

class FedoraGuest(Guest.CDGuest):
    def __init__(self, idl, config, nicmodel, haverepo, diskbus, brokenisomethod):
        update = idl.update()
        arch = idl.arch()
        self.ks_file = ozutil.generate_full_auto_path("fedora-" + update + "-jeos.ks")
        self.haverepo = haverepo
        self.brokenisomethod = brokenisomethod
        self.installtype = idl.installtype()

        if self.installtype == 'url':
            self.url = idl.url()
        elif self.installtype == 'iso':
            self.url = idl.iso()
        else:
            raise Exception, "Fedora installs must be done via url or iso"

        self.url = ozutil.check_url(self.url)

        if self.installtype == 'url':
            ozutil.deny_localhost(self.url)
        # FIXME: if doing an ISO install, we have to check that the ISO passed
        # in is the DVD, not the CD (since we can't change disks midway)

        Guest.CDGuest.__init__(self, "Fedora", update, arch, nicmodel, None,
                               None, diskbus, config)

    def modify_iso(self):
        self.log.debug("Putting the kickstart in place")

        shutil.copy(self.ks_file, self.iso_contents + "/ks.cfg")

        self.log.debug("Modifying the boot options")
        f = open(self.iso_contents + "/isolinux/isolinux.cfg", "r")
        lines = f.readlines()
        f.close()
        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            elif re.match("default", line):
                lines[lines.index(line)] = "default customiso\n"
        lines.append("label customiso\n")
        lines.append("  kernel vmlinuz\n")
        initrdline = "  append initrd=initrd.img ks=cdrom:/ks.cfg"
        if self.installtype == "url":
            if self.haverepo:
                initrdline += " repo="
            else:
                initrdline += " method="
            initrdline += self.url + "\n"
        else:
            # if the installtype is iso, then due to a bug in anaconda we leave
            # out the method completely
            if not self.brokenisomethod:
                initrdline += " method=cdrom:/dev/cdrom"
            initrdline += "\n"
        lines.append(initrdline)

        f = open(self.iso_contents + "/isolinux/isolinux.cfg", "w")
        f.writelines(lines)
        f.close()

    def generate_new_iso(self):
        self.log.debug("Generating new ISO")
        Guest.subprocess_check_output(["mkisofs", "-r", "-T", "-J", "-V",
                                       "Custom", "-b", "isolinux/isolinux.bin",
                                       "-c", "isolinux/boot.cat",
                                       "-no-emul-boot", "-boot-load-size", "4",
                                       "-boot-info-table", "-v", "-v",
                                       "-o", self.output_iso,
                                       self.iso_contents])

    def generate_install_media(self, force_download):
        self.log.info("Generating install media")
        fetchurl = self.url
        if self.installtype == 'url':
            fetchurl += "/images/boot.iso"
        self.get_original_iso(fetchurl, force_download)
        self.copy_iso()
        self.modify_iso()
        self.generate_new_iso()
        self.cleanup_iso()

    def get_default_runlevel(self):
        runlevel = "3"
        if self.g.exists('/etc/inittab'):
            lines = self.g.cat('/etc/inittab').split("\n")
            for line in lines:
                if re.match('id:', line):
                    # FIXME: this parsing is a bit iffy
                    runlevel = line.split(':')[1]
                    break

        return runlevel

    def get_service_runlevel_link(self, runlevel, service):
        lines = self.g.cat('/etc/init.d/' + service).split("\n")
        startlevel = "99"
        for line in lines:
            if re.match('# chkconfig:', line):
                # FIXME: this parsing could get ugly fast
                startlevel = line.split(':')[1].split()[1]
                break

        return "/etc/rc.d/rc" + runlevel + ".d/S" + startlevel + service

    def collect_setup(self, libvirt_xml):
        self.log.info("CDL Collection Setup")

        self.guestfs_handle_setup(libvirt_xml)

        # we have to do 3 things to make sure we can ssh into Fedora 13:
        # 1)  Upload our ssh key
        # 2)  Make sure sshd is running on boot
        # 3)  Make sure that port 22 is open in the firewall
        # 4)  Make the guest announce itself to the host

        runlevel = self.get_default_runlevel()

        # part 1; upload the keys
        self.log.debug("Step 1: Uploading ssh keys")
        if not self.g.exists('/root/.ssh'):
            self.g.mkdir('/root/.ssh')

        if self.g.exists('/root/.ssh/authorized_keys'):
            self.g.mv('/root/.ssh/authorized_keys',
                      '/root/.ssh/authorized_keys.cdl')

        if not os.access(self.cdl_tmp, os.F_OK):
            os.makedirs(self.cdl_tmp)

        privname = self.cdl_tmp + '/id_rsa-cdl-gen'
        pubname = self.cdl_tmp + '/id_rsa-cdl-gen.pub'
        if os.access(privname, os.F_OK):
            os.remove(privname)
        if os.access(pubname, os.F_OK):
            os.remove(pubname)
        subprocess.call(['ssh-keygen', '-q', '-t', 'rsa', '-b', '2048',
                         '-N', '', '-f', privname])

        self.g.upload(pubname, '/root/.ssh/authorized_keys')

        # part 2; check and setup sshd
        self.log.debug("Step 2: setup sshd")
        if not self.g.exists('/etc/init.d/sshd') or not self.g.exists('/usr/sbin/sshd'):
            raise Exception, "ssh not installed on the image, cannot continue"

        startuplink = self.get_service_runlevel_link(runlevel, 'sshd')
        if self.g.exists(startuplink):
            self.g.mv(startuplink, startuplink + ".cdl")
        self.g.ln_sf('/etc/init.d/sshd', startuplink)

        sshd_config = \
"""SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem	sftp	/usr/libexec/openssh/sftp-server
"""

        sshd_config_file = self.cdl_tmp + "/sshd_config"
        f = open(sshd_config_file, 'w')
        f.write(sshd_config)
        f.close()

        if self.g.exists('/etc/ssh/sshd_config'):
            self.g.mv('/etc/ssh/sshd_config', '/etc/ssh/sshd_config.cdl')
        self.g.upload(sshd_config_file, '/etc/ssh/sshd_config')
        os.unlink(sshd_config_file)

        # part 3; open up iptables
        self.log.debug("Step 3: Open up the firewall")
        if self.g.exists('/etc/sysconfig/iptables'):
            self.g.mv('/etc/sysconfig/iptables', '/etc/sysconfig/iptables.cdl')
        # implicit else; if there is no iptables file, the firewall is open

        # part 4; make sure the guest announces itself
        self.log.debug("Step 4: Guest announcement")
        if not self.g.exists('/etc/init.d/crond') or not self.g.exists('/usr/sbin/crond'):
            raise Exception, "cron not installed on the image, cannot continue"

        cdlpath = ozutil.generate_full_guesttools_path('cdl-nc')
        self.g.upload(cdlpath, '/root/cdl-nc')
        self.g.chmod(0755, '/root/cdl-nc')

        announcefile = self.cdl_tmp + "/announce"
        f = open(announcefile, 'w')
        f.write('*/1 * * * * root /bin/bash -c "/root/cdl-nc ' + self.host_bridge_ip + ' ' + str(self.listen_port) + '"\n')
        f.close()

        self.g.upload(announcefile, '/etc/cron.d/announce')

        startuplink = self.get_service_runlevel_link(runlevel, 'crond')
        if self.g.exists(startuplink):
            self.g.mv(startuplink, startuplink + ".cdl")
        self.g.ln_sf('/etc/init.d/crond', startuplink)

        os.unlink(announcefile)
        self.guestfs_handle_cleanup()

    def collect_teardown(self, libvirt_xml):
        self.log.info("CDL Collection Teardown")

        self.guestfs_handle_setup(libvirt_xml)

        # reset the authorized keys
        if self.g.exists('/root/.ssh/authorized_keys'):
            self.g.rm('/root/.ssh/authorized_keys')
        if self.g.exists('/root/.ssh/authorized_keys.cdl'):
            self.g.mv('/root/.ssh/authorized_keys.cdl',
                      '/root/.ssh/authorized_keys')

        # reset iptables
        if self.g.exists('/etc/sysconfig/iptables'):
            self.g.rm('/etc/sysconfig/iptables')
        if self.g.exists('/etc/sysconfig/iptables.cdl'):
            self.g.mv('/etc/sysconfig/iptables')

        # remove announce cronjob
        if self.g.exists('/etc/cron.d/announce'):
            self.g.rm('/etc/cron.d/announce')

        # remove cdl-nc binary
        if self.g.exists('/root/cdl-nc'):
            self.g.rm('/root/cdl-nc')

        # remove custom sshd_config
        if self.g.exists('/etc/ssh/sshd_config'):
            self.g.rm('/etc/ssh/sshd_config')
        if self.g.exists('/etc/ssh/sshd_config.cdl'):
            self.g.mv('/etc/ssh/sshd_config.cdl', '/etc/ssh/sshd_config')

        # reset the service links
        runlevel = self.get_default_runlevel()

        for service in ["sshd", "crond"]:
            startuplink = self.get_service_runlevel_link(runlevel, service)
            if self.g.exists(startuplink):
                self.g.rm(startuplink)
            if self.g.exists(startuplink + ".cdl"):
                self.g.mv(startuplink + ".cdl", startuplink)

        self.guestfs_handle_cleanup()

    def generate_cdl(self, libvirt_xml):
        self.log.info("Generating CDL")

        self.collect_setup(libvirt_xml)

        output = ''
        try:
            self.libvirt_dom = self.libvirt_conn.defineXML(libvirt_xml)
            self.libvirt_dom.create()

            guestaddr = self.wait_for_guest_boot()

            data = subprocess.Popen(["ssh", "-i",
                                     self.cdl_tmp + '/id_rsa-cdl-gen',
                                     "-o", "StrictHostKeyChecking=no",
                                     "-o", "ConnectTimeout=5", guestaddr,
                                     'rpm -qa'], stdout=subprocess.PIPE).communicate()

            # FIXME: what if the output is blank?

            output = self.output_cdl_xml(data[0].split("\n"))

            # FIXME: should we try to do a graceful shutdown here?  At the very
            # least we should do a sync
            self.libvirt_dom.destroy()
        finally:
            self.collect_teardown(libvirt_xml)

        return output

def get_class(idl, config):
    update = idl.update()
    if update == "10" or update == "11" or update == "12" or update == "13" or update == "14":
        return FedoraGuest(idl, config, "virtio", True, "virtio", True)
    if update == "9":
        return FedoraGuest(idl, config, "rtl8139", False, None, False)
    if update == "8" or update == "7":
        return FedoraGuest(idl, config, "rtl8139", False, None, True)
    raise Exception, "Unsupported Fedora update " + update
<commit_msg>Fix Fedora 7 and 8 ISO installs.<commit_after># Copyright (C) 2010  Chris Lalancette <clalance@redhat.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import Guest
import shutil
import subprocess
import re
import ozutil
import os

class FedoraGuest(Guest.CDGuest):
    def __init__(self, idl, config, nicmodel, haverepo, diskbus, brokenisomethod):
        update = idl.update()
        arch = idl.arch()
        self.ks_file = ozutil.generate_full_auto_path("fedora-" + update + "-jeos.ks")
        self.haverepo = haverepo
        self.brokenisomethod = brokenisomethod
        self.installtype = idl.installtype()

        if self.installtype == 'url':
            self.url = idl.url()
        elif self.installtype == 'iso':
            self.url = idl.iso()
        else:
            raise Exception, "Fedora installs must be done via url or iso"

        self.url = ozutil.check_url(self.url)

        if self.installtype == 'url':
            ozutil.deny_localhost(self.url)
        # FIXME: if doing an ISO install, we have to check that the ISO passed
        # in is the DVD, not the CD (since we can't change disks midway)

        Guest.CDGuest.__init__(self, "Fedora", update, arch, nicmodel, None,
                               None, diskbus, config)

    def modify_iso(self):
        self.log.debug("Putting the kickstart in place")

        shutil.copy(self.ks_file, self.iso_contents + "/ks.cfg")

        self.log.debug("Modifying the boot options")
        f = open(self.iso_contents + "/isolinux/isolinux.cfg", "r")
        lines = f.readlines()
        f.close()
        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            elif re.match("default", line):
                lines[lines.index(line)] = "default customiso\n"
        lines.append("label customiso\n")
        lines.append("  kernel vmlinuz\n")
        initrdline = "  append initrd=initrd.img ks=cdrom:/ks.cfg"
        if self.installtype == "url":
            if self.haverepo:
                initrdline += " repo="
            else:
                initrdline += " method="
            initrdline += self.url + "\n"
        else:
            # if the installtype is iso, then due to a bug in anaconda we leave
            # out the method completely
            if not self.brokenisomethod:
                initrdline += " method=cdrom:/dev/cdrom"
            initrdline += "\n"
        lines.append(initrdline)

        f = open(self.iso_contents + "/isolinux/isolinux.cfg", "w")
        f.writelines(lines)
        f.close()

    def generate_new_iso(self):
        self.log.debug("Generating new ISO")
        Guest.subprocess_check_output(["mkisofs", "-r", "-T", "-J", "-V",
                                       "Custom", "-b", "isolinux/isolinux.bin",
                                       "-c", "isolinux/boot.cat",
                                       "-no-emul-boot", "-boot-load-size", "4",
                                       "-boot-info-table", "-v", "-v",
                                       "-o", self.output_iso,
                                       self.iso_contents])

    def generate_install_media(self, force_download):
        self.log.info("Generating install media")
        fetchurl = self.url
        if self.installtype == 'url':
            fetchurl += "/images/boot.iso"
        self.get_original_iso(fetchurl, force_download)
        self.copy_iso()
        self.modify_iso()
        self.generate_new_iso()
        self.cleanup_iso()

    def get_default_runlevel(self):
        runlevel = "3"
        if self.g.exists('/etc/inittab'):
            lines = self.g.cat('/etc/inittab').split("\n")
            for line in lines:
                if re.match('id:', line):
                    # FIXME: this parsing is a bit iffy
                    runlevel = line.split(':')[1]
                    break

        return runlevel

    def get_service_runlevel_link(self, runlevel, service):
        lines = self.g.cat('/etc/init.d/' + service).split("\n")
        startlevel = "99"
        for line in lines:
            if re.match('# chkconfig:', line):
                # FIXME: this parsing could get ugly fast
                startlevel = line.split(':')[1].split()[1]
                break

        return "/etc/rc.d/rc" + runlevel + ".d/S" + startlevel + service

    def collect_setup(self, libvirt_xml):
        self.log.info("CDL Collection Setup")

        self.guestfs_handle_setup(libvirt_xml)

        # we have to do 3 things to make sure we can ssh into Fedora 13:
        # 1)  Upload our ssh key
        # 2)  Make sure sshd is running on boot
        # 3)  Make sure that port 22 is open in the firewall
        # 4)  Make the guest announce itself to the host

        runlevel = self.get_default_runlevel()

        # part 1; upload the keys
        self.log.debug("Step 1: Uploading ssh keys")
        if not self.g.exists('/root/.ssh'):
            self.g.mkdir('/root/.ssh')

        if self.g.exists('/root/.ssh/authorized_keys'):
            self.g.mv('/root/.ssh/authorized_keys',
                      '/root/.ssh/authorized_keys.cdl')

        if not os.access(self.cdl_tmp, os.F_OK):
            os.makedirs(self.cdl_tmp)

        privname = self.cdl_tmp + '/id_rsa-cdl-gen'
        pubname = self.cdl_tmp + '/id_rsa-cdl-gen.pub'
        if os.access(privname, os.F_OK):
            os.remove(privname)
        if os.access(pubname, os.F_OK):
            os.remove(pubname)
        subprocess.call(['ssh-keygen', '-q', '-t', 'rsa', '-b', '2048',
                         '-N', '', '-f', privname])

        self.g.upload(pubname, '/root/.ssh/authorized_keys')

        # part 2; check and setup sshd
        self.log.debug("Step 2: setup sshd")
        if not self.g.exists('/etc/init.d/sshd') or not self.g.exists('/usr/sbin/sshd'):
            raise Exception, "ssh not installed on the image, cannot continue"

        startuplink = self.get_service_runlevel_link(runlevel, 'sshd')
        if self.g.exists(startuplink):
            self.g.mv(startuplink, startuplink + ".cdl")
        self.g.ln_sf('/etc/init.d/sshd', startuplink)

        sshd_config = \
"""SyslogFacility AUTHPRIV
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
X11Forwarding yes
Subsystem	sftp	/usr/libexec/openssh/sftp-server
"""

        sshd_config_file = self.cdl_tmp + "/sshd_config"
        f = open(sshd_config_file, 'w')
        f.write(sshd_config)
        f.close()

        if self.g.exists('/etc/ssh/sshd_config'):
            self.g.mv('/etc/ssh/sshd_config', '/etc/ssh/sshd_config.cdl')
        self.g.upload(sshd_config_file, '/etc/ssh/sshd_config')
        os.unlink(sshd_config_file)

        # part 3; open up iptables
        self.log.debug("Step 3: Open up the firewall")
        if self.g.exists('/etc/sysconfig/iptables'):
            self.g.mv('/etc/sysconfig/iptables', '/etc/sysconfig/iptables.cdl')
        # implicit else; if there is no iptables file, the firewall is open

        # part 4; make sure the guest announces itself
        self.log.debug("Step 4: Guest announcement")
        if not self.g.exists('/etc/init.d/crond') or not self.g.exists('/usr/sbin/crond'):
            raise Exception, "cron not installed on the image, cannot continue"

        cdlpath = ozutil.generate_full_guesttools_path('cdl-nc')
        self.g.upload(cdlpath, '/root/cdl-nc')
        self.g.chmod(0755, '/root/cdl-nc')

        announcefile = self.cdl_tmp + "/announce"
        f = open(announcefile, 'w')
        f.write('*/1 * * * * root /bin/bash -c "/root/cdl-nc ' + self.host_bridge_ip + ' ' + str(self.listen_port) + '"\n')
        f.close()

        self.g.upload(announcefile, '/etc/cron.d/announce')

        startuplink = self.get_service_runlevel_link(runlevel, 'crond')
        if self.g.exists(startuplink):
            self.g.mv(startuplink, startuplink + ".cdl")
        self.g.ln_sf('/etc/init.d/crond', startuplink)

        os.unlink(announcefile)
        self.guestfs_handle_cleanup()

    def collect_teardown(self, libvirt_xml):
        self.log.info("CDL Collection Teardown")

        self.guestfs_handle_setup(libvirt_xml)

        # reset the authorized keys
        if self.g.exists('/root/.ssh/authorized_keys'):
            self.g.rm('/root/.ssh/authorized_keys')
        if self.g.exists('/root/.ssh/authorized_keys.cdl'):
            self.g.mv('/root/.ssh/authorized_keys.cdl',
                      '/root/.ssh/authorized_keys')

        # reset iptables
        if self.g.exists('/etc/sysconfig/iptables'):
            self.g.rm('/etc/sysconfig/iptables')
        if self.g.exists('/etc/sysconfig/iptables.cdl'):
            self.g.mv('/etc/sysconfig/iptables')

        # remove announce cronjob
        if self.g.exists('/etc/cron.d/announce'):
            self.g.rm('/etc/cron.d/announce')

        # remove cdl-nc binary
        if self.g.exists('/root/cdl-nc'):
            self.g.rm('/root/cdl-nc')

        # remove custom sshd_config
        if self.g.exists('/etc/ssh/sshd_config'):
            self.g.rm('/etc/ssh/sshd_config')
        if self.g.exists('/etc/ssh/sshd_config.cdl'):
            self.g.mv('/etc/ssh/sshd_config.cdl', '/etc/ssh/sshd_config')

        # reset the service links
        runlevel = self.get_default_runlevel()

        for service in ["sshd", "crond"]:
            startuplink = self.get_service_runlevel_link(runlevel, service)
            if self.g.exists(startuplink):
                self.g.rm(startuplink)
            if self.g.exists(startuplink + ".cdl"):
                self.g.mv(startuplink + ".cdl", startuplink)

        self.guestfs_handle_cleanup()

    def generate_cdl(self, libvirt_xml):
        self.log.info("Generating CDL")

        self.collect_setup(libvirt_xml)

        output = ''
        try:
            self.libvirt_dom = self.libvirt_conn.defineXML(libvirt_xml)
            self.libvirt_dom.create()

            guestaddr = self.wait_for_guest_boot()

            data = subprocess.Popen(["ssh", "-i",
                                     self.cdl_tmp + '/id_rsa-cdl-gen',
                                     "-o", "StrictHostKeyChecking=no",
                                     "-o", "ConnectTimeout=5", guestaddr,
                                     'rpm -qa'], stdout=subprocess.PIPE).communicate()

            # FIXME: what if the output is blank?

            output = self.output_cdl_xml(data[0].split("\n"))

            # FIXME: should we try to do a graceful shutdown here?  At the very
            # least we should do a sync
            self.libvirt_dom.destroy()
        finally:
            self.collect_teardown(libvirt_xml)

        return output

def get_class(idl, config):
    update = idl.update()
    if update == "10" or update == "11" or update == "12" or update == "13" or update == "14":
        return FedoraGuest(idl, config, "virtio", True, "virtio", True)
    if update == "7" or update == "8" or update == "9":
        return FedoraGuest(idl, config, "rtl8139", False, None, False)
    raise Exception, "Unsupported Fedora update " + update
<|endoftext|>


<commit_before>from __future__ import unicode_literals

import os
import shutil
import logging
import tempfile
import subprocess

from django.db import migrations
from django.core.files.uploadedfile import UploadedFile

from django_irods.storage import IrodsStorage
from hs_core import hydroshare
from hs_core.hydroshare.utils import resource_modified
from hs_geo_raster_resource.models import RasterResource
from hs_geo_raster_resource import raster_meta_extract

def migrate_tif_file(apps, schema_editor):
    log = logging.getLogger()
    istorage = IrodsStorage()
    success = 0
    fail = []
    not_modify = []

    for res in RasterResource.objects.all():
        if res.files.all():
            # update metadata information
            try:
                # copy all the files of a resource to temp dir
                temp_dir = tempfile.mkdtemp()
                for res_file in res.files.all():
                    shutil.copy(res_file.resource_file.file.name,
                                os.path.join(temp_dir, os.path.basename(res_file.resource_file.name)))

                # update vrt file for single tif file
                if len(os.listdir(temp_dir)) == 2:
                    # create new vrt file
                    tif_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.tif' == f[-4:]].pop()
                    vrt_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.vrt' == f[-4:]].pop()

                    try:
                        with open(os.devnull, 'w') as fp:
                            subprocess.Popen(['gdal_translate', '-of', 'VRT', tif_file_path, vrt_file_path], stdout=fp, stderr=fp).wait()   # remember to add .wait()

                        # delete vrt res file
                        for f in res.files.all():
                            if 'vrt' == f.resource_file.name[-3:]:
                                f.resource_file.delete()
                                f.delete()

                        # add new vrt file to resource
                        new_file = UploadedFile(file=open(vrt_file_path, 'r'), name=os.path.basename(vrt_file_path))
                        hydroshare.add_resource_files(res.short_id, new_file)

                        # update the bag
                        bag_name = 'bags/{res_id}.zip'.format(res_id=res.short_id)
                        if istorage.exists(bag_name):
                            # delete the resource bag as the old bag is not valid
                            istorage.delete(bag_name)
                        resource_modified(res, res.creator)

                    except:
                        print 'fail to update VRT'

                # metadata extraction from temp dir
                ori_dir = os.getcwd()
                os.chdir(temp_dir)
                vrt_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.vrt' == os.path.splitext(f)[1]].pop()
                res_md_dict = raster_meta_extract.get_raster_meta_dict(vrt_file_path)
                os.chdir(ori_dir)
                shutil.rmtree(temp_dir)

                # update band metadata of raster resources
                is_modified = False
                if res_md_dict['band_info']:
                    for i, band_meta in res_md_dict['band_info'].items():
                        band_obj = res.metadata.bandInformation.filter(name='Band_{}'.format(str(i))).first()
                        if band_obj:
                            res.metadata.update_element('bandInformation',
                                                        band_obj.id,
                                                        maximumValue=band_meta['maximumValue'],
                                                        minimumValue=band_meta['minimumValue'],
                                                        noDataValue=band_meta['noDataValue'],
                                                        )
                            is_modified = True

                # resource modify update
                if is_modified:
                    bag_name = 'bags/{res_id}.zip'.format(res_id=res.short_id)
                    if istorage.exists(bag_name):
                        # delete the resource bag as the old bag is not valid
                        istorage.delete(bag_name)
                    resource_modified(res, res.creator)

                    success += 1
                    log.info('Raster metadata is updated successfully for resource:ID:{} '
                             'Title:{}'.format(res.short_id, res.metadata.title.value))
                else:
                    log.info('Raster metadata is not updated for resource:ID:{} '
                             'Title:{}'.format(res.short_id, res.metadata.title.value))
                    not_modify.append({res.short_id: res.metadata.title.value})

            except Exception as e:
                fail.append({res.short_id : res.metadata.title.value})
                log.info('Raster metadata update failed for resource:ID:{} '
                        'Title:{}'.format(res.short_id, res.metadata.title.value))

    print 'total resources: {}'.format(str(RasterResource.objects.all().count()))
    print 'success update resources: {}'.format(str(success))
    print 'failed update resources: {}'.format(str(fail))
    print 'not updated resources: {}'.format(str(not_modify))

def undo_migrate_tif_file(apps, schema_editor):

    # loop through all the band information and change the nodata value, min, max values as none
    for res in RasterResource.objects.all():
        try:
            for band_obj in res.metadata.bandInformation:
                res.metadata.update_element('bandInformation',
                                            band_obj.id,
                                            maximumValue=None,
                                            minimumValue=None,
                                            noDataValue=None,
                                            )
        except:
            pass

class Migration(migrations.Migration):

    dependencies = [
        ('hs_geo_raster_resource', '0005_auto_20160509_2116'),
    ]

    operations = [
        migrations.RunPython(code=migrate_tif_file, reverse_code=undo_migrate_tif_file),
    ]
<commit_msg>[#1162] update the vrt file contents for existing raster resources<commit_after>from __future__ import unicode_literals

import os
import shutil
import logging
import tempfile
import subprocess
import xml.etree.ElementTree as ET

from django.db import migrations
from django.core.files.uploadedfile import UploadedFile

from django_irods.storage import IrodsStorage
from hs_core import hydroshare
from hs_core.hydroshare.utils import resource_modified
from hs_geo_raster_resource.models import RasterResource
from hs_geo_raster_resource import raster_meta_extract

def migrate_tif_file(apps, schema_editor):
    log = logging.getLogger()
    istorage = IrodsStorage()
    success = 0
    fail = []
    not_modify = []

    for res in RasterResource.objects.all():
        if res.files.all():
            # update metadata information
            try:
                # copy all the files of a resource to temp dir
                temp_dir = tempfile.mkdtemp()
                for res_file in res.files.all():
                    shutil.copy(res_file.resource_file.file.name,
                                os.path.join(temp_dir, os.path.basename(res_file.resource_file.name)))

                # update vrt file for single tif file
                if len(os.listdir(temp_dir)) == 2:
                    # create new vrt file
                    tif_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.tif' == f[-4:]].pop()
                    vrt_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.vrt' == f[-4:]].pop()

                    try:
                        with open(os.devnull, 'w') as fp:
                            subprocess.Popen(['gdal_translate', '-of', 'VRT', tif_file_path, vrt_file_path], stdout=fp, stderr=fp).wait()   # remember to add .wait()

                        # modify the vrt file contents
                        tree = ET.parse(vrt_file_path)
                        root = tree.getroot()
                        for element in root.iter('SourceFilename'):
                            element.attrib['relativeToVRT'] = '1'
                        tree.write(vrt_file_path)

                        # delete vrt res file
                        for f in res.files.all():
                            if 'vrt' == f.resource_file.name[-3:]:
                                f.resource_file.delete()
                                f.delete()

                        # add new vrt file to resource
                        new_file = UploadedFile(file=open(vrt_file_path, 'r'), name=os.path.basename(vrt_file_path))
                        hydroshare.add_resource_files(res.short_id, new_file)

                        # update the bag
                        bag_name = 'bags/{res_id}.zip'.format(res_id=res.short_id)
                        if istorage.exists(bag_name):
                            # delete the resource bag as the old bag is not valid
                            istorage.delete(bag_name)
                        resource_modified(res, res.creator)

                    except:
                        print 'fail to update VRT'

                # metadata extraction from temp dir
                ori_dir = os.getcwd()
                os.chdir(temp_dir)
                vrt_file_path = [os.path.join(temp_dir, f) for f in os.listdir(temp_dir) if '.vrt' == os.path.splitext(f)[1]].pop()
                res_md_dict = raster_meta_extract.get_raster_meta_dict(vrt_file_path)
                os.chdir(ori_dir)
                shutil.rmtree(temp_dir)

                # update band metadata of raster resources
                is_modified = False
                if res_md_dict['band_info']:
                    for i, band_meta in res_md_dict['band_info'].items():
                        band_obj = res.metadata.bandInformation.filter(name='Band_{}'.format(str(i))).first()
                        if band_obj:
                            res.metadata.update_element('bandInformation',
                                                        band_obj.id,
                                                        maximumValue=band_meta['maximumValue'],
                                                        minimumValue=band_meta['minimumValue'],
                                                        noDataValue=band_meta['noDataValue'],
                                                        )
                            is_modified = True

                # resource modify update
                if is_modified:
                    bag_name = 'bags/{res_id}.zip'.format(res_id=res.short_id)
                    if istorage.exists(bag_name):
                        # delete the resource bag as the old bag is not valid
                        istorage.delete(bag_name)
                    resource_modified(res, res.creator)

                    success += 1
                    log.info('Raster metadata is updated successfully for resource:ID:{} '
                             'Title:{}'.format(res.short_id, res.metadata.title.value))
                else:
                    log.info('Raster metadata is not updated for resource:ID:{} '
                             'Title:{}'.format(res.short_id, res.metadata.title.value))
                    not_modify.append({res.short_id: res.metadata.title.value})

            except Exception as e:
                fail.append({res.short_id : res.metadata.title.value})
                log.info('Raster metadata update failed for resource:ID:{} '
                        'Title:{}'.format(res.short_id, res.metadata.title.value))

    print 'total resources: {}'.format(str(RasterResource.objects.all().count()))
    print 'success update resources: {}'.format(str(success))
    print 'failed update resources: {}'.format(str(fail))
    print 'not updated resources: {}'.format(str(not_modify))

def undo_migrate_tif_file(apps, schema_editor):

    # loop through all the band information and change the nodata value, min, max values as none
    for res in RasterResource.objects.all():
        try:
            for band_obj in res.metadata.bandInformation:
                res.metadata.update_element('bandInformation',
                                            band_obj.id,
                                            maximumValue=None,
                                            minimumValue=None,
                                            noDataValue=None,
                                            )
        except:
            pass

class Migration(migrations.Migration):

    dependencies = [
        ('hs_geo_raster_resource', '0005_auto_20160509_2116'),
    ]

    operations = [
        migrations.RunPython(code=migrate_tif_file, reverse_code=undo_migrate_tif_file),
    ]
<|endoftext|>