<commit_before>import os
import PIL.Image
import multiprocessing
import psutil
import signal
import subprocess
import sys
import time

s0Settings = {
    'CameraTheta(rad)': 0,
    'CameraPhi(rad)': 0,
    'CameraPsi(rad)': 0,
    'Type': 'Plane',  # Cutaway
}
OrbLet = 'spdfghiklmnoqrtuvwxyz'

def generate_orbital(opts, n, l, m):
    """
    Generate an orbital, creating a PNG file.
    Enter: opts: a dictionary of options.
           n, l, m: orbital parameters.
    """
    starttime = time.time()
    size = opts['size']
    anti = max(1, opts.get('anti', 1))
    # These two equations were determined empirically to ensure that the s0
    # orbitals of different values of n would have all phases visible and the
    # image would frame the entire orbital.
    psi2 = -11.55 * n / (6.874 + n)
    cz = (39.5 * n ** 2.109 + 20) * 1e-10
    param = {
        'n': '%d' % n,
        'l': '%d' % l,
        'm': '%d' % m,
        'Psi^2(log10)': '%4.2f' % psi2,
        'CameraCenterZ(m)': '%20g' % cz,
        'CameraCx': '%d' % (size * anti / 2),
        'Scale(m)': '%20g' % (cz / 25),
        'FixedSize': 'Yes',
        'FixedWidth': '%d' % (size * anti),
        'FixedHeight': '%d' % (size * anti),
        'BackgroundColor': '0xFFFFFF',
    }
    if n > 1 and not l and not m:
        param.update(s0Settings)
    name = '%d%s%d' % (n, OrbLet[l] if l < len(OrbLet) else '_%d_' % l, m)
    dest = os.path.join(opts['dest'], name + '.png')
    description = '%10s %4.2f %g' % (name, psi2, cz * 1e10)
    if not opts['multi']:
        sys.stdout.write(description)
        sys.stdout.flush()
    # Create two orbital specification files by modifying the template.  The
    # two only differ by background color; this allows opacity to be extracted,
    # even though it is not explicitly saved.
    temp1 = os.path.join(opts['dest'], 'temp%sw.orb' % name)
    temp2 = os.path.join(opts['dest'], 'temp%sb.orb' % name)
    orb = update_orb_spec(opts['base'], param)
    open(temp1, 'wb').write(orb)
    proc1 = subprocess.Popen([opts['exe'], temp1], stdout=subprocess.PIPE)
    param['BackgroundColor'] = '0x000000'
    orb = update_orb_spec(opts['base'], param)
    open(temp2, 'wb').write(orb)
    proc2 = subprocess.Popen([opts['exe'], temp2], stdout=subprocess.PIPE)
    # We have to parse P3 images, as PIL doesn't support them.
    white = [float(val) for val in proc1.stdout.read().split()[4:]]
    black = [float(val) for val in proc2.stdout.read().split()[4:]]
    os.unlink(temp1)
    os.unlink(temp2)
    png = []
    ranti2 = 1.0 / (anti * anti)
    for h in xrange(size):
        for w in xrange(size):
            r = g = b = a = 0
            for y in xrange(anti):
                for x in xrange(anti):
                    i = ((h * anti + y) * size * anti + w * anti + x) * 3
                    al = 255 - (
                        white[i] - black[i] +
                        white[i + 1] - black[i + 1] +
                        white[i + 2] - black[i + 2]) / 3
                    if 0 < al < 255:
                        alpha = al / 255
                        r += black[i] / alpha
                        g += black[i + 1] / alpha
                        b += black[i + 2] / alpha
                        a += al
                    else:
                        r += black[i]
                        g += black[i + 1]
                        b += black[i + 2]
                        a += 255 if al else 0
            png.append((
                max(0, min(255, int(r * ranti2))),
                max(0, min(255, int(g * ranti2))),
                max(0, min(255, int(b * ranti2))),
                max(0, min(255, int(a * ranti2)))))
    img = PIL.Image.new('RGBA', (size, size))
    img.putdata(png)
    if os.path.exists(dest):
        try:
            os.unlink(dest)
        except Exception:
            pass
    img.save(dest, 'PNG', optimize=True)
    # Further optimize the png if optipng is available
    try:
        subprocess.call(['optipng', '-clobber', '-quiet', dest])
    except Exception:
        pass
    if opts['multi']:
        sys.stdout.write(description)
    filesize = os.path.getsize(dest)
    sys.stdout.write(' %d %3.1fs\n' % (filesize, time.time() - starttime))
    sys.stdout.flush()

def generate_table(opts):
    """
    Generate all orbitals in our range.
    Enter: opts: a dictionary of options.  Modified.
    """
    try:
        pool = None
        if opts['multi']:
            pool = multiprocessing.Pool(
                initializer=worker_init,
                processes=multiprocessing.cpu_count() / 2
                if opts['multi'] is True else opts['multi'])
            priorityLevel = (psutil.BELOW_NORMAL_PRIORITY_CLASS
                             if sys.platform == 'win32' else 10)
            parent = psutil.Process()
            parent.nice(priorityLevel)
            for child in parent.children():
                child.nice(priorityLevel)
            tasks = []
        for n in xrange(opts['minn'], opts['maxn'] + 1):
            for l in xrange(0, n):
                for m in xrange(-l if opts['allm'] else 0, l + 1):
                    if pool:
                        tasks.append(pool.apply_async(generate_orbital, args=(opts, n, l, m)))
                    else:
                        generate_orbital(opts, n, l, m)
        if pool:
            pool.close()
            for task in tasks:
                task.get()
            pool.join()
    except KeyboardInterrupt:
        if pool:
            try:
                pool.terminate()
                pool.join()
            except Exception:
                pass

def prep_table(opts):
    """
    Load the default orbital.
    Enter: opts: a dictionary of options.  Modified.
    """
    opts['base'] = open(opts['orbfile']).read()

def update_orb_spec(spec, param):
    """
    Update an orbital specification.
    Enter: spec: the text specification.
           param: a dictionary of parameters to update.
    """
    lines = [line for line in spec.replace('\r', '\n').split('\n') if line.strip()]
    out = []
    for line in lines:
        parts = line.split('#')[0].strip().split()
        if len(parts) == 2 and parts[0] in param:
            line = line[:line.index(parts[0]) + len(parts[0]) + 1] + str(param[parts[0]])
        out.append(line)
    out.append('')
    return '\n'.join(out)

def worker_init():
    """
    Supress the ctrl-c signal in the worker processes.
    """
    signal.signal(signal.SIGINT, signal.SIG_IGN)

if __name__ == '__main__':  # noqa
    opts = {
        'orbfile': 'table.orb',
        'size': 128,
        'dest': 'table',
        'minn': 1,
        'maxn': 10,
        'allm': False,
        'multi': False,
        'exe': 'ansiorb.exe',
    }
    help = False
    for arg in sys.argv[1:]:
        if arg == '--allm':
            opts['allm'] = True
        elif arg.startswith('--anti='):
            opts['anti'] = int(arg.split('=', 1)[1])
        elif arg.startswith('--dest='):
            opts['dest'] = arg.split('=', 1)[1]
        elif arg.startswith('--exe='):
            opts['exe'] = arg.split('=', 1)[1]
        elif arg.startswith('--maxn='):
            opts['maxn'] = int(arg.split('=', 1)[1])
        elif arg.startswith('--minn='):
            opts['minn'] = int(arg.split('=', 1)[1])
        elif arg == '--multi':
            opts['multi'] = True
        elif arg.startswith('--multi='):
            opts['multi'] = int(arg.split('=', 1)[1])
            if opts['multi'] < 2:
                opts['multi'] = False
        elif arg.startswith('--orb='):
            opts['orbfile'] = arg.split('=', 1)[1]
        elif arg == '--posm':
            opts['allm'] = False
        elif arg.startswith('--size='):
            opts['size'] = int(arg.split('=', 1)[1])
        else:
            help = True
    if help:
        print """Generate a table of orbitals.

Syntax: table.py --anti=(antialiasing factor) --dest=(directory)
    --exe=(ansiorb executable) --minn=(n) --maxn=(n) --orb=(orbital file)
    --size=(pixels) --allm|--posm --multi

--allm calculates positive and negative p values.
--anti is an integer >= 1.  This is an oversampling, which then scales down
    using a PIL function, which may not look any better than the native
    antialiasing.
--dest specifies a destination directory.
--exe specifies the path to the ansiorb.exe executable.
--minn is the minimum n value to calculate.
--maxn is the maximum n value to calculate.
--multi uses multiprocessing to parallelize computations.
--orb is the path of a default orbital to use as a basis for calculation.
--posm calculates only non-negative p values (the is the default).
--size is the output size in pixels.
"""
        sys.exit(0)
    prep_table(opts)
    generate_table(opts)
<commit_msg>Use less memory.<commit_after>import os
import PIL.Image
import multiprocessing
import psutil
import signal
import subprocess
import sys
import time

s0Settings = {
    'CameraTheta(rad)': 0,
    'CameraPhi(rad)': 0,
    'CameraPsi(rad)': 0,
    'Type': 'Plane',  # Cutaway
}
OrbLet = 'spdfghiklmnoqrtuvwxyz'

def generate_orbital(opts, n, l, m):
    """
    Generate an orbital, creating a PNG file.
    Enter: opts: a dictionary of options.
           n, l, m: orbital parameters.
    """
    starttime = time.time()
    size = opts['size']
    anti = max(1, opts.get('anti', 1))
    # These two equations were determined empirically to ensure that the s0
    # orbitals of different values of n would have all phases visible and the
    # image would frame the entire orbital.
    psi2 = -11.55 * n / (6.874 + n)
    cz = (39.5 * n ** 2.109 + 20) * 1e-10
    param = {
        'n': '%d' % n,
        'l': '%d' % l,
        'm': '%d' % m,
        'Psi^2(log10)': '%4.2f' % psi2,
        'CameraCenterZ(m)': '%20g' % cz,
        'CameraCx': '%d' % (size * anti / 2),
        'Scale(m)': '%20g' % (cz / 25),
        'FixedSize': 'Yes',
        'FixedWidth': '%d' % (size * anti),
        'FixedHeight': '%d' % (size * anti),
        'BackgroundColor': '0xFFFFFF',
    }
    if n > 1 and not l and not m:
        param.update(s0Settings)
    name = '%d%s%d' % (n, OrbLet[l] if l < len(OrbLet) else '_%d_' % l, m)
    dest = os.path.join(opts['dest'], name + '.png')
    description = '%10s %4.2f %g' % (name, psi2, cz * 1e10)
    if not opts['multi']:
        sys.stdout.write(description)
        sys.stdout.flush()
    # Create two orbital specification files by modifying the template.  The
    # two only differ by background color; this allows opacity to be extracted,
    # even though it is not explicitly saved.
    temp1 = os.path.join(opts['dest'], 'temp%sw.orb' % name)
    temp2 = os.path.join(opts['dest'], 'temp%sb.orb' % name)
    orb = update_orb_spec(opts['base'], param)
    open(temp1, 'wb').write(orb)
    proc1 = subprocess.Popen([opts['exe'], temp1], stdout=subprocess.PIPE)
    param['BackgroundColor'] = '0x000000'
    orb = update_orb_spec(opts['base'], param)
    open(temp2, 'wb').write(orb)
    proc2 = subprocess.Popen([opts['exe'], temp2], stdout=subprocess.PIPE)
    # We have to parse P3 images, as PIL doesn't support them.
    data1 = {}
    data2 = {}
    white = [read_to_whitespace(proc1.stdout, data1) for x in xrange(4)]
    black = [read_to_whitespace(proc2.stdout, data2) for x in xrange(4)]
    png = []
    ranti2 = 1.0 / (anti * anti)
    for h in xrange(size):
        white = [float(read_to_whitespace(proc1.stdout, data1))
                 for w in xrange(size * anti * anti * 3)]
        black = [float(read_to_whitespace(proc2.stdout, data2))
                 for w in xrange(size * anti * anti * 3)]
        for w in xrange(size):
            r = g = b = a = 0
            for y in xrange(anti):
                for x in xrange(anti):
                    i = ((y) * size * anti + w * anti + x) * 3
                    al = 255 - (
                        white[i] - black[i] +
                        white[i + 1] - black[i + 1] +
                        white[i + 2] - black[i + 2]) / 3
                    if 0 < al < 255:
                        alpha = al / 255
                        r += black[i] / alpha
                        g += black[i + 1] / alpha
                        b += black[i + 2] / alpha
                        a += al
                    else:
                        r += black[i]
                        g += black[i + 1]
                        b += black[i + 2]
                        a += 255 if al else 0
            png.append((
                max(0, min(255, int(r * ranti2))),
                max(0, min(255, int(g * ranti2))),
                max(0, min(255, int(b * ranti2))),
                max(0, min(255, int(a * ranti2)))))
    os.unlink(temp1)
    os.unlink(temp2)
    proc1 = proc2 = None
    img = PIL.Image.new('RGBA', (size, size))
    img.putdata(png)
    if os.path.exists(dest):
        try:
            os.unlink(dest)
        except Exception:
            pass
    img.save(dest, 'PNG', optimize=True)
    # Further optimize the png if optipng is available
    try:
        subprocess.call(['optipng', '-clobber', '-quiet', dest])
    except Exception:
        pass
    if opts['multi']:
        sys.stdout.write(description)
    filesize = os.path.getsize(dest)
    sys.stdout.write(' %d %3.1fs\n' % (filesize, time.time() - starttime))
    sys.stdout.flush()

def generate_table(opts):
    """
    Generate all orbitals in our range.
    Enter: opts: a dictionary of options.  Modified.
    """
    try:
        pool = None
        if opts['multi']:
            pool = multiprocessing.Pool(
                initializer=worker_init,
                processes=multiprocessing.cpu_count() / 2
                if opts['multi'] is True else opts['multi'])
            priorityLevel = (psutil.BELOW_NORMAL_PRIORITY_CLASS
                             if sys.platform == 'win32' else 10)
            parent = psutil.Process()
            parent.nice(priorityLevel)
            for child in parent.children():
                child.nice(priorityLevel)
            tasks = []
        for n in xrange(opts['minn'], opts['maxn'] + 1):
            for l in xrange(0, n):
                for m in xrange(-l if opts['allm'] else 0, l + 1):
                    if pool:
                        tasks.append(pool.apply_async(generate_orbital, args=(opts, n, l, m)))
                    else:
                        generate_orbital(opts, n, l, m)
        if pool:
            pool.close()
            for task in tasks:
                task.get()
            pool.join()
    except KeyboardInterrupt:
        if pool:
            try:
                pool.terminate()
                pool.join()
            except Exception:
                pass

def prep_table(opts):
    """
    Load the default orbital.
    Enter: opts: a dictionary of options.  Modified.
    """
    opts['base'] = open(opts['orbfile']).read()

def read_to_whitespace(fptr, track):
    """
    Read, skipping over white space.  Then read all non-whitespace until one
    character of white space is consumed.  Return the non-whitespace that was
    read.  If the end of the file is encountered, return any non-whitespace if
    available, or an empty bytes string.
    Enter: fptr: file-like object to read from.
           track: a dictionary used to hold temporary values
    Exit:  val: non-whitespace string
    """
    if 'data' in track:
        if track['next'] < len(track['data']) - 1:
            out = track['data'][track['next']]
            track['next'] += 1
            return out
        data = track['data'][-1][:-1]
    else:
        data = b''
    track['data'] = (data + fptr.read(1024 * 1024) + b'X').split()
    out = track['data'][0]
    track['next'] = 1
    return out

def update_orb_spec(spec, param):
    """
    Update an orbital specification.
    Enter: spec: the text specification.
           param: a dictionary of parameters to update.
    """
    lines = [line for line in spec.replace('\r', '\n').split('\n') if line.strip()]
    out = []
    for line in lines:
        parts = line.split('#')[0].strip().split()
        if len(parts) == 2 and parts[0] in param:
            line = line[:line.index(parts[0]) + len(parts[0]) + 1] + str(param[parts[0]])
        out.append(line)
    out.append('')
    return '\n'.join(out)

def worker_init():
    """
    Supress the ctrl-c signal in the worker processes.
    """
    signal.signal(signal.SIGINT, signal.SIG_IGN)

if __name__ == '__main__':  # noqa
    opts = {
        'orbfile': 'table.orb',
        'size': 128,
        'dest': 'table',
        'minn': 1,
        'maxn': 10,
        'allm': False,
        'multi': False,
        'exe': 'ansiorb.exe',
    }
    help = False
    for arg in sys.argv[1:]:
        if arg == '--allm':
            opts['allm'] = True
        elif arg.startswith('--anti='):
            opts['anti'] = int(arg.split('=', 1)[1])
        elif arg.startswith('--dest='):
            opts['dest'] = arg.split('=', 1)[1]
        elif arg.startswith('--exe='):
            opts['exe'] = arg.split('=', 1)[1]
        elif arg.startswith('--maxn='):
            opts['maxn'] = int(arg.split('=', 1)[1])
        elif arg.startswith('--minn='):
            opts['minn'] = int(arg.split('=', 1)[1])
        elif arg == '--multi':
            opts['multi'] = True
        elif arg.startswith('--multi='):
            opts['multi'] = int(arg.split('=', 1)[1])
            if opts['multi'] < 2:
                opts['multi'] = False
        elif arg.startswith('--orb='):
            opts['orbfile'] = arg.split('=', 1)[1]
        elif arg == '--posm':
            opts['allm'] = False
        elif arg.startswith('--size='):
            opts['size'] = int(arg.split('=', 1)[1])
        else:
            help = True
    if help:
        print """Generate a table of orbitals.

Syntax: table.py --anti=(antialiasing factor) --dest=(directory)
    --exe=(ansiorb executable) --minn=(n) --maxn=(n) --orb=(orbital file)
    --size=(pixels) --allm|--posm --multi

--allm calculates positive and negative p values.
--anti is an integer >= 1.  This is an oversampling, which then scales down
    using a PIL function, which may not look any better than the native
    antialiasing.
--dest specifies a destination directory.
--exe specifies the path to the ansiorb.exe executable.
--minn is the minimum n value to calculate.
--maxn is the maximum n value to calculate.
--multi uses multiprocessing to parallelize computations.
--orb is the path of a default orbital to use as a basis for calculation.
--posm calculates only non-negative p values (the is the default).
--size is the output size in pixels.
"""
        sys.exit(0)
    prep_table(opts)
    generate_table(opts)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Manage the chatting part of the Eqs Coffee Machine.

Currently integrated with Slack.

Released under MIT License. See LICENSE file.
"""
from slackclient import SlackClient
# Add your Slack API token in a token.py file:
# SLACK_TOKEN = 'YOUR_SLACK_API_TOKEN'
from token import SLACK_TOKEN
import json

# User experience constants.
USER_COFFEE = '@coffee'
CMD_COFFEE = ['coffee', u'café', 'court', 'short', 'long']
COFFEE_SHORT = ['short', 'court']
COFFEE_LONG = ['long']
FILE_PROCESSED = 'processed.json'
PROCESSED_MESSAGES = []
try:
	with open(FILE_PROCESSED) as f:
		PROCESSED_MESSAGES = json.load(f)
except IOError:
	pass
FILE_STATS = 'stats.json'
COFFEE_STATS = {}
try:
	with open(FILE_STATS) as f:
		COFFEE_STATS = json.load(f)
except IOError:
	pass

# Connect to Slack.
slack = SlackClient(SLACK_TOKEN)
actions = []
if not slack.rtm_connect():
	raise RuntimeError('Invalid token')

class Chat(object):
	@staticmethod
	def talk(message, channel='#hackathon_eqs'):
		slack.api_call(
			"chat.postMessage", channel=channel, text=message,
			username='Eqs Coffee Machine', icon_emoji=':coffee:'
		)

	@staticmethod
	def addCoffeeForUser(user, short_long, channel='#hackathon_eqs'):
		# Init the dict.
		if 'short' not in COFFEE_STATS:
			COFFEE_STATS['short'] = {}
			COFFEE_STATS['short']['total'] = 0
		if 'long' not in COFFEE_STATS:
			COFFEE_STATS['long'] = {}
			COFFEE_STATS['long']['total'] = 0
		if user not in COFFEE_STATS:
			COFFEE_STATS[user] = {}
			COFFEE_STATS[user]['short'] = 0
			COFFEE_STATS[user]['long'] = 0
			Chat.talk("First coffee for @{0}! Thumbs up from the HackEQS team! :+1:"
				.format(user), channel=channel)
		# Update the dict.
		COFFEE_STATS[short_long]['total'] = COFFEE_STATS[short_long]['total'] + 1
		COFFEE_STATS[user][short_long] = COFFEE_STATS[user][short_long] + 1
		if COFFEE_STATS[user][short_long] % 10 == 0:
			Chat.talk("Woow, that's your {0}th {1} coffee!".
				format(COFFEE_STATS[user][short_long], short_long), channel=channel)
		with open(FILE_STATS, 'wb') as f:
			json.dump(COFFEE_STATS, f)

	@staticmethod
	def setMessageAsProcessed(messageId):
		PROCESSED_MESSAGES.append(messageId)
		with open(FILE_PROCESSED, 'wb') as f:
			json.dump(PROCESSED_MESSAGES, f)

	@staticmethod
	def isShortInMessage(message):
		return any(a in message for a in COFFEE_SHORT)

	@staticmethod
	def isLongInMessage(message):
		return any(a in message for a in COFFEE_LONG)

	@staticmethod
	def getNotifications():
		return slack.rtm_read()

	@staticmethod
	def processNotification(notification):
		if notification['type'] != 'message' or 'text' not in notification:
			return
		message = notification['text'].lower()
		if not USER_COFFEE in message or 'user' not in notification:
			return
		print(notification)
		if notification['ts'] in PROCESSED_MESSAGES:
			print('Already processed, ignore')
			return
		Chat.setMessageAsProcessed(notification['ts'])
		user = slack.api_call('users.info', user=notification['user']).get('user')
		if not any(a in message.replace(USER_COFFEE, '') for a in CMD_COFFEE):
			Chat.talk('Yes @{0}, can I help you?'.format(user['name']),
				channel=notification['channel'])
			return
		if not Chat.isShortInMessage(message) and not Chat.isLongInMessage(message):
			Chat.talk('Here I am! Which kind of coffee do you want @{0}?'
				.format(user['name']), channel=notification['channel'])
			return
		isShort = Chat.isShortInMessage(message)
		actions.append({
			'type': 'brewAction',
			'notification': notification,
			'user': user,
			'isShort': isShort })

	@staticmethod
	def getNextAction():
		if len(actions) == 0:
			return None
		return actions.pop(0)
<commit_msg>added security : is disabled the weekend, between 8pm 8am and manually<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Manage the chatting part of the Eqs Coffee Machine.

Currently integrated with Slack.

Released under MIT License. See LICENSE file.
"""
from slackclient import SlackClient
# Add your Slack API token in a token.py file:
# SLACK_TOKEN = 'YOUR_SLACK_API_TOKEN'
from token import SLACK_TOKEN
import json
import datetime

# User experience constants.
USER_COFFEE = '@coffee'
CMD_COFFEE = ['coffee', u'café', 'court', 'short', 'long']
CMD_DISABLE = ['disable', 'sleep']
CMD_ENABLE = ['enable', 'wake_up']
COFFEE_SHORT = ['short', 'court']
COFFEE_LONG = ['long']
FILE_PROCESSED = 'processed.json'
PROCESSED_MESSAGES = []
IS_DISABLED = False
try:
	with open(FILE_PROCESSED) as f:
		PROCESSED_MESSAGES = json.load(f)
except IOError:
	pass
FILE_STATS = 'stats.json'
COFFEE_STATS = {}
try:
	with open(FILE_STATS) as f:
		COFFEE_STATS = json.load(f)
except IOError:
	pass

# Connect to Slack.
slack = SlackClient(SLACK_TOKEN)
actions = []
if not slack.rtm_connect():
	raise RuntimeError('Invalid token')

class Chat(object):
	@staticmethod
	def talk(message, channel='#hackathon_eqs'):
		slack.api_call(
			"chat.postMessage", channel=channel, text=message,
			username='Eqs Coffee Machine', icon_emoji=':coffee:'
		)

	@staticmethod
	def addCoffeeForUser(user, short_long, channel='#hackathon_eqs'):
		# Init the dict.
		if 'short' not in COFFEE_STATS:
			COFFEE_STATS['short'] = {}
			COFFEE_STATS['short']['total'] = 0
		if 'long' not in COFFEE_STATS:
			COFFEE_STATS['long'] = {}
			COFFEE_STATS['long']['total'] = 0
		if user not in COFFEE_STATS:
			COFFEE_STATS[user] = {}
			COFFEE_STATS[user]['short'] = 0
			COFFEE_STATS[user]['long'] = 0
			Chat.talk("First coffee for @{0}! Thumbs up from the HackEQS team! :+1:"
				.format(user), channel=channel)
		# Update the dict.
		COFFEE_STATS[short_long]['total'] = COFFEE_STATS[short_long]['total'] + 1
		COFFEE_STATS[user][short_long] = COFFEE_STATS[user][short_long] + 1
		if COFFEE_STATS[user][short_long] % 10 == 0:
			Chat.talk("Woow, that's your {0}th {1} coffee!".
				format(COFFEE_STATS[user][short_long], short_long), channel=channel)
		with open(FILE_STATS, 'wb') as f:
			json.dump(COFFEE_STATS, f)

	@staticmethod
	def setMessageAsProcessed(messageId):
		PROCESSED_MESSAGES.append(messageId)
		with open(FILE_PROCESSED, 'wb') as f:
			json.dump(PROCESSED_MESSAGES, f)

	@staticmethod
	def isShortInMessage(message):
		return any(a in message for a in COFFEE_SHORT)

	@staticmethod
	def isLongInMessage(message):
		return any(a in message for a in COFFEE_LONG)

	@staticmethod
	def getNotifications():
		return slack.rtm_read()

	@staticmethod
	def processNotification(notification):
		global IS_DISABLED
		if notification['type'] != 'message' or 'text' not in notification:
			return
		message = notification['text'].lower()
		if not USER_COFFEE in message or 'user' not in notification:
			return
		print(notification)
		if notification['ts'] in PROCESSED_MESSAGES:
			print('Already processed, ignore')
			return
		Chat.setMessageAsProcessed(notification['ts'])
		user = slack.api_call('users.info', user=notification['user']).get('user')
		if any(a in message for a in CMD_ENABLE):
			print "enable"
			IS_DISABLED = False
			Chat.talk('Hey Hey Hey! I missed you so much!',
				channel=notification['channel'])
			return

		if any(a in message for a in CMD_DISABLE):
			IS_DISABLED = True
			Chat.talk('Alright! I think it is time to sleep...',
				channel=notification['channel'])
			return

		if datetime.datetime.today().weekday() > 5 or datetime.datetime.today().hour > 20 or datetime.datetime.today().hour < 8 or IS_DISABLED:
			Chat.talk('I am on holiday :grin: Go ahead and prepare it by yourself!',
				channel=notification['channel'])
			return
		if not any(a in message.replace(USER_COFFEE, '') for a in CMD_COFFEE):
			Chat.talk('Yes @{0}, can I help you?'.format(user['name']),
				channel=notification['channel'])
			return
		if not Chat.isShortInMessage(message) and not Chat.isLongInMessage(message):
			Chat.talk('Here I am! Which kind of coffee do you want @{0}?'
				.format(user['name']), channel=notification['channel'])
			return
		isShort = Chat.isShortInMessage(message)
		actions.append({
			'type': 'brewAction',
			'notification': notification,
			'user': user,
			'isShort': isShort })

	@staticmethod
	def getNextAction():
		if len(actions) == 0:
			return None
		return actions.pop(0)
<|endoftext|>


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

"""This script scrapes all the info about the adsu of the university's city."""

import logging
import json
import configparser
import feedparser

def get_configuration():
    """Get global configuration from service.cfg"""

    config = configparser.ConfigParser()
    config.read("service.cfg")

    return config

def get_logger(debug):
    """Get logger object"""

    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO
        )
    logger = logging.getLogger(__name__)

    if debug is False:
        logging.disable(logging.CRITICAL)

    return logger

def write_json(data, json_file):
    """General function used everywhere to write data into a json file"""

    with open(json_file, "w") as json_file:
        json.dump(data, json_file)

def read_json(json_file):
    """This function read news locally stored into the json file"""

    with open(json_file, "r") as json_file:
        return json.load(json_file)

def pull_news():
    """This function is built to pull all the news from the rss endpoint"""

    document = feedparser.parse(
        "http://www.disim.univaq.it/didattica/content.php?fid=rss&pid=114&did=8&lid=it"
        )
    news = [
        {"title": item.title, "description": item.description.replace("&#39;", "'")}
        for item in document["entries"][:10]
        ]
    write_json(news, "json/news.json")
    return news

def check_news():
    """This function check if there is some unread news from the website"""

    pulled_news = pull_news()
    stored_news = read_json("json/news.json")
    unread_news = []

    for i in range(0, 10):
        if pulled_news[i]["title"] != stored_news[i]["title"]:
            unread_news.append(pulled_news[i]["title"])

    return unread_news
<commit_msg>Fixed description<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""This script scrapes all the info about the adsu of the university's city."""

import logging
import json
import configparser
import feedparser

def get_configuration():
    """Get global configuration from service.cfg"""

    config = configparser.ConfigParser()
    config.read("service.cfg")

    return config

def get_logger(debug):
    """Get logger object"""

    logging.basicConfig(
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        level=logging.INFO
        )
    logger = logging.getLogger(__name__)

    if debug is False:
        logging.disable(logging.CRITICAL)

    return logger

def write_json(data, json_file):
    """General function used everywhere to write data into a json file"""

    with open(json_file, "w") as json_file:
        json.dump(data, json_file)

def read_json(json_file):
    """General function used everywhere to read a json file"""

    with open(json_file, "r") as json_file:
        return json.load(json_file)

def pull_news():
    """This function is built to pull 10 news from the rss endpoint"""

    document = feedparser.parse(
        "http://www.disim.univaq.it/didattica/content.php?fid=rss&pid=114&did=8&lid=it"
        )
    news = [
        {"title": item.title, "description": item.description.replace("&#39;", "'")}
        for item in document["entries"][:10]
        ]

    return news

def check_news():
    """This function check if there is some unread news from the website"""

    pulled_news = pull_news()
    stored_news = read_json("json/news.json")
    unread_news = []

    for i in range(0, 10):
        if pulled_news[i]["title"] != stored_news[i]["title"]:
            unread_news.append(pulled_news[i]["title"])

    return unread_news
<|endoftext|>


<commit_before>#!/usr/bin/env python3
"""
Generate MUX.

MUXes come in two types,
 1) Configurable via logic signals,
 2) Statically configured by PnR (called "routing") muxes.
"""

import argparse
import itertools
import lxml.etree as ET
import math
import os
import sys

def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

parser = argparse.ArgumentParser(
    description='Generate a MUX wrapper.',
    fromfile_prefix_chars='@',
    prefix_chars='-~'
)

parser.add_argument(
    '--width', type=int, default=8,
    help="Width of the MUX.")

parser.add_argument(
    '--type', choices=['logic', 'routing'],
    default='logic',
    help="Type of MUX.")

parser.add_argument(
    '--split-inputs', type=str2bool, nargs='?', const=True, default=False,
    help="Split the inputs into separate signals")

parser.add_argument(
    '--split-selects', type=str2bool, nargs='?', const=True, default=False,
    help="Split the selects into separate signals")

parser.add_argument(
    '--name-mux', type=str, default='MUX',
    help="Name of the mux.")

parser.add_argument(
    '--name-input', type=str, default='I',
    help="Name of the input values for the mux.")

parser.add_argument(
    '--name-output', type=str, default='O',
    help="Name of the output value for the mux.")

parser.add_argument(
    '--name-select', type=str, default='S',
    help="Name of the select parameter for the mux.")

parser.add_argument(
    '--order', choices=[''.join(x) for x in itertools.permutations('ios')]+[''.join(x) for x in itertools.permutations('io')],
    default='iso',
    help="""Order of the arguments for the MUX. (i - Inputs, o - Output, s - Select)""")

parser.add_argument(
    '--outdir', default=None,
    help="""Directory to output generated content too.""")

parser.add_argument(
    '--comment', default=None,
    help="""Add some type of comment to the mux.""")

parser.add_argument(
    '--num_pb', default=1,
    help="""Set the num_pb for the mux.""")

parser.add_argument(
    '--subckt', default=None,
    help="""Override the subcircuit name.""")

def clog2(x):
    """Ceiling log 2 of x.

    >>> clog2(0), clog2(1), clog2(2), clog2(3), clog2(4)
    (0, 0, 1, 2, 2)
    >>> clog2(5), clog2(6), clog2(7), clog2(8), clog2(9)
    (3, 3, 3, 3, 4)
    >>> clog2(1 << 31)
    31
    >>> clog2(1 << 63)
    63
    >>> clog2(1 << 11)
    11
    """
    x -= 1
    i = 0
    while True:
        if x <= 0:
            break
        x = x >> 1
        i += 1
    return i

import doctest
doctest.testmod()

# Method for implementing the internals of the MUX.
method = {
    'impl': """\
	assign O = I[S];
""",

    'wrap': {
        # FIXME: Wrap the LOGIC_MUX_N
        'logic': """\
    LOGIC_MUX_N ...
""",
        # FIXME: Wrap the ROUTING_MUX_N
        'routing': """\
""",
    },

    'blackbox': "",
}

logic_template = """\
module {:name}({:args});

    output wire {:name_output};
    input wire [{:width}:0] {:name_;
    input wire [{:select}:0] {:name_select};

{:method}
endmodule
"""

routing_template = """\
module {:name}({:args});
    parameter {:name_select} = 0;

    output wire {:name_output};
    input wire [{:width}:0] {:name_input};

{:method}
    assign {:name_output} = {:name_input}[{:name_select}];
endmodule
"""

call_args = list(sys.argv)

args = parser.parse_args()
args.width_bits = clog2(args.width)
if not args.subckt:
    args.subckt = args.name_mux

mypath = __file__

print(mypath)
print(args)

if not args.outdir:
    outdir = os.path.join(".", args.name_mux.lower())
else:
    outdir = args.outdir

repo_args = []
skip_next = False
for i, arg in enumerate(sys.argv):
    if skip_next:
        skip_next = False
        continue

    if i == 0:
        repo_args.append(os.path.relpath(mypath, outdir))
        repo_args.append("--outdir")
        repo_args.append(".")
        continue

    # Outdir is special, ignore it.
    if arg.startswith("--outdir"):
        if "=" not in arg:
            skip_next = True
            continue

    repo_args.append(repr(arg))
    assert "--" not in arg[2:], "Can't have -- in argument value or name %r" % arg

os.makedirs(outdir, exist_ok=True)

# Generated headers
generated_with = """
Generated with mux_gen.py, run the following to regenerate in this directory;
%s
""" % " ".join(repo_args)
if args.comment:
    generated_with += args.comment

# XML Files can't have "--" in them, so instead we use ~~
xml_comment = """
Generated with %s
""" % mypath
if args.comment:
    xml_comment += "\n"
    xml_comment += args.comment.replace("--", "~~")
    xml_comment += "\n"

# Create a makefile to regenerate files.
makefile_file = os.path.join(outdir, "Makefile.mux")
output_files = ['model.xml', 'pb_type.xml', '.gitignore', 'Makefile.mux']
commit_files = ['.gitignore', 'Makefile.mux']
remove_files = [f for f in output_files if f not in commit_files]
with open(makefile_file, "w") as f:
    f.write("""\
%s

all: %s

clean:
\trm -f .mux_gen.stamp %s

.mux_gen.stamp: %s Makefile.mux
\t%s
\ttouch --reference $< $@

.PHONY: all clean

""" % ("\n# ".join(generated_with.split("\n")),
       " ".join(output_files),
       " ".join(remove_files),
       repo_args[0],
       " ".join(repo_args),
    ))

    for name in output_files:
        f.write("%s: .mux_gen.stamp\n\n" % name)

print("Makefile.mux", "-"*75)
print(open(makefile_file).read())
print("-"*75)

# .gitignore file for the generated file.
gitignore_file = os.path.join(outdir, ".gitignore")
with open(gitignore_file, "w") as f:
    f.write(".mux_gen.stamp\n")
    for name in remove_files:
        f.write(name+'\n')

print(".gitignore", "-"*75)
print(open(gitignore_file).read())
print("-"*75)

# Work out the port names
port_names = []
for i in args.order:
    if i == 'i':
        if args.split_inputs:
            port_names.extend(('i', args.name_input+str(i), 1, '[%i]' % i) for i in range(args.width))
        else:
            port_names.append(('i', args.name_input, args.width, '[%i:0]' % args.width))
    elif i == 's' and args.type == 'logic':
        if args.split_selects and args.width_bits > 1:
            port_names.extend(('s', args.name_select+str(i), 1, '[%i]' % i) for i in range(args.width_bits))
        else:
            port_names.append(('s', args.name_select, args.width_bits, '[%i:0]' % args.width_bits))
    elif i == 'o':
        port_names.append(('o', args.name_output, 1, ''))

# Generate the Model XML form.
models_xml = ET.Element('models')
models_xml.append(ET.Comment(xml_comment))

model_xml = ET.SubElement(models_xml, 'model', {'name': args.subckt})

input_ports = ET.SubElement(model_xml, 'input_ports')
output_ports = ET.SubElement(model_xml, 'output_ports')
for type, name, width, index in port_names:
    if type in ('i', 's'):
        ET.SubElement(
            input_ports, 'port', {
                'name': name,
                'combinational_sink_ports': ','.join(n for t, n, w, i in port_names if t in ('o',)),
            })
    elif type in ('o',):
        ET.SubElement(output_ports, 'port', {'name': args.name_output})

models_str = ET.tostring(models_xml, pretty_print=True).decode('utf-8')
print("models.xml", "-"*75)
print(models_str)
print("-"*75)
with open(os.path.join(outdir, "model.xml"), "w") as f:
    f.write(models_str)

# Generate the pb_type XML form.
pb_type_xml = ET.Element(
    'pb_type', {
        'name': args.name_mux,
        'num_pb': str(args.num_pb),
        'blif_model': '.subckt %s' % args.subckt,
    })
pb_type_xml.append(ET.Comment(xml_comment))

for type, name, width, index in port_names:
    ET.SubElement(
        pb_type_xml,
        {'i': 'input',
         's': 'input',
         'o': 'output'}[type],
        {'name': name, 'num_pins': str(width)},
    )

if args.type == 'logic':
    for itype, iname, iwidth, iindex in port_names:
        if itype not in ('i', 's'):
            continue

        for otype, oname, owidth, oindex in port_names:
            if otype not in ('o',):
                continue

            ET.SubElement(
                pb_type_xml,
                'delay_constant', {
                    'max': "10e-12",
                    'in_port': "%s.%s" % (args.name_mux, iname),
                    'out_port': "%s.%s" % (args.name_mux, oname),
                },
            )

if False:
    interconnect = ET.SubElement(pb_type_xml, 'interconnect')
    if args.type == 'routing':
        ET.SubElement(
            interconnect,
            'mux', {
                'name': 'OUT',
                'input': ' '.join(n for t, n, w, i in port_names if t in ('i',)),
                'output': args.name_output,
            },
        )
    else:
        for type, name, width, index in port_names:
            if type in ('i', 's'):
                ET.SubElement(
                    interconnect,
                    'direct', {
                        'name': name,
                        'input': name,
                        'output': "MUX.%s%s" % (type.upper(), index),
                    },
                )
            elif type in ('o',):
                ET.SubElement(
                    interconnect,
                    'direct', {
                        'name': name,
                        'output': name,
                        'input': "MUX.%s%s" % (type.upper(), index),
                    },
                )

pb_type_str = ET.tostring(pb_type_xml, pretty_print=True).decode('utf-8')
print("pb_type.xml", "-"*75)
print(pb_type_str)
print("-"*75)
with open(os.path.join(outdir, "pb_type.xml"), "w") as f:
    f.write(pb_type_str)

import sys; sys.exit(0)

mux_args = []
for i in args.order:
    if i == 'i':
        if args.split_inputs:
            for i in range(args.width):
                mux_args.append(args.name_input+str(i))
        else:
            mux_args.append(args.name_input)
    elif i == 'o':
        mux_args.append(args.name_output)
    elif i == 's':
        mux_args.append(args.name_select)
    else:
        assert False, "Unknown input argument."

<commit_msg>artix7: working on muxes.<commit_after>#!/usr/bin/env python3
"""
Generate MUX.

MUXes come in two types,
 1) Configurable via logic signals,
 2) Statically configured by PnR (called "routing") muxes.
"""

import argparse
import itertools
import lxml.etree as ET
import math
import os
import sys

def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

parser = argparse.ArgumentParser(
    description='Generate a MUX wrapper.',
    fromfile_prefix_chars='@',
    prefix_chars='-~'
)

parser.add_argument(
    '--width', type=int, default=8,
    help="Width of the MUX.")

parser.add_argument(
    '--type', choices=['logic', 'routing'],
    default='logic',
    help="Type of MUX.")

parser.add_argument(
    '--split-inputs', type=str2bool, nargs='?', const=True, default=False,
    help="Split the inputs into separate signals")

parser.add_argument(
    '--split-selects', type=str2bool, nargs='?', const=True, default=False,
    help="Split the selects into separate signals")

parser.add_argument(
    '--name-mux', type=str, default='MUX',
    help="Name of the mux.")

parser.add_argument(
    '--name-input', type=str, default='I',
    help="Name of the input values for the mux.")

parser.add_argument(
    '--name-inputs', type=str, default=None,
    help="Comma deliminator list for the name of each input to the mux (implies --split-inputs).")

parser.add_argument(
    '--name-output', type=str, default='O',
    help="Name of the output value for the mux.")

parser.add_argument(
    '--name-select', type=str, default='S',
    help="Name of the select parameter for the mux.")

parser.add_argument(
    '--name-selects', type=str, default=None,
    help="Comma deliminator list for the name of each select to the mux (implies --split-selects).")

parser.add_argument(
    '--order', choices=[''.join(x) for x in itertools.permutations('ios')]+[''.join(x) for x in itertools.permutations('io')],
    default='iso',
    help="""Order of the arguments for the MUX. (i - Inputs, o - Output, s - Select)""")

parser.add_argument(
    '--outdir', default=None,
    help="""Directory to output generated content too.""")

parser.add_argument(
    '--comment', default=None,
    help="""Add some type of comment to the mux.""")

parser.add_argument(
    '--num_pb', default=1,
    help="""Set the num_pb for the mux.""")

parser.add_argument(
    '--subckt', default=None,
    help="""Override the subcircuit name.""")

def output_block(name, s):
    print()
    print(name, '-'*(75-(len(name)+1)))
    print(s, end="")
    if s[-1] != '\n':
        print()
    print('-'*75)

def clog2(x):
    """Ceiling log 2 of x.

    >>> clog2(0), clog2(1), clog2(2), clog2(3), clog2(4)
    (0, 0, 1, 2, 2)
    >>> clog2(5), clog2(6), clog2(7), clog2(8), clog2(9)
    (3, 3, 3, 3, 4)
    >>> clog2(1 << 31)
    31
    >>> clog2(1 << 63)
    63
    >>> clog2(1 << 11)
    11
    """
    x -= 1
    i = 0
    while True:
        if x <= 0:
            break
        x = x >> 1
        i += 1
    return i

import doctest
doctest.testmod()

call_args = list(sys.argv)

args = parser.parse_args()
args.width_bits = clog2(args.width)
if not args.subckt:
    args.subckt = args.name_mux

mypath = __file__
mydir = os.path.dirname(mypath)

print(mypath)
print(args)

if not args.outdir:
    outdir = os.path.join(".", args.name_mux.lower())
else:
    outdir = args.outdir

mux_dir = os.path.relpath(os.path.abspath(os.path.join(mydir, '..', 'vpr', 'muxes')), outdir)

repo_args = []
skip_next = False
for i, arg in enumerate(sys.argv):
    if skip_next:
        skip_next = False
        continue

    if i == 0:
        repo_args.append(os.path.relpath(mypath, outdir))
        repo_args.append("--outdir")
        repo_args.append(".")
        continue

    # Outdir is special, ignore it.
    if arg.startswith("--outdir"):
        if "=" not in arg:
            skip_next = True
            continue

    repo_args.append(repr(arg))
    assert "--" not in arg[2:], "Can't have -- in argument value or name %r" % arg

os.makedirs(outdir, exist_ok=True)

# Generated headers
generated_with = """
Generated with mux_gen.py, run the following to regenerate in this directory;
%s
""" % " ".join(repo_args)
if args.comment:
    generated_with += args.comment

# XML Files can't have "--" in them, so instead we use ~~
xml_comment = """
Generated with %s
""" % mypath
if args.comment:
    xml_comment += "\n"
    xml_comment += args.comment.replace("--", "~~")
    xml_comment += "\n"

# ------------------------------------------------------------------------
# Create a makefile to regenerate files.
# ------------------------------------------------------------------------
makefile_file = os.path.join(outdir, "Makefile.mux")
output_files = ['model.xml', 'pb_type.xml', '.gitignore', 'Makefile.mux', 'sim.v']
commit_files = ['.gitignore', 'Makefile.mux']
remove_files = [f for f in output_files if f not in commit_files]
with open(makefile_file, "w") as f:
    f.write("""\
%s

all: %s

clean:
\trm -f .mux_gen.stamp %s

.mux_gen.stamp: %s Makefile.mux
\t%s
\ttouch --reference $< $@

.PHONY: all clean

""" % ("\n# ".join(generated_with.split("\n")),
       " ".join(output_files),
       " ".join(remove_files),
       repo_args[0],
       " ".join(repo_args),
    ))

    for name in output_files:
        f.write("%s: .mux_gen.stamp\n\n" % name)

output_block("Makefile.mux", open(makefile_file).read())

# ------------------------------------------------------------------------
# Create .gitignore file for the generated files.
# ------------------------------------------------------------------------
gitignore_file = os.path.join(outdir, ".gitignore")
with open(gitignore_file, "w") as f:
    f.write(".mux_gen.stamp\n")
    for name in remove_files:
        f.write(name+'\n')

output_block(".gitignore", open(gitignore_file).read())

# ------------------------------------------------------------------------
# Work out the port and their names
# ------------------------------------------------------------------------
if args.name_inputs:
    assert args.name_input == 'I'
    args.name_input = None
    args.split_inputs = True

    names = args.name_inputs.split(',')
    assert len(names) == args.width, "%s input names, but %s needed." % (names, args.width)
    args.name_inputs = names
elif args.split_inputs:
    args.name_inputs = [args.name_input+str(i) for i in range(args.width)]

if args.name_selects:
    assert args.name_input == 'I'
    args.name_input = None
    args.split_selects = True

    names = args.name_selects.split(',')
    assert len(names) == args.width, "%s select names, but %s needed." % (names, args.width_bits)
    args.name_selects = names
elif args.split_selects:
    args.name_selects = [args.name_select+str(i) for i in range(args.width_bits)]

port_names = []
for i in args.order:
    if i == 'i':
        if args.split_inputs:
            port_names.extend(('i', args.name_inputs[j], 1, '[%i]' % j) for j in range(args.width))
        else:
            port_names.append(('i', args.name_input, args.width, '[%i:0]' % args.width))
    elif i == 's':
        if args.split_selects and args.width_bits > 1:
            port_names.extend(('s', args.name_selects[j], 1, '[%i]' % j) for j in range(args.width_bits))
        else:
            port_names.append(('s', args.name_select, args.width_bits, '[%i:0]' % args.width_bits))
    elif i == 'o':
        port_names.append(('o', args.name_output, 1, ''))

# ------------------------------------------------------------------------
# Generate the sim.v Verilog module
# ------------------------------------------------------------------------

defs = {'i': 'input wire', 's': 'input wire', 'o': 'output wire'}

sim_file = os.path.join(outdir, "sim.v")
with open(sim_file, "w") as f:
    module_args = []
    for type, name, _, _ in port_names:
        if args.type == 'routing' and type == 's':
            continue
        module_args.append(name)

    f.write("/* ")
    f.write("\n * ".join(generated_with.splitlines()))
    f.write("\n */\n\n")
    f.write('`include "%s/sim.mux%i.v"\n' % (mux_dir, args.width))
    f.write("\n")
    f.write("module %s(%s);\n" % (args.name_mux, ", ".join(module_args)))
    previous_type = None
    for type, name, width, index in port_names:
        if previous_type != type:
            f.write("\n")
            previous_type = type
        if args.type == 'routing' and type == 's':
            if width == 1:
                f.write('\tparameter [0:0] %s = 0;\n' % (name))
            else:
                f.write('\tparameter %s %s %s = 0;\n' % (index, name))
            continue

        if width == 1:
            f.write('\t%s %s;\n' % (defs[type], name))
        else:
            f.write('\t%s %s %s;\n' % (defs[type], index, name))

    f.write("\n")
    f.write('\tMUX%s mux (\n' % args.width)
    for i in range(0, args.width):
        j = 0
        for type, name, width, index in port_names:
            if type != 'i':
                continue
            if j+width <= i:
                j += width
                continue
            break

        print(i, j, name)

        if width == 1:
            f.write('\t\t.I%i(%s),\n' % (i, name))
        else:
            f.write('\t\t.I%i(%s[%i]),\n' % (i, name, i-j))

    for i in range(0, args.width_bits):
        j = 0
        for type, name, width, index in port_names:
            if type != 's':
                continue
            if j+width < i:
                j += width
                continue
            break

        if width == 1:
            f.write('\t\t.S%i(%s),\n' % (i, name))
        else:
            f.write('\t\t.S%i(%s[%i]),\n' % (i, name, i-j))

    for type, name, width, index in port_names:
        if type != 'o':
            continue
        break
    assert width == 1
    f.write('\t\t.O(%s)\n\t);\n' % name)

    f.write('endmodule\n')

output_block("sim.v", open(sim_file).read())

# ------------------------------------------------------------------------
# Generate the Model XML form.
# ------------------------------------------------------------------------
if args.type == 'logic':
    models_xml = ET.Element('models')
    models_xml.append(ET.Comment(xml_comment))

    model_xml = ET.SubElement(models_xml, 'model', {'name': args.subckt})

    input_ports = ET.SubElement(model_xml, 'input_ports')
    output_ports = ET.SubElement(model_xml, 'output_ports')
    for type, name, width, index in port_names:
        if type in ('i', 's'):
            ET.SubElement(
                input_ports, 'port', {
                    'name': name,
                    'combinational_sink_ports': ','.join(n for t, n, w, i in port_names if t in ('o',)),
                })
        elif type in ('o',):
            ET.SubElement(output_ports, 'port', {'name': args.name_output})

    models_str = ET.tostring(models_xml, pretty_print=True).decode('utf-8')
    output_block("model.xml", model_str)
    with open(os.path.join(outdir, "model.xml"), "w") as f:
        f.write(models_str)
else:
    output_block("models.xml", "No model.xml for routing elements.")

# ------------------------------------------------------------------------
# Generate the pb_type XML form.
# ------------------------------------------------------------------------

mn = args.name_mux
pb_type_xml = ET.Element(
    'pb_type', {
        'name': mn,
        'num_pb': str(args.num_pb),
    })
if args.type == 'logic':
    pb_type_xml['blif_model'] = '.subckt %s' % args.subckt

pb_type_xml.append(ET.Comment(xml_comment))

for type, name, width, index in port_names:
    if args.type == 'routing' and type == 's':
        continue
    ET.SubElement(
        pb_type_xml,
        {'i': 'input',
         's': 'input',
         'o': 'output'}[type],
        {'name': name, 'num_pins': str(width)},
    )

if args.type == 'logic':
    for itype, iname, iwidth, iindex in port_names:
        if itype not in ('i', 's'):
            continue

        for otype, oname, owidth, oindex in port_names:
            if otype not in ('o',):
                continue

            ET.SubElement(
                pb_type_xml,
                'delay_constant', {
                    'max': "10e-12",
                    'in_port': "%s.%s" % (args.name_mux, iname),
                    'out_port': "%s.%s" % (args.name_mux, oname),
                },
            )
else:
    interconnect = ET.SubElement(pb_type_xml, 'interconnect')
    ET.SubElement(
        interconnect,
        'mux', {
            'name': '_'+args.name_mux,
            'input': ' '.join("%s.%s" % (mn,n) for t, n, _, _ in port_names if t in ('i',)),
            'output': "%s.%s" % (mn,args.name_output),
        },
    )

pb_type_str = ET.tostring(pb_type_xml, pretty_print=True).decode('utf-8')
output_block("pb_type.xml", pb_type_str)
with open(os.path.join(outdir, "pb_type.xml"), "w") as f:
    f.write(pb_type_str)
<|endoftext|>


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

import os
import logging
from datetime import datetime, timedelta

from swiftclient import client

from django.conf import settings
from django.contrib import messages
from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import gettext as _
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from vault.models import GroupProjects, CurrentProject
from identity.keystone import Keystone, KeystoneNoRequest

log = logging.getLogger(__name__)

def update_default_context(request, context={}):
    if not request.session.get('is_superuser'):
        request.session['is_superuser'] = request.user.is_superuser

    context.update({
        'logged_user': request.user,
        'project_id': request.session.get('project_id'),
        'project_name': request.session.get('project_name'),
        'is_superuser': request.user.is_superuser
    })

    return context

def generic_pagination(items, page=1, per_page=settings.PAGINATION_SIZE, query=None):
    paginator = Paginator(items, per_page)

    try:
        paginated_items = paginator.page(page)
        if (query):
            paginated_items.query = query
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_items = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_items = paginator.page(paginator.num_pages)

    return paginated_items

def save_current_project(user_id, project_id):
    """Save on database user current project"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
    except CurrentProject.DoesNotExist:
        current = CurrentProject()
        current.user_id = user_id

    current.project = project_id
    current.save()

    return current

def set_current_project(request, project):
    """Set current project on django session"""

    if project is not None:
        request.session['project_id'] = project.id
        request.session['project_name'] = project.name
    else:
        # Project doesn't exist
        request.session.pop('project_id', None)
        request.session.pop('project_name', None)

    return maybe_update_token(request)

def maybe_update_token(request):
    token_time = request.session.get('token_time')

    if token_time is None or token_time < datetime.utcnow():
        log.info('Updating token for user [{}]'.format(request.user))

        keystone = Keystone(request)

        if keystone.conn is None:
            return False

        request.session['token_time'] = (
            timedelta(minutes=15) + datetime.utcnow()
        )
        request.session['auth_token'] = keystone.conn.auth_token
        request.session['service_catalog'] = keystone.get_endpoints()

        return True

    return False

def get_current_project(user_id):
    """Return from database user current project"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
    except CurrentProject.DoesNotExist:
        return None

    keystone = KeystoneNoRequest()
    return keystone.project_get(current.project)

def delete_current_project(user_id):
    """Delete from database user current project and removes it from session"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
        current.delete()
    except Exception:
        return False

    request.session.pop('project_id')
    request.session.pop('project_name')

    return True

def purge_current_project(request, project_id):
    """Delete all entries for project_id and removes it from session"""

    try:
        items = CurrentProject.objects.filter(project=project_id)
        items.delete()
    except Exception:
        return False

    request.session.pop('project_id')
    request.session.pop('project_name')

    return True

def project_check(request, current_project):
    project_id = request.session.get('project_id')
    user = request.user

    if current_project:
        keystone = Keystone(request)
        project = keystone.project_get_by_name(current_project)

        if not project:
            # messages.add_message(request, messages.WARNING, u"Unauthorized")
            return False

        if not project_id or project.id != project_id:
            groups = user.groups.all()
            group_projects = GroupProjects.objects.filter(group_id__in=[group.id for group in groups])

            if group_projects.filter(project=project.id).count() == 0:
                # messages.add_message(request, messages.WARNING, u"Unauthorized")
                return False

            save_current_project(user.id, project.id)
            set_current_project(request, project)

    return True

def project_error(request, *args, **kwargs):
    context = {
        "project_name": request.session.get('project_name'),
        "inexistent_project": kwargs.get("inexistent_project"),
        "not_owned_project": kwargs.get("not_owned_project")
    }

    return render(request, "vault/project_error.html", context)

def project_required(view_func):
    def _wrapper(request, *args, **kwargs):
        current_project = kwargs.get('project')

        check = project_check(request, current_project)

        if not check:
            return project_error(request, inexistent_project=kwargs['project'])

        maybe_update_token(request)

        return view_func(request, *args, **kwargs)

    return _wrapper

def human_readable(value):
    """
    Returns the number in a human readable format; for example 1048576 = "1Mi".
    """
    value = float(value)
    index = -1
    suffixes = 'KMGTPEZY'
    while value >= 1024 and index + 1 < len(suffixes):
        index += 1
        value = round(value / 1024)
    if index == -1:
        return '{:d}'.format(value)
    return '{:d}{}i'.format(round(value), suffixes[index])
<commit_msg>Fixed error when user loses access to the project that is in their session<commit_after># -*- coding: utf-8 -*-

import os
import logging
from datetime import datetime, timedelta

from swiftclient import client

from django.conf import settings
from django.contrib import messages
from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import gettext as _
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from vault.models import GroupProjects, CurrentProject
from identity.keystone import Keystone, KeystoneNoRequest

log = logging.getLogger(__name__)

def update_default_context(request, context={}):
    if not request.session.get('is_superuser'):
        request.session['is_superuser'] = request.user.is_superuser

    context.update({
        'logged_user': request.user,
        'project_id': request.session.get('project_id'),
        'project_name': request.session.get('project_name'),
        'is_superuser': request.user.is_superuser
    })

    return context

def generic_pagination(items, page=1, per_page=settings.PAGINATION_SIZE, query=None):
    paginator = Paginator(items, per_page)

    try:
        paginated_items = paginator.page(page)
        if (query):
            paginated_items.query = query
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        paginated_items = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        paginated_items = paginator.page(paginator.num_pages)

    return paginated_items

def save_current_project(user_id, project_id):
    """Save on database user current project"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
    except CurrentProject.DoesNotExist:
        current = CurrentProject()
        current.user_id = user_id

    current.project = project_id
    current.save()

    return current

def set_current_project(request, project):
    """Set current project on django session"""

    if project is not None:
        request.session['project_id'] = project.id
        request.session['project_name'] = project.name
    else:
        # Project doesn't exist
        request.session.pop('project_id', None)
        request.session.pop('project_name', None)

    return maybe_update_token(request)

def maybe_update_token(request):
    token_time = request.session.get('token_time')

    if token_time is None or token_time < datetime.utcnow():
        log.info('Updating token for user [{}]'.format(request.user))

        keystone = Keystone(request)

        if keystone.conn is None:
            return False

        request.session['token_time'] = (
            timedelta(minutes=15) + datetime.utcnow()
        )
        request.session['auth_token'] = keystone.conn.auth_token
        request.session['service_catalog'] = keystone.get_endpoints()

        return True

    return False

def get_current_project(user_id):
    """Return from database user current project"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
    except CurrentProject.DoesNotExist:
        return None

    keystone = KeystoneNoRequest()
    return keystone.project_get(current.project)

def delete_current_project(user_id):
    """Delete from database user current project and removes it from session"""

    try:
        current = CurrentProject.objects.get(user_id=user_id)
        current.delete()
    except Exception:
        return False

    request.session.pop('project_id')
    request.session.pop('project_name')

    return True

def purge_current_project(request, project_id):
    """Delete all entries for project_id and removes it from session"""

    try:
        items = CurrentProject.objects.filter(project=project_id)
        items.delete()
    except Exception:
        return False

    request.session.pop('project_id')
    request.session.pop('project_name')

    return True

def project_check(request, current_project):
    project_id = request.session.get('project_id')
    user = request.user

    if current_project:
        keystone = Keystone(request)

        if not keystone.conn:
            return False

        project = keystone.project_get_by_name(current_project)

        if not project:
            # messages.add_message(request, messages.WARNING, u"Unauthorized")
            return False

        if not project_id or project.id != project_id:
            groups = user.groups.all()
            group_projects = GroupProjects.objects.filter(group_id__in=[group.id for group in groups])

            if group_projects.filter(project=project.id).count() == 0:
                # messages.add_message(request, messages.WARNING, u"Unauthorized")
                return False

            save_current_project(user.id, project.id)
            set_current_project(request, project)

    return True

def project_error(request, *args, **kwargs):
    context = {
        "project_name": request.session.get('project_name'),
        "inexistent_project": kwargs.get("inexistent_project"),
        "not_owned_project": kwargs.get("not_owned_project")
    }

    return render(request, "vault/project_error.html", context)

def project_required(view_func):
    def _wrapper(request, *args, **kwargs):
        current_project = kwargs.get('project')

        check = project_check(request, current_project)

        if not check:
            return project_error(request, inexistent_project=kwargs['project'])

        maybe_update_token(request)

        return view_func(request, *args, **kwargs)

    return _wrapper

def human_readable(value):
    """
    Returns the number in a human readable format; for example 1048576 = "1Mi".
    """
    value = float(value)
    index = -1
    suffixes = 'KMGTPEZY'
    while value >= 1024 and index + 1 < len(suffixes):
        index += 1
        value = round(value / 1024)
    if index == -1:
        return '{:d}'.format(value)
    return '{:d}{}i'.format(round(value), suffixes[index])
<|endoftext|>


<commit_before>import sys
from collections import defaultdict
import re

def conv_to_number(value):
    """
    Tries to convert a string to a number, silently returning the originally value if it fails.
    """
    try:
        return int(value)
    except ValueError:
        pass
    try:
        return float(value)
    except ValueError:
        pass
    return value

def split_and_convert(conv_func, split_max=-1, extract_single=False):
    """
    Performs a normal split() on a string, with support for converting the values and extraction of single values.

    :param conv_func: Function for converting the values
    :type conv_func: functions
    :param split_max: Maximum number of splits to perform. Default: No limit.
    :type split_max: int
    :param extract_single: If value ends up being a single value, do not return a list. Default: False
    :type extract_single: bool
    """
    def inner(x):
        l = [conv_func(i) for i in x.split(',', split_max)]
        if len(l) == 1 and extract_single:
            l = l[0]
        return l
    return inner

class VEPInfo(object):
    """
    Parser for the VEP INFO field.
    """

    field = 'CSQ'

    def __init__(self, meta):
        self.meta = meta
        self.fields = self._parseFieldsFromMeta()
        self.converters = {
            'DISTANCE': int,
            'ALLELE_NUM': int,
            'STRAND': int
        }

    def _parseFieldsFromMeta(self):
        info_line = next((l for l in self.meta['INFO'] if l.get('ID') == VEPInfo.field), None)
        if info_line:
            fields = info_line['Description'].split('Format: ', 1)[1].split('|')
            return fields
        return list()

    def parse(self, key, value, info_data, alleles):
        transcripts = value.split(',')

        all_data = [
            {
                k: self.converters.get(k, lambda x: x)(v) for k, v in zip(self.fields, t.split('|')) if v is not ''
            } for t in transcripts
        ]

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = [d for d in all_data if d['ALLELE_NUM']-1 == a_idx]

class SnpEffInfo(object):
    """
    Parser for the snpEff INFO field.
    """

    field = 'EFF'

    def __init__(self, meta):
        self.meta = meta
        self.fields = self._parseFieldsFromMeta()
        self.converters = {
            'Genotype_Number': int,
            'Exon_Rank': int,
            'Amino_Acid_length': int
        }

    def _parseFormat(self, line):
        """
        Parse following format to a flat list.
        Effect ( Effect_Impact | Functional_Class | Codon_Change | Amino_Acid_Change| Amino_Acid_length | Gene_Name | Transcript_BioType | Gene_Coding | Transcript_ID | Exon_Rank  | Genotype_Number [ | ERRORS | WARNINGS ] )
        """
        fields = list()

        line = line.replace('(', '|').replace(')', '').replace('[ | ERRORS | WARNINGS ]', '').replace('\'', '')
        fields = line.split('|')

        fields = [f.strip() for f in fields]

        return fields

    def _parseFieldsFromMeta(self):
        info_line = next((l for l in self.meta['INFO'] if l.get('ID') == SnpEffInfo.field), None)
        if info_line:
            fields = self._parseFormat(info_line['Description'].split('Format: \'', 1)[1])
            fields.append('ERRORS')
            return fields
        return list()

    def parse(self, key, value, info_data, alleles):
        transcripts = value.split(',')

        all_data = [
            {
                k: self.converters.get(k, lambda x: x)(v) for k, v in zip(self.fields, self._parseFormat(t)) if v is not ''
            } for t in transcripts
        ]

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = [d for d in all_data if d['Genotype_Number']-1 == a_idx]

class CsvAlleleParser(object):
    """
    Parses comma separated values, and inserts them into the data according to the allele the value belongs to.
    """
    def __init__(self, meta):
        self.meta = meta

    def parse(self, key, value, info_data, alleles):
        allele_values = value.split(',')
        if not len(allele_values) == len(alleles):
            raise RuntimeError("Number of allele values for {} not matching number of alleles".format(key))

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = allele_values[a_idx]

class HeaderParser(object):
    """
    Class for parsing the header part of the vcf and returns the metadata and header data.
    """

    RE_INFO = re.compile(r'[<]*(.*?)=["]*(.*?)["]*[,>]')

    def __init__(self, path):
        self.path = path
        self.metaProccessors = {
            'INFO': self._parseMetaInfo,
            'FILTER': self._parseMetaInfo,
            'FORMAT': self._parseMetaInfo
        }

    def _parseMetaInfo(self, infoline):
        groups = re.findall(HeaderParser.RE_INFO, infoline)
        info = {k: v for k, v in groups}
        return info

    def _parseHeader(self):
        meta = defaultdict(list)
        header = list()

        # Read in metadata and header
        with open(self.path) as fd:
            for line in fd.xreadlines():
                line = line.replace('\n', '')
                if line.startswith('##'):
                    key, value = line[2:].split('=', 1)
                    meta[key].append(value)
                elif(line.startswith('#')):
                    line = line.replace('#', '')
                    header = line.split('\t')
                else:
                    # End of header
                    break

        # Extract data with processors
        for key, func in self.metaProccessors.iteritems():
            if key in meta:
                for idx, value in enumerate(meta[key]):
                    meta[key][idx] = func(value)

        # Extract value from single-item lists ([val] -> val):
        for k, v in meta.iteritems():
            if len(v) == 1:
                meta[k] = v[0]

        return meta, header

    def parse(self):
        return self._parseHeader()

class DataParser(object):

    # Official fields in specification
    SPEC_FIELDS = [
        'CHROM',
        'POS',
        'ID',
        'REF',
        'ALT',
        'QUAL',
        'FILTER',
        'INFO',
        'FORMAT'
    ]

    def __init__(self, path, meta, header):
        self.path = path
        self.meta = meta
        self.header = header

        self.infoProcessors = self._generateInfoProcessors()
        self.infoProcessors.update({
            SnpEffInfo.field: SnpEffInfo(self.meta).parse,
            VEPInfo.field: VEPInfo(self.meta).parse,
            'AC': CsvAlleleParser(self.meta).parse,
            'AF': CsvAlleleParser(self.meta).parse,
            'MLEAC': CsvAlleleParser(self.meta).parse,
            'MLEAF': CsvAlleleParser(self.meta).parse,
        })

    def _generateInfoProcessors(self):
        """
        Generates the different processors to handle the keys in the INFO field.

        These processors are generated from INFO fields in the header metadata, using the specified type and length.

        Unlike the custom processors (e.g. VEPInfo), it does not generate allele specific data. Instead, all data is put
        into the 'ALL' key in 'INFO' in the resulting dictionary.
        """
        processors = dict()
        for f in self.meta['INFO']:
            parse_func = str
            if f['Type'] == 'Integer':
                parse_func = int
            elif f['Type'] in ['Number', 'Double', 'Float']:
                parse_func = float
            elif f['Type'] == 'Flag':
                parse_func = bool

            number = f['Number']

            try:
                # Number == int
                n = int(number)
                func = split_and_convert(parse_func, split_max=n, extract_single=True)
            except ValueError:
                # Number == Allele specific
                if number == 'A':
                    func = split_and_convert(parse_func)
                # Number == Unknown
                else:
                    func = parse_func

            # We ignore alleles for these values, but return them in the 'ALL' key
            def val_func(key, value, info_data, alleles):
                info_data['ALL'][key] = func(value)

            # Add processor for this value, recognized by it's ID
            processors[f['ID']] = val_func

        return processors

    def _parseDataInfoField(self, data):
        """
        Parses the INFO data into data structures.
        Data is split into general ('ALL') and allele specific data.
        """

        alleles = data['ALT']

        fields = data['INFO'].split(';')

        # Create dict for allele specific INFO
        info_data = {
            k: dict() for k in alleles
        }
        # And include INFO for 'ALL' alleles
        info_data['ALL'] = dict()

        for f in fields:
            if '=' in f:
                key, value = f.split('=', 1)
                # Process keys by processor, if present, or just give value
                # Data is inserted into info_data by the functions
                self.infoProcessors[key](key, value, info_data, alleles)
            else:
                info_data['ALL'][f] = True

        data['INFO'] = info_data

    def _parseDataSampleFields(self, data):
        # Any fields not part of spec if a sample
        sample_fields = [f for f in self.header if f not in DataParser.SPEC_FIELDS]

        sample_format = data['FORMAT'].split(':')

        samples = dict()
        extract = split_and_convert(conv_to_number, extract_single=True)
        for sample_field in sample_fields:
            sample_text = data.pop(sample_field)
            samples[sample_field] = {
                k: extract(v) for k, v in zip(sample_format, sample_text.split(':'))
            }

        data['SAMPLES'] = samples

        del data['FORMAT']

    def _parseData(self, line):
        data = {
            k: v for k, v in zip(self.header, line.split('\t'))
        }

        # Split by alleles
        data['ALT'] = data['ALT'].split(',')

        self._parseDataInfoField(data)

        self._parseDataSampleFields(data)

        # Manual conversion
        data['POS'] = int(data['POS'])
        data['QUAL'] = float(data['QUAL'])

        return data

    def iter(self):
        found_data_start = False
        with open(self.path) as fd:
            for line in fd.xreadlines():
                # Skip header, wait for #CHROM to signal start of data
                if line.startswith('#CHROM') and not found_data_start:
                    found_data_start = True
                    continue
                if not found_data_start:
                    continue
                line = line.replace('\n', '')
                data = self._parseData(line)
                yield data

class VcfIterator(object):

    def __init__(self, path):
        self.path = path

        self.meta, self.header = HeaderParser(self.path).parse()

    def getHeader(self):
        return self.header

    def getMeta(self):
        return self.meta

    def iter(self):
        d = DataParser(self.path, self.meta, self.header)
        for r in d.iter():
            yield r

if __name__ == '__main__':
    import json

    path = sys.argv[1]
    v = VcfIterator(path)

    for value in v.iter():
        print json.dumps(value, indent=4)

<commit_msg>Add getSamples() function for providing sample names from header<commit_after>import sys
from collections import defaultdict
import re

# Official fields in specification
SPEC_FIELDS = [
    'CHROM',
    'POS',
    'ID',
    'REF',
    'ALT',
    'QUAL',
    'FILTER',
    'INFO',
    'FORMAT'
]

class Util(object):

    @staticmethod
    def conv_to_number(value):
        """
        Tries to convert a string to a number, silently returning the originally value if it fails.
        """
        try:
            return int(value)
        except ValueError:
            pass
        try:
            return float(value)
        except ValueError:
            pass
        return value

    @staticmethod
    def split_and_convert(conv_func, split_max=-1, extract_single=False):
        """
        Performs a normal split() on a string, with support for converting the values and extraction of single values.

        :param conv_func: Function for converting the values
        :type conv_func: functions
        :param split_max: Maximum number of splits to perform. Default: No limit.
        :type split_max: int
        :param extract_single: If value ends up being a single value, do not return a list. Default: False
        :type extract_single: bool
        """
        def inner(x):
            l = [conv_func(i) for i in x.split(',', split_max)]
            if len(l) == 1 and extract_single:
                l = l[0]
            return l
        return inner

class VEPInfo(object):
    """
    Parser for the VEP INFO field.
    """

    field = 'CSQ'

    def __init__(self, meta):
        self.meta = meta
        self.fields = self._parseFieldsFromMeta()
        self.converters = {
            'DISTANCE': int,
            'ALLELE_NUM': int,
            'STRAND': int
        }

    def _parseFieldsFromMeta(self):
        info_line = next((l for l in self.meta['INFO'] if l.get('ID') == VEPInfo.field), None)
        if info_line:
            fields = info_line['Description'].split('Format: ', 1)[1].split('|')
            return fields
        return list()

    def parse(self, key, value, info_data, alleles):
        transcripts = value.split(',')

        all_data = [
            {
                k: self.converters.get(k, lambda x: x)(v) for k, v in zip(self.fields, t.split('|')) if v is not ''
            } for t in transcripts
        ]

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = [d for d in all_data if d['ALLELE_NUM']-1 == a_idx]

class SnpEffInfo(object):
    """
    Parser for the snpEff INFO field.
    """

    field = 'EFF'

    def __init__(self, meta):
        self.meta = meta
        self.fields = self._parseFieldsFromMeta()
        self.converters = {
            'Genotype_Number': int,
            'Exon_Rank': int,
            'Amino_Acid_length': int
        }

    def _parseFormat(self, line):
        """
        Parse following format to a flat list.
        Effect ( Effect_Impact | Functional_Class | Codon_Change | Amino_Acid_Change| Amino_Acid_length | Gene_Name | Transcript_BioType | Gene_Coding | Transcript_ID | Exon_Rank  | Genotype_Number [ | ERRORS | WARNINGS ] )
        """
        fields = list()

        line = line.replace('(', '|').replace(')', '').replace('[ | ERRORS | WARNINGS ]', '').replace('\'', '')
        fields = line.split('|')

        fields = [f.strip() for f in fields]

        return fields

    def _parseFieldsFromMeta(self):
        info_line = next((l for l in self.meta['INFO'] if l.get('ID') == SnpEffInfo.field), None)
        if info_line:
            fields = self._parseFormat(info_line['Description'].split('Format: \'', 1)[1])
            fields.append('ERRORS')
            return fields
        return list()

    def parse(self, key, value, info_data, alleles):
        transcripts = value.split(',')

        all_data = [
            {
                k: self.converters.get(k, lambda x: x)(v) for k, v in zip(self.fields, self._parseFormat(t)) if v is not ''
            } for t in transcripts
        ]

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = [d for d in all_data if d['Genotype_Number']-1 == a_idx]

class CsvAlleleParser(object):
    """
    Parses comma separated values, and inserts them into the data according to the allele the value belongs to.
    """
    def __init__(self, meta):
        self.meta = meta

    def parse(self, key, value, info_data, alleles):
        allele_values = value.split(',')
        if not len(allele_values) == len(alleles):
            raise RuntimeError("Number of allele values for {} not matching number of alleles".format(key))

        for a_idx, allele in enumerate(alleles):
            info_data[allele][key] = allele_values[a_idx]

class HeaderParser(object):
    """
    Class for parsing the header part of the vcf and returns the metadata and header data.
    """

    RE_INFO = re.compile(r'[<]*(.*?)=["]*(.*?)["]*[,>]')

    def __init__(self, path):
        self.path = path
        self.metaProccessors = {
            'INFO': self._parseMetaInfo,
            'FILTER': self._parseMetaInfo,
            'FORMAT': self._parseMetaInfo
        }

    def _getSamples(self, header):
        return [field for field in header if field not in SPEC_FIELDS]

    def _parseMetaInfo(self, infoline):
        groups = re.findall(HeaderParser.RE_INFO, infoline)
        info = {k: v for k, v in groups}
        return info

    def _parseHeader(self):
        meta = defaultdict(list)
        header = list()

        # Read in metadata and header
        with open(self.path) as fd:
            for line in fd.xreadlines():
                line = line.replace('\n', '')
                if line.startswith('##'):
                    key, value = line[2:].split('=', 1)
                    meta[key].append(value)
                elif(line.startswith('#')):
                    line = line.replace('#', '')
                    header = line.split('\t')
                else:
                    # End of header
                    break

        # Extract data with processors
        for key, func in self.metaProccessors.iteritems():
            if key in meta:
                for idx, value in enumerate(meta[key]):
                    meta[key][idx] = func(value)

        # Extract value from single-item lists ([val] -> val):
        for k, v in meta.iteritems():
            if len(v) == 1:
                meta[k] = v[0]

        samples = self._getSamples(header)
        return meta, header, samples

    def parse(self):
        return self._parseHeader()

class DataParser(object):

    def __init__(self, path, meta, header, samples):
        self.path = path
        self.meta = meta
        self.header = header
        self.samples = samples

        self.infoProcessors = self._generateInfoProcessors()
        self.infoProcessors.update({
            SnpEffInfo.field: SnpEffInfo(self.meta).parse,
            VEPInfo.field: VEPInfo(self.meta).parse,
            'AC': CsvAlleleParser(self.meta).parse,
            'AF': CsvAlleleParser(self.meta).parse,
            'MLEAC': CsvAlleleParser(self.meta).parse,
            'MLEAF': CsvAlleleParser(self.meta).parse,
        })

    def _generateInfoProcessors(self):
        """
        Generates the different processors to handle the keys in the INFO field.

        These processors are generated from INFO fields in the header metadata, using the specified type and length.

        Unlike the custom processors (e.g. VEPInfo), it does not generate allele specific data. Instead, all data is put
        into the 'ALL' key in 'INFO' in the resulting dictionary.
        """
        processors = dict()
        for f in self.meta['INFO']:
            parse_func = str
            if f['Type'] == 'Integer':
                parse_func = int
            elif f['Type'] in ['Number', 'Double', 'Float']:
                parse_func = float
            elif f['Type'] == 'Flag':
                parse_func = bool

            number = f['Number']

            try:
                # Number == int
                n = int(number)
                func = Util.split_and_convert(parse_func, split_max=n, extract_single=True)
            except ValueError:
                # Number == Allele specific
                if number == 'A':
                    func = Util.split_and_convert(parse_func)
                # Number == Unknown
                else:
                    func = parse_func

            # We ignore alleles for these values, but return them in the 'ALL' key
            def val_func(key, value, info_data, alleles):
                info_data['ALL'][key] = func(value)

            # Add processor for this value, recognized by it's ID
            processors[f['ID']] = val_func

        return processors

    def _parseDataInfoField(self, data):
        """
        Parses the INFO data into data structures.
        Data is split into general ('ALL') and allele specific data.
        """

        alleles = data['ALT']

        fields = data['INFO'].split(';')

        # Create dict for allele specific INFO
        info_data = {
            k: dict() for k in alleles
        }
        # And include INFO for 'ALL' alleles
        info_data['ALL'] = dict()

        for f in fields:
            if '=' in f:
                key, value = f.split('=', 1)
                # Process keys by processor, if present, or just give value
                # Data is inserted into info_data by the functions
                self.infoProcessors[key](key, value, info_data, alleles)
            else:
                info_data['ALL'][f] = True

        data['INFO'] = info_data

    def _parseDataSampleFields(self, data):
        sample_format = data['FORMAT'].split(':')

        samples = dict()
        extract = Util.split_and_convert(Util.conv_to_number, extract_single=True)
        for sample_name in self.samples:
            sample_text = data.pop(sample_name)
            samples[sample_name] = {
                k: extract(v) for k, v in zip(sample_format, sample_text.split(':'))
            }

        data['SAMPLES'] = samples

        del data['FORMAT']

    def _parseData(self, line):
        data = {
            k: v for k, v in zip(self.header, line.split('\t'))
        }

        # Split by alleles
        data['ALT'] = data['ALT'].split(',')

        self._parseDataInfoField(data)

        self._parseDataSampleFields(data)

        # Manual conversion
        data['POS'] = int(data['POS'])
        data['QUAL'] = float(data['QUAL'])

        return data

    def iter(self):
        found_data_start = False
        with open(self.path) as fd:
            for line in fd.xreadlines():
                # Skip header, wait for #CHROM to signal start of data
                if line.startswith('#CHROM') and not found_data_start:
                    found_data_start = True
                    continue
                if not found_data_start:
                    continue
                line = line.replace('\n', '')
                data = self._parseData(line)
                yield data

class VcfIterator(object):

    def __init__(self, path):
        self.path = path

        self.meta, self.header, self.samples = HeaderParser(self.path).parse()

    def getHeader(self):
        return self.header

    def getMeta(self):
        return self.meta

    def getSamples(self):
        return self.samples

    def iter(self):
        d = DataParser(self.path, self.meta, self.header, self.samples)
        for r in d.iter():
            yield r

if __name__ == '__main__':
    import json

    path = sys.argv[1]
    v = VcfIterator(path)

    for value in v.iter():
        print json.dumps(value, indent=4)

<|endoftext|>


<commit_before>from django.shortcuts import render
from django.http import HttpResponse, JsonResponse

# Create your views here.
def getconds(request):
	print "request mything", request.body
	return HttpResponse("heybi" + request.read()+ "+  body:"+ request.body)<commit_msg>django cors headers setts<commit_after>from django.shortcuts import render
from django.http import HttpResponse, JsonResponse

# Create your views here.
def getconds(request):
	print "request mything", request.body
	var objs= request.body
	return HttpResponse("heybi" + type(objs))<|endoftext|>


<commit_before>from six.moves.urllib.parse import urlparse, parse_qsl

class Request(object):

    def __init__(self, method, uri, body, headers):
        self.method = method
        self.uri = uri
        self.body = body
        # make headers a frozenset so it will be hashable
        self.headers = frozenset(headers.items())

    def add_header(self, key, value):
        tmp = dict(self.headers)
        tmp[key] = value
        self.headers = frozenset(tmp.iteritems())

    @property
    def scheme(self):
        return urlparse(self.uri).scheme

    @property
    def host(self):
        return urlparse(self.uri).hostname

    @property
    def port(self):
        return urlparse(self.uri).port

    @property
    def path(self):
        return urlparse(self.uri).path

    @property
    def query(self):
        q = urlparse(self.uri).query
        return sorted(parse_qsl(q))

    # alias for backwards compatibility
    @property
    def url(self):
        return self.uri

    # alias for backwards compatibility
    @property
    def protocol(self):
        return self.scheme

    def __key(self):
        return (
            self.method,
            self.uri,
            self.body,
            self.headers
        )

    def __hash__(self):
        return hash(self.__key())

    def __eq__(self, other):
        return hash(self) == hash(other)

    def __str__(self):
        return "<Request ({0}) {1}>".format(self.method, self.uri)

    def __repr__(self):
        return self.__str__()

    def _to_dict(self):
        return {
            'method': self.method,
            'uri': self.uri,
            'body': self.body,
            'headers': dict(self.headers),
        }

    @classmethod
    def _from_dict(cls, dct):
        return Request(**dct)
<commit_msg>Deleted unnecessary __hash__ method of Request<commit_after>from six.moves.urllib.parse import urlparse, parse_qsl

class Request(object):

    def __init__(self, method, uri, body, headers):
        self.method = method
        self.uri = uri
        self.body = body
        # make headers a frozenset so it will be hashable
        self.headers = frozenset(headers.items())

    def add_header(self, key, value):
        tmp = dict(self.headers)
        tmp[key] = value
        self.headers = frozenset(tmp.iteritems())

    @property
    def scheme(self):
        return urlparse(self.uri).scheme

    @property
    def host(self):
        return urlparse(self.uri).hostname

    @property
    def port(self):
        return urlparse(self.uri).port

    @property
    def path(self):
        return urlparse(self.uri).path

    @property
    def query(self):
        q = urlparse(self.uri).query
        return sorted(parse_qsl(q))

    # alias for backwards compatibility
    @property
    def url(self):
        return self.uri

    # alias for backwards compatibility
    @property
    def protocol(self):
        return self.scheme

    def __str__(self):
        return "<Request ({0}) {1}>".format(self.method, self.uri)

    def __repr__(self):
        return self.__str__()

    def _to_dict(self):
        return {
            'method': self.method,
            'uri': self.uri,
            'body': self.body,
            'headers': dict(self.headers),
        }

    @classmethod
    def _from_dict(cls, dct):
        return Request(**dct)
<|endoftext|>


<commit_before>#!/usr/bin/python3
# videoLister.py - walk a given directory and return list of files matching
# the given extensions

import os
import logging

VIDEO_EXT = ('.webm', '.mkv', '.flv', '.avi', '.mov', '.qt', '.wmv', '.mp4',
             '.m4v', '.mpg', '.mpeg')

def videoDir(path_to_videos):
    """Searches directory and generates a list of matching filenames.

    Argument:
        path_to_videos: path to search for files matching the defined video
            extensions.

    Yields:
        A matching filename, including path.

    Raises:
        Exception: Not a valid directory, please input a directory to search.
    """

    logger = logging.getLogger(__name__)
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    if os.path.exists(path_to_videos) and os.path.isdir(path_to_videos):
        for root, dirs, files in os.walk(path_to_videos):
            for filename in files:
                if filename.endswith(VIDEO_EXT):
                    yield os.path.join(root, filename)

    elif os.path.exists(path_to_videos) and os.path.isfile(path_to_videos):
        if path_to_videos.endswith(VIDEO_EXT):
            yield path_to_videos

    elif not (os.path.exists(path_to_videos) and (os.path.isdir(path_to_videos) or os.path.isfile(path_to_videos))):
        logger.warning('%s is not a valid directory or file.' % path_to_videos)
<commit_msg>simlify conditional and add debug logging.<commit_after>#!/usr/bin/python3
# videoLister.py - walk a given directory and return list of files matching
# the given extensions

import os
import logging

VIDEO_EXT = ('.webm', '.mkv', '.flv', '.avi', '.mov', '.qt', '.wmv', '.mp4',
             '.m4v', '.mpg', '.mpeg')

def videoDir(path_to_videos):
    """Searches directory and generates a list of matching filenames.

    Argument:
        path_to_videos: path to search for files matching the defined video
            extensions.

    Yields:
        A matching filename, including path.

    Raises:
        Exception: Not a valid directory, please input a directory to search.
    """

    logger = logging.getLogger(__name__)
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    if os.path.exists(path_to_videos) and os.path.isdir(path_to_videos):
        for root, dirs, files in os.walk(path_to_videos):
            for filename in files:
                if filename.endswith(VIDEO_EXT):
                    logger.debug('Found: {}'.format(os.path.join(root, filename)))
                    yield os.path.join(root, filename)

    elif os.path.exists(path_to_videos) and os.path.isfile(path_to_videos):
        if path_to_videos.endswith(VIDEO_EXT):
            logger.debug('Found: {}'.format(path_to_videos))
            yield path_to_videos

    else:
        logger.warning('{} is not a valid directory or file.'.format(path_to_videos))
<|endoftext|>