<commit_before>import random
#Eat at a French restaurant if you are hungry. You have the choice between snails and foie gras. If you are not hunry, you will leave.

def hunger(decision):
	if decision == "yes":
		print "Welcome to Le Boudoir! We have two exquisite items on the menu today!"
		choice = raw_input("Choose True or False to determine what you will eat: ")
		food(choice)
	elif decision =="no":
		print "That's too bad."

def food(decision2):
	if decision2 == "True":
		print "Wait a moment please; Your escargots will be out of the oven sizzling with butter in a few minutes."
	elif decision2 == "False":
		print "Your foie gras will be ready in at least 5 minutes!"

def drink(drinkoption):
	if drinkoption == "yes":
		print "We have surprise drinks today."
	elif drinkoption == "no":
		print "That's too bad."

def drinkchoice(surprise):
    surprise = int(raw_input("Choose a number between 1 and 10: "))
    if surprise > random.randint(1, 10):
        print "You chose champagne!"
    else:
        print "You chose wine!"

def main():
    decision1 = raw_input("Are you hungry?: ")
    hunger(decision1)
    decision3 = raw_input("Would u like something to drink?: ")
    drink(decision3)
    drinkchoice(surprise)
    surprise = int(raw_input("Choose a number between 1 and 10: "))
main()
<commit_msg>Nearly done<commit_after>import random
#Eat at a French restaurant if you are hungry. You have the choice between snails and foie gras. If you are not hunry, you will leave.

def getready(outfit):
    print "Let's get ready for dinner! Choose something to wear!"
    if outfit == "Parrot":
        print "You chose blue colored dress pants and a bright yellow suit."
    elif outfit == "Robin":
        print "You're wearing all red."
    else:
        outfit == "Parrot"
        print "You're going to dinner in a clown costume."

def footwear(shoes):
    if shoes == "Brazil":
        print "You chose high heels."
    elif shoes == "Canada":
        print "You chose loafers."
    else:
        shoes == "Thailand"
        print "You chose flip flops."

def hunger(decision):
    if decision == "yes":
        print "Welcome to Le Boudoir! We have two exquisite items on the menu today! We will not show you the menu."
        choice = raw_input("Choose True or False to determine what you will eat: ")
        food(choice)
    else:
        decision =="no"
        print "That's too bad."

def food(decision2):
    if decision2 == "True":
        print "Wait a moment please; Your escargots will be out of the oven sizzling with butter in a few minutes."
    else:
        decision2 == "False"
        print "Your foie gras will be ready in at least 5 minutes!"

def drink(drinkoption):
    if drinkoption == "yes":
        print "We have surprise drinks today."
    else:
        drinkoption == "no"
        print "That's too bad."

def drinkchoice(surprise):
    if surprise > random.randint(1, 10):
        print "You chose champagne!"
    else:
        print "You chose wine!"

def output(outfit, shoes):
    return """
You have chosen to wear the {} outfit and shoes from {}.
Hello valued customer!
You have blindly chosen your meal!
Bon appetit!
""".format(outfit, shoes)

def main():
    outfit = raw_input("Choose one of these birds to determine your outfit 'Peacock, Robin, Parrot': ")
    getready(outfit)
    shoes = raw_input("Choose one of these countries to determine your footwear 'Brazil, Canada, Thailand': ")
    footwear(shoes)
    decision = raw_input("Are you hungry?: ")
    hunger(decision)
    decision3 = raw_input("Would u like something to drink?: ")
    drink(decision3)
    surprise = int(raw_input("Choose a number between 1 and 10: "))
    drinkchoice(surprise)
    print output(outfit, shoes)
main()
<|endoftext|>


<commit_before># coding: utf8
# Copyright (c) 2007-2011 gocept gmbh & co. kg
# See also LICENSE.txt

from zeit.cms.i18n import MessageFactory as _
import PIL.Image
import zc.form.field
import zc.sourcefactory.basic
import zeit.cms.content.contentsource
import zeit.cms.content.interfaces
import zeit.cms.interfaces
import zeit.cms.repository.interfaces
import zeit.cms.workingcopy.interfaces
import zope.file.interfaces
import zope.schema

IMAGE_NAMESPACE = 'http://namespaces.zeit.de/CMS/image'

class ImageProcessingError(TypeError):
    """An error raised it's not possible to process the Image."""

class IImageType(zeit.cms.interfaces.ICMSContentType):
    """The interface of image interfaces."""

class AlignmentSource(zeit.cms.content.sources.SimpleFixedValueSource):

    values = (u'left', u'center', u'right')

class IImageMetadata(zope.interface.Interface):

    title = zope.schema.TextLine(
        title=_("Image title"),
        default=u'',
        required=False)

    year = zope.schema.Int(
        title=_("Year"),
        min=1900,
        max=2100,
        required=False)

    volume = zope.schema.Int(
        title=_("Volume"),
        min=1,
        max=53,
        required=False)

    copyrights = zope.schema.Tuple(
        title=_("Copyrights"),
        default=((u'©', None),),
        missing_value=(),
        required=False,
        value_type=zc.form.field.Combination(
            (zope.schema.TextLine(
                title=_("Copyright"),
                min_length=3,
                required=True),
            zope.schema.URI(
                title=_('Link'),
                description=_('Link to copyright holder'),
                required=False))))

    alt = zope.schema.TextLine(
        title=_("Alternative text"),
        description=_("Enter a textual description of the image"),
        default=u'',
        required=False)

    caption = zeit.cms.content.field.XMLSnippet(
        title=_("Image sub text"),
        default=u'',
        missing_value=u'',
        required=False)

    links_to = zope.schema.URI(
        title=_('Links to'),
        description=_('Enter a URL this image should link to.'),
        required=False)

    alignment = zope.schema.Choice(
        title=_('Alignment'),
        source=AlignmentSource(),
        default='left')

    acquire_metadata = zope.schema.Bool(
        title=u'True if metadata should be acquired from the parent.')

class IImage(zeit.cms.interfaces.IAsset,
             zeit.cms.repository.interfaces.IDAVContent,
             zope.file.interfaces.IFile):
    """Image."""

    def getImageSize():
        """return tuple (width, heigth) of image."""

class ITransform(zope.interface.Interface):

    def thumbnail(width, height, filter=PIL.Image.ANTIALIAS):
        """Create a thumbnail version of the image.

        returns IImage object.
        """

    def resize(width=None, height=None, filter=PIL.Image.ANTIALIAS):
        """Create a resized version of the image.

        returns IImage object.
        raises TypeError of both width and height are ommited.

        """

class IPersistentThumbnail(IImage):
    """Persistent thumbnail version of an image."""

class IThumbnailFolder(zope.interface.Interface):
    """The folder where to find thumbnails for an image."""

class MasterImageSource(
    zc.sourcefactory.contextual.BasicContextualSourceFactory):

    def getValues(self, context):
        repository = zope.component.getUtility(
            zeit.cms.repository.interfaces.IRepository)
        for name in repository.getContent(context.uniqueId):
            yield name

class IImageGroup(zeit.cms.interfaces.IAsset,
                  zeit.cms.repository.interfaces.IDAVContent):
    """An image group groups images with the same motif together."""

    master_image = zope.schema.Choice(
        title=_('Master image'),
        source=MasterImageSource(),
        required=False)

class IRepositoryImageGroup(IImageGroup,
                            zeit.cms.repository.interfaces.ICollection):
    """An image group in the repository.  It contains images.

    """

class ILocalImageGroup(IImageGroup,
                       zeit.cms.workingcopy.interfaces.ILocalContent):
    """Local version of an image group.

    The local version only holds the metadata, therefore it is not a container.
    """

class IImageSource(zeit.cms.content.interfaces.ICMSContentSource):
    """A source for images."""

class ImageSource(zeit.cms.content.contentsource.CMSContentSource):

    zope.interface.implements(IImageSource)
    check_interfaces = IImageType
    name = 'images'

imageSource = ImageSource()

class IImages(zope.interface.Interface):
    """An object which references images."""

    images = zope.schema.Tuple(
        title=_('Images'),
        required=False,
        default=(),
        value_type=zope.schema.Choice(
            source=imageSource))

class IReferences(zope.interface.Interface):

    references = zope.schema.Tuple(
        title=_('Objects using this image'),
        readonly=True,
        value_type=zope.schema.Choice(
            source=zeit.cms.content.contentsource.cmsContentSource))

class IMasterImage(zope.interface.Interface):
    """Marks an image in an image group as master for the other images."""
<commit_msg>Re #11568: Give specialized description for the reference field<commit_after># coding: utf8
# Copyright (c) 2007-2011 gocept gmbh & co. kg
# See also LICENSE.txt

from zeit.cms.i18n import MessageFactory as _
import PIL.Image
import zc.form.field
import zc.sourcefactory.basic
import zeit.cms.content.contentsource
import zeit.cms.content.interfaces
import zeit.cms.interfaces
import zeit.cms.repository.interfaces
import zeit.cms.workingcopy.interfaces
import zope.file.interfaces
import zope.schema

IMAGE_NAMESPACE = 'http://namespaces.zeit.de/CMS/image'

class ImageProcessingError(TypeError):
    """An error raised it's not possible to process the Image."""

class IImageType(zeit.cms.interfaces.ICMSContentType):
    """The interface of image interfaces."""

class AlignmentSource(zeit.cms.content.sources.SimpleFixedValueSource):

    values = (u'left', u'center', u'right')

class IImageMetadata(zope.interface.Interface):

    title = zope.schema.TextLine(
        title=_("Image title"),
        default=u'',
        required=False)

    year = zope.schema.Int(
        title=_("Year"),
        min=1900,
        max=2100,
        required=False)

    volume = zope.schema.Int(
        title=_("Volume"),
        min=1,
        max=53,
        required=False)

    copyrights = zope.schema.Tuple(
        title=_("Copyrights"),
        default=((u'©', None),),
        missing_value=(),
        required=False,
        value_type=zc.form.field.Combination(
            (zope.schema.TextLine(
                title=_("Copyright"),
                min_length=3,
                required=True),
            zope.schema.URI(
                title=_('Link'),
                description=_('Link to copyright holder'),
                required=False))))

    alt = zope.schema.TextLine(
        title=_("Alternative text"),
        description=_("Enter a textual description of the image"),
        default=u'',
        required=False)

    caption = zeit.cms.content.field.XMLSnippet(
        title=_("Image sub text"),
        default=u'',
        missing_value=u'',
        required=False)

    links_to = zope.schema.URI(
        title=_('Links to'),
        description=_('Enter a URL this image should link to.'),
        required=False)

    alignment = zope.schema.Choice(
        title=_('Alignment'),
        source=AlignmentSource(),
        default='left')

    acquire_metadata = zope.schema.Bool(
        title=u'True if metadata should be acquired from the parent.')

class IImage(zeit.cms.interfaces.IAsset,
             zeit.cms.repository.interfaces.IDAVContent,
             zope.file.interfaces.IFile):
    """Image."""

    def getImageSize():
        """return tuple (width, heigth) of image."""

class ITransform(zope.interface.Interface):

    def thumbnail(width, height, filter=PIL.Image.ANTIALIAS):
        """Create a thumbnail version of the image.

        returns IImage object.
        """

    def resize(width=None, height=None, filter=PIL.Image.ANTIALIAS):
        """Create a resized version of the image.

        returns IImage object.
        raises TypeError of both width and height are ommited.

        """

class IPersistentThumbnail(IImage):
    """Persistent thumbnail version of an image."""

class IThumbnailFolder(zope.interface.Interface):
    """The folder where to find thumbnails for an image."""

class MasterImageSource(
    zc.sourcefactory.contextual.BasicContextualSourceFactory):

    def getValues(self, context):
        repository = zope.component.getUtility(
            zeit.cms.repository.interfaces.IRepository)
        for name in repository.getContent(context.uniqueId):
            yield name

class IImageGroup(zeit.cms.interfaces.IAsset,
                  zeit.cms.repository.interfaces.IDAVContent):
    """An image group groups images with the same motif together."""

    master_image = zope.schema.Choice(
        title=_('Master image'),
        source=MasterImageSource(),
        required=False)

class IRepositoryImageGroup(IImageGroup,
                            zeit.cms.repository.interfaces.ICollection):
    """An image group in the repository.  It contains images.

    """

class ILocalImageGroup(IImageGroup,
                       zeit.cms.workingcopy.interfaces.ILocalContent):
    """Local version of an image group.

    The local version only holds the metadata, therefore it is not a container.
    """

class IImageSource(zeit.cms.content.interfaces.ICMSContentSource):
    """A source for images."""

class ImageSource(zeit.cms.content.contentsource.CMSContentSource):

    zope.interface.implements(IImageSource)
    check_interfaces = IImageType
    name = 'images'

imageSource = ImageSource()

class IImages(zope.interface.Interface):
    """An object which references images."""

    images = zope.schema.Tuple(
        title=_('Images'),
        description=_("Drag an image or image group here"),
        required=False,
        default=(),
        value_type=zope.schema.Choice(
            source=imageSource))

class IReferences(zope.interface.Interface):

    references = zope.schema.Tuple(
        title=_('Objects using this image'),
        readonly=True,
        value_type=zope.schema.Choice(
            source=zeit.cms.content.contentsource.cmsContentSource))

class IMasterImage(zope.interface.Interface):
    """Marks an image in an image group as master for the other images."""
<|endoftext|>


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

"""Module for the GSoC profile page.
"""

__authors__ = [
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
  ]

from google.appengine.api import users
from google.appengine.ext.db import djangoforms

from django.core.urlresolvers import reverse
from django.conf.urls.defaults import url

from soc.logic import dicts
from soc.views import forms
from soc.views import template

from soc.models.user import User
from soc.models.role import Profile
from soc.models.role import StudentInfo

from soc.modules.gsoc.views.base import RequestHandler
from soc.modules.gsoc.views.helper import access_checker
from soc.modules.gsoc.views.helper import url_patterns

class EmptyForm(forms.ModelForm):
  pass

class UserForm(forms.ModelForm):
  """
  """

  class Meta:
    model = User
    fields = ['link_id', 'name']

class ProfileForm(forms.ModelForm):
  """Django form for profile page.
  """

  class Meta:
    model = Profile
    exclude = ['link_id', 'user', 'scope', 'mentor_for', 'org_admin_for',
               'student_info', 'agreed_to_tos_on', 'scope_path', 'status',
               'name_on_documents']
    widgets = forms.choiceWidgets(Profile,
        ['res_country', 'ship_country',
         'tshirt_style', 'tshirt_size', 'gender'])

class StudentInfoForm(forms.ModelForm):
  """
  """

class ProfilePage(RequestHandler):
  """View for the participant profile.
  """

  def djangoURLPatterns(self):
    return [
        url(r'^gsoc/profile/%s$' % url_patterns.PROGRAM,
         self, name='edit_gsoc_profile'),
        url(r'^gsoc/profile/%s$' % url_patterns.PROFILE,
         self, name='create_gsoc_profile'),
    ]

  def checkAccess(self):
    check = access_checker.AccessChecker(self.data)
    check.isLoggedIn()
    check.isProgramActive()

    if 'role' in self.data.kwargs:
      role = self.data.kwargs['role']
      if role == 'student':
        check.isNotParticipatingInProgram()
        check.isActivePeriod('student_signup')
    else:
      check.isRoleActive()

  def templatePath(self):
    return 'v2/modules/gsoc/profile/base.html'

  def context(self):
    return {
        'page_name': 'Register',
        'user_form': UserForm(self.data.GET).render(),
        'profile_form': ProfileForm(self.data.GET).render(),
    }

  def validate(self):
    dirty = []
    if self.data.user:
      user_form = EmptyForm(self.data.POST)
    else:
      user_form = UserForm(self.data.POST)

      if user_form.is_valid():
        key_name = user_form.cleaned_data['link_id']
        account = users.get_current_user()
        user_form.cleaned_data['account'] = account
        user_form.cleaned_data['user_id'] = account.user_id()
        self.data.user = user_form.create(commit=False, key_name=key_name)
        dirty.append(self.data.user)

    profile_form = ProfileForm(self.data.POST, instance=self.data.role)
    if profile_form.is_valid():
      key_name = '%(sponsor)s/%(program)s/%(link_id)s' % self.data.POST
      profile = form.create(commit=False, key_name=key_name, parent=user)
      dirty.append(profile)

    if self.data.kwargs.get('role') == 'student':
      student_form = StudentInfoForm(self.data.POST)
      if self.data.role and student_form.is_valid():
        key_name = self.data.role.key().name()
        student_info = form.create(commit=False, key_name=key_name, parent=profile)
        self.data.role.student_info = student_info
        dirty.append(student_info)
    else:
      student_form = EmptyForm()

    if user_form.is_valid() and profile_form.is_valid() and student_form.is_valid():
      db.run_in_transaction(db.put, dirty)
      return True
    else:
      return False

  def post(self):
    """Handler for HTTP POST request.
    """
    if self.validate():
      kwargs = dicts.filter(self.data.kwargs, ['sponsor', 'program'])
      self.redirect(reverse('edit_gsoc_profile', kwargs=kwargs))
    else:
      self.get()
<commit_msg>Working profile saving<commit_after>#!/usr/bin/env python2.5
#
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module for the GSoC profile page.
"""

__authors__ = [
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
  ]

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

from django.core.urlresolvers import reverse
from django.conf.urls.defaults import url

from soc.logic import dicts
from soc.views import forms
from soc.views import template

from soc.models.user import User
from soc.models.role import Profile
from soc.models.role import StudentInfo

from soc.modules.gsoc.views.base import RequestHandler
from soc.modules.gsoc.views.helper import access_checker
from soc.modules.gsoc.views.helper import url_patterns

class EmptyForm(forms.ModelForm):
  pass

class UserForm(forms.ModelForm):
  """
  """

  class Meta:
    model = User
    fields = ['link_id', 'name']

class ProfileForm(forms.ModelForm):
  """Django form for profile page.
  """

  class Meta:
    model = Profile
    exclude = ['link_id', 'user', 'scope', 'mentor_for', 'org_admin_for',
               'student_info', 'agreed_to_tos_on', 'scope_path', 'status',
               'name_on_documents']
    widgets = forms.choiceWidgets(Profile,
        ['res_country', 'ship_country',
         'tshirt_style', 'tshirt_size', 'gender'])

class StudentInfoForm(forms.ModelForm):
  """
  """

class ProfilePage(RequestHandler):
  """View for the participant profile.
  """

  def djangoURLPatterns(self):
    return [
        url(r'^gsoc/profile/%s$' % url_patterns.PROGRAM,
         self, name='edit_gsoc_profile'),
        url(r'^gsoc/profile/%s$' % url_patterns.PROFILE,
         self, name='create_gsoc_profile'),
    ]

  def checkAccess(self):
    check = access_checker.AccessChecker(self.data)
    check.isLoggedIn()
    check.isProgramActive()

    if 'role' in self.data.kwargs:
      role = self.data.kwargs['role']
      if role == 'student':
        check.isNotParticipatingInProgram()
        check.isActivePeriod('student_signup')
    else:
      check.isRoleActive()

  def templatePath(self):
    return 'v2/modules/gsoc/profile/base.html'

  def context(self):
    if self.data.request.method == 'POST':
      user_form = UserForm(self.data.POST)
      profile_form = ProfileForm(self.data.POST)
    else:
      user_form = UserForm()
      profile_form = ProfileForm()
    return {
        'page_name': 'Register',
        'user_form': user_form.render(),
        'profile_form': profile_form.render(),
    }

  def validate(self):
    dirty = []
    if self.data.user:
      user_form = EmptyForm(self.data.POST)
    else:
      user_form = UserForm(self.data.POST)

      if user_form.is_valid():
        key_name = user_form.cleaned_data['link_id']
        account = users.get_current_user()
        user_form.cleaned_data['account'] = account
        user_form.cleaned_data['user_id'] = account.user_id()
        self.data.user = user_form.create(commit=False, key_name=key_name)
        dirty.append(self.data.user)

    profile_form = ProfileForm(self.data.POST, instance=self.data.role)
    if profile_form.is_valid():
      key_name = '%s/%s' % (self.data.program.key().name(),
                            self.data.user.link_id)
      profile_form.cleaned_data['link_id'] = self.data.user.link_id
      profile_form.cleaned_data['user'] = self.data.user
      profile = profile_form.create(commit=False, key_name=key_name, parent=self.data.user)
      dirty.append(profile)

    if self.data.kwargs.get('role') == 'student':
      student_form = StudentInfoForm(self.data.POST)
      if self.data.role and student_form.is_valid():
        key_name = self.data.role.key().name()
        student_info = form.create(commit=False, key_name=key_name, parent=profile)
        self.data.role.student_info = student_info
        dirty.append(student_info)
    else:
      student_form = EmptyForm()

    if user_form.is_valid() and profile_form.is_valid() and student_form.is_valid():
      db.run_in_transaction(db.put, dirty)
      return True
    else:
      return False

  def post(self):
    """Handler for HTTP POST request.
    """
    if self.validate():
      kwargs = dicts.filter(self.data.kwargs, ['sponsor', 'program'])
      self.redirect(reverse('edit_gsoc_profile', kwargs=kwargs))
    else:
      self.get()
<|endoftext|>


<commit_before>from fuel.converters.ilsvrc2010 import IMAGE_TARS
from fuel.downloaders.base import default_downloader

def fill_subparser(subparser):
    """Sets up a subparser to download the ILSVRC2010 dataset files.

    Note that you will need to use `--url-prefix` to download the
    non-public files (namely, the TARs of images). This is a single
    prefix that is common to all distributed files, which you can
    obtain by registering at the ImageNet website [DOWNLOAD].

    Note that these files are quite large and you may be better off
    simply downloading them separately and running ``fuel-convert``.

    .. [DOWNLOAD] http://www.image-net.org/download-images

    Parameters
    ----------
    subparser : :class:`argparse.ArgumentParser`
        Subparser handling the `mnist` command.

    """
    urls = [
        ('http://www.image-net.org/challenges/LSVRC/2010/'
         'ILSVRC2010_test_ground_truth.txt'),
        ('http://www.image-net.org/challenges/LSVRC/2010/',
         'download/ILSVRC2010_devkit-1.0.tar.gz'),
    ] + ([None] * len(IMAGE_TARS))
    filenames = [None, None] + list(IMAGE_TARS)
    subparser.set_defaults(
        func=default_downloader, urls=urls, filenames=filenames)
    subparser.add_argument('--url-prefix', type=str, default=None,
                           help="URL prefix to prepend to the filenames of "
                                "non-public files, in order to download them. "
                                "Be sure to include the trailing slash.")
<commit_msg>Fix copypasta mistake.<commit_after>from fuel.converters.ilsvrc2010 import IMAGE_TARS
from fuel.downloaders.base import default_downloader

def fill_subparser(subparser):
    """Sets up a subparser to download the ILSVRC2010 dataset files.

    Note that you will need to use `--url-prefix` to download the
    non-public files (namely, the TARs of images). This is a single
    prefix that is common to all distributed files, which you can
    obtain by registering at the ImageNet website [DOWNLOAD].

    Note that these files are quite large and you may be better off
    simply downloading them separately and running ``fuel-convert``.

    .. [DOWNLOAD] http://www.image-net.org/download-images

    Parameters
    ----------
    subparser : :class:`argparse.ArgumentParser`
        Subparser handling the `ilsvrc2010` command.

    """
    urls = [
        ('http://www.image-net.org/challenges/LSVRC/2010/'
         'ILSVRC2010_test_ground_truth.txt'),
        ('http://www.image-net.org/challenges/LSVRC/2010/',
         'download/ILSVRC2010_devkit-1.0.tar.gz'),
    ] + ([None] * len(IMAGE_TARS))
    filenames = [None, None] + list(IMAGE_TARS)
    subparser.set_defaults(
        func=default_downloader, urls=urls, filenames=filenames)
    subparser.add_argument('--url-prefix', type=str, default=None,
                           help="URL prefix to prepend to the filenames of "
                                "non-public files, in order to download them. "
                                "Be sure to include the trailing slash.")
<|endoftext|>


<commit_before>"""Copyright (c) 2010-2012 David Rio Vierra

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""

import sys
import os
import json
import glob
import shutil

def win32_utf8_argv():
    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of UTF-8
    strings.

    Versions 2.5 and older of Python don't support Unicode in sys.argv on
    Windows, with the underlying Windows API instead replacing multi-byte
    characters with '?'.

    Returns None on failure.

    Example usage:

    >>> def main(argv=None):
    ...    if argv is None:
    ...        argv = win32_utf8_argv() or sys.argv
    ...
    """

    try:
        from ctypes import POINTER, byref, cdll, c_int, windll
        from ctypes.wintypes import LPCWSTR, LPWSTR

        GetCommandLineW = cdll.kernel32.GetCommandLineW
        GetCommandLineW.argtypes = []
        GetCommandLineW.restype = LPCWSTR

        CommandLineToArgvW = windll.shell32.CommandLineToArgvW
        CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
        CommandLineToArgvW.restype = POINTER(LPWSTR)

        cmd = GetCommandLineW()
        argc = c_int(0)
        argv = CommandLineToArgvW(cmd, byref(argc))
        if argc.value > 0:
            # # Remove Python executable if present
            #            if argc.value - len(sys.argv) == 1:
            #                start = 1
            #            else:
            #                start = 0
            return [argv[i] for i in
                    xrange(0, argc.value)]
    except Exception:
        pass

def getDataDir():
    """Returns the folder where the executable is located."""
    if sys.platform == "win32":
        def fsdecode(x):
            return x.decode(sys.getfilesystemencoding())

        argzero = fsdecode(os.path.abspath(sys.argv[0]))
        if hasattr(sys, 'frozen'):
            dataDir = os.path.dirname(fsdecode(sys.executable))
        else:
            dataDir = os.path.dirname(argzero)
        dataDir = os.getcwdu()
    else:
        dataDir = os.getcwdu()

    return dataDir

def win32_appdata():
    # try to use win32 api to get the AppData folder since python doesn't populate os.environ with unicode strings.

    try:
        import win32com.client

        objShell = win32com.client.Dispatch("WScript.Shell")
        return objShell.SpecialFolders("AppData")
    except Exception, e:
        print "Error while getting AppData folder using WScript.Shell.SpecialFolders: {0!r}".format(e)
        try:
            from win32com.shell import shell, shellcon

            return shell.SHGetPathFromIDListEx(
                shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA)
            )
        except Exception, e:
            print "Error while getting AppData folder using SHGetSpecialFolderLocation: {0!r}".format(e)

            return os.environ['APPDATA'].decode(sys.getfilesystemencoding())

def getMinecraftProfileJSON():
    """Returns a dictionary object with the minecraft profile information"""
    if os.path.isfile(os.path.join(getMinecraftLauncherDirectory(), "launcher_profiles.json")):
        try:
            with open(os.path.join(getMinecraftLauncherDirectory(), "launcher_profiles.json")) as jsonString:
                minecraftProfilesJSON = json.load(jsonString)
            return minecraftProfilesJSON
        except:
            return None

def getMinecraftProfileDirectory(profileName):
    """Returns the path to the sent minecraft profile directory"""
    try:
        profileDir = getMinecraftProfileJSON()['profiles'][profileName][
            'gameDir']  # profileDir update to correct location.
        return profileDir
    except:
        return os.path.join(getMinecraftLauncherDirectory())

def getMinecraftLauncherDirectory():
    """Returns the /minecraft directory, note: may not contain the /saves folder!"""
    if sys.platform == "win32":
        return os.path.join(win32_appdata(), ".minecraft")
    elif sys.platform == "darwin":
        return os.path.expanduser("~/Library/Application Support/minecraft")
    else:
        return os.path.expanduser("~/.minecraft")

def getDocumentsFolder():
    docsFolder = None

    if sys.platform == "win32":
        try:
            import win32com.client
            objShell = win32com.client.Dispatch("WScript.Shell")
            docsFolder = objShell.SpecialFolders("MyDocuments")

        except Exception, e:
            print e
            try:
                docsFolder = shell.SHGetFolderPath(0, shellcon.CSIDL_MYDOCUMENTS, 0, 0)
            except Exception, e:
                userprofile = os.environ['USERPROFILE'].decode(sys.getfilesystemencoding())
                docsFolder = os.path.join(userprofile, "Documents")

    elif sys.platform == "darwin":
        docsFolder = os.path.expanduser("~/Documents")
    else:
        docsFolder = os.path.expanduser("~/.mcedit")
    try:
        os.mkdir(docsFolder)
    except:
        pass

    return docsFolder

def getSelectedProfile():
    try:
        selectedProfile = getMinecraftProfileJSON()['selectedProfile']
        return selectedProfile
    except:
        return None

minecraftSaveFileDir = os.path.join(getMinecraftProfileDirectory(getSelectedProfile()), "saves")
ini = u"mcedit.ini"
cache = u"usercache.json"

parentDir = os.path.dirname(getDataDir())
docsFolder = os.path.join(getDocumentsFolder(),'MCEdit')

if sys.platform != "darwin":

    portableConfigFilePath = os.path.join(parentDir, ini)
    portableCacheFilePath = os.path.join(parentDir, cache)
    portableGenericSupportPath = os.path.join(parentDir)
    portableSchematicsDir = os.path.join(parentDir, u"Schematics")
    portableJarStorageDir = os.path.join(parentDir, u"ServerJarStorage")
    portableFiltersDir = os.path.join(parentDir, u"Filters")
    if not os.path.exists(parentDir):
        os.makedirs(parentDir)

    fixedCacheFilePath = os.path.join(docsFolder, cache)
    fixedConfigFilePath = os.path.join(docsFolder, ini)
    fixedGenericSupportPath = os.path.join(docsFolder)
    fixedSchematicsDir = os.path.join(docsFolder, u"Schematics")
    fixedJarStorageDir = os.path.join(docsFolder, u"ServerJarStorage")
    fixedFiltersDir = os.path.join(docsFolder, u"Filters")
    if not os.path.exists(docsFolder):
        os.makedirs(docsFolder)

def goPortable():
    if sys.platform == "darwin":
        return False
    global configFilePath, schematicsDir, filtersDir, portable, cacheDir

    if os.path.exists(fixedSchematicsDir):
        move_displace(fixedSchematicsDir, portableSchematicsDir)
    if os.path.exists(fixedConfigFilePath):
        move_displace(fixedConfigFilePath, portableConfigFilePath)
    if os.path.exists(fixedFiltersDir):
        move_displace(fixedFiltersDir, portableFiltersDir)
    if os.path.exists(fixedCacheFilePath):
        move_displace(fixedCacheFilePath, portableCacheFilePath)
    if os.path.exists(fixedJarStorageDir):
        move_displace(fixedJarStorageDir, portableJarStorageDir)

    schematicsDir = portableSchematicsDir
    configFilePath = portableConfigFilePath
    filtersDir = portableFiltersDir
    cacheDir = portableGenericSupportPath
    portable = True
    return True

def move_displace(src, dst):
    dstFolder = os.path.basename(os.path.dirname(dst))
    if not os.path.exists(dst):

        print "Moving {0} to {1}".format(os.path.basename(src), dstFolder)
        shutil.move(src, dst)
    else:
        olddst = dst + ".old"
        i = 0
        while os.path.exists(olddst):
            olddst = dst + ".old" + str(i)
            i += 1

        print "{0} already found in {1}! Renamed it to {2}.".format(os.path.basename(src), dstFolder, dst)
        os.rename(dst, olddst)
        shutil.move(src, dst)
    return True

def goFixed():
    if sys.platform == "darwin":
        return False
    global configFilePath, schematicsDir, filtersDir, portable, cacheDir

    if os.path.exists(portableSchematicsDir):
        move_displace(portableSchematicsDir, fixedSchematicsDir)
    if os.path.exists(portableConfigFilePath):
        move_displace(portableConfigFilePath, fixedConfigFilePath)
    if os.path.exists(portableFiltersDir):
        move_displace(portableFiltersDir, fixedFiltersDir)
    if os.path.exists(portableCacheFilePath):
        move_displace(portableCacheFilePath, fixedCacheFilePath)
    if os.path.exists(portableJarStorageDir):
        move_displace(portableJarStorageDir, fixedJarStorageDir)

    schematicsDir = fixedSchematicsDir
    configFilePath = fixedConfigFilePath
    filtersDir = fixedFiltersDir
    cacheDir = fixedGenericSupportPath
    portable = False

def portableConfigExists():
    if sys.platform == "darwin":
        return False
    # Check for files at portable locations. Cannot be Mac because config doesn't move
    return (os.path.exists(portableConfigFilePath) or not os.path.exists(fixedConfigFilePath))

if portableConfigExists() and not sys.platform == "darwin":
    print "Running in portable mode. Support files are stored next to the MCEdit directory."
    portable = True
    schematicsDir = portableSchematicsDir
    configFilePath = portableConfigFilePath
    filtersDir = portableFiltersDir
    JarStorageDir = portableJarStorageDir
    genericSupportDir = portableGenericSupportPath

else:
    print "Running in fixed mode. Support files are in your " + (
    sys.platform == "darwin" and "App Support Folder (Available from the main menu of MCEdit)" or "Documents folder.")
    if not sys.platform == "darwin":
        schematicsDir = fixedSchematicsDir
        configFilePath = fixedConfigFilePath
        filtersDir = fixedFiltersDir
        JarStorageDir = fixedJarStorageDir
        genericSupportDir = fixedGenericSupportPath
    portable = False

if portable:
    serverJarStorageDir = portableJarStorageDir
#    ServerJarStorage.defaultCacheDir = serverJarStorageDir
#    jarStorage = ServerJarStorage(serverJarStorageDir)
#else:
    serverJarStorageDir = fixedJarStorageDir

def getAllFilters(filters_dir):
    return glob.glob(filters_dir+"/*.py")

def getCacheDir():
    """Returns the path to the cache folder. This folder is the Application Support folder on OS X, and the Documents Folder on Windows."""
    if sys.platform == "win32":
        return genericSupportDir
    elif sys.platform == "darwin":
        return os.path.expanduser("~/Library/Application Support/pymclevel")
    else:
        return os.path.expanduser("~/.pymclevel")

if sys.platform == "darwin":
    configFilePath = os.path.expanduser("~/Library/Preferences/mcedit.ini")
    schematicsDir = os.path.join(getCacheDir(), u"Schematics")
    filtersDir = os.path.join(getCacheDir(), u"Filters")
    if not os.path.exists(getCacheDir()):
        os.makedirs(getCacheDir())

# Create pymclevel folder as needed
if not os.path.exists(getCacheDir()):
    os.makedirs(getCacheDir())
# set userCachePath
userCachePath = os.path.join(getCacheDir(),'usercache.json')
# Make sure it exists
try:
    if not os.path.exists(userCachePath):
        f = open(userCachePath,'w')
        f.write('{}')
        f.close()
except:
    print "Unable to make usercache.json at {}".format(userCachePath)<commit_msg>Linux folder edit<commit_after>"""Copyright (c) 2010-2012 David Rio Vierra

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE."""

import sys
import os
import json
import glob
import shutil

def win32_utf8_argv():
    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of UTF-8
    strings.

    Versions 2.5 and older of Python don't support Unicode in sys.argv on
    Windows, with the underlying Windows API instead replacing multi-byte
    characters with '?'.

    Returns None on failure.

    Example usage:

    >>> def main(argv=None):
    ...    if argv is None:
    ...        argv = win32_utf8_argv() or sys.argv
    ...
    """

    try:
        from ctypes import POINTER, byref, cdll, c_int, windll
        from ctypes.wintypes import LPCWSTR, LPWSTR

        GetCommandLineW = cdll.kernel32.GetCommandLineW
        GetCommandLineW.argtypes = []
        GetCommandLineW.restype = LPCWSTR

        CommandLineToArgvW = windll.shell32.CommandLineToArgvW
        CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
        CommandLineToArgvW.restype = POINTER(LPWSTR)

        cmd = GetCommandLineW()
        argc = c_int(0)
        argv = CommandLineToArgvW(cmd, byref(argc))
        if argc.value > 0:
            # # Remove Python executable if present
            #            if argc.value - len(sys.argv) == 1:
            #                start = 1
            #            else:
            #                start = 0
            return [argv[i] for i in
                    xrange(0, argc.value)]
    except Exception:
        pass

def getDataDir():
    """Returns the folder where the executable is located."""
    if sys.platform == "win32":
        def fsdecode(x):
            return x.decode(sys.getfilesystemencoding())

        argzero = fsdecode(os.path.abspath(sys.argv[0]))
        if hasattr(sys, 'frozen'):
            dataDir = os.path.dirname(fsdecode(sys.executable))
        else:
            dataDir = os.path.dirname(argzero)
        dataDir = os.getcwdu()
    else:
        dataDir = os.getcwdu()

    return dataDir

def win32_appdata():
    # try to use win32 api to get the AppData folder since python doesn't populate os.environ with unicode strings.

    try:
        import win32com.client

        objShell = win32com.client.Dispatch("WScript.Shell")
        return objShell.SpecialFolders("AppData")
    except Exception, e:
        print "Error while getting AppData folder using WScript.Shell.SpecialFolders: {0!r}".format(e)
        try:
            from win32com.shell import shell, shellcon

            return shell.SHGetPathFromIDListEx(
                shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA)
            )
        except Exception, e:
            print "Error while getting AppData folder using SHGetSpecialFolderLocation: {0!r}".format(e)

            return os.environ['APPDATA'].decode(sys.getfilesystemencoding())

def getMinecraftProfileJSON():
    """Returns a dictionary object with the minecraft profile information"""
    if os.path.isfile(os.path.join(getMinecraftLauncherDirectory(), "launcher_profiles.json")):
        try:
            with open(os.path.join(getMinecraftLauncherDirectory(), "launcher_profiles.json")) as jsonString:
                minecraftProfilesJSON = json.load(jsonString)
            return minecraftProfilesJSON
        except:
            return None

def getMinecraftProfileDirectory(profileName):
    """Returns the path to the sent minecraft profile directory"""
    try:
        profileDir = getMinecraftProfileJSON()['profiles'][profileName][
            'gameDir']  # profileDir update to correct location.
        return profileDir
    except:
        return os.path.join(getMinecraftLauncherDirectory())

def getMinecraftLauncherDirectory():
    """Returns the /minecraft directory, note: may not contain the /saves folder!"""
    if sys.platform == "win32":
        return os.path.join(win32_appdata(), ".minecraft")
    elif sys.platform == "darwin":
        return os.path.expanduser("~/Library/Application Support/minecraft")
    else:
        return os.path.expanduser("~/.minecraft")

def getDocumentsFolder():
    docsFolder = None

    if sys.platform == "win32":
        try:
            import win32com.client
            objShell = win32com.client.Dispatch("WScript.Shell")
            docsFolder = objShell.SpecialFolders("MyDocuments")

        except Exception, e:
            print e
            try:
                docsFolder = shell.SHGetFolderPath(0, shellcon.CSIDL_MYDOCUMENTS, 0, 0)
            except Exception, e:
                userprofile = os.environ['USERPROFILE'].decode(sys.getfilesystemencoding())
                docsFolder = os.path.join(userprofile, "Documents")

    elif sys.platform == "darwin":
        docsFolder = os.path.expanduser("~/Documents")
    else:
        docsFolder = os.path.expanduser("~/.mcedit")
    try:
        os.mkdir(docsFolder)
    except:
        pass

    return docsFolder

def getSelectedProfile():
    try:
        selectedProfile = getMinecraftProfileJSON()['selectedProfile']
        return selectedProfile
    except:
        return None

minecraftSaveFileDir = os.path.join(getMinecraftProfileDirectory(getSelectedProfile()), "saves")
ini = u"mcedit.ini"
cache = u"usercache.json"

parentDir = os.path.dirname(getDataDir())
docsFolder = os.path.join(getDocumentsFolder(),'MCEdit')

if sys.platform != "darwin":

    portableConfigFilePath = os.path.join(parentDir, ini)
    portableCacheFilePath = os.path.join(parentDir, cache)
    portableGenericSupportPath = os.path.join(parentDir)
    portableSchematicsDir = os.path.join(parentDir, u"Schematics")
    portableJarStorageDir = os.path.join(parentDir, u"ServerJarStorage")
    portableFiltersDir = os.path.join(parentDir, u"Filters")
    if not os.path.exists(parentDir):
        os.makedirs(parentDir)

    fixedCacheFilePath = os.path.join(docsFolder, cache)
    fixedConfigFilePath = os.path.join(docsFolder, ini)
    fixedGenericSupportPath = os.path.join(docsFolder)
    fixedSchematicsDir = os.path.join(docsFolder, u"Schematics")
    fixedJarStorageDir = os.path.join(docsFolder, u"ServerJarStorage")
    fixedFiltersDir = os.path.join(docsFolder, u"Filters")
    if not os.path.exists(docsFolder):
        os.makedirs(docsFolder)

def goPortable():
    if sys.platform == "darwin":
        return False
    global configFilePath, schematicsDir, filtersDir, portable, cacheDir

    if os.path.exists(fixedSchematicsDir):
        move_displace(fixedSchematicsDir, portableSchematicsDir)
    if os.path.exists(fixedConfigFilePath):
        move_displace(fixedConfigFilePath, portableConfigFilePath)
    if os.path.exists(fixedFiltersDir):
        move_displace(fixedFiltersDir, portableFiltersDir)
    if os.path.exists(fixedCacheFilePath):
        move_displace(fixedCacheFilePath, portableCacheFilePath)
    if os.path.exists(fixedJarStorageDir):
        move_displace(fixedJarStorageDir, portableJarStorageDir)

    schematicsDir = portableSchematicsDir
    configFilePath = portableConfigFilePath
    filtersDir = portableFiltersDir
    cacheDir = portableGenericSupportPath
    portable = True
    return True

def move_displace(src, dst):
    dstFolder = os.path.basename(os.path.dirname(dst))
    if not os.path.exists(dst):

        print "Moving {0} to {1}".format(os.path.basename(src), dstFolder)
        shutil.move(src, dst)
    else:
        olddst = dst + ".old"
        i = 0
        while os.path.exists(olddst):
            olddst = dst + ".old" + str(i)
            i += 1

        print "{0} already found in {1}! Renamed it to {2}.".format(os.path.basename(src), dstFolder, dst)
        os.rename(dst, olddst)
        shutil.move(src, dst)
    return True

def goFixed():
    if sys.platform == "darwin":
        return False
    global configFilePath, schematicsDir, filtersDir, portable, cacheDir

    if os.path.exists(portableSchematicsDir):
        move_displace(portableSchematicsDir, fixedSchematicsDir)
    if os.path.exists(portableConfigFilePath):
        move_displace(portableConfigFilePath, fixedConfigFilePath)
    if os.path.exists(portableFiltersDir):
        move_displace(portableFiltersDir, fixedFiltersDir)
    if os.path.exists(portableCacheFilePath):
        move_displace(portableCacheFilePath, fixedCacheFilePath)
    if os.path.exists(portableJarStorageDir):
        move_displace(portableJarStorageDir, fixedJarStorageDir)

    schematicsDir = fixedSchematicsDir
    configFilePath = fixedConfigFilePath
    filtersDir = fixedFiltersDir
    cacheDir = fixedGenericSupportPath
    portable = False

def portableConfigExists():
    if sys.platform == "darwin":
        return False
    # Check for files at portable locations. Cannot be Mac because config doesn't move
    return (os.path.exists(portableConfigFilePath) or not os.path.exists(fixedConfigFilePath))

if portableConfigExists() and not sys.platform == "darwin":
    print "Running in portable mode. Support files are stored next to the MCEdit directory."
    portable = True
    schematicsDir = portableSchematicsDir
    configFilePath = portableConfigFilePath
    filtersDir = portableFiltersDir
    JarStorageDir = portableJarStorageDir
    genericSupportDir = portableGenericSupportPath

else:
    print "Running in fixed mode. Support files are in your " + (
    sys.platform == "darwin" and "App Support Folder (Available from the main menu of MCEdit)" or "Documents folder.")
    if not sys.platform == "darwin":
        schematicsDir = fixedSchematicsDir
        configFilePath = fixedConfigFilePath
        filtersDir = fixedFiltersDir
        JarStorageDir = fixedJarStorageDir
        genericSupportDir = fixedGenericSupportPath
    portable = False

if portable:
    serverJarStorageDir = portableJarStorageDir
#    ServerJarStorage.defaultCacheDir = serverJarStorageDir
#    jarStorage = ServerJarStorage(serverJarStorageDir)
#else:
    serverJarStorageDir = fixedJarStorageDir

def getAllFilters(filters_dir):
    return glob.glob(filters_dir+"/*.py")

def getCacheDir():
    """Returns the path to the cache folder. This folder is the Application Support folder on OS X, and the Documents Folder on Windows."""
    if sys.platform == "win32":
        return genericSupportDir
    elif sys.platform == "darwin":
        return os.path.expanduser("~/Library/Application Support/pymclevel")
    else:
        try:
            return genericSupportDir
        except:
            return os.path.expanduser("~/.pymclevel")

if sys.platform == "darwin":
    configFilePath = os.path.expanduser("~/Library/Preferences/mcedit.ini")
    schematicsDir = os.path.join(getCacheDir(), u"Schematics")
    filtersDir = os.path.join(getCacheDir(), u"Filters")
    if not os.path.exists(getCacheDir()):
        os.makedirs(getCacheDir())

# Create pymclevel folder as needed
if not os.path.exists(getCacheDir()):
    os.makedirs(getCacheDir())
# set userCachePath
userCachePath = os.path.join(getCacheDir(),'usercache.json')
# Make sure it exists
try:
    if not os.path.exists(userCachePath):
        f = open(userCachePath,'w')
        f.write('{}')
        f.close()
except:
    print "Unable to make usercache.json at {}".format(userCachePath)<|endoftext|>


<commit_before>import datetime, pickle, os
from elasticsearch import Elasticsearch
import argparse

dump_directory = 'db_dumps'

parser = argparse.ArgumentParser(prog='dumpAllData.py', description='Reads all data from a scouting database and dumps it into a pickle file')
parser.add_argument('--host', type=str, default='localhost')
parser.add_argument('--port', type=int, default=9200)
parser.add_argument('--filepath', type=str, default=os.path.join(dump_directory, 'db_data_dump_%s.p' % datetime.datetime.now().strftime("%I:%M%p_%B_%d_%Y")))

args = parser.parse_args()
host = args.host
port = args.port
filepath = args.filepath

try:
    es = Elasticsearch([{'host': host, 'port': port}])
except Exception:
    sys.exit("Error in connection to database: %s" % str())

MAX_SIZE = 10000

def get_all_from_index(index):
    res = es.search(index=index, body={ 'size': MAX_SIZE, 'query': {'match_all': { } } })
    if not 'hits' in res or not 'hits' in res['hits']:
        print('Could not find documents in index %s')
        return ''
    hits = res['hits']['hits']
    for hit in hits:
        del hit['_score']
    return hits

all_games = get_all_from_index('games')
all_events = get_all_from_index('events')
all_team_game_data = get_all_from_index('team-game-data')
kibana_data = get_all_from_index('.kibana')

data = { \
    'games': all_games,\
    'events': all_events,\
    'team-game-data': all_team_game_data,\
    '.kibana': kibana_data\
}

if not os.path.exists(dump_directory):
    os.makedirs(dump_directory)

with open(filepath, 'w+') as dumpfile:
    pickle.dump(data, dumpfile)

print('Successfuly pickled all data into into file %s' % filepath)
<commit_msg>Now dumping the mappings in additional to the data<commit_after>import datetime, pickle, os
from elasticsearch import Elasticsearch
import argparse

dump_directory = 'db_dumps'

parser = argparse.ArgumentParser(prog='dumpAllData.py', description='Reads all data from a scouting database and dumps it into a pickle file')
parser.add_argument('--host', type=str, default='localhost')
parser.add_argument('--port', type=int, default=9200)
parser.add_argument('--filepath', type=str, default=os.path.join(dump_directory, 'db_data_dump_%s.p' % datetime.datetime.now().strftime("%I:%M%p_%B_%d_%Y")))

args = parser.parse_args()
host = args.host
port = args.port
filepath = args.filepath

try:
    es = Elasticsearch([{'host': host, 'port': port}])
except Exception:
    sys.exit("Error in connection to database: %s" % str())

MAX_SIZE = 10000

def get_all_from_index(index):
    res = es.search(index=index, body={ 'size': MAX_SIZE, 'query': {'match_all': { } } })
    if not 'hits' in res or not 'hits' in res['hits']:
        print('Could not find documents in index %s')
        return ''
    hits = res['hits']['hits']
    for hit in hits:
        del hit['_score']
    return hits

def get_mapping(index):
    mapping = es.indices.get_mapping(index=index)
    return mapping

all_games = get_all_from_index('games')
all_events = get_all_from_index('events')
all_team_game_data = get_all_from_index('team-game-data')
kibana_data = get_all_from_index('.kibana')

games_mapping = get_mapping('games')
events_mapping= get_mapping('events')
team_game_data_mapping = get_mapping('team-game-data')

data = { \
    'games': all_games,\
    'games_mapping': games_mapping,
    'events_mapping': events_mapping,
    'team_game_data_mapping': team_game_data_mapping,
    'events': all_events,\
    'team-game-data': all_team_game_data,\
    '.kibana': kibana_data\
}

if not os.path.exists(dump_directory):
    os.makedirs(dump_directory)

with open(filepath, 'w+') as dumpfile:
    pickle.dump(data, dumpfile)

print('Successfuly pickled all data into into file %s' % filepath)
<|endoftext|>


<commit_before>"""
Models for FOIA Machine
"""

from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.db import models
from django.template.loader import render_to_string
from django.utils.text import slugify

from django_hosts.resolvers import reverse

class FoiaMachineRequest(models.Model):
    """
    A FOIA Machine Request stores information about the request.
    It is based on a reconciliation between MuckRock's existing FOIARequest model
    and FOIA Machine's existing Request model.
    """
    user = models.ForeignKey(User)
    title = models.CharField(max_length=255, db_index=True)
    slug = models.SlugField(max_length=255)
    request_language = models.TextField()
    jurisdiction = models.ForeignKey('jurisdiction.Jurisdiction')
    agency = models.ForeignKey('agency.Agency', blank=True, null=True)

    def save(self, *args, **kwargs):
        """Automatically update the slug field."""
        self.slug = slugify(self.title)
        super(FoiaMachineRequest, self).save(*args, **kwargs)

    def __unicode__(self):
        return unicode(self.title)

    def get_absolute_url(self):
        return reverse('foi-detail', host='foiamachine', kwargs={
            'slug': self.slug,
            'pk': self.pk,
        })

    def generate_letter(self):
        """Returns a public records request letter for the request's jurisdiction."""
        template = 'text/foia/request.txt'
        context = {
            'jurisdiction': self.jurisdiction,
            'document_request': self.request_language,
            'user_name': self.user.get_full_name()
        }
        return render_to_string(template, context=context)

class FoiaMachineCommunication(models.Model):
    """
    A FOIA Machine Communication stores information about an exchange between a user and an agency.
    It is based on the MuckRock existing FOIACommunication object, and also
    loosely mimics the structure of an email.
    """
    request = models.ForeignKey(FoiaMachineRequest, related_name='communications')
    sender = models.CharField(max_length=255)
    receiver = models.CharField(max_length=255, blank=True)
    message = models.TextField()
    date = models.DateField(auto_now_add=True)
    received = models.BooleanField(default=False)

    def __unicode__(self):
        return u'Communication from %s to %s' % (self.sender, self.receiver)

    def get_absolute_url(self):
        return reverse('comm-detail', host='foiamachine', kwargs={
            'foi-slug': self.request.slug,
            'foi-pk': self.request.pk,
            'pk': self.pk,
        })

<commit_msg>strips trailing whitespace from generated letter<commit_after>"""
Models for FOIA Machine
"""

from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.db import models
from django.template.loader import render_to_string
from django.utils.text import slugify

from django_hosts.resolvers import reverse

class FoiaMachineRequest(models.Model):
    """
    A FOIA Machine Request stores information about the request.
    It is based on a reconciliation between MuckRock's existing FOIARequest model
    and FOIA Machine's existing Request model.
    """
    user = models.ForeignKey(User)
    title = models.CharField(max_length=255, db_index=True)
    slug = models.SlugField(max_length=255)
    request_language = models.TextField()
    jurisdiction = models.ForeignKey('jurisdiction.Jurisdiction')
    agency = models.ForeignKey('agency.Agency', blank=True, null=True)

    def save(self, *args, **kwargs):
        """Automatically update the slug field."""
        self.slug = slugify(self.title)
        super(FoiaMachineRequest, self).save(*args, **kwargs)

    def __unicode__(self):
        return unicode(self.title)

    def get_absolute_url(self):
        return reverse('foi-detail', host='foiamachine', kwargs={
            'slug': self.slug,
            'pk': self.pk,
        })

    def generate_letter(self):
        """Returns a public records request letter for the request's jurisdiction."""
        template = 'text/foia/request.txt'
        context = {
            'jurisdiction': self.jurisdiction,
            'document_request': self.request_language,
            'user_name': self.user.get_full_name()
        }
        return render_to_string(template, context=context).strip()

class FoiaMachineCommunication(models.Model):
    """
    A FOIA Machine Communication stores information about an exchange between a user and an agency.
    It is based on the MuckRock existing FOIACommunication object, and also
    loosely mimics the structure of an email.
    """
    request = models.ForeignKey(FoiaMachineRequest, related_name='communications')
    sender = models.CharField(max_length=255)
    receiver = models.CharField(max_length=255, blank=True)
    message = models.TextField()
    date = models.DateField(auto_now_add=True)
    received = models.BooleanField(default=False)

    def __unicode__(self):
        return u'Communication from %s to %s' % (self.sender, self.receiver)

    def get_absolute_url(self):
        return reverse('comm-detail', host='foiamachine', kwargs={
            'foi-slug': self.request.slug,
            'foi-pk': self.request.pk,
            'pk': self.pk,
        })

<|endoftext|>


<commit_before>"""Unit tests for schedule.py"""
import datetime
import functools
import mock
import unittest

# Silence "missing docstring", "method could be a function",
# "class already defined", and "too many public methods" messages:
# pylint: disable-msg=R0201,C0111,E0102,R0904,R0901

import schedule
from schedule import every

def make_mock_job(name=None):
    job = mock.Mock()
    job.__name__ = name or 'job'
    return job

class mock_datetime(object):
    """
    Monkey-patch datetime for predictable results
    """
    def __init__(self, year, month, day, hour, minute):
        self.year = year
        self.month = month
        self.day = day
        self.hour = hour
        self.minute = minute

    def __enter__(self):
        class MockDate(datetime.datetime):
            @classmethod
            def today(cls):
                return cls(self.year, self.month, self.day)

            @classmethod
            def now(cls):
                return cls(self.year, self.month, self.day,
                           self.hour, self.minute)
        self.original_datetime = datetime.datetime
        datetime.datetime = MockDate

    def __exit__(self, *args, **kwargs):
        datetime.datetime = self.original_datetime

class SchedulerTests(unittest.TestCase):
    def setUp(self):
        schedule.clear()

    def test_time_units(self):
        assert every().seconds.unit == 'seconds'
        assert every().minutes.unit == 'minutes'
        assert every().hours.unit == 'hours'
        assert every().days.unit == 'days'
        assert every().weeks.unit == 'weeks'

    def test_singular_time_units_match_plural_units(self):
        assert every().second.unit == every().seconds.unit
        assert every().minute.unit == every().minutes.unit
        assert every().hour.unit == every().hours.unit
        assert every().day.unit == every().days.unit
        assert every().week.unit == every().weeks.unit

    def test_at_time(self):
        mock_job = make_mock_job()
        assert every().day.at('10:30').do(mock_job).next_run.hour == 10
        assert every().day.at('10:30').do(mock_job).next_run.minute == 30

    def test_at_time_hour(self):
        with mock_datetime(2010, 1, 6, 12, 20):
            mock_job = make_mock_job()
            assert every().hour.at(':30').do(mock_job).next_run.hour == 12
            assert every().hour.at(':30').do(mock_job).next_run.minute == 30
            assert every().hour.at(':10').do(mock_job).next_run.hour == 13
            assert every().hour.at(':10').do(mock_job).next_run.minute == 10
            assert every().hour.at(':00').do(mock_job).next_run.hour == 13
            assert every().hour.at(':00').do(mock_job).next_run.minute == 0

    def test_next_run_time(self):
        with mock_datetime(2010, 1, 6, 12, 15):
            mock_job = make_mock_job()
            assert schedule.next_run() is None
            assert every().minute.do(mock_job).next_run.minute == 16
            assert every(5).minutes.do(mock_job).next_run.minute == 20
            assert every().hour.do(mock_job).next_run.hour == 13
            assert every().day.do(mock_job).next_run.day == 7
            assert every().day.at('09:00').do(mock_job).next_run.day == 7
            assert every().day.at('12:30').do(mock_job).next_run.day == 6
            assert every().week.do(mock_job).next_run.day == 13
            assert every().monday.do(mock_job).next_run.day == 11
            assert every().tuesday.do(mock_job).next_run.day == 12
            assert every().wednesday.do(mock_job).next_run.day == 13
            assert every().thursday.do(mock_job).next_run.day == 7
            assert every().friday.do(mock_job).next_run.day == 8
            assert every().saturday.do(mock_job).next_run.day == 9
            assert every().sunday.do(mock_job).next_run.day == 10

    def test_run_all(self):
        mock_job = make_mock_job()
        every().minute.do(mock_job)
        every().hour.do(mock_job)
        every().day.at('11:00').do(mock_job)
        schedule.run_all()
        assert mock_job.call_count == 3

    def test_job_func_args_are_passed_on(self):
        mock_job = make_mock_job()
        every().second.do(mock_job, 1, 2, 'three', foo=23, bar={})
        schedule.run_all()
        mock_job.assert_called_once_with(1, 2, 'three', foo=23, bar={})

    def test_to_string(self):
        def job_fun():
            pass
        s = str(every().minute.do(job_fun, 'foo', bar=23))
        assert 'job_fun' in s
        assert 'foo' in s
        assert 'bar=23' in s

    def test_to_string_lambda_job_func(self):
        assert len(str(every().minute.do(lambda: 1))) > 1
        assert len(str(every().day.at('10:30').do(lambda: 1))) > 1

    def test_to_string_functools_partial_job_func(self):
        def job_fun(arg):
            pass
        job_fun = functools.partial(job_fun, 'foo')
        job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23))
        assert 'functools.partial' in job_repr
        assert 'bar=True' in job_repr
        assert 'somekey=23' in job_repr

    def test_run_pending(self):
        """Check that run_pending() runs pending jobs.
        We do this by overriding datetime.datetime with mock objects
        that represent increasing system times.

        Please note that it is *intended behavior that run_pending() does not
        run missed jobs*. For example, if you've registered a job that
        should run every minute and you only call run_pending() in one hour
        increments then your job won't be run 60 times in between but
        only once.
        """
        mock_job = make_mock_job()

        with mock_datetime(2010, 1, 6, 12, 15):
            every().minute.do(mock_job)
            every().hour.do(mock_job)
            every().day.do(mock_job)
            every().sunday.do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 12, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

        with mock_datetime(2010, 1, 6, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 2

        with mock_datetime(2010, 1, 7, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 3

        with mock_datetime(2010, 1, 10, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 4

    def test_run_every_weekday_at_specific_time_today(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 13, 16):
            every().wednesday.at('14:12').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 14, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

    def test_run_every_weekday_at_specific_time_past_today(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 13, 16):
            every().wednesday.at('13:15').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 13, 13, 14):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 13, 13, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

    def test_run_every_n_days_at_specific_time(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 11, 29):
            every(2).days.at('11:30').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 7, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 8, 11, 29):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 8, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 1

        with mock_datetime(2010, 1, 10, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 2

    def test_next_run_property(self):
        original_datetime = datetime.datetime
        with mock_datetime(2010, 1, 6, 13, 16):
            hourly_job = make_mock_job('hourly')
            daily_job = make_mock_job('daily')
            every().day.do(daily_job)
            every().hour.do(hourly_job)
            assert len(schedule.jobs) == 2
            # Make sure the hourly job is first
            assert schedule.next_run() == original_datetime(2010, 1, 6, 14, 16)
            assert schedule.idle_seconds() == 60 * 60

    def test_cancel_job(self):
        def stop_job():
            return schedule.CancelJob
        mock_job = make_mock_job()

        every().second.do(stop_job)
        mj = every().second.do(mock_job)
        assert len(schedule.jobs) == 2

        schedule.run_all()
        assert len(schedule.jobs) == 1
        assert schedule.jobs[0] == mj

        schedule.cancel_job('Not a job')
        assert len(schedule.jobs) == 1
        schedule.default_scheduler.cancel_job('Not a job')
        assert len(schedule.jobs) == 1

        schedule.cancel_job(mj)
        assert len(schedule.jobs) == 0

    def test_clear_by_tag(self):
        every().second.do(make_mock_job(name='job1')).tag('tag1')
        every().second.do(make_mock_job(name='job2')).tag('tag1', 'tag2')
        every().second.do(make_mock_job(name='job3')).tag('tag3', 'tag2')
        assert len(schedule.jobs) == 3
        schedule.run_all()
        assert len(schedule.jobs) == 3
        schedule.clear('tag3')
        assert len(schedule.jobs) == 2
        schedule.clear('tag1')
        assert len(schedule.jobs) == 0
        every().second.do(make_mock_job(name='job1'))
        every().second.do(make_mock_job(name='job2'))
        every().second.do(make_mock_job(name='job3'))
        schedule.clear()
        assert len(schedule.jobs) == 0<commit_msg>PEP8 compliance. Newline at EOF<commit_after>"""Unit tests for schedule.py"""
import datetime
import functools
import mock
import unittest

# Silence "missing docstring", "method could be a function",
# "class already defined", and "too many public methods" messages:
# pylint: disable-msg=R0201,C0111,E0102,R0904,R0901

import schedule
from schedule import every

def make_mock_job(name=None):
    job = mock.Mock()
    job.__name__ = name or 'job'
    return job

class mock_datetime(object):
    """
    Monkey-patch datetime for predictable results
    """
    def __init__(self, year, month, day, hour, minute):
        self.year = year
        self.month = month
        self.day = day
        self.hour = hour
        self.minute = minute

    def __enter__(self):
        class MockDate(datetime.datetime):
            @classmethod
            def today(cls):
                return cls(self.year, self.month, self.day)

            @classmethod
            def now(cls):
                return cls(self.year, self.month, self.day,
                           self.hour, self.minute)
        self.original_datetime = datetime.datetime
        datetime.datetime = MockDate

    def __exit__(self, *args, **kwargs):
        datetime.datetime = self.original_datetime

class SchedulerTests(unittest.TestCase):
    def setUp(self):
        schedule.clear()

    def test_time_units(self):
        assert every().seconds.unit == 'seconds'
        assert every().minutes.unit == 'minutes'
        assert every().hours.unit == 'hours'
        assert every().days.unit == 'days'
        assert every().weeks.unit == 'weeks'

    def test_singular_time_units_match_plural_units(self):
        assert every().second.unit == every().seconds.unit
        assert every().minute.unit == every().minutes.unit
        assert every().hour.unit == every().hours.unit
        assert every().day.unit == every().days.unit
        assert every().week.unit == every().weeks.unit

    def test_at_time(self):
        mock_job = make_mock_job()
        assert every().day.at('10:30').do(mock_job).next_run.hour == 10
        assert every().day.at('10:30').do(mock_job).next_run.minute == 30

    def test_at_time_hour(self):
        with mock_datetime(2010, 1, 6, 12, 20):
            mock_job = make_mock_job()
            assert every().hour.at(':30').do(mock_job).next_run.hour == 12
            assert every().hour.at(':30').do(mock_job).next_run.minute == 30
            assert every().hour.at(':10').do(mock_job).next_run.hour == 13
            assert every().hour.at(':10').do(mock_job).next_run.minute == 10
            assert every().hour.at(':00').do(mock_job).next_run.hour == 13
            assert every().hour.at(':00').do(mock_job).next_run.minute == 0

    def test_next_run_time(self):
        with mock_datetime(2010, 1, 6, 12, 15):
            mock_job = make_mock_job()
            assert schedule.next_run() is None
            assert every().minute.do(mock_job).next_run.minute == 16
            assert every(5).minutes.do(mock_job).next_run.minute == 20
            assert every().hour.do(mock_job).next_run.hour == 13
            assert every().day.do(mock_job).next_run.day == 7
            assert every().day.at('09:00').do(mock_job).next_run.day == 7
            assert every().day.at('12:30').do(mock_job).next_run.day == 6
            assert every().week.do(mock_job).next_run.day == 13
            assert every().monday.do(mock_job).next_run.day == 11
            assert every().tuesday.do(mock_job).next_run.day == 12
            assert every().wednesday.do(mock_job).next_run.day == 13
            assert every().thursday.do(mock_job).next_run.day == 7
            assert every().friday.do(mock_job).next_run.day == 8
            assert every().saturday.do(mock_job).next_run.day == 9
            assert every().sunday.do(mock_job).next_run.day == 10

    def test_run_all(self):
        mock_job = make_mock_job()
        every().minute.do(mock_job)
        every().hour.do(mock_job)
        every().day.at('11:00').do(mock_job)
        schedule.run_all()
        assert mock_job.call_count == 3

    def test_job_func_args_are_passed_on(self):
        mock_job = make_mock_job()
        every().second.do(mock_job, 1, 2, 'three', foo=23, bar={})
        schedule.run_all()
        mock_job.assert_called_once_with(1, 2, 'three', foo=23, bar={})

    def test_to_string(self):
        def job_fun():
            pass
        s = str(every().minute.do(job_fun, 'foo', bar=23))
        assert 'job_fun' in s
        assert 'foo' in s
        assert 'bar=23' in s

    def test_to_string_lambda_job_func(self):
        assert len(str(every().minute.do(lambda: 1))) > 1
        assert len(str(every().day.at('10:30').do(lambda: 1))) > 1

    def test_to_string_functools_partial_job_func(self):
        def job_fun(arg):
            pass
        job_fun = functools.partial(job_fun, 'foo')
        job_repr = repr(every().minute.do(job_fun, bar=True, somekey=23))
        assert 'functools.partial' in job_repr
        assert 'bar=True' in job_repr
        assert 'somekey=23' in job_repr

    def test_run_pending(self):
        """Check that run_pending() runs pending jobs.
        We do this by overriding datetime.datetime with mock objects
        that represent increasing system times.

        Please note that it is *intended behavior that run_pending() does not
        run missed jobs*. For example, if you've registered a job that
        should run every minute and you only call run_pending() in one hour
        increments then your job won't be run 60 times in between but
        only once.
        """
        mock_job = make_mock_job()

        with mock_datetime(2010, 1, 6, 12, 15):
            every().minute.do(mock_job)
            every().hour.do(mock_job)
            every().day.do(mock_job)
            every().sunday.do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 12, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

        with mock_datetime(2010, 1, 6, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 2

        with mock_datetime(2010, 1, 7, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 3

        with mock_datetime(2010, 1, 10, 13, 16):
            mock_job.reset_mock()
            schedule.run_pending()
            assert mock_job.call_count == 4

    def test_run_every_weekday_at_specific_time_today(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 13, 16):
            every().wednesday.at('14:12').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 14, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

    def test_run_every_weekday_at_specific_time_past_today(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 13, 16):
            every().wednesday.at('13:15').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 13, 13, 14):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 13, 13, 16):
            schedule.run_pending()
            assert mock_job.call_count == 1

    def test_run_every_n_days_at_specific_time(self):
        mock_job = make_mock_job()
        with mock_datetime(2010, 1, 6, 11, 29):
            every(2).days.at('11:30').do(mock_job)
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 6, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 7, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 8, 11, 29):
            schedule.run_pending()
            assert mock_job.call_count == 0

        with mock_datetime(2010, 1, 8, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 1

        with mock_datetime(2010, 1, 10, 11, 31):
            schedule.run_pending()
            assert mock_job.call_count == 2

    def test_next_run_property(self):
        original_datetime = datetime.datetime
        with mock_datetime(2010, 1, 6, 13, 16):
            hourly_job = make_mock_job('hourly')
            daily_job = make_mock_job('daily')
            every().day.do(daily_job)
            every().hour.do(hourly_job)
            assert len(schedule.jobs) == 2
            # Make sure the hourly job is first
            assert schedule.next_run() == original_datetime(2010, 1, 6, 14, 16)
            assert schedule.idle_seconds() == 60 * 60

    def test_cancel_job(self):
        def stop_job():
            return schedule.CancelJob
        mock_job = make_mock_job()

        every().second.do(stop_job)
        mj = every().second.do(mock_job)
        assert len(schedule.jobs) == 2

        schedule.run_all()
        assert len(schedule.jobs) == 1
        assert schedule.jobs[0] == mj

        schedule.cancel_job('Not a job')
        assert len(schedule.jobs) == 1
        schedule.default_scheduler.cancel_job('Not a job')
        assert len(schedule.jobs) == 1

        schedule.cancel_job(mj)
        assert len(schedule.jobs) == 0

    def test_clear_by_tag(self):
        every().second.do(make_mock_job(name='job1')).tag('tag1')
        every().second.do(make_mock_job(name='job2')).tag('tag1', 'tag2')
        every().second.do(make_mock_job(name='job3')).tag('tag3', 'tag2')
        assert len(schedule.jobs) == 3
        schedule.run_all()
        assert len(schedule.jobs) == 3
        schedule.clear('tag3')
        assert len(schedule.jobs) == 2
        schedule.clear('tag1')
        assert len(schedule.jobs) == 0
        every().second.do(make_mock_job(name='job1'))
        every().second.do(make_mock_job(name='job2'))
        every().second.do(make_mock_job(name='job3'))
        schedule.clear()
        assert len(schedule.jobs) == 0
<|endoftext|>


<commit_before><commit_msg>update China challenge<commit_after><|endoftext|>


<commit_before>"""
Adapters for fitting module
"""
import copy
import numpy
import math
from data_util.uncertainty import Uncertainty
from danse.common.plottools.plottables import Data1D as PlotData1D
from danse.common.plottools.plottables import Data2D as PlotData2D
from danse.common.plottools.plottables import Theory1D as PlotTheory1D

from sans.dataloader.data_info import Data1D as LoadData1D
from sans.dataloader.data_info import Data2D as LoadData2D

class Data1D(PlotData1D, LoadData1D):
    """
    """
    def __init__(self, x=None, y=None, dx=None, dy=None):
        """
        """
        if x is None:
            x = []
        if y is None:
            y = []
        PlotData1D.__init__(self, x, y, dx, dy)
        LoadData1D.__init__(self, x, y, dx, dy)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data1d):
        """
        copy values of Data1D of type DataLaoder.Data_info
        """
        self.x  = copy.deepcopy(data1d.x)
        self.y  = copy.deepcopy(data1d.y)
        self.dy = copy.deepcopy(data1d.dy)

        if hasattr(data1d, "dx"):
            self.dx = copy.deepcopy(data1d.dx)
        if hasattr(data1d, "dxl"):
            self.dxl = copy.deepcopy(data1d.dxl)
        if hasattr(data1d, "dxw"):
            self.dxw = copy.deepcopy(data1d.dxw)

        self.xaxis(data1d._xaxis, data1d._xunit)
        self.yaxis(data1d._yaxis, data1d._yunit)
        self.title = data1d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData1D.__str__(self)

        return _str

    def _perform_operation(self, other, operation):
        """
        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)
        result = Data1D(x=[], y=[], dx=None, dy=None)
        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data1d=self)
        for i in range(len(self.x)):
            result.x[i] = self.x[i]
            if self.dx is not None and len(self.x) == len(self.dx):
                result.dx[i] = self.dx[i]

            a = Uncertainty(self.y[i], dy[i]**2)
            if isinstance(other, Data1D):
                b = Uncertainty(other.y[i], dy_other[i]**2)
            else:
                b = other

            output = operation(a, b)
            result.y[i] = output.x
            if result.dy is None: result.dy = numpy.zeros(len(self.x))
            result.dy[i] = math.sqrt(math.fabs(output.variance))
        return result

class Theory1D(PlotTheory1D, LoadData1D):
    """
    """
    def __init__(self, x=None, y=None, dy=None):
        """
        """
        if x is None:
            x = []
        if y is None:
            y = []
        PlotTheory1D.__init__(self, x, y, dy)
        LoadData1D.__init__(self, x, y, dy)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data1d):
        """
        copy values of Data1D of type DataLaoder.Data_info
        """
        self.x  = copy.deepcopy(data1d.x)
        self.y  = copy.deepcopy(data1d.y)
        self.dy = copy.deepcopy(data1d.dy)
        if hasattr(data1d, "dx"):
            self.dx = copy.deepcopy(data1d.dx)
        if hasattr(data1d, "dxl"):
            self.dxl = copy.deepcopy(data1d.dxl)
        if hasattr(data1d, "dxw"):
            self.dxw = copy.deepcopy(data1d.dxw)
        self.xaxis(data1d._xaxis, data1d._xunit)
        self.yaxis(data1d._yaxis, data1d._yunit)
        self.title = data1d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData1D.__str__(self)

        return _str

    def _perform_operation(self, other, operation):
        """
        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)
        result = Theory1D(x=[], y=[], dy=None)
        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data1d=self)
        for i in range(len(self.x)):
            result.x[i] = self.x[i]

            a = Uncertainty(self.y[i], dy[i]**2)
            if isinstance(other, Data1D):
                b = Uncertainty(other.y[i], dy_other[i]**2)
            else:
                b = other
            output = operation(a, b)
            result.y[i] = output.x
            if result.dy is None:
                result.dy = numpy.zeros(len(self.x))
            result.dy[i] = math.sqrt(math.fabs(output.variance))
        return result

class Data2D(PlotData2D, LoadData2D):
    """
    """
    def __init__(self, image=None, err_image=None,
                 xmin=None, xmax=None, ymin=None, ymax=None,
                 zmin=None, zmax=None, qx_data=None, qy_data=None,
                 q_data=None, mask=None, dqx_data=None, dqy_data=None):
        """
        """
        PlotData2D.__init__(self, image=image, err_image=err_image,
                            xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
                            zmin=zmin, zmax=zmax, qx_data=qx_data,
                            qy_data=qy_data)

        LoadData2D.__init__(self, data=image, err_data=err_image,
                            qx_data=qx_data, qy_data=qy_data,
                            dqx_data=dqx_data, dqy_data=dqy_data,
                            q_data=q_data, mask=mask)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data2d):
        """
        copy value of Data2D of type DataLoader.data_info
        """
        self.data = copy.deepcopy(data2d.data)
        self.qx_data = copy.deepcopy(data2d.qx_data)
        self.qy_data = copy.deepcopy(data2d.qy_data)
        self.q_data = copy.deepcopy(data2d.q_data)
        self.mask = copy.deepcopy(data2d.mask)
        self.err_data = copy.deepcopy(data2d.err_data)
        self.x_bins = copy.deepcopy(data2d.x_bins)
        self.y_bins = copy.deepcopy(data2d.y_bins)
        if data2d.dqx_data is not None:
            self.dqx_data = copy.deepcopy(data2d.dqx_data)
        if data2d.dqy_data is not None:
            self.dqy_data = copy.deepcopy(data2d.dqy_data)
        self.xmin = data2d.xmin
        self.xmax = data2d.xmax
        self.ymin = data2d.ymin
        self.ymax = data2d.ymax
        if hasattr(data2d, "zmin"):
            self.zmin = data2d.zmin
        if hasattr(data2d, "zmax"):
            self.zmax = data2d.zmax
        self.xaxis(data2d._xaxis, data2d._xunit)
        self.yaxis(data2d._yaxis, data2d._yunit)
        self.title = data2d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData2D.__str__(self)
        return _str

    def _perform_operation(self, other, operation):
        """
        Perform 2D operations between data sets

        :param other: other data set
        :param operation: function defining the operation

        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)

        result = Data2D(image=None, qx_data=None, qy_data=None,
                         err_image=None, xmin=None, xmax=None,
                         ymin=None, ymax=None, zmin=None, zmax=None)

        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data2d=self)

        for i in range(numpy.size(self.data, 0)):
            for j in range(numpy.size(self.data, 1)):
                result.data[i][j] = self.data[i][j]
                if self.err_data is not None and \
                        numpy.size(self.data) == numpy.size(self.err_data):
                    result.err_data[i][j] = self.err_data[i][j]

                a = Uncertainty(self.data[i][j], dy[i][j]**2)
                if isinstance(other, Data2D):
                    b = Uncertainty(other.data[i][j], dy_other[i][j]**2)
                else:
                    b = other
                output = operation(a, b)
                result.data[i][j] = output.x
                result.err_data[i][j] = math.sqrt(math.fabs(output.variance))
        return result
        <commit_msg>add function to check valid data<commit_after>"""
Adapters for fitting module
"""
import copy
import numpy
import math
from data_util.uncertainty import Uncertainty
from danse.common.plottools.plottables import Data1D as PlotData1D
from danse.common.plottools.plottables import Data2D as PlotData2D
from danse.common.plottools.plottables import Theory1D as PlotTheory1D

from sans.dataloader.data_info import Data1D as LoadData1D
from sans.dataloader.data_info import Data2D as LoadData2D

class Data1D(PlotData1D, LoadData1D):
    """
    """
    def __init__(self, x=None, y=None, dx=None, dy=None):
        """
        """
        if x is None:
            x = []
        if y is None:
            y = []
        PlotData1D.__init__(self, x, y, dx, dy)
        LoadData1D.__init__(self, x, y, dx, dy)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data1d):
        """
        copy values of Data1D of type DataLaoder.Data_info
        """
        self.x  = copy.deepcopy(data1d.x)
        self.y  = copy.deepcopy(data1d.y)
        self.dy = copy.deepcopy(data1d.dy)

        if hasattr(data1d, "dx"):
            self.dx = copy.deepcopy(data1d.dx)
        if hasattr(data1d, "dxl"):
            self.dxl = copy.deepcopy(data1d.dxl)
        if hasattr(data1d, "dxw"):
            self.dxw = copy.deepcopy(data1d.dxw)

        self.xaxis(data1d._xaxis, data1d._xunit)
        self.yaxis(data1d._yaxis, data1d._yunit)
        self.title = data1d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData1D.__str__(self)

        return _str

    def _perform_operation(self, other, operation):
        """
        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)
        result = Data1D(x=[], y=[], dx=None, dy=None)
        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data1d=self)
        for i in range(len(self.x)):
            result.x[i] = self.x[i]
            if self.dx is not None and len(self.x) == len(self.dx):
                result.dx[i] = self.dx[i]

            a = Uncertainty(self.y[i], dy[i]**2)
            if isinstance(other, Data1D):
                b = Uncertainty(other.y[i], dy_other[i]**2)
            else:
                b = other

            output = operation(a, b)
            result.y[i] = output.x
            if result.dy is None: result.dy = numpy.zeros(len(self.x))
            result.dy[i] = math.sqrt(math.fabs(output.variance))
        return result

class Theory1D(PlotTheory1D, LoadData1D):
    """
    """
    def __init__(self, x=None, y=None, dy=None):
        """
        """
        if x is None:
            x = []
        if y is None:
            y = []
        PlotTheory1D.__init__(self, x, y, dy)
        LoadData1D.__init__(self, x, y, dy)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data1d):
        """
        copy values of Data1D of type DataLaoder.Data_info
        """
        self.x  = copy.deepcopy(data1d.x)
        self.y  = copy.deepcopy(data1d.y)
        self.dy = copy.deepcopy(data1d.dy)
        if hasattr(data1d, "dx"):
            self.dx = copy.deepcopy(data1d.dx)
        if hasattr(data1d, "dxl"):
            self.dxl = copy.deepcopy(data1d.dxl)
        if hasattr(data1d, "dxw"):
            self.dxw = copy.deepcopy(data1d.dxw)
        self.xaxis(data1d._xaxis, data1d._xunit)
        self.yaxis(data1d._yaxis, data1d._yunit)
        self.title = data1d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData1D.__str__(self)

        return _str

    def _perform_operation(self, other, operation):
        """
        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)
        result = Theory1D(x=[], y=[], dy=None)
        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data1d=self)
        for i in range(len(self.x)):
            result.x[i] = self.x[i]

            a = Uncertainty(self.y[i], dy[i]**2)
            if isinstance(other, Data1D):
                b = Uncertainty(other.y[i], dy_other[i]**2)
            else:
                b = other
            output = operation(a, b)
            result.y[i] = output.x
            if result.dy is None:
                result.dy = numpy.zeros(len(self.x))
            result.dy[i] = math.sqrt(math.fabs(output.variance))
        return result

class Data2D(PlotData2D, LoadData2D):
    """
    """
    def __init__(self, image=None, err_image=None,
                 xmin=None, xmax=None, ymin=None, ymax=None,
                 zmin=None, zmax=None, qx_data=None, qy_data=None,
                 q_data=None, mask=None, dqx_data=None, dqy_data=None):
        """
        """
        PlotData2D.__init__(self, image=image, err_image=err_image,
                            xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
                            zmin=zmin, zmax=zmax, qx_data=qx_data,
                            qy_data=qy_data)

        LoadData2D.__init__(self, data=image, err_data=err_image,
                            qx_data=qx_data, qy_data=qy_data,
                            dqx_data=dqx_data, dqy_data=dqy_data,
                            q_data=q_data, mask=mask)
        self.id = None
        self.list_group_id = []
        self.group_id = None
        self.is_data = True
        self.path = None
        self.xtransform = None
        self.ytransform = None
        self.title = ""
        self.scale = None

    def copy_from_datainfo(self, data2d):
        """
        copy value of Data2D of type DataLoader.data_info
        """
        self.data = copy.deepcopy(data2d.data)
        self.qx_data = copy.deepcopy(data2d.qx_data)
        self.qy_data = copy.deepcopy(data2d.qy_data)
        self.q_data = copy.deepcopy(data2d.q_data)
        self.mask = copy.deepcopy(data2d.mask)
        self.err_data = copy.deepcopy(data2d.err_data)
        self.x_bins = copy.deepcopy(data2d.x_bins)
        self.y_bins = copy.deepcopy(data2d.y_bins)
        if data2d.dqx_data is not None:
            self.dqx_data = copy.deepcopy(data2d.dqx_data)
        if data2d.dqy_data is not None:
            self.dqy_data = copy.deepcopy(data2d.dqy_data)
        self.xmin = data2d.xmin
        self.xmax = data2d.xmax
        self.ymin = data2d.ymin
        self.ymax = data2d.ymax
        if hasattr(data2d, "zmin"):
            self.zmin = data2d.zmin
        if hasattr(data2d, "zmax"):
            self.zmax = data2d.zmax
        self.xaxis(data2d._xaxis, data2d._xunit)
        self.yaxis(data2d._yaxis, data2d._yunit)
        self.title = data2d.title

    def __str__(self):
        """
        print data
        """
        _str = "%s\n" % LoadData2D.__str__(self)
        return _str

    def _perform_operation(self, other, operation):
        """
        Perform 2D operations between data sets

        :param other: other data set
        :param operation: function defining the operation

        """
        # First, check the data compatibility
        dy, dy_other = self._validity_check(other)

        result = Data2D(image=None, qx_data=None, qy_data=None,
                         err_image=None, xmin=None, xmax=None,
                         ymin=None, ymax=None, zmin=None, zmax=None)

        result.clone_without_data(clone=self)
        result.copy_from_datainfo(data2d=self)

        for i in range(numpy.size(self.data, 0)):
            for j in range(numpy.size(self.data, 1)):
                result.data[i][j] = self.data[i][j]
                if self.err_data is not None and \
                        numpy.size(self.data) == numpy.size(self.err_data):
                    result.err_data[i][j] = self.err_data[i][j]

                a = Uncertainty(self.data[i][j], dy[i][j]**2)
                if isinstance(other, Data2D):
                    b = Uncertainty(other.data[i][j], dy_other[i][j]**2)
                else:
                    b = other
                output = operation(a, b)
                result.data[i][j] = output.x
                result.err_data[i][j] = math.sqrt(math.fabs(output.variance))
        return result

def check_data_validity(data):
      """
      Return True is data is valid enough to compute chisqr, else False
      """
      flag = True
      if data is not None:
          if issubclass(data.__class__, Data2D):
              if (data.data is None) or (len(data.data) == 0) \
               or (len(data.err_data) == 0):
                  flag = False
          else:
              if (data.y is None) or (len(data.y) == 0):
                  flag = False
      else:
          flag = False
      return flag<|endoftext|>