<commit_before># Python script for additional style validation
# Author: Rintze M. Zelle
# Version: 2011-07-28
# * Requires lxml library (http://lxml.de/)
# Checks
# - whether ".csl" files conform to naming scheme [a-z0-9] with optional single
#   hyphen delimiters
# - whether style ID (content of cs:id) exists and matches the URI generated by
#   the Zotero Style Repository:
#   "http://www.zotero.org/styles/" + filename - extension
# - whether "self" link (value of "href" on cs:link with rel="self") exists
#   (independent styles only) and matches the URI generated by the Zotero Style
#   Repository
# - for independent styles, whether "template" link (value of "href" on cs:link
#   with rel="template") points to the Zotero Style Repository style URI of an
#   independent style
# - for dependent styles, whether "independent-parent" link (value of "href" on
#   cs:link with rel="template") exists and points to the Zotero Style
#   Repository style URI of an independent style
# - whether style filenames are unique (duplication is possible as dependent
#   styles are stored in their own subdirectory)
# - whether the optional "citation-format" (value of "citation-format" on
#   cs:category) exists and, for dependent styles, whether it matches that of
#   the independent parent
#
# Shows
# - number of dependent styles per independent style (if printDependentsCount is
#   set to True)

import os, glob, re
from lxml import etree

path = 'C:\Documents and Settings\zelle\My Documents\CSL\styles\\'
printDependentsCount = False

def parseStyle(stylePath):
    style = etree.parse(stylePath)
    styleElement = style.getroot()
    metadata = {}
    try:
        metadata["id"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}id").text
        metadata["selfLink"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='self']").attrib.get("href")
    except:
        pass
    try:
        metadata["template"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='template']").attrib.get("href")
    except:
        pass
    try:
        metadata["independentParent"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='independent-parent']").attrib.get("href")
    except:
        pass
    try:
        metadata["citationFormat"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}category[@citation-format]").attrib.get("citation-format")
    except:
        pass
    return(metadata)

metadataList = []
metadata = {}
bad_fileName = {"errorMessage":"Non-conforming Filename (only lowercase roman letters [a-z], digits [0-9] and separating hyphens are allowed):"}
bad_fileName["styles"] = []
citationFormat_mismatch = {"errorMessage":"Citation format does not match that of independent parent (value 'citation-format' on cs:category[@citation-format]):"}
citationFormat_mismatch["styles"] = []
duplicated_fileName = {"errorMessage":"Duplicate Filename (filename used by both an independent and dependent style):"}
duplicated_fileName["styles"] = []
fileName_id_mismatch = {"errorMessage":"Filename/ID Mismatch (filename does not match content cs:id):"}
fileName_id_mismatch["styles"] = []
fileName_selfLink_mismatch = {"errorMessage":"Filename/URI Mismatch (filename does not match value 'href' on cs:link[@rel=self]):"}
fileName_selfLink_mismatch["styles"] = []
missing_citationFormat = {"errorMessage":"Missing citation-format (value 'citation-format' on cs:category[@citation-format]):"}
missing_citationFormat["styles"] = []
missing_id = {"errorMessage":"Missing ID (content cs:id):"}
missing_id["styles"] = []
missing_independentParent = {"errorMessage":"No Parent Specified (value 'href' on cs:link[@rel=independent-parent]):"}
missing_independentParent["styles"] = []
missing_selfLink = {"errorMessage":"Missing URI (value 'href' on cs:link[@rel=self]):"}
missing_selfLink["styles"] = []
missing_template = {"errorMessage":"Template does not exist (no URI match for value 'href' on cs:link[@rel=template]):"}
missing_template["styles"] = []
orphan = {"errorMessage":"Parent does not exist (no URI match for value 'href' on cs:link[@rel=independent-parent]):"}
orphan["styles"] = []

styleErrors = [bad_fileName, duplicated_fileName, fileName_id_mismatch, fileName_selfLink_mismatch, missing_id, missing_selfLink, missing_independentParent, missing_template, orphan, missing_citationFormat, citationFormat_mismatch]
for independentStyle in glob.glob( os.path.join(path, '*.csl') ):
    fileName = os.path.basename(independentStyle)

    if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
        bad_fileName["styles"].append(fileName)

    metadata = parseStyle(independentStyle)
    metadata["fileName"] = fileName

    if(metadata.has_key("selfLink")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadata["selfLink"]+".csl")):
            fileName_selfLink_mismatch["styles"].append(fileName)
    else:
        missing_selfLink["styles"].append(fileName)
    if(metadata.has_key("id")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadata["id"]+".csl")):
            fileName_id_mismatch["styles"].append(fileName)
    else:
        missing_id["styles"].append(fileName)
    if not(metadata.has_key("citationFormat")):
        missing_citationFormat["styles"].append(fileName)

    metadataList.append(metadata)

for queryMetadataDict in metadataList:
    match = True

    if(queryMetadataDict.has_key("template")):
        match = False
        for metadataDict in metadataList:
            if(metadataDict.has_key("selfLink") and (queryMetadataDict["template"] == metadataDict["selfLink"])):
                match = True
        if(match == False):
            missing_template["styles"].append(queryMetadataDict["fileName"])

metadataListDependents = []
metadataDependents = {}
for dependentStyle in glob.glob( os.path.join(path, "dependent", '*.csl') ):
    fileName = os.path.basename(dependentStyle)

    if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
        bad_fileName["styles"].append("dependent" + os.sep + fileName)

    metadataDependents = parseStyle(dependentStyle)
    metadataDependents["fileName"] = fileName

    if(metadataDependents.has_key("selfLink")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["selfLink"]+".csl")):
            fileName_selfLink_mismatch["styles"].append("dependent" + os.sep + fileName)
    if(metadataDependents.has_key("id")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["id"]+".csl")):
            fileName_id_mismatch["styles"].append("dependent" + os.sep + fileName)
    else:
        missing_id["styles"].append("dependent" + os.sep + fileName)
    if not(metadataDependents.has_key("independentParent")):
        missing_independentParent["styles"].append("dependent" + os.sep + fileName)
    if not(metadataDependents.has_key("citationFormat")):
        missing_citationFormat["styles"].append("dependent" + os.sep + fileName)

    metadataListDependents.append(metadataDependents)

dependentsCount = {}
for queryMetadataDict in metadataListDependents:
    match = True

    if(queryMetadataDict.has_key("independentParent")):
        match = False
        for metadataDict in metadataList:
            if(metadataDict.has_key("selfLink") and (queryMetadataDict["independentParent"] == metadataDict["selfLink"])):
                match = True
                if metadataDict["fileName"] in dependentsCount:
                    dependentsCount[metadataDict["fileName"]] += 1
                else:
                    dependentsCount[metadataDict["fileName"]] = 1
                if(metadataDict.has_key("citationFormat") and queryMetadataDict.has_key("citationFormat") and not(queryMetadataDict["citationFormat"] == metadataDict["citationFormat"])):
                    citationFormat_mismatch["styles"].append("dependent" + os.sep + queryMetadataDict["fileName"])
            if(queryMetadataDict["fileName"] == metadataDict["fileName"]):
                duplicated_fileName["styles"].append(metadataDict["fileName"])
        if(match == False):
            orphan["styles"].append("dependent" + os.sep + queryMetadataDict["fileName"])

for styleError in styleErrors:
    if (len(styleError["styles"]) > 0):
        print(styleError["errorMessage"])
        for style in styleError["styles"]:
            print(style)
        print("----------")

if printDependentsCount:
    dependentsPopularitySort = sorted(dependentsCount, key=dependentsCount.get, reverse=True)
    print("Number of dependent styles per independent style:")
    for style in dependentsPopularitySort:
        print(style + ": %d" % (dependentsCount[style]))
<commit_msg>Update csl-quality-control.py to work on OS X<commit_after># Python script for additional style validation
# Author: Rintze M. Zelle
# Version: 2011-07-28
# * Requires lxml library (http://lxml.de/)
# Checks
# - whether ".csl" files conform to naming scheme [a-z0-9] with optional single
#   hyphen delimiters
# - whether style ID (content of cs:id) exists and matches the URI generated by
#   the Zotero Style Repository:
#   "http://www.zotero.org/styles/" + filename - extension
# - whether "self" link (value of "href" on cs:link with rel="self") exists
#   (independent styles only) and matches the URI generated by the Zotero Style
#   Repository
# - for independent styles, whether "template" link (value of "href" on cs:link
#   with rel="template") points to the Zotero Style Repository style URI of an
#   independent style
# - for dependent styles, whether "independent-parent" link (value of "href" on
#   cs:link with rel="template") exists and points to the Zotero Style
#   Repository style URI of an independent style
# - whether style filenames are unique (duplication is possible as dependent
#   styles are stored in their own subdirectory)
# - whether the optional "citation-format" (value of "citation-format" on
#   cs:category) exists and, for dependent styles, whether it matches that of
#   the independent parent
#
# Shows
# - number of dependent styles per independent style (if printDependentsCount is
#   set to True)

import os, glob, re, inspect
from lxml import etree

# http://stackoverflow.com/questions/50499
folderPath =  os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

parentFolderPath = os.path.dirname (folderPath)
path =  os.path.join(parentFolderPath, 'styles')

printDependentsCount = True

def parseStyle(stylePath):
    style = etree.parse(stylePath)
    styleElement = style.getroot()
    metadata = {}
    try:
        metadata["id"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}id").text
        metadata["selfLink"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='self']").attrib.get("href")
    except:
        pass
    try:
        metadata["template"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='template']").attrib.get("href")
    except:
        pass
    try:
        metadata["independentParent"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}link[@rel='independent-parent']").attrib.get("href")
    except:
        pass
    try:
        metadata["citationFormat"] = styleElement.find(".//{http://purl.org/net/xbiblio/csl}category[@citation-format]").attrib.get("citation-format")
    except:
        pass
    return(metadata)

metadataList = []
metadata = {}
bad_fileName = {"errorMessage":"Non-conforming Filename (only lowercase roman letters [a-z], digits [0-9] and separating hyphens are allowed):"}
bad_fileName["styles"] = []
citationFormat_mismatch = {"errorMessage":"Citation format does not match that of independent parent (value 'citation-format' on cs:category[@citation-format]):"}
citationFormat_mismatch["styles"] = []
duplicated_fileName = {"errorMessage":"Duplicate Filename (filename used by both an independent and dependent style):"}
duplicated_fileName["styles"] = []
fileName_id_mismatch = {"errorMessage":"Filename/ID Mismatch (filename does not match content cs:id):"}
fileName_id_mismatch["styles"] = []
fileName_selfLink_mismatch = {"errorMessage":"Filename/URI Mismatch (filename does not match value 'href' on cs:link[@rel=self]):"}
fileName_selfLink_mismatch["styles"] = []
missing_citationFormat = {"errorMessage":"Missing citation-format (value 'citation-format' on cs:category[@citation-format]):"}
missing_citationFormat["styles"] = []
missing_id = {"errorMessage":"Missing ID (content cs:id):"}
missing_id["styles"] = []
missing_independentParent = {"errorMessage":"No Parent Specified (value 'href' on cs:link[@rel=independent-parent]):"}
missing_independentParent["styles"] = []
missing_selfLink = {"errorMessage":"Missing URI (value 'href' on cs:link[@rel=self]):"}
missing_selfLink["styles"] = []
missing_template = {"errorMessage":"Template does not exist (no URI match for value 'href' on cs:link[@rel=template]):"}
missing_template["styles"] = []
orphan = {"errorMessage":"Parent does not exist (no URI match for value 'href' on cs:link[@rel=independent-parent]):"}
orphan["styles"] = []

styleErrors = [bad_fileName, duplicated_fileName, fileName_id_mismatch, fileName_selfLink_mismatch, missing_id, missing_selfLink, missing_independentParent, missing_template, orphan, missing_citationFormat, citationFormat_mismatch]
for independentStyle in glob.glob( os.path.join(path, '*.csl') ):
    fileName = os.path.basename(independentStyle)

    if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
        bad_fileName["styles"].append(fileName)

    metadata = parseStyle(independentStyle)
    metadata["fileName"] = fileName

    if(metadata.has_key("selfLink")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadata["selfLink"]+".csl")):
            fileName_selfLink_mismatch["styles"].append(fileName)
    else:
        missing_selfLink["styles"].append(fileName)
    if(metadata.has_key("id")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadata["id"]+".csl")):
            fileName_id_mismatch["styles"].append(fileName)
    else:
        missing_id["styles"].append(fileName)
    if not(metadata.has_key("citationFormat")):
        missing_citationFormat["styles"].append(fileName)

    metadataList.append(metadata)

for queryMetadataDict in metadataList:
    match = True

    if(queryMetadataDict.has_key("template")):
        match = False
        for metadataDict in metadataList:
            if(metadataDict.has_key("selfLink") and (queryMetadataDict["template"] == metadataDict["selfLink"])):
                match = True
        if(match == False):
            missing_template["styles"].append(queryMetadataDict["fileName"])

metadataListDependents = []
metadataDependents = {}
for dependentStyle in glob.glob( os.path.join(path, "dependent", '*.csl') ):
    fileName = os.path.basename(dependentStyle)

    if not(re.match("[a-z0-9](-?[a-z0-9]+)*(.csl)", fileName)):
        bad_fileName["styles"].append("dependent" + os.sep + fileName)

    metadataDependents = parseStyle(dependentStyle)
    metadataDependents["fileName"] = fileName

    if(metadataDependents.has_key("selfLink")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["selfLink"]+".csl")):
            fileName_selfLink_mismatch["styles"].append("dependent" + os.sep + fileName)
    if(metadataDependents.has_key("id")):
        if not(("http://www.zotero.org/styles/"+fileName) == (metadataDependents["id"]+".csl")):
            fileName_id_mismatch["styles"].append("dependent" + os.sep + fileName)
    else:
        missing_id["styles"].append("dependent" + os.sep + fileName)
    if not(metadataDependents.has_key("independentParent")):
        missing_independentParent["styles"].append("dependent" + os.sep + fileName)
    if not(metadataDependents.has_key("citationFormat")):
        missing_citationFormat["styles"].append("dependent" + os.sep + fileName)

    metadataListDependents.append(metadataDependents)

dependentsCount = {}
for queryMetadataDict in metadataListDependents:
    match = True

    if(queryMetadataDict.has_key("independentParent")):
        match = False
        for metadataDict in metadataList:
            if(metadataDict.has_key("selfLink") and (queryMetadataDict["independentParent"] == metadataDict["selfLink"])):
                match = True
                if metadataDict["fileName"] in dependentsCount:
                    dependentsCount[metadataDict["fileName"]] += 1
                else:
                    dependentsCount[metadataDict["fileName"]] = 1
                if(metadataDict.has_key("citationFormat") and queryMetadataDict.has_key("citationFormat") and not(queryMetadataDict["citationFormat"] == metadataDict["citationFormat"])):
                    citationFormat_mismatch["styles"].append("dependent" + os.sep + queryMetadataDict["fileName"])
            if(queryMetadataDict["fileName"] == metadataDict["fileName"]):
                duplicated_fileName["styles"].append(metadataDict["fileName"])
        if(match == False):
            orphan["styles"].append("dependent" + os.sep + queryMetadataDict["fileName"])

for styleError in styleErrors:
    if (len(styleError["styles"]) > 0):
        print(styleError["errorMessage"])
        for style in styleError["styles"]:
            print(style)
        print("----------")

if printDependentsCount:
    dependentsPopularitySort = sorted(dependentsCount, key=dependentsCount.get, reverse=True)
    print("Number of dependent styles per independent style:")
    for style in dependentsPopularitySort:
        print(style + ": %d" % (dependentsCount[style]))
<|endoftext|>


<commit_before>
import time

import bs4
import requests

class SousChef(object):
    """Organise underlings to determine column values."""
    def __init__(self):
        self.soup = None
        self.callbacks = dict()

    def soupify(self, handle):
        """Produce a tag soup from a HTML file."""
        self.soup = bs4.BeautifulSoup(handle)

    def open(self, filename):
        """Open an HTML file and process it."""
        with open(filename) as html_file:
            self.soupify(html_file)

    def rowify(self, matcher):
        """Find sampled portions of tag soup."""
        return matcher(self.soup)

    def register(self, **callbacks):
        """Add one or more callbacks by keyword arguments."""
        self.callbacks.update(callbacks)

    def process(self, row_pattern):
        """TODO: magic."""
        rows = self.rowify(row_pattern)
        for row in rows:
            columns = dict((key, f(row))
                           for key, f in self.callbacks.iteritems())

class DeskClerk(object):
    """
    Fill out form inputs and save resulting reports.
    """
    def __init__(self, base_url, cooldown=2.0, init=True):
        self.base_url = base_url
        self.session = requests.session()
        self.cooldown = cooldown
        if init:
            self.session.get(base_url)

    def submit(self, form, data):
        """Auto-fill a form "intelligently"."""
        form_url = self.base_url + form
        r = self.session.post(form_url, data)
        print r.content

    def repeat(self, form, repeat_field, repeat_values, static_data=None):
        """Repeatedly submit a form."""
        if static_data is not None:
            data = dict(static_data)
        else:
            data = dict()
        for count, value in enumerate(repeat_values):
            data.update({repeat_field: value})
            self.submit(form, data)
            if count < len(repeat_values) - 1:
                time.sleep(self.cooldown)

<commit_msg>Make DeskClerk.submit return response content<commit_after>
import time

import bs4
import requests

class SousChef(object):
    """Organise underlings to determine column values."""
    def __init__(self):
        self.soup = None
        self.callbacks = dict()

    def soupify(self, handle):
        """Produce a tag soup from a HTML file."""
        self.soup = bs4.BeautifulSoup(handle)

    def open(self, filename):
        """Open an HTML file and process it."""
        with open(filename) as html_file:
            self.soupify(html_file)

    def rowify(self, matcher):
        """Find sampled portions of tag soup."""
        return matcher(self.soup)

    def register(self, **callbacks):
        """Add one or more callbacks by keyword arguments."""
        self.callbacks.update(callbacks)

    def process(self, row_pattern):
        """TODO: magic."""
        rows = self.rowify(row_pattern)
        for row in rows:
            columns = dict((key, f(row))
                           for key, f in self.callbacks.iteritems())

class DeskClerk(object):
    """
    Fill out form inputs and save resulting reports.
    """
    def __init__(self, base_url, cooldown=2.0, init=True):
        self.base_url = base_url
        self.session = requests.session()
        self.cooldown = cooldown
        if init:
            self.session.get(base_url)

    def submit(self, form, data):
        """Auto-fill a form "intelligently"."""
        form_url = self.base_url + form
        r = self.session.post(form_url, data)
        return r.content

    def repeat(self, form, repeat_field, repeat_values, static_data=None):
        """Repeatedly submit a form."""
        if static_data is not None:
            data = dict(static_data)
        else:
            data = dict()
        for count, value in enumerate(repeat_values):
            data.update({repeat_field: value})
            self.submit(form, data)
            if count < len(repeat_values) - 1:
                time.sleep(self.cooldown)

<|endoftext|>


<commit_before>from tornado.web import authenticated
from tornado.web import HTTPError

from amgut.handlers.base_handlers import BaseHandler
from amgut.util import AG_DATA_ACCESS
from amgut import media_locale

class ParticipantOverviewHandler(BaseHandler):
    @authenticated
    def post(self, participant_name):
        skid = self.current_user
        ag_login_id = AG_DATA_ACCESS.get_user_for_kit(skid)

        # Check if we have to remove the participant
        participant_to_remove = self.get_argument("remove", None)
        if participant_to_remove:
            barcodes = AG_DATA_ACCESS.getParticipantSamples(
                ag_login_id, participant_to_remove)
            # Remove all the samples attached to the participant
            for bc in barcodes:
                AG_DATA_ACCESS.deleteSample(bc['barcode'], ag_login_id)
            # Remove the participant
            AG_DATA_ACCESS.deleteAGParticipant(
                ag_login_id, participant_to_remove)
            # Redirect to portal
            self.redirect(media_locale['SITEBASE'] + "/authed/portal/")
            return

        participant_type = self.get_argument('participant_type')
        survey_id = AG_DATA_ACCESS.get_survey_id(ag_login_id, participant_name)

        if survey_id is None:
            raise HTTPError(404, "Could not retrieve survey details for "
                            "participant '%s'" % participant_name)

        # Get the list of samples for this participant
        samples = AG_DATA_ACCESS.getParticipantSamples(ag_login_id,
                                                       participant_name)

        self.render('participant_overview.html', skid=skid,
                    participant_name=participant_name, survey_id=survey_id,
                    participant_type=participant_type, samples=samples)
<commit_msg>BUG: stripping '/'<commit_after>from tornado.web import authenticated
from tornado.web import HTTPError

from amgut.handlers.base_handlers import BaseHandler
from amgut.util import AG_DATA_ACCESS
from amgut import media_locale

class ParticipantOverviewHandler(BaseHandler):
    @authenticated
    def post(self, participant_name):
        participant_name = participant_name.strip('/')  # for nginx
        skid = self.current_user
        ag_login_id = AG_DATA_ACCESS.get_user_for_kit(skid)

        # Check if we have to remove the participant
        participant_to_remove = self.get_argument("remove", None)
        if participant_to_remove:
            barcodes = AG_DATA_ACCESS.getParticipantSamples(
                ag_login_id, participant_to_remove)
            # Remove all the samples attached to the participant
            for bc in barcodes:
                AG_DATA_ACCESS.deleteSample(bc['barcode'], ag_login_id)
            # Remove the participant
            AG_DATA_ACCESS.deleteAGParticipant(
                ag_login_id, participant_to_remove)
            # Redirect to portal
            self.redirect(media_locale['SITEBASE'] + "/authed/portal/")
            return

        participant_type = self.get_argument('participant_type')
        survey_id = AG_DATA_ACCESS.get_survey_id(ag_login_id, participant_name)

        if survey_id is None:
            raise HTTPError(404, "Could not retrieve survey details for "
                            "participant '%s'" % participant_name)

        # Get the list of samples for this participant
        samples = AG_DATA_ACCESS.getParticipantSamples(ag_login_id,
                                                       participant_name)

        self.render('participant_overview.html', skid=skid,
                    participant_name=participant_name, survey_id=survey_id,
                    participant_type=participant_type, samples=samples)
<|endoftext|>


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

try:
    from anapioficeandfire.models import (
        Book,
        Character,
        House
    )
    from anapioficeandfire.utils import query
    from anapioficeandfire import settings
except:
    import settings
    from models import (
        Book,
        Character,
        House
    )
    from utils import query

def _get(id, model_type):
    result = query("{0}/{1}/{2}".format(settings.API_BASE_URL, model_type,str(id)))
    return result

def get_book(book_id):
    ''' Return a single book '''
    result = _get(book_id, 'books')
    return Book(result.content)

def get_character(character_id):
    ''' Return a single character '''
    result = _get(character_id, 'characters')
    return Character(result.content)

def get_house(house_id):
    ''' Return a single house '''
    result = _get(house_id, 'houses')
    return House(result.content)

<commit_msg>Created definitions for getting all entities<commit_after># -*- coding: utf-8 -*-

try:
    from anapioficeandfire.models import (
        Book,
        Character,
        House
    )
    from anapioficeandfire.utils import query
    from anapioficeandfire import settings
except:
    import settings
    from models import (
        Book,
        Character,
        House
    )
    from utils import query

def _get(id, model_type):
    result = query("{0}/{1}/{2}".format(settings.API_BASE_URL, model_type,str(id)))
    return result

def get_all_books(name, from_release_date, to_release_date):
    return None

def get_book(book_id):
    ''' Return a single book '''
    result = _get(book_id, 'books')
    return Book(result.content)

def get_all_characters(name, culture, born, died, is_alive):
    return None

def get_character(character_id):
    ''' Return a single character '''
    result = _get(character_id, 'characters')
    return Character(result.content)

def get_all_houses(name, region, words, has_words, has_titles, has_seats, has_died_out, has_ancestral_weapons):
    return None

def get_house(house_id):
    ''' Return a single house '''
    result = _get(house_id, 'houses')
    return House(result.content)

<|endoftext|>


<commit_before>from bibliopixel.animation import BaseCircleAnim
from bibliopixel import colors
import time

class ArcClock(BaseCircleAnim):

    def __init__(self, layout):
        super(ArcClock, self).__init__(layout)
        last = self.lastRing
        self.hands = [
            {
                'rings': [last - 0, last - 1],
                'color': colors.Red,
                'segments': 60,
                'key': 'tm_sec'
            },
            {
                'rings': [last - 2, last - 3],
                'color': colors.Green,
                'segments': 60,
                'key': 'tm_min'
            },
            {
                'rings': [last - 4, last - 5],
                'color': colors.Blue,
                'segments': 12,
                'key': 'tm_hour'
            }
        ]

    def step(self, amt=1):
        self.layout.all_off()
        t = time.localtime()
        for h in self.hands:
            segs = h['segments']
            end = (360 / segs) * (getattr(t, h['key']) % segs)
            if end:
                for i in h['rings']:
                    self.layout.fillRing(i, h['color'], startAngle=0, endAngle=end)
<commit_msg>Tweak arc_clock<commit_after>from bibliopixel.animation import BaseCircleAnim
from bibliopixel import colors
import time

class ArcClock(BaseCircleAnim):

    def __init__(self, layout):
        super(ArcClock, self).__init__(layout)
        self.hands = [
            {
                'rings': [0, 1],
                'color': colors.Red,
                'segments': 60,
                'key': 'tm_sec'
            },
            {
                'rings': [2, 3],
                'color': colors.Green,
                'segments': 60,
                'key': 'tm_min'
            },
            {
                'rings': [4, 5],
                'color': colors.Blue,
                'segments': 12,
                'key': 'tm_hour'
            }
        ]

    def step(self, amt=1):
        self.layout.all_off()
        t = time.localtime()
        for h in self.hands:
            segs = h['segments']
            end = (360 / segs) * (getattr(t, h['key']) % segs)
            if end:
                for i in h['rings']:
                    self.layout.fillRing(i, h['color'], startAngle=0, endAngle=end)
<|endoftext|>


<commit_before>__author__ = 'aleivag'

import logging
import serial
from cStringIO import StringIO

HEADER = [0xEF, 0x01]

PACKAGE_HANDSHAKE = 0x17 #: To greet (and posible ping) the fingerprint
PACKAGE_EMPTY = 0x0d
PACKAGE_GETIMAGE = 0x01
PACKAGE_IMAGE2TZ = 0x02
PACKAGE_REGMODEL = 0x05
PACKAGE_RANDOM = 0x14
PACKAGE_STORE = 0x06
PACKAGE_MATCH = 0x03
PACKAGE_SEARCH = 0x04
PACKAGE_TEMPLATE_NUM = 0x1d
PACKAGE_UP_IMAGE = 0x0A

FINGERPRINT_OK = 0x00
FINGERPRINT_NOFINGER = 0x02
FINGERPRINT_ENROLLMISMATCH = 0x0A

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

class Fingerprint(object):
    def __init__(self, port='/dev/ttyAMA0', baudrate=57600, timeout=2):

        self.password = 0
        self.address = [0xFF, 0xFF, 0xFF, 0xFF]

        self.serial = None
        self.port = port
        self.baudrate=baudrate
        self.timeout=timeout

    def connect(self):
        self.serial = serial.Serial(self.port, self.baudrate, timeout=self.timeout)

    def close(self):
        self.serial.close()

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def write(self, instruction_code, data):

        size = len(data) + 3

        packet_size = list(divmod(size, 256))

        checksum = 0x01 + sum(packet_size) + instruction_code + sum(data)

        checksum = list(divmod(checksum, 256))

        buffer = map(
            lambda x: chr(x),
            HEADER + self.address + [0x01] + packet_size + [instruction_code] + data + checksum
        )

        self.last_write_package = buffer
        logger.debug('write package: %s' % repr(buffer))
        #print "==>", buffer
        self.serial.write(''.join(buffer))

    def read(self):
        header = self.serial.read(2)
        addr = self.serial.read(4)
        pi = self.serial.read(1)
        length = self.serial.read(2)
        confirmation_code = self.serial.read(1)
        ilen = sum([ord(i) for i in length])
        edata=''

        if ilen>3:
            edata=self.serial.read(ilen-3)

        csum = self.serial.read(2)

        self.last_read_package = [header, addr, pi, length, confirmation_code, edata, csum]

        logger.debug('read package: %s' % self.last_read_package)
        return dict(
            confirmation_code=ord(confirmation_code),
            extra_data=edata
        )

    def handshake(self):
        self.write(instruction_code=PACKAGE_HANDSHAKE, data=[0])
        print self.read()

    def empty(self):
        self.write(instruction_code=PACKAGE_EMPTY, data=[])
        print self.read()

    def get_image(self):
        self.write(instruction_code=PACKAGE_GETIMAGE, data=[])
        return self.read()

    def up_image(self):
        logger.info('UPLOAD IMAGE')
        self.write(instruction_code=PACKAGE_UP_IMAGE, data=[])
        resp = self.read()
        resp['image'] = StringIO()
        for i in xrange(288):
            r = self.read()
            #logger.debug('   => %s ' % [r])
            resp['image'].write(r['extra_data'])
        resp['image'].seek(0)

        return resp

    def image_2_tz(self, buffer):
        self.write(instruction_code=PACKAGE_IMAGE2TZ, data=[buffer])
        return self.read()

    def match(self):
        self.write(instruction_code=PACKAGE_MATCH, data=[])
        resp = self.read()
        resp['score'] = sum(map(ord, resp['extra_data']))
        return resp

    def register_model(self):
        self.write(instruction_code=PACKAGE_REGMODEL, data=[])
        return self.read()

    def store_model(self, id, buffer=0x01):
        self.write(instruction_code=PACKAGE_STORE, data=[buffer] + list(divmod(id, 255)))
        return self.read()

    def template_number(self):
        self.write(instruction_code=PACKAGE_TEMPLATE_NUM, data=[])
        resp = self.read()
        resp['number'] = sum(map(ord, resp['extra_data']))
        return resp

    def get_random_code(self):
        self.write(instruction_code=PACKAGE_RANDOM, data=[])
        resp = self.read()
        resp['random'] = sum(map(ord, resp['extra_data']))
        return resp

    def search(self, buffer, start_page=0, page_num=0x00a3):
        self.write(instruction_code=PACKAGE_SEARCH, data=[buffer] + list(divmod(start_page, 255)) + list(divmod(page_num, 255)))
        resp = self.read()

        resp['page_id'] = sum(map(ord, resp['extra_data'][:2]))
        resp['score'] = sum(map(ord, resp['extra_data'][2:]))

        if resp['confirmation_code'] == 0:
            resp['confirmation_desc'] = 'OK'
        elif resp['confirmation_code'] == 9:
            resp['desc'] = "No matching in the library (both the PageID and matching score are 0)"

        return resp

def register_finger(id):
    with Fingerprint() as f:

        print "place finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK: pass
        f.image_2_tz(buffer=1)

        print "remove your finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_NOFINGER: pass

        print "place finger again"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK: pass

        f.image_2_tz(buffer=2)

        model = f.register_model()
        if model['confirmation_code'] != FINGERPRINT_OK:
            raise Exception("No Match")

        print f.store_model(id=id, buffer=1)

def validate_finger():
    with Fingerprint() as f:
        print "place finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK:
            pass
        print f.image_2_tz(0x01)
        print f.search(buffer=0x01)

if __name__ == '__main__':
    with Fingerprint() as f:
        f.handshake()
        f.empty()
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK:pass
        print f.up_image()
<commit_msg>no message<commit_after>__author__ = 'aleivag'

import logging
import serial
from cStringIO import StringIO

HEADER = [0xEF, 0x01]

PACKAGE_HANDSHAKE = 0x17 #: To greet (and posible ping) the fingerprint
PACKAGE_EMPTY = 0x0d
PACKAGE_GETIMAGE = 0x01
PACKAGE_IMAGE2TZ = 0x02
PACKAGE_REGMODEL = 0x05
PACKAGE_RANDOM = 0x14
PACKAGE_STORE = 0x06
PACKAGE_MATCH = 0x03
PACKAGE_SEARCH = 0x04
PACKAGE_TEMPLATE_NUM = 0x1d
PACKAGE_UP_IMAGE = 0x0A

FINGERPRINT_OK = 0x00
FINGERPRINT_NOFINGER = 0x02
FINGERPRINT_ENROLLMISMATCH = 0x0A

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

class Fingerprint(object):
    def __init__(self, port='/dev/ttyAMA0', baudrate=57600, timeout=2):

        self.password = 0
        self.address = [0xFF, 0xFF, 0xFF, 0xFF]

        self.serial = None
        self.port = port
        self.baudrate=baudrate
        self.timeout=timeout

    def connect(self):
        self.serial = serial.Serial(self.port, self.baudrate, timeout=self.timeout)

    def close(self):
        self.serial.close()

    def __enter__(self):
        self.connect()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def write(self, instruction_code, data):

        size = len(data) + 3

        packet_size = list(divmod(size, 256))

        checksum = 0x01 + sum(packet_size) + instruction_code + sum(data)

        checksum = list(divmod(checksum, 256))

        buffer = map(
            lambda x: chr(x),
            HEADER + self.address + [0x01] + packet_size + [instruction_code] + data + checksum
        )

        self.last_write_package = buffer
        logger.debug('write package: %s' % repr(buffer))
        #print "==>", buffer
        self.serial.write(''.join(buffer))

    def read(self):
        header = self.serial.read(2)
        addr = self.serial.read(4)
        pi = self.serial.read(1)
        length = self.serial.read(2)

        ilen = sum([ord(i) for i in length])
        edata=self.serial.read(ilen-2)
        resp = {'identifier': pi}

        if pi == 0x07:
            resp['confirmation_code'] = edata[0]
            edata = edata[1:]

        resp['extra_data'] = edata

        csum = self.serial.read(2)

        self.last_read_package = [header, addr, pi, length, confirmation_code, edata, csum]

        logger.debug('read package: %s' % self.last_read_package)
        return resp

    def handshake(self):
        self.write(instruction_code=PACKAGE_HANDSHAKE, data=[0])
        print self.read()

    def empty(self):
        self.write(instruction_code=PACKAGE_EMPTY, data=[])
        print self.read()

    def get_image(self):
        self.write(instruction_code=PACKAGE_GETIMAGE, data=[])
        return self.read()

    def up_image(self):
        logger.info('UPLOAD IMAGE')
        self.write(instruction_code=PACKAGE_UP_IMAGE, data=[])
        resp = self.read()
        resp['image'] = StringIO()
        for i in xrange(288):
            r = self.read()
            #logger.debug('   => %s ' % [r])
            resp['image'].write(r['extra_data'])
            resp['image'].write(r['extra_data'])
        resp['image'].seek(0)

        return resp

    def image_2_tz(self, buffer):
        self.write(instruction_code=PACKAGE_IMAGE2TZ, data=[buffer])
        return self.read()

    def match(self):
        self.write(instruction_code=PACKAGE_MATCH, data=[])
        resp = self.read()
        resp['score'] = sum(map(ord, resp['extra_data']))
        return resp

    def register_model(self):
        self.write(instruction_code=PACKAGE_REGMODEL, data=[])
        return self.read()

    def store_model(self, id, buffer=0x01):
        self.write(instruction_code=PACKAGE_STORE, data=[buffer] + list(divmod(id, 255)))
        return self.read()

    def template_number(self):
        self.write(instruction_code=PACKAGE_TEMPLATE_NUM, data=[])
        resp = self.read()
        resp['number'] = sum(map(ord, resp['extra_data']))
        return resp

    def get_random_code(self):
        self.write(instruction_code=PACKAGE_RANDOM, data=[])
        resp = self.read()
        resp['random'] = sum(map(ord, resp['extra_data']))
        return resp

    def search(self, buffer, start_page=0, page_num=0x00a3):
        self.write(instruction_code=PACKAGE_SEARCH, data=[buffer] + list(divmod(start_page, 255)) + list(divmod(page_num, 255)))
        resp = self.read()

        resp['page_id'] = sum(map(ord, resp['extra_data'][:2]))
        resp['score'] = sum(map(ord, resp['extra_data'][2:]))

        if resp['confirmation_code'] == 0:
            resp['confirmation_desc'] = 'OK'
        elif resp['confirmation_code'] == 9:
            resp['desc'] = "No matching in the library (both the PageID and matching score are 0)"

        return resp

def register_finger(id):
    with Fingerprint() as f:

        print "place finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK: pass
        f.image_2_tz(buffer=1)

        print "remove your finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_NOFINGER: pass

        print "place finger again"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK: pass

        f.image_2_tz(buffer=2)

        model = f.register_model()
        if model['confirmation_code'] != FINGERPRINT_OK:
            raise Exception("No Match")

        print f.store_model(id=id, buffer=1)

def validate_finger():
    with Fingerprint() as f:
        print "place finger"
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK:
            pass
        print f.image_2_tz(0x01)
        print f.search(buffer=0x01)

if __name__ == '__main__':
    with Fingerprint() as f:
        f.handshake()
        f.empty()
        while f.get_image().get('confirmation_code') != FINGERPRINT_OK:pass
        print f.up_image()
<|endoftext|>


<commit_before>from abc import ABCMeta,abstractmethod
from consul import Consul
from models import Container
from random import randint

class AllocationStrategy(metaclass=ABCMeta):
    @classmethod
    def from_name(clazz, name):
        if name == "random":
            return RandomAllocationStrategy()
        elif name == "round-robin":
            return RoundRobinAllocationStrategy()
        elif name == "first-fit":
            return FirstFitAllocationStrategy()
        elif name == "best-fit":
            return BestFitAllocationStrategy()
        elif name == "worst-fit":
            return WorstFitAllocationStrategy()
        elif name == "best-worst-fit":
            return BestWorstFitAllocationStrategy()
        else:
            raise "Strategy {} not implemented.".format(name)

    @abstractmethod
    def select_node(self, nodes, service):
        return

class RandomAllocationStrategy(AllocationStrategy):
    def select_node(self, nodes, container=None):
        return nodes[randint(0, len(nodes)-1)]

class RoundRobinAllocationStrategy(AllocationStrategy):
    __nodes = None
    __current_index = 0

    def select_node(self, nodes, service):
        if RoundRobinAllocationStrategy.__nodes == None:
            RoundRobinAllocationStrategy.__nodes = nodes
            RoundRobinAllocationStrategy.__current_index = 0
        elif len(RoundRobinAllocationStrategy.__nodes) != len(nodes):
            print("List of nodes changed, reset round-robin")
            RoundRobinAllocationStrategy.__nodes = nodes
            RoundRobinAllocationStrategy.__current_index = 0

        node = RoundRobinAllocationStrategy.__nodes[RoundRobinAllocationStrategy.__current_index]
        RoundRobinAllocationStrategy.__current_index += 1
        if RoundRobinAllocationStrategy.__current_index == len(RoundRobinAllocationStrategy.__nodes):
            RoundRobinAllocationStrategy.__current_index = 0
        return node

class AnyFitAllocationStrategy(AllocationStrategy):
    def generate_problem(self, nodes, service):
        bins = list(map(lambda n: n.to_bin(), nodes))
        items = self._get_service_metrics(nodes, service)
        if len(items) == 0:
            return [0, 0, 0], bins
        item = self._average_of_items(items)
        print(item)
        print(list(map(lambda b: str(b), bins)))
        return item, bins

    def _get_service_metrics(self, nodes, service):
        containers = []
        for n in nodes:
            containers += n.containers_by_service(service)
        containers_items = list(map(lambda c: c.to_item(), containers))
        return containers_items

    def _average_of_items(self, items):
        average_item = []
        for dim in range(len(items[0])):
            sum_dim = 0
            for item in items:
                sum_dim += item[dim]
            average_dim = sum_dim / len(items)
            average_item.append(average_dim)
        return average_item

class FirstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        for index in range(len(bins)):
            if bins[index].has_capacity_for(item):
                return nodes[index]
        raise "Not enough capacity in the cluster"

class BestFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        return self.best_fit(item, bins)

    def best_fit(self, item, bins):
        best_remaining_capacity = [bins[0].get_remaining_capacity()]
        best_fit_index = 0
        for index in range(len(bins)):
            if index == 0:
                continue
            if not bins[index].has_capacity_for(item):
                continue
            rem_capacity = bins[index].get_remaining_capacity()
            if rem_capacity[0] < best_remaining_capacity[0]:
                best_remaining_capacity = rem_capacity
                best_fit_index = index
        if best_fit_index == -1:
            raise "Not enough capacity in the cluster"
        return nodes[best_fit_index]

class WorstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        return self.worst_fit(item, bins)
    def worst_fit(self, item, nodes):
        worst_remaining_capacity = [bins[0].get_remaining_capacity()]
        worst_fit_index = 0
        for index in range(len(bins)):
            if index == 0:
                continue
            if not bins[index].has_capacity_for(item):
                continue
            rem_capacity = bins[index].get_remaining_capacity()
            if rem_capacity[0] > worst_remaining_capacity[0]:
                worst_remaining_capacity = rem_capacity
                worst_fit_index = index
        if best_fit_index == -1:
            raise "Not enough capacity in the cluster"
        return nodes[worst_fit_index]

class BestWorstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        if item[0] != 0:
            return BestFitAllocationStrategy().best_fit(item, bins)
        else:
            return WorstFitAllocationStrategy().worst_fit(item, bins)

AllocationStrategy.register(RandomAllocationStrategy)
AllocationStrategy.register(RoundRobinAllocationStrategy)
AllocationStrategy.register(FirstFitAllocationStrategy)
AllocationStrategy.register(BestFitAllocationStrategy)
AllocationStrategy.register(WorstFitAllocationStrategy)
AllocationStrategy.register(BestWorstFitAllocationStrategy)
<commit_msg>list fix..<commit_after>from abc import ABCMeta,abstractmethod
from consul import Consul
from models import Container
from random import randint

class AllocationStrategy(metaclass=ABCMeta):
    @classmethod
    def from_name(clazz, name):
        if name == "random":
            return RandomAllocationStrategy()
        elif name == "round-robin":
            return RoundRobinAllocationStrategy()
        elif name == "first-fit":
            return FirstFitAllocationStrategy()
        elif name == "best-fit":
            return BestFitAllocationStrategy()
        elif name == "worst-fit":
            return WorstFitAllocationStrategy()
        elif name == "best-worst-fit":
            return BestWorstFitAllocationStrategy()
        else:
            raise "Strategy {} not implemented.".format(name)

    @abstractmethod
    def select_node(self, nodes, service):
        return

class RandomAllocationStrategy(AllocationStrategy):
    def select_node(self, nodes, container=None):
        return nodes[randint(0, len(nodes)-1)]

class RoundRobinAllocationStrategy(AllocationStrategy):
    __nodes = None
    __current_index = 0

    def select_node(self, nodes, service):
        if RoundRobinAllocationStrategy.__nodes == None:
            RoundRobinAllocationStrategy.__nodes = nodes
            RoundRobinAllocationStrategy.__current_index = 0
        elif len(RoundRobinAllocationStrategy.__nodes) != len(nodes):
            print("List of nodes changed, reset round-robin")
            RoundRobinAllocationStrategy.__nodes = nodes
            RoundRobinAllocationStrategy.__current_index = 0

        node = RoundRobinAllocationStrategy.__nodes[RoundRobinAllocationStrategy.__current_index]
        RoundRobinAllocationStrategy.__current_index += 1
        if RoundRobinAllocationStrategy.__current_index == len(RoundRobinAllocationStrategy.__nodes):
            RoundRobinAllocationStrategy.__current_index = 0
        return node

class AnyFitAllocationStrategy(AllocationStrategy):
    def generate_problem(self, nodes, service):
        bins = list(map(lambda n: n.to_bin(), nodes))
        items = self._get_service_metrics(nodes, service)
        if len(items) == 0:
            return [0, 0, 0], bins
        item = self._average_of_items(items)
        print(item)
        print(list(map(lambda b: str(b), bins)))
        return item, bins

    def _get_service_metrics(self, nodes, service):
        containers = []
        for n in nodes:
            containers += n.containers_by_service(service)
        containers_items = list(map(lambda c: c.to_item(), containers))
        return containers_items

    def _average_of_items(self, items):
        average_item = []
        for dim in range(len(items[0])):
            sum_dim = 0
            for item in items:
                sum_dim += item[dim]
            average_dim = sum_dim / len(items)
            average_item.append(average_dim)
        return average_item

class FirstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        for index in range(len(bins)):
            if bins[index].has_capacity_for(item):
                return nodes[index]
        raise "Not enough capacity in the cluster"

class BestFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        return self.best_fit(item, bins)

    def best_fit(self, item, bins):
        best_remaining_capacity = bins[0].get_remaining_capacity()
        best_fit_index = 0
        for index in range(len(bins)):
            if index == 0:
                continue
            if not bins[index].has_capacity_for(item):
                continue
            rem_capacity = bins[index].get_remaining_capacity()
            if rem_capacity[0] < best_remaining_capacity[0]:
                best_remaining_capacity = rem_capacity
                best_fit_index = index
        if best_fit_index == -1:
            raise "Not enough capacity in the cluster"
        return nodes[best_fit_index]

class WorstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        return self.worst_fit(item, bins)
    def worst_fit(self, item, nodes):
        worst_remaining_capacity = bins[0].get_remaining_capacity()
        worst_fit_index = 0
        for index in range(len(bins)):
            if index == 0:
                continue
            if not bins[index].has_capacity_for(item):
                continue
            rem_capacity = bins[index].get_remaining_capacity()
            if rem_capacity[0] > worst_remaining_capacity[0]:
                worst_remaining_capacity = rem_capacity
                worst_fit_index = index
        if best_fit_index == -1:
            raise "Not enough capacity in the cluster"
        return nodes[worst_fit_index]

class BestWorstFitAllocationStrategy(AnyFitAllocationStrategy):
    def select_node(self, nodes, service):
        (item, bins) = self.generate_problem(nodes, service)
        if item[0] != 0:
            return BestFitAllocationStrategy().best_fit(item, bins)
        else:
            return WorstFitAllocationStrategy().worst_fit(item, bins)

AllocationStrategy.register(RandomAllocationStrategy)
AllocationStrategy.register(RoundRobinAllocationStrategy)
AllocationStrategy.register(FirstFitAllocationStrategy)
AllocationStrategy.register(BestFitAllocationStrategy)
AllocationStrategy.register(WorstFitAllocationStrategy)
AllocationStrategy.register(BestWorstFitAllocationStrategy)
<|endoftext|>


<commit_before>import cupy

def packbits(myarray):
    """Packs the elements of a binary-valued array into bits in a uint8 array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The packed array.

    .. note::
        When the input array is empty, this function returns a copy of it,
        i.e., the type of the output array is not necessarily always uint8.
        This exactly follows the NumPy's behaviour (as of version 1.11),
        alghough this is inconsistent to the documentation.

    .. seealso:: :func:`numpy.packbits`
    """
    if myarray.dtype.kind not in 'biu':
        raise TypeError('Expected an input array of integer or boolean data type')

    if myarray.size == 0:
        return cupy.empty_like(myarray)

    myarray = myarray.ravel()
    packed_size = (myarray.size + 7) // 8
    packed = cupy.zeros((packed_size,), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw T myarray, raw int32 myarray_size', 'uint8 packed',
        '''for (int j = 0; j < 8; ++j) {
            int k = i * 8 + j;
            packed |= (k < myarray_size && myarray[k] ? 1 : 0) << (7 - j);
        }''',
        'packbits_kernel'
    )(myarray, myarray.size, packed)

    return packed

def unpackbits(myarray):
    """Unpacks elements of a uint8 array into a binary-valued output array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The unpacked array.

    .. seealso:: :func:`numpy.unpackbits`
    """
    if myarray.dtype != cupy.uint8:
        raise TypeError('Expected an input array of unsigned byte data type')

    unpacked = cupy.ndarray((myarray.size * 8), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw uint8 myarray', 'T unpacked',
        'unpacked = (myarray[i / 8] >> (7 - i % 8)) & 1;',
        'unpackbits_kernel'
    )(myarray, unpacked)

    return unpacked
<commit_msg>Fix a too long line<commit_after>import cupy

def packbits(myarray):
    """Packs the elements of a binary-valued array into bits in a uint8 array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The packed array.

    .. note::
        When the input array is empty, this function returns a copy of it,
        i.e., the type of the output array is not necessarily always uint8.
        This exactly follows the NumPy's behaviour (as of version 1.11),
        alghough this is inconsistent to the documentation.

    .. seealso:: :func:`numpy.packbits`
    """
    if myarray.dtype.kind not in 'biu':
        raise TypeError(
            'Expected an input array of integer or boolean data type')

    if myarray.size == 0:
        return cupy.empty_like(myarray)

    myarray = myarray.ravel()
    packed_size = (myarray.size + 7) // 8
    packed = cupy.zeros((packed_size,), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw T myarray, raw int32 myarray_size', 'uint8 packed',
        '''for (int j = 0; j < 8; ++j) {
            int k = i * 8 + j;
            packed |= (k < myarray_size && myarray[k] ? 1 : 0) << (7 - j);
        }''',
        'packbits_kernel'
    )(myarray, myarray.size, packed)

    return packed

def unpackbits(myarray):
    """Unpacks elements of a uint8 array into a binary-valued output array.

    This function currently does not support ``axis`` option.

    Args:
        myarray (cupy.ndarray): Input array.

    Returns:
        cupy.ndarray: The unpacked array.

    .. seealso:: :func:`numpy.unpackbits`
    """
    if myarray.dtype != cupy.uint8:
        raise TypeError('Expected an input array of unsigned byte data type')

    unpacked = cupy.ndarray((myarray.size * 8), dtype=cupy.uint8)

    cupy.ElementwiseKernel(
        'raw uint8 myarray', 'T unpacked',
        'unpacked = (myarray[i / 8] >> (7 - i % 8)) & 1;',
        'unpackbits_kernel'
    )(myarray, unpacked)

    return unpacked
<|endoftext|>


<commit_before># @purpose converts an XML file into a python dict, back and forth
# @author http://code.activestate.com/recipes/573463
#         slightly adapted to follow PEP8 conventions
# based on http://alxr.usatlas.bnl.gov/lxr/source/atlas/Tools/PyUtils/python/xmldict.py
# @author "Sebastien Binet <binet@cern.ch>"
#         modified to handle attributes, namespaces..

__doc__ = """\
functions to convert an XML file into a python dict, back and forth
"""
__author__ = "Wil Tan <wil@cloudregistry.net>"

from StringIO import StringIO

# hack: LCGCMT had the py-2.5 xml.etree module hidden by mistake.
#       this is to import it, by hook or by crook
def import_etree():
    import xml
    # first try the usual way
    try:
        import xml.etree
        return xml.etree
    except ImportError:
        pass
    # do it by hook or by crook...
    import sys, os, imp
    xml_site_package = os.path.join(os.path.dirname(os.__file__), 'xml')
    m = imp.find_module('etree', [xml_site_package])

    etree = imp.load_module('xml.etree', *m)
    setattr(xml, 'etree', etree)
    return etree

etree = import_etree()
from xml.etree import ElementTree

## module data ----------------------------------------------------------------
__all__ = [
    'xml2dict',
    'dict2xml',
    ]

_BASE_NSMAP = {
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
}

## module implementation ------------------------------------------------------
class XmlDictObject(dict):
    _path = ()
    _childorder = {} # relative to _path; only useful if defined at the same level at which _path is defined/overridden
    _multi_nodes = set()

    def __init__(self, initdict=None, nsmap=None, extra_nsmap=None):
        # NOTE: setting attributes in __init__ will require special handling, see XmlDictObject
        if initdict is None:
            initdict = {}
        dict.__init__(self, initdict)

        if nsmap is None:
            nsmap = self._nsmap = _BASE_NSMAP.copy()
        nsmap.update(extra_nsmap or {})
        self._nsmap = nsmap

        nsmap_r = {}
        # build reverse map
        for prefix, uri in nsmap.iteritems():
            if uri in nsmap_r and not prefix: # default prefix should not override anything already in the rmap
                continue
            nsmap_r[uri] = prefix
        self._nsmap_r = nsmap_r

        self.__initialized = True

    def __getattr__(self, item):
        it = self
        for p in self._path:
            it = it[p]
        if item in it:
            return it[item]
        else:
            raise AttributeError("no such node (%s/%s) in: %r (self=%s)" % ('/'.join(self._path),
                                                                            item,
                                                                            it,
                                                                            super(XmlDictObject, self).__str__()))

    def __setattr__(self, item, value):
        if not self.__dict__.has_key('_XmlDictObject__initialized'):
            # this test allows attributes to be set in the __init__ method
            return super(XmlDictObject, self).__setattr__(item, value)

        if item.startswith("__"):
            super(XmlDictObject, self).__setattr__(item, value)
            return

        it = self
        for p in self._path:
            it = it.__getitem__(p)

        if type(value) is dict:
            value = XmlDictObject(value)
        return it.__setitem__(item, value)

    def __delattr__(self, item):
        if item.startswith("__"):
            super(XmlDictObject, self).__delattr__(item)
            return

        it = self
        for p in self._path:
            it = it.__getitem__(p)
        return it.__delitem__(item)

    def __str__(self):
        if '_text' in self:
            return self['_text']
        else:
            return dict.__str__(self)

    @staticmethod
    def wrap(x):
        if isinstance(x, dict):
            return XmlDictObject ((k, XmlDictObject.wrap(v))
                                  for (k, v) in x.iteritems())
        elif isinstance(x, list):
            return [XmlDictObject.wrap(v) for v in x]
        else:
            return x

    @staticmethod
    def _unwrap(x):
        if isinstance(x, dict):
            return dict ((k, XmlDictObject._unwrap(v))
                         for (k, v) in x.iteritems())
        elif isinstance(x, list):
            return [XmlDictObject._unwrap(v) for v in x]
        else:
            return x

    def to_xml(self, childorder, force_prefix=False):
        el = dict2xml(self, childorder, force_prefix=force_prefix)
        indent(el)
        return ElementTree.tostring(el)

    @classmethod
    def from_xml(cls, buf, default_prefix=None, extra_nsmap=None):
        root = ElementTree.parse(StringIO(buf)).getroot()
        rv = xml2dict(root, outerclass=cls, default_prefix=default_prefix, multi_nodes=cls._multi_nodes, extra_nsmap=extra_nsmap)
        return rv

    def unwrap(self):
        return XmlDictObject._unwrap(self)

def _dict2xml_recurse(parent, dictitem, nsmap, current_prefixes, childorder, force_prefix=False):
    """
    :param nsmap: is a dict, can be `{}`
    :param current_prefixes: is a set
    :param childorder: is a dict, can be `{}`
    """
    if '_order' in dictitem or '__order' in childorder:
        ordr = dictitem.get('_order') or childorder['__order']
        nodeorder = dict((name, i) for i,name in enumerate(ordr))
        items = sorted(dictitem.iteritems(), key=lambda x: nodeorder.get(x[0].split(":")[-1], 0))
    else:
        items = dictitem.iteritems()

    for (tag, child) in items:
        if tag in ('_order', '_nsmap'):
            continue
        if tag == '_text':
            parent.text = str(child)
        elif tag.startswith("@"):
            attrname = tag[1:]
            _do_xmlns(parent, attrname, current_prefixes, nsmap, set_default_ns=False)
            parent.set(attrname, unicode(child))
        elif type(child) in (list, tuple):
            for listchild in child:
                nsmap_recurs = nsmap
                prefixes_recurs = current_prefixes.copy()
                if ":" in tag:
                    elem = ElementTree.Element(tag)
                    prefix, uri = _do_xmlns(elem, tag, current_prefixes, nsmap, set_default_ns=not force_prefix)
                    if uri: # we will change the default namespace for children with no prefix
                        # so we need to make copies of nsmap and current_prefixes instead of updating in-place
                        nsmap_recurs = nsmap.copy()
                        if not force_prefix:
                            nsmap_recurs[''] = uri
                        prefixes_recurs = current_prefixes.union([prefix])
                elif force_prefix:
                    # tag has no prefix, take parent's
                    parent_prefix = parent.tag.split(':')[0] if ':' in parent.tag else ''
                    if parent_prefix:
                        tag = '%s:%s' % (parent_prefix, tag)
                    elem = ElementTree.Element(tag)
                else:
                    elem = ElementTree.Element(tag)

                parent.append(elem)
                if isinstance(listchild, dict):
                    _dict2xml_recurse(elem,
                                      listchild,
                                      nsmap=nsmap_recurs,
                                      current_prefixes=prefixes_recurs,
                                      childorder=childorder.get(tag, {}),
                                      force_prefix=force_prefix)
                else:
                    elem.text = unicode(listchild)
        else:
            nsmap_recurs = nsmap
            prefixes_recurs = current_prefixes.copy()
            if ":" in tag:
                elem = ElementTree.Element(tag)
                prefix, uri = _do_xmlns(elem, tag, current_prefixes, nsmap, set_default_ns=not force_prefix)
                if uri: # we will change the default namespace for children with no prefix
                    nsmap_recurs = nsmap.copy()
                    if not force_prefix:
                        nsmap_recurs[''] = uri
                    prefixes_recurs = current_prefixes.union([prefix])
            elif force_prefix:
                # tag has no prefix, take parent's
                parent_prefix = parent.tag.split(':')[0] if ':' in parent.tag else ''
                if parent_prefix:
                    tag = '%s:%s' % (parent_prefix, tag)
                elem = ElementTree.Element(tag)
            else:
                elem = ElementTree.Element(tag)

            parent.append(elem)
            if isinstance(child, dict):
                _dict2xml_recurse(elem,
                                  child,
                                  nsmap=nsmap_recurs,
                                  current_prefixes=prefixes_recurs,
                                  childorder=childorder.get(tag, {}),
                                  force_prefix=force_prefix)
            else:
                elem.text = unicode(child)

def _do_xmlns(elem, tag, prefixes, nsmap, set_default_ns=True):
    if tag.startswith("{"):
        # tag is in the form of "{NSURI}tagname", which means NSURI is not
        # known in ``nsmap``
        uri = tag[1:tag.rindex('}')]
        return '', uri

    prefix, name = tag.split(":") if ":" in tag else ('', tag)
    if prefix in prefixes:
        return prefix, None
    uri = nsmap.get(prefix)
    if uri:
        if prefix:
            elem.set('xmlns:%s' % prefix, uri)
            if set_default_ns:
                elem.set('xmlns', uri)
        else:
            if set_default_ns:
                elem.set('xmlns', uri)
    return prefix, uri

def dict2xml(xmldict, childorder, force_prefix=False):
    """convert a python dictionary into an XML tree"""
    roottag = filter(lambda x: not x.startswith("_"), xmldict.keys())[0]
    root = ElementTree.Element(roottag)

    prefixes = set()
    nsmap = getattr(xmldict, '_nsmap', {})
    if nsmap:
        prefix, uri = _do_xmlns(root, roottag, prefixes, nsmap)
        if uri:
            prefixes.add(prefix)
    _dict2xml_recurse(root, xmldict[roottag],
                      current_prefixes=prefixes,
                      nsmap=nsmap,
                      childorder=childorder,
                      force_prefix=force_prefix)
    return root

def _compute_prefix(tag, nsmap_r={}, default_prefix=None):
    if tag.startswith("{"):
        enduri = tag.index("}")
        prefix = nsmap_r.get(tag[1:enduri])
        if prefix is not None:
            tag = tag[enduri+1:]
            if prefix != default_prefix: # namespace changed
                tag = "%s:%s" % (prefix, tag)
                default_prefix = prefix
    return tag, default_prefix

def get_prefix_and_name(nsmap_r, name):
    if name.startswith('{'):
        enduri = name.index("}")
        prefix = nsmap_r.get(name[1:enduri])
        return prefix, name[enduri+1:]
    else:
        return None, name

def get_prefixed_name(nsmap_r, name):
    """meant for attributes"""
    prefix, name = get_prefix_and_name(nsmap_r, name)
    if prefix is not None:
        return "%s:%s" % (prefix, name)
    else:
        return name

def _xml2dict_recurse(node, nodedict, dictclass, nsmap, nsmap_r, default_prefix=None, parent_path=None, multi_nodes=None):
    parent_path = parent_path or tuple()
    if len(node.items()) > 0:
        # if we have attributes, set them
        ## wil/rem nodedict.update(dict(node.items()))
        nodedict.update(dict(("@%s" % get_prefixed_name(nsmap_r, k), v) for k,v in node.items()))

    for child in node:
        childtag, childprefix = _compute_prefix(child.tag, nsmap_r, default_prefix)

        #print "recursing with", childtag, "[", childprefix, "] default=", default_prefix
        # recursively add the element's children
        newitem = _xml2dict_recurse(child, dictclass(), dictclass, nsmap, nsmap_r,
                                    default_prefix=childprefix,
                                    multi_nodes=multi_nodes,
                                    parent_path=parent_path+(childtag,))

        nodeval = nodedict.get(childtag)
        if nodeval is not None:
            # found duplicate tag, force a list
            if type(nodeval) is list:
                # append to existing list
                nodeval.append(newitem)
            else:
                # convert to list
                nodedict[childtag] = [nodeval, newitem]
        else:
            nodedict.setdefault('_order', []).append(childtag)
            nodepath = parent_path + (childtag,)
            if multi_nodes and nodepath in multi_nodes:
                # if this node is configured to appear multiple times, put it in a list
                nodedict[childtag] = [newitem]
            else:
                # only one, directly set the dictionary
                nodedict[childtag] = newitem

    if node.text is None:
        text = ''
    else:
        text = node.text.strip()

    if len(nodedict) > 0:
        # if we have a dictionary add the text as a dictionary value
        # (if there is any)
        if len(text) > 0:
            nodedict['_text'] = text
    else:
        # if we don't have child nodes or attributes, just set the text
        nodedict = node.text.strip() if node.text else ""

    #print "end nodedict = %r" % (nodedict,)
    return nodedict

def xml2dict(root, dictclass=XmlDictObject, outerclass=XmlDictObject, default_prefix=None, multi_nodes=None, extra_nsmap=None):
    """convert an xml tree into a python dictionary
    """
    rootnode = dictclass()
    # we cheat a bit, instantiate it to get the nsmap and nsmap_r

    outer = outerclass(extra_nsmap=extra_nsmap)
    nsmap = outer._nsmap
    nsmap_r = outer._nsmap_r

    tag, default_prefix = _compute_prefix(root.tag, nsmap_r, default_prefix)
    outer[tag] = _xml2dict_recurse(root, rootnode, dictclass, nsmap=nsmap, nsmap_r=nsmap_r,
                                   default_prefix=default_prefix, parent_path=(tag,),
                                   multi_nodes=multi_nodes)
    return outer

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

<commit_msg>fix child ordering when force_prefix is used<commit_after># @purpose converts an XML file into a python dict, back and forth
# @author http://code.activestate.com/recipes/573463
#         slightly adapted to follow PEP8 conventions
# based on http://alxr.usatlas.bnl.gov/lxr/source/atlas/Tools/PyUtils/python/xmldict.py
# @author "Sebastien Binet <binet@cern.ch>"
#         modified to handle attributes, namespaces..

__doc__ = """\
functions to convert an XML file into a python dict, back and forth
"""
__author__ = "Wil Tan <wil@cloudregistry.net>"

from StringIO import StringIO

# hack: LCGCMT had the py-2.5 xml.etree module hidden by mistake.
#       this is to import it, by hook or by crook
def import_etree():
    import xml
    # first try the usual way
    try:
        import xml.etree
        return xml.etree
    except ImportError:
        pass
    # do it by hook or by crook...
    import sys, os, imp
    xml_site_package = os.path.join(os.path.dirname(os.__file__), 'xml')
    m = imp.find_module('etree', [xml_site_package])

    etree = imp.load_module('xml.etree', *m)
    setattr(xml, 'etree', etree)
    return etree

etree = import_etree()
from xml.etree import ElementTree

## module data ----------------------------------------------------------------
__all__ = [
    'xml2dict',
    'dict2xml',
    ]

_BASE_NSMAP = {
    'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
}

## module implementation ------------------------------------------------------
class XmlDictObject(dict):
    _path = ()
    _childorder = {} # relative to _path; only useful if defined at the same level at which _path is defined/overridden
    _multi_nodes = set()

    def __init__(self, initdict=None, nsmap=None, extra_nsmap=None):
        # NOTE: setting attributes in __init__ will require special handling, see XmlDictObject
        if initdict is None:
            initdict = {}
        dict.__init__(self, initdict)

        if nsmap is None:
            nsmap = self._nsmap = _BASE_NSMAP.copy()
        nsmap.update(extra_nsmap or {})
        self._nsmap = nsmap

        nsmap_r = {}
        # build reverse map
        for prefix, uri in nsmap.iteritems():
            if uri in nsmap_r and not prefix: # default prefix should not override anything already in the rmap
                continue
            nsmap_r[uri] = prefix
        self._nsmap_r = nsmap_r

        self.__initialized = True

    def __getattr__(self, item):
        it = self
        for p in self._path:
            it = it[p]
        if item in it:
            return it[item]
        else:
            raise AttributeError("no such node (%s/%s) in: %r (self=%s)" % ('/'.join(self._path),
                                                                            item,
                                                                            it,
                                                                            super(XmlDictObject, self).__str__()))

    def __setattr__(self, item, value):
        if not self.__dict__.has_key('_XmlDictObject__initialized'):
            # this test allows attributes to be set in the __init__ method
            return super(XmlDictObject, self).__setattr__(item, value)

        if item.startswith("__"):
            super(XmlDictObject, self).__setattr__(item, value)
            return

        it = self
        for p in self._path:
            it = it.__getitem__(p)

        if type(value) is dict:
            value = XmlDictObject(value)
        return it.__setitem__(item, value)

    def __delattr__(self, item):
        if item.startswith("__"):
            super(XmlDictObject, self).__delattr__(item)
            return

        it = self
        for p in self._path:
            it = it.__getitem__(p)
        return it.__delitem__(item)

    def __str__(self):
        if '_text' in self:
            return self['_text']
        else:
            return dict.__str__(self)

    @staticmethod
    def wrap(x):
        if isinstance(x, dict):
            return XmlDictObject ((k, XmlDictObject.wrap(v))
                                  for (k, v) in x.iteritems())
        elif isinstance(x, list):
            return [XmlDictObject.wrap(v) for v in x]
        else:
            return x

    @staticmethod
    def _unwrap(x):
        if isinstance(x, dict):
            return dict ((k, XmlDictObject._unwrap(v))
                         for (k, v) in x.iteritems())
        elif isinstance(x, list):
            return [XmlDictObject._unwrap(v) for v in x]
        else:
            return x

    def to_xml(self, childorder, force_prefix=False):
        el = dict2xml(self, childorder, force_prefix=force_prefix)
        indent(el)
        return ElementTree.tostring(el)

    @classmethod
    def from_xml(cls, buf, default_prefix=None, extra_nsmap=None):
        root = ElementTree.parse(StringIO(buf)).getroot()
        rv = xml2dict(root, outerclass=cls, default_prefix=default_prefix, multi_nodes=cls._multi_nodes, extra_nsmap=extra_nsmap)
        return rv

    def unwrap(self):
        return XmlDictObject._unwrap(self)

def _dict2xml_recurse(parent, dictitem, nsmap, current_prefixes, childorder, force_prefix=False):
    """
    :param nsmap: is a dict, can be `{}`
    :param current_prefixes: is a set
    :param childorder: is a dict, can be `{}`
    """
    if '_order' in dictitem or '__order' in childorder:
        ordr = dictitem.get('_order') or childorder['__order']
        nodeorder = dict((name, i) for i,name in enumerate(ordr))
        items = sorted(dictitem.iteritems(), key=lambda x: nodeorder.get(x[0].split(":")[-1], 0))
    else:
        items = dictitem.iteritems()

    parent_prefix = parent.tag.partition(':')[0] if ':' in parent.tag else ''
    for (tag, child) in items:
        if tag in ('_order', '_nsmap'):
            continue
        if tag == '_text':
            parent.text = unicode(child)
        elif tag.startswith("@"):
            attrname = tag[1:]
            _do_xmlns(parent, attrname, current_prefixes, nsmap, set_default_ns=False)
            parent.set(attrname, unicode(child))
        elif type(child) in (list, tuple):
            for listchild in child:
                nsmap_recurs = nsmap
                prefixes_recurs = current_prefixes.copy()
                if ":" in tag:
                    elem = ElementTree.Element(tag)
                    prefix, uri = _do_xmlns(elem, tag, current_prefixes, nsmap, set_default_ns=not force_prefix)
                    if uri: # we will change the default namespace for children with no prefix
                        # so we need to make copies of nsmap and current_prefixes instead of updating in-place
                        nsmap_recurs = nsmap.copy()
                        if not force_prefix:
                            nsmap_recurs[''] = uri
                        prefixes_recurs = current_prefixes.union([prefix])
                else:
                    # tag has no prefix
                    if force_prefix:
                        # take parent's prefix
                        if parent_prefix:
                            tag = '%s:%s' % (parent_prefix, tag)
                    elem = ElementTree.Element(tag)

                parent.append(elem)
                if isinstance(listchild, dict):
                    tag_prefix = tag.partition(':')[0] if ':' in tag else ''
                    reltag = tag.rpartition(':')[2] if not tag_prefix or tag_prefix == parent_prefix else tag
                    _dict2xml_recurse(elem,
                                      listchild,
                                      nsmap=nsmap_recurs,
                                      current_prefixes=prefixes_recurs,
                                      childorder=childorder.get(reltag, {}),
                                      force_prefix=force_prefix)
                else:
                    elem.text = unicode(listchild)
        else:
            nsmap_recurs = nsmap
            prefixes_recurs = current_prefixes.copy()
            if ":" in tag:
                elem = ElementTree.Element(tag)
                prefix, uri = _do_xmlns(elem, tag, current_prefixes, nsmap, set_default_ns=not force_prefix)
                if uri: # we will change the default namespace for children with no prefix
                    nsmap_recurs = nsmap.copy()
                    if not force_prefix:
                        nsmap_recurs[''] = uri
                    prefixes_recurs = current_prefixes.union([prefix])
            else:
                # tag has no prefix
                if force_prefix:
                    # take parent's prefix
                    if parent_prefix:
                        tag = '%s:%s' % (parent_prefix, tag)
                elem = ElementTree.Element(tag)

            parent.append(elem)
            if isinstance(child, dict):
                tag_prefix = tag.partition(':')[0] if ':' in tag else ''
                reltag = tag.rpartition(':')[2] if not tag_prefix or tag_prefix == parent_prefix else tag
                _dict2xml_recurse(elem,
                                  child,
                                  nsmap=nsmap_recurs,
                                  current_prefixes=prefixes_recurs,
                                  childorder=childorder.get(reltag, {}),
                                  force_prefix=force_prefix)
            else:
                elem.text = unicode(child)

def _do_xmlns(elem, tag, prefixes, nsmap, set_default_ns=True):
    if tag.startswith("{"):
        # tag is in the form of "{NSURI}tagname", which means NSURI is not
        # known in ``nsmap``
        uri = tag[1:tag.rindex('}')]
        return '', uri

    prefix, name = tag.split(":") if ":" in tag else ('', tag)
    if prefix in prefixes:
        return prefix, None
    uri = nsmap.get(prefix)
    if uri:
        if prefix:
            elem.set('xmlns:%s' % prefix, uri)
            if set_default_ns:
                elem.set('xmlns', uri)
        else:
            if set_default_ns:
                elem.set('xmlns', uri)
    return prefix, uri

def dict2xml(xmldict, childorder, force_prefix=False):
    """convert a python dictionary into an XML tree"""
    roottag = filter(lambda x: not x.startswith("_"), xmldict.keys())[0]
    root = ElementTree.Element(roottag)

    prefixes = set()
    nsmap = getattr(xmldict, '_nsmap', {})
    if nsmap:
        prefix, uri = _do_xmlns(root, roottag, prefixes, nsmap)
        if uri:
            prefixes.add(prefix)
    _dict2xml_recurse(root, xmldict[roottag],
                      current_prefixes=prefixes,
                      nsmap=nsmap,
                      childorder=childorder,
                      force_prefix=force_prefix)
    return root

def _compute_prefix(tag, nsmap_r={}, default_prefix=None):
    if tag.startswith("{"):
        enduri = tag.index("}")
        prefix = nsmap_r.get(tag[1:enduri])
        if prefix is not None:
            tag = tag[enduri+1:]
            if prefix != default_prefix: # namespace changed
                tag = "%s:%s" % (prefix, tag)
                default_prefix = prefix
    return tag, default_prefix

def get_prefix_and_name(nsmap_r, name):
    if name.startswith('{'):
        enduri = name.index("}")
        prefix = nsmap_r.get(name[1:enduri])
        return prefix, name[enduri+1:]
    else:
        return None, name

def get_prefixed_name(nsmap_r, name):
    """meant for attributes"""
    prefix, name = get_prefix_and_name(nsmap_r, name)
    if prefix is not None:
        return "%s:%s" % (prefix, name)
    else:
        return name

def _xml2dict_recurse(node, nodedict, dictclass, nsmap, nsmap_r, default_prefix=None, parent_path=None, multi_nodes=None):
    parent_path = parent_path or tuple()
    if len(node.items()) > 0:
        # if we have attributes, set them
        ## wil/rem nodedict.update(dict(node.items()))
        nodedict.update(dict(("@%s" % get_prefixed_name(nsmap_r, k), v) for k,v in node.items()))

    for child in node:
        childtag, childprefix = _compute_prefix(child.tag, nsmap_r, default_prefix)

        #print "recursing with", childtag, "[", childprefix, "] default=", default_prefix
        # recursively add the element's children
        newitem = _xml2dict_recurse(child, dictclass(), dictclass, nsmap, nsmap_r,
                                    default_prefix=childprefix,
                                    multi_nodes=multi_nodes,
                                    parent_path=parent_path+(childtag,))

        nodeval = nodedict.get(childtag)
        if nodeval is not None:
            # found duplicate tag, force a list
            if type(nodeval) is list:
                # append to existing list
                nodeval.append(newitem)
            else:
                # convert to list
                nodedict[childtag] = [nodeval, newitem]
        else:
            nodedict.setdefault('_order', []).append(childtag)
            nodepath = parent_path + (childtag,)
            if multi_nodes and nodepath in multi_nodes:
                # if this node is configured to appear multiple times, put it in a list
                nodedict[childtag] = [newitem]
            else:
                # only one, directly set the dictionary
                nodedict[childtag] = newitem

    if node.text is None:
        text = ''
    else:
        text = node.text.strip()

    if len(nodedict) > 0:
        # if we have a dictionary add the text as a dictionary value
        # (if there is any)
        if len(text) > 0:
            nodedict['_text'] = text
    else:
        # if we don't have child nodes or attributes, just set the text
        nodedict = node.text.strip() if node.text else ""

    #print "end nodedict = %r" % (nodedict,)
    return nodedict

def xml2dict(root, dictclass=XmlDictObject, outerclass=XmlDictObject, default_prefix=None, multi_nodes=None, extra_nsmap=None):
    """convert an xml tree into a python dictionary
    """
    rootnode = dictclass()
    # we cheat a bit, instantiate it to get the nsmap and nsmap_r

    outer = outerclass(extra_nsmap=extra_nsmap)
    nsmap = outer._nsmap
    nsmap_r = outer._nsmap_r

    tag, default_prefix = _compute_prefix(root.tag, nsmap_r, default_prefix)
    outer[tag] = _xml2dict_recurse(root, rootnode, dictclass, nsmap=nsmap, nsmap_r=nsmap_r,
                                   default_prefix=default_prefix, parent_path=(tag,),
                                   multi_nodes=multi_nodes)
    return outer

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

<|endoftext|>