<commit_before># -*- coding: utf-8 -*-
#
# Default constance keys, and their values.
#

import datetime

#CONSTANCE_REDIS_CONNECTION is to be found in 'snippets/databases*'
CONSTANCE_BACKEND      = 'constance.backends.redisd.RedisBackend'
CONSTANCE_REDIS_PREFIX = 'c0s1f:'

CONSTANCE_CONFIG = {

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Read webapp

    'DOCUMENTS_ARCHIVING_DISABLED': (False, ugettext(u'Set this to True to '
                            u'disable maintenance archiving operations. '
                            u'Useful for archive database migrations (BUT '
                            u'NOT SUFFICIENT, because of `original_data` that '
                            u'must be disabled too, via `*_FETCH_DISABLED`).')),

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Read webapp

    'READ_INFINITE_AUTOSCROLL_ENABLED': (True, ugettext(u'Make the read view '
                                         u'(either list or tiles) '
                                         u'automatically load new content '
                                         u'when reaching end of page.')),

    'READ_INFINITE_ITEMS_PER_FETCH': (ENDLESS_PAGINATION_PER_PAGE,
                                      ugettext(u'Number of items per ajax '
                                      u'fetch on infinite pagination pages.')),

    'READ_ARTICLE_MIN_LENGTH': (24, ugettext(u'Minimum length of an article '
                                u'content. Set to 0 to always display '
                                u'Markdown content to users, whatever it is.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••••••••••••• RSS feed fetch

    # SEMANTIC NOTE: we use 'disabled' (and not 'enabled') because the rss
    # refreshing is something that is enabled in normal conditions. Stopping
    # it is an unusual / rare action, so the setting is named accordingly.
    'FEED_FETCH_DISABLED': (False, ugettext(u'Set this to True for '
                            u'maintenance operations and wait for all '
                            u'fetchers to terminate. It should take at '
                            u'most FEED_FETCH_DEFAULT_INTERVAL seconds.')),

    'FEED_FETCH_GHOST_ENABLED': (False, ugettext(u'Enable Ghost fetching or '
                                 u'not. Disabled by default, make OSX Python '
                                 u'crash.')),

    'FEED_FETCH_MAX_ERRORS': (90, ugettext(u'The maximum number of errors '
                              u'a feed can meet before beiing automatically '
                              u'closed.')),

    'FEED_FETCH_DEFAULT_INTERVAL': (43200, ugettext(u'Default feed fetch '
                                    u'interval in seconds. You can tune the '
                                    u'interval for each feed after creation.')),

    'FEED_FETCH_MIN_INTERVAL': (300, ugettext(u'The minimum interval between '
                                u'fetches of the same feed. Warning: anything '
                                u'under 60 will not be taken in account.')),

    'FEED_FETCH_MAX_INTERVAL': (259200, ugettext(u'The maximum interval between '
                                u'fetches of the same feed. Warning: anything '
                                u'more than 7 days (expressed in seconds) will '
                                u'not be taken in account.')),

    'FEED_FETCH_RAISE_THRESHOLD': (10, ugettext(u'If this feed produces more '
                                   u'new articles than this number, it will '
                                   u'be fetched more fast than a feed that '
                                   u'produces less. Minimum value: 5.')),

    'FEED_FETCH_PARALLEL_LIMIT': (16, ugettext(u'Default number of articles '
                                  u'fetchable from the same feed at the same '
                                  u'time. Workers should adjust the value '
                                  u'automatically as time passes.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••• Feed admin configuration

    'FEED_REFRESH_RANDOMIZE': (True, ugettext(u'Set this to False if you want '
                               u'all feeds with the same fetch interval to '
                               u'fetch at the same time. Default is to '
                               u'randomize the refreshs not to hammer our '
                               u'machines.')),

    'FEED_REFRESH_RANDOMIZE_DELAY': (40, ugettext(u'Number of seconds max for '
                                     u'the randomization (eg. between 0 and '
                                     u'this number). Warning: keep this '
                                     u'lower than the peridicity of the feed '
                                     u'refresher, else some tasks will be '
                                     u'duplicated.')),

    'FEED_ADMIN_LIST_PER_PAGE': (100, ugettext(u'How many feeds per page in '
                                 u'the Django admin. Increase only if '
                                 u'performance is acceptable; do NOT abuse!')),

    'FEED_ADMIN_MEANINGFUL_DELTA': (6 * 365 / 12, ugettext(u'We see how many '
                                    u'articles feeds have produced during this '
                                    u'delta (in days), to see if we can close '
                                    u'them or not (if they got 0 new '
                                    u'articles).')),

    'FEED_CLOSED_WARN_LIMIT': (5, ugettext(u'Number of days during which a '
                               u'just closed feed will be warned about to '
                               u'site managers via mail.')),

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Article parsing

    'ARTICLE_FETCHING_DEBUG': (False, ugettext(u'Enable this to log '
                               u'intermediate article versions. Default: '
                                  u'not enabled in normal conditions.')),

    'ARTICLE_ABSOLUTIZING_DISABLED': (False, ugettext(u'Disable or not the '
                                      u'article URL absolutization. Default: '
                                      u'enabled in normal conditions (and '
                                      u'without it we will have a lot of '
                                      u'duplicate content that will need '
                                      u'post-processing).')),

    'ARTICLE_FETCHING_DISABLED': (False, ugettext(u'Disable or not '
                                  u'global fetching operations for all '
                                  u'content types. Default: '
                                  u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_TEXT_DISABLED': (False, ugettext(u'Disable or not '
                                       u'text fetching operations. Default: '
                                       u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_VIDEO_DISABLED': (False, ugettext(u'Disable or not '
                                        u'video fetching operations. Default: '
                                        u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_IMAGE_DISABLED': (False, ugettext(u'Disable or not '
                                        u'image fetching operations. Default: '
                                        u'enabled in normal conditions.')),

    'ARTICLE_MARKDOWN_DISABLED': (False, ugettext(u'Disable or not the HTML '
                                  u'to Markdown internal conversion. '
                                  u'Default: enabled in normal conditions.')),

    'ARTICLE_ARCHIVE_BATCH_SIZE': (100 if DEBUG else 50000,
                                   ugettext(u'how much articles will be '
                                   u'archived at each archive task run.')),

    'ARTICLE_ARCHIVE_OLDER_THAN': (62, ugettext(u'Only articles older than '
                                   u'that will be archived, regarding their '
                                   u'`date_published` field. This delta is '
                                   u'expressed in days. Set to 0 to archive '
                                   u'everything without mercy.')),

    # ••••••••••••••••••••••••••••••••••••••••••• Various checks and core tasks

    'CHECK_SUBSCRIPTIONS_DISABLED': (False, ugettext(u'Disable or not the '
                                     u'night subscription check that will '
                                     u'check_reads() all subscriptions that '
                                     u'do not have the same number of '
                                     u'articles than the feed they belong '
                                     u'to. Default: enabled.')),

    'CHECK_DUPLICATES_DISABLED': (False, ugettext(u'Disable or not the '
                                  u'night duplicates check that will '
                                  u'ensure all duplicate articles have '
                                  u'no read left in the system. Default: '
                                  u'let it run (=enabled).')),

    'CHECK_READS_DISABLED': (False, ugettext(u'Disable or not the night '
                             u'reads check that will switch on-and-off their '
                             u'`is_good` attribute. Default: let it run '
                             u'(=enabled).')),

    # ——————————————————————————————————————————————————————— Exerpt generation

    'EXCERPT_PARAGRAPH_MIN_LENGTH': (64, ugettext(u'Number of characters '
                                     u'below which a paragraph does not '
                                     u'have enough data (words) to be '
                                     u'considered informational.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••••••• Google Reader Import

    # GR_LOAD_LIMIT * GR_WAVE_LIMIT must equals GR_MAX_ARTICLES:
    # In the worst case, a user could have only one feed containing all its
    # articles. In this case, if WAVE_LIMIT is too low, the import tasks will
    # stop because of waves exhaustion, but the global import will never stop
    # because neither GR_MAX_ARTICLES nor total_starred nor total_reads would
    # have been reached.
    'GR_MAX_ARTICLES': (25 if DEBUG else 500000, ugettext(u'maximum number '
                        u'of Google Reader articles imported for a user. '
                        u'WARNING: GR_LOAD_LIMIT * GR_WAVE_LIMIT must equals '
                        u'GR_MAX_ARTICLES, else you risk never-ending '
                        u'imports in some rare cases.')),

    'GR_MAX_FEEDS': (2 if DEBUG else 1000, ugettext(u'maximum number of '
                     u'articles imported from Google Reader for any user.')),

    'GR_LOAD_LIMIT': (10 if DEBUG else 500, ugettext(u'maximum number of '
                      u'articles in each wave of Google Reader feed import. '
                      u'WARNING: stop all imports before raising this, or '
                      u'some data will be missed by running tasks. No such '
                      u'problem if your lower it.')),

    'GR_WAVE_LIMIT': (10 if DEBUG else 1000, ugettext(u'maximum number of '
                      u'import waves for each Google Reader feed.')),

    'GR_IMPORT_ALLOWED': (True, ugettext(u'Are users allowed to import from '
                          u'Google Reader? Set to False if we reach limits '
                          u'too quickly.')),

    'GR_MAX_RETRIES': (5, ugettext(u'How many retries to attempt if a '
                       u'Google Reader Operation fails.')),

    # WARNING: keep this a date(), which is neither naive nor TZ aware.
    'GR_END_DATE': (datetime.date(2013, 07, 01),
                    ugettext(u'Google Reader official end date')),

    # NOTE about GR_STORAGE_LIMIT:
    # It's about the physical storage limit on duncan.licorn.org. On my
    # development machine, 260K articles = 1.7Gb only of storage reported in
    # MongoHub, but 6.5Gb really occupied on disk. Duncan's maximum storage
    # availability is ~650Gb, which leads us to a neat 27000000 articles.
    'GR_STORAGE_LIMIT': (100000 if DEBUG else 27000000, ugettext(u'Maximum '
                         u'number of articles in database, after which the '
                         u'Google Reader imports will be disabled.')),

}
<commit_msg>More constance directives, for the upcoming local user accounts implementation. Conflicts: 	oneflow/settings/snippets/constance.py<commit_after># -*- coding: utf-8 -*-
#
# Default constance keys, and their values.
#
"""
    Copyright 2013 Olivier Cortès <oc@1flow.io>

    This file is part of the 1flow project.

    1flow is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    1flow is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public
    License along with 1flow.  If not, see http://www.gnu.org/licenses/

"""

import datetime

#CONSTANCE_REDIS_CONNECTION is to be found in 'snippets/databases*'
CONSTANCE_BACKEND      = 'constance.backends.redisd.RedisBackend'
CONSTANCE_REDIS_PREFIX = 'c0s1f:'

CONSTANCE_CONFIG = {

    # ———————————————————————————————————————————— Allow new users to register?

    'SOCIAL_LOGIN_ENABLED': (True, ugettext(u'Already known users are allowed '
                             u'to sign in via social network accounts. '
                             u'Default: True. See SOCIAL_REGISTRATION_ENABLED '
                             u'for new accounts.')),

    'SOCIAL_REGISTRATION_ENABLED': (True, ugettext(u'New users are allowed to '
                                    u'auto-register accounts via social '
                                    u'networks signins. Default: False.')),

    'LOCAL_LOGIN_ENABLED': (True, ugettext(u'Already known users are allowed '
                            u'to sign in via local accounts. Default: True. '
                            u'You can disable this to allow only social '
                            u'signins.')),

    'LOCAL_REGISTRATION_ENABLED': (True, ugettext(u'New users are allowed to '
                                    u'register local accounts. Default: '
                                    u'False. NOTE2: the underlying code does '
                                    u'not currently use this dynamic '
                                    u'configuration. We have to contribute a '
                                    u'patch, but the configuration directive '
                                    u'exists, at least.')),

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Read webapp

    'DOCUMENTS_ARCHIVING_DISABLED': (False, ugettext(u'Set this to True to '
                            u'disable maintenance archiving operations. '
                            u'Useful for archive database migrations (BUT '
                            u'NOT SUFFICIENT, because of `original_data` that '
                            u'must be disabled too, via `*_FETCH_DISABLED`).')),

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Read webapp

    'READ_INFINITE_AUTOSCROLL_ENABLED': (True, ugettext(u'Make the read view '
                                         u'(either list or tiles) '
                                         u'automatically load new content '
                                         u'when reaching end of page.')),

    'READ_INFINITE_ITEMS_PER_FETCH': (ENDLESS_PAGINATION_PER_PAGE,
                                      ugettext(u'Number of items per ajax '
                                      u'fetch on infinite pagination pages.')),

    'READ_ARTICLE_MIN_LENGTH': (24, ugettext(u'Minimum length of an article '
                                u'content. Set to 0 to always display '
                                u'Markdown content to users, whatever it is.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••••••••••••• RSS feed fetch

    # SEMANTIC NOTE: we use 'disabled' (and not 'enabled') because the rss
    # refreshing is something that is enabled in normal conditions. Stopping
    # it is an unusual / rare action, so the setting is named accordingly.
    'FEED_FETCH_DISABLED': (False, ugettext(u'Set this to True for '
                            u'maintenance operations and wait for all '
                            u'fetchers to terminate. It should take at '
                            u'most FEED_FETCH_DEFAULT_INTERVAL seconds.')),

    'FEED_FETCH_GHOST_ENABLED': (False, ugettext(u'Enable Ghost fetching or '
                                 u'not. Disabled by default, make OSX Python '
                                 u'crash.')),

    'FEED_FETCH_MAX_ERRORS': (90, ugettext(u'The maximum number of errors '
                              u'a feed can meet before beiing automatically '
                              u'closed.')),

    'FEED_FETCH_DEFAULT_INTERVAL': (43200, ugettext(u'Default feed fetch '
                                    u'interval in seconds. You can tune the '
                                    u'interval for each feed after creation.')),

    'FEED_FETCH_MIN_INTERVAL': (300, ugettext(u'The minimum interval between '
                                u'fetches of the same feed. Warning: anything '
                                u'under 60 will not be taken in account.')),

    'FEED_FETCH_MAX_INTERVAL': (259200, ugettext(u'The maximum interval between '
                                u'fetches of the same feed. Warning: anything '
                                u'more than 7 days (expressed in seconds) will '
                                u'not be taken in account.')),

    'FEED_FETCH_RAISE_THRESHOLD': (10, ugettext(u'If this feed produces more '
                                   u'new articles than this number, it will '
                                   u'be fetched more fast than a feed that '
                                   u'produces less. Minimum value: 5.')),

    'FEED_FETCH_PARALLEL_LIMIT': (16, ugettext(u'Default number of articles '
                                  u'fetchable from the same feed at the same '
                                  u'time. Workers should adjust the value '
                                  u'automatically as time passes.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••• Feed admin configuration

    'FEED_REFRESH_RANDOMIZE': (True, ugettext(u'Set this to False if you want '
                               u'all feeds with the same fetch interval to '
                               u'fetch at the same time. Default is to '
                               u'randomize the refreshs not to hammer our '
                               u'machines.')),

    'FEED_REFRESH_RANDOMIZE_DELAY': (40, ugettext(u'Number of seconds max for '
                                     u'the randomization (eg. between 0 and '
                                     u'this number). Warning: keep this '
                                     u'lower than the peridicity of the feed '
                                     u'refresher, else some tasks will be '
                                     u'duplicated.')),

    'FEED_ADMIN_LIST_PER_PAGE': (100, ugettext(u'How many feeds per page in '
                                 u'the Django admin. Increase only if '
                                 u'performance is acceptable; do NOT abuse!')),

    'FEED_ADMIN_MEANINGFUL_DELTA': (6 * 365 / 12, ugettext(u'We see how many '
                                    u'articles feeds have produced during this '
                                    u'delta (in days), to see if we can close '
                                    u'them or not (if they got 0 new '
                                    u'articles).')),

    'FEED_CLOSED_WARN_LIMIT': (5, ugettext(u'Number of days during which a '
                               u'just closed feed will be warned about to '
                               u'site managers via mail.')),

    # ••••••••••••••••••••••••••••••••••••••••••••••••••••••••• Article parsing

    'ARTICLE_FETCHING_DEBUG': (False, ugettext(u'Enable this to log '
                               u'intermediate article versions. Default: '
                                  u'not enabled in normal conditions.')),

    'ARTICLE_ABSOLUTIZING_DISABLED': (False, ugettext(u'Disable or not the '
                                      u'article URL absolutization. Default: '
                                      u'enabled in normal conditions (and '
                                      u'without it we will have a lot of '
                                      u'duplicate content that will need '
                                      u'post-processing).')),

    'ARTICLE_FETCHING_DISABLED': (False, ugettext(u'Disable or not '
                                  u'global fetching operations for all '
                                  u'content types. Default: '
                                  u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_TEXT_DISABLED': (False, ugettext(u'Disable or not '
                                       u'text fetching operations. Default: '
                                       u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_VIDEO_DISABLED': (False, ugettext(u'Disable or not '
                                        u'video fetching operations. Default: '
                                        u'enabled in normal conditions.')),

    'ARTICLE_FETCHING_IMAGE_DISABLED': (False, ugettext(u'Disable or not '
                                        u'image fetching operations. Default: '
                                        u'enabled in normal conditions.')),

    'ARTICLE_MARKDOWN_DISABLED': (False, ugettext(u'Disable or not the HTML '
                                  u'to Markdown internal conversion. '
                                  u'Default: enabled in normal conditions.')),

    'ARTICLE_ARCHIVE_BATCH_SIZE': (100 if DEBUG else 50000,
                                   ugettext(u'how much articles will be '
                                   u'archived at each archive task run.')),

    'ARTICLE_ARCHIVE_OLDER_THAN': (62, ugettext(u'Only articles older than '
                                   u'that will be archived, regarding their '
                                   u'`date_published` field. This delta is '
                                   u'expressed in days. Set to 0 to archive '
                                   u'everything without mercy.')),

    # ••••••••••••••••••••••••••••••••••••••••••• Various checks and core tasks

    'CHECK_SUBSCRIPTIONS_DISABLED': (False, ugettext(u'Disable or not the '
                                     u'night subscription check that will '
                                     u'check_reads() all subscriptions that '
                                     u'do not have the same number of '
                                     u'articles than the feed they belong '
                                     u'to. Default: enabled.')),

    'CHECK_DUPLICATES_DISABLED': (False, ugettext(u'Disable or not the '
                                  u'night duplicates check that will '
                                  u'ensure all duplicate articles have '
                                  u'no read left in the system. Default: '
                                  u'let it run (=enabled).')),

    'CHECK_READS_DISABLED': (False, ugettext(u'Disable or not the night '
                             u'reads check that will switch on-and-off their '
                             u'`is_good` attribute. Default: let it run '
                             u'(=enabled).')),

    # ——————————————————————————————————————————————————————— Exerpt generation

    'EXCERPT_PARAGRAPH_MIN_LENGTH': (64, ugettext(u'Number of characters '
                                     u'below which a paragraph does not '
                                     u'have enough data (words) to be '
                                     u'considered informational.')),

    # •••••••••••••••••••••••••••••••••••••••••••••••••••• Google Reader Import

    # GR_LOAD_LIMIT * GR_WAVE_LIMIT must equals GR_MAX_ARTICLES:
    # In the worst case, a user could have only one feed containing all its
    # articles. In this case, if WAVE_LIMIT is too low, the import tasks will
    # stop because of waves exhaustion, but the global import will never stop
    # because neither GR_MAX_ARTICLES nor total_starred nor total_reads would
    # have been reached.
    'GR_MAX_ARTICLES': (25 if DEBUG else 500000, ugettext(u'maximum number '
                        u'of Google Reader articles imported for a user. '
                        u'WARNING: GR_LOAD_LIMIT * GR_WAVE_LIMIT must equals '
                        u'GR_MAX_ARTICLES, else you risk never-ending '
                        u'imports in some rare cases.')),

    'GR_MAX_FEEDS': (2 if DEBUG else 1000, ugettext(u'maximum number of '
                     u'articles imported from Google Reader for any user.')),

    'GR_LOAD_LIMIT': (10 if DEBUG else 500, ugettext(u'maximum number of '
                      u'articles in each wave of Google Reader feed import. '
                      u'WARNING: stop all imports before raising this, or '
                      u'some data will be missed by running tasks. No such '
                      u'problem if your lower it.')),

    'GR_WAVE_LIMIT': (10 if DEBUG else 1000, ugettext(u'maximum number of '
                      u'import waves for each Google Reader feed.')),

    'GR_IMPORT_ALLOWED': (True, ugettext(u'Are users allowed to import from '
                          u'Google Reader? Set to False if we reach limits '
                          u'too quickly.')),

    'GR_MAX_RETRIES': (5, ugettext(u'How many retries to attempt if a '
                       u'Google Reader Operation fails.')),

    # WARNING: keep this a date(), which is neither naive nor TZ aware.
    'GR_END_DATE': (datetime.date(2013, 07, 01),
                    ugettext(u'Google Reader official end date')),

    # NOTE about GR_STORAGE_LIMIT:
    # It's about the physical storage limit on duncan.licorn.org. On my
    # development machine, 260K articles = 1.7Gb only of storage reported in
    # MongoHub, but 6.5Gb really occupied on disk. Duncan's maximum storage
    # availability is ~650Gb, which leads us to a neat 27000000 articles.
    'GR_STORAGE_LIMIT': (100000 if DEBUG else 27000000, ugettext(u'Maximum '
                         u'number of articles in database, after which the '
                         u'Google Reader imports will be disabled.')),

}
<|endoftext|>


<commit_before>import clr
import sys
clr.AddReferenceByPartialName("UnityEngine")
clr.AddReferenceByPartialName("Pluton")
import UnityEngine
import Pluton
from Pluton import InvItem
from System import *
from UnityEngine import *

class Example:
	def On_PlayerConnected(self, player):
		for p in Server.ActivePlayers:
			if(p.Name != player.Name):
				p.Message(String.Format("{0} has joined the server!", player.Name))

	def On_PlayerDisconnected(self, player):
		for p in Server.ActivePlayers:
			if(p.Name != player.Name):
				p.Message(String.Format("{0} has left the server!", player.Name))

	def On_Command(self, cmd):
		try:
			if(cmd.cmd == "kit"):
				if(Server.LoadOuts.ContainsKey(cmd.quotedArgs[0]))
				loadout = Server.LoadOuts[cmd.quotedArgs[0]]
				loadout.ToInv(cmd.User.Inventory)
			if(cmd.cmd == "apple"):
				cmd.User.Message("An apple a day keeps the doctor away!")
				item = InvItem("Apple")
				item.Instantiate(Vector3(cmd.User.X + 3, cmd.User.Y + 3, cmd.User.Z + 3))
			if(cmd.cmd == "help"):
				cmd.User.Message("Usable command: /whereami, /kit starter")
		except:
			Debug.Log(String.Format("Something went wrong while executing: /{0} args", cmd.cmd, String.Join(" ", cmd.args)))<commit_msg>indent<commit_after>import clr
import sys
clr.AddReferenceByPartialName("UnityEngine")
clr.AddReferenceByPartialName("Pluton")
import UnityEngine
import Pluton
from Pluton import InvItem
from System import *
from UnityEngine import *

class Example:
	def On_PlayerConnected(self, player):
		for p in Server.ActivePlayers:
			if(p.Name != player.Name):
				p.Message(String.Format("{0} has joined the server!", player.Name))

	def On_PlayerDisconnected(self, player):
		for p in Server.ActivePlayers:
			if(p.Name != player.Name):
				p.Message(String.Format("{0} has left the server!", player.Name))

	def On_Command(self, cmd):
		try:
			if(cmd.cmd == "kit"):
				if(Server.LoadOuts.ContainsKey(cmd.quotedArgs[0])):
					loadout = Server.LoadOuts[cmd.quotedArgs[0]]
					loadout.ToInv(cmd.User.Inventory)
			if(cmd.cmd == "apple"):
				cmd.User.Message("An apple a day keeps the doctor away!")
				item = InvItem("Apple")
				item.Instantiate(Vector3(cmd.User.X + 3, cmd.User.Y + 3, cmd.User.Z + 3))
			if(cmd.cmd == "help"):
				cmd.User.Message("Usable command: /whereami, /kit starter")
		except:
			Debug.Log(String.Format("Something went wrong while executing: /{0} args", cmd.cmd, String.Join(" ", cmd.args)))<|endoftext|>


<commit_before>import webapp2
import json

from generic_handler import GenericHandler
from google.appengine.api import mail
from google.appengine.api import users
from google.appengine.ext import ndb
from handler_utils import CheckUserOwnsTournamentAndMaybeReturnStatus
from handler_utils import GetTourneyWithIdAndMaybeReturnStatus
from handler_utils import SetErrorStatus
from models import Tournament
from models import PlayerPair

class WelcomeHandler(GenericHandler):
  ''' Handles reuqests to /api/tournament/:id/welcome. Responsible for emailing
      players with their player codes.
  '''

  @ndb.toplevel
  def post(self, id):
    ''' Sends an email for all email addresses in the request.
    Checks that emails belong to players in the tournament and sends the email
    only to valid addresses.

    Args:
      id: tournament ID to look up. Tournament must already have been
          created.
    '''
    user = users.get_current_user()
    tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id)
    if not tourney:
      return
    if not CheckUserOwnsTournamentAndMaybeReturnStatus(self.response, user,
                                                       tourney):
      return

    request_dict = self._ParseRequestAndMaybeSetStatus()
    if not request_dict:
      return
    self._SendEmails(request_dict, user, tourney)
    self.response.headers['Content-Type'] = 'application/json'
    self.response.set_status(201)

  def _SendEmails(self, request_dict, user, tourney):
    '''Sends a welcome email for all email addresses in the request_dict.

    Args:
      request_dict: Parsed JSON dict.
      user: The ndb.User owning this tournament.
      tourney: The tournament model object.
    '''
    player_pairs = PlayerPair._query(ancestor=tourney.key).fetch()
    requested_emails = request_dict["emails"]
    for player_pair in player_pairs:
      for player in player_pair.player_list():
        if player.get("email") not in requested_emails:
          continue
        player_name = player.get("name")
        player_greeting = "Dear {},".format(player_name) if player_name else "Greetings!"
        email_text = """{}
\nWelcome to Tichu tournament \"{}\". Your pair's ID is {}.
You can use it to view and enter your results on
http://tichu-tournament.appspot.com.
\nGood Luck!
\nYour friendly neighborhood tournament director""".format(
                player_greeting, tourney.name, player_pair.id)
        mail.send_mail(
            sender=user.email(),
            to=player["email"],
            subject="Your Tichu Tournament Pair Code",
            body=email_text,
            reply_to=user.email())

  def _ParseRequestAndMaybeSetStatus(self):
    ''' Parses the client request for email sents an error status if the
        request is unreadable or the email list is empty.

    Returns: dict corresponding to the parsed request.s
    '''
    try:
      request_dict = json.loads(self.request.body)
    except ValueError:
      SetErrorStatus(self.response, 500, "Invalid Input",
                     "Unable to parse request body as JSON object")
      return None
    request_dict["emails"] = [e for e in request_dict["emails"] if e and e != ""]
    if len(request_dict["emails"]) == 0:
      SetErrorStatus(self.response, 400, "Invalid Input",
                     "No emails specified.")
      return None
    return request_dict<commit_msg>Remove reply to<commit_after>import webapp2
import json

from generic_handler import GenericHandler
from google.appengine.api import mail
from google.appengine.api import users
from google.appengine.ext import ndb
from handler_utils import CheckUserOwnsTournamentAndMaybeReturnStatus
from handler_utils import GetTourneyWithIdAndMaybeReturnStatus
from handler_utils import SetErrorStatus
from models import Tournament
from models import PlayerPair

class WelcomeHandler(GenericHandler):
  ''' Handles reuqests to /api/tournament/:id/welcome. Responsible for emailing
      players with their player codes.
  '''

  @ndb.toplevel
  def post(self, id):
    ''' Sends an email for all email addresses in the request.
    Checks that emails belong to players in the tournament and sends the email
    only to valid addresses.

    Args:
      id: tournament ID to look up. Tournament must already have been
          created.
    '''
    user = users.get_current_user()
    tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id)
    if not tourney:
      return
    if not CheckUserOwnsTournamentAndMaybeReturnStatus(self.response, user,
                                                       tourney):
      return

    request_dict = self._ParseRequestAndMaybeSetStatus()
    if not request_dict:
      return
    self._SendEmails(request_dict, user, tourney)
    self.response.headers['Content-Type'] = 'application/json'
    self.response.set_status(201)

  def _SendEmails(self, request_dict, user, tourney):
    '''Sends a welcome email for all email addresses in the request_dict.

    Args:
      request_dict: Parsed JSON dict.
      user: The ndb.User owning this tournament.
      tourney: The tournament model object.
    '''
    player_pairs = PlayerPair._query(ancestor=tourney.key).fetch()
    requested_emails = request_dict["emails"]
    for player_pair in player_pairs:
      for player in player_pair.player_list():
        if player.get("email") not in requested_emails:
          continue
        player_name = player.get("name")
        player_greeting = "Dear {},".format(player_name) if player_name else "Greetings!"
        email_text = """{}
\nWelcome to Tichu tournament \"{}\". Your pair's ID is {}.
You can use it to view and enter your results on https://tichu-tournament.appspot.com.
\nGood Luck!
Your friendly neighborhood tournament director""".format(
                player_greeting, tourney.name, player_pair.id)
        mail.send_mail(
            sender=user.email(),
            to=player["email"],
            subject="Your Tichu Tournament Pair Code",
            body=email_text)

  def _ParseRequestAndMaybeSetStatus(self):
    ''' Parses the client request for email sents an error status if the
        request is unreadable or the email list is empty.

    Returns: dict corresponding to the parsed request.s
    '''
    try:
      request_dict = json.loads(self.request.body)
    except ValueError:
      SetErrorStatus(self.response, 500, "Invalid Input",
                     "Unable to parse request body as JSON object")
      return None
    request_dict["emails"] = [e for e in request_dict["emails"] if e and e != ""]
    if len(request_dict["emails"]) == 0:
      SetErrorStatus(self.response, 400, "Invalid Input",
                     "No emails specified.")
      return None
    return request_dict<|endoftext|>


<commit_before>from __future__ import print_function

import codecs
import os
import re
import shutil
import subprocess
import sys
import tempfile
from xml.dom import minidom

from metakernel import MetaKernel, ProcessMetaKernel, REPLWrapper, u
from metakernel.pexpect import which
from IPython.display import Image, SVG

from . import __version__

class OctaveKernel(ProcessMetaKernel):
    implementation = 'Octave Kernel'
    implementation_version = __version__,
    language = 'octave'
    language_version = __version__,
    banner = "Octave Kernel",
    language_info = {
        'mimetype': 'text/x-octave',
        'name': 'octave',
        'file_extension': '.m',
        "version": __version__,
        'help_links': MetaKernel.help_links,
    }

    _banner = None
    _octave_engine = None

    @property
    def banner(self):
        if self._banner is None:
            self._banner = self.octave_engine.eval('info', silent=True)
        return self._banner

    @property
    def octave_engine(self):
        if self._octave_engine:
            return self._octave_engine
        self._octave_engine = OctaveEngine(plot_settings=self.plot_settings,
                                           error_handler=self.Error,
                                           stdin_handler=self.raw_input,
                                           stream_handler=self.Print)
        return self._octave_engine

    def makeWrapper(self):
        """Start an Octave process and return a :class:`REPLWrapper` object.
        """
        return self.octave_engine.repl

    def do_execute_direct(self, code, silent=False):
        if code.strip() in ['quit', 'quit()', 'exit', 'exit()']:
            self.do_shutdown(True)
            return
        val = ProcessMetaKernel.do_execute_direct(self, code, silent=silent)
        if not silent:
            for image in self.octave_engine.handle_figs():
                self.Display(image)
        return val

    def get_kernel_help_on(self, info, level=0, none_on_fail=False):
        obj = info.get('help_obj', '')
        if not obj or len(obj.split()) > 1:
            if none_on_fail:
                return None
            else:
                return ""
        return self.octave_engine.eval('help %s' % obj, silent=True)

    def get_completions(self, info):
        """
        Get completions from kernel based on info dict.
        """
        cmd = 'completion_matches("%s")' % info['obj']
        val = self.octave_engine.eval(cmd, silent=True)
        return val and val.splitlines() or []

    def handle_plot_settings(self):
        """Handle the current plot settings"""
        self.octave_engine.plot_settings = self.plot_settings

class OctaveEngine(object):

    def __init__(self, error_handler=None, stream_handler=None,
                 stdin_handler=None, plot_settings=None):
        self.executable = self._get_executable()
        self.repl = self._create_repl()
        self.error_handler = error_handler
        self.stream_handler = stream_handler
        self.stdin_handler = stdin_handler
        self._startup(plot_settings)

    @property
    def plot_settings(self):
        return self._plot_settings

    @plot_settings.setter
    def plot_settings(self, settings):
        self._plot_settings = settings or dict(backend='inline')
        if sys.platform == 'darwin':
            settings.setdefault('format', 'svg')
        else:
            settings.setdefault('format', 'png')

        settings.setdefault('backend', 'inline')
        settings.setdefault('width', -1)
        settings.setdefault('height', -1)
        settings.setdefault('resolution', 0)

        cmds = []
        if settings['backend'] == 'inline':
            cmds.append("set(0, 'defaultfigurevisible', 'off');")
            cmds.append("graphics_toolkit('gnuplot');")
        else:
            cmds.append("set(0, 'defaultfigurevisible', 'on');")
            cmds.append("graphics_toolkit('%s');" % settings['backend'])
        self.eval('\n'.join(cmds))

    def eval(self, code, timeout=None, silent=False):
        """Evaluate code using the engine.
        """
        stream_handler = None if silent else self.stream_handler
        try:
            return self.repl.run_command(code.rstrip(),
                                         timeout=timeout,
                                         stream_handler=stream_handler,
                                         stdin_handler=self.stdin_handler)
        except Exception as e:
            if self.error_handler:
                self.error_handler(e)
            else:
                raise e

    def handle_figs(self, plot_dir=None):
        """Get a list of IPython Image objects for the created figures.
        """
        settings = self._plot_settings
        if settings['backend'] != 'inline':
            self.eval('drawnow("expose");')
            return []
        fmt = settings['format']
        res = settings['resolution']
        wid = settings['width']
        hgt = settings['height']
        plot_dir = plot_dir or tempfile.mkdtemp()
        make_figs = '_make_figures("%s", "%s", %d, %d, %d)'
        self.eval(make_figs % (plot_dir, fmt, wid, hgt, res))
        images = []
        for fname in os.listdir(plot_dir):
            filename = os.path.join(plot_dir, fname)
            try:
                if fname.lower().endswith('.svg'):
                    im = self._handle_svg(filename)
                else:
                    im = Image(filename)
                images.append(im)
            except Exception as e:
                if self.error_handler:
                    self.error_handler(e)
                else:
                    raise e
        shutil.rmtree(plot_dir, True)
        return images

    def _startup(self, plot_settings):
        cwd = os.getcwd().replace(os.path.sep, '/')
        here = os.path.realpath(os.path.dirname(__file__))
        here = here.replace(os.path.sep, '/')
        # Changing directories will source the local ".octaverc" file.
        cmd = 'more off; source ~/.octaverc; cd("%s"); addpath(genpath("%s"));'
        self.eval(cmd % (cwd, here))
        available = self.eval('available_graphics_toolkits', silent=True)
        if 'gnuplot' not in available:
            msg = ('May not be able to display plots properly '
                   'without gnuplot, please install it')
            if self.error_handler:
                self.error_handler(msg)
            else:
                raise ValueError(msg)
        self.plot_settings = plot_settings

    def _handle_svg(self, filename):
        """
        Handle special considerations for SVG images.
        """
        # Gnuplot can create invalid characters in SVG files.
        with codecs.open(filename, 'r', encoding='utf-8',
                         errors='replace') as fid:
            data = fid.read()
        im = SVG(data=data)
        try:
            im.data = self._fix_svg_size(im.data)
        except Exception:
            pass
        return im

    def _fix_svg_size(self, data):
        """GnuPlot SVGs do not have height/width attributes.  Set
        these to be the same as the viewBox, so that the browser
        scales the image correctly.
        """
        # Minidom does not support parseUnicode, so it must be decoded
        # to accept unicode characters
        parsed = minidom.parseString(data.encode('utf-8'))
        (svg,) = parsed.getElementsByTagName('svg')

        viewbox = svg.getAttribute('viewBox').split(' ')
        width, height = viewbox[2:]

        svg.setAttribute('width', '%dpx' % int(width))
        svg.setAttribute('height', '%dpx' % int(height))
        return svg.toxml()

    def _create_repl(self):
        cmd = self.executable
        if 'octave-cli' not in cmd:
            version_cmd = [self.executable, '--version']
            version = subprocess.check_output(version_cmd).decode('utf-8')
            if 'version 4' in version:
                cmd += ' --no-gui'
        # Interactive mode prevents crashing on Windows on syntax errors.
        # Delay sourcing the "~/.octaverc" file in case it displays a pager.
        cmd += ' --interactive --quiet --no-init-file'
        if os.name == 'nt':
            orig_prompt = u(chr(3))
            prompt_emit_cmd = u('disp(char(3))')
            change_prompt = None
        else:
            orig_prompt = u('octave.*>')
            prompt_emit_cmd = None
            change_prompt = u("PS1('{0}'); PS2('{1}')")

        repl = REPLWrapper(cmd, orig_prompt, change_prompt,
                           stdin_prompt_regex=re.compile(r'\A[\w]+>>? '),
                           prompt_emit_cmd=prompt_emit_cmd)
        repl.linesep = '\n'
        return repl

    def _get_executable(self):
        """Find the best octave executable.
        """
        executable = os.environ.get('OCTAVE_EXECUTABLE', None)
        if not executable or not which(executable):
            if which('octave-cli'):
                executable = 'octave-cli'
            elif which('octave'):
                executable = 'octave'
            else:
                msg = ('Octave Executable not found, please add to path or set'
                       '"OCTAVE_EXECUTABLE" environment variable')
                raise OSError(msg)
        return executable
<commit_msg>Fixes for windows support<commit_after>from __future__ import print_function

import codecs
import os
import re
import shutil
import subprocess
import sys
import tempfile
from xml.dom import minidom

from metakernel import MetaKernel, ProcessMetaKernel, REPLWrapper, u
from metakernel.pexpect import which
from IPython.display import Image, SVG

from . import __version__

class OctaveKernel(ProcessMetaKernel):
    implementation = 'Octave Kernel'
    implementation_version = __version__,
    language = 'octave'
    language_version = __version__,
    banner = "Octave Kernel",
    language_info = {
        'mimetype': 'text/x-octave',
        'name': 'octave',
        'file_extension': '.m',
        "version": __version__,
        'help_links': MetaKernel.help_links,
    }

    _banner = None
    _octave_engine = None

    @property
    def banner(self):
        if self._banner is None:
            self._banner = self.octave_engine.eval('info', silent=True)
        return self._banner

    @property
    def octave_engine(self):
        if self._octave_engine:
            return self._octave_engine
        self._octave_engine = OctaveEngine(plot_settings=self.plot_settings,
                                           error_handler=self.Error,
                                           stdin_handler=self.raw_input,
                                           stream_handler=self.Print)
        return self._octave_engine

    def makeWrapper(self):
        """Start an Octave process and return a :class:`REPLWrapper` object.
        """
        return self.octave_engine.repl

    def do_execute_direct(self, code, silent=False):
        if code.strip() in ['quit', 'quit()', 'exit', 'exit()']:
            self.do_shutdown(True)
            return
        val = ProcessMetaKernel.do_execute_direct(self, code, silent=silent)
        if not silent:
            for image in self.octave_engine.handle_figs():
                self.Display(image)
        return val

    def get_kernel_help_on(self, info, level=0, none_on_fail=False):
        obj = info.get('help_obj', '')
        if not obj or len(obj.split()) > 1:
            if none_on_fail:
                return None
            else:
                return ""
        return self.octave_engine.eval('help %s' % obj, silent=True)

    def get_completions(self, info):
        """
        Get completions from kernel based on info dict.
        """
        cmd = 'completion_matches("%s")' % info['obj']
        val = self.octave_engine.eval(cmd, silent=True)
        return val and val.splitlines() or []

    def handle_plot_settings(self):
        """Handle the current plot settings"""
        self.octave_engine.plot_settings = self.plot_settings

class OctaveEngine(object):

    def __init__(self, error_handler=None, stream_handler=None,
                 stdin_handler=None, plot_settings=None):
        self.executable = self._get_executable()
        self.repl = self._create_repl()
        self.error_handler = error_handler
        self.stream_handler = stream_handler
        self.stdin_handler = stdin_handler
        self._startup(plot_settings)

    @property
    def plot_settings(self):
        return self._plot_settings

    @plot_settings.setter
    def plot_settings(self, settings):
        self._plot_settings = settings or dict(backend='inline')
        if sys.platform == 'darwin':
            settings.setdefault('format', 'svg')
        else:
            settings.setdefault('format', 'png')

        settings.setdefault('backend', 'inline')
        settings.setdefault('width', -1)
        settings.setdefault('height', -1)
        settings.setdefault('resolution', 0)

        cmds = []
        if settings['backend'] == 'inline':
            cmds.append("set(0, 'defaultfigurevisible', 'off');")
            cmds.append("graphics_toolkit('gnuplot');")
        else:
            cmds.append("set(0, 'defaultfigurevisible', 'on');")
            cmds.append("graphics_toolkit('%s');" % settings['backend'])
        self.eval('\n'.join(cmds))

    def eval(self, code, timeout=None, silent=False):
        """Evaluate code using the engine.
        """
        stream_handler = None if silent else self.stream_handler
        try:
            return self.repl.run_command(code.rstrip(),
                                         timeout=timeout,
                                         stream_handler=stream_handler,
                                         stdin_handler=self.stdin_handler)
        except Exception as e:
            if self.error_handler:
                self.error_handler(e)
            else:
                raise e

    def handle_figs(self, plot_dir=None):
        """Get a list of IPython Image objects for the created figures.
        """
        settings = self._plot_settings
        if settings['backend'] != 'inline':
            self.eval('drawnow("expose");')
            return []
        fmt = settings['format']
        res = settings['resolution']
        wid = settings['width']
        hgt = settings['height']
        plot_dir = plot_dir or tempfile.mkdtemp()
        plot_dir = plot_dir.replace(os.path.sep, '/')
        make_figs = '_make_figures("%s", "%s", %d, %d, %d)'
        self.eval(make_figs % (plot_dir, fmt, wid, hgt, res))
        images = []
        for fname in os.listdir(plot_dir):
            filename = os.path.join(plot_dir, fname)
            try:
                if fname.lower().endswith('.svg'):
                    im = self._handle_svg(filename)
                else:
                    im = Image(filename)
                images.append(im)
            except Exception as e:
                if self.error_handler:
                    self.error_handler(e)
                else:
                    raise e
        shutil.rmtree(plot_dir, True)
        return images

    def _startup(self, plot_settings):
        # Changing directories will source the local ".octaverc" file.
        cwd = os.getcwd().replace(os.path.sep, '/')
        self.eval('more off; source ~/.octaverc; cd("%s");' % cwd)
        here = os.path.realpath(os.path.dirname(__file__))
        self.eval('addpath("%s")' % here.replace(os.path.sep, '/'))
        available = self.eval('available_graphics_toolkits', silent=True)
        if 'gnuplot' not in available:
            msg = ('May not be able to display plots properly '
                   'without gnuplot, please install it')
            if self.error_handler:
                self.error_handler(msg)
            else:
                raise ValueError(msg)
        self.plot_settings = plot_settings

    def _handle_svg(self, filename):
        """
        Handle special considerations for SVG images.
        """
        # Gnuplot can create invalid characters in SVG files.
        with codecs.open(filename, 'r', encoding='utf-8',
                         errors='replace') as fid:
            data = fid.read()
        im = SVG(data=data)
        try:
            im.data = self._fix_svg_size(im.data)
        except Exception:
            pass
        return im

    def _fix_svg_size(self, data):
        """GnuPlot SVGs do not have height/width attributes.  Set
        these to be the same as the viewBox, so that the browser
        scales the image correctly.
        """
        # Minidom does not support parseUnicode, so it must be decoded
        # to accept unicode characters
        parsed = minidom.parseString(data.encode('utf-8'))
        (svg,) = parsed.getElementsByTagName('svg')

        viewbox = svg.getAttribute('viewBox').split(' ')
        width, height = viewbox[2:]

        svg.setAttribute('width', '%dpx' % int(width))
        svg.setAttribute('height', '%dpx' % int(height))
        return svg.toxml()

    def _create_repl(self):
        cmd = self.executable
        if 'octave-cli' not in cmd:
            version_cmd = [self.executable, '--version']
            version = subprocess.check_output(version_cmd).decode('utf-8')
            if 'version 4' in version:
                cmd += ' --no-gui'
        # Interactive mode prevents crashing on Windows on syntax errors.
        # Delay sourcing the "~/.octaverc" file in case it displays a pager.
        cmd += ' --interactive --quiet --no-init-file'
        orig_prompt = u('octave.*>')
        change_prompt = u("PS1('{0}'); PS2('{1}')")

        repl = REPLWrapper(cmd, orig_prompt, change_prompt,
                           stdin_prompt_regex=re.compile(r'\A[\w]+>>? '))
        repl.linesep = '\n'
        return repl

    def _get_executable(self):
        """Find the best octave executable.
        """
        executable = os.environ.get('OCTAVE_EXECUTABLE', None)
        if not executable or not which(executable):
            if which('octave-cli'):
                executable = 'octave-cli'
            elif which('octave'):
                executable = 'octave'
            else:
                msg = ('Octave Executable not found, please add to path or set'
                       '"OCTAVE_EXECUTABLE" environment variable')
                raise OSError(msg)
        return executable
<|endoftext|>


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

from webscript import WebScript
from django import forms
from amcat.scripts.searchscripts.articleids import ArticleidsScript
from amcat.scripts.processors.save_set import SaveAsSetScript, SaveAsSetForm

import logging
import json

log = logging.getLogger(__name__)

class SaveAsSetWebScriptForm(SaveAsSetForm):
    output = forms.CharField(widget=forms.HiddenInput(), initial='json-html')
    length = forms.IntegerField(widget=forms.HiddenInput(), initial=99999999)
    start = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

class SaveAsSet(WebScript):
    name = "Save as Set"
    form_template = None#"api/webscripts/save_set_form.html"
    form = SaveAsSetWebScriptForm
    displayLocation = ('ShowSummary', 'ShowArticleList')
    output_template = "api/webscripts/save_set.html"

    def run(self):
        articleids = ArticleidsScript(self.formData).run()
        result = SaveAsSetScript(self.formData).run(articleids)
        result.provenance = json.dumps(dict(self.formData))
        result.save()

        return self.outputResponse(result, object)

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

from webscript import WebScript
from django import forms
from amcat.scripts.searchscripts.articleids import ArticleidsScript
from amcat.scripts.processors.save_set import SaveAsSetScript, SaveAsSetForm

import logging
import json

log = logging.getLogger(__name__)

class SaveAsSetWebScriptForm(SaveAsSetForm):
    output = forms.CharField(widget=forms.HiddenInput(), initial='json-html')
    length = forms.IntegerField(widget=forms.HiddenInput(), max_value=99999999, initial=99999999)
    start = forms.IntegerField(widget=forms.HiddenInput(), initial=0)

class SaveAsSet(WebScript):
    name = "Save as Set"
    form_template = None#"api/webscripts/save_set_form.html"
    form = SaveAsSetWebScriptForm
    displayLocation = ('ShowSummary', 'ShowArticleList')
    output_template = "api/webscripts/save_set.html"

    def run(self):
        articleids = ArticleidsScript(self.formData).run()
        result = SaveAsSetScript(self.formData).run(articleids)
        result.provenance = json.dumps(dict(self.formData))
        result.save()

        return self.outputResponse(result, object)

<|endoftext|>


<commit_before>"""The test module for the Octojobs project."""

import transaction
import pytest
from octojobs.models import Job
from octojobs.models import get_tm_session
from octojobs.models.meta import Base
import faker
from pyramid import testing
from pyramid.httpexceptions import HTTPFound

fake = faker.Faker()

DUMMY_JOBS = [Job(
    title=fake.job(),
    city=fake.city(),
    description=fake.text(100),
    company=fake.company(),
    url=fake.url()
) for i in range(10)]

@pytest.fixture(scope="session")
def configuration(request):
    """Set up a Configurator instance.

    This Configurator instance sets up a pointer to the location of the
    database. It also includes the models from the octojobs model package.
    Finally it tears everything down, including the in-memory database.

    This configuration will persist for the entire duration of your PyTest run.
    """
    settings = {
        'sqlalchemy.url': 'postgres:///test_jobs'}
    config = testing.setUp(settings=settings)
    config.include('octojobs.models')
    config.include('octojobs.routes')

    def teardown():
        testing.tearDown()

    request.addfinalizer(teardown)
    return config

@pytest.fixture
def db_session(configuration, request):
    """Create a session for interacting with the test database.

    This uses the dbsession_factory on the configurator instance to create a
    new database session. It binds that session to the available engine
    and returns a new session for every call of the dummy_request object.
    """
    SessionFactory = configuration.registry['dbsession_factory']
    session = SessionFactory()
    engine = session.bind

    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

    def teardown():
        session.transaction.rollback()

    request.addfinalizer(teardown)
    return session

@pytest.fixture
def dummy_request(db_session):
    """It creates a fake HTTP Request and database session.

    This is a function-level fixture, so every new request will have a
    new database session.
    """
    return testing.DummyRequest(dbsession=db_session)

@pytest.fixture
def add_models(dummy_request):
    """Add a bunch of model instances to the database.

    Every test that includes this fixture will add jobs.
    """
    dummy_request.dbsession.add_all(DUMMY_JOBS)

# ================ UNIT TESTS =======================

def test_new_jobs_are_added(db_session):
    """New Job objects get added to the database."""
    db_session.add_all(DUMMY_JOBS)
    query = db_session.query(Job).all()
    assert len(query) == len(DUMMY_JOBS)

def test_get_home_view_is_empty_dict(dummy_request):
    """Assert empty dict is returned, from Get request."""
    from octojobs.views.default import home_view
    assert home_view(dummy_request) == {}

def test_post_home_view_is_http_found(dummy_request):
    """Assert is instance of HTTP found, from POST request."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = home_view(dummy_request)

    assert isinstance(result, HTTPFound)

def test_post_home_view_reroutes_with_query(dummy_request):
    """Assert search terms are passed on url."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = home_view(dummy_request)

    assert 'test' and 'seattle' in result.location

def test_post_result_view_reroutes_with_new_query(dummy_request):
    """Assert search terms are passed on url on result view."""
    from octojobs.views.default import result_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = result_view(dummy_request)

    assert 'test' and 'seattle' in result.location

# ============= FUNTIONAL TESTS =====================

@pytest.fixture
def testapp(request):
    """The fixture creates a test app."""
    from webtest import TestApp
    from pyramid.config import Configurator

    def main(global_config, **settings):
        """The function returns a Pyramid WSGI application."""
        config = Configurator(settings=settings)
        config.include('pyramid_jinja2')
        config.include('.models')
        config.include('.routes')
        config.scan()
        return config.make_wsgi_app()

    app = main({}, **{
        "sqlalchemy.url": 'postgres:///test_jobs'
    })
    testapp = TestApp(app)

    SessionFactory = app.registry["dbsession_factory"]
    engine = SessionFactory().bind

    Base.metadata.create_all(bind=engine)

    def tear_down():
        Base.metadata.drop_all(bind=engine)

    request.addfinalizer(tear_down)

    return testapp

@pytest.fixture
def fill_the_db(testapp):
    """It fills the database."""
    SessionFactory = testapp.app.registry["dbsession_factory"]
    with transaction.manager:
        dbsession = get_tm_session(SessionFactory, transaction.manager)
        dbsession.add_all(DUMMY_JOBS)

# def test_search_returns_results(testapp, fill_the_db):
#     """When there's data in the database, the home page has rows."""
#     response = testapp.get('results', method="POST", location="Seattle")
#     html = response.html
#     assert html.find_all("Windows") is True
<commit_msg>added tests for only one entry in the search bar<commit_after>"""The test module for the Octojobs project."""

import transaction
import pytest
from octojobs.models import Job
from octojobs.models import get_tm_session
from octojobs.models.meta import Base
import faker
from pyramid import testing
from pyramid.httpexceptions import HTTPFound

fake = faker.Faker()

DUMMY_JOBS = [Job(
    title=fake.job(),
    city=fake.city(),
    description=fake.text(100),
    company=fake.company(),
    url=fake.url()
) for i in range(10)]

@pytest.fixture(scope="session")
def configuration(request):
    """Set up a Configurator instance.

    This Configurator instance sets up a pointer to the location of the
    database. It also includes the models from the octojobs model package.
    Finally it tears everything down, including the in-memory database.

    This configuration will persist for the entire duration of your PyTest run.
    """
    settings = {
        'sqlalchemy.url': 'postgres:///test_jobs'}
    config = testing.setUp(settings=settings)
    config.include('octojobs.models')
    config.include('octojobs.routes')

    def teardown():
        testing.tearDown()

    request.addfinalizer(teardown)
    return config

@pytest.fixture
def db_session(configuration, request):
    """Create a session for interacting with the test database.

    This uses the dbsession_factory on the configurator instance to create a
    new database session. It binds that session to the available engine
    and returns a new session for every call of the dummy_request object.
    """
    SessionFactory = configuration.registry['dbsession_factory']
    session = SessionFactory()
    engine = session.bind

    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

    def teardown():
        session.transaction.rollback()

    request.addfinalizer(teardown)
    return session

@pytest.fixture
def dummy_request(db_session):
    """It creates a fake HTTP Request and database session.

    This is a function-level fixture, so every new request will have a
    new database session.
    """
    return testing.DummyRequest(dbsession=db_session)

@pytest.fixture
def add_models(dummy_request):
    """Add a bunch of model instances to the database.

    Every test that includes this fixture will add jobs.
    """
    dummy_request.dbsession.add_all(DUMMY_JOBS)

# ================ UNIT TESTS =======================

def test_new_jobs_are_added(db_session):
    """New Job objects get added to the database."""
    db_session.add_all(DUMMY_JOBS)
    query = db_session.query(Job).all()
    assert len(query) == len(DUMMY_JOBS)

def test_get_home_view_is_empty_dict(dummy_request):
    """Assert empty dict is returned, from Get request."""
    from octojobs.views.default import home_view
    assert home_view(dummy_request) == {}

def test_post_home_view_is_http_found(dummy_request):
    """Assert is instance of HTTP found, from POST request."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = home_view(dummy_request)

    assert isinstance(result, HTTPFound)

def test_post_home_view_reroutes_with_query(dummy_request):
    """Assert search terms are passed on url."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = home_view(dummy_request)

    assert 'test' and 'seattle' in result.location

def test_post_home_view_with_only_location_query(dummy_request):
    """Test only one query with only location filled."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["location"] = "seattle"
    dummy_request.POST["searchbar"] = ""

    result = home_view(dummy_request)

    assert result.location == 'http://example.com/results?location=seattle'

def test_post_home_view_with_only_searchterm_query(dummy_request):
    """Test only one query with only searchterm filled."""
    from octojobs.views.default import home_view

    dummy_request.method = "POST"
    dummy_request.POST["location"] = ""
    dummy_request.POST["searchbar"] = "developer"

    result = home_view(dummy_request)

    assert result.location == 'http://example.com/results?search=developer'

def test_post_result_view_reroutes_with_new_query(dummy_request):
    """Assert search terms are passed on url on result view."""
    from octojobs.views.default import result_view

    dummy_request.method = "POST"
    dummy_request.POST["searchbar"] = "test"
    dummy_request.POST["location"] = "seattle"

    result = result_view(dummy_request)

    assert 'test' and 'seattle' in result.location

# ============= FUNTIONAL TESTS =====================

@pytest.fixture
def testapp(request):
    """The fixture creates a test app."""
    from webtest import TestApp
    from pyramid.config import Configurator

    def main(global_config, **settings):
        """The function returns a Pyramid WSGI application."""
        config = Configurator(settings=settings)
        config.include('pyramid_jinja2')
        config.include('.models')
        config.include('.routes')
        config.scan()
        return config.make_wsgi_app()

    app = main({}, **{
        "sqlalchemy.url": 'postgres:///test_jobs'
    })
    testapp = TestApp(app)

    SessionFactory = app.registry["dbsession_factory"]
    engine = SessionFactory().bind

    Base.metadata.create_all(bind=engine)

    def tear_down():
        Base.metadata.drop_all(bind=engine)

    request.addfinalizer(tear_down)

    return testapp

@pytest.fixture
def fill_the_db(testapp):
    """It fills the database."""
    SessionFactory = testapp.app.registry["dbsession_factory"]
    with transaction.manager:
        dbsession = get_tm_session(SessionFactory, transaction.manager)
        dbsession.add_all(DUMMY_JOBS)

# def test_search_returns_results(testapp, fill_the_db):
#     """When there's data in the database, the home page has rows."""
#     response = testapp.get('results', method="POST", location="Seattle")
#     html = response.html
#     assert html.find_all("Windows") is True
<|endoftext|>


<commit_before># when dates.format is called without a format argument, what format to use?
# FIXME: this is actually wrong - should really use the timezone correctly
DEFAULT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ"

# date formats that we know about, and should try, in order, when parsing
DATE_FORMATS = [
    "%Y-%m-%dT%H:%M:%SZ",   # e.g. 2014-09-23T11:30:45Z
    "%Y-%m-%d",             # e.g. 2014-09-23
    "%d/%m/%y",             # e.g. 29/02/80
    "%d/%m/%Y",             # e.g. 29/02/1980
    "%Y.%m.%d",             # e.g. 2014.09.12
    "%d.%m.%Y",             # e.g. 12.9.2014
    "%d.%m.%y",             # e.g. 12.9.14
    "%d %B %Y",             # e.g. 21 June 2014
    "%d-%b-%Y",             # e.g. 31-Jul-13
    "%d-%b-%y",             # e.g. 31-Jul-2013
    "%b-%y",                # e.g. Aug-13
    "%B %Y",                # e.g. February 2014
    "%Y"                    # e.g. 1978
]<commit_msg>add new date format to default list<commit_after># when dates.format is called without a format argument, what format to use?
# FIXME: this is actually wrong - should really use the timezone correctly
DEFAULT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ"

# date formats that we know about, and should try, in order, when parsing
DATE_FORMATS = [
    "%Y-%m-%dT%H:%M:%SZ",   # e.g. 2014-09-23T11:30:45Z
    "%Y-%m-%d",             # e.g. 2014-09-23
    "%d/%m/%y",             # e.g. 29/02/80
    "%d/%m/%Y",             # e.g. 29/02/1980
    "%d-%m-%Y",             # e.g. 01-01-2015
    "%Y.%m.%d",             # e.g. 2014.09.12
    "%d.%m.%Y",             # e.g. 12.9.2014
    "%d.%m.%y",             # e.g. 12.9.14
    "%d %B %Y",             # e.g. 21 June 2014
    "%d-%b-%Y",             # e.g. 31-Jul-13
    "%d-%b-%y",             # e.g. 31-Jul-2013
    "%b-%y",                # e.g. Aug-13
    "%B %Y",                # e.g. February 2014
    "%Y"                    # e.g. 1978
]<|endoftext|>


<commit_before># coding=utf-8
VERSION = (1, 2, 0, 'final', 0)

def get_version(version=None):
    """Derives a PEP386-compliant version number from VERSION."""
    if version is None:
        version = VERSION
    assert len(version) == 5
    assert version[3] in ('alpha', 'beta', 'rc', 'final')

    # Now build the two parts of the version number:
    # main = X.Y[.Z]
    # sub = .devN - for pre-alpha releases
    #     | {a|b|c}N - for alpha, beta and rc releases

    parts = 2 if version[2] == 0 else 3
    main = '.'.join(str(x) for x in version[:parts])

    sub = ''
    if version[3] != 'final':
        mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
        sub = mapping[version[3]] + str(version[4])

    return main + sub

__version__ = get_version()

def configure():
    from django.conf import settings as django_settings
    from cio.conf import settings

    # Djedi default config
    config = dict(
        ENVIRONMENT={
            'default': {
                'i18n': django_settings.LANGUAGE_CODE,
                'l10n': getattr(django_settings, 'ROOT_URLCONF', 'local').split('.', 1)[0],
                'g11n': 'global'
            }
        },
        CACHE='djedi.backends.django.cache.Backend',
        STORAGE='djedi.backends.django.db.Backend',
        PLUGINS=[
            'cio.plugins.txt.TextPlugin',
            'cio.plugins.md.MarkdownPlugin',
            'djedi.plugins.img.ImagePlugin'
        ],
        THEME='darth'
    )

    # Update config with global djedi django settings
    config.update(getattr(django_settings, 'DJEDI', {}))

    # Overwrite config with prefixed variables from django settings
    for setting in ('ENVIRONMENT', 'CACHE', 'STORAGE', 'PIPELINE', 'PLUGINS', 'THEME', 'XSS_DOMAIN'):
        conf = getattr(django_settings, 'DJEDI_%s' % setting, None)
        if conf is not None:
            config[setting] = conf

    # Configure content-io settings
    settings.configure(config)
<commit_msg>Bump version<commit_after># coding=utf-8
VERSION = (1, 2, 1, 'final', 0)

def get_version(version=None):
    """Derives a PEP386-compliant version number from VERSION."""
    if version is None:
        version = VERSION
    assert len(version) == 5
    assert version[3] in ('alpha', 'beta', 'rc', 'final')

    # Now build the two parts of the version number:
    # main = X.Y[.Z]
    # sub = .devN - for pre-alpha releases
    #     | {a|b|c}N - for alpha, beta and rc releases

    parts = 2 if version[2] == 0 else 3
    main = '.'.join(str(x) for x in version[:parts])

    sub = ''
    if version[3] != 'final':
        mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
        sub = mapping[version[3]] + str(version[4])

    return main + sub

__version__ = get_version()

def configure():
    from django.conf import settings as django_settings
    from cio.conf import settings

    # Djedi default config
    config = dict(
        ENVIRONMENT={
            'default': {
                'i18n': django_settings.LANGUAGE_CODE,
                'l10n': getattr(django_settings, 'ROOT_URLCONF', 'local').split('.', 1)[0],
                'g11n': 'global'
            }
        },
        CACHE='djedi.backends.django.cache.Backend',
        STORAGE='djedi.backends.django.db.Backend',
        PLUGINS=[
            'cio.plugins.txt.TextPlugin',
            'cio.plugins.md.MarkdownPlugin',
            'djedi.plugins.img.ImagePlugin'
        ],
        THEME='darth'
    )

    # Update config with global djedi django settings
    config.update(getattr(django_settings, 'DJEDI', {}))

    # Overwrite config with prefixed variables from django settings
    for setting in ('ENVIRONMENT', 'CACHE', 'STORAGE', 'PIPELINE', 'PLUGINS', 'THEME', 'XSS_DOMAIN'):
        conf = getattr(django_settings, 'DJEDI_%s' % setting, None)
        if conf is not None:
            config[setting] = conf

    # Configure content-io settings
    settings.configure(config)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#import argparse
#from glob import glob

from os.path import join as jp
from os.path import abspath
import os
import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-s', "--variant", help="raw variant (.vcf) file", required=True)
parser.add_argument('-b', "--bwaindex", help="Path to bwa index file.", required=True)
args = parser.parse_args()

VERBOSE=False

#Function definitions:
def log(txt, out):
    if VERBOSE:
        print(txt)
    out.write(txt+'\n')
    out.flush()

# Setup folders and paths variables:
variantFolder = abspath('03-Calls')
variantResults = abspath('03-Calls/qc')
PBS_scripts = abspath('MuTect_PBS_scripts')
normal_PBS_scripts = abspath('MuTect_PBS_scripts/normal_PBS_scripts')
tumor_PBS_scripts = abspath('MuTect_PBS_scripts/tumor_PBS_scripts')
bwaIndex = abspath(args.bwaindex)
gatkCall = 'java -jar /opt/modules/biology/gatk/3.5/bin/GenomeAnalysisTK.jar -R %s' % bwaIndex

os.system('mkdir -p %s' % bamFolder)
os.system('mkdir -p %s' % variantFolder)
os.system('mkdir -p %s' % PBS_scripts)
os.system('mkdir -p %s' % normal_PBS_scripts)
os.system('mkdir -p %s' % tumor_PBS_scripts)

##### Run pipeline ###
# for normalsample in normalsamples:
#     print "Processing", normalsample, "....."

# Set up files:
logFile = jp(variantFolder, 'GATK_filter.log')
logCommands = open(jp(normal_PBS_scripts,'GATK_filter_commands.sh'), 'w')

#Setup for qsub
log('#!/bin/bash', logCommands)
log('#PBS -N filter', logCommands)
log('#PBS -j oe', logCommands)
log('#PBS -o filter_job.log', logCommands)
log('#PBS -m abe', logCommands)
log('#PBS -M shendri4@gmail.com', logCommands)
log('#PBS -q short', logCommands)
log('#PBS -l mem=100gb', logCommands)
log(". /usr/modules/init/bash", logCommands)
log("module load python/2.7.10", logCommands)
log("module load grc", logCommands)

##################################
## Evaluate all raw variants in file to see quality of variants before filters are applied (OPTIONAL)
##################################
cmd = ' '.join([gatkCall, ' -T VariantEval ',
' -o ' + jp(variantResults, variant + 'variants.raw.eval.gatkreport.grp'),
' --eval: ' + jp(variantFolder, variant + '.vcf'),
' --doNotUseAllStandardStratifications ', ' --doNotUseAllStandardModules ', ' --evalModule CountVariants ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

###########################################################################
#### Extract the SNPs from the call set
cmd = ' '.join([gatkCall, ' -T SelectVariants ',
' -o ' + jp(variantFolder, variant + '_raw_SNPs.vcf'),
' -V ' + jp(variantFolder, variant + '.vcf'),
' -selectType SNP ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

###########################################################################
#### High quality filters
#### Filter variant sites from raw variants
cmd = ' '.join([gatkCall, ' -T VariantFiltration ',
' -V ' + jp(variantFolder, variant + '_raw_SNPs.vcf'),
' -o ' + jp(variantFolder, variant + '_filtered_SNPs.vcf'),
' --filterExpression "QD < 2.0 || FS > 60.0 || MQ < 40.0 || MQRankSum < -12.5 || ReadPosRankSum < -8.0" ',
' --filterName "BadSNP" ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

#### GQ20
cmd = ' '.join([gatkCall, ' -T VariantFiltration ',
' -o ' + jp(variantFolder, variant + '_GQ20_filtered_SNPs.vcf'),
' -V ' + jp(variantFolder, variant + '_filtered_SNPs.vcf'),
' G_filter "GQ < 20.0" ',
' G_filterName lowGQ ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

# the DP threshold should be set a 5 or 6 sigma from the mean coverage across all samples,
# so that the DP > X threshold eliminates sites with excessive coverage caused by alignment artifacts.

###########################################################################
#### Exclude non-variant loci and filtered loci (trim remaining alleles by default):
cmd = ' '.join([gatkCall, ' -T SelectVariants ',
' -o ' + jp(variantFolder, variant + '_selected_GQ20_filtered_SNPs.vcf'),
' -V ' + jp(variantFolder, variant + '_GQ20_filtered_SNPs.vcf'),
' --excludeNonVariants ', ' --excludeFiltered ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

###########################################################################
##################################
## Evaluate all variants in file
##################################
cmd = ' '.join([gatkCall, ' -T VariantEval ',
' -o ' + jp(variantResults, variant + '_selected_GQ20_filtered_SNPs.eval.gatkreport.grp'),
' --eval: ' + jp(variantFolder, variant + '_selected_GQ20_filtered_SNPs.vcf'),
' --doNotUseAllStandardStratifications ', ' --doNotUseAllStandardModules ',
' --evalModule CountVariants '
' --evalModule TiTvVariantEvaluator '
' --stratificationModule CompRod '
' --stratificationModule EvalRod '
' --stratificationModule Sample '
' --stratificationModule Filter '
' --stratificationModule FunctionalClass '
'>>', logFile, '2>&1'])
log(cmd, logCommands)

logCommands.close()

<commit_msg>trial<commit_after>#!/usr/bin/env python
#import argparse
#from glob import glob

from os.path import join as jp
from os.path import abspath
import os
import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-s', "--variant", help="raw variant (.vcf) file", required=True)
parser.add_argument('-b', "--bwaindex", help="Path to bwa index file.", required=True)
parser.add_argument('-c', "--chromosome", help="the chromosome to call", required=True)
args = parser.parse_args()

VERBOSE=False

#Function definitions:
def log(txt, out):
    if VERBOSE:
        print(txt)
    out.write(txt+'\n')
    out.flush()

## Read in samples and put them in a list:
samples = []
for l in open(args.samples):
    if len(l) > 1:
        samples.append(l.split('/')[-1].replace('.fastq.1.gz', '').strip())

# Setup folders and paths variables:
bamFolder = abspath('02-Mapped')
variantFolder = abspath('03-Calls')
jointFolder = abspath('04-Joint_Calls')
filteredFolder = abspath('05-Filtered_Calls')
PBS_scripts = abspath('filter_GATK_PBS_scripts')
bwaIndex = abspath(args.bwaindex)
gatkCall = 'java -jar /opt/modules/biology/gatk/3.5/bin/GenomeAnalysisTK.jar -R %s' % bwaIndex

os.system('mkdir -p %s' % bamFolder)
os.system('mkdir -p %s' % variantFolder)
os.system('mkdir -p %s' % filteredFolder)
os.system('mkdir -p %s' % PBS_scripts)
os.system('mkdir -p %s' % normal_PBS_scripts)
os.system('mkdir -p %s' % tumor_PBS_scripts)

logFile = jp(filteredFolder, 'chr' + args.chromosome + '_joint_GATK.log')
logCommands = open(jp(PBS_scripts, 'chr' + args.chromosome + '_joint_commands.sh'), 'w')

#Setup for qsub
log('#!/bin/bash', logCommands)
log('#PBS -N joint_%s' % (args.chromosome), logCommands)
log('#PBS -j oe', logCommands)
log('#PBS -o joint_%s_job.log' % (args.chromosome), logCommands)
log('#PBS -m abe', logCommands)
log('#PBS -M shendri4@gmail.com', logCommands)
log('#PBS -q reg', logCommands)
log('#PBS -l mem=100gb', logCommands)
log(". /usr/modules/init/bash", logCommands)
log("module load python/2.7.10", logCommands)
log("module load grc", logCommands)

# variants = []
# for sample in samples:
#     sample = ' '.join(['--variant ' + jp(variantFolder, sample) + '_chr' + args.chromosome + '.raw.snps.indels.g.vcf'])
#     variants.append(sample)
#         #variants.append(l.join(['--variant ' + jp(variantFolder, sample) + '.raw.snps.indels.g.vcf'].strip('/n').split('\t'))
# #print variants
# variantList = ' '.join(str(x) for x in variants)
# print variantList

##################################
## Evaluate all raw variants in file to see quality of variants before filters are applied (OPTIONAL)
##################################
cmd = ' '.join([gatkCall, ' -T VariantEval ',
' -o ' + jp(filteredFolder, 'chr' + args.chromosome + 'variants.raw.eval.gatkreport.grp'),
' --eval: ' + jp(jointFolder, 'chr' + args.chromosome + '_joint_variants.vcf'),
' --doNotUseAllStandardStratifications ', ' --doNotUseAllStandardModules ', ' --evalModule CountVariants ',
'>>', logFile, '2>&1'])
log(cmd, logCommands)

###########################################################################
#### Extract the SNPs from the call set
# cmd = ' '.join([gatkCall, ' -T SelectVariants ',
# ' -o ' + jp(jointFolder, 'chr' + args.chromosome + '_raw_SNPs.vcf'),
# ' -V ' + jp(jointFolder, 'chr' + args.chromosome + '_joint_variants.vcf'),
# ' -selectType SNP ',
# '>>', logFile, '2>&1'])
# log(cmd, logCommands)

###########################################################################
#### High quality filters
#### Filter variant sites from raw variants
# cmd = ' '.join([gatkCall, ' -T VariantFiltration ',
# ' -V ' + jp(jointFolder, 'chr' + args.chromosome + '_raw_SNPs.vcf'),
# ' -o ' + jp(filteredFolder, 'chr' + args.chromosome + '_filtered_SNPs.vcf'),
# ' --filterExpression "QD < 2.0 || FS > 60.0 || MQ < 40.0 || MQRankSum < -12.5 || ReadPosRankSum < -8.0" ',
# ' --filterName "BadSNP" ',
# '>>', logFile, '2>&1'])
# log(cmd, logCommands)

#### GQ20
# cmd = ' '.join([gatkCall, ' -T VariantFiltration ',
# ' -o ' + jp(filteredFolder, 'chr' + args.chromosome + '_GQ20_filtered_SNPs.vcf'),
# ' -V ' + jp(filteredFolder, 'chr' + args.chromosome + '_filtered_SNPs.vcf'),
# ' G_filter "GQ < 20.0" ',
# ' G_filterName lowGQ ',
# '>>', logFile, '2>&1'])
# log(cmd, logCommands)

# the DP threshold should be set a 5 or 6 sigma from the mean coverage across all samples,
# so that the DP > X threshold eliminates sites with excessive coverage caused by alignment artifacts.

###########################################################################
#### Exclude non-variant loci and filtered loci (trim remaining alleles by default):
# cmd = ' '.join([gatkCall, ' -T SelectVariants ',
# ' -o ' + jp(filteredFolder, 'chr' + args.chromosome + '_selected_GQ20_filtered_SNPs.vcf'),
# ' -V ' + jp(filteredFolder, 'chr' + args.chromosome + '_GQ20_filtered_SNPs.vcf'),
# ' --excludeNonVariants ', ' --excludeFiltered ',
# '>>', logFile, '2>&1'])
# log(cmd, logCommands)

###########################################################################
##################################
## Evaluate all variants in file
##################################
# cmd = ' '.join([gatkCall, ' -T VariantEval ',
# ' -o ' + jp(filteredFolder, 'chr' + args.chromosome + '_selected_GQ20_filtered_SNPs.eval.gatkreport.grp'),
# ' --eval: ' + jp(filteredFolder, 'chr' + args.chromosome + '_selected_GQ20_filtered_SNPs.vcf'),
# ' --doNotUseAllStandardStratifications ', ' --doNotUseAllStandardModules ',
# ' --evalModule CountVariants ',
# ' --evalModule TiTvVariantEvaluator ',
# ' --stratificationModule CompRod ',
# ' --stratificationModule EvalRod ',
# ' --stratificationModule Sample ',
# ' --stratificationModule Filter ',
# ' --stratificationModule FunctionalClass ',
# '>>', logFile, '2>&1'])
# log(cmd, logCommands)

logCommands.close()

<|endoftext|>


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

"""Usefull utility functions on discrete spaces (i.e., either Rn/Cn or
discretized function spaces), for example obtaining a matrix representation of
an operator. """

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()

# External
import numpy as np

# Internal
from odl.space.base_ntuples import FnBase
from odl.set.pspace import ProductSpace

def matrix_representation(op):
    """Returns a matrix representation of a linear operator.

    Parameters
    ----------
    op : :class:`~odl.Operator`
        The linear operator of which one wants a matrix representation.

    Returns
    ----------
    matrix : `numpy.ndarray`
        The matrix representation of the operator.

    Notes
    ----------
    The algorithm works by letting the operator act on all unit vectors, and
    stacking the output as a matrix.

    """

    if not op.is_linear:
        raise ValueError('The operator is not linear')

    if not (isinstance(op.domain, FnBase) or
            (isinstance(op.domain, ProductSpace) and
            all(isinstance(spc, FnBase) for spc in op.domain))):
        raise TypeError('Operator domain {} is not FnBase, nor ProductSpace '
                        'with only FnBase components'.format(op.domain))

    if not (isinstance(op.range, FnBase) or
            (isinstance(op.range, ProductSpace) and
            all(isinstance(spc, FnBase) for spc in op.range))):
        raise TypeError('Operator range {} is not FnBase, nor ProductSpace '
                        'with only FnBase components'.format(op.range))

    # Get the size of the range, and handle ProductSpace
    # Store for reuse in loop
    op_ran_is_prod_space = isinstance(op.range, ProductSpace)
    if op_ran_is_prod_space:
        num_ran = op.range.size
        n = [ran.size for ran in op.range]
    else:
        num_ran = 1
        n = [op.range.size]

    # Get the size of the domain, and handle ProductSpace
    # Store for reuse in loop
    op_dom_is_prod_space = isinstance(op.domain, ProductSpace)
    if op_dom_is_prod_space:
        num_dom = op.domain.size
        m = [dom.size for dom in op.domain]
    else:
        num_dom = 1
        m = [op.domain.size]

    # Generate the matrix
    matrix = np.zeros([np.sum(n), np.sum(m)])
    tmp_ran = op.range.element()  # Store for reuse in loop
    tmp_dom = op.domain.element()  # Store for reuse in loop
    index = 0

    for i in range(num_dom):
        for j in range(m[i]):
            tmp_dom.set_zero()
            if op_dom_is_prod_space:
                tmp_dom[i][j] = 1.0
            else:
                tmp_dom[j] = 1.0
            op(tmp_dom, out=tmp_ran)
            if op_ran_is_prod_space:
                tmp_result = np.empty(np.sum(n))
                tmp_idx = 0
                for k in range(num_ran):
                    tmp_result[tmp_idx: tmp_idx + op.range[k].size] = \
                        tmp_ran[k].asarray()
                    tmp_idx += op.range[k].size
            else:
                tmp_result = tmp_ran.asarray()
            matrix[:, index] = tmp_result
            index += 1

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

"""Usefull utility functions on discrete spaces (i.e., either Rn/Cn or
discretized function spaces), for example obtaining a matrix representation of
an operator. """

# Imports for common Python 2/3 codebase
from __future__ import print_function, division, absolute_import
from future import standard_library
standard_library.install_aliases()

# External
import numpy as np

# Internal
from odl.space.base_ntuples import FnBase
from odl.set.pspace import ProductSpace

def matrix_representation(op):
    """Returns a matrix representation of a linear operator.

    Parameters
    ----------
    op : :class:`~odl.Operator`
        The linear operator of which one wants a matrix representation.

    Returns
    ----------
    matrix : `numpy.ndarray`
        The matrix representation of the operator.

    Notes
    ----------
    The algorithm works by letting the operator act on all unit vectors, and
    stacking the output as a matrix.

    """

    if not op.is_linear:
        raise ValueError('The operator is not linear')

    if not (isinstance(op.domain, FnBase) or
            (isinstance(op.domain, ProductSpace) and
            all(isinstance(spc, FnBase) for spc in op.domain))):
        raise TypeError('Operator domain {} is not FnBase, nor ProductSpace '
                        'with only FnBase components'.format(op.domain))

    if not (isinstance(op.range, FnBase) or
            (isinstance(op.range, ProductSpace) and
            all(isinstance(spc, FnBase) for spc in op.range))):
        raise TypeError('Operator range {} is not FnBase, nor ProductSpace '
                        'with only FnBase components'.format(op.range))

    # Get the size of the range, and handle ProductSpace
    # Store for reuse in loop
    op_ran_is_prod_space = isinstance(op.range, ProductSpace)
    if op_ran_is_prod_space:
        num_ran = op.range.size
        n = [ran.size for ran in op.range]
    else:
        num_ran = 1
        n = [op.range.size]

    # Get the size of the domain, and handle ProductSpace
    # Store for reuse in loop
    op_dom_is_prod_space = isinstance(op.domain, ProductSpace)
    if op_dom_is_prod_space:
        num_dom = op.domain.size
        m = [dom.size for dom in op.domain]
    else:
        num_dom = 1
        m = [op.domain.size]

    # Generate the matrix
    matrix = np.zeros([np.sum(n), np.sum(m)])
    tmp_ran = op.range.element()  # Store for reuse in loop
    tmp_dom = op.domain.zero()  # Store for reuse in loop
    index = 0
    last_i = last_j = 0

    for i in range(num_dom):
        for j in range(m[i]):
            if op_dom_is_prod_space:
                tmp_dom[last_i][last_j] = 0.0
                tmp_dom[i][j] = 1.0
            else:
                tmp_dom[last_j] = 0.0
                tmp_dom[j] = 1.0
            op(tmp_dom, out=tmp_ran)
            if op_ran_is_prod_space:
                tmp_idx = 0
                for k in range(num_ran):
                    matrix[tmp_idx: tmp_idx + op.range[k].size, index] = (
                        tmp_ran[k])
                    tmp_idx += op.range[k].size
            else:
                matrix[:, index] = tmp_ran.asarray()
            index += 1
            last_j = j
            last_i = i

    return matrix
<|endoftext|>