<commit_before>from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url('^$', 'opendebates.views.list_ideas', name="list_ideas"),
    url(r'^healthcheck.html$', 'opendebates.views.health_check', name='health_check'),
    url(r'^test/$', 'opendebates.views.test', name='test'),

    url(r'^recent/$', 'opendebates.views.recent_activity', name='recent_activity'),
    url('^questions/(?P<id>\d+)/vote/$', 'opendebates.views.vote', name="vote"),
    url('^comment/$', 'opendebates_comments.views.post_comment',
        name="comment"),
    url('^questions/(?P<id>\d+)/$', 'opendebates.views.vote', name="show_idea"),

    url('^category/(?P<cat_id>\d+)/$', 'opendebates.views.list_category', name="list_category"),
    url('^category/(?P<cat_id>\d+)/search/$', 'opendebates.views.category_search',
        name="category_search"),
    url('^search/$', 'opendebates.views.search_ideas', name="search_ideas"),
    url('^questions/$', 'opendebates.views.questions', name="questions"),
    url('^candidates/$', 'opendebates.views.list_candidates', name="candidates"),

    url('^moderation/mark_duplicate/$', 'opendebates.moderator_views.mark_duplicate',
        name="moderation_mark_duplicate"),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^registration/', include('opendebates.registration_urls')),
]
<commit_msg>Comment out the comments URL<commit_after>from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url('^$', 'opendebates.views.list_ideas', name="list_ideas"),
    url(r'^healthcheck.html$', 'opendebates.views.health_check', name='health_check'),
    url(r'^test/$', 'opendebates.views.test', name='test'),

    url(r'^recent/$', 'opendebates.views.recent_activity', name='recent_activity'),
    url('^questions/(?P<id>\d+)/vote/$', 'opendebates.views.vote', name="vote"),
    # url('^comment/$', 'opendebates_comments.views.post_comment',
    #     name="comment"),
    url('^questions/(?P<id>\d+)/$', 'opendebates.views.vote', name="show_idea"),

    url('^category/(?P<cat_id>\d+)/$', 'opendebates.views.list_category', name="list_category"),
    url('^category/(?P<cat_id>\d+)/search/$', 'opendebates.views.category_search',
        name="category_search"),
    url('^search/$', 'opendebates.views.search_ideas', name="search_ideas"),
    url('^questions/$', 'opendebates.views.questions', name="questions"),
    url('^candidates/$', 'opendebates.views.list_candidates', name="candidates"),

    url('^moderation/mark_duplicate/$', 'opendebates.moderator_views.mark_duplicate',
        name="moderation_mark_duplicate"),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^registration/', include('opendebates.registration_urls')),
]
<|endoftext|>


<commit_before>"""
Internationalization tasks
"""
import sys
import subprocess
from path import path
from paver.easy import task, cmdopts, needs, sh

try:
    from pygments.console import colorize
except ImportError:
    colorize = lambda color, text: text  # pylint: disable-msg=invalid-name

@task
@needs(
    "pavelib.i18n.i18n_validate_gettext",
    "pavelib.assets.compile_coffeescript",
)
@cmdopts([
    ("verbose", "v", "Sets 'verbose' to True"),
])
def i18n_extract(options):
    """
    Extract localizable strings from sources
    """
    verbose = getattr(options, "verbose", None)
    cmd = "i18n_tool extract"

    if verbose:
        cmd += " -vv"

    sh(cmd)

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate():
    """
    Compile localizable strings from sources, extracting strings first.
    """
    cmd = "i18n_tool generate"
    sh(cmd)

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate_strict():
    """
    Compile localizable strings from sources, extracting strings first.
    Complains if files are missing.
    """
    cmd = "i18n_tool generate"
    sh(cmd + " --strict")

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_dummy():
    """
    Simulate international translation by generating dummy strings
    corresponding to source strings.
    """
    cmd = "i18n_tool dummy"
    sh(cmd)

@task
def i18n_validate_gettext():
    """
    Make sure GNU gettext utilities are available
    """

    returncode = subprocess.call(['which', 'xgettext'])

    if returncode != 0:
        msg = colorize(
            'red',
            "Cannot locate GNU gettext utilities, which are "
            "required by django for internationalization.\n (see "
            "https://docs.djangoproject.com/en/dev/topics/i18n/"
            "translation/#message-files)\nTry downloading them from "
            "http://www.gnu.org/software/gettext/ \n"
        )

        sys.stderr.write(msg)
        sys.exit(1)

@task
def i18n_validate_transifex_config():
    """
    Make sure config file with username/password exists
    """
    home = path('~').expanduser()
    config = home / '.transifexrc'

    if not config.isfile or config.getsize == 0:
        msg = colorize(
            'red',
            "Cannot connect to Transifex, config file is missing"
            " or empty: {config} \nSee "
            "http://help.transifex.com/features/client/#transifexrc \n".format(
                config=config,
            )
        )

        sys.stderr.write(msg)
        sys.exit(1)

@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_push():
    """
    Push source strings to Transifex for translation
    """
    cmd = "i18n_tool transifex"
    sh("{cmd} push".format(cmd=cmd))

@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_pull():
    """
    Pull translated strings from Transifex
    """
    cmd = "i18n_tool transifex"
    sh("{cmd} pull".format(cmd=cmd))

@task
def i18n_rtl():
    """
    Pull all RTL translations (reviewed AND unreviewed) from Transifex
    """
    cmd = "i18n_tool transifex"
    sh(cmd + " rtl")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"

    sh(cmd + " --rtl")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')

@task
def i18n_ltr():
    """
    Pull all LTR translations (reviewed AND unreviewed) from Transifex
    """
    cmd = "i18n_tool transifex"
    sh(cmd + " ltr")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"

    sh(cmd + " --ltr")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')

@task
@needs(
    "pavelib.i18n.i18n_transifex_pull",
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_dummy",
    "pavelib.i18n.i18n_generate_strict",
)
def i18n_robot_pull():
    """
    Pull source strings, generate po and mo files, and validate
    """
    sh('git clean -fdX conf/locale')
    # sh('paver test_i18n')  # tests were removed from repo, but there should still be tests that cover the translations...

    # Validate the recently pulled translations, and give a bail option
    cmd = "i18n_tool validate"
    sh("{cmd}".format(cmd=cmd))

    con = raw_input("Continue with committing these translations (y/n)? ")

    if con.lower() == 'y':
        sh('git add conf/locale')

        sh(
            'git commit --message="Update translations '
            '(autogenerated message)" --edit'
        )

@task
@needs(
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_transifex_push",
)
def i18n_robot_push():
    """
    Extract new strings, and push to transifex
    """
    pass
<commit_msg>Update "paver i18n_dummy" to compile assets<commit_after>"""
Internationalization tasks
"""
import sys
import subprocess
from path import path
from paver.easy import task, cmdopts, needs, sh

try:
    from pygments.console import colorize
except ImportError:
    colorize = lambda color, text: text  # pylint: disable-msg=invalid-name

@task
@needs(
    "pavelib.i18n.i18n_validate_gettext",
    "pavelib.assets.compile_coffeescript",
)
@cmdopts([
    ("verbose", "v", "Sets 'verbose' to True"),
])
def i18n_extract(options):
    """
    Extract localizable strings from sources
    """
    verbose = getattr(options, "verbose", None)
    cmd = "i18n_tool extract"

    if verbose:
        cmd += " -vv"

    sh(cmd)

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate():
    """
    Compile localizable strings from sources, extracting strings first.
    """
    cmd = "i18n_tool generate"
    sh(cmd)

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_generate_strict():
    """
    Compile localizable strings from sources, extracting strings first.
    Complains if files are missing.
    """
    cmd = "i18n_tool generate"
    sh(cmd + " --strict")

@task
@needs("pavelib.i18n.i18n_extract")
def i18n_dummy():
    """
    Simulate international translation by generating dummy strings
    corresponding to source strings.
    """
    cmd = "i18n_tool dummy"
    sh(cmd)
    # Need to then compile the new dummy strings
    cmd = "i18n_tool generate"
    sh(cmd)

@task
def i18n_validate_gettext():
    """
    Make sure GNU gettext utilities are available
    """

    returncode = subprocess.call(['which', 'xgettext'])

    if returncode != 0:
        msg = colorize(
            'red',
            "Cannot locate GNU gettext utilities, which are "
            "required by django for internationalization.\n (see "
            "https://docs.djangoproject.com/en/dev/topics/i18n/"
            "translation/#message-files)\nTry downloading them from "
            "http://www.gnu.org/software/gettext/ \n"
        )

        sys.stderr.write(msg)
        sys.exit(1)

@task
def i18n_validate_transifex_config():
    """
    Make sure config file with username/password exists
    """
    home = path('~').expanduser()
    config = home / '.transifexrc'

    if not config.isfile or config.getsize == 0:
        msg = colorize(
            'red',
            "Cannot connect to Transifex, config file is missing"
            " or empty: {config} \nSee "
            "http://help.transifex.com/features/client/#transifexrc \n".format(
                config=config,
            )
        )

        sys.stderr.write(msg)
        sys.exit(1)

@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_push():
    """
    Push source strings to Transifex for translation
    """
    cmd = "i18n_tool transifex"
    sh("{cmd} push".format(cmd=cmd))

@task
@needs("pavelib.i18n.i18n_validate_transifex_config")
def i18n_transifex_pull():
    """
    Pull translated strings from Transifex
    """
    cmd = "i18n_tool transifex"
    sh("{cmd} pull".format(cmd=cmd))

@task
def i18n_rtl():
    """
    Pull all RTL translations (reviewed AND unreviewed) from Transifex
    """
    cmd = "i18n_tool transifex"
    sh(cmd + " rtl")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"

    sh(cmd + " --rtl")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')

@task
def i18n_ltr():
    """
    Pull all LTR translations (reviewed AND unreviewed) from Transifex
    """
    cmd = "i18n_tool transifex"
    sh(cmd + " ltr")

    print("Now generating langugage files...")

    cmd = "i18n_tool generate"

    sh(cmd + " --ltr")

    print("Committing translations...")
    sh('git clean -fdX conf/locale')
    sh('git add conf/locale')
    sh('git commit --amend')

@task
@needs(
    "pavelib.i18n.i18n_transifex_pull",
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_dummy",
    "pavelib.i18n.i18n_generate_strict",
)
def i18n_robot_pull():
    """
    Pull source strings, generate po and mo files, and validate
    """
    sh('git clean -fdX conf/locale')
    # sh('paver test_i18n')  # tests were removed from repo, but there should still be tests that cover the translations...

    # Validate the recently pulled translations, and give a bail option
    cmd = "i18n_tool validate"
    sh("{cmd}".format(cmd=cmd))

    con = raw_input("Continue with committing these translations (y/n)? ")

    if con.lower() == 'y':
        sh('git add conf/locale')

        sh(
            'git commit --message="Update translations '
            '(autogenerated message)" --edit'
        )

@task
@needs(
    "pavelib.i18n.i18n_extract",
    "pavelib.i18n.i18n_transifex_push",
)
def i18n_robot_push():
    """
    Extract new strings, and push to transifex
    """
    pass
<|endoftext|>


<commit_before>#!/usr/bin/env python

''' USAGE:
      zlog-sink [<config>]

    <config> default is log.json
'''

from ansicolor import *

class Logout(object):
    def __init__(self, conf):
        self.colwidth = [0] * 5
        self.lvls = {}
        for lvl, col in conf['levels']:
            self.lvls[lvl] = eval(col)
        self.ts_format = conf['ts-format']

    def tty(self, logline):
        def wide(n, s):
            self.colwidth[n] = max(self.colwidth[n], len(s))
            return ('%-' + str(self.colwidth[n]) + 's') % s

        from json import loads
        from time import strftime
        try:
            sender, host, lvl, ts, msg = loads(logline)
        except (ValueError, TypeError) as e:
            lvl = host = sender = '?'
            ts = strftime(self.ts_format)
            msg = logline
        if lvl not in self.lvls:
            lvl = '?'
        try:
            msg = iter(msg.split('\n'))
        except AttributeError:
            tmp = repr(msg)
            msg = []
            msg.append(tmp[:40])
            i = 40
            while i < len(tmp):
                msg.append(tmp[i:i+80])
                t += 80
            msg = iter(msg)
        col = self.lvls[lvl]
        print col(wide(0, lvl)), wide(1, ts), col(wide(2, host)), cya(wide(3, sender)), msg.next()
        for m in msg:
            print self.lvls[lvl](' ------>'), m

def main():
    from env import HERE
    from sys import argv
    from json import load
    from os import environ
    from zlog import log_format
    from zero import zero, ZeroSetup
    from os.path import exists
    conf = HERE + '/log.json'
    if len(argv) > 1:
        conf = argv[1]
    with open(conf) as fin:
        conf = load(fin)['log']
    setup = ZeroSetup('pull', conf['port']).binding()
    path = conf['file']
    if path[0] != '/':
        path = HERE + '/' + path
    print 'Logger started for', setup
    print 'Logging to', path
    with open(path, 'a', 1) as fout:
        logout = Logout(conf)
        try:
            for line in zero(setup):
                fout.write(line)
                fout.write('\n')
                logout.tty(line)
        except KeyboardInterrupt:
            print 'Logger quitting.'
            sock.close()
    print 'Logger stopped on', setup

if __name__ == '__main__':
    main()
<commit_msg>Added debug output.<commit_after>#!/usr/bin/env python

''' USAGE:
      zlog-sink [<config>]

    <config> default is log.json
'''

from ansicolor import *

class Logout(object):
    def __init__(self, conf):
        self.colwidth = [0] * 5
        self.lvls = {}
        for lvl, col in conf['levels']:
            self.lvls[lvl] = eval(col)
        self.ts_format = conf['ts-format']

    def tty(self, logline):
        def wide(n, s):
            self.colwidth[n] = max(self.colwidth[n], len(s))
            return ('%-' + str(self.colwidth[n]) + 's') % s

        from json import loads
        from time import strftime
        try:
            sender, host, lvl, ts, msg = loads(logline)
        except (ValueError, TypeError) as e:
            lvl = host = sender = '?'
            ts = strftime(self.ts_format)
            msg = logline
        if lvl not in self.lvls:
            lvl = '?'
        try:
            msg = iter(msg.split('\n'))
        except AttributeError:
            tmp = repr(msg)
            msg = []
            msg.append(tmp[:40])
            i = 40
            while i < len(tmp):
                msg.append(tmp[i:i+80])
                t += 80
            msg = iter(msg)
        col = self.lvls[lvl]
        print col(wide(0, lvl)), wide(1, ts), col(wide(2, host)), cya(wide(3, sender)), msg.next()
        for m in msg:
            print self.lvls[lvl](' ------>'), m

def main():
    from env import HERE
    from sys import argv
    from json import load
    from os import environ
    from zlog import log_format
    from zero import zero, ZeroSetup
    from os.path import exists
    conf = HERE + '/log.json'
    if len(argv) > 1:
        conf = argv[1]
    print 'Loading config from', conf
    with open(conf) as fin:
        conf = load(fin)['log']
    setup = ZeroSetup('pull', conf['port']).binding()
    path = conf['file']
    if path[0] != '/':
        path = HERE + '/' + path
    print 'Logger started for', setup
    print 'Logging to', path
    with open(path, 'a', 1) as fout:
        logout = Logout(conf)
        try:
            for line in zero(setup):
                fout.write(line)
                fout.write('\n')
                logout.tty(line)
        except KeyboardInterrupt:
            print 'Logger quitting.'
            sock.close()
    print 'Logger stopped on', setup

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


<commit_before># vim:ts=4:sts=4:sw=4:expandtab

from datetime import datetime

from django.db import models

from satori.core.dbev   import Events
from satori.core.models import Entity

@ExportModel
class TestSuite(Entity):
    """Model. A group of tests, with dispatch and aggregation algorithm.
    """
    parent_entity = models.OneToOneField(Entity, parent_link=True, related_name='cast_testsuite')

    problem       = models.ForeignKey('Problem', related_name='test_suites')
    name          = models.CharField(max_length=50)
    description   = models.TextField(blank=True, default="")
    tests         = models.ManyToManyField('Test', through='TestMapping', related_name='test_suites')
    dispatcher    = models.CharField(max_length=128)
    accumulators  = models.CharField(max_length=1024)

    class Meta:                                                # pylint: disable-msg=C0111
        unique_together = (('problem', 'name'),)

    class ExportMeta(object):
        fields = [('problem', 'VIEW'), ('name', 'VIEW'), ('description', 'VIEW'), ('dispatcher', 'VIEW'), ('accumulators', 'VIEW')]

    @classmethod
    def inherit_rights(cls):
        inherits = super(TestSuite, cls).inherit_rights()
        cls._inherit_add(inherits, 'MANAGE', 'problem', 'EDIT')
        return inherits

    def save(self, *args, **kwargs):
        from satori.core.checking.dispatchers  import dispatchers
        from satori.core.checking.accumulators import accumulators
#        if not self.name:
#            self.name = str(datetime.now())
        if not self.dispatcher in dispatchers:
            raise ValueError('Dispatcher '+self.dispatcher+' is not allowed')
        for accumulator in self.accumulators.split(','):
            if not accumulator in accumulators:
                raise ValueError('Accumulator '+accumulator+' is not allowed')
        super(TestSuite,self).save(*args, **kwargs)

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoStruct('TestSuite'), DjangoIdList('Test')], PCArgField('fields', 'problem', 'MANAGE'), [CannotSetField])
    @staticmethod
    def create(fields, test_list):
        test_suite = TestSuite()
        test_suite.forbid_fields(fields, ['id'])
        test_suite.update_fields(fields, ['problem', 'name', 'description', 'dispatcher', 'accumulators'])
        test_suite.save()
        count = 0
        for test in test_list:
            count += 1
            TestMapping(suite=test_suite, test=test, order=count).save()
        return test_suite

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoId('TestSuite'), DjangoStruct('TestSuite')], PCArg('self', 'MANAGE'), [CannotSetField])
    def modify(self, fields):
        self.forbid_fields(fields, ['id', 'problem', 'dispatcher', 'accumulators'])
        self.update_fields(fields, ['name', 'description'])
        return self

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoId('TestSuite'), DjangoStruct('TestSuite'), DjangoIdList('Test')], PCArg('self', 'MANAGE'), [CannotSetField])
    def modify_full(self, fields, test_list):
        self.forbid_fields(fields, ['id', 'problem'])
        self.update_fields(fields, ['name', 'description', 'dispatcher', 'accumulators'])
        self.save()
        TestMapping.objects.filter(suite=self).delete()
        count = 0
        for test in test_list:
            count += 1
            TestMapping(suite=self, test=test, order=count).save()
        self.rejudge()
        return self

    @ExportMethod(DjangoStructList('Test'), [DjangoId('TestSuite')], PCArg('self', 'MANAGE'))
    def get_tests(self):
        return self.tests.all()

    @ExportMethod(NoneType, [DjangoId('TestSuite')], PCArg('self', 'MANAGE'))
    def rejudge(self):
        RawEvent().send(Event(type='checking_rejudge_test_suite', id=self.id))

class TestSuiteEvents(Events):
    model = TestSuite
    on_insert = on_update = ['owner', 'problem', 'name']
    on_delete = []
<commit_msg>TestSuite clean-up<commit_after># vim:ts=4:sts=4:sw=4:expandtab

from django.db import models

from satori.core.dbev   import Events
from satori.core.models import Entity

@ExportModel
class TestSuite(Entity):
    """Model. A group of tests, with dispatch and aggregation algorithm.
    """
    parent_entity = models.OneToOneField(Entity, parent_link=True, related_name='cast_testsuite')

    problem       = models.ForeignKey('Problem', related_name='test_suites')
    name          = models.CharField(max_length=50)
    description   = models.TextField(blank=True, default="")
    tests         = models.ManyToManyField('Test', through='TestMapping', related_name='test_suites')
    dispatcher    = models.CharField(max_length=128)
    accumulators  = models.CharField(max_length=1024)

    class Meta:                                                # pylint: disable-msg=C0111
        unique_together = (('problem', 'name'),)

    class ExportMeta(object):
        fields = [('problem', 'VIEW'), ('name', 'VIEW'), ('description', 'VIEW'), ('dispatcher', 'VIEW'), ('accumulators', 'VIEW')]

    @classmethod
    def inherit_rights(cls):
        inherits = super(TestSuite, cls).inherit_rights()
        cls._inherit_add(inherits, 'MANAGE', 'problem', 'EDIT')
        return inherits

    def save(self, *args, **kwargs):
        from satori.core.checking.dispatchers  import dispatchers
        from satori.core.checking.accumulators import accumulators
        if not self.dispatcher in dispatchers:
            raise ValueError('Dispatcher '+self.dispatcher+' is not allowed')
        for accumulator in self.accumulators.split(','):
            if not accumulator in accumulators:
                raise ValueError('Accumulator '+accumulator+' is not allowed')
        super(TestSuite,self).save(*args, **kwargs)

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoStruct('TestSuite'), DjangoIdList('Test')], PCArgField('fields', 'problem', 'MANAGE'), [CannotSetField])
    @staticmethod
    def create(fields, test_list):
        test_suite = TestSuite()
        test_suite.forbid_fields(fields, ['id'])
        test_suite.update_fields(fields, ['problem', 'name', 'description', 'dispatcher', 'accumulators'])
        test_suite.save()
        count = 0
        for test in test_list:
            count += 1
            TestMapping(suite=test_suite, test=test, order=count).save()
        return test_suite

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoId('TestSuite'), DjangoStruct('TestSuite')], PCArg('self', 'MANAGE'), [CannotSetField])
    def modify(self, fields):
        self.forbid_fields(fields, ['id', 'problem', 'dispatcher', 'accumulators'])
        self.update_fields(fields, ['name', 'description'])
        return self

    @ExportMethod(DjangoStruct('TestSuite'), [DjangoId('TestSuite'), DjangoStruct('TestSuite'), DjangoIdList('Test')], PCArg('self', 'MANAGE'), [CannotSetField])
    def modify_full(self, fields, test_list):
        self.forbid_fields(fields, ['id', 'problem'])
        self.update_fields(fields, ['name', 'description', 'dispatcher', 'accumulators'])
        self.save()
        TestMapping.objects.filter(suite=self).delete()
        count = 0
        for test in test_list:
            count += 1
            TestMapping(suite=self, test=test, order=count).save()
        self.rejudge()
        return self

    @ExportMethod(DjangoStructList('Test'), [DjangoId('TestSuite')], PCArg('self', 'MANAGE'))
    def get_tests(self):
        return self.tests.all()

    @ExportMethod(NoneType, [DjangoId('TestSuite')], PCArg('self', 'MANAGE'))
    def rejudge(self):
        RawEvent().send(Event(type='checking_rejudge_test_suite', id=self.id))

class TestSuiteEvents(Events):
    model = TestSuite
    on_insert = on_update = ['owner', 'problem', 'name']
    on_delete = []
<|endoftext|>


<commit_before>SECRET_KEY = "lorem ipsum"

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sites',

    'tango-shared-core',
    'easy-thumbnails',
    'tango_comments',
)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

SITE_ID = 1

ROOT_URLCONF = 'tango_comments.urls'

AUTH_USER_MODEL = 'auth.User'

#stripped down middleware
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
<commit_msg>Added thumbnails and tango-shared<commit_after>SECRET_KEY = "lorem ipsum"

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sites',

    'tango_shared',
    'easy_thumbnails',
    'tango_comments',
)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

SITE_ID = 1

ROOT_URLCONF = 'tango_comments.urls'

AUTH_USER_MODEL = 'auth.User'

#stripped down middleware
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
<|endoftext|>


<commit_before>"""
The definition of the base pulse object that generates pulse waveforms.

The pulse objects represent an analytical form of the pulses, and can generate
the waveforms for the time-values that are passed in to its waveform generation
function.

The actual pulse implementations are defined in separate modules,
e.g. pulse_library.py.

The module variable `pulse_libraries` is a
"""

pulse_libraries = set()
"""set of module: The set of pulse implementation libraries.

These will be searched when a pulse dictionary is converted to the pulse object.
The pulse class is stored as a string in a pulse dictionary.

Each pulse library module should add itself to this set, e.g.
>>> import sys
>>> from pyceqed.measurement.waveform_control import pulse
>>> pulse.pulse_libraries.add(sys.modules[__name__])
"""

class Pulse(object):
    """
    The pulse base class.

    Args:
        name (str): The name of the pulse, used for referencing to other pulses
            in a sequence. Typically generated automatically by the `Segment`
            class.
        element_name (str): Name of the element the pulse should be played in.
        codeword (int or 'no_codeword'): The codeword that the pulse belongs in.
            Defaults to 'no_codeword'.
        length (float, optional): The length of the pulse instance in seconds.
            Defaults to 0.
        channels (list of str, optional): A list of channel names that the pulse
            instance generates waveforms form. Defaults to empty list.
    """

    def __init__(self, name, element_name, **kw):

        self.name = name
        self.element_name = element_name
        self.codeword = kw.pop('codeword', 'no_codeword')

        # Set default pulse_params and overwrite with params in keyword argument list if applicable
        for k, v in self.pulse_params().items():
            setattr(self, k, (v if k not in kw else kw[k]))

        self._t0 = None

    def waveforms(self, tvals_dict):
        """Generate waveforms for any channels of the pulse.

        Calls `Pulse.chan_wf` internally.

        Args:
            tvals_dict (dict of np.ndarray): a dictionary of the sample
                start times for the channels to generate the waveforms for.

        Returns:
            dict of np.ndarray: a dictionary of the voltage-waveforms for the
            channels that are both in the tvals_dict and in the
            pulse channels list.
        """
        wfs_dict = {}
        for c in self.channels:
            if c in tvals_dict:
                wfs_dict[c] = self.chan_wf(c, tvals_dict[c])
        return wfs_dict

    def pulse_area(self, channel, tvals):
        """
        Calculates the area of a pulse on the given channel and time-interval.

        Args:
            channel (str): The channel name
            tvals (np.ndarray): the sample start-times

        Returns:
            float: The pulse area.
        """
        wfs = self.chan_wf(channel, tvals)
        dt = tvals[1] - tvals[0]

        return sum(wfs) * dt

    def algorithm_time(self, val=None):
        """
        Getter and setter for the start time of the pulse.
        """
        if val is None:
            return self._t0
        else:
            self._t0 = val

    def element_time(self, element_start_time):
        """
        Returns the pulse time in the element frame.
        """
        return self.algorithm_time() - element_start_time

    def hashables(self, tstart, channel):
        """Abstract base method for a list of hash-elements for this pulse.

        The hash-elements must uniquely define the returned waveform as it is
        used to determine whether waveforms can be reused.

        Args:
            tstart (float): start time of the element
            channel (str): channel name

        Returns:
            list: A list of hash-elements
        """
        raise NotImplementedError('hashables() not implemented for {}'
                                  .format(str(type(self))[1:-1]))

    def chan_wf(self, channel, tvals):
        """Abstract base method for generating the pulse waveforms.

        Args:
            channel (str): channel name
            tvals (np.ndarray): the sample start times

        Returns:
            np.ndarray: the waveforms corresponding to `tvals` on
            `channel`
        """
        raise NotImplementedError('chan_wf() not implemented for {}'
                                  .format(str(type(self))[1:-1]))

    @classmethod
    def pulse_params(cls):
        """
        Returns a dictionary of pulse parameters and initial values.
        """
        raise NotImplementedError('pulse_params() not implemented for your pulse')
<commit_msg>remove unnecessary inherit from object for Pulse<commit_after>"""
The definition of the base pulse object that generates pulse waveforms.

The pulse objects represent an analytical form of the pulses, and can generate
the waveforms for the time-values that are passed in to its waveform generation
function.

The actual pulse implementations are defined in separate modules,
e.g. pulse_library.py.

The module variable `pulse_libraries` is a
"""

pulse_libraries = set()
"""set of module: The set of pulse implementation libraries.

These will be searched when a pulse dictionary is converted to the pulse object.
The pulse class is stored as a string in a pulse dictionary.

Each pulse library module should add itself to this set, e.g.
>>> import sys
>>> from pyceqed.measurement.waveform_control import pulse
>>> pulse.pulse_libraries.add(sys.modules[__name__])
"""

class Pulse:
    """
    The pulse base class.

    Args:
        name (str): The name of the pulse, used for referencing to other pulses
            in a sequence. Typically generated automatically by the `Segment`
            class.
        element_name (str): Name of the element the pulse should be played in.
        codeword (int or 'no_codeword'): The codeword that the pulse belongs in.
            Defaults to 'no_codeword'.
        length (float, optional): The length of the pulse instance in seconds.
            Defaults to 0.
        channels (list of str, optional): A list of channel names that the pulse
            instance generates waveforms form. Defaults to empty list.
    """

    def __init__(self, name, element_name, **kw):

        self.name = name
        self.element_name = element_name
        self.codeword = kw.pop('codeword', 'no_codeword')

        # Set default pulse_params and overwrite with params in keyword argument list if applicable
        for k, v in self.pulse_params().items():
            setattr(self, k, (v if k not in kw else kw[k]))

        self._t0 = None

    def waveforms(self, tvals_dict):
        """Generate waveforms for any channels of the pulse.

        Calls `Pulse.chan_wf` internally.

        Args:
            tvals_dict (dict of np.ndarray): a dictionary of the sample
                start times for the channels to generate the waveforms for.

        Returns:
            dict of np.ndarray: a dictionary of the voltage-waveforms for the
            channels that are both in the tvals_dict and in the
            pulse channels list.
        """
        wfs_dict = {}
        for c in self.channels:
            if c in tvals_dict:
                wfs_dict[c] = self.chan_wf(c, tvals_dict[c])
        return wfs_dict

    def pulse_area(self, channel, tvals):
        """
        Calculates the area of a pulse on the given channel and time-interval.

        Args:
            channel (str): The channel name
            tvals (np.ndarray): the sample start-times

        Returns:
            float: The pulse area.
        """
        wfs = self.chan_wf(channel, tvals)
        dt = tvals[1] - tvals[0]

        return sum(wfs) * dt

    def algorithm_time(self, val=None):
        """
        Getter and setter for the start time of the pulse.
        """
        if val is None:
            return self._t0
        else:
            self._t0 = val

    def element_time(self, element_start_time):
        """
        Returns the pulse time in the element frame.
        """
        return self.algorithm_time() - element_start_time

    def hashables(self, tstart, channel):
        """Abstract base method for a list of hash-elements for this pulse.

        The hash-elements must uniquely define the returned waveform as it is
        used to determine whether waveforms can be reused.

        Args:
            tstart (float): start time of the element
            channel (str): channel name

        Returns:
            list: A list of hash-elements
        """
        raise NotImplementedError('hashables() not implemented for {}'
                                  .format(str(type(self))[1:-1]))

    def chan_wf(self, channel, tvals):
        """Abstract base method for generating the pulse waveforms.

        Args:
            channel (str): channel name
            tvals (np.ndarray): the sample start times

        Returns:
            np.ndarray: the waveforms corresponding to `tvals` on
            `channel`
        """
        raise NotImplementedError('chan_wf() not implemented for {}'
                                  .format(str(type(self))[1:-1]))

    @classmethod
    def pulse_params(cls):
        """
        Returns a dictionary of pulse parameters and initial values.
        """
        raise NotImplementedError('pulse_params() not implemented for your pulse')
<|endoftext|>


<commit_before>from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread, QStandardPaths

from functools import partial
from os import path, makedirs
from webbrowser import open as webopen
from sys import platform
from json import loads

from API.CurseAPI import CurseAPI, CurseProject, CurseModpack
from API.MultiMC import MultiMC, MultiMCInstance

from CurseMetaDB.DB import DB

from Utils.Utils import clear_layout, confirm_box
from Utils.Updater import UpdateCheckThread
from Utils.Logger import *
from Utils.Config import Config, Setting

from GUI.MainWindow import Ui_MainWindow

from GUI.InstanceWindowWrapper import InstanceWindow

from GUI.UpdateDialog import Ui_UpdateDialog

from GUI.FileDialogWrapper import FileDialog
from GUI.DownloadDialogWrapper import DownloadDialog
from GUI.InitialSetupWrapper import SetupWindow

from GUI.InstanceWidget import Ui_InstanceWidget
from GUI.PackWidget import Ui_PackWidget

class MainWindow:
    def __init__(self):
        data_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation)
        cache_dir = QStandardPaths.writableLocation(QStandardPaths.CacheLocation)
        if not path.exists(data_dir):
            makedirs(data_dir)
        if not path.exists(cache_dir):
            makedirs(cache_dir)

        info("Data dir: {}".format(data_dir))
        info("Cache dir: {}".format(cache_dir))

        if not path.isfile(path.join(data_dir, "settings.ini")):
            dia = SetupWindow(data_dir, cache_dir)
            dia.win.exec_()

        self.conf = Config(data_dir)
        self.db = DB(loads(open(path.join(cache_dir, "meta.json")).read()))
        self.curse = CurseAPI(self.db)

        self.conf.write(Setting.current_version, self.curse.version)

        self.win = QMainWindow()

        self.mmc = MultiMC(self.conf.read(Setting.location))

        self.children = list()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.win)

        self.init_instances()

        self.ui.mmc_folder.setText(self.conf.read(Setting.location))

        self.ui.analytics_check.setChecked(self.conf.read(Setting.analytics))
        self.ui.analytics_check.clicked.connect(self.analytics_checked)

        self.ui.update_check.setChecked(self.conf.read(Setting.update))
        self.ui.update_check.clicked.connect(self.update_checked)

        self.ui.file_check.setChecked(self.conf.read(Setting.ask_file))
        self.ui.file_check.clicked.connect(self.file_checked)

        self.ui.pack_search.textChanged.connect(self.q_typed)
        self.ui.pack_search.returnPressed.connect(self.search_packs)
        self.ui.pack_search_button.clicked.connect(self.search_packs)

        self.win.setWindowTitle("OpenMineMods v{}".format(self.curse.version))

        self.init_packs(self.curse.get_modpacks())

        self.win.show()

        if self.conf.read(Setting.update):
            self.update_tr = QThread()
            self.uc = UpdateCheckThread(self.curse)
            self.uc.done.connect(self.update_check_done)

            self.uc.moveToThread(self.update_tr)
            self.update_tr.started.connect(self.uc.check_updates)

            self.update_tr.start()

    """UI Initializations"""

    def init_instances(self):
        clear_layout(self.ui.instance_box)

        for instance in self.mmc.instances:
            widget = QWidget()
            el = Ui_InstanceWidget()

            el.setupUi(widget)

            el.instance_delete.clicked.connect(partial(self.delete_clicked, instance))
            el.instance_edit.clicked.connect(partial(self.edit_clicked, instance))

            el.instance_update.hide()

            el.instance_name.setText(instance.name)

            self.ui.instance_box.addWidget(widget)

        self.ui.instance_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    def init_packs(self, packs: list):
        clear_layout(self.ui.pack_box)

        for pack in packs:
            widget = QWidget()
            el = Ui_PackWidget()

            el.setupUi(widget)

            el.pack_name.setText("{} (MC {})".format(pack.name, pack.versions[-1]))
            el.pack_install.clicked.connect(partial(self.install_clicked, pack))
            el.pack_more.clicked.connect(partial(webopen, pack.page))
            self.ui.pack_box.addWidget(widget)

        self.ui.pack_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    def reset_packs(self):
        clear_layout(self.ui.pack_box)

        self.ui.pack_box.addWidget(self.ui.loading_label)
        self.ui.pack_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    """Event Listeners"""

    def q_typed(self):
        if not self.conf.read(Setting.live_search):
            return
        if self.ui.pack_search.text() == "":
            self.init_packs(self.curse.get_modpacks())
            return
        self.init_packs(self.curse.search(self.ui.pack_search.text(), "modpack"))

    def search_packs(self):
        if self.ui.pack_search.text() == "":
            self.init_packs(self.curse.get_modpacks())
            return
        self.init_packs(self.curse.search(self.ui.pack_search.text(), "modpack"))

    def delete_clicked(self, instance: MultiMCInstance):
        print(confirm_box(self.win, QMessageBox.Question,
                          "Are you sure you want to delete {}".format(instance.name)))

    def edit_clicked(self, instance: MultiMCInstance):
        self.children.append(InstanceWindow(instance, self.curse, self.conf))

    def install_clicked(self, project: CurseProject):
        fs = project.files
        if self.conf.read(Setting.ask_file):
            dia = FileDialog(fs)
            f = dia.dia.exec_()
            if not f:
                return

            f = fs[f - 1]

        else:
            f = fs[0]

        dia = DownloadDialog()
        dia.download_pack(project, self.curse.get_file(f), self.curse, self.mmc)
        self.mmc = MultiMC(self.conf.read(Setting.location))
        self.init_instances()

    # Settings Checkboxes

    def analytics_checked(self):
        self.conf.write(Setting.analytics, self.ui.analytics_check.isChecked())

    def update_checked(self):
        self.conf.write(Setting.update, self.ui.update_check.isChecked())

    def file_checked(self):
        self.conf.write(Setting.ask_file, self.ui.file_check.isChecked())

    # Update Checker

    def update_check_done(self, res: dict):
        if not res["res"] or not res["update"]["downloads"][platform]:
            return

        up_win = QDialog()
        upd = Ui_UpdateDialog()
        upd.setupUi(up_win)

        upd.textBrowser.setText(res["update"]["changelog"])

        if not up_win.exec_():
            return

    def data_found(self, dat: dict):
        if len(dat["res"]) < 1 and dat["succ"]:
            return

        if dat["type"] == "packs":
            if not dat["succ"]:
                err("Pack loading failed!")
                self.ui.loading_label.setText("Network Error!")
                return
            self.init_packs(dat["res"])
<commit_msg>actually delete instance<commit_after>from PyQt5.QtWidgets import *
from PyQt5.QtCore import QThread, QStandardPaths

from functools import partial
from os import path, makedirs
from webbrowser import open as webopen
from sys import platform
from json import loads

from API.CurseAPI import CurseAPI, CurseProject, CurseModpack
from API.MultiMC import MultiMC, MultiMCInstance

from CurseMetaDB.DB import DB

from Utils.Utils import clear_layout, confirm_box
from Utils.Updater import UpdateCheckThread
from Utils.Logger import *
from Utils.Config import Config, Setting

from GUI.MainWindow import Ui_MainWindow

from GUI.InstanceWindowWrapper import InstanceWindow

from GUI.UpdateDialog import Ui_UpdateDialog

from GUI.FileDialogWrapper import FileDialog
from GUI.DownloadDialogWrapper import DownloadDialog
from GUI.InitialSetupWrapper import SetupWindow

from GUI.InstanceWidget import Ui_InstanceWidget
from GUI.PackWidget import Ui_PackWidget

class MainWindow:
    def __init__(self):
        data_dir = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation)
        cache_dir = QStandardPaths.writableLocation(QStandardPaths.CacheLocation)
        if not path.exists(data_dir):
            makedirs(data_dir)
        if not path.exists(cache_dir):
            makedirs(cache_dir)

        info("Data dir: {}".format(data_dir))
        info("Cache dir: {}".format(cache_dir))

        if not path.isfile(path.join(data_dir, "settings.ini")):
            dia = SetupWindow(data_dir, cache_dir)
            dia.win.exec_()

        self.conf = Config(data_dir)
        self.db = DB(loads(open(path.join(cache_dir, "meta.json")).read()))
        self.curse = CurseAPI(self.db)

        self.conf.write(Setting.current_version, self.curse.version)

        self.win = QMainWindow()

        self.mmc = MultiMC(self.conf.read(Setting.location))

        self.children = list()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.win)

        self.init_instances()

        self.ui.mmc_folder.setText(self.conf.read(Setting.location))

        self.ui.analytics_check.setChecked(self.conf.read(Setting.analytics))
        self.ui.analytics_check.clicked.connect(self.analytics_checked)

        self.ui.update_check.setChecked(self.conf.read(Setting.update))
        self.ui.update_check.clicked.connect(self.update_checked)

        self.ui.file_check.setChecked(self.conf.read(Setting.ask_file))
        self.ui.file_check.clicked.connect(self.file_checked)

        self.ui.pack_search.textChanged.connect(self.q_typed)
        self.ui.pack_search.returnPressed.connect(self.search_packs)
        self.ui.pack_search_button.clicked.connect(self.search_packs)

        self.win.setWindowTitle("OpenMineMods v{}".format(self.curse.version))

        self.init_packs(self.curse.get_modpacks())

        self.win.show()

        if self.conf.read(Setting.update):
            self.update_tr = QThread()
            self.uc = UpdateCheckThread(self.curse)
            self.uc.done.connect(self.update_check_done)

            self.uc.moveToThread(self.update_tr)
            self.update_tr.started.connect(self.uc.check_updates)

            self.update_tr.start()

    """UI Initializations"""

    def init_instances(self):
        clear_layout(self.ui.instance_box)

        for instance in self.mmc.instances:
            widget = QWidget()
            el = Ui_InstanceWidget()

            el.setupUi(widget)

            el.instance_delete.clicked.connect(partial(self.delete_clicked, instance))
            el.instance_edit.clicked.connect(partial(self.edit_clicked, instance))

            el.instance_update.hide()

            el.instance_name.setText(instance.name)

            self.ui.instance_box.addWidget(widget)

        self.ui.instance_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    def init_packs(self, packs: list):
        clear_layout(self.ui.pack_box)

        for pack in packs:
            widget = QWidget()
            el = Ui_PackWidget()

            el.setupUi(widget)

            el.pack_name.setText("{} (MC {})".format(pack.name, pack.versions[-1]))
            el.pack_install.clicked.connect(partial(self.install_clicked, pack))
            el.pack_more.clicked.connect(partial(webopen, pack.page))
            self.ui.pack_box.addWidget(widget)

        self.ui.pack_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    def reset_packs(self):
        clear_layout(self.ui.pack_box)

        self.ui.pack_box.addWidget(self.ui.loading_label)
        self.ui.pack_box.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))

    """Event Listeners"""

    def q_typed(self):
        if not self.conf.read(Setting.live_search):
            return
        if self.ui.pack_search.text() == "":
            self.init_packs(self.curse.get_modpacks())
            return
        self.init_packs(self.curse.search(self.ui.pack_search.text(), "modpack"))

    def search_packs(self):
        if self.ui.pack_search.text() == "":
            self.init_packs(self.curse.get_modpacks())
            return
        self.init_packs(self.curse.search(self.ui.pack_search.text(), "modpack"))

    def delete_clicked(self, instance: MultiMCInstance):
        print(confirm_box(self.win, QMessageBox.Question,
                          "Are you sure you want to delete {}".format(instance.name)))
        self.mmc.delete_instance(instance)
        self.init_instances()

    def edit_clicked(self, instance: MultiMCInstance):
        self.children.append(InstanceWindow(instance, self.curse, self.conf))

    def install_clicked(self, project: CurseProject):
        fs = project.files
        if self.conf.read(Setting.ask_file):
            dia = FileDialog(fs)
            f = dia.dia.exec_()
            if not f:
                return

            f = fs[f - 1]

        else:
            f = fs[0]

        dia = DownloadDialog()
        dia.download_pack(project, self.curse.get_file(f), self.curse, self.mmc)
        self.mmc = MultiMC(self.conf.read(Setting.location))
        self.init_instances()

    # Settings Checkboxes

    def analytics_checked(self):
        self.conf.write(Setting.analytics, self.ui.analytics_check.isChecked())

    def update_checked(self):
        self.conf.write(Setting.update, self.ui.update_check.isChecked())

    def file_checked(self):
        self.conf.write(Setting.ask_file, self.ui.file_check.isChecked())

    # Update Checker

    def update_check_done(self, res: dict):
        if not res["res"] or not res["update"]["downloads"][platform]:
            return

        up_win = QDialog()
        upd = Ui_UpdateDialog()
        upd.setupUi(up_win)

        upd.textBrowser.setText(res["update"]["changelog"])

        if not up_win.exec_():
            return

    def data_found(self, dat: dict):
        if len(dat["res"]) < 1 and dat["succ"]:
            return

        if dat["type"] == "packs":
            if not dat["succ"]:
                err("Pack loading failed!")
                self.ui.loading_label.setText("Network Error!")
                return
            self.init_packs(dat["res"])
<|endoftext|>


<commit_before>from numba.control_flow.control_flow import (ControlBlock, ControlFlowAnalysis,
                                             FuncDefExprNode)
from numba.control_flow.cfstats import *
<commit_msg>Add DeleteStatement to control flow package<commit_after>from numba.control_flow.control_flow import (ControlBlock, ControlFlowAnalysis,
                                             FuncDefExprNode)
from numba.control_flow.cfstats import *
from numba.control_flow.delete_cfnode import DeleteStatement<|endoftext|>


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

def parse_optimization_expression(obj, linear=True, quadratic=False, expression=None, **kwargs):
    """
    Function for parsing the expression of a Constraint or Objective object.

    Parameters
    ----------
    object: Constraint or Objective
        The optimization expression to be parsed
    linear: Boolean
        If True the expression will be assumed to be linear
    quadratic: Boolean
        If True the expression will be assumed to be quadratic
    expression: Sympy expression or None (optional)
        An expression can be passed explicitly to avoid getting the expression from the solver.
        If this is used then 'linear' or 'quadratic' should be True.

    If both linear and quadratic are False, the is_Linear and is_Quadratic methods will be used to determine how it should be parsed

    Returns
    ----------
    A tuple of (linear_coefficients, quadratic_coefficients)
        linear_coefficients is a dictionary of {variable: coefficient} pairs
        quadratic_coefficients is a dictionary of {frozenset(variables): coefficient} pairs
    """
    if expression is None:
        expression = obj.expression

    if not (linear or quadratic):
        if obj.is_Linear:
            linear = True
        elif obj.is_Quadratic:
            quadratic = True
        else:
            raise ValueError("Expression is not linear or quadratic. Other expressions are not currently supported.")

    assert linear or quadratic

    if quadratic:
        offset, linear_coefficients, quadratic_coefficients = _parse_quadratic_expression(expression, **kwargs)
    else:
        offset, linear_coefficients = _parse_linear_expression(expression, **kwargs)
        quadratic_coefficients = {}

    return offset, linear_coefficients, quadratic_coefficients

def _parse_linear_expression(expression, expanded=False, **kwargs):
    """
    Parse the coefficients of a linear expression (linearity is assumed).

    Returns a dictionary of variable: coefficient pairs.
    """
    offset = 0
    constant = None

    if expression.is_Add:
        coefficients = expression.as_coefficients_dict()
    elif expression.is_Mul:
        coefficients = {expression.args[1]: expression.args[0]}
    elif expression.is_Symbol:
        coefficients = {expression: 1}
    elif expression.is_Number:
        coefficients = {}
    else:
        raise ValueError("Expression {} seems to be invalid".format(expression))
    for var in coefficients:
        if not (var.is_Symbol):
            if var == 1:
                constant = var
                offset = coefficients[var]
            elif expanded:
                raise ValueError("Expression {} seems to be invalid".format(expression))
            else:
                coefficients = _parse_linear_expression(expression, expanded=True, **kwargs)
    if constant is not None:
        del coefficients[constant]
    return offset, coefficients

def _parse_quadratic_expression(expression, expanded=False):
    """
    Parse a quadratic expression. It is assumed that the expression is known to be quadratic or linear.

    The 'expanded' parameter tells whether the expression has already been expanded. If it hasn't the parsing
    might fail and will expand the expression and try again.
    """
    linear_coefficients = {}
    quadratic_coefficients = {}
    offset = 0

    if expression.is_Number:  # Constant expression, no coefficients
        return expression, linear_coefficients, quadratic_coefficients

    if expression.is_Mul:
        terms = (expression,)
    elif expression.is_Add:
        terms = expression.args
    else:
        raise ValueError("Expression of type {} could not be parsed.".format(type(expression)))

    try:
        for term in terms:
            if term.is_Number:
                offset += term
                continue
            if term.is_Pow:
                term = 1.0 * term
            assert term.is_Mul, "What is this? {}".format(type(term))
            factors = term.args
            coef = factors[0]
            vars = factors[1:]
            assert len(vars) <= 2, "This should not happen. Is this expression quadratic?"
            if len(vars) == 2:
                key = frozenset(vars)
                quadratic_coefficients[key] = quadratic_coefficients.get(key, 0) + coef
            else:
                var = vars[0]
                if var.is_Symbol:
                    linear_coefficients[var] = linear_coefficients.get(var, 0) + coef
                elif var.is_Pow:
                    var, exponent = var.args
                    if exponent != 2:
                        raise ValueError("The expression is not quadratic")
                    key = frozenset((var,))
                    quadratic_coefficients[key] = quadratic_coefficients.get(key, 0) + coef
        if quadratic_coefficients:
            assert all(var.is_Symbol for var in frozenset.union(*quadratic_coefficients))  # Raise an exception to trigger expand
        if linear_coefficients:
            assert all(var.is_Symbol for var in linear_coefficients)
    except Exception as e:
        if expanded:
            raise e
        else:
            # Try to expand the expression and parse it again
            return _parse_quadratic_expression(expression.expand(), expanded=True)

    return offset, linear_coefficients, quadratic_coefficients
<commit_msg>fix: convert obj offsets to floats<commit_after># Copyright 2016 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def parse_optimization_expression(obj, linear=True, quadratic=False, expression=None, **kwargs):
    """
    Function for parsing the expression of a Constraint or Objective object.

    Parameters
    ----------
    object: Constraint or Objective
        The optimization expression to be parsed
    linear: Boolean
        If True the expression will be assumed to be linear
    quadratic: Boolean
        If True the expression will be assumed to be quadratic
    expression: Sympy expression or None (optional)
        An expression can be passed explicitly to avoid getting the expression from the solver.
        If this is used then 'linear' or 'quadratic' should be True.

    If both linear and quadratic are False, the is_Linear and is_Quadratic methods will be used to determine how it should be parsed

    Returns
    ----------
    A tuple of (linear_coefficients, quadratic_coefficients)
        linear_coefficients is a dictionary of {variable: coefficient} pairs
        quadratic_coefficients is a dictionary of {frozenset(variables): coefficient} pairs
    """
    if expression is None:
        expression = obj.expression

    if not (linear or quadratic):
        if obj.is_Linear:
            linear = True
        elif obj.is_Quadratic:
            quadratic = True
        else:
            raise ValueError("Expression is not linear or quadratic. Other expressions are not currently supported.")

    assert linear or quadratic

    if quadratic:
        offset, linear_coefficients, quadratic_coefficients = _parse_quadratic_expression(expression, **kwargs)
    else:
        offset, linear_coefficients = _parse_linear_expression(expression, **kwargs)
        quadratic_coefficients = {}

    return offset, linear_coefficients, quadratic_coefficients

def _parse_linear_expression(expression, expanded=False, **kwargs):
    """
    Parse the coefficients of a linear expression (linearity is assumed).

    Returns a dictionary of variable: coefficient pairs.
    """
    offset = 0
    constant = None

    if expression.is_Add:
        coefficients = expression.as_coefficients_dict()
    elif expression.is_Mul:
        coefficients = {expression.args[1]: expression.args[0]}
    elif expression.is_Symbol:
        coefficients = {expression: 1}
    elif expression.is_Number:
        coefficients = {}
    else:
        raise ValueError("Expression {} seems to be invalid".format(expression))
    for var in coefficients:
        if not (var.is_Symbol):
            if var == 1:
                constant = var
                offset = float(coefficients[var])
            elif expanded:
                raise ValueError("Expression {} seems to be invalid".format(expression))
            else:
                coefficients = _parse_linear_expression(expression, expanded=True, **kwargs)
    if constant is not None:
        del coefficients[constant]
    return offset, coefficients

def _parse_quadratic_expression(expression, expanded=False):
    """
    Parse a quadratic expression. It is assumed that the expression is known to be quadratic or linear.

    The 'expanded' parameter tells whether the expression has already been expanded. If it hasn't the parsing
    might fail and will expand the expression and try again.
    """
    linear_coefficients = {}
    quadratic_coefficients = {}
    offset = 0

    if expression.is_Number:  # Constant expression, no coefficients
        return expression, linear_coefficients, quadratic_coefficients

    if expression.is_Mul:
        terms = (expression,)
    elif expression.is_Add:
        terms = expression.args
    else:
        raise ValueError("Expression of type {} could not be parsed.".format(type(expression)))

    try:
        for term in terms:
            if term.is_Number:
                offset += float(term)
                continue
            if term.is_Pow:
                term = 1.0 * term
            assert term.is_Mul, "What is this? {}".format(type(term))
            factors = term.args
            coef = factors[0]
            vars = factors[1:]
            assert len(vars) <= 2, "This should not happen. Is this expression quadratic?"
            if len(vars) == 2:
                key = frozenset(vars)
                quadratic_coefficients[key] = quadratic_coefficients.get(key, 0) + coef
            else:
                var = vars[0]
                if var.is_Symbol:
                    linear_coefficients[var] = linear_coefficients.get(var, 0) + coef
                elif var.is_Pow:
                    var, exponent = var.args
                    if exponent != 2:
                        raise ValueError("The expression is not quadratic")
                    key = frozenset((var,))
                    quadratic_coefficients[key] = quadratic_coefficients.get(key, 0) + coef
        if quadratic_coefficients:
            assert all(var.is_Symbol for var in frozenset.union(*quadratic_coefficients))  # Raise an exception to trigger expand
        if linear_coefficients:
            assert all(var.is_Symbol for var in linear_coefficients)
    except Exception as e:
        if expanded:
            raise e
        else:
            # Try to expand the expression and parse it again
            return _parse_quadratic_expression(expression.expand(), expanded=True)

    return offset, linear_coefficients, quadratic_coefficients
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Trying to implement the following example
http://mdtraj.org/latest/examples/pca.html

Gives memory error for the moment
"""

import mdtraj as md
import sys
import glob
import numpy as np
import os
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from itertools import combinations
import argparse

parser = argparse.ArgumentParser(usage="{} Trajectories*.nc Topology.prmtop".
                                 format(sys.argv[0]),
                                 epilog="Load up an AMBER trajectories and\
                                 their corresponding topology with MDtraj.\
                                 Plot the PCA done with sklearn's PCA class")

parser.add_argument("Trajectories", help="An indefinite amount of AMBER\
                    trajectories", nargs="+")
parser.add_argument("Topology", help="The topology .prmtop file that matches\
                    the trajectories")
args = parser.parse_args()

def load_Trajs(names, topology):
    filenames = sorted(glob.glob(names))
    trajectories = md.load(filenames, top=topology)
    return trajectories

def plot_PCA1(trajectory):
    pca1 = PCA(n_components=2)
    trajectory.superpose(trajectory, 0)
    reduced_cartesian = pca1.fit_transform(trajectory.xyz.reshape(
        trajectory.n_frames, trajectory.n_atoms * 3))
    plt.figure()
    plt.scatter(reduced_cartesian[:, 0], reduced_cartesian[:, 1], marker='x',
                c=trajectory.time)
    plt.xlabel('PC1')
    plt.ylabel('PC2')
    plt.title('Cartesian coordinate PCA')
    cbar = plt.colorbar()
    cbar.set_label('Time [ps]')

def plot_PCA2(trajectory):
    pca2 = PCA(n_components=2)
    atom_pairs = list(combinations(range(trajectory.n_atoms), 2))
    pairwise_distances = md.geometry.compute_distances(trajectory, atom_pairs)
    reduced_distances = pca2.fit_transform(pairwise_distances)
    plt.figure()
    plt.scatter(reduced_distances[:, 0], reduced_distances[:, 1],
                marker='x', c=trajectory.time)
    plt.xlabel('PC1')
    plt.ylabel('PC2')
    plt.title('Pairwise distance PCA: alanine dipeptide')
    cbar = plt.colorbar()
    cbar.set_label('Time [ps]')

def main():
    print('\n', args, '\n')
    if args:
        trajectory = load_Trajs(args.Trajectories, args.Topology)
        plot1 = plot_PCA1(trajectory)
        # plot2 = plot_PCA2(trajectory)

if __name__ == "__main__":
    main()
<commit_msg>Added md.iterload() to loop trough multiple .nc files<commit_after>#!/usr/bin/env python
"""
Trying to implement the following example
http://mdtraj.org/latest/examples/pca.html

Gives memory error for the moment
"""

import mdtraj as md
import sys
from glob import glob
import numpy as np
import os
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from itertools import combinations
import argparse

parser = argparse.ArgumentParser(usage="{} Trajectories*.nc Topology.prmtop".
                                 format(sys.argv[0]),
                                 epilog="Load up an AMBER trajectories and\
                                 their corresponding topology with MDtraj.\
                                 Plot the PCA done with sklearn's PCA class")

parser.add_argument("Trajectories", help="An indefinite amount of AMBER\
                    trajectories", nargs="+")
parser.add_argument("Topology", help="The topology .prmtop file that matches\
                    the trajectories")
args = parser.parse_args()

def load_Trajs(names, topology):
    filenames = sorted(glob.glob(names))
    trajectories = md.load(filenames, top=topology)
    return trajectories

def plot_PCA1(trajectory):
    pca1 = PCA(n_components=2)
    trajectory.superpose(trajectory, 0)
    reduced_cartesian = pca1.fit_transform(trajectory.xyz.reshape(
        trajectory.n_frames, trajectory.n_atoms * 3))
    plt.figure()
    plt.scatter(reduced_cartesian[:, 0], reduced_cartesian[:, 1], marker='x',
                c=trajectory.time)
    plt.xlabel('PC1')
    plt.ylabel('PC2')
    plt.title('Cartesian coordinate PCA')
    cbar = plt.colorbar()
    cbar.set_label('Time [ps]')

def plot_PCA2(trajectory):
    pca2 = PCA(n_components=2)
    atom_pairs = list(combinations(range(trajectory.n_atoms), 2))
    pairwise_distances = md.geometry.compute_distances(trajectory, atom_pairs)
    reduced_distances = pca2.fit_transform(pairwise_distances)
    plt.figure()
    plt.scatter(reduced_distances[:, 0], reduced_distances[:, 1],
                marker='x', c=trajectory.time)
    plt.xlabel('PC1')
    plt.ylabel('PC2')
    plt.title('Pairwise distance PCA: alanine dipeptide')
    cbar = plt.colorbar()
    cbar.set_label('Time [ps]')

def plot_PCA3(filenames, topology):
    pca = PCA(n_components=2)
    first_frame = md.load_frame(filenames[0], 0, top=topology)
    pca_fits = []
    for fragment in filenames:
        for chunk in md.iterload(fragment, chunk=100, top=topology):
            chunk.superpose(chunk, first_frame)
            pca_fits.append(
                            pca.fit_transform(chunk.xyz.reshape(
                            chunk.n_frames, chunk.n_atoms * 3)))
    data_plot = np.concatenate(pca_fits)

def rmsd(filenames, topology):
    rmsds = []
    first_frame = md.load_frame(filenames[0], 0, top=topology)
    for fragment in filenames:
        for chunk in md.iterload(fragment, chunk=100, top=topology):
            rmsds.append(md.rmsd(chunk, first_frame))
    return np.concatenate(rmsds)

def main():
    print('\n', args, '\n')
    if args:
        rmsds = rmsd(args.Trajectories, args.Topology)
        print(rmsds)

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


<commit_before># -*- coding: utf-8 -*-
"""
Nosetest modules
"""
from nose.tools import *
import os
"""
Model tests for artifact
"""
from datetime import datetime
from time import sleep

from pylons import c
import re
import markdown

import pymongo
from pymongo.errors import OperationFailure

from ming import schema as S
from ming import Field

from pyforge.model import Artifact, Message

def test_artifact():
    class Checkartifact(Artifact):
        class __mongometa__:
            name='checkartifact'
        title=Field(str)
        version=Field(int, if_missing=0)
        author_id=Field(S.ObjectId, if_missing=lambda:c.user._id)
        timestamp=Field(S.DateTime, if_missing=datetime.utcnow)
        text=Field(S.String, if_missing='')

    assert_true('PyForge has available model::Artifact class')

def test_message():
    class Checkmessage(Message):
        class __mongometa__:
            name='checkmessage'
        page_title=Field(str)

    assert_true('PyForge has available model::Message class')

    m = Checkmessage.make(dict(page_title='test_title'))

    m.author()
    assert_true('PyForge message has author')

    m.reply()
    assert_true('PyForge message has reply')

    m.descendents()
    assert_true('PyForge message has descendents')

    m.replies()
    assert_true('PyForge message has replies')

<commit_msg>FORGE-60: corrected typo in descendants call<commit_after># -*- coding: utf-8 -*-
"""
Nosetest modules
"""
from nose.tools import *
import os
"""
Model tests for artifact
"""
from datetime import datetime
from time import sleep

from pylons import c
import re
import markdown

import pymongo
from pymongo.errors import OperationFailure

from ming import schema as S
from ming import Field

from pyforge.model import Artifact, Message

def test_artifact():
    class Checkartifact(Artifact):
        class __mongometa__:
            name='checkartifact'
        title=Field(str)
        version=Field(int, if_missing=0)
        author_id=Field(S.ObjectId, if_missing=lambda:c.user._id)
        timestamp=Field(S.DateTime, if_missing=datetime.utcnow)
        text=Field(S.String, if_missing='')

    assert_true('PyForge has available model::Artifact class')

def test_message():
    class Checkmessage(Message):
        class __mongometa__:
            name='checkmessage'
        page_title=Field(str)

    assert_true('PyForge has available model::Message class')

    m = Checkmessage.make(dict(page_title='test_title'))

    m.author()
    assert_true('PyForge message has author')

    m.reply()
    assert_true('PyForge message has reply')

    m.descendants()
    assert_true('PyForge message has descendants')

    m.replies()
    assert_true('PyForge message has replies')

<|endoftext|>


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

import sys

PY_VERSION = sys.version_info[:2]

PY26 = PY_VERSION == (2, 6)
PY3K = PY_VERSION[0] == 3

if PY26:  # pragma: no cover
    import zipfile
    class ZipFile(zipfile.ZipFile):

        def __enter__(self):
            return self

        def __exit__(self, *exc_inf):
            self.close()
            return True
else:
    from zipfile import ZipFile

if PY26:  # pragma: no cover
    import gzip
    class GzipFile(gzip.GzipFile):

        def __enter__(self):
            return self

        def __exit__(self, *exc_inf):
            self.close()
            return True
else:
    from gzip import GzipFile

if PY3K:
    imap = map
else:
    from itertools import imap

try:
    from StringIO import StringIO
except ImportError:  # pragma: no cover
    from io import StringIO
<commit_msg>code never touched in coverage<commit_after># -*- coding: utf-8 -*-

import sys

PY_VERSION = sys.version_info[:2]

PY26 = PY_VERSION == (2, 6)
PY3K = PY_VERSION[0] == 3

if PY26:  # pragma: no cover
    import zipfile
    class ZipFile(zipfile.ZipFile):

        def __enter__(self):
            return self

        def __exit__(self, *exc_inf):
            self.close()
            return True
else:
    from zipfile import ZipFile

if PY26:  # pragma: no cover
    import gzip
    class GzipFile(gzip.GzipFile):

        def __enter__(self):
            return self

        def __exit__(self, *exc_inf):
            self.close()
            return True
else:
    from gzip import GzipFile

if PY3K:  # pragma: no cover
    imap = map
else:
    from itertools import imap

try:
    from StringIO import StringIO
except ImportError:  # pragma: no cover
    from io import StringIO
<|endoftext|>


<commit_before>"""
    Conan tools: classes and function in this module are intended to be used out of the box
    with the Conan configuration already currified into them. This configuration refers
    mainly to two items:
     - requester: on network calls, this will include proxy definition.
     - output: the output configuration

    Here in this module there should be no logic, all functions and classes must be implemented
    elsewhere (mainly in conans.util or conans.client.tools) and ready to be used without
    the currification.
"""

from __future__ import print_function

import requests

from conans.client.output import ConanOutput
# Tools from conans.client.tools
from conans.client.tools import files as tools_files, net as tools_net, oss as tools_oss, \
    system_pm as tools_system_pm, win as tools_win
from conans.client.tools.env import *  # pylint: disable=unused-import
from conans.client.tools.pkg_config import *  # pylint: disable=unused-import
from conans.client.tools.scm import *  # pylint: disable=unused-import
# Tools form conans.util
from conans.util.env_reader import get_env
from conans.util.files import _generic_algorithm_sum, load, md5, md5sum, mkdir, relative_dirs, \
    rmdir, save as files_save, save_append, sha1sum, sha256sum, touch, sha1sum, sha256sum, \
    to_file_bytes, touch
from conans.util.log import logger

# This global variables are intended to store the configuration of the running Conan application
_global_output = None
_global_requester = None

def set_global_instances(the_output, the_requester):
    global _global_output
    global _global_requester

    old_output, old_requester = _global_output, _global_requester

    # TODO: pass here the configuration file, and make the work here (explicit!)
    _global_output = the_output
    _global_requester = the_requester

    return old_output, old_requester

def get_global_instances():
    return _global_output, _global_requester

# Assign a default, will be overwritten in the factory of the ConanAPI
set_global_instances(the_output=ConanOutput(sys.stdout, True), the_requester=requests)

"""
From here onwards only currification is expected, no logic
"""

def save(path, content, append=False):
    # TODO: All this three functions: save, save_append and this one should be merged into one.
    if append:
        save_append(path=path, content=content)
    else:
        files_save(path=path, content=content, only_if_modified=False)

# From conans.client.tools.net
ftp_download = tools_net.ftp_download

def download(*args, **kwargs):
    return tools_net.download(out=_global_output, requester=_global_requester, *args, **kwargs)

def get(*args, **kwargs):
    return tools_net.get(output=_global_output, requester=_global_requester, *args, **kwargs)

# from conans.client.tools.files
chdir = tools_files.chdir
human_size = tools_files.human_size
untargz = tools_files.untargz
check_with_algorithm_sum = tools_files.check_with_algorithm_sum
check_sha1 = tools_files.check_sha1
check_md5 = tools_files.check_md5
check_sha256 = tools_files.check_sha256
patch = tools_files.patch
replace_prefix_in_pc_file = tools_files.replace_prefix_in_pc_file
collect_libs = tools_files.collect_libs
which = tools_files.which
unix2dos = tools_files.unix2dos
dos2unix = tools_files.dos2unix

def unzip(*args, **kwargs):
    return tools_files.unzip(output=_global_output, *args, **kwargs)

def replace_in_file(*args, **kwargs):
    return tools_files.replace_in_file(output=_global_output, *args, **kwargs)

# from conans.client.tools.oss
args_to_string = tools_oss.args_to_string
detected_architecture = tools_oss.detected_architecture
OSInfo = tools_oss.OSInfo
cross_building = tools_oss.cross_building
get_cross_building_settings = tools_oss.get_cross_building_settings
get_gnu_triplet = tools_oss.get_gnu_triplet

def cpu_count(*args, **kwargs):
    return tools_oss.cpu_count(output=_global_output, *args, **kwargs)

# from conans.client.tools.system_pm
class SystemPackageTool(tools_system_pm.SystemPackageTool):
    def __init__(self, *args, **kwargs):
        super(SystemPackageTool, self).__init__(output=_global_output, *args, **kwargs)

class NullTool(tools_system_pm.NullTool):
    def __init__(self, *args, **kwargs):
        super(NullTool, self).__init__(output=_global_output, *args, **kwargs)

class AptTool(tools_system_pm.AptTool):
    def __init__(self, *args, **kwargs):
        super(AptTool, self).__init__(output=_global_output, *args, **kwargs)

class YumTool(tools_system_pm.YumTool):
    def __init__(self, *args, **kwargs):
        super(YumTool, self).__init__(output=_global_output, *args, **kwargs)

class BrewTool(tools_system_pm.BrewTool):
    def __init__(self, *args, **kwargs):
        super(BrewTool, self).__init__(output=_global_output, *args, **kwargs)

class PkgTool(tools_system_pm.PkgTool):
    def __init__(self, *args, **kwargs):
        super(PkgTool, self).__init__(output=_global_output, *args, **kwargs)

class ChocolateyTool(tools_system_pm.ChocolateyTool):
    def __init__(self, *args, **kwargs):
        super(ChocolateyTool, self).__init__(output=_global_output, *args, **kwargs)

class PkgUtilTool(tools_system_pm.PkgUtilTool):
    def __init__(self, *args, **kwargs):
        super(PkgUtilTool, self).__init__(output=_global_output, *args, **kwargs)

class PacManTool(tools_system_pm.PacManTool):
    def __init__(self, *args, **kwargs):
        super(PacManTool, self).__init__(output=_global_output, *args, **kwargs)

class ZypperTool(tools_system_pm.ZypperTool):
    def __init__(self, *args, **kwargs):
        super(ZypperTool, self).__init__(output=_global_output, *args, **kwargs)

# from conans.client.tools.win
msvc_build_command = tools_win.msvc_build_command
vs_installation_path = tools_win.vs_installation_path
vswhere = tools_win.vswhere
vs_comntools = tools_win.vs_comntools
find_windows_10_sdk = tools_win.find_windows_10_sdk
vcvars_dict = tools_win.vcvars_dict
vcvars = tools_win.vcvars
escape_windows_cmd = tools_win.escape_windows_cmd
get_cased_path = tools_win.get_cased_path
MSYS2 = tools_win.MSYS2
MSYS = tools_win.MSYS
CYGWIN = tools_win.CYGWIN
WSL = tools_win.WSL
SFU = tools_win.SFU
unix_path = tools_win.unix_path
run_in_windows_bash = tools_win.run_in_windows_bash

def build_sln_command(*args, **kwargs):
    return tools_win.build_sln_command(output=_global_output, *args, **kwargs)

def vcvars_command(*args, **kwargs):
    return tools_win.vcvars_command(output=_global_output, *args, **kwargs)

# Ready to use objects.
try:
    os_info = OSInfo()
except Exception as exc:
    logger.error(exc)
    _global_output.error("Error detecting os_info")<commit_msg>Add missing tools to `conans.tools` (#4108)<commit_after>"""
    Conan tools: classes and function in this module are intended to be used out of the box
    with the Conan configuration already currified into them. This configuration refers
    mainly to two items:
     - requester: on network calls, this will include proxy definition.
     - output: the output configuration

    Here in this module there should be no logic, all functions and classes must be implemented
    elsewhere (mainly in conans.util or conans.client.tools) and ready to be used without
    the currification.
"""

import requests

from conans.client.output import ConanOutput
# Tools from conans.client.tools
from conans.client.tools import files as tools_files, net as tools_net, oss as tools_oss, \
    system_pm as tools_system_pm, win as tools_win
from conans.client.tools.env import *  # pylint: disable=unused-import
from conans.client.tools.pkg_config import *  # pylint: disable=unused-import
from conans.client.tools.scm import *  # pylint: disable=unused-import
from conans.client.tools.apple import *
# Tools form conans.util
from conans.util.env_reader import get_env
from conans.util.files import _generic_algorithm_sum, load, md5, md5sum, mkdir, relative_dirs, \
    rmdir, save as files_save, save_append, sha1sum, sha256sum, touch, sha1sum, sha256sum, \
    to_file_bytes, touch
from conans.util.log import logger

# This global variables are intended to store the configuration of the running Conan application
_global_output = None
_global_requester = None

def set_global_instances(the_output, the_requester):
    global _global_output
    global _global_requester

    old_output, old_requester = _global_output, _global_requester

    # TODO: pass here the configuration file, and make the work here (explicit!)
    _global_output = the_output
    _global_requester = the_requester

    return old_output, old_requester

def get_global_instances():
    return _global_output, _global_requester

# Assign a default, will be overwritten in the factory of the ConanAPI
set_global_instances(the_output=ConanOutput(sys.stdout, True), the_requester=requests)

"""
From here onwards only currification is expected, no logic
"""

def save(path, content, append=False):
    # TODO: All this three functions: save, save_append and this one should be merged into one.
    if append:
        save_append(path=path, content=content)
    else:
        files_save(path=path, content=content, only_if_modified=False)

# From conans.client.tools.net
ftp_download = tools_net.ftp_download

def download(*args, **kwargs):
    return tools_net.download(out=_global_output, requester=_global_requester, *args, **kwargs)

def get(*args, **kwargs):
    return tools_net.get(output=_global_output, requester=_global_requester, *args, **kwargs)

# from conans.client.tools.files
chdir = tools_files.chdir
human_size = tools_files.human_size
untargz = tools_files.untargz
check_with_algorithm_sum = tools_files.check_with_algorithm_sum
check_sha1 = tools_files.check_sha1
check_md5 = tools_files.check_md5
check_sha256 = tools_files.check_sha256
patch = tools_files.patch
replace_prefix_in_pc_file = tools_files.replace_prefix_in_pc_file
collect_libs = tools_files.collect_libs
which = tools_files.which
unix2dos = tools_files.unix2dos
dos2unix = tools_files.dos2unix

def unzip(*args, **kwargs):
    return tools_files.unzip(output=_global_output, *args, **kwargs)

def replace_in_file(*args, **kwargs):
    return tools_files.replace_in_file(output=_global_output, *args, **kwargs)

def replace_path_in_file(*args, **kwargs):
    return tools_files.replace_path_in_file(output=_global_output, *args, **kwargs)

# from conans.client.tools.oss
args_to_string = tools_oss.args_to_string
detected_architecture = tools_oss.detected_architecture
OSInfo = tools_oss.OSInfo
cross_building = tools_oss.cross_building
get_cross_building_settings = tools_oss.get_cross_building_settings
get_gnu_triplet = tools_oss.get_gnu_triplet

def cpu_count(*args, **kwargs):
    return tools_oss.cpu_count(output=_global_output, *args, **kwargs)

# from conans.client.tools.system_pm
class SystemPackageTool(tools_system_pm.SystemPackageTool):
    def __init__(self, *args, **kwargs):
        super(SystemPackageTool, self).__init__(output=_global_output, *args, **kwargs)

class NullTool(tools_system_pm.NullTool):
    def __init__(self, *args, **kwargs):
        super(NullTool, self).__init__(output=_global_output, *args, **kwargs)

class AptTool(tools_system_pm.AptTool):
    def __init__(self, *args, **kwargs):
        super(AptTool, self).__init__(output=_global_output, *args, **kwargs)

class YumTool(tools_system_pm.YumTool):
    def __init__(self, *args, **kwargs):
        super(YumTool, self).__init__(output=_global_output, *args, **kwargs)

class BrewTool(tools_system_pm.BrewTool):
    def __init__(self, *args, **kwargs):
        super(BrewTool, self).__init__(output=_global_output, *args, **kwargs)

class PkgTool(tools_system_pm.PkgTool):
    def __init__(self, *args, **kwargs):
        super(PkgTool, self).__init__(output=_global_output, *args, **kwargs)

class ChocolateyTool(tools_system_pm.ChocolateyTool):
    def __init__(self, *args, **kwargs):
        super(ChocolateyTool, self).__init__(output=_global_output, *args, **kwargs)

class PkgUtilTool(tools_system_pm.PkgUtilTool):
    def __init__(self, *args, **kwargs):
        super(PkgUtilTool, self).__init__(output=_global_output, *args, **kwargs)

class PacManTool(tools_system_pm.PacManTool):
    def __init__(self, *args, **kwargs):
        super(PacManTool, self).__init__(output=_global_output, *args, **kwargs)

class ZypperTool(tools_system_pm.ZypperTool):
    def __init__(self, *args, **kwargs):
        super(ZypperTool, self).__init__(output=_global_output, *args, **kwargs)

# from conans.client.tools.win
msvc_build_command = tools_win.msvc_build_command
vs_installation_path = tools_win.vs_installation_path
vswhere = tools_win.vswhere
vs_comntools = tools_win.vs_comntools
find_windows_10_sdk = tools_win.find_windows_10_sdk
vcvars_dict = tools_win.vcvars_dict
vcvars = tools_win.vcvars
escape_windows_cmd = tools_win.escape_windows_cmd
get_cased_path = tools_win.get_cased_path
MSYS2 = tools_win.MSYS2
MSYS = tools_win.MSYS
CYGWIN = tools_win.CYGWIN
WSL = tools_win.WSL
SFU = tools_win.SFU
unix_path = tools_win.unix_path
run_in_windows_bash = tools_win.run_in_windows_bash

def build_sln_command(*args, **kwargs):
    return tools_win.build_sln_command(output=_global_output, *args, **kwargs)

def vcvars_command(*args, **kwargs):
    return tools_win.vcvars_command(output=_global_output, *args, **kwargs)

def latest_vs_version_installed(*args, **kwargs):
    return tools_win.latest_vs_version_installed(output=_global_output, *args, **kwargs)

# Ready to use objects.
try:
    os_info = OSInfo()
except Exception as exc:
    logger.error(exc)
    _global_output.error("Error detecting os_info")<|endoftext|>


<commit_before>"""
Event handling system.

Author: Henrik Thostrup Jensen <htj@nordu.net>
Copyright: NORDUnet (2012)
"""

DISCOVER                = 'discover'

RESERVE                 = 'reserve'
RESERVE_RESPONSE        = 'reserve_response'
RESERVE_COMMIT          = 'reserve_commit'
RESERVE_COMMIT_RESPONSE = 'reserve_commit_response'
RESERVE_ABORT           = 'reserve_abort'
RESERVE_ABORT_RESPONSE  = 'reserve_abort_response'
RESERVE_TIMEOUT         = 'reserve_timeout'

PROVISION               = 'provision'
PROVISION_RESPONSE      = 'provision_response'
RELEASE                 = 'release'
RELEASE_RESPONSE        = 'release_response'

TERMINATE               = 'terminate'
TERMINATE_RESPONSE      = 'terminate_response'

QUERY                   = 'query'
QUERY_RESPONSE          = 'query_response'
DATA_PLANE_CHANGE       = 'data_plane_change'

SYSTEM_SERVICE          = 'service'
NSI2_REMOTE             = 'nsi2-remote'
NSI2_LOCAL              = 'nsi2-local'

class ServiceRegistry:

    def __init__(self):
        self.handlers = {}

    def getHandler(self, event, system):

        es = (event, system)
        return self.handlers[es]

    def registerEventHandler(self, event, handler, system):

        if event in self.handlers:
            raise ValueError('Handler for event %s already registered' % event)

        es = (event, system)
        self.handlers[es] = handler

    def deregisterEventHandler(self, event, system):

        es = (event, system)
        self.handlers.pop(es)

<commit_msg>add error event to registry<commit_after>"""
Event handling system.

Author: Henrik Thostrup Jensen <htj@nordu.net>
Copyright: NORDUnet (2012)
"""

DISCOVER                = 'discover'

RESERVE                 = 'reserve'
RESERVE_RESPONSE        = 'reserve_response'
RESERVE_COMMIT          = 'reserve_commit'
RESERVE_COMMIT_RESPONSE = 'reserve_commit_response'
RESERVE_ABORT           = 'reserve_abort'
RESERVE_ABORT_RESPONSE  = 'reserve_abort_response'
RESERVE_TIMEOUT         = 'reserve_timeout'

PROVISION               = 'provision'
PROVISION_RESPONSE      = 'provision_response'
RELEASE                 = 'release'
RELEASE_RESPONSE        = 'release_response'

TERMINATE               = 'terminate'
TERMINATE_RESPONSE      = 'terminate_response'

QUERY                   = 'query'
QUERY_RESPONSE          = 'query_response'

DATA_PLANE_CHANGE       = 'data_plane_change'
ERROR_EVENT             = 'error_event'

SYSTEM_SERVICE          = 'service'
NSI2_REMOTE             = 'nsi2-remote'
NSI2_LOCAL              = 'nsi2-local'

class ServiceRegistry:

    def __init__(self):
        self.handlers = {}

    def getHandler(self, event, system):

        es = (event, system)
        return self.handlers[es]

    def registerEventHandler(self, event, handler, system):

        if event in self.handlers:
            raise ValueError('Handler for event %s already registered' % event)

        es = (event, system)
        self.handlers[es] = handler

    def deregisterEventHandler(self, event, system):

        es = (event, system)
        self.handlers.pop(es)

<|endoftext|>