<commit_before>from django_selenium.testcases import SeleniumTestCase
from metashare import settings, test_utils
from metashare.repository.seltests.test_utils import setup_screenshots_folder, import_dir
from metashare.settings import DJANGO_BASE, ROOT_PATH
from selenium import webdriver
import time
from django.core.management import call_command
from metashare.repository.models import resourceInfoType_model

TESTFIXTURE_XML = '{}/repository/test_fixtures/ELRA/'.format(ROOT_PATH)

class FilterTest(SeleniumTestCase):

    def setUp(self):
        # make sure the index does not contain any stale entries
        call_command('rebuild_index', interactive=False, using=settings.TEST_MODE_NAME)
        # load test fixture; status will be set 'published'
        test_utils.setup_test_storage()
        import_dir(TESTFIXTURE_XML)

        # init Selenium
        driver = getattr(webdriver, settings.SELENIUM_DRIVER, None)
        assert driver, "settings.SELENIUM_DRIVER contains non-existing driver"
        self.driver = driver()
        self.driver.implicitly_wait(30)
        host = getattr(settings, 'SELENIUM_TESTSERVER_HOST', 'localhost')
        port = getattr(settings, 'SELENIUM_TESTSERVER_PORT', 8000)
        self.base_url = 'http://{0}:{1}/{2}'.format(host, port, DJANGO_BASE)
        self.verification_errors = []

    def tearDown(self):
        resourceInfoType_model.objects.all().delete()

        # clean up Selenium
        self.driver.quit()
        self.assertEqual([], self.verification_errors)

    def test_filter(self):
        ss_path = setup_screenshots_folder(
          "PNG-metashare.repository.seltests.test_editor.FilterTest",
          "test_filter")

        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_id("browse").click()
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))
        self.assertEqual("17 Language Resources", driver.find_element_by_css_selector("h3").text)
        # make sure all filters are available
        self.assertEqual("Availability", driver.find_element_by_link_text("Availability").text)
        self.assertEqual("Language", driver.find_element_by_link_text("Language").text)
        self.assertEqual("Licence", driver.find_element_by_link_text("Licence").text)
        self.assertEqual("Linguality Type", driver.find_element_by_link_text("Linguality Type").text)
        self.assertEqual("Media Type", driver.find_element_by_link_text("Media Type").text)
        self.assertEqual("MIME Type", driver.find_element_by_link_text("MIME Type").text)
        self.assertEqual("Resource Type", driver.find_element_by_link_text("Resource Type").text)
        self.assertEqual("Restrictions of Use", driver.find_element_by_link_text("Restrictions of Use").text)
        # check Availability filter
        driver.find_element_by_link_text("Availability").click()
        self.assertEqual("available-restrictedUse (17)", driver.find_element_by_css_selector("dd").text)
        driver.find_element_by_link_text("Availability").click()
        # check Language filter
        driver.find_element_by_link_text("Language").click()
        self.assertEqual("English (8)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[2]").text)
        self.assertEqual("Spanish (6)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[3]").text)
        self.assertEqual("French (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[4]").text)
        self.assertEqual("Italian (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[5]").text)
        self.assertEqual("German (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[6]").text)
        self.assertEqual("Portuguese (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[7]").text)
        self.assertEqual("Arabic (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[8]").text)
        self.assertEqual("Chinese (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[9]").text)
        self.assertEqual("Estonian (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[10]").text)
        self.assertEqual("Thai (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[11]").text)
        self.assertEqual("Turkish (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[12]").text)
        self.assertEqual("more", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        # check language filter more/less
        driver.find_element_by_link_text("more").click()
        self.assertEqual("less", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        driver.find_element_by_link_text("less").click()
        self.assertEqual("more", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        driver.find_element_by_link_text("Language").click()
        # check Licence filter
        driver.find_element_by_link_text("Licence").click()
        self.assertEqual("ELRA_VAR (14)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[14]").text)
        self.assertEqual("ELRA_END_USER (13)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[15]").text)
        self.assertEqual("ELRA_EVALUATION (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[16]").text)
        driver.find_element_by_link_text("Licence").click()
        # check Linguality Type filter
        driver.find_element_by_link_text("Linguality Type").click()
        self.assertEqual("monolingual (17)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[17]").text)
        driver.find_element_by_link_text("Linguality Type").click()
        # check Media Type filter
        driver.find_element_by_link_text("Media Type").click()
        self.assertEqual("text (11)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[18]").text)
        self.assertEqual("audio (6)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[19]").text)
        driver.find_element_by_link_text("Media Type").click()
        # check MIME Type filter
        driver.find_element_by_link_text("MIME Type").click()
        self.assertEqual("Plain text (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[20]").text)
        driver.find_element_by_link_text("MIME Type").click()
        # check Resource Type filter
        driver.find_element_by_link_text("Resource Type").click()
        self.assertEqual("lexicalConceptualResource (9)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[21]").text)
        self.assertEqual("corpus (8)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[22]").text)
        driver.find_element_by_link_text("Resource Type").click()
        # check Restrictions of Use filter
        driver.find_element_by_link_text("Restrictions of Use").click()
        self.assertEqual("commercialUse (14)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[23]").text)
        self.assertEqual("academic-nonCommercialUse (13)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[24]").text)
        self.assertEqual("evaluationUse (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[25]").text)
        driver.find_element_by_link_text("Restrictions of Use").click()

        # test sorting:
        # default sorting is by resource name, ascending
        self.assertEqual("sortDesc", driver.find_element_by_xpath(
          "//a[contains(text(),'Resource Name')]").get_attribute("class"))
        self.assertEqual("AURORA-5", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/th/a").text)
        # now sort by Resource name descending
        driver.find_element_by_link_text("Resource Name").click()
        self.assertEqual("sortAsc", driver.find_element_by_xpath(
          "//a[contains(text(),'Resource Name')]").get_attribute("class"))
        self.assertEqual("VERBA Polytechnic and Plurilingual Terminological Database - S-AA Anatomy", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/th/a").text)
        # now sort by resource type ascending
        driver.find_element_by_xpath("(//a[contains(text(),'Resource Type')])[2]").click()
        self.assertEqual("sortDesc", driver.find_element_by_xpath(
          "(//a[contains(text(),'Resource Type')])[2]").get_attribute("class"))
        self.assertEqual("Corpus", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td/img").text)
        # now sort by resource type descending
        driver.find_element_by_xpath("(//a[contains(text(),'Resource Type')])[2]").click()
        self.assertEqual("sortAsc", driver.find_element_by_xpath(
          "(//a[contains(text(),'Resource Type')])[2]").get_attribute("class"))
        self.assertEqual("Lexical/Conceptual", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td/img").text)
        # now sort by media type ascending
        driver.find_element_by_link_text("Media Type(s)").click()
        self.assertEqual("sortDesc", driver.find_element_by_link_text(
          "Media Type(s)").get_attribute("class"))
        self.assertEqual("audio", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[2]/img").get_attribute("title"))
        # now sort by media type descending
        driver.find_element_by_link_text("Media Type(s)").click()
        self.assertEqual("sortAsc", driver.find_element_by_link_text(
          "Media Type(s)").get_attribute("class"))
        self.assertEqual("text", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[2]/img").get_attribute("title"))
        # now sort by language ascending
        driver.find_element_by_link_text("Language(s)").click()
        self.assertEqual("sortDesc", driver.find_element_by_link_text(
          "Language(s)").get_attribute("class"))
        self.assertEqual("Arabic, English, French", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[3]").text)
        # now sort by language descending
        driver.find_element_by_link_text("Language(s)").click()
        self.assertEqual("sortAsc", driver.find_element_by_link_text(
          "Language(s)").get_attribute("class"))
        self.assertEqual("Turkish", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[3]").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))

        # test filter application:
        # filter by language English
        driver.find_element_by_link_text("Language").click()
        driver.find_element_by_link_text("English").click()
        self.assertEqual("8 Language Resources", driver.find_element_by_css_selector("h3").text)
        # additionally filter by license
        driver.find_element_by_link_text("Licence").click()
        driver.find_element_by_link_text("ELRA_VAR").click()
        self.assertEqual("5 Language Resources", driver.find_element_by_css_selector("h3").text)
        # addtionally filter by media type
        driver.find_element_by_link_text("Media Type").click()
        driver.find_element_by_link_text("text").click()
        self.assertEqual("4 Language Resources", driver.find_element_by_css_selector("h3").text)
        # additionally filter by restriction of use
        driver.find_element_by_link_text("Restrictions of Use").click()
        driver.find_element_by_link_text("commercialUse").click()
        self.assertEqual("4 Language Resources", driver.find_element_by_css_selector("h3").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))
        # remove language filter
        driver.find_element_by_link_text("English").click()
        self.assertEqual("9 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove license filter
        driver.find_element_by_link_text("ELRA_VAR").click()
        self.assertEqual("9 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove media type filter
        driver.find_element_by_link_text("text").click()
        self.assertEqual("14 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove restiriction of use filter
        driver.find_element_by_link_text("commercialUse").click()
        self.assertEqual("17 Language Resources", driver.find_element_by_css_selector("h3").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))

<commit_msg>Updated selenium filter test to work with new results page.<commit_after>from django_selenium.testcases import SeleniumTestCase
from metashare import settings, test_utils
from metashare.repository.seltests.test_utils import setup_screenshots_folder, import_dir
from metashare.settings import DJANGO_BASE, ROOT_PATH
from selenium import webdriver
import time
from django.core.management import call_command
from metashare.repository.models import resourceInfoType_model

TESTFIXTURE_XML = '{}/repository/test_fixtures/ELRA/'.format(ROOT_PATH)

class FilterTest(SeleniumTestCase):

    def setUp(self):
        # make sure the index does not contain any stale entries
        call_command('rebuild_index', interactive=False, using=settings.TEST_MODE_NAME)
        # load test fixture; status will be set 'published'
        test_utils.setup_test_storage()
        import_dir(TESTFIXTURE_XML)

        # init Selenium
        driver = getattr(webdriver, settings.SELENIUM_DRIVER, None)
        assert driver, "settings.SELENIUM_DRIVER contains non-existing driver"
        self.driver = driver()
        self.driver.implicitly_wait(30)
        host = getattr(settings, 'SELENIUM_TESTSERVER_HOST', 'localhost')
        port = getattr(settings, 'SELENIUM_TESTSERVER_PORT', 8000)
        self.base_url = 'http://{0}:{1}/{2}'.format(host, port, DJANGO_BASE)
        self.verification_errors = []

    def tearDown(self):
        resourceInfoType_model.objects.all().delete()

        # clean up Selenium
        self.driver.quit()
        self.assertEqual([], self.verification_errors)

    def test_filter(self):
        ss_path = setup_screenshots_folder(
          "PNG-metashare.repository.seltests.test_editor.FilterTest",
          "test_filter")

        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_id("browse").click()
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))
        self.assertEqual("17 Language Resources", driver.find_element_by_css_selector("h3").text)
        # make sure all filters are available
        self.assertEqual("Availability", driver.find_element_by_link_text("Availability").text)
        self.assertEqual("Language", driver.find_element_by_link_text("Language").text)
        self.assertEqual("Licence", driver.find_element_by_link_text("Licence").text)
        self.assertEqual("Linguality Type", driver.find_element_by_link_text("Linguality Type").text)
        self.assertEqual("Media Type", driver.find_element_by_link_text("Media Type").text)
        self.assertEqual("MIME Type", driver.find_element_by_link_text("MIME Type").text)
        self.assertEqual("Resource Type", driver.find_element_by_link_text("Resource Type").text)
        self.assertEqual("Restrictions of Use", driver.find_element_by_link_text("Restrictions of Use").text)
        # check Availability filter
        driver.find_element_by_link_text("Availability").click()
        self.assertEqual("available-restrictedUse (17)", driver.find_element_by_css_selector("dd").text)
        driver.find_element_by_link_text("Availability").click()
        # check Language filter
        driver.find_element_by_link_text("Language").click()
        self.assertEqual("English (8)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[2]").text)
        self.assertEqual("Spanish (6)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[3]").text)
        self.assertEqual("French (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[4]").text)
        self.assertEqual("Italian (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[5]").text)
        self.assertEqual("German (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[6]").text)
        self.assertEqual("Portuguese (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[7]").text)
        self.assertEqual("Arabic (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[8]").text)
        self.assertEqual("Chinese (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[9]").text)
        self.assertEqual("Estonian (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[10]").text)
        self.assertEqual("Thai (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[11]").text)
        self.assertEqual("Turkish (1)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[12]").text)
        self.assertEqual("more", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        # check language filter more/less
        driver.find_element_by_link_text("more").click()
        self.assertEqual("less", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        driver.find_element_by_link_text("less").click()
        self.assertEqual("more", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[13]").text)
        driver.find_element_by_link_text("Language").click()
        # check Licence filter
        driver.find_element_by_link_text("Licence").click()
        self.assertEqual("ELRA_VAR (14)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[14]").text)
        self.assertEqual("ELRA_END_USER (13)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[15]").text)
        self.assertEqual("ELRA_EVALUATION (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[16]").text)
        driver.find_element_by_link_text("Licence").click()
        # check Linguality Type filter
        driver.find_element_by_link_text("Linguality Type").click()
        self.assertEqual("monolingual (17)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[17]").text)
        driver.find_element_by_link_text("Linguality Type").click()
        # check Media Type filter
        driver.find_element_by_link_text("Media Type").click()
        self.assertEqual("text (11)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[18]").text)
        self.assertEqual("audio (6)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[19]").text)
        driver.find_element_by_link_text("Media Type").click()
        # check MIME Type filter
        driver.find_element_by_link_text("MIME Type").click()
        self.assertEqual("Plain text (2)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[20]").text)
        driver.find_element_by_link_text("MIME Type").click()
        # check Resource Type filter
        driver.find_element_by_link_text("Resource Type").click()
        self.assertEqual("lexicalConceptualResource (9)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[21]").text)
        self.assertEqual("corpus (8)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[22]").text)
        driver.find_element_by_link_text("Resource Type").click()
        # check Restrictions of Use filter
        driver.find_element_by_link_text("Restrictions of Use").click()
        self.assertEqual("commercialUse (14)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[23]").text)
        self.assertEqual("academic-nonCommercialUse (13)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[24]").text)
        self.assertEqual("evaluationUse (3)", driver.find_element_by_xpath(
          "//dl[@id='searchFilters']/dd[25]").text)
        driver.find_element_by_link_text("Restrictions of Use").click()

        # test sorting:
        # default sorting is by resource name, ascending
        self.assertEqual("sortDesc", driver.find_element_by_xpath(
          "//a[contains(text(),'Resource Name')]").get_attribute("class"))
        self.assertEqual("AURORA-5", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/th/a").text)
        # now sort by Resource name descending
        driver.find_element_by_link_text("Resource Name").click()
        self.assertEqual("sortAsc", driver.find_element_by_xpath(
          "//a[contains(text(),'Resource Name')]").get_attribute("class"))
        self.assertEqual("VERBA Polytechnic and Plurilingual Terminological Database - S-AA Anatomy", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/th/a").text)
        # now sort by resource type ascending
        driver.find_element_by_xpath("(//a[contains(text(),'Resource Type')])[2]").click()
        self.assertEqual("sortDesc", driver.find_element_by_xpath(
          "(//a[contains(text(),'Resource Type')])[2]").get_attribute("class"))
        self.assertEqual("Corpus", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td/img/title").text)
        # now sort by resource type descending
        driver.find_element_by_xpath("(//a[contains(text(),'Resource Type')])[2]").click()
        self.assertEqual("sortAsc", driver.find_element_by_xpath(
          "(//a[contains(text(),'Resource Type')])[2]").get_attribute("class"))
        self.assertEqual("Lexical/Conceptual", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td/img").text)
        # now sort by media type ascending
        driver.find_element_by_link_text("Media Type(s)").click()
        self.assertEqual("sortDesc", driver.find_element_by_link_text(
          "Media Type(s)").get_attribute("class"))
        self.assertEqual("audio", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[2]/img").get_attribute("title"))
        # now sort by media type descending
        driver.find_element_by_link_text("Media Type(s)").click()
        self.assertEqual("sortAsc", driver.find_element_by_link_text(
          "Media Type(s)").get_attribute("class"))
        self.assertEqual("text", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[2]/img").get_attribute("title"))
        # now sort by language ascending
        driver.find_element_by_link_text("Language(s)").click()
        self.assertEqual("sortDesc", driver.find_element_by_link_text(
          "Language(s)").get_attribute("class"))
        self.assertEqual("Arabic, English, French", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[3]").text)
        # now sort by language descending
        driver.find_element_by_link_text("Language(s)").click()
        self.assertEqual("sortAsc", driver.find_element_by_link_text(
          "Language(s)").get_attribute("class"))
        self.assertEqual("Turkish", driver.find_element_by_xpath(
          "//table[@class='result_table']/tbody/tr[2]/td[3]").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))

        # test filter application:
        # filter by language English
        driver.find_element_by_link_text("Language").click()
        driver.find_element_by_link_text("English").click()
        self.assertEqual("8 Language Resources", driver.find_element_by_css_selector("h3").text)
        # additionally filter by license
        driver.find_element_by_link_text("Licence").click()
        driver.find_element_by_link_text("ELRA_VAR").click()
        self.assertEqual("5 Language Resources", driver.find_element_by_css_selector("h3").text)
        # addtionally filter by media type
        driver.find_element_by_link_text("Media Type").click()
        driver.find_element_by_link_text("text").click()
        self.assertEqual("4 Language Resources", driver.find_element_by_css_selector("h3").text)
        # additionally filter by restriction of use
        driver.find_element_by_link_text("Restrictions of Use").click()
        driver.find_element_by_link_text("commercialUse").click()
        self.assertEqual("4 Language Resources", driver.find_element_by_css_selector("h3").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))
        # remove language filter
        driver.find_element_by_link_text("English").click()
        self.assertEqual("9 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove license filter
        driver.find_element_by_link_text("ELRA_VAR").click()
        self.assertEqual("9 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove media type filter
        driver.find_element_by_link_text("text").click()
        self.assertEqual("14 Language Resources", driver.find_element_by_css_selector("h3").text)
        # remove restiriction of use filter
        driver.find_element_by_link_text("commercialUse").click()
        self.assertEqual("17 Language Resources", driver.find_element_by_css_selector("h3").text)
        driver.get_screenshot_as_file('{0}/{1}.png'.format(ss_path, time.time()))

<|endoftext|>


<commit_before>"""Fill admin recv

Create Date: 2012-08-11 17:28:35.464047

"""

# revision identifiers, used by Alembic.
from sqlalchemy.ext.declarative import declared_attr
from werkzeug import generate_password_hash

from base.core.models import BaseMixin
from base.ext import db

revision = '00000002'
down_revision = '00000001'

db.metadata.clear()

class MigrateRole(db.Model, BaseMixin):
    __tablename__ = 'auth_role'
    __table_args__ = {'extend_existing': True}
    name = db.Column(db.String(19), nullable=False, unique=True)

userroles = db.Table(
    'auth_userroles',
    db.Column('user_id', db.Integer, db.ForeignKey('auth_user.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('auth_role.id')),
    extend_existing=True,
)

class MigrateUser(db.Model, BaseMixin):
    __tablename__ = 'auth_user'
    __table_args__ = {'extend_existing': True}
    username = db.Column(db.String(50), unique=True)
    email = db.Column(db.String(120))
    active = db.Column(db.Boolean, default=True)
    _pw_hash = db.Column(db.String(199), nullable=False)

    @declared_attr
    def roles(self):
        assert self
        return db.relationship(MigrateRole, secondary=userroles, backref="auth")

def upgrade():
    admin = MigrateRole(name='admin')
    staff = MigrateRole(name='staff')
    user = MigrateUser(username='admin',
                       email='admin@admin.com',
                       _pw_hash=generate_password_hash('adminft7'))
    user.roles.append(admin)
    user.roles.append(staff)
    db.session.add(user)
    db.session.commit()

def downgrade():
    pass

# pymode:lint_ignore=E0611,E0202
<commit_msg>Fix migrations<commit_after>"""Fill admin recv

Create Date: 2012-08-11 17:28:35.464047

"""

# revision identifiers, used by Alembic.
from sqlalchemy.ext.declarative import declared_attr
from werkzeug import generate_password_hash

from base.core.models import BaseMixin
from flask_sqlalchemy import SQLAlchemy
from flask import current_app

revision = '00000002'
down_revision = '00000001'

db = SQLAlchemy(current_app)

class MigrateRole(db.Model, BaseMixin):
    __tablename__ = 'auth_role'
    __table_args__ = {'extend_existing': True}
    name = db.Column(db.String(19), nullable=False, unique=True)

userroles = db.Table(
    'auth_userroles',
    db.Column('user_id', db.Integer, db.ForeignKey('auth_user.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('auth_role.id')),
    extend_existing=True,
)

class MigrateUser(db.Model, BaseMixin):
    __tablename__ = 'auth_user'
    __table_args__ = {'extend_existing': True}
    username = db.Column(db.String(50), unique=True)
    email = db.Column(db.String(120))
    active = db.Column(db.Boolean, default=True)
    _pw_hash = db.Column(db.String(199), nullable=False)

    @declared_attr
    def roles(self):
        assert self
        return db.relationship(MigrateRole, secondary=userroles, backref="auth")

def upgrade():
    admin = MigrateRole(name='admin')
    staff = MigrateRole(name='staff')
    user = MigrateUser(username='admin',
                       email='admin@admin.com',
                       _pw_hash=generate_password_hash('adminft7'))
    user.roles.append(admin)
    user.roles.append(staff)
    db.session.add(user)
    db.session.commit()

def downgrade():
    pass

# pymode:lint_ignore=E0611,E0202
<|endoftext|>


<commit_before>'''
	@ Harris Christiansen (Harris@HarrisChristiansen.com)
	January 2016
	Generals.io Automated Client - https://github.com/harrischristiansen/generals-bot
	Generals Bot: Base Bot Class
'''

import logging
import os
from queue import Queue
import random
import threading
import time

from .client import generals
from .viewer import GeneralsViewer

# Opponent Type Definitions
OPP_EMPTY = 0
OPP_ARMY = 1
OPP_CITY = 2
OPP_GENERAL = 3

DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]

class GeneralsBot(object):
	def __init__(self, updateMethod, name="PurdueBot", gameType="private", privateRoomID="PurdueBot", gameViewer=True):
		# Save Config
		self._updateMethod = updateMethod
		self._name = name
		self._gameType = gameType
		self._privateRoomID = privateRoomID

		# ----- Start Game -----
		self._running = True
		self._move_event = threading.Event()

		# Start Game Thread
		_create_thread(self._start_game_thread)
		# Start Chat Message Thead
		_create_thread(self._start_chat_thread)
		# Start Game Move Thread
		_create_thread(self._start_moves_thread)

		# Start Game Viewer
		if (gameViewer):
			window_title = "%s (%s)" % (self._name, self._gameType)
			self._viewer = GeneralsViewer(window_title)
			self._viewer.mainViewerLoop() # Consumes Main Thread
			os._exit(0) # End Program

		while (self._running):
			time.sleep(10)

		os._exit(0) # End Program

	######################### Handle Updates From Server #########################

	def _start_game_thread(self):
		# Create Game
		if (self._gameType == "ffa"): # FFA
			self._game = generals.Generals(self._name, self._name, 'ffa')
		elif (self._gameType == "1v1"): # 1v1
			self._game = generals.Generals(self._name, self._name, '1v1')
		else: # private
			self._game = generals.Generals(self._name, self._name, 'private', gameid=self._privateRoomID)

		# Start Receiving Updates
		try:
			for update in self._game.get_updates():
				self._set_update(update)

				# Perform Make Move
				#self._make_move()
				self._move_event.set()

				# Update GeneralsViewer Grid
				selfDir = dir(self)
				if '_viewer' in selfDir:
					if '_path' in selfDir:
						self._update.path = self._path
					if '_collect_path' in selfDir:
						self._update.collect_path = self._collect_path
					if '_moves_realized' in selfDir:
						self._update.bottomText = "Realized: "+str(self._moves_realized)
					self._viewer.updateGrid(self._update)
		except ValueError: # Already in match, restart
			logging.info("Exit: Already in match in _start_update_loop")
			time.sleep(45)
			os._exit(0) # End Program

	def _set_update(self, update):
		if (update.complete):
			logging.info("!!!! Game Complete. Result = " + str(update.result) + " !!!!")
			if '_moves_realized' in dir(self):
				logging.info("Moves: %d, Realized: %d" % (self._update.turn, self._moves_realized))
			self._running = False
			os._exit(0) # End Program
			return

		self._update = update

	######################### Move Generation #########################

	def _start_moves_thread(self):
		self._moves_realized = 0
		while (self._running):
			self._move_event.wait()
			self._move_event.clear()
			self._make_move()
			self._moves_realized+=1

	def _make_move(self):
		self._updateMethod(self, self._update)

	######################### Chat Messages #########################

	def _start_chat_thread(self):
		# Send Chat Messages
		while (self._running):
			msg = str(input('Send Msg:'))
			self._game.send_chat(msg)
			time.sleep(0.7)

		return

	######################### Tile Finding #########################

	def find_largest_tile(self, ofType=None, notInPath=[], includeGeneral=False): # ofType = Integer, notInPath = [Tile], includeGeneral = False|True|Int Acceptable Largest|0.1->0.9 Ratio
		if (ofType == None):
			ofType = self._update.player_index
		general = self._update.generals[ofType]

		largest = None
		for x in range(self._update.cols): # Check Each Square
			for y in range(self._update.rows):
				tile = self._update.grid[y][x]
				if (tile.tile == ofType and (largest == None or largest.army < tile.army)): # New Largest
					if ((tile not in notInPath) and tile != general): # Exclude Path and General
						largest = tile

		if (includeGeneral > 0 and general not in notInPath): # Handle includeGeneral
			if (includeGeneral < 1):
				includeGeneral = general.army * includeGeneral
				if (includeGeneral < 6):
					includeGeneral = 6
			if (largest == None):
				largest = general
			elif (includeGeneral == True and largest.army < general.army):
				largest = general
			elif (includeGeneral > True and largest.army < general.army and largest.army <= includeGeneral):
				largest = general

		return largest

	def find_city(self, ofType=None, notOfType=None, notInPath=[], findLargest=True, includeGeneral=False): # ofType = Integer, notOfType = Integer, notInPath = [Tile], findLargest = Boolean
		if (ofType == None and notOfType == None):
			ofType = self._update.player_index

		found_city = None
		for city in self._update.cities: # Check Each City
			if (city in notInPath):
				continue

			if (city.tile == ofType or (notOfType != None and city.tile != notOfType)):
				if (found_city == None):
					found_city = city
				elif (findLargest and found_city.army < city.army) or (not findLargest and city.army < found_city.army):
					found_city = city

		if includeGeneral:
			general = self._update.generals[ofType]
			if (found_city == None):
				return general
			if (general != None and ((findLargest and general.army > found_city.army) or (not findLargest and general.army < found_city.army))):
				return general

		return found_city

	def find_closest_in_path(self, tile, path):
		closest = None
		closest_distance = 9999
		for dest in path:
			distance = self.distance(tile, dest)
			if (distance < closest_distance):
				closest = dest
				closest_distance = distance

		return closest

	def find_closest_target(self, source):
		max_target_army = source.army * 2 + 14

		closest = None
		closest_distance = 9999
		for x in range(self._update.cols): # Check Each Square
			for y in range(self._update.rows):
				dest = self._update.grid[y][x]
				if (dest.tile < generals.map.TILE_EMPTY or dest.tile == self._update.player_index or dest.army > max_target_army): # Non Target Tiles
					continue

				distance = self.distance(source, dest)
				if (dest in self._update.generals): # Generals appear closer
					distance = distance * 0.13
				elif (dest in self._update.cities): # Cities vary distance based on size, but appear closer
					distance = distance * sorted((0.22, (dest.army / (1.4*source.army)), 4))[1]
				elif (dest.tile == generals.map.TILE_EMPTY): # Empties appear further away
					distance = distance * 3.9

				if (dest.army > source.army): # Larger targets appear further away
					distance = distance * (1.4*dest.army/source.army)

				if (distance < closest_distance and self._validTarget(dest)):
					closest = dest
					closest_distance = distance

		return closest

	def find_primary_target(self, target=None):
		target_type = OPP_EMPTY - 1
		if (target != None and target.tile == self._update.player_index): # Acquired Target
			target = None
		if target != None: # Determine Previous Target Type
			target_type = OPP_EMPTY
			if target in self._update.generals:
				target_type = OPP_GENERAL
			elif target in self._update.cities:
				target_type = OPP_CITY
			elif target.army > 0:
				target_type = OPP_ARMY

		# Determine Max Target Size
		largest = self.find_largest_tile(includeGeneral=True)
		max_target_size = largest.army * 1.25

		for x in range(self._update.cols): # Check Each Tile
			for y in range(self._update.rows):
				source = self._update.grid[y][x]
				if (not self._validTarget(source)) or (source.tile == self._update.player_index): # Don't target invalid tiles
					continue

				if (target_type <= OPP_GENERAL): # Search for Generals
					if (source.tile >= 0 and source in self._update.generals and source.army < max_target_size):
						return source

				if (target_type <= OPP_CITY): # Search for Smallest Cities
					if (source in self._update.cities and source.army < max_target_size):
						if (target_type < OPP_CITY or source.army < target.army):
							target = source
							target_type = OPP_CITY

				if (target_type <= OPP_ARMY): # Search for Largest Opponent Armies
					if (source.tile >= 0 and (target == None or source.army > target.army) and source not in self._update.cities):
						target = source
						target_type = OPP_ARMY

				if (target_type < OPP_EMPTY): # Search for Empty Squares
					if (source.tile == generals.map.TILE_EMPTY and source.army < largest.army):
						target = source
						target_type = OPP_EMPTY

		return target

	######################### Pathfinding #########################

	def find_path(self, source=None, dest=None):
		# Verify Source and Dest
		if (source == None): # No Source, Use General
			source = self._update.generals[self._update.player_index]
		if (dest == None): # No Dest, Use Primary Target
			dest = self.find_primary_target()
		if (source==None or dest==None):
			return []

		# Determine Path To Destination
		frontier = Queue()
		frontier.put(source)
		came_from = {}
		came_from[source] = None

		while not frontier.empty():
			current = frontier.get()

			if current == dest: # Found Destination
				break

			for next in self._neighbors(current):
				if (next not in came_from) and (next not in self._update.cities or next.tile == self._update.player_index): # Add to frontier
					#priority = self.distance(next, dest)
					frontier.put(next)
					came_from[next] = current

		# Create Path List
		path = self._path_reconstruct(came_from, dest)

		return path

	def _path_reconstruct(self, came_from, dest):
		current = dest
		path = [current]
		try:
			while came_from[current] != None:
				current = came_from[current]
				path.append(current)
		except KeyError:
			None
		path.reverse()

		return path

	def _neighbors(self, source):
		x = source.x
		y = source.y

		neighbors = []
		for dy, dx in DIRECTIONS:
			if (self.validPosition(x+dx, y+dy)):
				current = self._update.grid[y+dy][x+dx]
				if (current.tile != generals.map.TILE_OBSTACLE or current in self._update.cities or current in self._update.generals):
					neighbors.append(current)

		return neighbors

	######################### Movement Helpers #########################

	def path_forward_moves(self, path):
		if (len(path) < 2):
			return (None, None)

		# Find largest tile in path to move forward
		largest = path[0]
		largest_index = 0
		for i, tile in enumerate(path):
			if (tile == path[-1]):
				break
			if (tile.tile == path[0].tile and tile > largest):
				largest = tile
				largest_index = i

		dest = path[largest_index+1]
		return (largest, dest)

	def toward_dest_moves(self, source, dest=None):
		# Determine Destination
		if (dest == None):
			dest = self.find_primary_target()

		# Compute X/Y Directions
		dir_y = 1
		if source.y > dest.y:
			dir_y = -1

		dir_x = 1
		if source.x > dest.x:
			dir_x = -1

		# Return List of Moves
		moves = random.sample([(0, dir_x), (dir_y, 0)],2)
		moves.extend(random.sample([(0, -dir_x), (-dir_y, 0)],2))
		return moves

	def away_king_moves(self, source):
		general = self._update.generals[self._update.player_index]

		if (source.y == general.y and source.x == general.x): # Moving from General
			return self.moves_random()

		dir_y = 1
		if source.y < general.y:
			dir_y = -1

		dir_x = 1
		if source.x < general.x:
			dir_x = -1

		moves = random.sample([(0, dir_x), (dir_y, 0)],2)
		moves.extend(random.sample([(0, -dir_x), (-dir_y, 0)],2))
		return moves

	def moves_random(self):
		return random.sample(DIRECTIONS, 4)

	def distance(self, source, dest):
		if source != None and dest != None:
			return abs(source.x - dest.x) + abs(source.y - dest.y)
		return 0

	def place_move(self, source, dest, move_half=False):
		if (self.validPosition(dest.x, dest.y)):
			self._game.move(source.y, source.x, dest.y, dest.x, move_half)
			return True
		return False

	def validPosition(self, x, y):
		return 0 <= y < self._update.rows and 0 <= x < self._update.cols and self._update._tile_grid[y][x] != generals.map.TILE_MOUNTAIN

	def _validTarget(self, target): # Check target to verify reachable
		for dy, dx in DIRECTIONS:
			if (self.validPosition(target.x+dx, target.y+dy)):
				tile = self._update.grid[target.y+dy][target.x+dx]
				if (tile.tile != generals.map.TILE_OBSTACLE or tile in self._update.cities or tile in self._update.generals):
					return True
		return False

######################### Global Helpers #########################

def _create_thread(f):
	t = threading.Thread(target=f)
	t.daemon = True
	t.start()

def _shuffle(seq):
	shuffled = list(seq)
	random.shuffle(shuffled)
	return iter(shuffled)
<commit_msg>Fixed pathfinding to cities bug<commit_after>'''
	@ Harris Christiansen (Harris@HarrisChristiansen.com)
	January 2016
	Generals.io Automated Client - https://github.com/harrischristiansen/generals-bot
	Generals Bot: Base Bot Class
'''

import logging
import os
from queue import Queue
import random
import threading
import time

from .client import generals
from .viewer import GeneralsViewer

# Opponent Type Definitions
OPP_EMPTY = 0
OPP_ARMY = 1
OPP_CITY = 2
OPP_GENERAL = 3

DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]

class GeneralsBot(object):
	def __init__(self, updateMethod, name="PurdueBot", gameType="private", privateRoomID="PurdueBot", gameViewer=True):
		# Save Config
		self._updateMethod = updateMethod
		self._name = name
		self._gameType = gameType
		self._privateRoomID = privateRoomID

		# ----- Start Game -----
		self._running = True
		self._move_event = threading.Event()

		# Start Game Thread
		_create_thread(self._start_game_thread)
		# Start Chat Message Thead
		_create_thread(self._start_chat_thread)
		# Start Game Move Thread
		_create_thread(self._start_moves_thread)

		# Start Game Viewer
		if (gameViewer):
			window_title = "%s (%s)" % (self._name, self._gameType)
			self._viewer = GeneralsViewer(window_title)
			self._viewer.mainViewerLoop() # Consumes Main Thread
			os._exit(0) # End Program

		while (self._running):
			time.sleep(10)

		os._exit(0) # End Program

	######################### Handle Updates From Server #########################

	def _start_game_thread(self):
		# Create Game
		if (self._gameType == "ffa"): # FFA
			self._game = generals.Generals(self._name, self._name, 'ffa')
		elif (self._gameType == "1v1"): # 1v1
			self._game = generals.Generals(self._name, self._name, '1v1')
		else: # private
			self._game = generals.Generals(self._name, self._name, 'private', gameid=self._privateRoomID)

		# Start Receiving Updates
		try:
			for update in self._game.get_updates():
				self._set_update(update)

				# Perform Make Move
				#self._make_move()
				self._move_event.set()

				# Update GeneralsViewer Grid
				selfDir = dir(self)
				if '_viewer' in selfDir:
					if '_path' in selfDir:
						self._update.path = self._path
					if '_collect_path' in selfDir:
						self._update.collect_path = self._collect_path
					if '_moves_realized' in selfDir:
						self._update.bottomText = "Realized: "+str(self._moves_realized)
					self._viewer.updateGrid(self._update)
		except ValueError: # Already in match, restart
			logging.info("Exit: Already in match in _start_update_loop")
			time.sleep(45)
			os._exit(0) # End Program

	def _set_update(self, update):
		if (update.complete):
			logging.info("!!!! Game Complete. Result = " + str(update.result) + " !!!!")
			if '_moves_realized' in dir(self):
				logging.info("Moves: %d, Realized: %d" % (self._update.turn, self._moves_realized))
			self._running = False
			os._exit(0) # End Program
			return

		self._update = update

	######################### Move Generation #########################

	def _start_moves_thread(self):
		self._moves_realized = 0
		while (self._running):
			self._move_event.wait()
			self._move_event.clear()
			self._make_move()
			self._moves_realized+=1

	def _make_move(self):
		self._updateMethod(self, self._update)

	######################### Chat Messages #########################

	def _start_chat_thread(self):
		# Send Chat Messages
		while (self._running):
			msg = str(input('Send Msg:'))
			self._game.send_chat(msg)
			time.sleep(0.7)

		return

	######################### Tile Finding #########################

	def find_largest_tile(self, ofType=None, notInPath=[], includeGeneral=False): # ofType = Integer, notInPath = [Tile], includeGeneral = False|True|Int Acceptable Largest|0.1->0.9 Ratio
		if (ofType == None):
			ofType = self._update.player_index
		general = self._update.generals[ofType]

		largest = None
		for x in range(self._update.cols): # Check Each Square
			for y in range(self._update.rows):
				tile = self._update.grid[y][x]
				if (tile.tile == ofType and (largest == None or largest.army < tile.army)): # New Largest
					if ((tile not in notInPath) and tile != general): # Exclude Path and General
						largest = tile

		if (includeGeneral > 0 and general not in notInPath): # Handle includeGeneral
			if (includeGeneral < 1):
				includeGeneral = general.army * includeGeneral
				if (includeGeneral < 6):
					includeGeneral = 6
			if (largest == None):
				largest = general
			elif (includeGeneral == True and largest.army < general.army):
				largest = general
			elif (includeGeneral > True and largest.army < general.army and largest.army <= includeGeneral):
				largest = general

		return largest

	def find_city(self, ofType=None, notOfType=None, notInPath=[], findLargest=True, includeGeneral=False): # ofType = Integer, notOfType = Integer, notInPath = [Tile], findLargest = Boolean
		if (ofType == None and notOfType == None):
			ofType = self._update.player_index

		found_city = None
		for city in self._update.cities: # Check Each City
			if (city in notInPath):
				continue

			if (city.tile == ofType or (notOfType != None and city.tile != notOfType)):
				if (found_city == None):
					found_city = city
				elif (findLargest and found_city.army < city.army) or (not findLargest and city.army < found_city.army):
					found_city = city

		if includeGeneral:
			general = self._update.generals[ofType]
			if (found_city == None):
				return general
			if (general != None and ((findLargest and general.army > found_city.army) or (not findLargest and general.army < found_city.army))):
				return general

		return found_city

	def find_closest_in_path(self, tile, path):
		closest = None
		closest_distance = 9999
		for dest in path:
			distance = self.distance(tile, dest)
			if (distance < closest_distance):
				closest = dest
				closest_distance = distance

		return closest

	def find_closest_target(self, source):
		max_target_army = source.army * 2 + 14

		closest = None
		closest_distance = 9999
		for x in range(self._update.cols): # Check Each Square
			for y in range(self._update.rows):
				dest = self._update.grid[y][x]
				if (dest.tile < generals.map.TILE_EMPTY or dest.tile == self._update.player_index or dest.army > max_target_army): # Non Target Tiles
					continue

				distance = self.distance(source, dest)
				if (dest in self._update.generals): # Generals appear closer
					distance = distance * 0.13
				elif (dest in self._update.cities): # Cities vary distance based on size, but appear closer
					distance = distance * sorted((0.22, (dest.army / (1.4*source.army)), 4))[1]
				elif (dest.tile == generals.map.TILE_EMPTY): # Empties appear further away
					distance = distance * 3.9

				if (dest.army > source.army): # Larger targets appear further away
					distance = distance * (1.4*dest.army/source.army)

				if (distance < closest_distance and self._validTarget(dest)):
					closest = dest
					closest_distance = distance

		return closest

	def find_primary_target(self, target=None):
		target_type = OPP_EMPTY - 1
		if (target != None and target.tile == self._update.player_index): # Acquired Target
			target = None
		if target != None: # Determine Previous Target Type
			target_type = OPP_EMPTY
			if target in self._update.generals:
				target_type = OPP_GENERAL
			elif target in self._update.cities:
				target_type = OPP_CITY
			elif target.army > 0:
				target_type = OPP_ARMY

		# Determine Max Target Size
		largest = self.find_largest_tile(includeGeneral=True)
		max_target_size = largest.army * 1.25

		for x in range(self._update.cols): # Check Each Tile
			for y in range(self._update.rows):
				source = self._update.grid[y][x]
				if (not self._validTarget(source)) or (source.tile == self._update.player_index): # Don't target invalid tiles
					continue

				if (target_type <= OPP_GENERAL): # Search for Generals
					if (source.tile >= 0 and source in self._update.generals and source.army < max_target_size):
						return source

				if (target_type <= OPP_CITY): # Search for Smallest Cities
					if (source in self._update.cities and source.army < max_target_size):
						if (target_type < OPP_CITY or source.army < target.army):
							target = source
							target_type = OPP_CITY

				if (target_type <= OPP_ARMY): # Search for Largest Opponent Armies
					if (source.tile >= 0 and (target == None or source.army > target.army) and source not in self._update.cities):
						target = source
						target_type = OPP_ARMY

				if (target_type < OPP_EMPTY): # Search for Empty Squares
					if (source.tile == generals.map.TILE_EMPTY and source.army < largest.army):
						target = source
						target_type = OPP_EMPTY

		return target

	######################### Pathfinding #########################

	def find_path(self, source=None, dest=None):
		# Verify Source and Dest
		if (source == None): # No Source, Use General
			source = self._update.generals[self._update.player_index]
		if (dest == None): # No Dest, Use Primary Target
			dest = self.find_primary_target()
		if (source==None or dest==None):
			return []

		# Determine Path To Destination
		frontier = Queue()
		frontier.put(source)
		came_from = {}
		came_from[source] = None

		while not frontier.empty():
			current = frontier.get()

			if current == dest: # Found Destination
				break

			for next in self._neighbors(current):
				if (next not in came_from) and (next not in self._update.cities or next==dest or next.tile == self._update.player_index): # Add to frontier
					#priority = self.distance(next, dest)
					frontier.put(next)
					came_from[next] = current

		# Create Path List
		path = self._path_reconstruct(came_from, dest)

		return path

	def _path_reconstruct(self, came_from, dest):
		current = dest
		path = [current]
		try:
			while came_from[current] != None:
				current = came_from[current]
				path.append(current)
		except KeyError:
			None
		path.reverse()

		return path

	def _neighbors(self, source):
		x = source.x
		y = source.y

		neighbors = []
		for dy, dx in DIRECTIONS:
			if (self.validPosition(x+dx, y+dy)):
				current = self._update.grid[y+dy][x+dx]
				if (current.tile != generals.map.TILE_OBSTACLE or current in self._update.cities or current in self._update.generals):
					neighbors.append(current)

		return neighbors

	######################### Movement Helpers #########################

	def path_forward_moves(self, path):
		if (len(path) < 2):
			return (None, None)

		# Find largest tile in path to move forward
		largest = path[0]
		largest_index = 0
		for i, tile in enumerate(path):
			if (tile == path[-1]):
				break
			if (tile.tile == path[0].tile and tile > largest):
				largest = tile
				largest_index = i

		dest = path[largest_index+1]
		return (largest, dest)

	def toward_dest_moves(self, source, dest=None):
		# Determine Destination
		if (dest == None):
			dest = self.find_primary_target()

		# Compute X/Y Directions
		dir_y = 1
		if source.y > dest.y:
			dir_y = -1

		dir_x = 1
		if source.x > dest.x:
			dir_x = -1

		# Return List of Moves
		moves = random.sample([(0, dir_x), (dir_y, 0)],2)
		moves.extend(random.sample([(0, -dir_x), (-dir_y, 0)],2))
		return moves

	def away_king_moves(self, source):
		general = self._update.generals[self._update.player_index]

		if (source.y == general.y and source.x == general.x): # Moving from General
			return self.moves_random()

		dir_y = 1
		if source.y < general.y:
			dir_y = -1

		dir_x = 1
		if source.x < general.x:
			dir_x = -1

		moves = random.sample([(0, dir_x), (dir_y, 0)],2)
		moves.extend(random.sample([(0, -dir_x), (-dir_y, 0)],2))
		return moves

	def moves_random(self):
		return random.sample(DIRECTIONS, 4)

	def distance(self, source, dest):
		if source != None and dest != None:
			return abs(source.x - dest.x) + abs(source.y - dest.y)
		return 0

	def place_move(self, source, dest, move_half=False):
		if (self.validPosition(dest.x, dest.y)):
			self._game.move(source.y, source.x, dest.y, dest.x, move_half)
			return True
		return False

	def validPosition(self, x, y):
		return 0 <= y < self._update.rows and 0 <= x < self._update.cols and self._update._tile_grid[y][x] != generals.map.TILE_MOUNTAIN

	def _validTarget(self, target): # Check target to verify reachable
		for dy, dx in DIRECTIONS:
			if (self.validPosition(target.x+dx, target.y+dy)):
				tile = self._update.grid[target.y+dy][target.x+dx]
				if (tile.tile != generals.map.TILE_OBSTACLE or tile in self._update.cities or tile in self._update.generals):
					return True
		return False

######################### Global Helpers #########################

def _create_thread(f):
	t = threading.Thread(target=f)
	t.daemon = True
	t.start()

def _shuffle(seq):
	shuffled = list(seq)
	random.shuffle(shuffled)
	return iter(shuffled)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import csv
import os

from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist

from base.models import UserProfile

class UserImporter(object):
    mapping = {
        'last_name': 'Name',
        'first_name': 'Vorname',
        'birth_date': 'Geburt',
        'admittance_date': 'Eintritt',
        'phone_number': 'Tel.',
        'mobile_phone_number': 'Handy',
        'email': 'E-Mail',
    }

    character_mapping = {
        0xe4: u'ae',
        ord(u'ö'): u'oe',
        ord(u'ü'): u'ue',
        ord(u'ß'): u'ss',
    }

    def get_users_from_csv_file(self, file_name='user.csv'):
        current_mapping = {}
        base_path = os.path.dirname(os.path.realpath(__file__))
        path = os.path.abspath(os.path.join(base_path, os.pardir))
        path = os.path.join(path, file_name)
        rows = []
        with open(path, 'rb') as csv_file:
            reader = csv.reader(csv_file, delimiter=b';', quotechar=b'"')
            for row in reader:
                if len(current_mapping) == 0:
                    current_mapping = self.get_current_mapping(row)
                    continue
                rows.append(row)

        users = []
        for row in rows:
            user = {}
            for field, index in current_mapping.iteritems():
                user[field] = row[index - 1]
            users.append(user)
        return users

    def get_current_mapping(self, row):
        current_mapping = {}
        reverted_mapping = dict((v, k) for k, v in self.mapping.iteritems())
        row_pos = 1
        for field in row:
            if field in reverted_mapping:
                current_mapping[reverted_mapping[field]] = row_pos
            row_pos += 1
        return current_mapping

    def import_users(self, users):
        for user in users:
            first_name = unicode(user['first_name'], 'utf-8')
            last_name = unicode(user['last_name'], 'utf-8')
            username = '{0}.{1}'.format(first_name[:1], last_name)
            username = username.lower()
            username = username.translate(self.character_mapping)
            try:
                User.objects.get(username=username)
                continue
            except ObjectDoesNotExist:
                pass

            u = User.objects.create_user(username, user['email'], 'test')
            u.first_name = first_name
            u.last_name = last_name
            u.save()

            from datetime import datetime
            birth_date = datetime.strptime(user['birth_date'], '%d.%m.%Y') if user['birth_date'] != '' else None
            admittance_date = datetime.strptime(user['admittance_date'], '%d.%m.%Y') if user['admittance_date'] != '' else None
            profile = UserProfile.objects.create(
                user_id=u.id,
                birth_date=birth_date,
                admittance_date=admittance_date,
                phone_number=user['phone_number'],
                mobile_phone_number=user['mobile_phone_number'],
            )
            profile.save()
<commit_msg>added training importer<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import csv
import os

from datetime import datetime
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.utils import timezone

from base.models import UserProfile, Training

class Importer(object):
    mapping = {}

    character_mapping = {
        0xe4: u'ae',
        ord(u'ö'): u'oe',
        ord(u'ü'): u'ue',
        ord(u'ß'): u'ss',
    }

    def get_current_mapping(self, row):
        current_mapping = {}
        reverted_mapping = dict((v, k) for k, v in self.mapping.iteritems())
        row_pos = 1
        for field in row:
            if field in reverted_mapping:
                current_mapping[reverted_mapping[field]] = row_pos
            row_pos += 1
        return current_mapping

class UserImporter(Importer):
    mapping = {
        'last_name': 'Name',
        'first_name': 'Vorname',
        'birth_date': 'Geburt',
        'admittance_date': 'Eintritt',
        'phone_number': 'Tel.',
        'mobile_phone_number': 'Handy',
        'email': 'E-Mail',
    }

    def get_users_from_csv_file(self, file_name='user.csv'):
        current_mapping = {}
        base_path = os.path.dirname(os.path.realpath(__file__))
        path = os.path.abspath(os.path.join(base_path, os.pardir))
        path = os.path.join(path, file_name)
        rows = []
        with open(path, 'rb') as csv_file:
            reader = csv.reader(csv_file, delimiter=b';', quotechar=b'"')
            for row in reader:
                if len(current_mapping) == 0:
                    current_mapping = self.get_current_mapping(row)
                    continue
                rows.append(row)

        users = []
        for row in rows:
            user = {}
            for field, index in current_mapping.iteritems():
                user[field] = row[index - 1]
            users.append(user)
        return users

    def import_users(self, users, editor):
        for user in users:
            first_name = unicode(user['first_name'], 'utf-8')
            last_name = unicode(user['last_name'], 'utf-8')
            username = '{0}.{1}'.format(first_name[:1], last_name)
            username = username.lower()
            username = username.translate(self.character_mapping)
            try:
                User.objects.get(username=username)
                continue
            except ObjectDoesNotExist:
                pass

            u = User.objects.create_user(username, user['email'], 'test')
            u.first_name = first_name
            u.last_name = last_name
            u.save()

            from datetime import datetime
            birth_date = datetime.strptime(user['birth_date'], '%d.%m.%Y') if user['birth_date'] != '' else None
            admittance_date = datetime.strptime(user['admittance_date'], '%d.%m.%Y') if user['admittance_date'] != '' else None
            profile = UserProfile.objects.create(
                user_id=u.id,
                birth_date=birth_date,
                admittance_date=admittance_date,
                phone_number=user['phone_number'],
                mobile_phone_number=user['mobile_phone_number'],
            )
            profile.creation_date = timezone.now()
            profile.last_update = timezone.now()
            profile.editor_id = editor.id
            profile.save()

class TrainingImporter(Importer):
    mapping = {
        'date': 'DATUM',
        'subject': 'THEMA',
        'responsibles': 'VERANTWORTLICHER',
        'note': 'SONSTIGES',
    }

    def get_trainings_from_csv_file(self, file_name='mission.csv'):
        current_mapping = {}
        base_path = os.path.dirname(os.path.realpath(__file__))
        path = os.path.abspath(os.path.join(base_path, os.pardir))
        path = os.path.join(path, file_name)
        rows = []
        with open(path, 'rb') as csv_file:
            reader = csv.reader(csv_file, delimiter=b';', quotechar=b'"')
            for row in reader:
                if len(current_mapping) == 0:
                    current_mapping = self.get_current_mapping(row)
                    continue
                rows.append(row)

        trainings = []
        for row in rows:
            training = {}
            for field, index in current_mapping.iteritems():
                training[field] = row[index - 1]
            trainings.append(training)

        for training in trainings:
            training['date'] = datetime.fromtimestamp(int(training['date']))
            training['responsibles'] = self.get_responsibles(training['responsibles'])
        return trainings

    def get_responsibles(self, raw_string):
        responsibles = []
        raw_string = unicode(raw_string, 'utf-8')
        raw_names = raw_string.split(',')
        for raw_name in raw_names:
            raw_name = raw_name.strip()
            name_parts = raw_name.split(' ')
            if len(name_parts) > 1:
                # try to get users like 'Meier, A.'
                last_name = name_parts[0]
                first_name = name_parts[1][:1]
                users = User.objects.filter(last_name=last_name, first_name__startswith=first_name)
            else:
                # try to get users like 'Meier'
                users = User.objects.filter(last_name=raw_name)

            try:
                user = users.first()
                user_profile = UserProfile.objects.get(user_id=user.id)
                responsibles.append(user_profile)
            except AttributeError:
                pass
            except ObjectDoesNotExist:
                pass
        return responsibles

    def import_trainings(self, trainings, editor):
        for training in trainings:
            try:
                Training.objects.get(date=training['date'])
                continue
            except ObjectDoesNotExist:
                pass

            t = Training.objects.create(
                date=training['date'],
                subject=training['subject'],
                note=training['note'],
            )
            t.responsibles = training['responsibles']
            t.creation_date = timezone.now()
            t.last_update = timezone.now()
            t.editor_id = editor.id
            t.save()
<|endoftext|>


<commit_before>import string
import numpy as np
import matplotlib as mpl
from pydap.client import open_url
from xml.dom import minidom as md
import matplotlib.pyplot as plt
def radar_colormap():
    nws_reflectivity_colors = [
    "#646464", # ND
    "#ccffff", # -30
    "#cc99cc", # -25
    "#996699", # -20
    "#663366", # -15
    "#cccc99", # -10
    "#999966", # -5
    "#646464", # 0
    "#04e9e7", # 5
    "#019ff4", # 10
    "#0300f4", # 15
    "#02fd02", # 20
    "#01c501", # 25
    "#008e00", # 30
    "#fdf802", # 35
    "#e5bc00", # 40
    "#fd9500", # 45
    "#fd0000", # 50
    "#d40000", # 55
    "#bc0000", # 60
    "#f800fd", # 65
    "#9854c6", # 70
    "#fdfdfd" # 75
    ]

    cmap = mpl.colors.ListedColormap(nws_reflectivity_colors)

    return cmap

def basic_http_request(full_url, return_response = False):
    #
    # just a basic http request
    #
    import urllib2

    url_request = urllib2.Request(full_url)
    #url_request.add_header('User-agent', "your user agent here")
    try:
        response = urllib2.urlopen(url_request)
        if return_response:
            return response
        else:
            del response
    except IOError, e:
        if hasattr(e, 'reason'):
            print 'We failed to reach a server.'
            print 'Reason: {}'.format(e.reason)
            print 'Full  url: {}'.format(full_url)
            raise
        elif hasattr(e, 'code'):
            print 'The server couldn\'t fulfill the request.'
            print 'Error code: {}'.format(e.code)
            print 'TDS response: {}'.format(e.read())
            print 'Full  url: {}'.format(full_url)
            raise
        else:
            print 'error not caught!'
            raise

def get_latest_data_url(site,date):
    #
    # Get the latest data for site and given day
    #
    # read the xml file, and figure out where latest.xml currently points
    #
    top_level_url = "http://thredds.ucar.edu/thredds"
    # base_url example http://thredds.ucar.edu/thredds/"catalog/nexrad/level2
    base_url = string.join([top_level_url, "catalog/nexrad/level2"],'/')

    # latest_xml_doc example http://thredds.ucar.edu/thredds/"catalog/nexrad/level2/KTLX/20130429/latest.xml
    latest_xml_doc = string.join([base_url, site, date, "latest.xml"],'/')

    xml_data = basic_http_request(latest_xml_doc, return_response = True)
    doc = md.parse(xml_data)
    root = doc.firstChild
    for cn in root.childNodes:
        if cn.nodeName == "dataset":
            dataset_url_path = cn.getAttribute("urlPath")

    latest_data_url = string.join([top_level_url, "dodsC", dataset_url_path], "/")

    return latest_data_url

# basic input
site = "KBRO"
date = "20130611"
variable = "Reflectivity"
scan = 0
#
# get latest data url
#
latest_data_url = get_latest_data_url(site,date)

# open url using pydap
dataset = open_url(latest_data_url)

# get some basic info from the global metadata
global_attrs = dataset.attributes['NC_GLOBAL']
lat = global_attrs["StationLatitude"]
lon = global_attrs["StationLongitude"]
station_id = global_attrs["StationName"]

# get data array and metadata
data = dataset["Reflectivity"][scan,::].squeeze()
theta = dataset["azimuthR"][scan,::].squeeze()
r = dataset["distanceR"][:]
data_attrs = dataset["Reflectivity"].attributes

# check for scale and offset
if data_attrs.has_key("scale_factor"):
    data = data * data_attrs["scale_factor"]

if data_attrs.has_key("add_offset"):
    data = data + data_attrs["add_offset"]

data = np.ma.masked_array(data, data < 5)

print("number of radials: {}".format(r.shape[0],))
print("number of azimuth angles: {}".format(theta.shape[0],))
print("dimensions of data: ({}, {})".format(data.shape[0],data.shape[1]))

theta = theta * np.pi / 180.
r = r / 1000.
x = np.array((np.matrix(np.cos(theta)).transpose() * np.matrix(r)))
y = np.array((np.matrix(np.sin(theta)).transpose() * np.matrix(r)))

cmap = radar_colormap()
norm = mpl.colors.Normalize(vmin=-35, vmax=80)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(0, 0, '+')
cax = ax.pcolormesh(y,x,data, cmap=cmap, norm=norm)
ax.set_xlim(-200,200)
ax.set_ylim(-200,200)
cbar = fig.colorbar(cax)
plt.show()
<commit_msg>more radar work, but notebook coming soon<commit_after>import string
import numpy as np
import matplotlib as mpl
from pydap.client import open_url
from xml.dom import minidom as md
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import datetime as dt

def radar_colormap():
    nws_reflectivity_colors = [
    "#646464", # ND
    "#ccffff", # -30
    "#cc99cc", # -25
    "#996699", # -20
    "#663366", # -15
    "#cccc99", # -10
    "#999966", # -5
    "#646464", # 0
    "#04e9e7", # 5
    "#019ff4", # 10
    "#0300f4", # 15
    "#02fd02", # 20
    "#01c501", # 25
    "#008e00", # 30
    "#fdf802", # 35
    "#e5bc00", # 40
    "#fd9500", # 45
    "#fd0000", # 50
    "#d40000", # 55
    "#bc0000", # 60
    "#f800fd", # 65
    "#9854c6", # 70
    "#fdfdfd" # 75
    ]

    cmap = mpl.colors.ListedColormap(nws_reflectivity_colors)

    return cmap

def basic_http_request(full_url, return_response = False):
    #
    # just a basic http request
    #
    import urllib2

    url_request = urllib2.Request(full_url)
    #url_request.add_header('User-agent', "your user agent here")
    try:
        response = urllib2.urlopen(url_request)
        if return_response:
            return response
        else:
            del response
    except IOError, e:
        if hasattr(e, 'reason'):
            print 'We failed to reach a server.'
            print 'Reason: {}'.format(e.reason)
            print 'Full  url: {}'.format(full_url)
            raise
        elif hasattr(e, 'code'):
            print 'The server couldn\'t fulfill the request.'
            print 'Error code: {}'.format(e.code)
            print 'TDS response: {}'.format(e.read())
            print 'Full  url: {}'.format(full_url)
            raise
        else:
            print 'error not caught!'
            raise

def get_latest_data_url(site,date):
    #
    # Get the latest data for site and given day
    #
    # read the xml file, and figure out where latest.xml currently points
    #
    top_level_url = "http://thredds.ucar.edu/thredds"
    # base_url example http://thredds.ucar.edu/thredds/"catalog/nexrad/level2
    base_url = string.join([top_level_url, "catalog/nexrad/level2"],'/')

    # latest_xml_doc example http://thredds.ucar.edu/thredds/"catalog/nexrad/level2/KTLX/20130429/latest.xml
    latest_xml_doc = string.join([base_url, site, date, "latest.xml"],'/')

    xml_data = basic_http_request(latest_xml_doc, return_response = True)
    doc = md.parse(xml_data)
    root = doc.firstChild
    for cn in root.childNodes:
        if cn.nodeName == "dataset":
            dataset_url_path = cn.getAttribute("urlPath")

    latest_data_url = string.join([top_level_url, "dodsC", dataset_url_path], "/")

    return latest_data_url

# basic input
site = "KFTG"
variable = "Reflectivity"
scan = 0
#
# get latest data url
#
date = dt.date.today().strftime("%Y%m%d")
latest_data_url = get_latest_data_url(site,date)

# open url using pydap
dataset = open_url(latest_data_url)

# get some basic info from the global metadata
global_attrs = dataset.attributes['NC_GLOBAL']
station_lat = global_attrs["StationLatitude"]
station_lon = global_attrs["StationLongitude"]
station_id = global_attrs["StationName"]

# get data array and metadata
data = dataset[variable][scan,::].squeeze()
theta = dataset["azimuthR"][scan,::].squeeze()
tilt = dataset["elevationR"][scan,0]
r = dataset["distanceR"][:]
data_attrs = dataset[variable].attributes

# check for scale and offset
if data_attrs.has_key("scale_factor"):
    data = data * data_attrs["scale_factor"]

if data_attrs.has_key("add_offset"):
    data = data + data_attrs["add_offset"]

data = np.ma.masked_array(data, data < 5)

theta = theta * np.pi / 180.
d = r * np.cos(tilt * np.pi / 180.)
x = np.array((np.matrix(np.cos(theta)).transpose() * np.matrix(d)))
y = np.array((np.matrix(np.sin(theta)).transpose() * np.matrix(d)))
height = np.abs(x.min()) + np.abs(x.max())
width = np.abs(y.min()) + np.abs(y.max())
x = x + height / 2.
y = y + width / 2.

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

m=Basemap(lat_0=station_lat,lon_0=station_lon,resolution='l',projection='laea',height=height,width=width,ax=ax)

station_x, station_y = m(station_lon, station_lat)

cmap = radar_colormap()
norm = mpl.colors.Normalize(vmin=-35, vmax=80)

ax.text(station_x, station_y, "+{}".format(site))

cax = m.pcolormesh(y, x, data, cmap=cmap, norm=norm)
m.drawcoastlines()
m.drawstates()
m.drawcountries()
cbar = m.colorbar(cax)
cbar.set_label(data_attrs["units"])
ax.set_title(station_id)
plt.show()
<|endoftext|>