<commit_before>import os, sys, re
from item_object import Item
from spooky_logs import log
from error_handling import Error
from ui import GUI_INTERFACE

class Ghost(object, GUI_INTERFACE):
    def __init__(self, ocr_key):
        self.METHODS = {}
        self.currentresults = []
        self.ocr_key = ocr_key

        directory = './'+sys.argv[0]+'/imgs' # the ./ may be a problem?
        names = os.listdir(directory)
        self.Items = {
            re.sub('(\.PNG)', '', name): Item(directory+"/"+name, self.ocr_key)
            for name in names
        }

    def __getitem__(self, key):
        return self.Items[key]

    def new_method(self, fn):
        self.METHODS[fn.__name__] = fn
        return fn

    def start(self, obj, cmd):
        raise NotImplementedError("start(obj, cmd) must be implemented.")

    def failed(self, e, obj, cmd):
        raise NotImplementedError("failed(e, obj, cmd) must be implemented")

    def completed(self, obj, cmd):
        raise NotImplementedError("completed(obj, cmd) must be implemented")

    @log
    def click_all(self, *args):
        for i in args:
            self[i].click()

    @log
    def wait_for_all(self, *args, **kwargs):
        theres = []
        while theres != [True]*len(args):
            theres = []
            for i in args:
                theres.append(self[i].found)
            self.keywrite(*kwargs["typewrite_between"])

    def run(self, obj=None, cmd=None, cmd_args=None, cmd_kwargs=None):
        try:
            self.start(obj, cmd)
            currentresults = METHODS[cmd](*cmd_args, **cmd_kwargs)
        except Error as e:
            self.failed(e, obj, cmd)
        else:
            self.completed(obj, cmd)

    # def run_all(filename="commands.csv", model=[]):
    #     self.
<commit_msg>sanity print checks<commit_after>import os, sys, re
from item_object import Item
from spooky_logs import log
from error_handling import Error
from ui import GUI_INTERFACE

class Ghost(object, GUI_INTERFACE):
    def __init__(self, ocr_key):
        self.METHODS = {}
        self.currentresults = []
        self.ocr_key = ocr_key

        directory = './'+sys.argv[0]+'/imgs' # the ./ may be a problem?
        names = os.listdir(directory)
        self.Items = {
            re.sub('(\.PNG)', '', name): Item(directory+"/"+name, self.ocr_key)
            for name in names
        }

    def __getitem__(self, key):
        return self.Items[key]

    def new_method(self, fn):
        self.METHODS[fn.__name__] = fn
        return fn

    def start(self, obj, cmd):
        raise NotImplementedError("start(obj, cmd) must be implemented.")

    def failed(self, e, obj, cmd):
        raise NotImplementedError("failed(e, obj, cmd) must be implemented")

    def completed(self, obj, cmd):
        raise NotImplementedError("completed(obj, cmd) must be implemented")

    @log
    def click_all(self, *args):
        for i in args:
            self[i].click()

    @log
    def wait_for_all(self, *args, **kwargs):
        theres = []
        print(theres)
        while theres != [True]*len(args):
            theres = []
            for i in args:
                theres.append(self[i].found)
            print(theres)
            print([True]*len(args))
            self.keywrite(*kwargs["typewrite_between"])

    def run(self, obj=None, cmd=None, cmd_args=None, cmd_kwargs=None):
        try:
            self.start(obj, cmd)
            currentresults = METHODS[cmd](*cmd_args, **cmd_kwargs)
        except Error as e:
            self.failed(e, obj, cmd)
        else:
            self.completed(obj, cmd)

    # def run_all(filename="commands.csv", model=[]):
    #     self.
<|endoftext|>


<commit_before>########################################################################
# test/xslt/test_basics.py
import cStringIO

from amara.xslt.processor import processor
from amara.lib import inputsource, treecompare
from amara.xpath import util

def _run(source_xml, transform_xml, expected, parameters,
              compare_method):
    P = processor()
    source = inputsource(source_xml)
    transform = inputsource(transform_xml)
    P.append_transform(transform)
    if parameters is not None:
        parameters = util.parameterize(parameters)
    result = str(P.run(source, parameters=parameters))
    diff = compare_method(result, expected)
    diff = list(diff)
    assert not diff, (source_xml, transform_xml, result, expected, diff)

def _run_html(source_xml, transform_xml, expected, parameters=None):
    _run(source_xml, transform_xml, expected, parameters,
         treecompare.html_diff)

def _run_xml(source_xml, transform_xml, expected, parameters=None):
    _run(source_xml, transform_xml, expected, parameters,
         treecompare.xml_diff)

def test_basics_1():
    _run_html('xslt/addr_book1.xml', 'xslt/addr_book1.xsl',
         """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <title>Address Book</title>

Tabulate Just Names and Phone Numbers

\x20\x20\x20\x20

        Pieter Aaron
        (Work) 404-555-1234
(Fax) 404-555-4321
(Pager) 404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi
        (Work) 767-555-7676
(Fax) 767-555-7642
(Pager) 800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev
        (Work) 000-987-6543
(Cell) 000-000-0000

""")

"""  XXX FIXME
    def test_transform_output(self):
        from amara.xslt import transform
        io = cStringIO.StringIO()
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
 """

def test_basics_2():
    _run_html('xslt/addr_book1.xml',
         """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  >
  <xsl:output method="html"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ENTRY">

        <xsl:apply-templates select='NAME|PHONENUM'/>

  </xsl:template>

  <xsl:template match="NAME">

      <xsl:apply-templates/>

  </xsl:template>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

\x20\x20\x20\x20

        Pieter Aaron	404-555-1234404-555-4321404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi	767-555-7676767-555-7642800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev	000-987-6543000-000-0000

""")

# This fails because of differences in whitespace
def test_basics_3():
    _run_html('xslt/addr_book1.xml',
         """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  >

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ADDRBOOK">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ENTRY">

      <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="NAME">

      <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="*"/>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

        Pieter Aaron

        Emeka Ndubuisi

        Vasia Zhugenev

""")

# FIXME: This has an embedded DTD and the original test set the
# "validate" flag, but the test code didn't actually validate.
# I'm not validating either.

def test_basics_4():
    _run_html("""<?xml version = "1.0"?>
<?xml-stylesheet type="text/xml" href="Xml/Xslt/Core/addr_book1.xsl"?>
<!DOCTYPE ADDRBOOK [
  <!ELEMENT ADDRBOOK (ENTRY*)>
  <!ATTLIST ADDRBOOK
    xmlns CDATA #FIXED 'http://spam.org'
  >
  <!ELEMENT ENTRY (NAME, ADDRESS, PHONENUM*, EMAIL)>
  <!ATTLIST ENTRY
    ID ID #REQUIRED
  >
  <!ELEMENT NAME (#PCDATA)>
  <!ELEMENT ADDRESS (#PCDATA)>
  <!ELEMENT PHONENUM (#PCDATA)>
  <!ATTLIST PHONENUM
    DESC CDATA #REQUIRED
  >
  <!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRBOOK>
    <ENTRY ID="pa">
        <NAME>Pieter Aaron</NAME>
        <ADDRESS>404 Error Way</ADDRESS>
        <PHONENUM DESC="Work">404-555-1234</PHONENUM>
        <PHONENUM DESC="Fax">404-555-4321</PHONENUM>
        <PHONENUM DESC="Pager">404-555-5555</PHONENUM>
        <EMAIL>pieter.aaron@inter.net</EMAIL>
    </ENTRY>
    <ENTRY ID="en">
        <NAME>Emeka Ndubuisi</NAME>
        <ADDRESS>42 Spam Blvd</ADDRESS>
        <PHONENUM DESC="Work">767-555-7676</PHONENUM>
        <PHONENUM DESC="Fax">767-555-7642</PHONENUM>
        <PHONENUM DESC="Pager">800-SKY-PAGEx767676</PHONENUM>
        <EMAIL>endubuisi@spamtron.com</EMAIL>
    </ENTRY>
    <ENTRY ID="vz">
        <NAME>Vasia Zhugenev</NAME>
        <ADDRESS>2000 Disaster Plaza</ADDRESS>
        <PHONENUM DESC="Work">000-987-6543</PHONENUM>
        <PHONENUM DESC="Cell">000-000-0000</PHONENUM>
        <EMAIL>vxz@magog.ru</EMAIL>
    </ENTRY>
</ADDRBOOK>""", """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:A="http://spam.org"
  version="1.0"
  >

  <xsl:output method="html"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="A:ENTRY">

        <xsl:apply-templates select='A:NAME|A:PHONENUM'/>

  </xsl:template>

  <xsl:template match="A:NAME">

      <xsl:apply-templates/>

  </xsl:template>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

\x20\x20\x20\x20

        Pieter Aaron	404-555-1234404-555-4321404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi	767-555-7676767-555-7642800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev	000-987-6543000-000-0000

""")

def test_basics_5():
    _run_xml("<dummy/>",
         """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

  <xsl:param name='override' select='"abc"'/>
  <xsl:param name='list' select='foo'/>

  <xsl:template match="/">
    <doc>
      <overridden><xsl:value-of select='$override'/></overridden>
      <list><xsl:apply-templates select="$list"/></list>
    </doc>
  </xsl:template>

  <xsl:template match="text()">
    <item><xsl:value-of select="."/></item>
  </xsl:template>

</xsl:stylesheet>
""", """<?xml version='1.0' encoding='UTF-8'?>
<doc><overridden>xyz</overridden><list><item>a</item><item>b</item><item>c</item></list></doc>""",
    parameters = {'override': 'xyz', 'list': ('a', 'b', 'c')}
         )

    # Appending explicit stylesheet when xml-stylesheet PI already
    # specifies one results in both stylesheets being appended. If
    # it's the same stylesheet, it's an error.
    #
    #source = test_harness.FileInfo(uri='Xml/Xslt/Core/addr_book1.xml')
    #sheet = test_harness.FileInfo(uri='Xml/Xslt/Core/addr_book1.xsl')
    #    test_harness.XsltTest(tester, source, [sheet], EXPECTED1, ignorePis=0,
    #                          title='test 5')

OUTPUT_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
 <xsl:strip-space elements="*"/>
 <xsl:output %s/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:transform>
"""

def test_basics_6():
    'Basic output parameter test, 1'
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml" indent="yes"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_7():
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml" indent="no"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_8():
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_9():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html" indent="no"',
         """

""")

def test_basics_10():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html" indent="yes"',
         expected = """

""")

def test_basics_11():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html"',
         """

""")

if __name__ == '__main__':
    raise SystemExit("Use nosetests")
<commit_msg>Added option to set the document URI, instead of using the default<commit_after>########################################################################
# test/xslt/test_basics.py
import cStringIO

from amara.xslt.processor import processor
from amara.lib import inputsource, treecompare
from amara.xpath import util

def _run(source_xml, transform_xml, expected, parameters,
              compare_method, source_uri=None, transform_uri=None):
    P = processor()
    source = inputsource(source_xml, source_uri)
    transform = inputsource(transform_xml, transform_uri)
    P.append_transform(transform)
    if parameters is not None:
        parameters = util.parameterize(parameters)
    result = str(P.run(source, parameters=parameters))
    diff = compare_method(result, expected)
    diff = list(diff)
    assert not diff, (source_xml, transform_xml, result, expected, diff)

def _run_html(source_xml, transform_xml, expected, parameters=None,
              source_uri=None, transform_uri=None):
    _run(source_xml, transform_xml, expected, parameters,
         treecompare.html_diff, source_uri, transform_uri)

def _run_xml(source_xml, transform_xml, expected, parameters=None,
              source_uri=None, transform_uri=None):
    _run(source_xml, transform_xml, expected, parameters,
         treecompare.xml_diff, source_uri, transform_uri)

def test_basics_1():
    _run_html('xslt/addr_book1.xml', 'xslt/addr_book1.xsl',
         """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <title>Address Book</title>

Tabulate Just Names and Phone Numbers

\x20\x20\x20\x20

        Pieter Aaron
        (Work) 404-555-1234
(Fax) 404-555-4321
(Pager) 404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi
        (Work) 767-555-7676
(Fax) 767-555-7642
(Pager) 800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev
        (Work) 000-987-6543
(Cell) 000-000-0000

""")

"""  XXX FIXME
    def test_transform_output(self):
        from amara.xslt import transform
        io = cStringIO.StringIO()
        result = transform(self.source, self.transform, output=io)
        self.assert_(treecompare.html_compare(self.expected, io.getvalue()))
        return
 """

def test_basics_2():
    _run_html('xslt/addr_book1.xml',
         """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  >
  <xsl:output method="html"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ENTRY">

        <xsl:apply-templates select='NAME|PHONENUM'/>

  </xsl:template>

  <xsl:template match="NAME">

      <xsl:apply-templates/>

  </xsl:template>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

\x20\x20\x20\x20

        Pieter Aaron	404-555-1234404-555-4321404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi	767-555-7676767-555-7642800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev	000-987-6543000-000-0000

""")

# This fails because of differences in whitespace
def test_basics_3():
    _run_html('xslt/addr_book1.xml',
         """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  >

  <xsl:strip-space elements="*"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="ADDRBOOK">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="ENTRY">

      <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="NAME">

      <xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="*"/>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

        Pieter Aaron

        Emeka Ndubuisi

        Vasia Zhugenev

""")

# FIXME: This has an embedded DTD and the original test set the
# "validate" flag, but the test code didn't actually validate.
# I'm not validating either.

def test_basics_4():
    _run_html("""<?xml version = "1.0"?>
<?xml-stylesheet type="text/xml" href="Xml/Xslt/Core/addr_book1.xsl"?>
<!DOCTYPE ADDRBOOK [
  <!ELEMENT ADDRBOOK (ENTRY*)>
  <!ATTLIST ADDRBOOK
    xmlns CDATA #FIXED 'http://spam.org'
  >
  <!ELEMENT ENTRY (NAME, ADDRESS, PHONENUM*, EMAIL)>
  <!ATTLIST ENTRY
    ID ID #REQUIRED
  >
  <!ELEMENT NAME (#PCDATA)>
  <!ELEMENT ADDRESS (#PCDATA)>
  <!ELEMENT PHONENUM (#PCDATA)>
  <!ATTLIST PHONENUM
    DESC CDATA #REQUIRED
  >
  <!ELEMENT EMAIL (#PCDATA)>
]>
<ADDRBOOK>
    <ENTRY ID="pa">
        <NAME>Pieter Aaron</NAME>
        <ADDRESS>404 Error Way</ADDRESS>
        <PHONENUM DESC="Work">404-555-1234</PHONENUM>
        <PHONENUM DESC="Fax">404-555-4321</PHONENUM>
        <PHONENUM DESC="Pager">404-555-5555</PHONENUM>
        <EMAIL>pieter.aaron@inter.net</EMAIL>
    </ENTRY>
    <ENTRY ID="en">
        <NAME>Emeka Ndubuisi</NAME>
        <ADDRESS>42 Spam Blvd</ADDRESS>
        <PHONENUM DESC="Work">767-555-7676</PHONENUM>
        <PHONENUM DESC="Fax">767-555-7642</PHONENUM>
        <PHONENUM DESC="Pager">800-SKY-PAGEx767676</PHONENUM>
        <EMAIL>endubuisi@spamtron.com</EMAIL>
    </ENTRY>
    <ENTRY ID="vz">
        <NAME>Vasia Zhugenev</NAME>
        <ADDRESS>2000 Disaster Plaza</ADDRESS>
        <PHONENUM DESC="Work">000-987-6543</PHONENUM>
        <PHONENUM DESC="Cell">000-000-0000</PHONENUM>
        <EMAIL>vxz@magog.ru</EMAIL>
    </ENTRY>
</ADDRBOOK>""", """<?xml version="1.0"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:A="http://spam.org"
  version="1.0"
  >

  <xsl:output method="html"/>

  <xsl:template match="/">

    <TITLE>Address Book</TITLE>

<xsl:text>Tabulate just the Names</xsl:text>

<xsl:apply-templates/>

  </xsl:template>

  <xsl:template match="A:ENTRY">

        <xsl:apply-templates select='A:NAME|A:PHONENUM'/>

  </xsl:template>

  <xsl:template match="A:NAME">

      <xsl:apply-templates/>

  </xsl:template>

</xsl:stylesheet>
""", """

    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
    <TITLE>Address Book</TITLE>

Tabulate just the Names

\x20\x20\x20\x20

        Pieter Aaron	404-555-1234404-555-4321404-555-5555

\x20\x20\x20\x20

        Emeka Ndubuisi	767-555-7676767-555-7642800-SKY-PAGEx767676

\x20\x20\x20\x20

        Vasia Zhugenev	000-987-6543000-000-0000

""")

def test_basics_5():
    _run_xml("<dummy/>",
         """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

  <xsl:param name='override' select='"abc"'/>
  <xsl:param name='list' select='foo'/>

  <xsl:template match="/">
    <doc>
      <overridden><xsl:value-of select='$override'/></overridden>
      <list><xsl:apply-templates select="$list"/></list>
    </doc>
  </xsl:template>

  <xsl:template match="text()">
    <item><xsl:value-of select="."/></item>
  </xsl:template>

</xsl:stylesheet>
""", """<?xml version='1.0' encoding='UTF-8'?>
<doc><overridden>xyz</overridden><list><item>a</item><item>b</item><item>c</item></list></doc>""",
    parameters = {'override': 'xyz', 'list': ('a', 'b', 'c')}
         )

    # Appending explicit stylesheet when xml-stylesheet PI already
    # specifies one results in both stylesheets being appended. If
    # it's the same stylesheet, it's an error.
    #
    #source = test_harness.FileInfo(uri='Xml/Xslt/Core/addr_book1.xml')
    #sheet = test_harness.FileInfo(uri='Xml/Xslt/Core/addr_book1.xsl')
    #    test_harness.XsltTest(tester, source, [sheet], EXPECTED1, ignorePis=0,
    #                          title='test 5')

OUTPUT_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
 <xsl:strip-space elements="*"/>
 <xsl:output %s/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:transform>
"""

def test_basics_6():
    'Basic output parameter test, 1'
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml" indent="yes"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_7():
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml" indent="no"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_8():
    _run_xml('

',
         OUTPUT_TEMPLATE % 'method="xml"',
         """<?xml version="1.0" encoding="UTF-8"?>

""")

def test_basics_9():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html" indent="no"',
         """

""")

def test_basics_10():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html" indent="yes"',
         expected = """

""")

def test_basics_11():
    _run_html('

',
         OUTPUT_TEMPLATE % 'method="html"',
         """

""")

if __name__ == '__main__':
    raise SystemExit("Use nosetests")
<|endoftext|>


<commit_before>"""
 mbed CMSIS-DAP debugger
 Copyright (c) 2006-2013 ARM Limited

 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 logging
import array
from transport import TransferError

COMMAND_ID = {'DAP_INFO': 0x00,
              'DAP_LED': 0x01,
              'DAP_CONNECT': 0x02,
              'DAP_DISCONNECT': 0x03,
              'DAP_TRANSFER_CONFIGURE': 0x04,
              'DAP_TRANSFER': 0x05,
              'DAP_TRANSFER_BLOCK': 0x06,
              'DAP_TRANSFER_ABORT': 0x07,
              'DAP_WRITE_ABORT': 0x08,
              'DAP_DELAY': 0x09,
              'DAP_RESET_TARGET': 0x0a,
              'DAP_SWJ_PINS': 0x10,
              'DAP_SWJ_CLOCK': 0x11,
              'DAP_SWJ_SEQUENCE': 0x12,
              'DAP_SWD_CONFIGURE': 0x13,
              'DAP_JTAG_SEQUENCE': 0x14,
              'DAP_JTAG_CONFIGURE': 0x15,
              'DAP_JTAG_IDCODE': 0x16,
              'DAP_VENDOR0': 0x80,
              }

ID_INFO = {'VENDOR_ID': 0x01,
           'PRODUCT_ID': 0x02,
           'SERIAL_NUMBER': 0x03,
           'CMSIS_DAP_FW_VERSION': 0x04,
           'TARGET_DEVICE_VENDOR': 0x05,
           'TARGET_DEVICE_NAME': 0x06,
           'CAPABILITIES': 0xf0,
           'PACKET_COUNT': 0xfe,
           'PACKET_SIZE': 0xff
           }

PINS = {'None': 0x00,
        'SWCLK_TCK': (1 << 0),
        'SWDIO_TMS': (1 << 1),
        'TDI': (1 << 2),
        'TDO': (1 << 3),
        'nTRST': (1 << 5),
        'nRESET': (1 << 7),
        }

DAP_DEFAULT_PORT = 0
DAP_SWD_PORT = 1
DAP_JTAG_POR = 2

DAP_OK = 0
DAP_ERROR = 0xff

# Responses to DAP_Transfer and DAP_TransferBlock
DAP_TRANSFER_OK = 1
DAP_TRANSFER_WAIT = 2
DAP_TRANSFER_FAULT = 4

MAX_PACKET_SIZE = 0x0E

## @brief This class implements the CMSIS-DAP wire protocol.
class CMSIS_DAP_Protocol(object):
    def __init__(self, interface):
        self.interface = interface

    def dapInfo(self, id_):
        cmd = []
        cmd.append(COMMAND_ID['DAP_INFO'])
        cmd.append(ID_INFO[id_])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_INFO']:
            raise ValueError('DAP_INFO response error')

        if resp[1] == 0:
            return

        # Integer values
        if id_ in ('CAPABILITIES', 'PACKET_COUNT', 'PACKET_SIZE'):
            if resp[1] == 1:
                return resp[2]
            if resp[1] == 2:
                return (resp[3] << 8) | resp[2]

        # String values
        x = array.array('B', [i for i in resp[2:2+resp[1]]])

        return x.tostring()

    def setLed(interface):
        #not yet implemented
        return

    def connect(self, mode = DAP_DEFAULT_PORT):
        cmd = []
        cmd.append(COMMAND_ID['DAP_CONNECT'])
        cmd.append(mode)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_CONNECT']:
            raise ValueError('DAP_CONNECT response error')

        if resp[1] == 0:
            raise ValueError('DAP Connect failed')

        if resp[1] == 1:
            logging.info('DAP SWD MODE initialised')

        if resp[1] == 2:
            logging.info('DAP JTAG MODE initialised')

        return resp[1]

    def disconnect(self):
        cmd = []
        cmd.append(COMMAND_ID['DAP_DISCONNECT'])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_DISCONNECT']:
            raise ValueError('DAP_DISCONNECT response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Disconnect failed')

        return resp[1]

    def writeAbort(self, data, dap_index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_WRITE_ABORT'])
        cmd.append(dap_index)
        cmd.append((data >> 0) & 0xff)
        cmd.append((data >> 8) & 0xff)
        cmd.append((data >> 16) & 0xff)
        cmd.append((data >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_WRITE_ABORT']:
            raise ValueError('DAP_WRITE_ABORT response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Write Abort failed')

        return True

    def resetTarget(interface):
        cmd = []
        cmd.append(COMMAND_ID['DAP_RESET_TARGET'])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_RESET_TARGET']:
            raise ValueError('DAP_RESET_TARGET response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Reset target failed')

        return resp[1]

    def transferConfigure(self, idle_cycles = 0x00, wait_retry = 0x0050, match_retry = 0x0000):
        cmd = []
        cmd.append(COMMAND_ID['DAP_TRANSFER_CONFIGURE'])
        cmd.append(idle_cycles)
        cmd.append(wait_retry & 0xff)
        cmd.append(wait_retry >> 8)
        cmd.append(match_retry & 0xff)
        cmd.append(match_retry >> 8)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_TRANSFER_CONFIGURE']:
            raise ValueError('DAP_TRANSFER_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Transfer Configure failed')

        return resp[1]

    def transfer(self, count, request, data = [0], dap_index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_TRANSFER'])
        cmd.append(dap_index)
        cmd.append(count)
        count_write = count
        for i in range(count):
            cmd.append(request[i])
            if not ( request[i] & ((1 << 1) | (1 << 4))):
                cmd.append(data[i] & 0xff)
                cmd.append((data[i] >> 8) & 0xff)
                cmd.append((data[i] >> 16) & 0xff)
                cmd.append((data[i] >> 24) & 0xff)
                count_write -= 1
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_TRANSFER']:
            raise ValueError('DAP_TRANSFER response error')

        if resp[2] != DAP_TRANSFER_OK:
            if resp[2] == DAP_TRANSFER_FAULT:
                raise TransferError()
            raise ValueError('SWD Fault')

        # Check for count mismatch after checking for DAP_TRANSFER_FAULT
        # This allows TransferError to get thrown instead of ValueError
        if resp[1] != count:
            raise ValueError('Transfer not completed')

        return resp[3:3+count_write*4]

    def transferBlock(self, count, request, data = [0], dap_index = 0):
        packet_count = count
        max_pending_reads = self.interface.getPacketCount()
        reads_pending = 0
        nb = 0
        resp = []
        error_transfer = False
        error_response = False

        # we send successfully several packets if the size is bigger than MAX_PACKET_COUNT
        while packet_count > 0 or reads_pending > 0:
            # Make sure the transmit buffer stays saturated
            while packet_count > 0 and reads_pending < max_pending_reads:
                cmd = []
                cmd.append(COMMAND_ID['DAP_TRANSFER_BLOCK'])
                cmd.append(dap_index)
                packet_written = min(packet_count, MAX_PACKET_SIZE)
                cmd.append(packet_written & 0xff)
                cmd.append((packet_written >> 8) & 0xff)
                cmd.append(request)
                if not (request & ((1 << 1))):
                    for i in range(packet_written):
                        cmd.append(data[i + nb*MAX_PACKET_SIZE] & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 8) & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 16) & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 24) & 0xff)
                self.interface.write(cmd)
                packet_count = packet_count - MAX_PACKET_SIZE
                nb = nb + 1
                reads_pending = reads_pending + 1

            # Read data
            if reads_pending > 0:
                # we then read
                tmp = self.interface.read()
                if tmp[0] != COMMAND_ID['DAP_TRANSFER_BLOCK']:
                    # Error occurred - abort further writes
                    # but make sure to finish reading remaining packets
                    packet_count = 0
                    error_response = True

                if tmp[3] != DAP_TRANSFER_OK:
                    # Error occurred - abort further writes
                    # but make sure to finish reading remaining packets
                    packet_count = 0
                    if tmp[3] == DAP_TRANSFER_FAULT:
                        error_transfer = True
                    else:
                        error_response = True

                size_transfer = tmp[1] | (tmp[2] << 8)
                resp.extend(tmp[4:4+size_transfer*4])
                reads_pending = reads_pending - 1

        # Raise pending errors
        if error_response:
            raise ValueError('DAP_TRANSFER_BLOCK response error')
        elif error_transfer:
            raise TransferError()

        return resp

    def setSWJClock(self, clock = 1000000):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_CLOCK'])
        cmd.append(clock & 0xff)
        cmd.append((clock >> 8) & 0xff)
        cmd.append((clock >> 16) & 0xff)
        cmd.append((clock >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_CLOCK']:
                raise ValueError('DAP_SWJ_CLOCK response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWJ Clock failed')

        return resp[1]

    def setSWJPins(self, output, pin, wait = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_PINS'])
        try:
            p = PINS[pin]
        except KeyError:
                logging.error('cannot find %s pin', pin)
                return
        cmd.append(output & 0xff)
        cmd.append(p)
        cmd.append(wait & 0xff)
        cmd.append((wait >> 8) & 0xff)
        cmd.append((wait >> 16) & 0xff)
        cmd.append((wait >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_PINS']:
                raise ValueError('DAP_SWJ_PINS response error')

        return resp[1]

    def swdConfigure(self, conf = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWD_CONFIGURE'])
        cmd.append(conf)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWD_CONFIGURE']:
                raise ValueError('DAP_SWD_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWD Configure failed')

        return resp[1]

    def swjSequence(self, data):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_SEQUENCE'])
        cmd.append(len(data)*8)
        for i in range(len(data)):
            cmd.append(data[i])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_SEQUENCE']:
                raise ValueError('DAP_SWJ_SEQUENCE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWJ Sequence failed')

        return resp[1]

    def jtagSequence(self, info, tdi):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_SEQUENCE'])
        cmd.append(1)
        cmd.append(info)
        cmd.append(tdi)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_SEQUENCE']:
            raise ValueError('DAP_JTAG_SEQUENCE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG Sequence failed')

        return resp[2]

    def jtagConfigure(self, irlen, dev_num = 1):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_CONFIGURE'])
        cmd.append(dev_num)
        cmd.append(irlen)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_CONFIGURE']:
            raise ValueError('DAP_JTAG_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG Configure failed')

        return resp[2:]

    def jtagIDCode(self, index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_IDCODE'])
        cmd.append(index)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_IDCODE']:
            raise ValueError('DAP_JTAG_IDCODE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG ID code failed')

        return  (resp[2] << 0)  | \
                (resp[3] << 8)  | \
                (resp[4] << 16) | \
                (resp[5] << 24)

    def vendor(self, index):
        cmd = []
        cmd.append(COMMAND_ID['DAP_VENDOR0'] + index)
        self.interface.write(cmd)

        resp = self.interface.read()

        if resp[0] != COMMAND_ID['DAP_VENDOR0'] + index:
            raise ValueError('DAP_VENDOR response error')
<commit_msg>Missed replacing interface arg with self for a couple methods in CMSIS_DAP_Protocol.<commit_after>"""
 mbed CMSIS-DAP debugger
 Copyright (c) 2006-2013 ARM Limited

 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 logging
import array
from transport import TransferError

COMMAND_ID = {'DAP_INFO': 0x00,
              'DAP_LED': 0x01,
              'DAP_CONNECT': 0x02,
              'DAP_DISCONNECT': 0x03,
              'DAP_TRANSFER_CONFIGURE': 0x04,
              'DAP_TRANSFER': 0x05,
              'DAP_TRANSFER_BLOCK': 0x06,
              'DAP_TRANSFER_ABORT': 0x07,
              'DAP_WRITE_ABORT': 0x08,
              'DAP_DELAY': 0x09,
              'DAP_RESET_TARGET': 0x0a,
              'DAP_SWJ_PINS': 0x10,
              'DAP_SWJ_CLOCK': 0x11,
              'DAP_SWJ_SEQUENCE': 0x12,
              'DAP_SWD_CONFIGURE': 0x13,
              'DAP_JTAG_SEQUENCE': 0x14,
              'DAP_JTAG_CONFIGURE': 0x15,
              'DAP_JTAG_IDCODE': 0x16,
              'DAP_VENDOR0': 0x80,
              }

ID_INFO = {'VENDOR_ID': 0x01,
           'PRODUCT_ID': 0x02,
           'SERIAL_NUMBER': 0x03,
           'CMSIS_DAP_FW_VERSION': 0x04,
           'TARGET_DEVICE_VENDOR': 0x05,
           'TARGET_DEVICE_NAME': 0x06,
           'CAPABILITIES': 0xf0,
           'PACKET_COUNT': 0xfe,
           'PACKET_SIZE': 0xff
           }

PINS = {'None': 0x00,
        'SWCLK_TCK': (1 << 0),
        'SWDIO_TMS': (1 << 1),
        'TDI': (1 << 2),
        'TDO': (1 << 3),
        'nTRST': (1 << 5),
        'nRESET': (1 << 7),
        }

DAP_DEFAULT_PORT = 0
DAP_SWD_PORT = 1
DAP_JTAG_POR = 2

DAP_OK = 0
DAP_ERROR = 0xff

# Responses to DAP_Transfer and DAP_TransferBlock
DAP_TRANSFER_OK = 1
DAP_TRANSFER_WAIT = 2
DAP_TRANSFER_FAULT = 4

MAX_PACKET_SIZE = 0x0E

## @brief This class implements the CMSIS-DAP wire protocol.
class CMSIS_DAP_Protocol(object):
    def __init__(self, interface):
        self.interface = interface

    def dapInfo(self, id_):
        cmd = []
        cmd.append(COMMAND_ID['DAP_INFO'])
        cmd.append(ID_INFO[id_])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_INFO']:
            raise ValueError('DAP_INFO response error')

        if resp[1] == 0:
            return

        # Integer values
        if id_ in ('CAPABILITIES', 'PACKET_COUNT', 'PACKET_SIZE'):
            if resp[1] == 1:
                return resp[2]
            if resp[1] == 2:
                return (resp[3] << 8) | resp[2]

        # String values
        x = array.array('B', [i for i in resp[2:2+resp[1]]])

        return x.tostring()

    def setLed(self):
        #not yet implemented
        return

    def connect(self, mode = DAP_DEFAULT_PORT):
        cmd = []
        cmd.append(COMMAND_ID['DAP_CONNECT'])
        cmd.append(mode)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_CONNECT']:
            raise ValueError('DAP_CONNECT response error')

        if resp[1] == 0:
            raise ValueError('DAP Connect failed')

        if resp[1] == 1:
            logging.info('DAP SWD MODE initialised')

        if resp[1] == 2:
            logging.info('DAP JTAG MODE initialised')

        return resp[1]

    def disconnect(self):
        cmd = []
        cmd.append(COMMAND_ID['DAP_DISCONNECT'])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_DISCONNECT']:
            raise ValueError('DAP_DISCONNECT response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Disconnect failed')

        return resp[1]

    def writeAbort(self, data, dap_index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_WRITE_ABORT'])
        cmd.append(dap_index)
        cmd.append((data >> 0) & 0xff)
        cmd.append((data >> 8) & 0xff)
        cmd.append((data >> 16) & 0xff)
        cmd.append((data >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_WRITE_ABORT']:
            raise ValueError('DAP_WRITE_ABORT response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Write Abort failed')

        return True

    def resetTarget(self):
        cmd = []
        cmd.append(COMMAND_ID['DAP_RESET_TARGET'])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_RESET_TARGET']:
            raise ValueError('DAP_RESET_TARGET response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Reset target failed')

        return resp[1]

    def transferConfigure(self, idle_cycles = 0x00, wait_retry = 0x0050, match_retry = 0x0000):
        cmd = []
        cmd.append(COMMAND_ID['DAP_TRANSFER_CONFIGURE'])
        cmd.append(idle_cycles)
        cmd.append(wait_retry & 0xff)
        cmd.append(wait_retry >> 8)
        cmd.append(match_retry & 0xff)
        cmd.append(match_retry >> 8)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_TRANSFER_CONFIGURE']:
            raise ValueError('DAP_TRANSFER_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP Transfer Configure failed')

        return resp[1]

    def transfer(self, count, request, data = [0], dap_index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_TRANSFER'])
        cmd.append(dap_index)
        cmd.append(count)
        count_write = count
        for i in range(count):
            cmd.append(request[i])
            if not ( request[i] & ((1 << 1) | (1 << 4))):
                cmd.append(data[i] & 0xff)
                cmd.append((data[i] >> 8) & 0xff)
                cmd.append((data[i] >> 16) & 0xff)
                cmd.append((data[i] >> 24) & 0xff)
                count_write -= 1
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_TRANSFER']:
            raise ValueError('DAP_TRANSFER response error')

        if resp[2] != DAP_TRANSFER_OK:
            if resp[2] == DAP_TRANSFER_FAULT:
                raise TransferError()
            raise ValueError('SWD Fault')

        # Check for count mismatch after checking for DAP_TRANSFER_FAULT
        # This allows TransferError to get thrown instead of ValueError
        if resp[1] != count:
            raise ValueError('Transfer not completed')

        return resp[3:3+count_write*4]

    def transferBlock(self, count, request, data = [0], dap_index = 0):
        packet_count = count
        max_pending_reads = self.interface.getPacketCount()
        reads_pending = 0
        nb = 0
        resp = []
        error_transfer = False
        error_response = False

        # we send successfully several packets if the size is bigger than MAX_PACKET_COUNT
        while packet_count > 0 or reads_pending > 0:
            # Make sure the transmit buffer stays saturated
            while packet_count > 0 and reads_pending < max_pending_reads:
                cmd = []
                cmd.append(COMMAND_ID['DAP_TRANSFER_BLOCK'])
                cmd.append(dap_index)
                packet_written = min(packet_count, MAX_PACKET_SIZE)
                cmd.append(packet_written & 0xff)
                cmd.append((packet_written >> 8) & 0xff)
                cmd.append(request)
                if not (request & ((1 << 1))):
                    for i in range(packet_written):
                        cmd.append(data[i + nb*MAX_PACKET_SIZE] & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 8) & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 16) & 0xff)
                        cmd.append((data[i + nb*MAX_PACKET_SIZE] >> 24) & 0xff)
                self.interface.write(cmd)
                packet_count = packet_count - MAX_PACKET_SIZE
                nb = nb + 1
                reads_pending = reads_pending + 1

            # Read data
            if reads_pending > 0:
                # we then read
                tmp = self.interface.read()
                if tmp[0] != COMMAND_ID['DAP_TRANSFER_BLOCK']:
                    # Error occurred - abort further writes
                    # but make sure to finish reading remaining packets
                    packet_count = 0
                    error_response = True

                if tmp[3] != DAP_TRANSFER_OK:
                    # Error occurred - abort further writes
                    # but make sure to finish reading remaining packets
                    packet_count = 0
                    if tmp[3] == DAP_TRANSFER_FAULT:
                        error_transfer = True
                    else:
                        error_response = True

                size_transfer = tmp[1] | (tmp[2] << 8)
                resp.extend(tmp[4:4+size_transfer*4])
                reads_pending = reads_pending - 1

        # Raise pending errors
        if error_response:
            raise ValueError('DAP_TRANSFER_BLOCK response error')
        elif error_transfer:
            raise TransferError()

        return resp

    def setSWJClock(self, clock = 1000000):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_CLOCK'])
        cmd.append(clock & 0xff)
        cmd.append((clock >> 8) & 0xff)
        cmd.append((clock >> 16) & 0xff)
        cmd.append((clock >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_CLOCK']:
                raise ValueError('DAP_SWJ_CLOCK response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWJ Clock failed')

        return resp[1]

    def setSWJPins(self, output, pin, wait = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_PINS'])
        try:
            p = PINS[pin]
        except KeyError:
                logging.error('cannot find %s pin', pin)
                return
        cmd.append(output & 0xff)
        cmd.append(p)
        cmd.append(wait & 0xff)
        cmd.append((wait >> 8) & 0xff)
        cmd.append((wait >> 16) & 0xff)
        cmd.append((wait >> 24) & 0xff)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_PINS']:
                raise ValueError('DAP_SWJ_PINS response error')

        return resp[1]

    def swdConfigure(self, conf = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWD_CONFIGURE'])
        cmd.append(conf)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWD_CONFIGURE']:
                raise ValueError('DAP_SWD_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWD Configure failed')

        return resp[1]

    def swjSequence(self, data):
        cmd = []
        cmd.append(COMMAND_ID['DAP_SWJ_SEQUENCE'])
        cmd.append(len(data)*8)
        for i in range(len(data)):
            cmd.append(data[i])
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_SWJ_SEQUENCE']:
                raise ValueError('DAP_SWJ_SEQUENCE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP SWJ Sequence failed')

        return resp[1]

    def jtagSequence(self, info, tdi):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_SEQUENCE'])
        cmd.append(1)
        cmd.append(info)
        cmd.append(tdi)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_SEQUENCE']:
            raise ValueError('DAP_JTAG_SEQUENCE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG Sequence failed')

        return resp[2]

    def jtagConfigure(self, irlen, dev_num = 1):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_CONFIGURE'])
        cmd.append(dev_num)
        cmd.append(irlen)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_CONFIGURE']:
            raise ValueError('DAP_JTAG_CONFIGURE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG Configure failed')

        return resp[2:]

    def jtagIDCode(self, index = 0):
        cmd = []
        cmd.append(COMMAND_ID['DAP_JTAG_IDCODE'])
        cmd.append(index)
        self.interface.write(cmd)

        resp = self.interface.read()
        if resp[0] != COMMAND_ID['DAP_JTAG_IDCODE']:
            raise ValueError('DAP_JTAG_IDCODE response error')

        if resp[1] != DAP_OK:
            raise ValueError('DAP JTAG ID code failed')

        return  (resp[2] << 0)  | \
                (resp[3] << 8)  | \
                (resp[4] << 16) | \
                (resp[5] << 24)

    def vendor(self, index):
        cmd = []
        cmd.append(COMMAND_ID['DAP_VENDOR0'] + index)
        self.interface.write(cmd)

        resp = self.interface.read()

        if resp[0] != COMMAND_ID['DAP_VENDOR0'] + index:
            raise ValueError('DAP_VENDOR response error')
<|endoftext|>


<commit_before>from django import forms
from tasks.models import Task

class NewTaskForm(forms.ModelForm):

	class Meta:
		model = Task
		fields = ('title', 'description', 'priority', 'due_to', 'share_with')
<commit_msg>changed field share_with to collaborators in add task form<commit_after>from django import forms
from tasks.models import Task

class NewTaskForm(forms.ModelForm):

	class Meta:
		model = Task
		fields = ('title', 'description', 'priority',
			'due_to', 'collaborators')
<|endoftext|>


<commit_before>"""Integration between SQLAlchemy and BigQuery."""

from __future__ import absolute_import
from __future__ import unicode_literals

from google.cloud.bigquery import dbapi
from google.cloud import bigquery
from google.api_core.exceptions import NotFound
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy import types, util
from sqlalchemy.sql.compiler import SQLCompiler, IdentifierPreparer
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.engine.base import Engine

class UniversalSet(object):
    """
    Set containing everything
    https://github.com/dropbox/PyHive/blob/master/pyhive/common.py
    """

    def __contains__(self, item):
        return True

class BigQueryIdentifierPreparer(IdentifierPreparer):
    """
    Set containing everything
    https://github.com/dropbox/PyHive/blob/master/pyhive/sqlalchemy_presto.py
    """
    reserved_words = UniversalSet()

    def __init__(self, dialect):
        super(BigQueryIdentifierPreparer, self).__init__(
            dialect,
            initial_quote="`",
        )

    def format_label(self, *args, **kwargs):
        # Replace dots with underscores in labels
        result = super(BigQueryIdentifierPreparer, self).format_label(*args, **kwargs)
        return result.replace('.', '_')

_type_map = {
    'STRING': types.String,
    'BOOLEAN': types.Boolean,
    'INTEGER': types.Integer,
    'FLOAT': types.Float,
    'TIMESTAMP': types.TIMESTAMP,
    'DATETIME': types.DATETIME,
    'DATE': types.DATE,
    'BYTES': types.BINARY,
    'TIME': types.TIME
    # TODO
    # 'RECORD'
}

class BigQueryExecutionContext(DefaultExecutionContext):
    def create_cursor(self):
        # Set arraysize
        c = super(BigQueryExecutionContext, self).create_cursor()
        if self.dialect.arraysize:
            c.arraysize = self.dialect.arraysize
        return c

class BigQueryCompiler(SQLCompiler):
    def visit_label(self, *args, **kwargs):
        # Use labels in GROUP BY clause
        if len(kwargs) == 0 or len(kwargs) == 1:
            kwargs['render_label_as_label'] = args[0]
        result = super(BigQueryCompiler, self).visit_label(*args, **kwargs)
        return result

class BigQueryDialect(DefaultDialect):
    name = 'bigquery'
    driver = 'bigquery'
    preparer = BigQueryIdentifierPreparer
    statement_compiler = BigQueryCompiler
    execution_ctx_cls = BigQueryExecutionContext
    supports_alter = False
    supports_pk_autoincrement = False
    supports_default_values = False
    supports_empty_insert = False
    supports_unicode_statements = True
    supports_unicode_binds = True
    returns_unicode_strings = True
    description_encoding = None
    supports_native_boolean = True
    supports_simple_order_by_label = True
    postfetch_lastrowid = False

    def __init__(self, arraysize=5000, *args, **kwargs):
        super(BigQueryDialect, self).__init__(*args, **kwargs)
        self.arraysize = arraysize

    @classmethod
    def dbapi(cls):
        return dbapi

    def create_connect_args(self, url):
        client = bigquery.Client(url.host) if url.host else None
        return ([client], {})

    def _split_table_name(self, full_table_name):
        # Split full_table_name to get project, dataset and table name
        dataset = None
        table_name = None
        project = None

        table_name_split = full_table_name.split('.')
        if len(table_name_split) == 2:
            dataset, table_name = table_name_split
        elif len(table_name_split) == 3:
            project, dataset, table_name = table_name_split

        return (project, dataset, table_name)

    def _get_table(self, connection, table_name, schema=None):
        if isinstance(connection, Engine):
            connection = connection.connect()

        project, dataset, table_name_prepared = self._split_table_name(table_name)
        if dataset is None and schema is not None:
            dataset = schema
            table_name_prepared = table_name

        table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
        try:
            t = connection.connection._client.get_table(table)
        except NotFound as e:
            raise NoSuchTableError(table_name)
        return t

    def has_table(self, connection, table_name, schema=None):
        try:
            self._get_table(connection, table_name, schema)
            return True
        except NoSuchTableError:
            return False

    def get_columns(self, connection, table_name, schema=None, **kw):
        table = self._get_table(connection, table_name, schema)
        columns = table.schema
        result = []
        for col in columns:
            try:
                coltype = _type_map[col.field_type]
            except KeyError:
                util.warn("Did not recognize type '%s' of column '%s'" % (col.field_type, col.name))

            result.append({
                'name': col.name,
                'type': coltype,
                'nullable': True if col.mode == 'NULLABLE' else False,
                'default': None,
            })

        return result

    def get_foreign_keys(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for foreign keys.
        return []

    def get_pk_constraint(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for primary keys.
        return []

    def get_indexes(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for indexes.
        return []

    def get_schema_names(self, connection, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        return [d.dataset_id for d in datasets]

    def get_table_names(self, connection, schema=None, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        result = []
        for d in datasets:
            if schema is not None and d.dataset_id != schema:
                continue
            tables = connection.connection._client.list_dataset_tables(d.reference)
            for t in tables:
                result.append(d.dataset_id + '.' + t.table_id)
        return result

    def do_rollback(self, dbapi_connection):
        # BigQuery has no support for transactions.
        pass

    def _check_unicode_returns(self, connection, additional_tests=None):
        # requests gives back Unicode strings
        return True

    def _check_unicode_description(self, connection):
        # requests gives back Unicode strings
        return True
<commit_msg>change method name; Client.list_dataset_tables(dataset) has been deprecated in favor of Client.list_tables(dataset)<commit_after>"""Integration between SQLAlchemy and BigQuery."""

from __future__ import absolute_import
from __future__ import unicode_literals

from google.cloud.bigquery import dbapi
from google.cloud import bigquery
from google.api_core.exceptions import NotFound
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy import types, util
from sqlalchemy.sql.compiler import SQLCompiler, IdentifierPreparer
from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext
from sqlalchemy.engine.base import Engine

class UniversalSet(object):
    """
    Set containing everything
    https://github.com/dropbox/PyHive/blob/master/pyhive/common.py
    """

    def __contains__(self, item):
        return True

class BigQueryIdentifierPreparer(IdentifierPreparer):
    """
    Set containing everything
    https://github.com/dropbox/PyHive/blob/master/pyhive/sqlalchemy_presto.py
    """
    reserved_words = UniversalSet()

    def __init__(self, dialect):
        super(BigQueryIdentifierPreparer, self).__init__(
            dialect,
            initial_quote="`",
        )

    def format_label(self, *args, **kwargs):
        # Replace dots with underscores in labels
        result = super(BigQueryIdentifierPreparer, self).format_label(*args, **kwargs)
        return result.replace('.', '_')

_type_map = {
    'STRING': types.String,
    'BOOLEAN': types.Boolean,
    'INTEGER': types.Integer,
    'FLOAT': types.Float,
    'TIMESTAMP': types.TIMESTAMP,
    'DATETIME': types.DATETIME,
    'DATE': types.DATE,
    'BYTES': types.BINARY,
    'TIME': types.TIME
    # TODO
    # 'RECORD'
}

class BigQueryExecutionContext(DefaultExecutionContext):
    def create_cursor(self):
        # Set arraysize
        c = super(BigQueryExecutionContext, self).create_cursor()
        if self.dialect.arraysize:
            c.arraysize = self.dialect.arraysize
        return c

class BigQueryCompiler(SQLCompiler):
    def visit_label(self, *args, **kwargs):
        # Use labels in GROUP BY clause
        if len(kwargs) == 0 or len(kwargs) == 1:
            kwargs['render_label_as_label'] = args[0]
        result = super(BigQueryCompiler, self).visit_label(*args, **kwargs)
        return result

class BigQueryDialect(DefaultDialect):
    name = 'bigquery'
    driver = 'bigquery'
    preparer = BigQueryIdentifierPreparer
    statement_compiler = BigQueryCompiler
    execution_ctx_cls = BigQueryExecutionContext
    supports_alter = False
    supports_pk_autoincrement = False
    supports_default_values = False
    supports_empty_insert = False
    supports_unicode_statements = True
    supports_unicode_binds = True
    returns_unicode_strings = True
    description_encoding = None
    supports_native_boolean = True
    supports_simple_order_by_label = True
    postfetch_lastrowid = False

    def __init__(self, arraysize=5000, *args, **kwargs):
        super(BigQueryDialect, self).__init__(*args, **kwargs)
        self.arraysize = arraysize

    @classmethod
    def dbapi(cls):
        return dbapi

    def create_connect_args(self, url):
        client = bigquery.Client(url.host) if url.host else None
        return ([client], {})

    def _split_table_name(self, full_table_name):
        # Split full_table_name to get project, dataset and table name
        dataset = None
        table_name = None
        project = None

        table_name_split = full_table_name.split('.')
        if len(table_name_split) == 2:
            dataset, table_name = table_name_split
        elif len(table_name_split) == 3:
            project, dataset, table_name = table_name_split

        return (project, dataset, table_name)

    def _get_table(self, connection, table_name, schema=None):
        if isinstance(connection, Engine):
            connection = connection.connect()

        project, dataset, table_name_prepared = self._split_table_name(table_name)
        if dataset is None and schema is not None:
            dataset = schema
            table_name_prepared = table_name

        table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
        try:
            t = connection.connection._client.get_table(table)
        except NotFound as e:
            raise NoSuchTableError(table_name)
        return t

    def has_table(self, connection, table_name, schema=None):
        try:
            self._get_table(connection, table_name, schema)
            return True
        except NoSuchTableError:
            return False

    def get_columns(self, connection, table_name, schema=None, **kw):
        table = self._get_table(connection, table_name, schema)
        columns = table.schema
        result = []
        for col in columns:
            try:
                coltype = _type_map[col.field_type]
            except KeyError:
                util.warn("Did not recognize type '%s' of column '%s'" % (col.field_type, col.name))

            result.append({
                'name': col.name,
                'type': coltype,
                'nullable': True if col.mode == 'NULLABLE' else False,
                'default': None,
            })

        return result

    def get_foreign_keys(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for foreign keys.
        return []

    def get_pk_constraint(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for primary keys.
        return []

    def get_indexes(self, connection, table_name, schema=None, **kw):
        # BigQuery has no support for indexes.
        return []

    def get_schema_names(self, connection, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        return [d.dataset_id for d in datasets]

    def get_table_names(self, connection, schema=None, **kw):
        if isinstance(connection, Engine):
            connection = connection.connect()

        datasets = connection.connection._client.list_datasets()
        result = []
        for d in datasets:
            if schema is not None and d.dataset_id != schema:
                continue
            tables = connection.connection._client.list_tables(d.reference)
            for t in tables:
                result.append(d.dataset_id + '.' + t.table_id)
        return result

    def do_rollback(self, dbapi_connection):
        # BigQuery has no support for transactions.
        pass

    def _check_unicode_returns(self, connection, additional_tests=None):
        # requests gives back Unicode strings
        return True

    def _check_unicode_description(self, connection):
        # requests gives back Unicode strings
        return True
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import math
from SimpleCV import Image, Color, Display, Camera

imgVelleda = Image('velleda.jpg')
cam = Camera()
imgVelleda = imgVelleda.resize(100, 100)

disp = Display((640, 480))
wb = imgVelleda.hueDistance(color=Color.BLUE).invert().threshold(210)
while disp.isNotDone:
    for i_time in range(0, imgVelleda.width) :
        imgVelleda = cam.getImage().resize(100, 100)
        wb = imgVelleda.hueDistance(color=Color.BLUE).invert().threshold(210)
        # Move a vertical scan line and take blue pixel index
        blue_pix = [i for i in range(len(wb.getVertScanline(i_time))) if wb.getVertScanline(i_time)[i].any()]
        # Draw the scan line
        imgVelleda_copy = imgVelleda.copy()
        imgVelleda_copy.drawLine((i_time,0), (i_time, imgVelleda.height), color= Color.RED)
        imgVelleda_copy.drawCircle( ((0.5+math.floor(i_time*8/imgVelleda.width))*imgVelleda.width/8.0, 10), 5, color=Color.RED, thickness = -1)
        # Draw circles at blue point
        for pix in blue_pix:
            imgVelleda_copy.drawCircle((i_time, pix), 5)

        imgVelleda_copy.save(disp)
<commit_msg>Comment needless reading of image<commit_after># -*- coding: utf-8 -*-
import math
from SimpleCV import Image, Color, Display, Camera

cam = Camera()
#imgVelleda = Image('velleda.jpg')
imgVelleda = cam.getImage()
imgVelleda = imgVelleda.resize(100, 100)

disp = Display((640, 480))
wb = imgVelleda.hueDistance(color=Color.BLUE).invert().threshold(210)
while disp.isNotDone:
    for i_time in range(0, imgVelleda.width) :
        imgVelleda = cam.getImage().resize(100, 100)
        wb = imgVelleda.hueDistance(color=Color.BLUE).invert().threshold(210)
        # Move a vertical scan line and take blue pixel index
        blue_pix = [i for i in range(len(wb.getVertScanline(i_time))) if wb.getVertScanline(i_time)[i].any()]
        # Draw the scan line
        imgVelleda_copy = imgVelleda.copy()
        imgVelleda_copy.drawLine((i_time,0), (i_time, imgVelleda.height), color= Color.RED)
        imgVelleda_copy.drawCircle( ((0.5+math.floor(i_time*8/imgVelleda.width))*imgVelleda.width/8.0, 10), 5, color=Color.RED, thickness = -1)
        # Draw circles at blue point
        for pix in blue_pix:
            imgVelleda_copy.drawCircle((i_time, pix), 5)

        imgVelleda_copy.save(disp)
<|endoftext|>


<commit_before>
#
# spyne - Copyright (C) Spyne contributors.
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

import decimal
import datetime
import math
import time
import pytz
import uuid

from collections import deque

from pytz import FixedOffset

from spyne.error import ValidationError
from spyne.model import nillable_string
from spyne.model import nillable_iterable
from spyne.model.binary import File
from spyne.model.binary import Attachment
from spyne.model.binary import binary_encoding_handlers
from spyne.model.binary import binary_decoding_handlers
from spyne.model.primitive import _time_re
from spyne.model.primitive import _duration_re

try:
    from lxml import etree
    from lxml import html
except ImportError:
    etree = None
    html = None

__all__ = [
    'uuid_to_string', 'uuid_from_string',
    'null_to_string', 'null_from_string',
    'any_xml_to_string', 'any_xml_from_string',
    'any_html_to_string', 'any_html_from_string',
    'unicode_to_string', 'unicode_from_string',
    'string_from_string',
    'decimal_to_string', 'decimal_from_string',
    'double_to_string', 'double_from_string',
    'integer_to_string', 'integer_from_string',
    'time_to_string', 'time_from_string',
    'datetime_to_string', 'datetime_from_string', 'datetime_from_string_iso',
    'date_from_string', 'date_from_string_iso',
    'duration_to_string', 'duration_from_string',
    'boolean_to_string', 'boolean_from_string',
    'byte_array_to_string', 'byte_array_from_string', 'byte_array_to_string_iterable',
    'file_from_string', 'file_to_string_iterable',
    'attachment_to_string', 'attachment_from_string',
    'complex_model_base_to_string', 'complex_model_base_from_string',
    'simple_model_to_string_iterable',
]

def null_to_string(cls, value):
    return ""

def null_from_string(cls, value):
    return None

@nillable_string
def any_xml_to_string(cls, value):
    return etree.tostring(value)

@nillable_string
def any_xml_from_string(cls, string):
    try:
        return etree.fromstring(string)
    except etree.XMLSyntaxError, e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def any_html_to_string(cls, value):
    return html.tostring(value)

@nillable_string
def any_html_from_string(cls, string):
    return html.fromstring(string)

@nillable_string
def uuid_to_string(cls, value):
    return str(value)

@nillable_string
def uuid_from_string(cls, string):
    return uuid.UUID(string)

@nillable_string
def unicode_to_string(cls, value):
    retval = value
    if cls.Attributes.encoding is not None and isinstance(value, unicode):
        retval = value.encode(cls.Attributes.encoding)
    if cls.Attributes.format is None:
        return retval
    else:
        return cls.Attributes.format % retval

@nillable_string
def unicode_from_string(cls, value):
    retval = value
    if isinstance(value, str):
        if cls.Attributes.encoding is None:
            retval = unicode(value, errors = cls.Attributes.unicode_errors)
        else:
            retval = unicode(value, cls.Attributes.encoding,
                                    errors = cls.Attributes.unicode_errors)
    return retval

@nillable_string
def string_from_string(cls, value):
    retval = value
    if isinstance(value, unicode):
        if cls.Attributes.encoding is None:
            raise Exception("You need to define an encoding to convert the "
                            "incoming unicode values to.")
        else:
            retval = value.encode(cls.Attributes.encoding)

    return retval

@nillable_string
def decimal_to_string(cls, value):
    decimal.Decimal(value)
    if cls.Attributes.str_format is not None:
        return cls.Attributes.str_format.format(value)
    elif cls.Attributes.format is not None:
        return cls.Attributes.format % value
    else:
        return str(value)

@nillable_string
def decimal_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Decimal %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return decimal.Decimal(string)
    except decimal.InvalidOperation, e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def double_to_string(cls, value):
    float(value) # sanity check

    if cls.Attributes.format is None:
        return repr(value)
    else:
        return cls.Attributes.format % value

@nillable_string
def double_from_string(cls, string):
    try:
        return float(string)
    except (TypeError, ValueError), e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def integer_to_string(cls, value):
    int(value) # sanity check

    if cls.Attributes.format is None:
        return str(value)
    else:
        return cls.Attributes.format % value

@nillable_string
def integer_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Integer %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return int(string)
    except ValueError:
        raise ValidationError(string, "Could not cast %r to integer")

@nillable_string
def time_to_string(cls, value):
    """Returns ISO formatted dates."""

    return value.isoformat()

@nillable_string
def time_from_string(cls, string):
    """Expects ISO formatted times."""

    match = _time_re.match(string)
    if match is None:
        raise ValidationError(string, "%%r does not match regex %r " %
                                                               _time_re.pattern)

    fields = match.groupdict(0)
    microsec = fields.get('sec_frac')
    if microsec is None or microsec == 0:
        microsec = 0
    else:
        microsec = int(microsec[1:])

    return datetime.time(int(fields['hr']), int(fields['min']),
                                                   int(fields['sec']), microsec)

@nillable_string
def datetime_to_string(cls, value):
    if cls.Attributes.as_time_zone is not None and value.tzinfo is not None:
        value = value.astimezone(cls.Attributes.as_time_zone) \
                                                    .replace(tzinfo=None)

    format = cls.Attributes.format
    if format is None:
        ret_str = value.isoformat()
    else:
        ret_str = datetime.datetime.strftime(value, format)

    string_format = cls.Attributes.string_format
    if string_format is None:
        return ret_str
    else:
        return string_format % ret_str

def datetime_from_string_iso(cls, string):
    match = cls._utc_re.match(string)
    if match:
        return cls.parse(match, tz=pytz.utc)

    match = cls._offset_re.match(string)
    if match:
        tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
        return cls.parse(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))

    match = cls._local_re.match(string)
    if match is None:
        raise ValidationError(string)

    return cls.parse(match)

def date_from_string_iso(cls, string):
    """This is used by protocols like SOAP who need ISO8601-formatted dates
    no matter what.
    """
    try:
        return datetime.date(*(time.strptime(string, '%Y-%m-%d')[0:3]))
    except ValueError:
        raise ValidationError(string)

@nillable_string
def datetime_from_string(cls, string):
    format = cls.Attributes.format

    if format is None:
        retval = datetime_from_string_iso(cls, string)
    else:
        retval = datetime.datetime.strptime(string, format)

    if cls.Attributes.as_time_zone is not None and retval.tzinfo is not None:
        retval = retval.astimezone(cls.Attributes.as_time_zone) \
                                                    .replace(tzinfo=None)

    return retval

@nillable_string
def date_from_string(cls, string):
    try:
        d = datetime.datetime.strptime(string, cls.Attributes.format)
        return datetime.date(d.year, d.month, d.day)
    except ValueError, e:
        raise ValidationError(string, "%%r: %r" % e)

def duration_to_string(cls, value):
    if value.days < 0:
        value = -value
        negative = True
    else:
        negative = False

    total_seconds = value.total_seconds()
    seconds = value.seconds % 60
    minutes = value.seconds / 60
    hours = minutes / 60
    minutes = minutes % 60
    seconds = float(seconds)
    useconds = value.microseconds

    retval = deque()
    if negative:
        retval.append("-P")
    else:
        retval.append("P")
    if value.days != 0:
        retval.extend([
            "%iD" % value.days,
            ])

    if total_seconds != 0 and total_seconds % 86400 == 0 and useconds == 0:
        return ''.join(retval)

    retval.append('T')

    if hours > 0:
        retval.append("%iH" % hours)

    if minutes > 0:
        retval.append("%iM" % minutes)

    if seconds > 0 or useconds > 0:
        retval.append("%i" % seconds)
        if useconds > 0:
            retval.append(".%i" % useconds)
        retval.append("S")

    if len(retval) == 2:
        retval.append('0S')

    return ''.join(retval)

@nillable_string
def duration_from_string(cls, string):
    duration = _duration_re.match(string).groupdict(0)
    if duration is None:
        raise ValidationError("time data '%s' does not match regex '%s'" %(string, _duration_re.pattern))

    days = int(duration['days'])
    days += int(duration['months']) * 30
    days += int(duration['years']) * 365
    hours = int(duration['hours'])
    minutes = int(duration['minutes'])
    seconds = float(duration['seconds'])
    f, i = math.modf(seconds)
    seconds = i
    microseconds = int(1e6 * f)

    delta = datetime.timedelta(days=days, hours=hours, minutes=minutes,
        seconds=seconds, microseconds=microseconds)

    if duration['sign'] == "-":
        delta *= -1

    return delta

@nillable_string
def boolean_to_string(cls, value):
    return str(bool(value)).lower()

@nillable_string
def boolean_from_string(cls, string):
    return (string.lower() in ['true', '1'])

@nillable_string
def byte_array_from_string(cls, value, suggested_encoding=None):
    encoding = cls.Attributes.encoding
    if encoding is None:
        encoding = suggested_encoding
    return binary_decoding_handlers[encoding](value)

@nillable_string
def byte_array_to_string(cls, value, suggested_encoding=None):
    if suggested_encoding is None:
        suggested_encoding = cls.Attributes.encoding
    return binary_encoding_handlers[suggested_encoding](value)

@nillable_iterable
def byte_array_to_string_iterable(prot, cls, value):
    return value

@nillable_string
def file_from_string(cls, value, suggested_encoding=None):
    encoding = cls.Attributes.encoding
    if encoding is None:
        encoding = suggested_encoding
    return File.Value(data=binary_decoding_handlers[encoding](value))

def _file_to_iter(f):
    data = f.read(65536)
    while len(data) > 0:
        yield data
        data = f.read(65536)

    f.close()

@nillable_iterable
def file_to_string_iterable(prot, cls, value):
    if value.data is None:
        if value.handle is None:
            assert value.path is not None, "You need to write data to " \
                        "persistent storage first if you want to read it back."

            f = open(value.path, 'rb')

        else:
            f = value.handle
            f.seek(0)

        return _file_to_iter(f)

    else:
        return iter(value.data)

@nillable_iterable
def simple_model_to_string_iterable(prot, cls, value):
    retval = prot.to_string(cls, value)
    if retval is None:
        return ('',)
    return (retval,)

@nillable_string
def attachment_to_string(cls, value):
    if not (value.data is None):
        # the data has already been loaded, just encode
        # and return the element
        data = value.data

    elif not (value.file_name is None):
        # the data hasn't been loaded, but a file has been
        # specified
        data = open(value.file_name, 'rb').read()

    else:
        raise ValueError("Neither data nor a file_name has been specified")

    return data

@nillable_string
def attachment_from_string(cls, value):
    return Attachment(data=value)

@nillable_string
def complex_model_base_to_string(cls, value):
    raise TypeError("Only primitives can be serialized to string.")

@nillable_string
def complex_model_base_from_string(cls, string):
    raise TypeError("Only primitives can be deserialized from string.")
<commit_msg>fix for python 2.6 timedelta missing total_seconds<commit_after>
#
# spyne - Copyright (C) Spyne contributors.
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

import decimal
import datetime
import math
import time
import pytz
import uuid

from collections import deque

from pytz import FixedOffset

from spyne.error import ValidationError
from spyne.model import nillable_string
from spyne.model import nillable_iterable
from spyne.model.binary import File
from spyne.model.binary import Attachment
from spyne.model.binary import binary_encoding_handlers
from spyne.model.binary import binary_decoding_handlers
from spyne.model.primitive import _time_re
from spyne.model.primitive import _duration_re

try:
    from lxml import etree
    from lxml import html
except ImportError:
    etree = None
    html = None

__all__ = [
    'uuid_to_string', 'uuid_from_string',
    'null_to_string', 'null_from_string',
    'any_xml_to_string', 'any_xml_from_string',
    'any_html_to_string', 'any_html_from_string',
    'unicode_to_string', 'unicode_from_string',
    'string_from_string',
    'decimal_to_string', 'decimal_from_string',
    'double_to_string', 'double_from_string',
    'integer_to_string', 'integer_from_string',
    'time_to_string', 'time_from_string',
    'datetime_to_string', 'datetime_from_string', 'datetime_from_string_iso',
    'date_from_string', 'date_from_string_iso',
    'duration_to_string', 'duration_from_string',
    'boolean_to_string', 'boolean_from_string',
    'byte_array_to_string', 'byte_array_from_string', 'byte_array_to_string_iterable',
    'file_from_string', 'file_to_string_iterable',
    'attachment_to_string', 'attachment_from_string',
    'complex_model_base_to_string', 'complex_model_base_from_string',
    'simple_model_to_string_iterable',
]

def null_to_string(cls, value):
    return ""

def null_from_string(cls, value):
    return None

@nillable_string
def any_xml_to_string(cls, value):
    return etree.tostring(value)

@nillable_string
def any_xml_from_string(cls, string):
    try:
        return etree.fromstring(string)
    except etree.XMLSyntaxError, e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def any_html_to_string(cls, value):
    return html.tostring(value)

@nillable_string
def any_html_from_string(cls, string):
    return html.fromstring(string)

@nillable_string
def uuid_to_string(cls, value):
    return str(value)

@nillable_string
def uuid_from_string(cls, string):
    return uuid.UUID(string)

@nillable_string
def unicode_to_string(cls, value):
    retval = value
    if cls.Attributes.encoding is not None and isinstance(value, unicode):
        retval = value.encode(cls.Attributes.encoding)
    if cls.Attributes.format is None:
        return retval
    else:
        return cls.Attributes.format % retval

@nillable_string
def unicode_from_string(cls, value):
    retval = value
    if isinstance(value, str):
        if cls.Attributes.encoding is None:
            retval = unicode(value, errors = cls.Attributes.unicode_errors)
        else:
            retval = unicode(value, cls.Attributes.encoding,
                                    errors = cls.Attributes.unicode_errors)
    return retval

@nillable_string
def string_from_string(cls, value):
    retval = value
    if isinstance(value, unicode):
        if cls.Attributes.encoding is None:
            raise Exception("You need to define an encoding to convert the "
                            "incoming unicode values to.")
        else:
            retval = value.encode(cls.Attributes.encoding)

    return retval

@nillable_string
def decimal_to_string(cls, value):
    decimal.Decimal(value)
    if cls.Attributes.str_format is not None:
        return cls.Attributes.str_format.format(value)
    elif cls.Attributes.format is not None:
        return cls.Attributes.format % value
    else:
        return str(value)

@nillable_string
def decimal_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Decimal %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return decimal.Decimal(string)
    except decimal.InvalidOperation, e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def double_to_string(cls, value):
    float(value) # sanity check

    if cls.Attributes.format is None:
        return repr(value)
    else:
        return cls.Attributes.format % value

@nillable_string
def double_from_string(cls, string):
    try:
        return float(string)
    except (TypeError, ValueError), e:
        raise ValidationError(string, "%%r: %r" % e)

@nillable_string
def integer_to_string(cls, value):
    int(value) # sanity check

    if cls.Attributes.format is None:
        return str(value)
    else:
        return cls.Attributes.format % value

@nillable_string
def integer_from_string(cls, string):
    if cls.Attributes.max_str_len is not None and len(string) > \
                                                     cls.Attributes.max_str_len:
        raise ValidationError(string, "Integer %%r longer than %d characters"
                                                   % cls.Attributes.max_str_len)

    try:
        return int(string)
    except ValueError:
        raise ValidationError(string, "Could not cast %r to integer")

@nillable_string
def time_to_string(cls, value):
    """Returns ISO formatted dates."""

    return value.isoformat()

@nillable_string
def time_from_string(cls, string):
    """Expects ISO formatted times."""

    match = _time_re.match(string)
    if match is None:
        raise ValidationError(string, "%%r does not match regex %r " %
                                                               _time_re.pattern)

    fields = match.groupdict(0)
    microsec = fields.get('sec_frac')
    if microsec is None or microsec == 0:
        microsec = 0
    else:
        microsec = int(microsec[1:])

    return datetime.time(int(fields['hr']), int(fields['min']),
                                                   int(fields['sec']), microsec)

@nillable_string
def datetime_to_string(cls, value):
    if cls.Attributes.as_time_zone is not None and value.tzinfo is not None:
        value = value.astimezone(cls.Attributes.as_time_zone) \
                                                    .replace(tzinfo=None)

    format = cls.Attributes.format
    if format is None:
        ret_str = value.isoformat()
    else:
        ret_str = datetime.datetime.strftime(value, format)

    string_format = cls.Attributes.string_format
    if string_format is None:
        return ret_str
    else:
        return string_format % ret_str

def datetime_from_string_iso(cls, string):
    match = cls._utc_re.match(string)
    if match:
        return cls.parse(match, tz=pytz.utc)

    match = cls._offset_re.match(string)
    if match:
        tz_hr, tz_min = [int(match.group(x)) for x in ("tz_hr", "tz_min")]
        return cls.parse(match, tz=FixedOffset(tz_hr * 60 + tz_min, {}))

    match = cls._local_re.match(string)
    if match is None:
        raise ValidationError(string)

    return cls.parse(match)

def date_from_string_iso(cls, string):
    """This is used by protocols like SOAP who need ISO8601-formatted dates
    no matter what.
    """
    try:
        return datetime.date(*(time.strptime(string, '%Y-%m-%d')[0:3]))
    except ValueError:
        raise ValidationError(string)

@nillable_string
def datetime_from_string(cls, string):
    format = cls.Attributes.format

    if format is None:
        retval = datetime_from_string_iso(cls, string)
    else:
        retval = datetime.datetime.strptime(string, format)

    if cls.Attributes.as_time_zone is not None and retval.tzinfo is not None:
        retval = retval.astimezone(cls.Attributes.as_time_zone) \
                                                    .replace(tzinfo=None)

    return retval

@nillable_string
def date_from_string(cls, string):
    try:
        d = datetime.datetime.strptime(string, cls.Attributes.format)
        return datetime.date(d.year, d.month, d.day)
    except ValueError, e:
        raise ValidationError(string, "%%r: %r" % e)

if hasattr(datetime.timedelta, 'total_seconds'):
    def total_seconds(td):
        return td.total_seconds()

else:
    def total_seconds(td):
        return (td.microseconds +
                            (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6

def duration_to_string(cls, value):
    if value.days < 0:
        value = -value
        negative = True
    else:
        negative = False

    tot_sec = total_seconds(value)
    seconds = value.seconds % 60
    minutes = value.seconds / 60
    hours = minutes / 60
    minutes = minutes % 60
    seconds = float(seconds)
    useconds = value.microseconds

    retval = deque()
    if negative:
        retval.append("-P")
    else:
        retval.append("P")
    if value.days != 0:
        retval.extend([
            "%iD" % value.days,
            ])

    if tot_sec != 0 and tot_sec % 86400 == 0 and useconds == 0:
        return ''.join(retval)

    retval.append('T')

    if hours > 0:
        retval.append("%iH" % hours)

    if minutes > 0:
        retval.append("%iM" % minutes)

    if seconds > 0 or useconds > 0:
        retval.append("%i" % seconds)
        if useconds > 0:
            retval.append(".%i" % useconds)
        retval.append("S")

    if len(retval) == 2:
        retval.append('0S')

    return ''.join(retval)

@nillable_string
def duration_from_string(cls, string):
    duration = _duration_re.match(string).groupdict(0)
    if duration is None:
        raise ValidationError("time data '%s' does not match regex '%s'" %(string, _duration_re.pattern))

    days = int(duration['days'])
    days += int(duration['months']) * 30
    days += int(duration['years']) * 365
    hours = int(duration['hours'])
    minutes = int(duration['minutes'])
    seconds = float(duration['seconds'])
    f, i = math.modf(seconds)
    seconds = i
    microseconds = int(1e6 * f)

    delta = datetime.timedelta(days=days, hours=hours, minutes=minutes,
        seconds=seconds, microseconds=microseconds)

    if duration['sign'] == "-":
        delta *= -1

    return delta

@nillable_string
def boolean_to_string(cls, value):
    return str(bool(value)).lower()

@nillable_string
def boolean_from_string(cls, string):
    return (string.lower() in ['true', '1'])

@nillable_string
def byte_array_from_string(cls, value, suggested_encoding=None):
    encoding = cls.Attributes.encoding
    if encoding is None:
        encoding = suggested_encoding
    return binary_decoding_handlers[encoding](value)

@nillable_string
def byte_array_to_string(cls, value, suggested_encoding=None):
    if suggested_encoding is None:
        suggested_encoding = cls.Attributes.encoding
    return binary_encoding_handlers[suggested_encoding](value)

@nillable_iterable
def byte_array_to_string_iterable(prot, cls, value):
    return value

@nillable_string
def file_from_string(cls, value, suggested_encoding=None):
    encoding = cls.Attributes.encoding
    if encoding is None:
        encoding = suggested_encoding
    return File.Value(data=binary_decoding_handlers[encoding](value))

def _file_to_iter(f):
    data = f.read(65536)
    while len(data) > 0:
        yield data
        data = f.read(65536)

    f.close()

@nillable_iterable
def file_to_string_iterable(prot, cls, value):
    if value.data is None:
        if value.handle is None:
            assert value.path is not None, "You need to write data to " \
                        "persistent storage first if you want to read it back."

            f = open(value.path, 'rb')

        else:
            f = value.handle
            f.seek(0)

        return _file_to_iter(f)

    else:
        return iter(value.data)

@nillable_iterable
def simple_model_to_string_iterable(prot, cls, value):
    retval = prot.to_string(cls, value)
    if retval is None:
        return ('',)
    return (retval,)

@nillable_string
def attachment_to_string(cls, value):
    if not (value.data is None):
        # the data has already been loaded, just encode
        # and return the element
        data = value.data

    elif not (value.file_name is None):
        # the data hasn't been loaded, but a file has been
        # specified
        data = open(value.file_name, 'rb').read()

    else:
        raise ValueError("Neither data nor a file_name has been specified")

    return data

@nillable_string
def attachment_from_string(cls, value):
    return Attachment(data=value)

@nillable_string
def complex_model_base_to_string(cls, value):
    raise TypeError("Only primitives can be serialized to string.")

@nillable_string
def complex_model_base_from_string(cls, string):
    raise TypeError("Only primitives can be deserialized from string.")
<|endoftext|>