<commit_before>import os
import string
import pycrfsuite
import re
from collections import OrderedDict

DIRECTIONS = set(['n', 's', 'e', 'w',
                  'ne', 'nw', 'se', 'sw',
                  'north', 'south', 'east', 'west',
                  'northeast', 'northwest', 'southeast', 'southwest'])

TAGGER = pycrfsuite.Tagger()
TAGGER.open(os.path.split(os.path.abspath(__file__))[0]
            + '/usaddr.crfsuite')

def parse(address_string) :

    tokens = tokenize(address_string)

    if not tokens :
        return []

    features = addr2features(tokens)

    tags = TAGGER.tag(features)
    return zip(tokens, tags)

def tag(address_string) :
    tagged_address = OrderedDict()
    for token, label in parse(address_string) :
        tagged_address.setdefault(label, []).append(token)

    for token in tagged_address :
        component = ' '.join(tagged_address[token])
        component = component.strip(" ,;")
        tagged_address[token] = component

    return tagged_address

def tokenize(address_string) :
    re_tokens = re.compile(r"""
    \b[^\s,;#]+[.,;]*   # ['ab. cd,ef '] -> ['ab.', 'cd,', 'ef']
    |
    [#&]                # [^'#abc'] -> ['#']
    """,
                           re.VERBOSE | re.UNICODE)

    tokens = re_tokens.findall(address_string)

    if not tokens :
        return []

    return tokens

def tokenFeatures(token) :

    token_clean = re.sub(r'(^[\W]*)|([\s,;]$)', u'', token)
    token_abbrev = re.sub(r'[.]', u'', token_clean.lower())
    features = {'lower' : token_clean.lower(),
                'nopunc' : token_abbrev,
                'isupper' : token_clean.isupper(),
                'islower' : token_clean.islower(),
                'istitle' : token_clean.istitle(),
                'isalldigits' : token_clean.isdigit(),
                'split_digit' :  bool(re.match(r'\d+[\W\S]+\d+',
                                               token_clean)),
                'digit.length' : unicode(len(token_clean)
                                         if token_clean.isdigit()
                                         else False),
                'endsinpunc' : token[-1] in string.punctuation,
                'end.delim' : token[-1] in (u',', u';'),
                'word.length' : unicode(len(token_clean)
                                        if not token_clean.isdigit()
                                        else False),
                'directional' : token_abbrev in DIRECTIONS,
                'has.vowels'  : bool(set(token_clean) & set('aeiou')),
                }

    return features

def addr2features(address):

    feature_sequence = [tokenFeatures(address[0])]
    previous_features = feature_sequence[-1].copy()

    for token in address[1:] :
        token_features = tokenFeatures(token)
        current_features = token_features.copy()

        feature_sequence[-1]['next'] = current_features
        token_features['previous'] = previous_features

        feature_sequence.append(token_features)

        previous_features = current_features

    feature_sequence[0]['address.start'] = True
    feature_sequence[-1]['address.end'] = True

    if len(feature_sequence) > 1 :
        feature_sequence[1]['previous']['address.start'] = True
        feature_sequence[-2]['next']['address.end'] = True

    return feature_sequence
<commit_msg>handle missing usaddr.crfsuite close #45<commit_after>import os
import string
import pycrfsuite
import re
from collections import OrderedDict
import warnings

DIRECTIONS = set(['n', 's', 'e', 'w',
                  'ne', 'nw', 'se', 'sw',
                  'north', 'south', 'east', 'west',
                  'northeast', 'northwest', 'southeast', 'southwest'])

try :
    TAGGER = pycrfsuite.Tagger()
    TAGGER.open(os.path.split(os.path.abspath(__file__))[0]
                + '/usaddr.crfsuite')
except IOError :
    warnings.warn("You must train the model (run training/training.py) and create the usaddr.crfsuite file before you can use the parse and tag methods")

def parse(address_string) :

    tokens = tokenize(address_string)

    if not tokens :
        return []

    features = addr2features(tokens)

    tags = TAGGER.tag(features)
    return zip(tokens, tags)

def tag(address_string) :
    tagged_address = OrderedDict()
    for token, label in parse(address_string) :
        tagged_address.setdefault(label, []).append(token)

    for token in tagged_address :
        component = ' '.join(tagged_address[token])
        component = component.strip(" ,;")
        tagged_address[token] = component

    return tagged_address

def tokenize(address_string) :
    re_tokens = re.compile(r"""
    \b[^\s,;#]+[.,;]*   # ['ab. cd,ef '] -> ['ab.', 'cd,', 'ef']
    |
    [#&]                # [^'#abc'] -> ['#']
    """,
                           re.VERBOSE | re.UNICODE)

    tokens = re_tokens.findall(address_string)

    if not tokens :
        return []

    return tokens

def tokenFeatures(token) :

    token_clean = re.sub(r'(^[\W]*)|([\s,;]$)', u'', token)
    token_abbrev = re.sub(r'[.]', u'', token_clean.lower())
    features = {'lower' : token_clean.lower(),
                'nopunc' : token_abbrev,
                'isupper' : token_clean.isupper(),
                'islower' : token_clean.islower(),
                'istitle' : token_clean.istitle(),
                'isalldigits' : token_clean.isdigit(),
                'split_digit' :  bool(re.match(r'\d+[\W\S]+\d+',
                                               token_clean)),
                'digit.length' : unicode(len(token_clean)
                                         if token_clean.isdigit()
                                         else False),
                'endsinpunc' : token[-1] in string.punctuation,
                'end.delim' : token[-1] in (u',', u';'),
                'word.length' : unicode(len(token_clean)
                                        if not token_clean.isdigit()
                                        else False),
                'directional' : token_abbrev in DIRECTIONS,
                'has.vowels'  : bool(set(token_clean) & set('aeiou')),
                }

    return features

def addr2features(address):

    feature_sequence = [tokenFeatures(address[0])]
    previous_features = feature_sequence[-1].copy()

    for token in address[1:] :
        token_features = tokenFeatures(token)
        current_features = token_features.copy()

        feature_sequence[-1]['next'] = current_features
        token_features['previous'] = previous_features

        feature_sequence.append(token_features)

        previous_features = current_features

    feature_sequence[0]['address.start'] = True
    feature_sequence[-1]['address.end'] = True

    if len(feature_sequence) > 1 :
        feature_sequence[1]['previous']['address.start'] = True
        feature_sequence[-2]['next']['address.end'] = True

    return feature_sequence
<|endoftext|>


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

from __future__ import absolute_import
from behave.formatter.plain import PlainFormatter
from behave.textutil import make_indentation
import textwrap

# -----------------------------------------------------------------------------
# CLASS: PlainFormatter
# -----------------------------------------------------------------------------
class TravisFormatter(PlainFormatter):
    """
    Provides a simple plain formatter without coloring/formatting.
    The formatter displays now also:

       * multi-line text (doc-strings)
       * table
       * tags (maybe)
    """
    name = "travis"
    description = "Very basic formatter with maximum compatibility but shortened line lengths for Travis"

    LINE_WIDTH = 125

    def result(self, step):
        """
        Process the result of a step (after step execution).

        :param step:   Step object with result to process.
        """
        step = self.steps.pop(0)
        indent = make_indentation(2 * self.indent_size)
        if self.show_aligned_keywords:
            # -- RIGHT-ALIGN KEYWORDS (max. keyword width: 6):
            text = u"%s%6s %s" % (indent, step.keyword, step.name)
        else:
            text = u"%s%s %s" % (indent, step.keyword, step.name)
        self.stream.write(textwrap.shorten(text, width=self.LINE_WIDTH - 30) + ' ')

        status_text = f'({step.status.name}'
        if self.show_timings:
            status_text += " in %0.3fs" % step.duration
        status_text += ')'

        unicode_errors = 0
        if step.error_message:
            try:
                self.stream.write(u"%s\n%s\n" % (status_text, step.error_message))
            except UnicodeError as e:
                unicode_errors += 1
                self.stream.write(u"%s\n" % status_text)
                self.stream.write(u"%s while writing error message: %s\n" % \
                                  (e.__class__.__name__, e))
                if self.RAISE_OUTPUT_ERRORS:
                    raise
        else:
            self.stream.write(u"%s\n" % status_text)

        if self.show_multiline:
            if step.text:
                try:
                    self.doc_string(step.text)
                except UnicodeError as e:
                    unicode_errors += 1
                    self.stream.write(u"%s while writing docstring: %s\n" % \
                                      (e.__class__.__name__, e))
                    if self.RAISE_OUTPUT_ERRORS:
                        raise
            if step.table:
                self.table(step.table)

<commit_msg>seems to work<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import
from behave.formatter.plain import PlainFormatter
from behave.textutil import make_indentation
import textwrap

# -----------------------------------------------------------------------------
# CLASS: PlainFormatter
# -----------------------------------------------------------------------------
class TravisFormatter(PlainFormatter):
    """
    Provides a simple plain formatter without coloring/formatting.
    The formatter displays now also:

       * multi-line text (doc-strings)
       * table
       * tags (maybe)
    """
    name = "travis"
    description = "Very basic formatter with maximum compatibility but shortened line lengths for Travis"

    LINE_WIDTH = 130

    def result(self, step):
        """
        Process the result of a step (after step execution).

        :param step:   Step object with result to process.
        """
        step = self.steps.pop(0)
        indent = make_indentation(2 * self.indent_size)
        if self.show_aligned_keywords:
            # -- RIGHT-ALIGN KEYWORDS (max. keyword width: 6):
            text = u"%s%6s %s" % (indent, step.keyword, step.name)
        else:
            text = u"%s%s %s" % (indent, step.keyword, step.name)
        if step.status.name == 'failed': text = u'\u26A0' + ' ' + text
        self.stream.write(textwrap.shorten(text, width=self.LINE_WIDTH - 30) + ' ')

        status_text = ': '
        status_text += {'passed': u'\u2713', 'failed': u'\u26A0'}[step.status.name]
        if self.show_timings:
            status_text += " in %0.3fs" % step.duration

        unicode_errors = 0
        if step.error_message:
            try:
                self.stream.write(u"%s\n%s\n" % (status_text, step.error_message))
            except UnicodeError as e:
                unicode_errors += 1
                self.stream.write(u"%s\n" % status_text)
                self.stream.write(u"%s while writing error message: %s\n" % \
                                  (e.__class__.__name__, e))
                if self.RAISE_OUTPUT_ERRORS:
                    raise
        else:
            self.stream.write(u"%s\n" % status_text)

        if self.show_multiline:
            if step.text:
                try:
                    self.doc_string(step.text)
                except UnicodeError as e:
                    unicode_errors += 1
                    self.stream.write(u"%s while writing docstring: %s\n" % \
                                      (e.__class__.__name__, e))
                    if self.RAISE_OUTPUT_ERRORS:
                        raise
            if step.table:
                self.table(step.table)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Django base settings for dum_common project.

from os.path import dirname, join, abspath

import boris

PROJECT_ROOT = abspath(dirname(boris.__file__))

gettext = lambda s: s

ADMINS = (
    ('Filip Varecha', 'filip.varecha@fragaria.cz'),
    ('Hynek Urban', 'hynek.urban@fragaria.cz'),
)
MANAGERS = ADMINS

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Prague'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'cs'

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

# Site ID
SITE_ID = 1

# Absolute path to the directory that holds media.
MEDIA_ROOT = join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'

ROOT_URLCONF = 'boris.urls'

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

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
)

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

FIXTURE_DIRS = (
   join(PROJECT_ROOT, 'fixtures'),
)

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

STATICFILES_DIRS = (
   join(PROJECT_ROOT, 'static_ex'),
)

STATIC_URL = '/static/'

INSTALLED_APPS = (
    'grappelli',
    # core django apps
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.redirects',
    'django.contrib.sitemaps',
    'django.contrib.staticfiles',

    'south',

    'boris.clients',
)

# always freeze south migrations
SOUTH_AUTO_FREEZE_APP = True

# don't run south tests
SKIP_SOUTH_TESTS = True

# logout the user on browser close
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

GRAPPELLI_ADMIN_TITLE = gettext(u'Informační systém BorIS')

<commit_msg>Updated web title<commit_after># -*- coding: utf-8 -*-
# Django base settings for dum_common project.

from os.path import dirname, join, abspath

import boris

PROJECT_ROOT = abspath(dirname(boris.__file__))

gettext = lambda s: s

ADMINS = (
    ('Filip Varecha', 'filip.varecha@fragaria.cz'),
    ('Hynek Urban', 'hynek.urban@fragaria.cz'),
)
MANAGERS = ADMINS

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Prague'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'cs'

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

# Site ID
SITE_ID = 1

# Absolute path to the directory that holds media.
MEDIA_ROOT = join(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'

ROOT_URLCONF = 'boris.urls'

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

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
)

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

FIXTURE_DIRS = (
   join(PROJECT_ROOT, 'fixtures'),
)

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

STATICFILES_DIRS = (
   join(PROJECT_ROOT, 'static_ex'),
)

STATIC_URL = '/static/'

INSTALLED_APPS = (
    'grappelli',
    # core django apps
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.redirects',
    'django.contrib.sitemaps',
    'django.contrib.staticfiles',

    'south',

    'boris.clients',
)

# always freeze south migrations
SOUTH_AUTO_FREEZE_APP = True

# don't run south tests
SKIP_SOUTH_TESTS = True

# logout the user on browser close
SESSION_EXPIRE_AT_BROWSER_CLOSE = True

GRAPPELLI_ADMIN_TITLE = gettext(u'Informační systém BorIS - Elektronická databáze pro evidenci výkonů v sociálních službách')

<|endoftext|>


<commit_before>import sys
import time
import json
import discord
from discord.ext import commands
from random import choice as rchoice
startepoch = time.time()
config = json.load(open('config.json'))

class Utility:
  def __init__(self, bot):
    self.bot = bot
    self.support = 'https://discord.gg/PEW4wx9'

  @commands.command()
  async def uptime(self, ctx):
    """Shows uptime of lolbot"""
    upEm = discord.Embed(title='Uptime', colour=0x690E8)
    startedOn = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(startepoch))
    #timeUp = get_up()
    nowEpoch = time.time()
    upEpoch = nowEpoch - startepoch
    upEm.add_field(name='Started on', value=startedOn)
    upEm.add_field(name='Uptime', value=time.strftime('%H:%M:%S', time.localtime(upEpoch)))
    try:
      await ctx.send(embed=upEm)
    except Exception as ex:
      await ctx.send('Something broke - sorry!')

  @commands.command()
  async def stats(self, ctx):
    """A few stats."""
    # get_owner = bot.get_user_info(config['ownerid'])
    statInfo = await ctx.bot.application_info()
    statEmbed = discord.Embed(title='Stats', description='This bot is'
   ' powered by [lolbot](https://github.com/xshotD/lolbot), a fast and powerful '
   'Python bot.', colour=0x690E8)
    statEmbed.add_field(name='Owner', value=statInfo.owner.mention + '('
    + str(statInfo.owner) + ' - ID: ' + str(statInfo.owner.id) + ')')
    statEmbed.add_field(name='Python', value=sys.version)
    statEmbed.add_field(name='discord.py', value=discord.__version__)
    statEmbed.add_field(name='Servers', value=len(self.bot.guilds))
    statPool = ['What have you done now?', 'Why should I do this again?', 'Oh..',
    'Where did the RAM go?', 'grumble grumble', 'Please hold.', 'No, just, no.',
    'Have you tried rebooting?', 'memework makes the dreamwork!']
    statEmbed.set_footer(text=rchoice(statPool))
    try:
      await ctx.send(embed=statEmbed)
    except:
      await ctx.send('Sorry, I can\'t send the Embed.')
      await ctx.send('Maybe I don\'t have Embed Links permission?')
    else:
      pass

  @commands.command()
  async def invite(self, ctx):
      """Gives a invite for the bot (and also the official server)"""
      invEmb = discord.Embed(colour=0x690E8)
      invEmb.add_field(name='Invite lolbot', value='[Click here]'
      '(' + discord.utils.oauth_url(config['botid']) + ')')
      invEmb.add_field(name='Official server', value='[Click here]'
      '(' + str(self.support) + ')')
      await ctx.send(embed=invEmb)

def setup(bot):
  bot.add_cog(Utility(bot))
<commit_msg>[cogs, util] escape embed warning by using direct link<commit_after>import sys
import time
import json
import discord
from discord.ext import commands
from random import choice as rchoice
startepoch = time.time()
config = json.load(open('config.json'))

class Utility:
  def __init__(self, bot):
    self.bot = bot
    self.support = 'https://discord.gg/PEW4wx9'

  @commands.command()
  async def uptime(self, ctx):
    """Shows uptime of lolbot"""
    upEm = discord.Embed(title='Uptime', colour=0x690E8)
    startedOn = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(startepoch))
    #timeUp = get_up()
    nowEpoch = time.time()
    upEpoch = nowEpoch - startepoch
    upEm.add_field(name='Started on', value=startedOn)
    upEm.add_field(name='Uptime', value=time.strftime('%H:%M:%S', time.localtime(upEpoch)))
    try:
      await ctx.send(embed=upEm)
    except Exception as ex:
      await ctx.send('Something broke - sorry!')

  @commands.command()
  async def stats(self, ctx):
    """A few stats."""
    # get_owner = bot.get_user_info(config['ownerid'])
    statInfo = await ctx.bot.application_info()
    statEmbed = discord.Embed(title='Stats', description='This bot is'
   ' powered by [lolbot](https://github.com/xshotD/lolbot), a fast and powerful '
   'Python bot.', colour=0x690E8)
    statEmbed.add_field(name='Owner', value=statInfo.owner.mention + '('
    + str(statInfo.owner) + ' - ID: ' + str(statInfo.owner.id) + ')')
    statEmbed.add_field(name='Python', value=sys.version)
    statEmbed.add_field(name='discord.py', value=discord.__version__)
    statEmbed.add_field(name='Servers', value=len(self.bot.guilds))
    statPool = ['What have you done now?', 'Why should I do this again?', 'Oh..',
    'Where did the RAM go?', 'grumble grumble', 'Please hold.', 'No, just, no.',
    'Have you tried rebooting?', 'memework makes the dreamwork!']
    statEmbed.set_footer(text=rchoice(statPool))
    try:
      await ctx.send(embed=statEmbed)
    except:
      await ctx.send('Sorry, I can\'t send the Embed.')
      await ctx.send('Maybe I don\'t have Embed Links permission?')
    else:
      pass

  @commands.command()
  async def invite(self, ctx):
      """Gives a invite for the bot (and also the official server)"""
      invEmb = discord.Embed(colour=0x690E8)
      invEmb.add_field(name='Invite lolbot', value='[Click here]'
      '(' + discord.utils.oauth_url(config['botid']) + ')')
      invEmb.add_field(name='Official server', value=str(self.support))
      await ctx.send(embed=invEmb)

def setup(bot):
  bot.add_cog(Utility(bot))
<|endoftext|>


<commit_before>#!python
"""
Entrypoint for starting the application.
"""
import os
import sys
import logging
import time
import threading
from optparse import OptionParser

try:
    daemon_support = True
    import daemon
    import signal
    import lockfile
except ImportError:
    daemon_support = False

from coilmq.config import config as global_config, init_config, init_logging, resolve_name
from coilmq.topic import TopicManager
from coilmq.queue import QueueManager

from coilmq.exception import ConfigError
from coilmq.server.socketserver import ThreadedStompServer

__authors__ = ['"Hans Lellelid" <hans@xmpl.org>']
__copyright__ = "Copyright 2009 Hans Lellelid"
__license__ = """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."""

logger = lambda: logging.getLogger(__name__)

def server_from_config(config=None, server_class=None, additional_kwargs=None):
    """
    Gets a configured L{coilmq.server.StompServer} from specified config.

    If `config` is None, global L{coilmq.config.config} var will be used instead.

    The `server_class` and `additional_kwargs` are primarily hooks for using this method
    from a testing environment.

    @param config: A C{ConfigParser.ConfigParser} instance with loaded config values.
    @type config: C{ConfigParser.ConfigParser}

    @param server_class: Which class to use for the server.  (This doesn't come from config currently.)
    @type server_class: C{class}

    @param additional_kwargs: Any additional args that should be passed to class.
    @type additional_kwargs: C{list}

    @return: The configured StompServer.
    @rtype: L{coilmq.server.StompServer}
    """
    global global_config
    if not config: config = global_config

    queue_store_factory = resolve_name(config.get('coilmq', 'qstore.factory'))
    subscriber_scheduler_factory = resolve_name(config.get('coilmq', 'scheduler.subscriber_priority_factory'))
    queue_scheduler_factory = resolve_name(config.get('coilmq', 'scheduler.queue_priority_factory'))

    if config.has_option('coilmq', 'auth.factory'):
        authenticator_factory = resolve_name(config.get('coilmq', 'auth.factory'))
        authenticator = authenticator_factory()
    else:
        authenticator = None

    server = ThreadedStompServer((config.get('coilmq', 'listen_addr'), config.getint('coilmq', 'listen_port')),
                                 queue_manager=QueueManager(store=queue_store_factory(),
                                                            subscriber_scheduler=subscriber_scheduler_factory(),
                                                            queue_scheduler=queue_scheduler_factory()),
                                 topic_manager=TopicManager(),
                                 authenticator=authenticator)
    logger().info("Created server:%r"% server)
    return server

def context_serve(options, context):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface
    and starts a server within that context.

    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough)
    context object for the non-daemon use case.

    @param options: The compiled collection of options that need to be parsed.
    @type options: C{ConfigParser}

    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}

    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    global global_config

    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if options.debug else logging.INFO
            init_logging(logfile=options.logfile, loglevel=level, configfile=options.configfile)

            server = server_from_config()
            logger().info("Stomp server listening on %s:%s" % server.server_address)

            if options.debug:
                poll_interval = float(global_config.get('coilmq', 'debug.stats_poll_interval'))
                if poll_interval: # Setting poll_interval to 0 effectively disables it.
                    def diagnostic_loop(server):
                        log = logger()
                        while True:
                            log.debug("Stats heartbeat -------------------------------")
                            store = server.queue_manager.store
                            for dest in store.destinations():
                                log.debug("Queue %s: size=%s, subscribers=%s" % (dest, store.size(dest), server.queue_manager.subscriber_count(dest)))

                            # TODO: Add number of subscribers?

                            time.sleep(poll_interval)

                    diagnostic_thread = threading.Thread(target=diagnostic_loop, name='DiagnosticThread', args=(server,))
                    diagnostic_thread.daemon = True
                    diagnostic_thread.start()

            server.serve_forever()

    except (KeyboardInterrupt, SystemExit):
        logger().info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception, e:
        logger().error("Stomp server stopped due to error: %s" % e)
        logger().exception(e)
        raise SystemExit()
    finally:
        if server: server.server_close()

def main():
    """
    Main entry point for running a socket server from the commandline.

    This method will read in options from the commandline and call the L{config.init_config} method
    to get everything setup.  Then, depending on whether deamon mode was specified or not,
    the process may be forked (or not) and the server will be started.
    """

    parser = OptionParser()
    parser.add_option("-c", "--config", dest="configfile",
                      help="Read configuration from FILE. (CLI options override config file.)", metavar="FILE")

    parser.add_option("-b", "--host", dest="listen_addr",
                      help="Listen on specified address (default 0.0.0.0)", metavar="ADDR")

    parser.add_option("-p", "--port", dest="listen_port",
                      help="Listen on specified port (default 61613)", type="int", metavar="PORT")

    parser.add_option("-l", "--logfile", dest="logfile",
                      help="Log to specified file (unless logging configured in config file).", metavar="FILE")

    parser.add_option("--debug", action="store_true", dest="debug", default=False,
                      help="Sets logging to debug (unless logging configured in config file).")

    parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=False,
                      help="Run server as a daemon (default False).")

    parser.add_option("-u", "--uid", dest="uid",
                      help="The user/UID to use for daemon process.", metavar="UID")

    parser.add_option("-g", "--gid", dest="gid",
                      help="The group/GID to use for daemon process.", metavar="GID")

    parser.add_option("--pidfile", dest="pidfile", help="The PID file to use.", metavar="FILE")

    parser.add_option("--umask", dest="umask",
                      help="Umask (octal) to apply for daemonized process.", metavar="MASK")

    parser.add_option('--rundir', dest="rundir",
                      help="The working directory to use for the daemonized process (default /).", metavar="DIR")

    (options, args) = parser.parse_args()

    # Note that we must initialize the configuration before we enter the context
    # block; however, we _cannot_ initialize logging until we are in the context block
    # (so we defer that until the context_serve call.)
    init_config(options.configfile)

    if options.listen_addr is not None:
        global_config.set('coilmq', 'listen_addr', options.listen_addr)

    if options.listen_port is not None:
        global_config.set('coilmq', 'listen_port', str(options.listen_port))

    if options.daemon:
        if not daemon_support:
            raise ConfigError("This environment/platform does not support running as daemon. (Are you running on *nix and did you install python-daemon package?)")
        else:

            context = daemon.DaemonContext()

            if options.uid is not None:
                context.uid = options.uid

            if options.gid is not None:
                context.gid = options.gid

            if options.pidfile is not None:
                context.pidfile = lockfile.FileLock(options.pidfile)

            if options.umask is not None:
                context.umask = int(options.umask, 8)

            if options.rundir is not None:
                context.working_directory = options.rundir

            context_serve(options, context)

    else:
        # Non-daemon mode, so we use a dummy context objectx
        # so we can use the same run-server code as the daemon version.

        class DumbContext(object):
            def __enter__(self):
                pass
            def __exit__(self, type, value, traceback):
                pass

        context_serve(options, DumbContext())

if __name__ == '__main__':
    try:
        main()
    except (KeyboardInterrupt, SystemExit):
        pass
    except Exception, e:
        logger().error("Server terminated due to error: %s" % e)
        logger().exception(e)<commit_msg>(Fixes issue 29) Fixing the default in the help message to indicate that we bind to 127.0.0.1 by default.<commit_after>#!python
"""
Entrypoint for starting the application.
"""
import os
import sys
import logging
import time
import threading
from optparse import OptionParser

try:
    daemon_support = True
    import daemon
    import signal
    import lockfile
except ImportError:
    daemon_support = False

from coilmq.config import config as global_config, init_config, init_logging, resolve_name
from coilmq.topic import TopicManager
from coilmq.queue import QueueManager

from coilmq.exception import ConfigError
from coilmq.server.socketserver import ThreadedStompServer

__authors__ = ['"Hans Lellelid" <hans@xmpl.org>']
__copyright__ = "Copyright 2009 Hans Lellelid"
__license__ = """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."""

logger = lambda: logging.getLogger(__name__)

def server_from_config(config=None, server_class=None, additional_kwargs=None):
    """
    Gets a configured L{coilmq.server.StompServer} from specified config.

    If `config` is None, global L{coilmq.config.config} var will be used instead.

    The `server_class` and `additional_kwargs` are primarily hooks for using this method
    from a testing environment.

    @param config: A C{ConfigParser.ConfigParser} instance with loaded config values.
    @type config: C{ConfigParser.ConfigParser}

    @param server_class: Which class to use for the server.  (This doesn't come from config currently.)
    @type server_class: C{class}

    @param additional_kwargs: Any additional args that should be passed to class.
    @type additional_kwargs: C{list}

    @return: The configured StompServer.
    @rtype: L{coilmq.server.StompServer}
    """
    global global_config
    if not config: config = global_config

    queue_store_factory = resolve_name(config.get('coilmq', 'qstore.factory'))
    subscriber_scheduler_factory = resolve_name(config.get('coilmq', 'scheduler.subscriber_priority_factory'))
    queue_scheduler_factory = resolve_name(config.get('coilmq', 'scheduler.queue_priority_factory'))

    if config.has_option('coilmq', 'auth.factory'):
        authenticator_factory = resolve_name(config.get('coilmq', 'auth.factory'))
        authenticator = authenticator_factory()
    else:
        authenticator = None

    server = ThreadedStompServer((config.get('coilmq', 'listen_addr'), config.getint('coilmq', 'listen_port')),
                                 queue_manager=QueueManager(store=queue_store_factory(),
                                                            subscriber_scheduler=subscriber_scheduler_factory(),
                                                            queue_scheduler=queue_scheduler_factory()),
                                 topic_manager=TopicManager(),
                                 authenticator=authenticator)
    logger().info("Created server:%r"% server)
    return server

def context_serve(options, context):
    """
    Takes a context object, which implements the __enter__/__exit__ "with" interface
    and starts a server within that context.

    This method is a refactored single-place for handling the server-run code whether
    running in daemon or non-daemon mode.  It is invoked with a dummy (passthrough)
    context object for the non-daemon use case.

    @param options: The compiled collection of options that need to be parsed.
    @type options: C{ConfigParser}

    @param context: The context object that implements __enter__/__exit__ "with" methods.
    @type context: C{object}

    @raise Exception: Any underlying exception will be logged but then re-raised.
    @see: server_from_config()
    """
    global global_config

    server = None
    try:
        with context:
            # There's a possibility here that init_logging() will throw an exception.  If it does,
            # AND we're in a daemon context, then we're not going to be able to do anything with it.
            # We've got no stderr/stdout here; and so (to my knowledge) no reliable (& cross-platform),
            # way to display errors.
            level = logging.DEBUG if options.debug else logging.INFO
            init_logging(logfile=options.logfile, loglevel=level, configfile=options.configfile)

            server = server_from_config()
            logger().info("Stomp server listening on %s:%s" % server.server_address)

            if options.debug:
                poll_interval = float(global_config.get('coilmq', 'debug.stats_poll_interval'))
                if poll_interval: # Setting poll_interval to 0 effectively disables it.
                    def diagnostic_loop(server):
                        log = logger()
                        while True:
                            log.debug("Stats heartbeat -------------------------------")
                            store = server.queue_manager.store
                            for dest in store.destinations():
                                log.debug("Queue %s: size=%s, subscribers=%s" % (dest, store.size(dest), server.queue_manager.subscriber_count(dest)))

                            # TODO: Add number of subscribers?

                            time.sleep(poll_interval)

                    diagnostic_thread = threading.Thread(target=diagnostic_loop, name='DiagnosticThread', args=(server,))
                    diagnostic_thread.daemon = True
                    diagnostic_thread.start()

            server.serve_forever()

    except (KeyboardInterrupt, SystemExit):
        logger().info("Stomp server stopped by user interrupt.")
        raise SystemExit()
    except Exception, e:
        logger().error("Stomp server stopped due to error: %s" % e)
        logger().exception(e)
        raise SystemExit()
    finally:
        if server: server.server_close()

def main():
    """
    Main entry point for running a socket server from the commandline.

    This method will read in options from the commandline and call the L{config.init_config} method
    to get everything setup.  Then, depending on whether deamon mode was specified or not,
    the process may be forked (or not) and the server will be started.
    """

    parser = OptionParser()
    parser.add_option("-c", "--config", dest="configfile",
                      help="Read configuration from FILE. (CLI options override config file.)", metavar="FILE")

    parser.add_option("-b", "--host", dest="listen_addr",
                      help="Listen on specified address (default 127.0.0.1)", metavar="ADDR")

    parser.add_option("-p", "--port", dest="listen_port",
                      help="Listen on specified port (default 61613)", type="int", metavar="PORT")

    parser.add_option("-l", "--logfile", dest="logfile",
                      help="Log to specified file (unless logging configured in config file).", metavar="FILE")

    parser.add_option("--debug", action="store_true", dest="debug", default=False,
                      help="Sets logging to debug (unless logging configured in config file).")

    parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=False,
                      help="Run server as a daemon (default False).")

    parser.add_option("-u", "--uid", dest="uid",
                      help="The user/UID to use for daemon process.", metavar="UID")

    parser.add_option("-g", "--gid", dest="gid",
                      help="The group/GID to use for daemon process.", metavar="GID")

    parser.add_option("--pidfile", dest="pidfile", help="The PID file to use.", metavar="FILE")

    parser.add_option("--umask", dest="umask",
                      help="Umask (octal) to apply for daemonized process.", metavar="MASK")

    parser.add_option('--rundir', dest="rundir",
                      help="The working directory to use for the daemonized process (default /).", metavar="DIR")

    (options, args) = parser.parse_args()

    # Note that we must initialize the configuration before we enter the context
    # block; however, we _cannot_ initialize logging until we are in the context block
    # (so we defer that until the context_serve call.)
    init_config(options.configfile)

    if options.listen_addr is not None:
        global_config.set('coilmq', 'listen_addr', options.listen_addr)

    if options.listen_port is not None:
        global_config.set('coilmq', 'listen_port', str(options.listen_port))

    if options.daemon:
        if not daemon_support:
            raise ConfigError("This environment/platform does not support running as daemon. (Are you running on *nix and did you install python-daemon package?)")
        else:

            context = daemon.DaemonContext()

            if options.uid is not None:
                context.uid = options.uid

            if options.gid is not None:
                context.gid = options.gid

            if options.pidfile is not None:
                context.pidfile = lockfile.FileLock(options.pidfile)

            if options.umask is not None:
                context.umask = int(options.umask, 8)

            if options.rundir is not None:
                context.working_directory = options.rundir

            context_serve(options, context)

    else:
        # Non-daemon mode, so we use a dummy context objectx
        # so we can use the same run-server code as the daemon version.

        class DumbContext(object):
            def __enter__(self):
                pass
            def __exit__(self, type, value, traceback):
                pass

        context_serve(options, DumbContext())

if __name__ == '__main__':
    try:
        main()
    except (KeyboardInterrupt, SystemExit):
        pass
    except Exception, e:
        logger().error("Server terminated due to error: %s" % e)
        logger().exception(e)<|endoftext|>


<commit_before>import logging

from mixpanel import Mixpanel

from django.conf import settings
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from django.http import Http404
from comics.models import Post
from petroglyphs.models import Setting
from pebbles.models import Pebble, PebbleSettings

class LatestPostFeed(Feed):
    description_template = "postfeed.html"

    def link(self):
        return 'http://' + self.pebble_settings.primary_domain.url

    def feed_url(self):
        return 'http://' + self.pebble_settings.primary_domain.url + '/feed/'

    def description(self):
        return self.pebble_settings.settings.get('feed_description', 'Comic Feed')

    def title(self):
        return self.pebble_settings.settings.get('feed_title', self.pebble.title)

    def items(self):
        return Post.published_posts.filter(pebbles=self.pebble).order_by('-published')[:5]

    def item_title(self, item):
        return item.title

    def item_link(self, item):
        url = self.pebble_settings.primary_domain.url
        if item.comic:
            return 'http://' + url + reverse('comicpostview', args=[item.slug])
        return 'http://' + url + reverse('postview', args=[item.slug])

    def item_pubdate(self, item):
        return item.published

    def get_feed(self, obj, request):
        log_string = "path='%s' host='%s' remote_addr='%s' user_agent='%s'" % (
            request.path,
            request.get_host(),
            request.META.get('REMOTE_ADDR'),
            request.META.get('HTTP_USER_AGENT'),
        )
        loggly_logger = logging.getLogger('loggly_logs')
        loggly_logger.info(log_string)
        self.request = request
        self.pebble = self.request.pebble
        if not self.pebble:
            raise Http404()
        self.pebble_settings = PebbleSettings.objects.get(pebble=self.pebble)
        feed = super(LatestPostFeed, self).get_feed(obj, request)
        mp = Mixpanel(Setting.objects.get(key='mixpanel_key').value)
        mp.track('rss', 'rss_hit', {
            'pebble_id': self.request.pebble.id,
            'user_agent': request.META.get('HTTP_USER_AGENT'),
            'remote_addr': request.META.get('REMOTE_ADDR'),
            'http_host': request.META.get('HTTP_HOST'),
        })
        return feed
<commit_msg>Try to grab remote user ip<commit_after>import logging

from mixpanel import Mixpanel

from django.conf import settings
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from django.http import Http404
from comics.models import Post
from petroglyphs.models import Setting
from pebbles.models import Pebble, PebbleSettings

class LatestPostFeed(Feed):
    description_template = "postfeed.html"

    def link(self):
        return 'http://' + self.pebble_settings.primary_domain.url

    def feed_url(self):
        return 'http://' + self.pebble_settings.primary_domain.url + '/feed/'

    def description(self):
        return self.pebble_settings.settings.get('feed_description', 'Comic Feed')

    def title(self):
        return self.pebble_settings.settings.get('feed_title', self.pebble.title)

    def items(self):
        return Post.published_posts.filter(pebbles=self.pebble).order_by('-published')[:5]

    def item_title(self, item):
        return item.title

    def item_link(self, item):
        url = self.pebble_settings.primary_domain.url
        if item.comic:
            return 'http://' + url + reverse('comicpostview', args=[item.slug])
        return 'http://' + url + reverse('postview', args=[item.slug])

    def item_pubdate(self, item):
        return item.published

    def get_feed(self, obj, request):
        log_string = "path='%s' host=%s remote_addr=%s forwarded_for='%s' user_agent=%s" % (
            request.path,
            request.get_host(),
            request.META.get('REMOTE_ADDR', ''),
            request.META.get('HTTP_X_FORWARDED_FOR', ''),
            request.META.get('HTTP_USER_AGENT', ''),
        )
        loggly_logger = logging.getLogger('loggly_logs')
        loggly_logger.info(log_string)
        self.request = request
        self.pebble = self.request.pebble
        if not self.pebble:
            raise Http404()
        self.pebble_settings = PebbleSettings.objects.get(pebble=self.pebble)
        feed = super(LatestPostFeed, self).get_feed(obj, request)
        mp = Mixpanel(Setting.objects.get(key='mixpanel_key').value)
        mp.track('rss', 'rss_hit', {
            'pebble_id': self.request.pebble.id,
            'user_agent': request.META.get('HTTP_USER_AGENT'),
            'remote_addr': request.META.get('REMOTE_ADDR'),
            'http_host': request.META.get('HTTP_HOST'),
        })
        return feed
<|endoftext|>


<commit_before>from django.views import generic

from gallery.models import GalleryImage
from .models import Arc, Issue

class IndexView(generic.ListView):
    model = Arc
    template_name = "comics/index.html"
    context_object_name = "arcs"

class IssueView(generic.DetailView):
    model = Issue
    template_name = "comics/issue.html"

class ComicPageView(generic.DetailView):
    model = GalleryImage
    template_name = "comics/comic_page.html"

    def get_queryset(self):
        query_set = (
            super().get_queryset().filter(issue__slug=self.kwargs.get("issue_slug"))
        )
        return query_set
<commit_msg>Fix bug where arc slug could be literally anything<commit_after>from django.views import generic

from gallery.models import GalleryImage
from .models import Arc, Issue

class IndexView(generic.ListView):
    model = Arc
    template_name = "comics/index.html"
    context_object_name = "arcs"

class IssueView(generic.DetailView):
    model = Issue
    template_name = "comics/issue.html"

    def get_queryset(self):
        query_set = super().get_queryset().filter(arc__slug=self.kwargs.get("arc_slug"))
        return query_set

class ComicPageView(generic.DetailView):
    model = GalleryImage
    template_name = "comics/comic_page.html"

    def get_queryset(self):
        query_set = (
            super().get_queryset().filter(issue__slug=self.kwargs.get("issue_slug"))
        )
        return query_set
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  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.
###############################################################################

import base64
import datetime
import hashlib

from girder.constants import AccessType
from girder.models.user import User as UserModel
from girder.models.model_base import AccessException, ValidationException

class User(UserModel):
    def initialize(self):
        super(User, self).initialize()

        # Note, this will not expose this field though the upstream User API
        self.exposeFields(level=AccessType.READ, fields=('acceptTerms',))

    def filteredSummary(self, user, accessorUser):
        if self.hasAccess(user, accessorUser):
            return {
                field: user[field]
                for field in
                ['_id', 'login', 'firstName', 'lastName']
            }
        else:
            obfuscatedName = base64.b32encode(
                hashlib.sha256(user['login'].encode('utf8')).digest()
            ).decode('ascii')[:4]
            return {
                '_id': user['_id'],
                'name': 'User %s' % obfuscatedName
            }

    def _isAdminOrMember(self, user, groupName):
        Group = self.model('group')
        if not user:
            return False
        if user.get('admin', False):
            return True
        group = Group.findOne({'name': groupName})
        if not group:
            raise ValidationException('Could not load group: %s' % groupName)
        return group['_id'] in user['groups']

    def canAcceptTerms(self, user):
        return user.get('acceptTerms') is not None

    def acceptTerms(self, user):
        user['acceptTerms'] = datetime.datetime.utcnow()

    def requireAcceptTerms(self, user):
        if not self.canAcceptTerms(user):
            raise AccessException(
                'The user has not accepted the Terms of Use.')

    def canCreateDataset(self, user):
        return self._isAdminOrMember(user, 'Dataset Contributors')

    def requireCreateDataset(self, user):
        if not self.canCreateDataset(user):
            raise AccessException(
                'Only members of the Dataset Contributors group can create '
                'datasets.')

    def canReviewDataset(self, user):
        return self._isAdminOrMember(user, 'Dataset QC Reviewers')

    def requireReviewDataset(self, user):
        if not self.canReviewDataset(user):
            raise AccessException(
                'Only members of the Dataset QC Reviewers group can review '
                'datasets.')

    def getSegmentationSkill(self, user):
        Group = self.model('group')
        Segmentation = self.model('segmentation', 'isic_archive')
        if not user:
            return None
        expertGroup = Group.findOne({'name': 'Segmentation Experts'})
        if expertGroup['_id'] in user['groups']:
            return Segmentation.Skill.EXPERT
        noviceGroup = Group.findOne({'name': 'Segmentation Novices'})
        if noviceGroup['_id'] in user['groups']:
            return Segmentation.Skill.NOVICE
        return None

    def requireSegmentationSkill(self, user):
        if self.getSegmentationSkill(user) is None:
            raise AccessException(
                'Only members of the Segmentation Experts and Segmentation '
                'Novices groups can create or review segmentations.')

    def canAdminStudy(self, user):
        return self._isAdminOrMember(user, 'Study Administrators')

    def requireAdminStudy(self, user):
        if not self.canAdminStudy(user):
            raise AccessException(
                'Only members of the Study Administrators group can create or '
                'modify studies.')
<commit_msg>Add a math comment<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  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.
###############################################################################

import base64
import datetime
import hashlib

from girder.constants import AccessType
from girder.models.user import User as UserModel
from girder.models.model_base import AccessException, ValidationException

class User(UserModel):
    def initialize(self):
        super(User, self).initialize()

        # Note, this will not expose this field though the upstream User API
        self.exposeFields(level=AccessType.READ, fields=('acceptTerms',))

    def filteredSummary(self, user, accessorUser):
        if self.hasAccess(user, accessorUser):
            return {
                field: user[field]
                for field in
                ['_id', 'login', 'firstName', 'lastName']
            }
        else:
            # For 4 characters of a Base32 encoding, we have 20 bits of entropy,
            # or 1,048,576 possible combinations. Per the formula at
            # http://preshing.com/20110504/hash-collision-probabilities/
            # and assuming 100 (active) users, we have a collision probability
            # of 0.47%.
            obfuscatedName = base64.b32encode(
                hashlib.sha256(user['login'].encode('utf8')).digest()
            ).decode('ascii')[:4]
            return {
                '_id': user['_id'],
                'name': 'User %s' % obfuscatedName
            }

    def _isAdminOrMember(self, user, groupName):
        Group = self.model('group')
        if not user:
            return False
        if user.get('admin', False):
            return True
        group = Group.findOne({'name': groupName})
        if not group:
            raise ValidationException('Could not load group: %s' % groupName)
        return group['_id'] in user['groups']

    def canAcceptTerms(self, user):
        return user.get('acceptTerms') is not None

    def acceptTerms(self, user):
        user['acceptTerms'] = datetime.datetime.utcnow()

    def requireAcceptTerms(self, user):
        if not self.canAcceptTerms(user):
            raise AccessException(
                'The user has not accepted the Terms of Use.')

    def canCreateDataset(self, user):
        return self._isAdminOrMember(user, 'Dataset Contributors')

    def requireCreateDataset(self, user):
        if not self.canCreateDataset(user):
            raise AccessException(
                'Only members of the Dataset Contributors group can create '
                'datasets.')

    def canReviewDataset(self, user):
        return self._isAdminOrMember(user, 'Dataset QC Reviewers')

    def requireReviewDataset(self, user):
        if not self.canReviewDataset(user):
            raise AccessException(
                'Only members of the Dataset QC Reviewers group can review '
                'datasets.')

    def getSegmentationSkill(self, user):
        Group = self.model('group')
        Segmentation = self.model('segmentation', 'isic_archive')
        if not user:
            return None
        expertGroup = Group.findOne({'name': 'Segmentation Experts'})
        if expertGroup['_id'] in user['groups']:
            return Segmentation.Skill.EXPERT
        noviceGroup = Group.findOne({'name': 'Segmentation Novices'})
        if noviceGroup['_id'] in user['groups']:
            return Segmentation.Skill.NOVICE
        return None

    def requireSegmentationSkill(self, user):
        if self.getSegmentationSkill(user) is None:
            raise AccessException(
                'Only members of the Segmentation Experts and Segmentation '
                'Novices groups can create or review segmentations.')

    def canAdminStudy(self, user):
        return self._isAdminOrMember(user, 'Study Administrators')

    def requireAdminStudy(self, user):
        if not self.canAdminStudy(user):
            raise AccessException(
                'Only members of the Study Administrators group can create or '
                'modify studies.')
<|endoftext|>


<commit_before>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cartopy.crs as ccrs

from xmitgcm import open_mdsdataset
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
plt.ion()

dir0 = '/homedata/bderembl/runmit/test_southatlgyre3'

ds0 = open_mdsdataset(dir0,prefix=['T'])

nt = 0
nz = 0
while (nt < 150) :
    nt = nt+1
    print(nt)
    plt.figure(1)
    ax = plt.subplot(projection=ccrs.PlateCarree());
    ds0['T'][nt,nz,:,:].plot.pcolormesh('XC', 'YC',ax=ax,vmin=-10,vmax=35,cmap='ocean')
    plt.title('Case 4 : Temperature ')
    plt.text(5,5,nt,ha='center',wrap=True)
    ax.coastlines()
    gl = ax.gridlines(draw_labels=True, alpha = 0.5, linestyle='--');
    gl.xlabels_top = False
    gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    if (nt < 10):
        plt.savefig('Temperature_cas4-'+'00'+str(nt)+'.png')
        plt.clf()
    elif (nt > 9) and (nt < 100):
        plt.savefig('Temperature_cas4-'+'0'+str(nt)+'.png')
        plt.clf()
    else:
        plt.savefig('Temperature_cas4-'+str(nt)+'.png')
        plt.clf()
<commit_msg>Update B_Temperature.py<commit_after>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cartopy.crs as ccrs

from xmitgcm import open_mdsdataset
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
plt.ion()

dir0 = '/homedata/bderembl/runmit/test_southatlgyre3'

ds0 = open_mdsdataset(dir0,prefix=['T'])

nt = 0
nz = 0
while (nt < 150) :
    nt = nt+1
    print(nt)
    plt.figure(1)
    ax = plt.subplot(projection=ccrs.PlateCarree());
    ds0['T'][nt,nz,:,:].plot.pcolormesh('XC', 'YC',ax=ax,vmin=0,vmax=35,cmap='ocean')
    plt.title('Case 4 : Temperature ')
    plt.text(5,5,nt,ha='center',wrap=True)
    ax.coastlines()
    gl = ax.gridlines(draw_labels=True, alpha = 0.5, linestyle='--');
    gl.xlabels_top = False
    gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    if (nt < 10):
        plt.savefig('Temperature_cas4-'+'00'+str(nt)+'.png')
        plt.clf()
    elif (nt > 9) and (nt < 100):
        plt.savefig('Temperature_cas4-'+'0'+str(nt)+'.png')
        plt.clf()
    else:
        plt.savefig('Temperature_cas4-'+str(nt)+'.png')
        plt.clf()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- author: Kung Fury -*-

import socket
import urllib2
import os
import sys
import platform

class SystemUtils(object):

    VERSION_CENTOS_6_6 = 'Centos 6.6'
    VERSION_CENTOS_6_5 = 'Centos 6.5'
    VERSION_CENTOS_7 = 'Centos 7'

    # Check if Internet connection is working
    @staticmethod
    def check_internet_connection():
        try:
            url = 'http://www.google.com'
            data = urllib2.urlopen(url, timeout=1)
            print 'Connection to Internet is Ok \n'
        except urllib2.URLError as err:
            pass
            print 'You are not connected to Internet \n'

    # Check local hostname
    @staticmethod
    def check_host_name():
        host_name = socket.getfqdn()
        print "Hostname is: ", host_name

    # Check local IP address
    @staticmethod
    def check_local_ip():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 0))
        local_ip_address = s.getsockname()[0]
        return local_ip_address

    # Check if user run CentOS 6.5 or 6.6
    @staticmethod
    def check_centos_version():
        if os.path.exists("/etc/redhat-release"):
            with open("/etc/redhat-release", 'r') as file:
                version = ''
                for line in file:
                    version += line
                if 'release 6.6' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_6_6
                elif 'release 6.5' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_6_5
                elif 'CentOS Linux release 7' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_7
                    file.close()
        else:
            print '\n<------------SYSTEM INFO------------>'
            print sys.version
            print 'Architecture: ' + platform.machine()
            print 'Platform: ' + platform.system()
            #print platform.uname()
            print 'Your version of Linux incompatible with this script, exit...'
            print '<------------SYSTEM INFO------------>\n'
            sys.exit()

<commit_msg>- Fixes in DeployScript<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- author: Kung Fury -*-

import socket
import urllib2
import os
import sys
import platform

class SystemUtils(object):

    VERSION_CENTOS_6_6 = 'Centos 6.6'
    VERSION_CENTOS_6_5 = 'Centos 6.5'
    VERSION_CENTOS_7 = 'Centos 7'

    # Check if Internet connection is working
    @staticmethod
    def check_internet_connection():
        try:
            url = 'http://www.google.com'
            data = urllib2.urlopen(url, timeout=1)
            print 'Connection to Internet is Ok \n'
        except urllib2.URLError as err:
            pass
            print 'You are not connected to Internet \n'

    # Check local hostname
    @staticmethod
    def check_host_name():
        host_name = socket.getfqdn()
        print "Hostname is: ", host_name

    # Check local IP address
    @staticmethod
    def check_local_ip():
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 0))
        local_ip_address = s.getsockname()[0]
        return local_ip_address

    # Check if user run CentOS 6.5 or 6.6
    @staticmethod
    def check_centos_version():
        if os.path.exists("/etc/redhat-release"):
            with open("/etc/redhat-release", 'r') as file:
                version = ''
                for line in file:
                    version += line
                if 'release 6.6' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_6_6
                elif 'release 6.5' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_6_5
                elif 'CentOS Linux release 7' in version:
                    print 'You are using: ' + SystemUtils.VERSION_CENTOS_7
                    file.close()
        else:
            print '\n<------------SYSTEM INFO------------>'
            print sys.version
            print 'Architecture: ' + platform.machine()
            print 'Platform: ' + platform.platform()
            #print platform.uname()
            print 'Your version of Linux incompatible with this script, exit...'
            print '<------------SYSTEM INFO------------>\n'
            sys.exit()

<|endoftext|>


<commit_before>from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
from django.conf import settings
import datetime
import Image, ImageFilter
import os.path

AVATARSIZES = ( 128, 96, 64, 32, 16 )
GENDER_CHOICES = ( ('F', _('Female')), ('M', _('Male')),)
GENDER_IMAGES = { "M": "%simages/male.png" % settings.MEDIA_URL, "F": "%simages/female.png" % settings.MEDIA_URL }

class Continent(models.Model):
    """
    Continent class. Simple class with the information about continents.
    It can be filled up with calling the "importdata" method:

    >>> Continent().importdata()

    """
    slug = models.SlugField(prepopulate_from=('name',), unique=True)
    code = models.CharField(max_length=2, primary_key=True)
    name = models.CharField(max_length=255, unique=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/continent/%s/" % self.slug

    def importdata(self):
        Continent.objects.all().delete()
        Continent(name="Asia", slug=slugify("Asia"), code="AS").save()
        Continent(name="Africa", slug=slugify("Africa"), code="AF").save()
        Continent(name="Europe", slug=slugify("Europe"), code="EU").save()
        Continent(name="North America", slug=slugify("North America"), code="NA").save()
        Continent(name="South America", slug=slugify("South America"), code="SA").save()
        Continent(name="Oceania", slug=slugify("Oceania"), code="OC").save()
        Continent(name="Antarctica", slug=slugify("Antarctica"), code="AN").save()

    class Admin:
        pass

    class Meta:
        verbose_name = _('Continent')
        verbose_name_plural = _('Continents')

class Country(models.Model):
    """
    Country class with the countries data needed in the Profile class. Dependent
    of the Continent class.
    To fill it with data, the file "countries.txt" is needed:
    >>> Country().importdata()
    """
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(prepopulate_from=('name',), unique=True)
    code = models.CharField(max_length=2, primary_key=True)
    continent = models.ForeignKey(Continent)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/country/%s/" % self.slug

    def importdata(self, file="db/countries.txt"):
        Country.objects.all().delete()
        f = open(file)
        for line in f.xreadlines():
            line = line.strip()
            d, name = line.split('"')[:-1]
            continent, code = d.split(",")[:-1]
            c = Continent.objects.filter(code=continent)[0]
            p = Country(name=name, slug=slugify(name), code=code, continent=c)
            p.save()

    class Admin:
        list_display = ('name', 'continent')
        list_filter = ['continent']

    class Meta:
        ordering = ['name']
        verbose_name = _('Country')
        verbose_name_plural = _('Countries')

class Avatar(models.Model):
    """
    Avatar class. Every user can have one avatar associated.
    """
    photo = models.ImageField(upload_to="avatars/%Y/%b/%d")
    date = models.DateTimeField(default=datetime.datetime.now)
    user = models.OneToOneField(User, blank=True)
    box = models.CharField(max_length=255, blank=True)
    valid = models.BooleanField(default=False)

    def get_absolute_url(self):
        return self.get_photo_url()

    def __unicode__(self):
        return "%s-%s" % (self.user, self.photo)

    class Admin:
        pass

    def save(self):

        super(Avatar, self).save()

        if self.valid:
            Avatar.objects.filter(user=self.user).exclude(pk=self.pk).delete()
            base, ext = os.path.splitext(self.get_photo_filename())

            image = Image.open(self.get_photo_filename())
            box = self.box.split("-")
            box = [ int(num) for num in box ]
            image = image.crop(box)
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            for size in AVATARSIZES:
                image.thumbnail((size, size), Image.ANTIALIAS)
                image.save("%s.%s%s" % ( base, size, ext))

            del image

    def delete(self):
        filename = self.get_photo_filename()
        base, ext = os.path.splitext(filename)
        for size in AVATARSIZES:
            try:
                os.remove("%s.%s%s" % ( base, size, ext))
            except:
                pass

        try:
            os.remove(self.get_photo_filename())
        except:
            pass

        super(Avatar, self).delete()

class Profile(models.Model):
    """
    User profile model
    """

    firstname = models.CharField(max_length=255, blank=True)
    surname = models.CharField(max_length=255, blank=True)
    user = models.OneToOneField(User)
    birthdate = models.DateField(default=datetime.date.today(), blank=True)
    date = models.DateTimeField(default=datetime.datetime.now)
    url = models.URLField(blank=True, core=True)
    about = models.TextField(blank=True)
    latitude = models.DecimalField(max_digits=10, decimal_places=6, default=0)
    longitude = models.DecimalField(max_digits=10, decimal_places=6, default=0)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
    country = models.ForeignKey(Country, null=True, blank=True)
    location = models.CharField(max_length=255, blank=True)

    class Admin:
        pass

    def __unicode__(self):
        return _("%s's profile") % self.user

    def get_genderimage_url(self):
        return GENDER_IMAGES[self.gender]

    def get_absolute_url(self):
        return "/profile/users/%s/" % self.user

    def yearsold(self):
        return (datetime.date.today().toordinal() - self.birthdate.toordinal()) / 365
<commit_msg>Modified the Profile model with the new syntax for the OneToOne fields<commit_after>from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
from django.conf import settings
import datetime
import Image, ImageFilter
import os.path

AVATARSIZES = ( 128, 96, 64, 32, 16 )
GENDER_CHOICES = ( ('F', _('Female')), ('M', _('Male')),)
GENDER_IMAGES = { "M": "%simages/male.png" % settings.MEDIA_URL, "F": "%simages/female.png" % settings.MEDIA_URL }

class Continent(models.Model):
    """
    Continent class. Simple class with the information about continents.
    It can be filled up with calling the "importdata" method:

    >>> Continent().importdata()

    """
    slug = models.SlugField(prepopulate_from=('name',), unique=True)
    code = models.CharField(max_length=2, primary_key=True)
    name = models.CharField(max_length=255, unique=True)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/continent/%s/" % self.slug

    def importdata(self):
        Continent.objects.all().delete()
        Continent(name="Asia", slug=slugify("Asia"), code="AS").save()
        Continent(name="Africa", slug=slugify("Africa"), code="AF").save()
        Continent(name="Europe", slug=slugify("Europe"), code="EU").save()
        Continent(name="North America", slug=slugify("North America"), code="NA").save()
        Continent(name="South America", slug=slugify("South America"), code="SA").save()
        Continent(name="Oceania", slug=slugify("Oceania"), code="OC").save()
        Continent(name="Antarctica", slug=slugify("Antarctica"), code="AN").save()

    class Admin:
        pass

    class Meta:
        verbose_name = _('Continent')
        verbose_name_plural = _('Continents')

class Country(models.Model):
    """
    Country class with the countries data needed in the Profile class. Dependent
    of the Continent class.
    To fill it with data, the file "countries.txt" is needed:
    >>> Country().importdata()
    """
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(prepopulate_from=('name',), unique=True)
    code = models.CharField(max_length=2, primary_key=True)
    continent = models.ForeignKey(Continent)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "/country/%s/" % self.slug

    def importdata(self, file="db/countries.txt"):
        Country.objects.all().delete()
        f = open(file)
        for line in f.xreadlines():
            line = line.strip()
            d, name = line.split('"')[:-1]
            continent, code = d.split(",")[:-1]
            c = Continent.objects.filter(code=continent)[0]
            p = Country(name=name, slug=slugify(name), code=code, continent=c)
            p.save()

    class Admin:
        list_display = ('name', 'continent')
        list_filter = ['continent']

    class Meta:
        ordering = ['name']
        verbose_name = _('Country')
        verbose_name_plural = _('Countries')

class Avatar(models.Model):
    """
    Avatar class. Every user can have one avatar associated.
    """
    photo = models.ImageField(upload_to="avatars/%Y/%b/%d")
    date = models.DateTimeField(default=datetime.datetime.now)
    user = models.OneToOneField(User, blank=True)
    box = models.CharField(max_length=255, blank=True)
    valid = models.BooleanField(default=False)

    def get_absolute_url(self):
        return self.get_photo_url()

    def __unicode__(self):
        return "%s-%s" % (self.user, self.photo)

    class Admin:
        pass

    def save(self):

        super(Avatar, self).save()

        if self.valid:
            Avatar.objects.filter(user=self.user).exclude(pk=self.pk).delete()
            base, ext = os.path.splitext(self.get_photo_filename())

            image = Image.open(self.get_photo_filename())
            box = self.box.split("-")
            box = [ int(num) for num in box ]
            image = image.crop(box)
            if image.mode not in ('L', 'RGB'):
                image = image.convert('RGB')

            for size in AVATARSIZES:
                image.thumbnail((size, size), Image.ANTIALIAS)
                image.save("%s.%s%s" % ( base, size, ext))

            del image

    def delete(self):
        filename = self.get_photo_filename()
        base, ext = os.path.splitext(filename)
        for size in AVATARSIZES:
            try:
                os.remove("%s.%s%s" % ( base, size, ext))
            except:
                pass

        try:
            os.remove(self.get_photo_filename())
        except:
            pass

        super(Avatar, self).delete()

class Profile(models.Model):
    """
    User profile model
    """

    firstname = models.CharField(max_length=255, blank=True)
    surname = models.CharField(max_length=255, blank=True)
    user = models.OneToOneField(User, primary_key=True)
    birthdate = models.DateField(default=datetime.date.today(), blank=True)
    date = models.DateTimeField(default=datetime.datetime.now)
    url = models.URLField(blank=True, core=True)
    about = models.TextField(blank=True)
    latitude = models.DecimalField(max_digits=10, decimal_places=6, default=0)
    longitude = models.DecimalField(max_digits=10, decimal_places=6, default=0)
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
    country = models.ForeignKey(Country, null=True, blank=True)
    location = models.CharField(max_length=255, blank=True)

    class Admin:
        pass

    def __unicode__(self):
        return _("%s's profile") % self.user

    def get_genderimage_url(self):
        return GENDER_IMAGES[self.gender]

    def get_absolute_url(self):
        return "/profile/users/%s/" % self.user

    def yearsold(self):
        return (datetime.date.today().toordinal() - self.birthdate.toordinal()) / 365
<|endoftext|>


<commit_before>#!/Users/Yaxin.Cheng@iCloud.com/Developer/venv/bin/python
import re
import requests
import argparse
import webbrowser
import sys
from datetime import datetime

baseURL = 'https://dalonline.dal.ca/PROD/fysktime.P_DisplaySchedule?s_term={term}&s_crn=&s_subj={faculty}&s_numb=&n={page}&s_district=All'
parser = argparse.ArgumentParser(description='Search for the existence of courses')
parser.add_argument('-o', '--open', action='store_true', help='Open the url in the browser')
parser.add_argument('-t', '--term', nargs='*', default='w', help='Term (Available input: winter/fall/w/f; winter by default)')
parser.add_argument('-d', '--digit', nargs='*', help='Course Digit (Precise search only; Cannot search with name at the same time)')
parser.add_argument('-n', '--name', nargs='*', help='Name (Partial search is available; Mobile Computing by default; Cannot search with digit at the same time)')
parser.add_argument('-p', '--page', type=int, help='Page (One specific page needs to be search; All pages by default)')
parser.add_argument('-f', '--faculty', help='Faculty (Shorthand of faculty name; CSCI by default)')
parser.add_argument('-y', '--year', type=int, help='Year of the timetable (Historical data may be unaccessible)')
args = parser.parse_args()

def openURL(baseURL, term, faculty, page):
    page = page[0] if isinstance(page, list) else (page - 1) * 20 + 1
    webbrowser.open(baseURL.format(term=term, faculty=faculty, page=page))
    sys.exit(0)

encodePage = lambda number: int((number - 1) * 20 + 1)
decodePage = lambda number: int((number - 1) / 20 + 1)
encodeYear = lambda year: (year + 1) * 100
decodeYear = lambda year: int(year / 100) - 1

year = encodeYear(args.year or datetime.now().year)
__FALL__, __WINTER__, __SUMMER__ = year + 10, year + 20, year + 30

Page = [encodePage(args.page)] if args.page else [ encodePage(i) for i in range(1, 8) ]
reverseTermMapping = {__FALL__: 'fall', __WINTER__: 'winter', __SUMMER__: 'summer'}
termMapping = {**{value: key for key, value in reverseTermMapping.items()}, 'w': __WINTER__, 'f': __FALL__, 's': __SUMMER__}
Term = [termMapping[args.term.lower()]] if not isinstance(args.term, list) else [termMapping[term.lower()] for term in args.term]
Facu = (args.faculty or 'CSCI').upper()
if args.open:
    openURL(baseURL, Term[0], Facu, Page)
Name = args.name or ['Mobile Computing']
Digit = args.digit
if Digit:
    Name = '.+?'
    Digit = '(' + '|'.join(args.digit) + ')'
else:
    Digit = '[0-9]*?'
    Name = '(' + '|'.join(Name) + ')'

nameRegex = re.compile('{faculty}\s{digit}\s.*?{name}.*?<\/b>'.format(faculty=Facu, name=Name, digit=Digit), re.I)
emptyRegex = re.compile('{faculty}\s[0-9]*?\s.+?<\/b>'.format(faculty=Facu))
clearRegex = re.compile('()|(<\/b>)')
for term in Term:
    print('Searching {name} in {term} term for {faculty} (Year {year})'.format(name=Facu+' '+str(Digit) if Name == '.+?' else Name, term=reverseTermMapping[term], faculty=Facu, year=decodeYear(year)))
    for index, page in enumerate(Page):
        searchURL = baseURL.format(term=term, faculty=Facu, page=page)
        source = requests.get(searchURL).text
        if not emptyRegex.search(source): break
        print('Searching on page', index + 1 if len(Page) > 1 else decodePage(page), ':')
        for course in nameRegex.finditer(source):
            print(clearRegex.sub('', course.group()))
    print()
<commit_msg>Advanced search with more information<commit_after>#!/Users/Yaxin.Cheng@iCloud.com/Developer/venv/bin/python
import re
import requests
import argparse
import webbrowser
import sys
from datetime import datetime

baseURL = 'https://dalonline.dal.ca/PROD/fysktime.P_DisplaySchedule?s_term={term}&s_crn=&s_subj={faculty}&s_numb=&n={page}&s_district=All'
parser = argparse.ArgumentParser(description='Search for the existence of courses')
parser.add_argument('-o', '--open', action='store_true', help='Open the url in the browser')
parser.add_argument('-t', '--term', nargs='*', default=['f', 'w'], help='Term (Available input: winter/fall/w/f; winter by default)')
parser.add_argument('-d', '--digit', nargs='*', help='Course Digit (Fuzzy search available (regex); Cannot search with name at the same time)')
parser.add_argument('-n', '--name', nargs='*', help='Name (Partial search is available; Mobile Computing by default; Cannot search with digit at the same time)')
parser.add_argument('-p', '--page', type=int, help='Page (One specific page needs to be search; All pages by default)')
parser.add_argument('-f', '--faculty', help='Faculty (Shorthand of faculty name; CSCI by default)')
parser.add_argument('-s', '--show', action='store_true', help='Show course dates')
parser.add_argument('-y', '--year', type=int, help='Year of the timetable (Historical data may be unaccessible)')
args = parser.parse_args()

def openURL(baseURL, term, faculty, page):
    page = page[0] if isinstance(page, list) else (page - 1) * 20 + 1
    webbrowser.open(baseURL.format(term=term, faculty=faculty, page=page))
    sys.exit(0)

encodePage = lambda number: int((number - 1) * 20 + 1)
decodePage = lambda number: int((number - 1) / 20 + 1)
encodeYear = lambda year: (year + 1) * 100
decodeYear = lambda year: int(year / 100) - 1

year = encodeYear(args.year or datetime.now().year)
__FALL__, __WINTER__, __SUMMER__ = year + 10, year + 20, year + 30

Page = [encodePage(args.page)] if args.page else [ encodePage(i) for i in range(1, 8) ]
reverseTermMapping = {__FALL__: 'fall', __WINTER__: 'winter', __SUMMER__: 'summer'}
termMapping = {**{value: key for key, value in reverseTermMapping.items()}, 'w': __WINTER__, 'f': __FALL__, 's': __SUMMER__}
Term = [termMapping[args.term.lower()]] if not isinstance(args.term, list) else [termMapping[term.lower()] for term in args.term]
Facu = (args.faculty or 'CSCI').upper()
if args.open:
    openURL(baseURL, Term[0], Facu, Page)
Name = args.name or ['Mobile Computing']
Digit = args.digit
if Digit:
    Name = '.+?'
    Digit = '(' + '|'.join(args.digit) + ')'
else:
    Digit = '[0-9]*?'
    Name = '(' + '|'.join(Name) + ')'

crnRegex  = re.compile('\d{5}<\/b>')
coreRegex = re.compile('^(.|\s)*?<tr.*valign=', re.M)
nameRegex = re.compile('{faculty}\s{digit}\s.*?{name}.*?<\/b>'.format(faculty=Facu, name=Name, digit=Digit), re.I|re.M)
typeRegex = re.compile('(Lec|Lab|Tut|WkT|Ths)')
timeRegex = re.compile('[0-9]{4}\-[0-9]{4}')
weekRegex = re.compile('
[MTWRF]<\/p>')
dateRegex = re.compile('\d{2}\-\w{3}\-\d{4}\s\-\s\d{2}\-\w{3}\-\d{4}')
percRegex = re.compile('(\d{1,2}\.\d{1,2}\%)|(WLIST)|(FULL)')
emptyRegex = re.compile('{faculty}\s[0-9]*?\s.+?<\/b>'.format(faculty=Facu))
clearRegex = re.compile('()|(<\/b>)')
splitRegex = re.compile('<\/tr>\s*?
')

for term in Term:
    print('Searching {name} in {term} term for {faculty} (Year {year})\n'.format(name=Facu+' '+str(Digit) if Name == '.+?' else Name, term=reverseTermMapping[term], faculty=Facu, year=decodeYear(year)))
    print('=' * 50)
    for index, page in enumerate(Page):
        searchURL = baseURL.format(term=term, faculty=Facu, page=page)
        source = requests.get(searchURL).text
        if not emptyRegex.search(source): break
        for course in coreRegex.finditer(source):
            components = splitRegex.split(course.group())
            try:
                name = nameRegex.search(components[0]).group()
                summary = "Name: " + clearRegex.sub('', name) + '\n'
            except:
                continue
            if args.show: summary += dateRegex.search(components[0]) + '\n'
            print(summary)
            print('\t'.join(['CRN', 'Type', 'Weeks', 'Time', 'Percentage']))
            for detail in components[1:]:
                try:
                    crn = crnRegex.search(detail).group().strip('')
                except: continue
                ctype = typeRegex.search(detail).group()
                weeks = '|'.join(map(lambda week: week.strip('

'), weekRegex.findall(detail)))
                time = timeRegex.search(detail).group()
                percent = percRegex.search(detail).group()
                print('\t'.join([crn, ctype, weeks, time, percent]))
            print('=' * 50)
    print()
<|endoftext|>


<commit_before>#!/usr/bin/python

import os
import time
import pymongo
import json
import sys
import optparse
import codecs

def main():
	usage = "usage: %prog [options]"
	parser = optparse.OptionParser(usage)
	parser.add_option("-t", "--test", dest="testing", action="store_true", help="Enable testing mode")
	parser.add_option("-u", "--user", dest="user_name", action="store", type="string", help="username")
	parser.add_option("-r", "--repo", dest="repo_name", action="store", type="string", help="repo name")
	parser.add_option("--geocode", action="store_true", dest="geocode")
	parser.add_option("--user-search", action="store_true", dest="user_search")
	parser.add_option("--commits", action="store_true", dest="commits")
	parser.add_option("--repos-show", action="store_true", dest="repos_show")

	options, args = parser.parse_args()

	conn = pymongo.Connection()

	db = conn.raw
	queue = conn.queue
	if options.testing:
		db = conn.rawtest
		queue = conn.queuetest

	char_stream = codecs.getreader("utf-8")(sys.stdin)
	data = char_stream.read()
	obj = json.loads(data)
	if options.geocode and options.user_name:
		if obj["ResultSet"]["Found"] == 1:
			existing = db.users.find_one({"name" : options.user_name})
			if not existing:
				id = db.users.insert({"name" : options.user_name, "geo" : obj["ResultSet"]["Results"][0]})
				queue.users.insert({"id" : id})
			else:
				db.users.update({"name":options.user_name}, {"$set" : {"geo" : obj["ResultSet"]["Results"][0]}})
				queue.users.insert({"id" : existing["_id"]})
	elif options.user_search:
		for user in obj["users"]:
			existing = db.users.find_one({"name" : user["name"]})
			if not existing:
				id = db.users.insert(user)
				queue.users.insert({"id" : id})
	elif options.commits:
		cmts = obj["commits"]
		for cmt in cmts:
			existing = db.commits.find_one({"id" : cmt["id"]})
			if not existing:
				id = db.commits.insert(cmt)
				queue.commits.insert({"id" : id})
	elif options.repos_show and options.repo_name and options.user_name:
		key = obj.keys()[0]
		for sub_key in obj[key].keys():
			clean_key = sub_key.replace(".", "_")
			if clean_key != sub_key:
				obj[key][clean_key] = obj[key][sub_key]
				del obj[key][sub_key]
		existing = db.repos.find_one({"name" : options.repo_name, "owner" : options.user_name})
		if existing:
			db.repos.update({"name": options.repo_name, "owner" : options.user_name}, {"$set" : {key : obj[key]}})
			queue.repos.insert({"id" : existing["_id"]})
		else:
			id = db.repos.insert({"name": options.repo_name, "owner" : options.user_name, key : obj[key]})
			queue.repos.insert({"id" : id})

if __name__ == '__main__':
	main()

<commit_msg>added repo_l and repo_n to crawl2mongo<commit_after>#!/usr/bin/python

import os
import time
import pymongo
import json
import sys
import optparse
import codecs

def main():
	usage = "usage: %prog [options]"
	parser = optparse.OptionParser(usage)
	parser.add_option("-t", "--test", dest="testing", action="store_true", help="Enable testing mode")
	parser.add_option("-u", "--user", dest="user_name", action="store", type="string", help="username")
	parser.add_option("-r", "--repo", dest="repo_name", action="store", type="string", help="repo name")
	parser.add_option("--geocode", action="store_true", dest="geocode")
	parser.add_option("--user-search", action="store_true", dest="user_search")
	parser.add_option("--commits", action="store_true", dest="commits")
	parser.add_option("--repos-show", action="store_true", dest="repos_show")

	options, args = parser.parse_args()

	conn = pymongo.Connection()

	db = conn.raw
	queue = conn.queue
	if options.testing:
		db = conn.rawtest
		queue = conn.queuetest

	char_stream = codecs.getreader("utf-8")(sys.stdin)
	data = char_stream.read()
	obj = json.loads(data)
	if options.geocode and options.user_name:
		if obj["ResultSet"]["Found"] == 1:
			existing = db.users.find_one({"name" : options.user_name})
			if not existing:
				id = db.users.insert({"name" : options.user_name, "geo" : obj["ResultSet"]["Results"][0]})
				queue.users.insert({"id" : id})
			else:
				db.users.update({"name":options.user_name}, {"$set" : {"geo" : obj["ResultSet"]["Results"][0]}})
				queue.users.insert({"id" : existing["_id"]})
	elif options.user_search:
		for user in obj["users"]:
			existing = db.users.find_one({"name" : user["name"]})
			if not existing:
				id = db.users.insert(user)
				queue.users.insert({"id" : id})
	elif options.commits and options.repo_name and options.user_name
		cmts = obj["commits"]
		for cmt in cmts:
			existing = db.commits.find_one({"id" : cmt["id"]})
			long_name = options.user_name + '/' + options.repo_name
			if not existing:
				cmt["repo_n"] = [options.repo_name]
				cmt["repo_l"] = [long_name]
				id = db.commits.insert(cmt)
				queue.commits.insert({"id" : id})
			else:
				changed = False
				if cmt.has_key("repo_n"):
					if not (options.repo_name in cmt["repo_n"]):
						cmt["repo_n"].append(options.repo_name)
						changed = True
				else:
					cmt["repo_n"] = [options.repo_name]
					changed = True
				if cmt.has_key("repo_l"):
					if not (long_name in cmt["repo_l"]):
						cmt["repo_l"].append(long_name)
						changed = True
				else:
					cmt["repo_l"] = [long_name]
					changed = True
				if changed:
					db.commits.update({"id": cmt["id"]}, {"$set" : {"repo_l" : cmt["repo_l"], "repo_n" : cmt["repo_n"]}})
					queue.commits.insert({"id": existing["_id"]})
	elif options.repos_show and options.repo_name and options.user_name:
		key = obj.keys()[0]
		for sub_key in obj[key].keys():
			clean_key = sub_key.replace(".", "_")
			if clean_key != sub_key:
				obj[key][clean_key] = obj[key][sub_key]
				del obj[key][sub_key]
		existing = db.repos.find_one({"name" : options.repo_name, "owner" : options.user_name})
		if existing:
			db.repos.update({"name": options.repo_name, "owner" : options.user_name}, {"$set" : {key : obj[key]}})
			queue.repos.insert({"id" : existing["_id"]})
		else:
			id = db.repos.insert({"name": options.repo_name, "owner" : options.user_name, key : obj[key]})
			queue.repos.insert({"id" : id})

if __name__ == '__main__':
	main()

<|endoftext|>


<commit_before>from i3pystatus.file import File
from i3pystatus import Module
from i3pystatus.core.command import run_through_shell
import shutil

class Backlight(File):
    """
    Screen backlight info

    - (Optional) requires `xbacklight` to change the backlight brightness with the scollwheel.

    .. rubric:: Available formatters

    * `{brightness}` — current brightness relative to max_brightness
    * `{max_brightness}` — maximum brightness value
    * `{percentage}` — current brightness in percent
    """

    settings = (
        ("format", "format string, formatters: brightness, max_brightness, percentage"),
        ("backlight", "backlight, see `/sys/class/backlight/`"),
        "color",
    )
    required = ()

    backlight = "acpi_video0"
    format = "{brightness}/{max_brightness}"

    base_path = "/sys/class/backlight/{backlight}/"
    components = {
        "brightness": (int, "brightness"),
        "max_brightness": (int, "max_brightness"),
    }
    transforms = {
        "percentage": lambda cdict: round((cdict["brightness"] / cdict["max_brightness"]) * 100),
    }
    on_upscroll = "lighter"
    on_downscroll = "darker"

    def init(self):
        self.base_path = self.base_path.format(backlight=self.backlight)
        self.has_xbacklight = shutil.which("xbacklight") is not None

        # xbacklight expects a percentage as parameter. Calculate the percentage
        # for one step (if smaller xbacklight doesn't increases the brightness)
        if self.has_xbacklight:
            parsefunc = self.components['max_brightness'][0]
            maxbfile = self.components['max_brightness'][1]
            with open(self.base_path + maxbfile, "r") as f:
                max_steps = parsefunc(f.read().strip())
                if max_steps:
                    self.step_size = 100 // max_steps + 1
                else:
                    self.step_size = 5  # default?
        super().init()

    def lighter(self):
        if self.has_xbacklight:
            run_through_shell(["xbacklight", "-inc", str(self.step_size)])

    def darker(self):
        if self.has_xbacklight:
            run_through_shell(["xbacklight", "-dec", str(self.step_size)])
<commit_msg>Glob expansion in backlight<commit_after>from i3pystatus.file import File
from i3pystatus import Module
from i3pystatus.core.command import run_through_shell
import glob
import shutil

class Backlight(File):
    """
    Screen backlight info

    - (Optional) requires `xbacklight` to change the backlight brightness with the scollwheel.

    .. rubric:: Available formatters

    * `{brightness}` — current brightness relative to max_brightness
    * `{max_brightness}` — maximum brightness value
    * `{percentage}` — current brightness in percent
    """

    settings = (
        ("format", "format string, formatters: brightness, max_brightness, percentage"),
        ("backlight",
            "backlight, see `/sys/class/backlight/`. Supports glob expansion, i.e. `*` matches anything. "
            "If it matches more than one filename, selects the first one in alphabetical order"),
        "color",
    )
    required = ()

    backlight = "*"
    format = "{brightness}/{max_brightness}"

    base_path = "/sys/class/backlight/{backlight}/"
    components = {
        "brightness": (int, "brightness"),
        "max_brightness": (int, "max_brightness"),
    }
    transforms = {
        "percentage": lambda cdict: round((cdict["brightness"] / cdict["max_brightness"]) * 100),
    }
    on_upscroll = "lighter"
    on_downscroll = "darker"

    def init(self):
        self.base_path = self.base_path.format(backlight=self.backlight)
        self.base_path = sorted(glob.glob(self.base_path))[0]
        self.has_xbacklight = shutil.which("xbacklight") is not None

        # xbacklight expects a percentage as parameter. Calculate the percentage
        # for one step (if smaller xbacklight doesn't increases the brightness)
        if self.has_xbacklight:
            parsefunc = self.components['max_brightness'][0]
            maxbfile = self.components['max_brightness'][1]
            with open(self.base_path + maxbfile, "r") as f:
                max_steps = parsefunc(f.read().strip())
                if max_steps:
                    self.step_size = 100 // max_steps + 1
                else:
                    self.step_size = 5  # default?
        super().init()

    def lighter(self):
        if self.has_xbacklight:
            run_through_shell(["xbacklight", "-inc", str(self.step_size)])

    def darker(self):
        if self.has_xbacklight:
            run_through_shell(["xbacklight", "-dec", str(self.step_size)])
<|endoftext|>