<commit_before>from __future__ import print_function, absolute_import

import datetime
import json
import logging
import os
import sys
from argparse import ArgumentParser

import jsone
import requests
import yaml
from six import string_types

from .formatter import all_formatters
from .main import log

here = os.path.abspath(os.path.dirname(__file__))

ACTIVE_DATA_URL = 'http://activedata.allizom.org/query'
QUERY_DIR = os.path.join(here, 'queries')

def query_activedata(query):
    response = requests.post(ACTIVE_DATA_URL,
                             data=query,
                             stream=True)
    response.raise_for_status()
    return response.json()

def load_query(name):
    found = False
    for path in os.listdir(QUERY_DIR):
        query = os.path.splitext(path)[0]
        if name != query:
            continue

        found = True
        with open(os.path.join(QUERY_DIR, path)) as fh:
            for query in yaml.load_all(fh):
                yield query

    if not found:
        log.error("query '{}' not found".format(name))

def run_query(name, **context):
    for query in load_query(name):
        # If limit is in the context, override the queries' value. We do this
        # to keep the results down to a sane level when testing queries.
        if 'limit' in context:
            query['limit'] = context['limit']
        query = jsone.render(query, context)
        query_str = json.dumps(query, indent=2, separators=(',', ':'))
        log.debug("Running query {}:\n{}".format(name, query_str))
        yield query_activedata(query_str)

def format_date(timestamp, interval='day'):
    return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')

def cli(args=sys.argv[1:]):
    parser = ArgumentParser()
    parser.add_argument('query', nargs='?', help="Query to run.")
    parser.add_argument('-l', '--list', action='store_true', default=False,
                        help="List available queries.")
    parser.add_argument('-f', '--format', dest='fmt', default='table',
                        choices=all_formatters.keys(),
                        help="Format to print data in, defaults to 'table'.")
    parser.add_argument('-v', '--verbose', action='store_true', default=False,
                        help="Print the query and other debugging information.")

    args, remainder = parser.parse_known_args(args)
    if args.list:
        queries = [os.path.splitext(q)[0] for q in os.listdir(QUERY_DIR)]
        log.info('\n'.join(sorted(queries)))
        return

    if args.verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Fake the context for convenience
    fake_context = {
        'branch': 'mozilla-central',
        'from_date': 'now-week',
        'to_date': 'now',
        'rev': '5b33b070378a',
        'path': 'dom/indexedDB',
        'limit': 10,
    }

    if isinstance(args.fmt, string_types):
        fmt = all_formatters[args.fmt]

    for result in run_query(args.query, **fake_context):
        data = result['data']
        if 'header' in result:
            data.insert(0, result['header'])
        print(fmt(data))

if __name__ == '__main__':
    sys.exit(cli())
<commit_msg>Don't insert header when using the json format with adr-query<commit_after>from __future__ import print_function, absolute_import

import datetime
import json
import logging
import os
import sys
from argparse import ArgumentParser

import jsone
import requests
import yaml
from six import string_types

from .formatter import all_formatters
from .main import log

here = os.path.abspath(os.path.dirname(__file__))

ACTIVE_DATA_URL = 'http://activedata.allizom.org/query'
QUERY_DIR = os.path.join(here, 'queries')

def query_activedata(query):
    response = requests.post(ACTIVE_DATA_URL,
                             data=query,
                             stream=True)
    response.raise_for_status()
    return response.json()

def load_query(name):
    found = False
    for path in os.listdir(QUERY_DIR):
        query = os.path.splitext(path)[0]
        if name != query:
            continue

        found = True
        with open(os.path.join(QUERY_DIR, path)) as fh:
            for query in yaml.load_all(fh):
                yield query

    if not found:
        log.error("query '{}' not found".format(name))

def run_query(name, **context):
    for query in load_query(name):
        # If limit is in the context, override the queries' value. We do this
        # to keep the results down to a sane level when testing queries.
        if 'limit' in context:
            query['limit'] = context['limit']
        query = jsone.render(query, context)
        query_str = json.dumps(query, indent=2, separators=(',', ':'))
        log.debug("Running query {}:\n{}".format(name, query_str))
        yield query_activedata(query_str)

def format_date(timestamp, interval='day'):
    return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')

def cli(args=sys.argv[1:]):
    parser = ArgumentParser()
    parser.add_argument('query', nargs='?', help="Query to run.")
    parser.add_argument('-l', '--list', action='store_true', default=False,
                        help="List available queries.")
    parser.add_argument('-f', '--format', dest='fmt', default='table',
                        choices=all_formatters.keys(),
                        help="Format to print data in, defaults to 'table'.")
    parser.add_argument('-v', '--verbose', action='store_true', default=False,
                        help="Print the query and other debugging information.")

    args, remainder = parser.parse_known_args(args)
    if args.list:
        queries = [os.path.splitext(q)[0] for q in os.listdir(QUERY_DIR)]
        log.info('\n'.join(sorted(queries)))
        return

    if args.verbose:
        log.setLevel(logging.DEBUG)
    else:
        log.setLevel(logging.INFO)

    # Fake the context for convenience
    fake_context = {
        'branch': 'mozilla-central',
        'from_date': 'now-week',
        'to_date': 'now',
        'rev': '5b33b070378a',
        'path': 'dom/indexedDB',
        'limit': 10,
    }

    if isinstance(args.fmt, string_types):
        fmt = all_formatters[args.fmt]

    for result in run_query(args.query, **fake_context):
        data = result['data']
        if args.fmt != 'json' and 'header' in result:
            data.insert(0, result['header'])
        print(fmt(data))

if __name__ == '__main__':
    sys.exit(cli())
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# This file is part of Pristav.
# Copyright (c) 2009-2010 Ionuț Arțăriși

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

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

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

import random
from urlparse import urlparse

from django.shortcuts import (render_to_response,
                              get_object_or_404)
from django.http import Http404
from pristav.ads.models import Ad, Impression, Product, Publisher

DEFAULTSLUG = 'default'
DEFAULT_SIZE= '125x125'

def serve(request, slugpub=None, size=DEFAULT_SIZE):
    '''Serve an ad with the given slugpub and size

    see the get_ad docstring for how
    '''
    if size == None:
        size = DEFAULT_SIZE

    ad = get_ad(size)

    if request.META.has_key("HTTP_REFERER"):
        referer = request.META["HTTP_REFERER"]
        # get schema and netloc from the referer url
        referer_loc = '%s://%s/' % urlparse(referer)[:2]
    else:
        referer = None

    publisher = get_object_or_404(Publisher, slug=(slugpub or DEFAULTSLUG))

    impression = Impression.objects.create(
        ip=request.META["REMOTE_ADDR"],
        referer = referer,
        referer_netloc = referer_loc if referer else None,
        publisher = publisher.url,
        ad = ad)
    impression.save()

    return render_to_response('serve.html', {
        'ad_url' : ad.url,
        'ad_name' : ad.name,
        'ad_img_url' : ad.image.url,
        'ad_size' : ad.size.size
        })

def get_ad(size):
    '''Get the ad from the database.

    Go through each Product and get the first Ad of that Product. If the
    Product has more than one Ad, get the next one, next time we get to that
    Ad.

    Return a 404 if nothing is found
    '''

    products = Product.objects.filter(ad__size__size__exact=size).distinct()
    if not products:
        raise Http404

    product = products[Impression.objects.count() % len(products)]

    # FIXME: maybe we can make this sequential rather than random
    return random.choice(product.ad_set.filter(accepted=True).all())
<commit_msg>fix the error handling for HTTP headers handling<commit_after># -*- coding: utf-8 -*-
# This file is part of Pristav.
# Copyright (c) 2009-2010 Ionuț Arțăriși

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

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

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

import random
from urlparse import urlparse

from django.shortcuts import (render_to_response,
                              get_object_or_404)
from django.http import Http404
from pristav.ads.models import Ad, Impression, Product, Publisher

DEFAULTSLUG = 'default'
DEFAULT_SIZE= '125x125'
NOIP = '10.0.0.1'

def serve(request, slugpub=None, size=DEFAULT_SIZE):
    '''Serve an ad with the given slugpub and size

    see the get_ad docstring for how
    '''
    if size == None:
        size = DEFAULT_SIZE

    ad = get_ad(size)

    try:
        referer = request.META["HTTP_REFERER"]
        # get schema and netloc from the referer url
        referer_loc = '%s://%s/' % urlparse(referer)[:2]
    except KeyError:
        referer = None

    publisher = get_object_or_404(Publisher, slug=(slugpub or DEFAULTSLUG))

    impression = Impression.objects.create(
        ip=request.META.get("REMOTE_ADDR", NOIP),
        referer = referer,
        referer_netloc = referer_loc if referer else None,
        publisher = publisher.url,
        ad = ad)
    impression.save()

    return render_to_response('serve.html', {
        'ad_url' : ad.url,
        'ad_name' : ad.name,
        'ad_img_url' : ad.image.url,
        'ad_size' : ad.size.size
        })

def get_ad(size):
    '''Get the ad from the database.

    Go through each Product and get the first Ad of that Product. If the
    Product has more than one Ad, get the next one, next time we get to that
    Ad.

    Return a 404 if nothing is found
    '''

    products = Product.objects.filter(ad__size__size__exact=size).distinct()
    if not products:
        raise Http404

    product = products[Impression.objects.count() % len(products)]

    # FIXME: maybe we can make this sequential rather than random
    return random.choice(product.ad_set.filter(accepted=True).all())
<|endoftext|>


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

#-------------------------------------------------------------------------
# drawElements Quality Program utilities
# --------------------------------------
#
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-------------------------------------------------------------------------

import os
import sys
import shutil
import tarfile
import urllib2
import hashlib
import argparse
import subprocess

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "scripts"))

from build.common import *

EXTERNAL_DIR	= os.path.realpath(os.path.normpath(os.path.dirname(__file__)))

def computeChecksum (data):
	return hashlib.sha256(data).hexdigest()

class Source:
	def __init__(self, baseDir, extractDir):
		self.baseDir		= baseDir
		self.extractDir		= extractDir

	def clean (self):
		fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
		if os.path.exists(fullDstPath):
			shutil.rmtree(fullDstPath, ignore_errors=False)

class SourcePackage (Source):
	def __init__(self, url, filename, checksum, baseDir, extractDir = "src", postExtract=None):
		Source.__init__(self, baseDir, extractDir)
		self.url			= url
		self.filename		= filename
		self.checksum		= checksum
		self.archiveDir		= "packages"
		self.postExtract	= postExtract

	def clean (self):
		Source.clean(self)
		self.removeArchives()

	def update (self):
		if not self.isArchiveUpToDate():
			self.fetchAndVerifyArchive()

		if self.getExtractedChecksum() != self.checksum:
			Source.clean(self)
			self.extract()
			self.storeExtractedChecksum(self.checksum)

	def removeArchives (self):
		archiveDir = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir)
		if os.path.exists(archiveDir):
			shutil.rmtree(archiveDir, ignore_errors=False)

	def isArchiveUpToDate (self):
		archiveFile = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, pkg.filename)
		if os.path.exists(archiveFile):
			return computeChecksum(readFile(archiveFile)) == self.checksum
		else:
			return False

	def getExtractedChecksumFilePath (self):
		return os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, "extracted")

	def getExtractedChecksum (self):
		extractedChecksumFile = self.getExtractedChecksumFilePath()

		if os.path.exists(extractedChecksumFile):
			return readFile(extractedChecksumFile)
		else:
			return None

	def storeExtractedChecksum (self, checksum):
		writeFile(self.getExtractedChecksumFilePath(), checksum)

	def fetchAndVerifyArchive (self):
		print "Fetching %s" % self.url

		req			= urllib2.urlopen(self.url)
		data		= req.read()
		checksum	= computeChecksum(data)
		dstPath		= os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)

		if checksum != self.checksum:
			raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (self.filename, self.checksum, checksum))

		if not os.path.exists(os.path.dirname(dstPath)):
			os.mkdir(os.path.dirname(dstPath))

		writeFile(dstPath, data)

	def extract (self):
		print "Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir)

		srcPath	= os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
		tmpPath	= os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % self.baseDir)
		dstPath	= os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
		archive	= tarfile.open(srcPath)

		if os.path.exists(tmpPath):
			shutil.rmtree(tmpPath, ignore_errors=False)

		os.mkdir(tmpPath)

		archive.extractall(tmpPath)
		archive.close()

		extractedEntries = os.listdir(tmpPath)
		if len(extractedEntries) != 1 or not os.path.isdir(os.path.join(tmpPath, extractedEntries[0])):
			raise Exception("%s doesn't contain single top-level directory" % self.filename)

		topLevelPath = os.path.join(tmpPath, extractedEntries[0])

		if not os.path.exists(dstPath):
			os.mkdir(dstPath)

		for entry in os.listdir(topLevelPath):
			if os.path.exists(os.path.join(dstPath, entry)):
				raise Exception("%s exists already" % entry)

			shutil.move(os.path.join(topLevelPath, entry), dstPath)

		shutil.rmtree(tmpPath, ignore_errors=True)

		if self.postExtract != None:
			self.postExtract(dstPath)

class GitRepo (Source):
	def __init__(self, url, revision, baseDir, extractDir = "src"):
		Source.__init__(self, baseDir, extractDir)
		self.url		= url
		self.revision	= revision

	def update (self):
		fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)

		if not os.path.exists(fullDstPath):
			execute(["git", "clone", "--no-checkout", self.url, fullDstPath])

		pushWorkingDir(fullDstPath)
		try:
			execute(["git", "fetch", self.url, "+refs/heads/*:refs/remotes/origin/*"])
			execute(["git", "checkout", self.revision])
		finally:
			popWorkingDir()

def postExtractLibpng (path):
	shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
				os.path.join(path, "pnglibconf.h"))

PACKAGES = [
	SourcePackage(
		"http://zlib.net/zlib-1.2.8.tar.gz",
		"zlib-1.2.8.tar.gz",
		"36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
		"zlib"),
	SourcePackage(
		"http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz",
		"libpng-1.6.17.tar.gz",
		"a18233c99e1dc59a256180e6871d9305a42e91b3f98799b3ceb98e87e9ec5e31",
		"libpng",
		postExtract = postExtractLibpng),
	GitRepo(
		"https://github.com/KhronosGroup/SPIRV-Tools.git",
		"f18e1f293b7cf8f24a42587b48f35bc49912a5dd",
		"spirv-tools"),
	GitRepo(
		"https://github.com/KhronosGroup/glslang.git",
		"e3aa654c4b0c761b28d7864192ca8ceea6faf70a",
		"glslang"),
	GitRepo(
		"https://github.com/KhronosGroup/SPIRV-Headers.git",
		"e4e22fd832810c45f46d542c67910b0c6bf4ca2d",
		"spirv-headers"),
]

def parseArgs ():
	parser = argparse.ArgumentParser(description = "Fetch external sources")
	parser.add_argument('--clean', dest='clean', action='store_true', default=False,
						help='Remove sources instead of fetching')
	return parser.parse_args()

if __name__ == "__main__":
	args = parseArgs()

	for pkg in PACKAGES:
		if args.clean:
			pkg.clean()
		else:
			pkg.update()
<commit_msg>Update SPIR-V dependencies<commit_after># -*- coding: utf-8 -*-

#-------------------------------------------------------------------------
# drawElements Quality Program utilities
# --------------------------------------
#
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-------------------------------------------------------------------------

import os
import sys
import shutil
import tarfile
import urllib2
import hashlib
import argparse
import subprocess

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "scripts"))

from build.common import *

EXTERNAL_DIR	= os.path.realpath(os.path.normpath(os.path.dirname(__file__)))

def computeChecksum (data):
	return hashlib.sha256(data).hexdigest()

class Source:
	def __init__(self, baseDir, extractDir):
		self.baseDir		= baseDir
		self.extractDir		= extractDir

	def clean (self):
		fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
		if os.path.exists(fullDstPath):
			shutil.rmtree(fullDstPath, ignore_errors=False)

class SourcePackage (Source):
	def __init__(self, url, filename, checksum, baseDir, extractDir = "src", postExtract=None):
		Source.__init__(self, baseDir, extractDir)
		self.url			= url
		self.filename		= filename
		self.checksum		= checksum
		self.archiveDir		= "packages"
		self.postExtract	= postExtract

	def clean (self):
		Source.clean(self)
		self.removeArchives()

	def update (self):
		if not self.isArchiveUpToDate():
			self.fetchAndVerifyArchive()

		if self.getExtractedChecksum() != self.checksum:
			Source.clean(self)
			self.extract()
			self.storeExtractedChecksum(self.checksum)

	def removeArchives (self):
		archiveDir = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir)
		if os.path.exists(archiveDir):
			shutil.rmtree(archiveDir, ignore_errors=False)

	def isArchiveUpToDate (self):
		archiveFile = os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, pkg.filename)
		if os.path.exists(archiveFile):
			return computeChecksum(readFile(archiveFile)) == self.checksum
		else:
			return False

	def getExtractedChecksumFilePath (self):
		return os.path.join(EXTERNAL_DIR, pkg.baseDir, pkg.archiveDir, "extracted")

	def getExtractedChecksum (self):
		extractedChecksumFile = self.getExtractedChecksumFilePath()

		if os.path.exists(extractedChecksumFile):
			return readFile(extractedChecksumFile)
		else:
			return None

	def storeExtractedChecksum (self, checksum):
		writeFile(self.getExtractedChecksumFilePath(), checksum)

	def fetchAndVerifyArchive (self):
		print "Fetching %s" % self.url

		req			= urllib2.urlopen(self.url)
		data		= req.read()
		checksum	= computeChecksum(data)
		dstPath		= os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)

		if checksum != self.checksum:
			raise Exception("Checksum mismatch for %s, exepected %s, got %s" % (self.filename, self.checksum, checksum))

		if not os.path.exists(os.path.dirname(dstPath)):
			os.mkdir(os.path.dirname(dstPath))

		writeFile(dstPath, data)

	def extract (self):
		print "Extracting %s to %s/%s" % (self.filename, self.baseDir, self.extractDir)

		srcPath	= os.path.join(EXTERNAL_DIR, self.baseDir, self.archiveDir, self.filename)
		tmpPath	= os.path.join(EXTERNAL_DIR, ".extract-tmp-%s" % self.baseDir)
		dstPath	= os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)
		archive	= tarfile.open(srcPath)

		if os.path.exists(tmpPath):
			shutil.rmtree(tmpPath, ignore_errors=False)

		os.mkdir(tmpPath)

		archive.extractall(tmpPath)
		archive.close()

		extractedEntries = os.listdir(tmpPath)
		if len(extractedEntries) != 1 or not os.path.isdir(os.path.join(tmpPath, extractedEntries[0])):
			raise Exception("%s doesn't contain single top-level directory" % self.filename)

		topLevelPath = os.path.join(tmpPath, extractedEntries[0])

		if not os.path.exists(dstPath):
			os.mkdir(dstPath)

		for entry in os.listdir(topLevelPath):
			if os.path.exists(os.path.join(dstPath, entry)):
				raise Exception("%s exists already" % entry)

			shutil.move(os.path.join(topLevelPath, entry), dstPath)

		shutil.rmtree(tmpPath, ignore_errors=True)

		if self.postExtract != None:
			self.postExtract(dstPath)

class GitRepo (Source):
	def __init__(self, url, revision, baseDir, extractDir = "src"):
		Source.__init__(self, baseDir, extractDir)
		self.url		= url
		self.revision	= revision

	def update (self):
		fullDstPath = os.path.join(EXTERNAL_DIR, self.baseDir, self.extractDir)

		if not os.path.exists(fullDstPath):
			execute(["git", "clone", "--no-checkout", self.url, fullDstPath])

		pushWorkingDir(fullDstPath)
		try:
			execute(["git", "fetch", self.url, "+refs/heads/*:refs/remotes/origin/*"])
			execute(["git", "checkout", self.revision])
		finally:
			popWorkingDir()

def postExtractLibpng (path):
	shutil.copy(os.path.join(path, "scripts", "pnglibconf.h.prebuilt"),
				os.path.join(path, "pnglibconf.h"))

PACKAGES = [
	SourcePackage(
		"http://zlib.net/zlib-1.2.8.tar.gz",
		"zlib-1.2.8.tar.gz",
		"36658cb768a54c1d4dec43c3116c27ed893e88b02ecfcb44f2166f9c0b7f2a0d",
		"zlib"),
	SourcePackage(
		"http://prdownloads.sourceforge.net/libpng/libpng-1.6.17.tar.gz",
		"libpng-1.6.17.tar.gz",
		"a18233c99e1dc59a256180e6871d9305a42e91b3f98799b3ceb98e87e9ec5e31",
		"libpng",
		postExtract = postExtractLibpng),
	GitRepo(
		"https://github.com/KhronosGroup/SPIRV-Tools.git",
		"38036a7203ca604c0f25b0dc33da169ac58e2900",
		"spirv-tools"),
	GitRepo(
		"https://github.com/KhronosGroup/glslang.git",
		"e3aa654c4b0c761b28d7864192ca8ceea6faf70a",
		"glslang"),
	GitRepo(
		"https://github.com/KhronosGroup/SPIRV-Headers.git",
		"bd47a9abaefac00be692eae677daed1b977e625c",
		"spirv-headers"),
]

def parseArgs ():
	parser = argparse.ArgumentParser(description = "Fetch external sources")
	parser.add_argument('--clean', dest='clean', action='store_true', default=False,
						help='Remove sources instead of fetching')
	return parser.parse_args()

if __name__ == "__main__":
	args = parseArgs()

	for pkg in PACKAGES:
		if args.clean:
			pkg.clean()
		else:
			pkg.update()
<|endoftext|>


<commit_before>"""Model an algorithm as a list of functions.

Installation
------------

:py:mod:`algorithm` is available on `GitHub`_ and on `PyPI`_::

    $ pip install algorithm

We `test <https://travis-ci.org/gittip/algorithm.py>`_ against
Python 2.6, 2.7, 3.2, and 3.3.

:py:mod:`algorithm` is in the `public domain`_.

.. _GitHub: https://github.com/gittip/algorithm.py
.. _PyPI: https://pypi.python.org/pypi/algorithm
.. _public domain: http://creativecommons.org/publicdomain/zero/1.0/

Tutorial
--------

This module provides an abstraction for implementing arbitrary algorithms as a
list of functions that operate on a shared state dictionary. Algorithms defined
this way are easy to arbitrarily modify at run time, and they provide cascading
exception handling.

To get started, define an algorithm by defining a series of functions in a
Python file::

    def foo():
        return {'baz': 1}

    def bar():
        return {'buz': 2}

    def bloo(baz, buz):
        return {'sum': baz + buz)

Each function returns a :py:class:`dict`, which is used to update the state of
the current run of the algorithm. Names from the state dictionary are made
available to downstream functions via :py:mod:`dependency_injection`. Save this
file on your ``PYTHONPATH`` as ``blah_algorithm.py``. Now here's how to use it:

    >>> from algorithm import Algorithm
    >>> blah = Algorithm('blah_algorithm')

When you instantiate :py:class:`Algorithm` you give it the dotted path to a
Python module. All of the functions defined in the module are loaded into a
list, in the order they're defined in the file:

    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function bloo ...>]

Now you can use :py:func:`~Algorithm.run` to run the algorithm. You'll get back
a dictionary representing the algorithm's final state:

    >>> state = blah.run()
    >>> state['sum']
    3

Let's add two functions to the algorithm, to illustrate both algorithm
modification and exception handling. First let's define the functions:

    >>> def uh_oh(baz):
    ...     if baz == 2:
    ...         raise heck
    ...
    >>> def deal_with_it(exc_info):
    ...     print(exc_info[1])
    ...     return {'exc_info': None}
    ...

Now let's interpolate them into our algorithm. Let's put the ``uh_oh`` function between
``bar`` and ``bloo``:

    >>> blah.insert_before('bloo', uh_oh)
    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function uh_oh ...>, <function bloo ...>]

Then let's add our exception handler at the end:

    >>> blah.insert_after('bloo', deal_with_it)
    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function uh_oh ...>, <function bloo ...>, <function deal_with_it ...>]

Just for kicks, let's remove the ``foo`` function while we're at it:

    >>> blah.remove('foo')
    >>> blah.functions #doctest: +ELLIPSIS
    [<function bar ...>, <function uh_oh ...>, <function bloo ...>, <function deal_with_it ...>]

What happens when we run it? Since we no longer have the ``foo`` function
providing a value for ``bar``, we'll need to supply that using a keyword
argument to :py:func:`~Algorithm.run`:

    >>> state = blah.run(baz=2)
    global name 'heck' is not defined

The ``global name`` print statement came from our ``deal_with_it`` function.
Whenever a function raises an exception, like ``uh_oh`` did,
:py:class:`~Algorithm.run` captures the exception and populates an ``exc_info``
key in the current algorithm run state dictionary. While ``exc_info`` is not
``None``, any normal function is skipped, and only functions that ask for
``exc_info`` get called. So in our example ``deal_with_it`` got called, but
``bloo`` didn't, which is why there is no ``sum``:

    >>> 'sum' in state
    False

If we run without tripping the exception in ``uh_oh`` then we have ``sum`` at
the end:

    >>> blah.run(baz=5)['sum']
    7

API Reference
-------------

"""
from __future__ import absolute_import, division, print_function, unicode_literals

import re
import sys
import types
import traceback

from dependency_injection import resolve_dependencies

if sys.version_info >= (3, 0, 0):

    _get_func_code = lambda f: f.__code__
    _get_func_name = lambda f: f.__name__

    def _transfer_func_name(to, from_):
        to.__name__ = from_.__name__

    def exec_(some_python, namespace):
        exec(some_python, namespace)
else:
    _get_func_code = lambda f: f.func_code
    _get_func_name = lambda f: f.func_name

    def _transfer_func_name(to, from_):
        to.func_name = from_.func_name

    def exec_(some_python, namespace):
        # Have to double-exec because the Python 2 form is SyntaxError in 3.
        exec("exec some_python in namespace")

class FunctionNotFound(Exception):
    def __str__(self):
        return "The function '{0}' isn't in this algorithm.".format(*self.args)

class Algorithm(object):
    """Model an algorithm as a list of functions.

    :param dotted_name: The dotted name of a Python module containing the
        algorithm definition.

    An algorithm definition is a regular Python file. All functions defined in
    the file whose name doesn't begin with ``_`` are loaded into a list at
    :py:attr:`functions` in the order they're defined in the file.

    Each function in your algorithm must return a mapping or :py:class:`None`.
    If it returns a mapping, the mapping will be used to update a state
    dictionary for the current run of the algorithm. Functions in the algorithm
    can use any name from the current state dictionary as a parameter, and the
    value will then be supplied dynamically via :py:mod:`dependency_injection`.
    See the `tutorial`_ for an example.

    """

    short_circuit = False

    def __init__(self, dotted_name):
        self.module = self._load_module_from_dotted_name(dotted_name)
        self.functions = self._load_functions_from_module(self.module) #: A list of functions.

    def __iter__(self):
        return iter(self.functions)

    def get_names(self):
        return [_get_func_name(f) for f in self]

    def insert_after(self, name, newfunc):
        """Insert ``newfunc`` in the :py:attr:`functions` list right after the function named
        ``name``.
        """
        self.insert_relative_to(name, newfunc, relative_position=1)

    def insert_before(self, name, newfunc):
        """Insert ``newfunc`` in the :py:attr:`functions` list right before the function named
        ``name``.
        """
        self.insert_relative_to(name, newfunc, relative_position=0)

    def insert_relative_to(self, name, newfunc, relative_position):
        func = self.get_function(name)
        index = self.functions.index(func) + relative_position
        self.functions.insert(index, newfunc)

    def remove(self, name):
        """Remove the function named ``name`` from the :py:attr:`functions` list.
        """
        func = self.get_function(name)
        self.functions.remove(func)

    def get_function(self, name):
        """Given the name of a function in the :py:attr:`functions` list, return the function
        itself.
        """
        func = None
        for func in self.functions:
            if _get_func_name(func) == name:
                break
        if func is None:
            raise FunctionNotFound(name)
        return func

    def run(self, _through=None, **state):
        """Given a state dictionary, run through the functions in the list.
        """
        if _through is not None:
            if _through not in self.get_names():
                raise FunctionNotFound(_through)
        # XXX bring these back when we've sorted out logging
        #print()

        if 'algorithm' not in state:    state['algorithm'] = self
        if 'state' not in state:        state['state'] = state
        if 'exc_info' not in state:     state['exc_info'] = None

        for function in self.functions:
            function_name = _get_func_name(function)
            try:
                deps = resolve_dependencies(function, state)
                if 'exc_info' in deps.signature.required and state['exc_info'] is None:
                    pass    # Hook needs an exc_info but we don't have it.
                    #print("{0:>48}  \x1b[33;1mskipped\x1b[0m".format(function_name))
                elif 'exc_info' not in deps.signature.parameters and state['exc_info'] is not None:
                    pass    # Hook doesn't want an exc_info but we have it.
                    #print("{0:>48}  \x1b[33;1mskipped\x1b[0m".format(function_name))
                else:
                    new_state = function(**deps.as_kwargs)
                    #print("{0:>48}  \x1b[32;1mdone\x1b[0m".format(function_name))
                    if new_state is not None:
                        state.update(new_state)
            except:
                #print("{0:>48}  \x1b[31;1mfailed\x1b[0m".format(function_name))
                state['exc_info'] = sys.exc_info()[:2] + (traceback.format_exc().strip(),)
                if self.short_circuit:
                    raise

            if _through is not None and function_name == _through:
                break

        if state['exc_info'] is not None:
            raise

        return state

    # Helpers for loading from a file.
    # ================================

    def _load_module_from_dotted_name(self, dotted_name):
        class RootModule(object): pass
        module = RootModule()  # let's us use getattr to traverse down
        exec_('import {0}'.format(dotted_name), module.__dict__)
        for name in dotted_name.split('.'):
            module = getattr(module, name)
        return module

    def _load_functions_from_module(self, module):
        """Given a module object, return a list of functions from the module, sorted by lineno.
        """
        functions_with_lineno = []
        for name in dir(module):
            if name.startswith('_'):
                continue
            obj = getattr(module, name)
            if type(obj) != types.FunctionType:
                continue
            func = obj
            lineno = _get_func_code(func).co_firstlineno
            functions_with_lineno.append((lineno, func))
        functions_with_lineno.sort()
        return [func for lineno, func in functions_with_lineno]

# Filters
# =======

def by_lambda(filter_lambda):
    def wrap(flow_step):
        def wrapped_flow_step_by_lambda(*args,**kwargs):
            if filter_lambda():
                return flow_step(*args,**kwargs)
        _transfer_func_name(wrapped_flow_step_by_lambda, flow_step)
        return wrapped_flow_step_by_lambda
    return wrap

def by_regex(regex_tuples, default=True):
    """A filter for flow steps.

    regex_tuples is a list of (regex, filter?) where if the regex matches the
    requested URI, then the flow is applied or not based on if filter? is True
    or False.

    For example:

        from aspen.flows.filter import by_regex

        @by_regex( ( ("/secret/agenda", True), ( "/secret.*", False ) ) )
        def use_public_formatting(request):
            ...

    would call the 'use_public_formatting' flow step only on /secret/agenda
    and any other URLs not starting with /secret.

    """
    regex_res = [ (re.compile(regex), disposition) \
                           for regex, disposition in regex_tuples.iteritems() ]
    def filter_flow_step(flow_step):
        def flow_step_filter(request, *args):
            for regex, disposition in regex_res:
                if regex.matches(request.line.uri):
                    if disposition:
                        return flow_step(*args)
            if default:
                return flow_step(*args)
        _transfer_func_name(flow_step_filter, flow_step)
        return flow_step_filter
    return filter_flow_step

def by_dict(truthdict, default=True):
    """A filter for hooks.

    truthdict is a mapping of URI -> filter? where if the requested URI is a
    key in the dict, then the hook is applied based on the filter? value.

    """
    def filter_flow_step(flow_step):
        def flow_step_filter(request, *args):
            do_hook = truthdict.get(request.line.uri, default)
            if do_hook:
                return flow_step(*args)
        flow_step_filter.func_name = flow_step.func_name
        return flow_step_filter
    return filter_flow_step

if __name__ == '__main__':
    import doctest
    doctest.testmod()
<commit_msg>Start really documenting run<commit_after>"""Model an algorithm as a list of functions.

Installation
------------

:py:mod:`algorithm` is available on `GitHub`_ and on `PyPI`_::

    $ pip install algorithm

We `test <https://travis-ci.org/gittip/algorithm.py>`_ against
Python 2.6, 2.7, 3.2, and 3.3.

:py:mod:`algorithm` is in the `public domain`_.

.. _GitHub: https://github.com/gittip/algorithm.py
.. _PyPI: https://pypi.python.org/pypi/algorithm
.. _public domain: http://creativecommons.org/publicdomain/zero/1.0/

Tutorial
--------

This module provides an abstraction for implementing arbitrary algorithms as a
list of functions that operate on a shared state dictionary. Algorithms defined
this way are easy to arbitrarily modify at run time, and they provide cascading
exception handling.

To get started, define an algorithm by defining a series of functions in a
Python file::

    def foo():
        return {'baz': 1}

    def bar():
        return {'buz': 2}

    def bloo(baz, buz):
        return {'sum': baz + buz)

Each function returns a :py:class:`dict`, which is used to update the state of
the current run of the algorithm. Names from the state dictionary are made
available to downstream functions via :py:mod:`dependency_injection`. Save this
file on your ``PYTHONPATH`` as ``blah_algorithm.py``. Now here's how to use it:

    >>> from algorithm import Algorithm
    >>> blah = Algorithm('blah_algorithm')

When you instantiate :py:class:`Algorithm` you give it the dotted path to a
Python module. All of the functions defined in the module are loaded into a
list, in the order they're defined in the file:

    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function bloo ...>]

Now you can use :py:func:`~Algorithm.run` to run the algorithm. You'll get back
a dictionary representing the algorithm's final state:

    >>> state = blah.run()
    >>> state['sum']
    3

Let's add two functions to the algorithm, to illustrate both algorithm
modification and exception handling. First let's define the functions:

    >>> def uh_oh(baz):
    ...     if baz == 2:
    ...         raise heck
    ...
    >>> def deal_with_it(exc_info):
    ...     print(exc_info[1])
    ...     return {'exc_info': None}
    ...

Now let's interpolate them into our algorithm. Let's put the ``uh_oh`` function between
``bar`` and ``bloo``:

    >>> blah.insert_before('bloo', uh_oh)
    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function uh_oh ...>, <function bloo ...>]

Then let's add our exception handler at the end:

    >>> blah.insert_after('bloo', deal_with_it)
    >>> blah.functions #doctest: +ELLIPSIS
    [<function foo ...>, <function bar ...>, <function uh_oh ...>, <function bloo ...>, <function deal_with_it ...>]

Just for kicks, let's remove the ``foo`` function while we're at it:

    >>> blah.remove('foo')
    >>> blah.functions #doctest: +ELLIPSIS
    [<function bar ...>, <function uh_oh ...>, <function bloo ...>, <function deal_with_it ...>]

What happens when we run it? Since we no longer have the ``foo`` function
providing a value for ``bar``, we'll need to supply that using a keyword
argument to :py:func:`~Algorithm.run`:

    >>> state = blah.run(baz=2)
    global name 'heck' is not defined

The ``global name`` print statement came from our ``deal_with_it`` function.
Whenever a function raises an exception, like ``uh_oh`` did,
:py:class:`~Algorithm.run` captures the exception and populates an ``exc_info``
key in the current algorithm run state dictionary. While ``exc_info`` is not
``None``, any normal function is skipped, and only functions that ask for
``exc_info`` get called. So in our example ``deal_with_it`` got called, but
``bloo`` didn't, which is why there is no ``sum``:

    >>> 'sum' in state
    False

If we run without tripping the exception in ``uh_oh`` then we have ``sum`` at
the end:

    >>> blah.run(baz=5)['sum']
    7

API Reference
-------------

"""
from __future__ import absolute_import, division, print_function, unicode_literals

import re
import sys
import types
import traceback

from dependency_injection import resolve_dependencies

if sys.version_info >= (3, 0, 0):

    _get_func_code = lambda f: f.__code__
    _get_func_name = lambda f: f.__name__

    def _transfer_func_name(to, from_):
        to.__name__ = from_.__name__

    def exec_(some_python, namespace):
        exec(some_python, namespace)
else:
    _get_func_code = lambda f: f.func_code
    _get_func_name = lambda f: f.func_name

    def _transfer_func_name(to, from_):
        to.func_name = from_.func_name

    def exec_(some_python, namespace):
        # Have to double-exec because the Python 2 form is SyntaxError in 3.
        exec("exec some_python in namespace")

class FunctionNotFound(Exception):
    """Used when a function is not found in an algorithm function list.
    """
    def __str__(self):
        return "The function '{0}' isn't in this algorithm.".format(*self.args)

class Algorithm(object):
    """Model an algorithm as a list of functions.

    :param dotted_name: The dotted name of a Python module containing the
        algorithm definition.

    An algorithm definition is a regular Python file. All functions defined in
    the file whose name doesn't begin with ``_`` are loaded into a list at
    :py:attr:`functions` in the order they're defined in the file.

    Each function in your algorithm must return a mapping or :py:class:`None`.
    If it returns a mapping, the mapping will be used to update a state
    dictionary for the current run of the algorithm. Functions in the algorithm
    can use any name from the current state dictionary as a parameter, and the
    value will then be supplied dynamically via :py:mod:`dependency_injection`.
    See the `tutorial`_ for an example, and see the :py:func:`run` method for
    details on exception handling.

    """

    short_circuit = False #: Whether to raise exceptions immediately.

    def __init__(self, dotted_name):
        self.module = self._load_module_from_dotted_name(dotted_name)
        self.functions = self._load_functions_from_module(self.module) #: A list of functions.

    def __iter__(self):
        return iter(self.functions)

    def get_names(self):
        """Returns a list of the names of the functions in the :py:attr:`functions` list.
        """
        return [_get_func_name(f) for f in self]

    def get_function(self, name):
        """Return the function in the :py:attr:`functions` list named ``name``, or raise
        :py:exc:`FunctionNotFound`.
        """
        func = None
        for func in self.functions:
            if _get_func_name(func) == name:
                break
        if func is None:
            raise FunctionNotFound(name)
        return func

    def insert_after(self, name, newfunc):
        """Insert ``newfunc`` in the :py:attr:`functions` list after the function named
        ``name``, or raise :py:exc:`FunctionNotFound`.
        """
        self.insert_relative_to(name, newfunc, relative_position=1)

    def insert_before(self, name, newfunc):
        """Insert ``newfunc`` in the :py:attr:`functions` list before the function named
        ``name``, or raise :py:exc:`FunctionNotFound`.
        """
        self.insert_relative_to(name, newfunc, relative_position=0)

    def insert_relative_to(self, name, newfunc, relative_position):
        func = self.get_function(name)
        index = self.functions.index(func) + relative_position
        self.functions.insert(index, newfunc)

    def remove(self, name):
        """Remove the function named ``name`` from the :py:attr:`functions` list, or raise
        :py:exc:`FunctionNotFound`.
        """
        func = self.get_function(name)
        self.functions.remove(func)

    def run(self, _through=None, **state):
        """Run through the functions in the :py:attr:`functions` list.

        :param _through: if not ``None``, return after reaching the function
            with this name

        :param state: remaining keyword arguments are used for the initial
            state dictionary for this run of the algorithm

        :raises: :py:exc:`FunctionNotFound`, if there is no function named
            ``_through``

        :returns: a dictionary representing the final algorithm state

        The state dictionary is initialized with three items (their default
        values can be overriden using keyword arguments to :py:func:`run`):

         - ``algorithm`` - a reference to the parent :py:class:`Algorithm` instance
         - ``state`` - a circular reference to the state dictionary
         - ``exc_info`` - ``None``

        For each function in the :py:attr:`functions` list, we look at the
        function signature and compare it to the current value of ``exc_info``
        in the state dictionary. If ``exc_info`` is ``None`` then we skip any
        function that asks for ``exc_info``, and if ``exc_info`` is *not*
        ``None`` then we only call functions that *do* ask for it. The upshot
        is that any function that raises an exception will cause us to
        fast-forward to the next exception-handling function in the list.

        """
        if _through is not None:
            if _through not in self.get_names():
                raise FunctionNotFound(_through)
        # XXX bring these back when we've sorted out logging
        #print()

        if 'algorithm' not in state:    state['algorithm'] = self
        if 'state' not in state:        state['state'] = state
        if 'exc_info' not in state:     state['exc_info'] = None

        for function in self.functions:
            function_name = _get_func_name(function)
            try:
                deps = resolve_dependencies(function, state)
                if 'exc_info' in deps.signature.required and state['exc_info'] is None:
                    pass    # Hook needs an exc_info but we don't have it.
                    #print("{0:>48}  \x1b[33;1mskipped\x1b[0m".format(function_name))
                elif 'exc_info' not in deps.signature.parameters and state['exc_info'] is not None:
                    pass    # Hook doesn't want an exc_info but we have it.
                    #print("{0:>48}  \x1b[33;1mskipped\x1b[0m".format(function_name))
                else:
                    new_state = function(**deps.as_kwargs)
                    #print("{0:>48}  \x1b[32;1mdone\x1b[0m".format(function_name))
                    if new_state is not None:
                        state.update(new_state)
            except:
                #print("{0:>48}  \x1b[31;1mfailed\x1b[0m".format(function_name))
                state['exc_info'] = sys.exc_info()[:2] + (traceback.format_exc().strip(),)
                if self.short_circuit:
                    raise

            if _through is not None and function_name == _through:
                break

        if state['exc_info'] is not None:
            raise

        return state

    # Helpers for loading from a file.
    # ================================

    def _load_module_from_dotted_name(self, dotted_name):
        class RootModule(object): pass
        module = RootModule()  # let's us use getattr to traverse down
        exec_('import {0}'.format(dotted_name), module.__dict__)
        for name in dotted_name.split('.'):
            module = getattr(module, name)
        return module

    def _load_functions_from_module(self, module):
        """Given a module object, return a list of functions from the module, sorted by lineno.
        """
        functions_with_lineno = []
        for name in dir(module):
            if name.startswith('_'):
                continue
            obj = getattr(module, name)
            if type(obj) != types.FunctionType:
                continue
            func = obj
            lineno = _get_func_code(func).co_firstlineno
            functions_with_lineno.append((lineno, func))
        functions_with_lineno.sort()
        return [func for lineno, func in functions_with_lineno]

# Filters
# =======

def by_lambda(filter_lambda):
    def wrap(flow_step):
        def wrapped_flow_step_by_lambda(*args,**kwargs):
            if filter_lambda():
                return flow_step(*args,**kwargs)
        _transfer_func_name(wrapped_flow_step_by_lambda, flow_step)
        return wrapped_flow_step_by_lambda
    return wrap

def by_regex(regex_tuples, default=True):
    """A filter for flow steps.

    regex_tuples is a list of (regex, filter?) where if the regex matches the
    requested URI, then the flow is applied or not based on if filter? is True
    or False.

    For example:

        from aspen.flows.filter import by_regex

        @by_regex( ( ("/secret/agenda", True), ( "/secret.*", False ) ) )
        def use_public_formatting(request):
            ...

    would call the 'use_public_formatting' flow step only on /secret/agenda
    and any other URLs not starting with /secret.

    """
    regex_res = [ (re.compile(regex), disposition) \
                           for regex, disposition in regex_tuples.iteritems() ]
    def filter_flow_step(flow_step):
        def flow_step_filter(request, *args):
            for regex, disposition in regex_res:
                if regex.matches(request.line.uri):
                    if disposition:
                        return flow_step(*args)
            if default:
                return flow_step(*args)
        _transfer_func_name(flow_step_filter, flow_step)
        return flow_step_filter
    return filter_flow_step

def by_dict(truthdict, default=True):
    """A filter for hooks.

    truthdict is a mapping of URI -> filter? where if the requested URI is a
    key in the dict, then the hook is applied based on the filter? value.

    """
    def filter_flow_step(flow_step):
        def flow_step_filter(request, *args):
            do_hook = truthdict.get(request.line.uri, default)
            if do_hook:
                return flow_step(*args)
        flow_step_filter.func_name = flow_step.func_name
        return flow_step_filter
    return filter_flow_step

if __name__ == '__main__':
    import doctest
    doctest.testmod()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2018 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import api, models, fields
from openerp.addons.l10n_br_hr_payroll.models.hr_payslip import MES_DO_ANO

class HrTelefonia(models.Model):
    _name = 'hr.telefonia'
    _rec_name = 'display_name'

    def _get_display_name(self):
        for record in self:
            display_name = "Registros Telefônicos - {}/{}".format(
                record.mes, record.ano
            )
            record.display_name = display_name

    display_name = fields.Char(
        compute='_get_display_name'
    )

    arquivo_ligacoes = fields.Binary(
        string='Arquivo de retorno',
        filters='*.csv',
        require=True,
        copy=False,
    )

    arquivo_ramais = fields.Binary(
        string='Listagem de Ramais',
        filters='*.csv',
        require=True,
        copy=False,
    )

    mes = fields.Selection(
        string=u'Mês Competência',
        selection=MES_DO_ANO,
        require=True,
    )

    ano = fields.Char(
        string=u'Ano Competência',
        required=True,
        size=4,
    )

    ligacoes_id = fields.One2many(
        comodel_name='hr.telefonia.line',
        inverse_name='registro_telefonico_id',
    )

    @api.multi
    def button_buscar_dono_ligacao(self):

        ligacoes_sem_dono_id = self.env['hr.telefonia.line'].search([('employee_id','=',False)])

        for ligacoes_id in ligacoes_sem_dono_id:

            funcionario_id = self.env['hr.employee'].search([('ramais', '=', ligacoes_id.ramal.name)])

            if len(funcionario_id) == 1:
                ligacoes_id.employee_id = funcionario_id

    @api.multi
    def button_import_ramais(self):

        ramal_obj = self.env['hr.ramal']

        for record in self:
            if record.arquivo_ramais:

                # import csv
                import base64

                arq = base64.b64decode(record.arquivo_ramais)
                linhas = arq.splitlines(True)

                for linha in linhas:

                    l = linha.split(',')

                    email_ramal = l[1].strip()
                    numero_ramal = l[2].strip('\n')

                    funcionario_id = self.env['hr.employee'].search([('work_email','=',email_ramal)])

                    if funcionario_id:

                        ramal_id = ramal_obj.search([('name','=', numero_ramal)])

                        if not numero_ramal in funcionario_id.ramais.mapped('name'):

                            if not ramal_id:
                                ramal_id = ramal_obj.create({'name': numero_ramal})

                            funcionario_id.ramais = [(4, ramal_id.id)]

                    else:
                        print ("Nao encontrado funcionario: " + email_ramal)

    @api.multi
    def button_importar_csv(self):

        ramal_obj = self.env['hr.ramal']

        for record in self:
            if record.arquivo_ligacoes:

                # import csv
                import base64

                arq = base64.b64decode(record.arquivo_ligacoes)
                linhas = arq.splitlines(True)

                for linha in linhas:

                    l = linha.split(';')

                    if len(l) > 7 and len(l[0]) == 4:
                        name_ramal = l[0]
                        ramal_id = ramal_obj.search([('name', '=', name_ramal)])

                        if not ramal_id:
                            ramal_id = ramal_obj.create({'name': name_ramal})

                        funcionario_id = self.env['hr.employee'].search([('ramais', '=', name_ramal)])

                        data = l[1]
                        numero_discado = l[2]
                        concessionaria = l[3]
                        localidade = l[4]
                        inicio = l[5]
                        duracao = l[6]
                        valor = float(l[7].replace(',','.'))

                        vals = {
                            'ramal': ramal_id.id,
                            'data': data,
                            'numero_discado': numero_discado,
                            'concessionaria': concessionaria,
                            'localidade': localidade,
                            'inicio': inicio,
                            'duracao': duracao,
                            'valor': valor,
                            'registro_telefonico_id': record.id,
                            'employee_id': funcionario_id.id if len(funcionario_id) == 1 else False
                        }

                        self.env['hr.telefonia.line'].create(vals)

class HrTelefoniaLine(models.Model):
    _name = 'hr.telefonia.line'
    _rec_name = 'display_name'

    @api.multi
    def _get_telefonia_line_name(self):
        for record in self:
            title = '{} - {}{}'.format(
                record.ramal.name,
                record.employee_id.name + ' - ' if record.employee_id else '',
                record.data
            )
            record.display_name = title

    display_name = fields.Char(
        compute='_get_telefonia_line_name'
    )

    name = fields.Char(
        compute='compute_name',
        string=u'Descrição',
        store=True,
    )

    ramal = fields.Many2one(
        string='Ramal',
        comodel_name='hr.ramal',
        required=True,
        readonly=True,
    )

    employee_id = fields.Many2one(
        string='Empregado',
        comodel_name='hr.employee',
    )

    valor = fields.Float(
        string='Valor',
        readonly=True,
    )

    data = fields.Date(
        string='Data',
        required=True,
        readonly=True,
    )

    tipo = fields.Selection(
        string='Tipo',
        selection=[
            ('', ''),
            ('particular', 'Particular'),
            ('empresa', 'Empresa')
        ],
        default='empresa',
        # states={'validate': [('readonly', True)]},
    )

    state = fields.Selection(
        string=u'Situação',
        selection=[
            ('open', 'Em aberto'),
            ('validate', 'Atestado'),
            ('paid', 'Debitado'),
        ],
        default='open',
    )

    payslip_id = fields.Many2one(
        comodel_name='hr.payslip',
        string='Holerite',
    )

    registro_telefonico_id = fields.Many2one(
        string='Registro Telefonico',
        comodel_name='hr.telefonia',
        required=True,
        # states={'validate': [('readonly', True)]},
    )

    concessionaria = fields.Char(
        string='Concessionária',
        # states={'validate': [('readonly', True)]},
    )

    localidade = fields.Char(
        string='Localidade',
        readonly=True,
    )

    hora_inicio = fields.Datetime(
        string='Hora de Início',
        # states={'validate': [('readonly', True)]},
    )

    inicio = fields.Char(
        string='Inicio',
        required=True,
        readonly=True,
    )

    duracao = fields.Char(
        string='Duração da ligação',
        required=True,
        readonly=True,
    )

    numero_discado = fields.Char(
        string='Numero Discado',
        readonly=True,
    )

    @api.multi
    @api.depends('ramal', 'employee_id')
    def compute_name(self):
        for record in self:
            if record.employee_id and record.ramal:
                record.name = 'Ligação Ramal: {} '.format(record.ramal.name)

    @api.multi
    def set_validate_ligacoes(self):
        """
        Rotina para atestar ligacoes como particulares ou nao
        depois dessa rotina a ligacao sera bloqueada para edicoes
        :return:
        """
        for record in self:
            # Atesta as ligacoes
            record.state = 'validate'

    @api.multi
    def set_particular(self):
        """
        Setar as ligações para particular
        :return:
        """
        for record in self:
            # record.particular = True
            record.tipo = 'particular'

    @api.multi
    def set_empresa(self):
        """
        Setar as ligações como ligações da empresa
        :return:
        """
        for record in self:
            # record.particular = False
            record.tipo = 'empresa'
<commit_msg>[FIX] Renomear string<commit_after># -*- coding: utf-8 -*-
# Copyright 2018 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import api, models, fields
from openerp.addons.l10n_br_hr_payroll.models.hr_payslip import MES_DO_ANO

class HrTelefonia(models.Model):
    _name = 'hr.telefonia'
    _rec_name = 'display_name'

    def _get_display_name(self):
        for record in self:
            display_name = "Registros Telefônicos - {}/{}".format(
                record.mes, record.ano
            )
            record.display_name = display_name

    display_name = fields.Char(
        compute='_get_display_name'
    )

    arquivo_ligacoes = fields.Binary(
        string='Arquivo Telefonia (PABX)',
        filters='*.csv',
        require=True,
        copy=False,
    )

    arquivo_ramais = fields.Binary(
        string='Listagem de Ramais',
        filters='*.csv',
        require=True,
        copy=False,
    )

    mes = fields.Selection(
        string=u'Mês Competência',
        selection=MES_DO_ANO,
        require=True,
    )

    ano = fields.Char(
        string=u'Ano Competência',
        required=True,
        size=4,
    )

    ligacoes_id = fields.One2many(
        comodel_name='hr.telefonia.line',
        inverse_name='registro_telefonico_id',
    )

    @api.multi
    def button_buscar_dono_ligacao(self):

        ligacoes_sem_dono_id = self.env['hr.telefonia.line'].search([('employee_id','=',False)])

        for ligacoes_id in ligacoes_sem_dono_id:

            funcionario_id = self.env['hr.employee'].search([('ramais', '=', ligacoes_id.ramal.name)])

            if len(funcionario_id) == 1:
                ligacoes_id.employee_id = funcionario_id

    @api.multi
    def button_import_ramais(self):

        ramal_obj = self.env['hr.ramal']

        for record in self:
            if record.arquivo_ramais:

                # import csv
                import base64

                arq = base64.b64decode(record.arquivo_ramais)
                linhas = arq.splitlines(True)

                for linha in linhas:

                    l = linha.split(',')

                    email_ramal = l[1].strip()
                    numero_ramal = l[2].strip('\n')

                    funcionario_id = self.env['hr.employee'].search([('work_email','=',email_ramal)])

                    if funcionario_id:

                        ramal_id = ramal_obj.search([('name','=', numero_ramal)])

                        if not numero_ramal in funcionario_id.ramais.mapped('name'):

                            if not ramal_id:
                                ramal_id = ramal_obj.create({'name': numero_ramal})

                            funcionario_id.ramais = [(4, ramal_id.id)]

                    else:
                        print ("Nao encontrado funcionario: " + email_ramal)

    @api.multi
    def button_importar_csv(self):

        ramal_obj = self.env['hr.ramal']

        for record in self:
            if record.arquivo_ligacoes:

                # import csv
                import base64

                arq = base64.b64decode(record.arquivo_ligacoes)
                linhas = arq.splitlines(True)

                for linha in linhas:

                    l = linha.split(';')

                    if len(l) > 7 and len(l[0]) == 4:
                        name_ramal = l[0]
                        ramal_id = ramal_obj.search([('name', '=', name_ramal)])

                        if not ramal_id:
                            ramal_id = ramal_obj.create({'name': name_ramal})

                        funcionario_id = self.env['hr.employee'].search([('ramais', '=', name_ramal)])

                        data = l[1]
                        numero_discado = l[2]
                        concessionaria = l[3]
                        localidade = l[4]
                        inicio = l[5]
                        duracao = l[6]
                        valor = float(l[7].replace(',','.'))

                        vals = {
                            'ramal': ramal_id.id,
                            'data': data,
                            'numero_discado': numero_discado,
                            'concessionaria': concessionaria,
                            'localidade': localidade,
                            'inicio': inicio,
                            'duracao': duracao,
                            'valor': valor,
                            'registro_telefonico_id': record.id,
                            'employee_id': funcionario_id.id if len(funcionario_id) == 1 else False
                        }

                        self.env['hr.telefonia.line'].create(vals)

class HrTelefoniaLine(models.Model):
    _name = 'hr.telefonia.line'
    _rec_name = 'display_name'

    @api.multi
    def _get_telefonia_line_name(self):
        for record in self:
            title = '{} - {}{}'.format(
                record.ramal.name,
                record.employee_id.name + ' - ' if record.employee_id else '',
                record.data
            )
            record.display_name = title

    display_name = fields.Char(
        compute='_get_telefonia_line_name'
    )

    name = fields.Char(
        compute='compute_name',
        string=u'Descrição',
        store=True,
    )

    ramal = fields.Many2one(
        string='Ramal',
        comodel_name='hr.ramal',
        required=True,
        readonly=True,
    )

    employee_id = fields.Many2one(
        string='Empregado',
        comodel_name='hr.employee',
    )

    valor = fields.Float(
        string='Valor',
        readonly=True,
    )

    data = fields.Date(
        string='Data',
        required=True,
        readonly=True,
    )

    tipo = fields.Selection(
        string='Tipo',
        selection=[
            ('', ''),
            ('particular', 'Particular'),
            ('empresa', 'Empresa')
        ],
        default='empresa',
        # states={'validate': [('readonly', True)]},
    )

    state = fields.Selection(
        string=u'Situação',
        selection=[
            ('open', 'Em aberto'),
            ('validate', 'Atestado'),
            ('paid', 'Debitado'),
        ],
        default='open',
    )

    payslip_id = fields.Many2one(
        comodel_name='hr.payslip',
        string='Holerite',
    )

    registro_telefonico_id = fields.Many2one(
        string='Registro Telefonico',
        comodel_name='hr.telefonia',
        required=True,
        # states={'validate': [('readonly', True)]},
    )

    concessionaria = fields.Char(
        string='Concessionária',
        # states={'validate': [('readonly', True)]},
    )

    localidade = fields.Char(
        string='Localidade',
        readonly=True,
    )

    hora_inicio = fields.Datetime(
        string='Hora de Início',
        # states={'validate': [('readonly', True)]},
    )

    inicio = fields.Char(
        string='Inicio',
        required=True,
        readonly=True,
    )

    duracao = fields.Char(
        string='Duração da ligação',
        required=True,
        readonly=True,
    )

    numero_discado = fields.Char(
        string='Numero Discado',
        readonly=True,
    )

    @api.multi
    @api.depends('ramal', 'employee_id')
    def compute_name(self):
        for record in self:
            if record.employee_id and record.ramal:
                record.name = 'Ligação Ramal: {} '.format(record.ramal.name)

    @api.multi
    def set_validate_ligacoes(self):
        """
        Rotina para atestar ligacoes como particulares ou nao
        depois dessa rotina a ligacao sera bloqueada para edicoes
        :return:
        """
        for record in self:
            # Atesta as ligacoes
            record.state = 'validate'

    @api.multi
    def set_particular(self):
        """
        Setar as ligações para particular
        :return:
        """
        for record in self:
            # record.particular = True
            record.tipo = 'particular'

    @api.multi
    def set_empresa(self):
        """
        Setar as ligações como ligações da empresa
        :return:
        """
        for record in self:
            # record.particular = False
            record.tipo = 'empresa'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2018 Joan Marín <Github@JoanMarin>
# Copyright 2018 Guillermo Montoya <Github@guillermm>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class ResPartner(models.Model):
    _inherit = 'res.partner'

    document_type_id = fields.Many2one(
        string = 'Document Type',
        comodel_name = 'res.partner.document.type')
    document_type_code = fields.Char(
            related='document_type_id.code',
            store=False)
    check_digit = fields.Char(string='Verification Digit', size=1)
    identification_document = fields.Char('Identification Document')

    @api.onchange('country_id', 'identification_document', 'check_digit', 'document_type_id')
    def _onchange_vat(self):
        if self.country_id and self.identification_document:
            if self.country_id.code:
                if self.check_digit and self.document_type_code == '31':
                    self.vat = self.country_id.code + self.identification_document + self.check_digit
                elif self.document_type_code == '43':
                    self.check_digit = False
                    self.vat = 'CO' + self.identification_document
                else:
                    self.check_digit = False
                    self.vat = self.country_id.code + self.identification_document
            else:
                msg = _('The Country has No ISO Code.')
                raise ValidationError(msg)
        elif not self.identification_document and self.vat:
            self.vat = False

    @api.constrains('vat', 'document_type_id', 'country_id')
    def check_vat(self):
        def _checking_required(partner):
            '''
            Este método solo aplica para Colombia y obliga a seleccionar
            un tipo de documento de identidad con el fin de determinar
            si es verificable por el algoritmo VAT. Si no se define,
            de todas formas el VAT se evalua como un NIT.
            '''
            return ((partner.document_type_id and \
                partner.document_type_id.checking_required) or \
                not partner.document_type_id) == True

        msg = _('The Identification Document does not seems to be correct.')

        for partner in self:
            if not partner.vat:
                continue

            vat_country, vat_number = self._split_vat(partner.vat)

            if self.document_type_code == '43':
                vat_country = 'co'
            elif partner.country_id:
                vat_country = partner.country_id.code.lower()

            if not hasattr(self, 'check_vat_' + vat_country):
                continue

            check = getattr(self, 'check_vat_' + vat_country)

            if vat_country == 'co':
                if not _checking_required(partner):
                    continue

            if check and not check(vat_number):
                raise ValidationError(msg)

        return True

    def check_vat_co(self, vat):
        '''
        Check VAT Routine for Colombia.
        '''
        if type(vat) == str:
            vat = vat.replace('-', '', 1).replace('.', '', 2)

        if len(str(vat)) < 4:
            return False

        try:
            int(vat)
        except ValueError:
            return False

        # Validación Sin identificación del exterior
        # o para uso definido por la DIAN
        if len(str(vat)) == 9 and str(vat)[0:5] == '44444' \
            and int(str(vat)[5:]) <= 9000 \
            and int(str(vat)[5:]) >= 4001:

            return True

        prime = [3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71]
        sum = 0
        vat_len = len(str(vat))

        for i in range(vat_len - 2, -1, -1):
            sum += int(str(vat)[i]) * prime[vat_len - 2 - i]

        if sum % 11 > 1:
            return str(vat)[vat_len - 1] == str(11 - (sum % 11))
        else:
            return str(vat)[vat_len - 1] == str(sum % 11)
<commit_msg>[FIX] Bug check_vat without parent_id<commit_after># -*- coding: utf-8 -*-
# Copyright 2018 Joan Marín <Github@JoanMarin>
# Copyright 2018 Guillermo Montoya <Github@guillermm>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError

class ResPartner(models.Model):
    _inherit = 'res.partner'

    document_type_id = fields.Many2one(
        string = 'Document Type',
        comodel_name = 'res.partner.document.type')
    document_type_code = fields.Char(
            related='document_type_id.code',
            store=False)
    check_digit = fields.Char(string='Verification Digit', size=1)
    identification_document = fields.Char('Identification Document')

    @api.onchange('country_id', 'identification_document', 'check_digit', 'document_type_id')
    def _onchange_vat(self):
        if self.country_id and self.identification_document:
            if self.country_id.code:
                if self.check_digit and self.document_type_code == '31':
                    self.vat = self.country_id.code + self.identification_document + self.check_digit
                elif self.document_type_code == '43':
                    self.check_digit = False
                    self.vat = 'CO' + self.identification_document
                else:
                    self.check_digit = False
                    self.vat = self.country_id.code + self.identification_document
            else:
                msg = _('The Country has No ISO Code.')
                raise ValidationError(msg)
        elif not self.identification_document and self.vat:
            self.vat = False

    @api.constrains('vat', 'document_type_id', 'country_id')
    def check_vat(self):
        def _checking_required(partner):
            '''
            Este método solo aplica para Colombia y obliga a seleccionar
            un tipo de documento de identidad con el fin de determinar
            si es verificable por el algoritmo VAT. Si no se define,
            de todas formas el VAT se evalua como un NIT.
            '''
            return ((partner.document_type_id and partner.document_type_id.checking_required)
                    or (not partner.parent_id and not partner.document_type_id)) == True

        msg = _('The Identification Document does not seems to be correct.')

        for partner in self:
            if not partner.vat:
                continue

            vat_country, vat_number = self._split_vat(partner.vat)

            if self.document_type_code == '43':
                vat_country = 'co'
            elif partner.country_id:
                vat_country = partner.country_id.code.lower()

            if not hasattr(self, 'check_vat_' + vat_country):
                continue

            check = getattr(self, 'check_vat_' + vat_country)

            if vat_country == 'co':
                if not _checking_required(partner):
                    continue

            if check and not check(vat_number):
                raise ValidationError(msg)

        return True

    def check_vat_co(self, vat):
        '''
        Check VAT Routine for Colombia.
        '''
        if type(vat) == str:
            vat = vat.replace('-', '', 1).replace('.', '', 2)

        if len(str(vat)) < 4:
            return False

        try:
            int(vat)
        except ValueError:
            return False

        # Validación Sin identificación del exterior
        # o para uso definido por la DIAN
        if len(str(vat)) == 9 and str(vat)[0:5] == '44444' \
            and int(str(vat)[5:]) <= 9000 \
            and int(str(vat)[5:]) >= 4001:

            return True

        prime = [3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71]
        sum = 0
        vat_len = len(str(vat))

        for i in range(vat_len - 2, -1, -1):
            sum += int(str(vat)[i]) * prime[vat_len - 2 - i]

        if sum % 11 > 1:
            return str(vat)[vat_len - 1] == str(11 - (sum % 11))
        else:
            return str(vat)[vat_len - 1] == str(sum % 11)
<|endoftext|>


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

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

from __future__ import print_function
import sys
from lxml import etree
from optparse import OptionParser
from xml.dom import minidom
import codecs

from androguard.core import androconf
from androguard.core.bytecodes import apk
from androguard.util import read

option_0 = {
    'name': ('-i', '--input'),
    'help': 'filename input (APK or android\'s binary xml)',
    'nargs': 1
}
option_1 = {
    'name': ('-o', '--output'),
    'help': 'filename output of the xml',
    'nargs': 1
}
option_2 = {
    'name': ('-v', '--version'),
    'help': 'version of the API',
    'action': 'count'
}
options = [option_0, option_1, option_2]

def main(options, arguments):
    if options.input is not None:
        buff = ""

        ret_type = androconf.is_android(options.input)
        if ret_type == "APK":
            a = apk.APK(options.input)
            xml = a.get_android_manifest_xml()
            buff = etree.tostring(xml, pretty_print=True)
        elif ".xml" in options.input:
            ap = apk.AXMLPrinter(read(options.input))
            buff = minidom.parseString(ap.get_buff()).toprettyxml(
                encoding="utf-8")
        else:
            print("Unknown file type")
            return

        if options.output is not None:
            fd = codecs.open(options.output, "w", "utf-8")
            fd.write(buff)
            fd.close()
        else:
            print(buff)

    elif options.version is not None:
        print("Androaxml version %s" % androconf.ANDROGUARD_VERSION)

if __name__ == "__main__":
    parser = OptionParser()
    for option in options:
        param = option['name']
        del option['name']
        parser.add_option(*param, **option)

    options, arguments = parser.parse_args()
    sys.argv[:] = arguments
    main(options, arguments)
<commit_msg>fix return value<commit_after>#!/usr/bin/env python

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

from __future__ import print_function
import sys
from lxml import etree
from optparse import OptionParser
from xml.dom import minidom
import codecs

from androguard.core import androconf
from androguard.core.bytecodes import apk
from androguard.util import read

option_0 = {
    'name': ('-i', '--input'),
    'help': 'filename input (APK or android\'s binary xml)',
    'nargs': 1
}
option_1 = {
    'name': ('-o', '--output'),
    'help': 'filename output of the xml',
    'nargs': 1
}
option_2 = {
    'name': ('-v', '--version'),
    'help': 'version of the API',
    'action': 'count'
}
options = [option_0, option_1, option_2]

def main(options, arguments):
    if options.input is not None:
        buff = ""

        ret_type = androconf.is_android(options.input)
        if ret_type == "APK":
            a = apk.APK(options.input)
            xml = a.get_android_manifest_xml()
            buff = etree.tostring(xml, pretty_print=True).decode("UTF-8")
        elif ".xml" in options.input:
            ap = apk.AXMLPrinter(read(options.input))
            buff = minidom.parseString(ap.get_buff()).toprettyxml(
                encoding="utf-8")
        else:
            print("Unknown file type")
            return

        if options.output is not None:
            fd = codecs.open(options.output, "w", "utf-8")
            fd.write(buff)
            fd.close()
        else:
            print(buff)

    elif options.version is not None:
        print("Androaxml version %s" % androconf.ANDROGUARD_VERSION)

if __name__ == "__main__":
    parser = OptionParser()
    for option in options:
        param = option['name']
        del option['name']
        parser.add_option(*param, **option)

    options, arguments = parser.parse_args()
    sys.argv[:] = arguments
    main(options, arguments)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#@created: 09.10.2013
#@author: Aleksey Komissarov
#@contact: ad3002@gmail.com

def read_snp_position_data(file_name):
    ''' Read SNP positions from file.
    @param file_name: file name with data (chr\tposition\n)
    @return: chr -> positions
    '''
    print "Read SNPs data"
    bands = defaultdict(list)
    with open(file_name) as fh:
    	lines = fh.readlines()
    	data = [x.strip().split() for x in lines if not x.startswith("#")]
    for chrom, pos in data:
    	bands[chrom].append(int(pos))
    return bands

def compute_interval_freq(bands, chrs_lengths, bp_per_pixel=100000):
    ''' Compute SNPs intervals for drawing.
    @param bands: dictionary chr_name -> positions
    @param chrs_lengths: dictionary chr_name -> chr length
    @param bp_per_pixel: number nucleotides per pixel on image
    '''
    chr2pos2freq = {}
    for name, length in chrs_lengths.items():
    	snps = bands[name]
    	snps.sort()
    	bins = defaultdict(int)
    	for snip in snps:
    		b = snip / bp_per_pixel
    		bins[b*bp_per_pixel] += 1
    	max_value = float(max(bins.values()))
    	chr2pos2freq[name] = bins
    return chr2pos2freq, max_value

def normalize_interval_freq(bands, chrs_lengths, bp_per_pixel=100000, max_cutoff=70):
    ''' Compute normalized SNPs intervals for drawing.
    @param bands: dictionary chr_name -> positions
    @param chrs_lengths: dictionary chr_name -> chr length
    @param bp_per_pixel: number nucleotides per pixel on image
    @param max_cutoff: maximal value of enrichment in interval
    '''
    max_cutoff = float(max_cutoff)
    chr2pos2freq = {}
    for name, length in chrs_lengths.items():
    	snps = bands[name]
    	snps.sort()
    	bins = defaultdict(int)
    	for snip in snps:
    		b = snip / bp_per_pixel
    		bins[b*bp_per_pixel] += 1
    	max_value = float(max(bins.values()))
    	chr2pos2freq[name] = bins
    M = [max(chr2pos2freq[x].values()) for x in chr2pos2freq.keys()]
    m = float(max(M))
    for name in chr2pos2freq:
    	for k in chr2pos2freq[name]:
    		if chr2pos2freq[name][k] > max_cutoff:
    			chr2pos2freq[name][k] = max_cutoff
    		chr2pos2freq[name][k] /= max_cutoff
    return chr2pos2freq
<commit_msg>normalization<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#@created: 09.10.2013
#@author: Aleksey Komissarov
#@contact: ad3002@gmail.com

def read_snp_position_data(file_name):
    ''' Read SNP positions from file.
    @param file_name: file name with data (chr\tposition\n)
    @return: chr -> positions
    '''
    print "Read SNPs data"
    bands = defaultdict(list)
    with open(file_name) as fh:
    	lines = fh.readlines()
    	data = [x.strip().split() for x in lines if not x.startswith("#")]
    for chrom, pos in data:
    	bands[chrom].append(int(pos))
    return bands

def compute_interval_freq(bands, chrs_lengths, bp_per_pixel=100000):
    ''' Compute SNPs intervals for drawing.
    @param bands: dictionary chr_name -> positions
    @param chrs_lengths: dictionary chr_name -> chr length
    @param bp_per_pixel: number nucleotides per pixel on image
    @return: (chr2pos2freq, max_value)
    '''
    chr2pos2freq = {}
    for name, length in chrs_lengths.items():
    	snps = bands[name]
    	snps.sort()
    	bins = defaultdict(int)
    	for snip in snps:
    		b = snip / bp_per_pixel
    		bins[b*bp_per_pixel] += 1
    	max_value = float(max(bins.values()))
    	chr2pos2freq[name] = bins
    return chr2pos2freq, max_value

def normalize_interval_freq(chr2pos2freq, max_cutoff=70):
    ''' Compute normalized SNPs intervals for drawing.
    @param bands: dictionary chr_name -> positions
    @param chrs_lengths: dictionary chr_name -> chr length
    @param bp_per_pixel: number nucleotides per pixel on image
    @param max_cutoff: maximal value of enrichment in interval
    '''
    M = [max(chr2pos2freq[x].values()) for x in chr2pos2freq.keys()]
    m = float(max(M))
    for name in chr2pos2freq:
    	for k in chr2pos2freq[name]:
    		if chr2pos2freq[name][k] > max_cutoff:
    			chr2pos2freq[name][k] = max_cutoff
    		chr2pos2freq[name][k] /= max_cutoff
    return chr2pos2freq
<|endoftext|>


<commit_before>import pygame
import math
from random import randint, choice

class Ant(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.initial_image = pygame.image.load("images/ant01a.png").convert_alpha()
        self.direction = randint(0, 359)
        self.image = self.initial_image
        self.rect = self.image.get_rect()
        self.rect.x = 450
        self.rect.y = 300
        self.stop_count = 0

    def rotate(self, angle):
        self.direction += angle
        self.image = rotate_center(self.initial_image, self.direction)

    def resolve_direction(self):
        while self.direction < 0:
            self.direction += 360
        self.direction %= 360

    def change_collision_direction(self, boundary_value):
        if self.direction < boundary_value:
            self.direction -= randint(20, 30)
        elif boundary_value == 0:
            if self.direction > 270:
                self.direction -= randint(20, 30)
            else:
                self.direction += randint(20, 30)
        elif self.direction > boundary_value:
            self.direction += randint(20, 30)
        else:  # Facing edge
            magnitude = randint(30, 40)
            if randint(0, 1) == 0:
                self.direction += magnitude
            else:
                self.direction -= magnitude

    def detect_edge(self):
        self.resolve_direction()
        if self.rect.x < 1:  # Left edge
            self.change_collision_direction(90)
        if self.rect.x > 875:  # Right edge
            self.change_collision_direction(90)
        if self.rect.y < 1:  # Top edge
            self.change_collision_direction(0)
        if self.rect.y > 575:  # Bottom edge
            self.change_collision_direction(180)

    def move(self, distance):
        self.detect_edge()
        dx = (math.cos(math.radians(self.direction - 90)))
        dy = (math.sin(math.radians(self.direction - 90)))
        if dx < 0:
            dx *= 1.5
        if dy > 0:
            dy *= 1.5
        # print(str(dx) + " and " + str(dy))
        self.rect.x -= dx * distance
        self.rect.y += dy * distance

    def stop(self, iterations):
        self.stop_count += iterations

class Hole(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/hole01a.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = randint(1, 875)
        self.rect.y = randint(1, 575)

class Food(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        food_images = ["pictures/banana.png", "pictures/apple.png", "pictures/donut.png", "pictures/GrapeRedSeedless.png"]
        self.image = pygame.image.load(choice(food_images)).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = randint(1, 875)
        self.rect.y = randint(1, 575)

def display_text(screen, text, size, location):
    # Create font
    my_font = pygame.font.SysFont("monospace", size)
    # Label text
    label = my_font.render(text, 1, (250, 250, 250))
    # Display text
    screen.blit(label, location)

def rotate_center(image, angle):
    """rotate an image while keeping its center and size"""
    # From http://pygame.org/wiki/RotateCenter
    orig_rect = image.get_rect()
    rotate_image = pygame.transform.rotate(image, angle)
    rotate_rect = orig_rect.copy()
    rotate_rect.center = rotate_image.get_rect().center
    rotate_image = rotate_image.subsurface(rotate_rect).copy()
    return rotate_image

def get_random_ish_direction(max_degrees=10):
    return randint(-max_degrees, max_degrees)

def main():
    """
    Main game code

    Contains pygame setup and the main game loop
    """
    pygame.init()  # Initialise pygame
    # Set window title
    pygame.display.set_caption("Ants: An Artificial Intelligence Experiment by C7")
    screen = pygame.display.set_mode((900, 600))  # Set window size
    done = False  # The game loop is broken when done becomes True

    # Load background image resource
    bg = pygame.image.load("images/bg01a.jpg")

    ant_list = [Ant() for i in range(40)]
    hole_list = [Hole() for i in range(3)]
    food_list = [Food() for i in range(10)]

    # Create groups for objects
    ants = pygame.sprite.Group()
    holes = pygame.sprite.Group()
    rocks = pygame.sprite.Group()
    food = pygame.sprite.Group()

    # Add ants_list objects to ants list
    for individual_ant in ant_list:
        ants.add(individual_ant)

    for individual_hole in hole_list:
        holes.add(individual_hole)

    for individual_meal in food_list:
        food.add(individual_meal)

    # List of all sprites
    all_sprites = pygame.sprite.Group()
    # Add sprite groups to main group
    all_sprites.add(ants, holes, rocks, food)
    # pygame clock
    clock = pygame.time.Clock()

    while not done:  # Main game loop
        clock.tick(60)  # Frame-rate
        # Add background image, overlaying everything
        screen.blit(bg, (0, 0))

        hits = pygame.sprite.spritecollide(ants, Hole, False, pygame.sprite.collide_circle)
        if hits:
            done = True

        # Rotate the ant - currently an experiment
        # Will be used for when the ant moves around on its own
        for ant in ant_list:
            if ant.stop_count > 0:  # If need to wait for stop
                ant.stop_count -= 1
            else:  # Rotate and/or move or neither
                if randint(0, 2) == 0:  # Rotate
                    ant.rotate(get_random_ish_direction(14))
                if randint(0, 7) > 0:  # Move
                    ant.move(2)
                if randint(0, 150) == 0:
                    ant.stop(randint(10, 40))

        # Draw all sprites group to the screen
        all_sprites.draw(screen)
        # Update all sprites
        all_sprites.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # If close button
                done = True  # Exit
            if event.type == pygame.KEYDOWN:  # If key pressed
                if event.key == pygame.K_ESCAPE:  # If 'esc' pressed
                    done = True  # Exit

        display_text(screen, "fps:" + str(int(round(clock.get_fps(), 0))), 14, (850, 0))

        # Update display to show changes made in current iteration
        pygame.display.update()

main()
<commit_msg>Holes being randomly generated whenever the program is run, music soon-to-be added<commit_after>import pygame
import math
from random import randint, choice

class Ant(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.initial_image = pygame.image.load("images/ant01a.png").convert_alpha()
        self.direction = randint(0, 359)
        self.image = self.initial_image
        self.rect = self.image.get_rect()
        self.rect.x = 450
        self.rect.y = 300
        self.stop_count = 0

    def rotate(self, angle):
        self.direction += angle
        self.image = rotate_center(self.initial_image, self.direction)

    def resolve_direction(self):
        while self.direction < 0:
            self.direction += 360
        self.direction %= 360

    def change_collision_direction(self, boundary_value):
        if self.direction < boundary_value:
            self.direction -= randint(20, 30)
        elif boundary_value == 0:
            if self.direction > 270:
                self.direction -= randint(20, 30)
            else:
                self.direction += randint(20, 30)
        elif self.direction > boundary_value:
            self.direction += randint(20, 30)
        else:  # Facing edge
            magnitude = randint(30, 40)
            if randint(0, 1) == 0:
                self.direction += magnitude
            else:
                self.direction -= magnitude

    def detect_edge(self):
        self.resolve_direction()
        if self.rect.x < 1:  # Left edge
            self.change_collision_direction(90)
        if self.rect.x > 875:  # Right edge
            self.change_collision_direction(90)
        if self.rect.y < 1:  # Top edge
            self.change_collision_direction(0)
        if self.rect.y > 575:  # Bottom edge
            self.change_collision_direction(180)

    def move(self, distance):
        self.detect_edge()
        dx = (math.cos(math.radians(self.direction - 90)))
        dy = (math.sin(math.radians(self.direction - 90)))
        if dx < 0:
            dx *= 1.5
        if dy > 0:
            dy *= 1.5
        # print(str(dx) + " and " + str(dy))
        self.rect.x -= dx * distance
        self.rect.y += dy * distance

    def stop(self, iterations):
        self.stop_count += iterations

class Hole(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/hole01a.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.x = randint(1, 875)
        self.rect.y = randint(1, 575)

def display_text(screen, text, size, location):
    # Create font
    my_font = pygame.font.SysFont("monospace", size)
    # Label text
    label = my_font.render(text, 1, (250, 250, 250))
    # Display text
    screen.blit(label, location)

def rotate_center(image, angle):
    """rotate an image while keeping its center and size"""
    # From http://pygame.org/wiki/RotateCenter
    orig_rect = image.get_rect()
    rotate_image = pygame.transform.rotate(image, angle)
    rotate_rect = orig_rect.copy()
    rotate_rect.center = rotate_image.get_rect().center
    rotate_image = rotate_image.subsurface(rotate_rect).copy()
    return rotate_image

def get_random_ish_direction(max_degrees=10):
    return randint(-max_degrees, max_degrees)

def main():
    """
    Main game code

    Contains pygame setup and the main game loop
    """
    pygame.init()  # Initialise pygame
    # Set window title
    pygame.display.set_caption("Ants: An Artificial Intelligence Experiment by C7")
    screen = pygame.display.set_mode((900, 600))  # Set window size
    done = False  # The game loop is broken when done becomes True

    # Load background image resource
    bg = pygame.image.load("images/bg01a.jpg")

    ant_list = [Ant() for i in range(40)]
    hole_list = [Hole() for i in range(3)]

    # Create groups for objects
    ants = pygame.sprite.Group()
    holes = pygame.sprite.Group()
    rocks = pygame.sprite.Group()
    food = pygame.sprite.Group()

    # Add ants_list objects to ants list
    for individual_ant in ant_list:
        ants.add(individual_ant)

    for individual_hole in hole_list:
        holes.add(individual_hole)

    # List of all sprites
    all_sprites = pygame.sprite.Group()
    # Add sprite groups to main group
    all_sprites.add(ants, holes, rocks, food)
    # pygame clock
    clock = pygame.time.Clock()

    while not done:  # Main game loop
        clock.tick(60)  # Frame-rate
        # Add background image, overlaying everything
        screen.blit(bg, (0, 0))

        hits = pygame.sprite.spritecollide(ants, Hole, False, pygame.sprite.collide_circle)
        if hits:
            done = True

        # Rotate the ant - currently an experiment
        # Will be used for when the ant moves around on its own
        for ant in ant_list:
            if ant.stop_count > 0:  # If need to wait for stop
                ant.stop_count -= 1
            else:  # Rotate and/or move or neither
                if randint(0, 2) == 0:  # Rotate
                    ant.rotate(get_random_ish_direction(14))
                if randint(0, 7) > 0:  # Move
                    ant.move(2)
                if randint(0, 150) == 0:
                    ant.stop(randint(10, 40))

        # Draw all sprites group to the screen
        all_sprites.draw(screen)
        # Update all sprites
        all_sprites.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # If close button
                done = True  # Exit
            if event.type == pygame.KEYDOWN:  # If key pressed
                if event.key == pygame.K_ESCAPE:  # If 'esc' pressed
                    done = True  # Exit

        display_text(screen, "fps:" + str(int(round(clock.get_fps(), 0))), 14, (850, 0))

        # Update display to show changes made in current iteration
        pygame.display.update()

main()
<|endoftext|>


<commit_before>from django.contrib import admin

from api import models

class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'country', )
    search_fields = ['user__email', 'user__username', 'user__first_name', 'user__last_name']

class EventAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}
    list_display = ('title', 'status', 'location', 'country', 'start_date')
    list_editable = ('status',)
    list_filter = ('status', 'start_date')
    filter_horizontal = ('audience',)
    filter_horizontal = ('theme',)

admin.site.register(models.UserProfile, UserProfileAdmin)
admin.site.register(models.Event, EventAdmin)
admin.site.register(models.events.EventAudience)
admin.site.register(models.events.EventTheme)
<commit_msg>Make events in the admin searchable by a few fields<commit_after>from django.contrib import admin

from api import models

class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'country', )
    search_fields = ['user__email', 'user__username', 'user__first_name', 'user__last_name']

class EventAdmin(admin.ModelAdmin):
    search_fields = [
        'title',
        'description',
        'codeweek_for_all_participation_code',
        'creator__email',
        'creator__username',
        'creator__first_name',
        'creator__last_name',
    ]
    prepopulated_fields = {'slug': ('title',)}
    list_display = ('title', 'status', 'location', 'country', 'start_date')
    list_editable = ('status',)
    list_filter = ('status', 'start_date')
    filter_horizontal = ('audience',)
    filter_horizontal = ('theme',)

admin.site.register(models.UserProfile, UserProfileAdmin)
admin.site.register(models.Event, EventAdmin)
admin.site.register(models.events.EventAudience)
admin.site.register(models.events.EventTheme)
<|endoftext|>