<commit_before>import os
import sys
import tempfile
import shutil
import zipfile
import urllib
import pkg_resources
from cStringIO import StringIO
from lxml import etree, html

try:
    import json
except ImportError:
    import simplejson as json

import symbols

dirname = os.path.dirname(__file__)

NAMESPACES = {
  'office':'urn:oasis:names:tc:opendocument:xmlns:office:1.0',
  'draw':'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0',
  'xlink':'http://www.w3.org/1999/xlink',
  }

MATH_XPATH = etree.XPath('//draw:object[@xlink:href]', namespaces=NAMESPACES)
MATH_HREF_XPATH = etree.XPath('@xlink:href', namespaces=NAMESPACES)

IMAGE_XPATH = etree.XPath('//draw:frame[not(draw:object or draw:object-ole) and @draw:name and draw:image[@xlink:href and @xlink:type="simple"]]', namespaces=NAMESPACES)
IMAGE_HREF_XPATH = etree.XPath('draw:image/@xlink:href', namespaces=NAMESPACES)
IMAGE_NAME_XPATH = etree.XPath('@draw:name', namespaces=NAMESPACES)
STYLES_XPATH = etree.XPath('//office:styles', namespaces=NAMESPACES)
DRAW_XPATH = etree.XPath('//draw:g[not(parent::draw:*)]', namespaces=NAMESPACES)
DRAW_STYLES_XPATH = etree.XPath('/office:document-content/office:automatic-styles/*', namespaces=NAMESPACES)

DRAW_PREFIX = "draw_odg"

def makeXsl(filename):
  """ Helper that creates a XSLT stylesheet """
  pkg = 'xsl'
  package = ''.join(['.' + x for x in __name__.split('.')[:-1]])[1:]
  if package != '':
      pkg = package + '.' + pkg
  path = pkg_resources.resource_filename(pkg, filename)
  xml = etree.parse(path)
  return etree.XSLT(xml)

def writeXMLFile(filename, content):
    """ Used only for debugging to write out intermediate files"""
    xmlfile = open(filename, 'w')
    # pretty print
    content = etree.tostring(content, pretty_print=True)
    xmlfile.write(content)
    xmlfile.close()

def transform(odtfile, debug=False, outputdir=None):
    """ Given an ODT file this returns a tuple containing
        the cnxml, a dictionary of filename -> data, and a list of errors """
    # Store mapping of images extracted from the ODT file (and their bits)
    images = {}
    # Log of Errors and Warnings generated
    # For example, the text produced by XSLT should be:
    # {'level':'WARNING',
    #  'msg'  :'Headings without text between them are not allowed',
    #  'id'   :'import-auto-id2376'}
    # That way we can put a little * near all the cnxml where issues arose
    errors = []

    zip = zipfile.ZipFile(odtfile, 'r')
    content = zip.read('content.xml')
    xml = etree.fromstring(content)

    def appendLog(xslDoc):
        if hasattr(xslDoc, 'error_log'):
            for entry in xslDoc.error_log:
                # Entries are of the form:
                # {'level':'ERROR','id':'id1234','msg':'Descriptive message'}
                text = entry.message
                try:
                    dict = json.loads(text)
                    errors.append(dict)
                except ValueError:
                    errors.append({
                      u'level':u'CRITICAL',
                      u'id'   :u'(none)',
                      u'msg'  :unicode(text) })

    def injectStyles(xml):
        # HACK - need to find the object location from the manifest ...
        strStyles = zip.read('styles.xml')

        parser = etree.XMLParser()
        parser.feed(strStyles)
        stylesXml = parser.close()

        for i, obj in enumerate(STYLES_XPATH(stylesXml)):
          xml.append(obj)

        return xml

    # All MathML is stored in separate files "Object #/content.xml"
    # This converter includes the MathML by looking up the file in the zip
    def mathIncluder(xml):
        for i, obj in enumerate(MATH_XPATH(xml)):
            strMathPath = MATH_HREF_XPATH(obj)[0] # Or obj.get('{%s}href' % XLINK_NS)
            if strMathPath[0] == '#':
                strMathPath = strMathPath[1:]
            # Remove leading './' Zip doesn't like it
            if strMathPath[0] == '.':
                strMathPath = strMathPath[2:]

            # HACK - need to find the object location from the manifest ...
            strMathPath = os.path.join(strMathPath, 'content.xml')
            strMath = zip.read(strMathPath)

            #parser = etree.XMLParser(encoding='utf-8')
            #parser.feed(strMath)
            #math = parser.close()
            math = etree.parse(StringIO(strMath)).getroot()

            # Replace the reference to the Math with the actual MathML
            obj.getparent().replace(obj, math)
        return xml

    def imagePuller(xml):
        for i, obj in enumerate(IMAGE_XPATH(xml)):
            strPath = IMAGE_HREF_XPATH(obj)[0]
            strName = IMAGE_NAME_XPATH(obj)[0]

            strName = strName + strPath[strPath.index('.'):]

            if strPath[0] == '#':
                strPath = strPath[1:]
            # Remove leading './' Zip doesn't like it
            if strPath[0] == '.':
                strPath = strPath[2:]

            image = zip.read(strPath)
            images[strName] = image

            # Later on, an XSL pass will convert the draw:frame to a c:image and
            # set the @src correctly

        return xml

    def drawPuller(xml):
        styles = DRAW_STYLES_XPATH(xml)

        empty_odg_dirname = os.path.join(dirname, 'empty_odg_template')

        temp_dirname = tempfile.mkdtemp()
        print "======"
        print temp_dirname
        print "======"

        for i, obj in enumerate(DRAW_XPATH(xml)):
            # Copy everything except content.xml from the empty ODG (OOo Draw) template into a new zipfile

            odg_filename = DRAW_PREFIX + str(i) + '.odg'
            png_filename = DRAW_PREFIX + str(i) + '.png'

            odg_zip = zipfile.ZipFile(os.path.join(temp_dirname, odg_filename), 'w', zipfile.ZIP_DEFLATED)
            for root, dirs, files in os.walk(empty_odg_dirname):
                for name in files:
                    if name not in ('content.xml', 'styles.xml'):   # copy everything inside ZIP except content.xml or styles.xml
                        sourcename = os.path.join(root, name)
                        # http://stackoverflow.com/a/1193171/756056
                        arcname = os.path.join(root[len(empty_odg_dirname):], name)  # Path name inside the ZIP file, empty_odg_template is the root folder
                        odg_zip.write(sourcename, arcname)

            content = etree.parse(os.path.join(empty_odg_dirname, 'content.xml'))

            # Inject content styles in empty OOo Draw content.xml
            content_style_xpath = etree.XPath('/office:document-content/office:automatic-styles', namespaces=NAMESPACES)
            content_styles = content_style_xpath(content)
            for style in styles:
                content_styles[0].append(style)

            # Inject DRAW_XPATH in empty OOo Draw content.xml
            content_page_xpath = etree.XPath('/office:document-content/office:body/office:graphics/draw:page', namespaces=NAMESPACES)
            content_page = content_page_xpath(content)
            content_page[0].append(obj)

            # write modified content.xml
            odg_zip.writestr('content.xml', etree.tostring(content, xml_declaration=True, encoding='UTF-8'))

            # copy styles.xml from odt to odg without modification
            styles_xml = zip.read('styles.xml')
            odg_zip.writestr('styles.xml', styles_xml)

            odg_zip.close()

            if outputdir is not None:
                shutil.copy (os.path.join(temp_dirname, odg_filename), os.path.join(outputdir, odg_filename))

            #shutil.rmtree(temp_dirname) # TODO

        return xml

    # Reparse after XSL because the RED-escape pass injects arbitrary XML
    def redParser(xml):
        xsl = makeXsl('pass1_odt2red-escape.xsl')
        result = xsl(xml)
        appendLog(xsl)
        try:
            xml = etree.fromstring(etree.tostring(result))
        except etree.XMLSyntaxError, e:
            msg = str(e)
            xml = makeXsl('pass1_odt2red-failed.xsl')(xml, message="'%s'" % msg.replace("'", '"'))
            xml = xml.getroot()
        return xml

    def replaceSymbols(xml):
        xmlstr = etree.tostring(xml)
        xmlstr = symbols.replace(xmlstr)
        return etree.fromstring(xmlstr)

    PIPELINE = [
      drawPuller, # gets OOo Draw objects out of odt and generate odg (OOo Draw) files
      replaceSymbols,
      injectStyles, # include the styles.xml file because it contains list numbering info
      makeXsl('pass2_odt-normalize.xsl'), # This needs to be done 2x to fix headings
      makeXsl('pass2_odt-normalize.xsl'), # In the worst case all headings are 9
                            # and need to be 1. See (testbed) southwood__Lesson_2.doc
      makeXsl('pass2_odt-collapse-spans.xsl'), # Collapse adjacent spans (for RED)
      redParser, # makeXsl('pass1_odt2red-escape.xsl'),
      makeXsl('pass4_odt-headers.xsl'),
      imagePuller, # Need to run before math because both have a <draw:image> (see xpath)
      mathIncluder,
      makeXsl('pass7_odt2cnxml.xsl'),
      makeXsl('pass8_cnxml-cleanup.xsl'),
      makeXsl('pass9_id-generation.xsl'),
      makeXsl('pass10_processing-instruction-logger.xsl'),
      makeXsl('pass11_red-unescape.xsl'),
      ]

    # "xml" variable gets replaced during each iteration
    passNum = 0
    for xslDoc in PIPELINE:
        if debug: errors.append("DEBUG: Starting pass %d" % passNum)
        xml = xslDoc(xml)

        appendLog(xslDoc)
        if outputdir is not None: writeXMLFile(os.path.join(outputdir, 'pass%d.xml' % passNum), xml)
        passNum += 1

    return (xml, images, errors)

def validate(xml):
    # Validate against schema
    schemafile = open(os.path.join(dirname,
                              'schema/cnxml/rng/0.7/cnxml.rng'))
    relaxng_doc = etree.parse(schemafile)
    relaxng = etree.RelaxNG(relaxng_doc)
    if relaxng.validate(xml):
        return None
    else:
        return relaxng.error_log

def main():
    try:
      import argparse
      parser = argparse.ArgumentParser(description='Convert odt file to CNXML')
      parser.add_argument('-v', dest='verbose', help='Verbose printing to stderr', action='store_true')
      parser.add_argument('odtfile', help='/path/to/odtfile')
      parser.add_argument('outputdir', help='/path/to/outputdir', nargs='?')
      args = parser.parse_args()

      if args.verbose: print >> sys.stderr, "Transforming..."
      xml, files, errors = transform(args.odtfile, debug=args.verbose, outputdir=args.outputdir)

      if args.verbose:
          for name, bytes in files.items():
              print >> sys.stderr, "Extracted %s (%d)" % (name, len(bytes))
      for err in errors:
          print >> sys.stderr, err
      if xml is not None:
        if args.verbose: print >> sys.stderr, "Validating..."
        invalids = validate(xml)
        if invalids: print >> sys.stderr, invalids
        print etree.tostring(xml, pretty_print=True)

      if invalids:
        return 1
    except ImportError:
      print "argparse is needed for commandline"

if __name__ == '__main__':
    sys.exit(main())
<commit_msg>convert every odg to png<commit_after>import os
import sys
import tempfile
import shutil
import zipfile
import urllib
import pkg_resources
from cStringIO import StringIO
from lxml import etree, html

try:
    import json
except ImportError:
    import simplejson as json

import symbols

dirname = os.path.dirname(__file__)

NAMESPACES = {
  'office':'urn:oasis:names:tc:opendocument:xmlns:office:1.0',
  'draw':'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0',
  'xlink':'http://www.w3.org/1999/xlink',
  }

MATH_XPATH = etree.XPath('//draw:object[@xlink:href]', namespaces=NAMESPACES)
MATH_HREF_XPATH = etree.XPath('@xlink:href', namespaces=NAMESPACES)

IMAGE_XPATH = etree.XPath('//draw:frame[not(draw:object or draw:object-ole) and @draw:name and draw:image[@xlink:href and @xlink:type="simple"]]', namespaces=NAMESPACES)
IMAGE_HREF_XPATH = etree.XPath('draw:image/@xlink:href', namespaces=NAMESPACES)
IMAGE_NAME_XPATH = etree.XPath('@draw:name', namespaces=NAMESPACES)
STYLES_XPATH = etree.XPath('//office:styles', namespaces=NAMESPACES)
DRAW_XPATH = etree.XPath('//draw:g[not(parent::draw:*)]', namespaces=NAMESPACES)
DRAW_STYLES_XPATH = etree.XPath('/office:document-content/office:automatic-styles/*', namespaces=NAMESPACES)

DRAW_PREFIX = "draw_odg"

def makeXsl(filename):
  """ Helper that creates a XSLT stylesheet """
  pkg = 'xsl'
  package = ''.join(['.' + x for x in __name__.split('.')[:-1]])[1:]
  if package != '':
      pkg = package + '.' + pkg
  path = pkg_resources.resource_filename(pkg, filename)
  xml = etree.parse(path)
  return etree.XSLT(xml)

def writeXMLFile(filename, content):
    """ Used only for debugging to write out intermediate files"""
    xmlfile = open(filename, 'w')
    # pretty print
    content = etree.tostring(content, pretty_print=True)
    xmlfile.write(content)
    xmlfile.close()

def transform(odtfile, debug=False, outputdir=None):
    """ Given an ODT file this returns a tuple containing
        the cnxml, a dictionary of filename -> data, and a list of errors """
    # Store mapping of images extracted from the ODT file (and their bits)
    images = {}
    # Log of Errors and Warnings generated
    # For example, the text produced by XSLT should be:
    # {'level':'WARNING',
    #  'msg'  :'Headings without text between them are not allowed',
    #  'id'   :'import-auto-id2376'}
    # That way we can put a little * near all the cnxml where issues arose
    errors = []

    zip = zipfile.ZipFile(odtfile, 'r')
    content = zip.read('content.xml')
    xml = etree.fromstring(content)

    def appendLog(xslDoc):
        if hasattr(xslDoc, 'error_log'):
            for entry in xslDoc.error_log:
                # Entries are of the form:
                # {'level':'ERROR','id':'id1234','msg':'Descriptive message'}
                text = entry.message
                try:
                    dict = json.loads(text)
                    errors.append(dict)
                except ValueError:
                    errors.append({
                      u'level':u'CRITICAL',
                      u'id'   :u'(none)',
                      u'msg'  :unicode(text) })

    def injectStyles(xml):
        # HACK - need to find the object location from the manifest ...
        strStyles = zip.read('styles.xml')

        parser = etree.XMLParser()
        parser.feed(strStyles)
        stylesXml = parser.close()

        for i, obj in enumerate(STYLES_XPATH(stylesXml)):
          xml.append(obj)

        return xml

    # All MathML is stored in separate files "Object #/content.xml"
    # This converter includes the MathML by looking up the file in the zip
    def mathIncluder(xml):
        for i, obj in enumerate(MATH_XPATH(xml)):
            strMathPath = MATH_HREF_XPATH(obj)[0] # Or obj.get('{%s}href' % XLINK_NS)
            if strMathPath[0] == '#':
                strMathPath = strMathPath[1:]
            # Remove leading './' Zip doesn't like it
            if strMathPath[0] == '.':
                strMathPath = strMathPath[2:]

            # HACK - need to find the object location from the manifest ...
            strMathPath = os.path.join(strMathPath, 'content.xml')
            strMath = zip.read(strMathPath)

            #parser = etree.XMLParser(encoding='utf-8')
            #parser.feed(strMath)
            #math = parser.close()
            math = etree.parse(StringIO(strMath)).getroot()

            # Replace the reference to the Math with the actual MathML
            obj.getparent().replace(obj, math)
        return xml

    def imagePuller(xml):
        for i, obj in enumerate(IMAGE_XPATH(xml)):
            strPath = IMAGE_HREF_XPATH(obj)[0]
            strName = IMAGE_NAME_XPATH(obj)[0]

            strName = strName + strPath[strPath.index('.'):]

            if strPath[0] == '#':
                strPath = strPath[1:]
            # Remove leading './' Zip doesn't like it
            if strPath[0] == '.':
                strPath = strPath[2:]

            image = zip.read(strPath)
            images[strName] = image

            # Later on, an XSL pass will convert the draw:frame to a c:image and
            # set the @src correctly

        return xml

    def drawPuller(xml):
        styles = DRAW_STYLES_XPATH(xml)

        empty_odg_dirname = os.path.join(dirname, 'empty_odg_template')

        temp_dirname = tempfile.mkdtemp()

        for i, obj in enumerate(DRAW_XPATH(xml)):
            # Copy everything except content.xml from the empty ODG (OOo Draw) template into a new zipfile

            odg_filename = DRAW_PREFIX + str(i) + '.odg'
            png_filename = DRAW_PREFIX + str(i) + '.png'

            odg_zip = zipfile.ZipFile(os.path.join(temp_dirname, odg_filename), 'w', zipfile.ZIP_DEFLATED)
            for root, dirs, files in os.walk(empty_odg_dirname):
                for name in files:
                    if name not in ('content.xml', 'styles.xml'):   # copy everything inside ZIP except content.xml or styles.xml
                        sourcename = os.path.join(root, name)
                        # http://stackoverflow.com/a/1193171/756056
                        arcname = os.path.join(root[len(empty_odg_dirname):], name)  # Path name inside the ZIP file, empty_odg_template is the root folder
                        odg_zip.write(sourcename, arcname)

            content = etree.parse(os.path.join(empty_odg_dirname, 'content.xml'))

            # Inject content styles in empty OOo Draw content.xml
            content_style_xpath = etree.XPath('/office:document-content/office:automatic-styles', namespaces=NAMESPACES)
            content_styles = content_style_xpath(content)
            for style in styles:
                content_styles[0].append(style)

            # Inject DRAW_XPATH in empty OOo Draw content.xml
            content_page_xpath = etree.XPath('/office:document-content/office:body/office:graphics/draw:page', namespaces=NAMESPACES)
            content_page = content_page_xpath(content)
            content_page[0].append(obj)

            # write modified content.xml
            odg_zip.writestr('content.xml', etree.tostring(content, xml_declaration=True, encoding='UTF-8'))

            # copy styles.xml from odt to odg without modification
            styles_xml = zip.read('styles.xml')
            odg_zip.writestr('styles.xml', styles_xml)

            odg_zip.close()

            # convert every odg to png
            command = '/usr/bin/soffice -headless -nologo -nofirststartwizard "macro:///Standard.Module1.SaveAsPNG(%s,%s)"' % (os.path.join(temp_dirname, odg_filename),os.path.join(temp_dirname, png_filename))
            os.system(command)

            if outputdir is not None:
                shutil.copy (os.path.join(temp_dirname, odg_filename), os.path.join(outputdir, odg_filename))
                shutil.copy (os.path.join(temp_dirname, png_filename), os.path.join(outputdir, png_filename))

            #shutil.rmtree(temp_dirname) # TODO

        return xml

    # Reparse after XSL because the RED-escape pass injects arbitrary XML
    def redParser(xml):
        xsl = makeXsl('pass1_odt2red-escape.xsl')
        result = xsl(xml)
        appendLog(xsl)
        try:
            xml = etree.fromstring(etree.tostring(result))
        except etree.XMLSyntaxError, e:
            msg = str(e)
            xml = makeXsl('pass1_odt2red-failed.xsl')(xml, message="'%s'" % msg.replace("'", '"'))
            xml = xml.getroot()
        return xml

    def replaceSymbols(xml):
        xmlstr = etree.tostring(xml)
        xmlstr = symbols.replace(xmlstr)
        return etree.fromstring(xmlstr)

    PIPELINE = [
      drawPuller, # gets OOo Draw objects out of odt and generate odg (OOo Draw) files
      replaceSymbols,
      injectStyles, # include the styles.xml file because it contains list numbering info
      makeXsl('pass2_odt-normalize.xsl'), # This needs to be done 2x to fix headings
      makeXsl('pass2_odt-normalize.xsl'), # In the worst case all headings are 9
                            # and need to be 1. See (testbed) southwood__Lesson_2.doc
      makeXsl('pass2_odt-collapse-spans.xsl'), # Collapse adjacent spans (for RED)
      redParser, # makeXsl('pass1_odt2red-escape.xsl'),
      makeXsl('pass4_odt-headers.xsl'),
      imagePuller, # Need to run before math because both have a <draw:image> (see xpath)
      mathIncluder,
      makeXsl('pass7_odt2cnxml.xsl'),
      makeXsl('pass8_cnxml-cleanup.xsl'),
      makeXsl('pass9_id-generation.xsl'),
      makeXsl('pass10_processing-instruction-logger.xsl'),
      makeXsl('pass11_red-unescape.xsl'),
      ]

    # "xml" variable gets replaced during each iteration
    passNum = 0
    for xslDoc in PIPELINE:
        if debug: errors.append("DEBUG: Starting pass %d" % passNum)
        xml = xslDoc(xml)

        appendLog(xslDoc)
        if outputdir is not None: writeXMLFile(os.path.join(outputdir, 'pass%d.xml' % passNum), xml)
        passNum += 1

    return (xml, images, errors)

def validate(xml):
    # Validate against schema
    schemafile = open(os.path.join(dirname,
                              'schema/cnxml/rng/0.7/cnxml.rng'))
    relaxng_doc = etree.parse(schemafile)
    relaxng = etree.RelaxNG(relaxng_doc)
    if relaxng.validate(xml):
        return None
    else:
        return relaxng.error_log

def main():
    try:
      import argparse
      parser = argparse.ArgumentParser(description='Convert odt file to CNXML')
      parser.add_argument('-v', dest='verbose', help='Verbose printing to stderr', action='store_true')
      parser.add_argument('odtfile', help='/path/to/odtfile')
      parser.add_argument('outputdir', help='/path/to/outputdir', nargs='?')
      args = parser.parse_args()

      if args.verbose: print >> sys.stderr, "Transforming..."
      xml, files, errors = transform(args.odtfile, debug=args.verbose, outputdir=args.outputdir)

      if args.verbose:
          for name, bytes in files.items():
              print >> sys.stderr, "Extracted %s (%d)" % (name, len(bytes))
      for err in errors:
          print >> sys.stderr, err
      if xml is not None:
        if args.verbose: print >> sys.stderr, "Validating..."
        invalids = validate(xml)
        if invalids: print >> sys.stderr, invalids
        print etree.tostring(xml, pretty_print=True)

      if invalids:
        return 1
    except ImportError:
      print "argparse is needed for commandline"

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


<commit_before>from paynova_api_python_client import Paynova, PaynovaException

import logging
logging.basicConfig(level='DEBUG')

# test client

client = Paynova('<MERCHANT ID>', '<API Password>')

try:
    # create order
    # For more information about parameters, see http://docs.paynova.com/display/API/Create+Order

    response = client.create_order({
        'orderNumber': 'order-id-0001',
        'currencyCode': 'EUR',
        'totalAmount': 10
    })

except PaynovaException as e:
    # process exception
    # e.errorNumber, e.statusKey, e.statusMessage, e.errors
    pass

# get order id from response
# For more information about parameters, see http://docs.paynova.com/display/API/Initialize+Payment

orderId = response.get('orderId')

try:
    response = client.initialize_payment({
        'orderId': orderId,
        'totalAmount': 10,
        'paymentChannelId': 1,
        'interfaceOptions': {
            'interfaceId': 5,
            'customerLanguageCode': 'eng',
            'urlRedirectSuccess': 'http://www.merchant-url.com/success',    # Change to your URL
            'urlRedirectCancel': 'http://www.merchant-url.com/cancel',    # Change to your URL
            'urlRedirectPending': 'http://www.merchant-url.com/pending',    # Change to your URL
            'urlCallback': 'http://www.url-where-to-receive-event-hooks.com/',    # Change to your URL
        }
    })
except PaynovaException as e:
    # process exception
    # e.errorNumber, e.statusKey, e.statusMessage, e.errors
    pass

url = response.get('url')<commit_msg>optimize example<commit_after>from paynova_api_python_client import Paynova, PaynovaException

import logging
logging.basicConfig(level='DEBUG')

# test client

client = Paynova('<MERCHANT ID>', '<API Password>')

try:
    # create order
    # For more information about parameters, see http://docs.paynova.com/display/API/Create+Order

    response = client.create_order({
        'orderNumber': 'order-id-0001',
        'currencyCode': 'EUR',
        'totalAmount': 10
    })

    # get order id from response

    orderId = response.get('orderId')

    # init payment
    # For more information about parameters, see http://docs.paynova.com/display/API/Initialize+Payment

    response = client.initialize_payment({
        'orderId': orderId,
        'totalAmount': 10,
        'paymentChannelId': 1,
        'interfaceOptions': {
            'interfaceId': 5,
            'customerLanguageCode': 'eng',
            'urlRedirectSuccess': 'http://www.merchant-url.com/success',    # Change to your URL
            'urlRedirectCancel': 'http://www.merchant-url.com/cancel',    # Change to your URL
            'urlRedirectPending': 'http://www.merchant-url.com/pending',    # Change to your URL
            'urlCallback': 'http://www.url-where-to-receive-event-hooks.com/',    # Change to your URL
        }
    })

    url = response.get('url')

    # redirect to url

except PaynovaException as e:
    # process exception
    # e.errorNumber, e.statusKey, e.statusMessage, e.errors
    pass<|endoftext|>


<commit_before>import numpy as np
import bct
from sklearn.externals import joblib
from my_settings import *

from sklearn.ensemble import AdaBoostClassifier
from sklearn.cross_validation import (StratifiedShuffleSplit, cross_val_score)
from sklearn.grid_search import GridSearchCV

subjects = ["0008", "0009", "0010", "0012", "0013", "0014", "0015", "0016",
            "0017", "0018", "0019", "0020", "0022"]

cls_all = []
pln_all = []

scores_all = np.empty([4, 10])

for subject in subjects:
    cls = np.load(source_folder + "graph_data/%s_classic_pow_pln.npy" %
                  subject).item()

    pln = np.load(source_folder + "graph_data/%s_plan_pow_pln.npy" %
                  subject).item()

    cls_all.append(cls)
    pln_all.append(pln)

for band in bands.keys():
    data_cls = []
    for j in range(len(cls_all)):
        tmp = cls_all[j][band]
        data_cls.append(np.asarray([bct.centrality.pagerank_centrality(
            g, d=0.85) for g in tmp]).mean(axis=0))
    data_pln = []
    for j in range(len(pln_all)):
        tmp = pln_all[j][band]
        data_pln.append(np.asarray([bct.centrality.pagerank_centrality(
            g, d=0.85) for g in tmp]).mean(axis=0))

    data_cls = np.asarray(data_cls)
    data_pln = np.asarray(data_pln)

    X = np.vstack([data_cls, data_pln])
    y = np.concatenate([np.zeros(len(data_cls)), np.ones(len(data_pln))])

    cv = StratifiedShuffleSplit(y, test_size=0.1)

    cv_params = {"learning_rate": np.arange(0.1, 1.1, 0.1),
                 'n_estimators': np.arange(1, 80, 2)}

    grid = GridSearchCV(AdaBoostClassifier(),
                        cv_params,
                        scoring='accuracy',
                        cv=cv,
                        n_jobs=6,
                        verbose=1)
    grid.fit(X, y)
    ada_cv = grid.best_estimator_

    scores = cross_val_score(ada_cv, X, y, cv=cv)
    scores_all[j, :] = scores

    # save the classifier
    joblib.dump(ada_cv, source_folder + "graph_data/pagerank_ada_%.plk" % band)

np.save(source_folder + "graph_data/pagerank_scores_all.npy")
<commit_msg>Fixed save index<commit_after>import numpy as np
import bct
from sklearn.externals import joblib
from my_settings import *

from sklearn.ensemble import AdaBoostClassifier
from sklearn.cross_validation import (StratifiedShuffleSplit, cross_val_score)
from sklearn.grid_search import GridSearchCV

subjects = ["0008", "0009", "0010", "0012", "0013", "0014", "0015", "0016",
            "0017", "0018", "0019", "0020", "0022"]

cls_all = []
pln_all = []

scores_all = np.empty([4, 10])

for subject in subjects:
    cls = np.load(source_folder + "graph_data/%s_classic_pow_pln.npy" %
                  subject).item()

    pln = np.load(source_folder + "graph_data/%s_plan_pow_pln.npy" %
                  subject).item()

    cls_all.append(cls)
    pln_all.append(pln)

for k, band in enumerate(bands.keys()):
    data_cls = []
    for j in range(len(cls_all)):
        tmp = cls_all[j][band]
        data_cls.append(np.asarray([bct.centrality.pagerank_centrality(
            g, d=0.85) for g in tmp]).mean(axis=0))
    data_pln = []
    for j in range(len(pln_all)):
        tmp = pln_all[j][band]
        data_pln.append(np.asarray([bct.centrality.pagerank_centrality(
            g, d=0.85) for g in tmp]).mean(axis=0))

    data_cls = np.asarray(data_cls)
    data_pln = np.asarray(data_pln)

    X = np.vstack([data_cls, data_pln])
    y = np.concatenate([np.zeros(len(data_cls)), np.ones(len(data_pln))])

    cv = StratifiedShuffleSplit(y, test_size=0.1)

    cv_params = {"learning_rate": np.arange(0.1, 1.1, 0.1),
                 'n_estimators': np.arange(1, 80, 2)}

    grid = GridSearchCV(AdaBoostClassifier(),
                        cv_params,
                        scoring='accuracy',
                        cv=cv,
                        n_jobs=6,
                        verbose=1)
    grid.fit(X, y)
    ada_cv = grid.best_estimator_

    scores = cross_val_score(ada_cv, X, y, cv=cv)
    scores_all[k, :] = scores

    # save the classifier
    joblib.dump(ada_cv, source_folder + "graph_data/pagerank_ada_%.plk" % band)

np.save(source_folder + "graph_data/pagerank_scores_all.npy")
<|endoftext|>


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

from scipy import asarray, random, spatial, stats, zeros

def Test(X, Y, perms=100000, method='pearson'):
  """
  Takes two distance matrices (either redundant matrices or condensed
  vectors) and performs a Mantel test.

  Parameters
  ----------
  X : array_like
      First distance matrix (condensed or redundant)
  Y : array_like
      Second distance matrix (condensed or redundant), where the order
      of elements corresponds to the order of elements in the first matrix
  perms : int, optional
      The number of permutations to perform
  method : str, optional
      Type of correlation coefficient to use; either 'pearson', 'spearman',
      or 'kendall'

  Returns
  -------
  z : float
      A standard score (z-score)
  """

  X = asarray(X, dtype=float)
  Y = asarray(Y, dtype=float)

  if spatial.distance.is_valid_dm(X) == False and spatial.distance.is_valid_y(X) == False:
    raise ValueError('X is not a valid distance matrix')

  if spatial.distance.is_valid_dm(Y) == False and spatial.distance.is_valid_y(Y) == False:
    raise ValueError('Y is not a valid distance matrix')

  # X is vector and Y is vector
  if len(X.shape) == 1 and len(Y.shape) == 1:
    Y_as_matrix = spatial.distance.squareform(Y, 'tomatrix', False)

  # X is vector and Y is matrix
  elif len(X.shape) == 1 and len(Y.shape) == 2:
    Y_as_matrix = Y
    Y = spatial.distance.squareform(Y, 'tovector', False)

  # X is matrix and Y is vector
  elif len(X.shape) == 2 and len(Y.shape) == 1:
    Y_as_matrix = X
    X, Y = Y, spatial.distance.squareform(X, 'tovector', False)

  # X is matrix and Y is matrix
  elif len(X.shape) == 2 and len(Y.shape) == 2:
    Y_as_matrix = Y
    X = spatial.distance.squareform(X, 'tovector', False)
    Y = spatial.distance.squareform(Y, 'tovector', False)

  else:
    raise ValueError('X and Y should have 1 or 2 dimensions')

  if X.shape[0] != Y.shape[0]:
    raise ValueError('X and Y are not of equal size')

  if method == 'pearson':
    correlate = stats.pearsonr

  elif method == 'spearman':
    correlate = stats.spearmanr

  elif method == 'kendall':
    correlate = stats.kendalltau

  else:
    raise ValueError('The correlation method should be set to "pearson", "spearman", or "kendall"')

  r = correlate(X, Y)[0] # Veridical correlation
  n = Y_as_matrix.shape[0] # Matrix size (N x N)
  MC_corrs = zeros(perms, dtype=float) # Empty array for Monte Carlo correlations

  for i in xrange(perms):
    permutation = random.permutation(n) # Random order in which to permute the matrix
    Y_as_matrix_permuted = Y_as_matrix[permutation, :][:, permutation] # Permute the matrix
    Y_permuted = spatial.distance.squareform(Y_as_matrix_permuted, 'tovector', False) # Convert back to vector
    MC_corrs[i] = correlate(X, Y_permuted)[0] # Store the correlation between X and permuted Y

  m = MC_corrs.mean() # Mean of Monte Carlo correlations
  sd = MC_corrs.std() # Standard deviation of Monte Carlo correlations
  z = (r - m) / sd # Z-score

  return z, r, m, sd
<commit_msg>Updated comment to detail all return values<commit_after>#!/usr/bin/env python

from scipy import asarray, random, spatial, stats, zeros

def Test(X, Y, perms=100000, method='pearson'):
  """
  Takes two distance matrices (either redundant matrices or condensed
  vectors) and performs a Mantel test.

  Parameters
  ----------
  X : array_like
      First distance matrix (condensed or redundant)
  Y : array_like
      Second distance matrix (condensed or redundant), where the order
      of elements corresponds to the order of elements in the first matrix
  perms : int, optional
      The number of permutations to perform
  method : str, optional
      Type of correlation coefficient to use; either 'pearson', 'spearman',
      or 'kendall'

  Returns
  -------
  z : float
      A standard score (z-score)
  r : float
      Veridical correlation
  m : float
      Mean of Monte Carlo sample correlations
  sd : float
      Standard deviation of Monte Carlo sample correlations
  """

  X = asarray(X, dtype=float)
  Y = asarray(Y, dtype=float)

  if spatial.distance.is_valid_dm(X) == False and spatial.distance.is_valid_y(X) == False:
    raise ValueError('X is not a valid distance matrix')

  if spatial.distance.is_valid_dm(Y) == False and spatial.distance.is_valid_y(Y) == False:
    raise ValueError('Y is not a valid distance matrix')

  # X is vector and Y is vector
  if len(X.shape) == 1 and len(Y.shape) == 1:
    Y_as_matrix = spatial.distance.squareform(Y, 'tomatrix', False)

  # X is vector and Y is matrix
  elif len(X.shape) == 1 and len(Y.shape) == 2:
    Y_as_matrix = Y
    Y = spatial.distance.squareform(Y, 'tovector', False)

  # X is matrix and Y is vector
  elif len(X.shape) == 2 and len(Y.shape) == 1:
    Y_as_matrix = X
    X, Y = Y, spatial.distance.squareform(X, 'tovector', False)

  # X is matrix and Y is matrix
  elif len(X.shape) == 2 and len(Y.shape) == 2:
    Y_as_matrix = Y
    X = spatial.distance.squareform(X, 'tovector', False)
    Y = spatial.distance.squareform(Y, 'tovector', False)

  else:
    raise ValueError('X and Y should have 1 or 2 dimensions')

  if X.shape[0] != Y.shape[0]:
    raise ValueError('X and Y are not of equal size')

  if method == 'pearson':
    correlate = stats.pearsonr

  elif method == 'spearman':
    correlate = stats.spearmanr

  elif method == 'kendall':
    correlate = stats.kendalltau

  else:
    raise ValueError('The correlation method should be set to "pearson", "spearman", or "kendall"')

  r = correlate(X, Y)[0] # Veridical correlation
  n = Y_as_matrix.shape[0] # Matrix size (N x N)
  MC_corrs = zeros(perms, dtype=float) # Empty array for Monte Carlo correlations

  for i in xrange(perms):
    permutation = random.permutation(n) # Random order in which to permute the matrix
    Y_as_matrix_permuted = Y_as_matrix[permutation, :][:, permutation] # Permute the matrix
    Y_permuted = spatial.distance.squareform(Y_as_matrix_permuted, 'tovector', False) # Convert back to vector
    MC_corrs[i] = correlate(X, Y_permuted)[0] # Store the correlation between X and permuted Y

  m = MC_corrs.mean() # Mean of Monte Carlo correlations
  sd = MC_corrs.std() # Standard deviation of Monte Carlo correlations
  z = (r - m) / sd # Z-score

  return z, r, m, sd
<|endoftext|>


<commit_before>#!/usr/bin/env python3
#!/usr/bin/env pypy3
"""Format ontology files using a uniform ttl serializer from rdflib

Usage:
    ttlfmt [options]
    ttlfmt [options] <file>...

Options:
    -h --help       print this
    -v --verbose    do something fun!
    -a --vanilla    use the regular rdflib turtle serializer
    -y --subclass   use the subClassOf turtle serializer
    -c --compact    use the compact turtle serializer
    -u --uncompact  use the uncompact turtle serializer
    -r --racket     use the racket turtle serializer
    -j --jsonld     use the rdflib-jsonld serializer

    -f --format=FM  specify the input format (used for pipes)
    -t --outfmt=F   specify the output format [default: nifttl]

    -s --slow       do not use a process pool
    -n --nowrite    parse the file and reserialize it but do not write changes
    -o --output=FI  serialize all input files to output file
    -p --profile    enable profiling on parsing and serialization
    -d --debug      launch debugger after parsing and before serialization

"""
import os
import sys
from io import StringIO, TextIOWrapper
from json.decoder import JSONDecodeError
from concurrent.futures import ProcessPoolExecutor
from docopt import docopt, parse_defaults
import rdflib
from rdflib.plugins.parsers.notation3 import BadSyntax
from ttlser.utils import regjsonld

defaults = {o.name:o.value if o.argcount else None for o in parse_defaults(__doc__)}
#GRAPHCLASS = rdflib.Graph
GRAPHCLASS = rdflib.ConjunctiveGraph

def getVersion():
    ttlser = rdflib.plugin.get('nifttl', rdflib.serializer.Serializer)
    ttlser_version = ttlser._CustomTurtleSerializer__version  # FIXME
    version = "ttlfmt v0.0.2\nttlser {}".format(ttlser_version)
    return version

def prepare(filepath_or_stream, outpath=None, stream=False):
    if stream:
        infmt_guess = 'turtle'
        if outpath is None:
            outpath = sys.stdout
    else:
        filepath_or_stream = os.path.expanduser(filepath_or_stream)
        _, ext = os.path.splitext(filepath_or_stream)
        filetype = ext.strip('.')
        if filetype == 'ttl':
            infmt_guess = 'ttl'
        elif filetype in ('json', 'jsonld'):
            infmt_guess = 'json-ld'
            regjsonld()
        else:
            infmt_guess = None
        if outpath is None:
            outpath = filepath_or_stream
        sys.stderr.write(str(filepath_or_stream) + "\n")

    return dict(source=filepath_or_stream,
                format_guess=infmt_guess,
                outpath=outpath)

formats = ('ttl', 'json-ld', None, 'xml', 'n3', 'nt', 'nquads', 'trix',
           'trig', 'hturtle', 'rdfa', 'mdata', 'rdfa1.0', 'html')

def parse(source, format_guess, outpath, graph=None, infmt=None, graph_class=GRAPHCLASS):
    graph = graph_class() if graph is None else graph
    errors = []
    if infmt:
        format_guess = infmt

    for format in (format_guess, *(f for f in formats if f != format_guess)):
        # TODO we don't need to reset just saved parsed streams to the point where they fail?
        if type(source) == TextIOWrapper:  # stdin can't reset
            src = source.read()
            source = StringIO(src)
        try:
            graph.parse(source=source, format=format)
            a = next(iter(graph))
            return graph, outpath
        except (StopIteration, BadSyntax, JSONDecodeError) as e:
            sys.stderr.write('PARSING FAILED {} {}\n'.format(format, source))
            if infmt:  # or format_guess != None:
                raise e
            errors.append(e)
            if type(source) == StringIO:
                source.seek(0)
    raise BadSyntax(str(errors)) from errors[0]

def serialize(graph, outpath, outfmt=defaults['--outfmt'],
              debug=False, profile=False, nowrite=False):
    if debug:
        if type(outpath) == type(sys.stdout):
            pipe_debug(graph=graph, outpath=outpath)
        else:
            breakpoint()

    elif profile and type(outpath) != type(sys.stdout):
        *_, _out = outpath.rsplit('/', 1)
        sys.stderr.write('triple count for {_out}: {_len}\n'.format(_out=_out, _len=len(graph)))

    if outfmt == 'json-ld':
        kwargs = {'auto_compact': True}
    else:
        kwargs = {}

    out = graph.serialize(format=outfmt, encoding='utf-8', **kwargs)

    if nowrite:
        sys.stderr.write('FILE NOT WRITTEN {}\n'.format(outpath))
        return

    if profile:
        sys.stderr.write('PARSING Success {}\n'.format(outpath))
    elif not isinstance(outpath, str):  # FIXME not a good test that it is stdout
        outpath.buffer.write(out)
    else:
        with open(outpath, 'wb') as f:
            f.write(out)

def convert(file_or_list_or_stream, outpath=None, stream=False,
            infmt=None, outfmt=defaults['--outfmt'],
            debug=False, profile=False, nowrite=False, graph_class=GRAPHCLASS):
    if stream or type(file_or_list_or_stream) == str:
        file_or_stream = file_or_list_or_stream
        serialize(*parse(**prepare(file_or_stream, outpath, stream),
                         infmt=infmt),
                  outfmt=outfmt, debug=debug, profile=profile, nowrite=nowrite)
    else:
        # file list is used here because this allows is to merge files
        # without any additional code if we pass it more than one file
        # in normal use a tuple with a single element is passed in
        file_list = file_or_list_or_stream
        if outpath is not None:
            graph = graph_class()
            [parse(**prepare(file, outpath), graph=graph, infmt=infmt) for file in file_list]
            serialize(graph, outpath, outfmt=outfmt,
                      debug=debug, profile=profile, nowrite=nowrite)
        else:
            [convert(file, infmt=infmt, outfmt=outfmt,
                     debug=debug, profile=profile,
                     graph_class=graph_class) for file in file_list]

def pipe_debug(*args, source=None, graph=None, outpath=None, **kwargs):
    fn = sys.stdout.fileno()
    tty = os.ttyname(fn)
    with open(tty) as sys.stdin:
        breakpoint()

def main():
    #global args  # vastly preferable to classing everything since this way we can see
    #global outfmt  # in 2 lines what is actually shared instead of stuffed into self
    args = docopt(__doc__, version=getVersion())

    profile = args['--profile']
    if profile:
        try:
            from desc.prof import profile_me
            global parse
            global serialize
            parse = profile_me(parse)
            serialize = profile_me(serialize)
        except ImportError:
            pass

    nowrite = args['--nowrite']

    infmt = args['--format']
    debug = args['--debug']

    if args['--subclass']:
        outfmt = 'scottl'
    elif args['--vanilla']:
        outfmt = 'turtle'
    elif args['--compact']:
        outfmt = 'cmpttl'
    elif args['--uncompact']:
        outfmt = 'uncmpttl'
    elif args['--jsonld']:
        outfmt = 'json-ld'
    elif args['--racket']:
        outfmt = 'rktttl'
    else:
        outfmt = args['--outfmt']

    if outfmt == 'json-ld' or infmt == 'json-ld':
        regjsonld()

    outpath = args['--output']
    files = args['<file>']
    if not files:
        from ttlser.utils import readFromStdIn
        stdin = readFromStdIn(sys.stdin)
        if stdin is not None:
            convert(stdin, outpath, stream=True,
                    infmt=infmt, outfmt=outfmt,
                    debug=debug, profile=profile,
                    nowrite=nowrite)
        else:
            print(__doc__)
    else:
        lenfiles = len(files)
        if outpath or args['--slow'] or lenfiles == 1:
            if lenfiles == 1:
                files,  = files

            convert(files, outpath=outpath,
                    infmt=infmt, outfmt=outfmt,
                    debug=debug, profile=profile,
                    nowrite=nowrite)
        else:
            from joblib import Parallel, delayed
            nj = 9
            if lenfiles < nj:
                nj = lenfiles

            Parallel(n_jobs=nj, verbose=10)(delayed(convert)
                                            (file,
                                             infmt=infmt, outfmt=outfmt,
                                             debug=debug, profile=profile,
                                             nowrite=nowrite)
                                            for file in files)

if __name__ == '__main__':
    main()
<commit_msg>ttlfmt remove regjsonld<commit_after>#!/usr/bin/env python3
#!/usr/bin/env pypy3
"""Format ontology files using a uniform ttl serializer from rdflib

Usage:
    ttlfmt [options]
    ttlfmt [options] <file>...

Options:
    -h --help       print this
    -v --verbose    do something fun!
    -a --vanilla    use the regular rdflib turtle serializer
    -y --subclass   use the subClassOf turtle serializer
    -c --compact    use the compact turtle serializer
    -u --uncompact  use the uncompact turtle serializer
    -r --racket     use the racket turtle serializer
    -j --jsonld     use the rdflib-jsonld serializer

    -f --format=FM  specify the input format (used for pipes)
    -t --outfmt=F   specify the output format [default: nifttl]

    -s --slow       do not use a process pool
    -n --nowrite    parse the file and reserialize it but do not write changes
    -o --output=FI  serialize all input files to output file
    -p --profile    enable profiling on parsing and serialization
    -d --debug      launch debugger after parsing and before serialization

"""
import os
import sys
from io import StringIO, TextIOWrapper
from json.decoder import JSONDecodeError
from concurrent.futures import ProcessPoolExecutor
from docopt import docopt, parse_defaults
import rdflib
from rdflib.plugins.parsers.notation3 import BadSyntax

defaults = {o.name:o.value if o.argcount else None for o in parse_defaults(__doc__)}
#GRAPHCLASS = rdflib.Graph
GRAPHCLASS = rdflib.ConjunctiveGraph

def getVersion():
    ttlser = rdflib.plugin.get('nifttl', rdflib.serializer.Serializer)
    ttlser_version = ttlser._CustomTurtleSerializer__version  # FIXME
    version = "ttlfmt v0.0.2\nttlser {}".format(ttlser_version)
    return version

def prepare(filepath_or_stream, outpath=None, stream=False):
    if stream:
        infmt_guess = 'turtle'
        if outpath is None:
            outpath = sys.stdout
    else:
        filepath_or_stream = os.path.expanduser(filepath_or_stream)
        _, ext = os.path.splitext(filepath_or_stream)
        filetype = ext.strip('.')
        if filetype == 'ttl':
            infmt_guess = 'ttl'
        elif filetype in ('json', 'jsonld'):
            infmt_guess = 'json-ld'
        else:
            infmt_guess = None
        if outpath is None:
            outpath = filepath_or_stream
        sys.stderr.write(str(filepath_or_stream) + "\n")

    return dict(source=filepath_or_stream,
                format_guess=infmt_guess,
                outpath=outpath)

formats = ('ttl', 'json-ld', None, 'xml', 'n3', 'nt', 'nquads', 'trix',
           'trig', 'hturtle', 'rdfa', 'mdata', 'rdfa1.0', 'html')

def parse(source, format_guess, outpath, graph=None, infmt=None, graph_class=GRAPHCLASS):
    graph = graph_class() if graph is None else graph
    errors = []
    if infmt:
        format_guess = infmt

    for format in (format_guess, *(f for f in formats if f != format_guess)):
        # TODO we don't need to reset just saved parsed streams to the point where they fail?
        if type(source) == TextIOWrapper:  # stdin can't reset
            src = source.read()
            source = StringIO(src)
        try:
            graph.parse(source=source, format=format)
            a = next(iter(graph))
            return graph, outpath
        except (StopIteration, BadSyntax, JSONDecodeError) as e:
            sys.stderr.write('PARSING FAILED {} {}\n'.format(format, source))
            if infmt:  # or format_guess != None:
                raise e
            errors.append(e)
            if type(source) == StringIO:
                source.seek(0)
    raise BadSyntax(str(errors)) from errors[0]

def serialize(graph, outpath, outfmt=defaults['--outfmt'],
              debug=False, profile=False, nowrite=False):
    if debug:
        if type(outpath) == type(sys.stdout):
            pipe_debug(graph=graph, outpath=outpath)
        else:
            breakpoint()

    elif profile and type(outpath) != type(sys.stdout):
        *_, _out = outpath.rsplit('/', 1)
        sys.stderr.write('triple count for {_out}: {_len}\n'.format(_out=_out, _len=len(graph)))

    if outfmt == 'json-ld':
        kwargs = {'auto_compact': True}
    else:
        kwargs = {}

    out = graph.serialize(format=outfmt, encoding='utf-8', **kwargs)

    if nowrite:
        sys.stderr.write('FILE NOT WRITTEN {}\n'.format(outpath))
        return

    if profile:
        sys.stderr.write('PARSING Success {}\n'.format(outpath))
    elif not isinstance(outpath, str):  # FIXME not a good test that it is stdout
        outpath.buffer.write(out)
    else:
        with open(outpath, 'wb') as f:
            f.write(out)

def convert(file_or_list_or_stream, outpath=None, stream=False,
            infmt=None, outfmt=defaults['--outfmt'],
            debug=False, profile=False, nowrite=False, graph_class=GRAPHCLASS):
    if stream or type(file_or_list_or_stream) == str:
        file_or_stream = file_or_list_or_stream
        serialize(*parse(**prepare(file_or_stream, outpath, stream),
                         infmt=infmt),
                  outfmt=outfmt, debug=debug, profile=profile, nowrite=nowrite)
    else:
        # file list is used here because this allows is to merge files
        # without any additional code if we pass it more than one file
        # in normal use a tuple with a single element is passed in
        file_list = file_or_list_or_stream
        if outpath is not None:
            graph = graph_class()
            [parse(**prepare(file, outpath), graph=graph, infmt=infmt) for file in file_list]
            serialize(graph, outpath, outfmt=outfmt,
                      debug=debug, profile=profile, nowrite=nowrite)
        else:
            [convert(file, infmt=infmt, outfmt=outfmt,
                     debug=debug, profile=profile,
                     graph_class=graph_class) for file in file_list]

def pipe_debug(*args, source=None, graph=None, outpath=None, **kwargs):
    fn = sys.stdout.fileno()
    tty = os.ttyname(fn)
    with open(tty) as sys.stdin:
        breakpoint()

def main():
    #global args  # vastly preferable to classing everything since this way we can see
    #global outfmt  # in 2 lines what is actually shared instead of stuffed into self
    args = docopt(__doc__, version=getVersion())

    profile = args['--profile']
    if profile:
        try:
            from desc.prof import profile_me
            global parse
            global serialize
            parse = profile_me(parse)
            serialize = profile_me(serialize)
        except ImportError:
            pass

    nowrite = args['--nowrite']

    infmt = args['--format']
    debug = args['--debug']

    if args['--subclass']:
        outfmt = 'scottl'
    elif args['--vanilla']:
        outfmt = 'turtle'
    elif args['--compact']:
        outfmt = 'cmpttl'
    elif args['--uncompact']:
        outfmt = 'uncmpttl'
    elif args['--jsonld']:
        outfmt = 'json-ld'
    elif args['--racket']:
        outfmt = 'rktttl'
    else:
        outfmt = args['--outfmt']

    outpath = args['--output']
    files = args['<file>']
    if not files:
        from ttlser.utils import readFromStdIn
        stdin = readFromStdIn(sys.stdin)
        if stdin is not None:
            convert(stdin, outpath, stream=True,
                    infmt=infmt, outfmt=outfmt,
                    debug=debug, profile=profile,
                    nowrite=nowrite)
        else:
            print(__doc__)
    else:
        lenfiles = len(files)
        if outpath or args['--slow'] or lenfiles == 1:
            if lenfiles == 1:
                files,  = files

            convert(files, outpath=outpath,
                    infmt=infmt, outfmt=outfmt,
                    debug=debug, profile=profile,
                    nowrite=nowrite)
        else:
            from joblib import Parallel, delayed
            nj = 9
            if lenfiles < nj:
                nj = lenfiles

            Parallel(n_jobs=nj, verbose=10)(delayed(convert)
                                            (file,
                                             infmt=infmt, outfmt=outfmt,
                                             debug=debug, profile=profile,
                                             nowrite=nowrite)
                                            for file in files)

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>from pysimm import cassandra
from pysimm import system
from os import path as osp
import numpy
import re
from matplotlib import pyplot as mplp

try:
    import pyiast
    import pandas
except ImportError:
    print('Either PyIAST or Pandas (that is PyIAST dependence) packages are not installed or cannot be found by this '
          'Python setup. \nThis example will not work properly. \nExiting...')
    exit(1)

def run(test=False):
    # Set to False if you **do not** want to recalculate pure gas adsorption isotherms
    is_simulate_loadings = False
    loadings_file = 'loadings.dat'

    # Option to draw the isotherms: it is either **'ToFile'** or **'ToScreen'** (case insensitive).
    # Any other value will be interpreted as no graphics
    graphing = 'none'

    # Gas names as they will be referred through simulations
    gas_names = ['ch4', 'co2']

    # Corresponding mole fractions of gases that will allow us to calculate their partial pressures through the Dalton's law
    mol_frac = [0.5, 0.5]

    # Calibrated previously functional forms of chemical potentials of gases for GCMC simulations as a functions of pressure
    chem_pots = [lambda x: 2.4153 * numpy.log(x) - 36.722,
                 lambda x: 2.40 * numpy.log(x) - 40.701]

    # Root directory for some data (For PySIMM examples it is )
    data_dir = osp.join('..', '09_cassandra_simulations', 'gcmc')

    # Setup of adsorbate model
    gases = []
    for gn in gas_names:
        gases.append(system.read_lammps(osp.join(data_dir, gn + '.lmps')))
        gases[-1].forcefield = 'trappe/amber'

    # Setup of adsorbent model
    frame = system.read_lammps('pim.lmps')
    frame.forcefield = 'trappe/amber'

    # Constant for loadings calculations
    molec2mmols_g = 1e+3 / frame.mass

    # Setup of the GCMC simulations
    css = cassandra.Cassandra(frame)
    sim_settings = css.read_input('run_props.inp')

    # This function in given context will calculate the loading from short GCMC simulations
    def calculate_isotherm_point(gas_name, press):
        run_fldr = osp.join(gas_name, str(press))
        idx = gas_names.index(gas_name)
        # sim_settings.update({'Run_Name':  'gcmc'})
        css.add_gcmc(species=gases[idx], is_new=True, chem_pot=chem_pots[idx](press),
                     out_folder=run_fldr, props_file='gcmc.inp', **sim_settings)
        css.run()
        full_prp = css.run_queue[0].get_prp()
        return molec2mmols_g * numpy.average(full_prp[3][int(len(2 * full_prp[3]) / 3):])

    # This function in given context will load the pre-calculated loading value from previously done GCMC simulations
    def load_isotherm_point(gas_name, press):
        with open(loadings_file, 'r') as pntr:
            stream = pntr.read()
            tmp = stream.split('\n' + gas_name)[1]
            idx = re.search('[a-zA-Z]|\Z', tmp)
            value = re.findall('\n{:}\s+\d+\.\d+'.format(press), tmp[:idx.start()])[0]
            return float(re.split('\s+', value)[-1])

    # Calculation of adsorption isotherms for pure CH4 and CO2 gases for further usage in IAST simulations.
    # This is the **MOST TIME CONSUMING STEP** in this example, if you want to skip it switch the key is_simulated to False
    # The IAST will be done using PyIAST package, thus isotherms are wrapped into the corresponding object
    gas_press = [0.1, 1, 5, 10, 25, 50]
    lk = 'Loading(mmol/g)'
    pk = 'Pressure(bar)'
    isotherms = []
    loadings = dict.fromkeys(gas_names)
    for gn in gas_names:
        loadings[gn] = []
        for p in gas_press:
            if is_simulate_loadings:
                data = calculate_isotherm_point(gn, p)
            else:
                data = load_isotherm_point(gn, p)
            loadings[gn].append(data)
        isotherms.append(pyiast.ModelIsotherm(pandas.DataFrame(zip(gas_press, loadings[gn]), columns=[pk, lk]),
                                              loading_key=lk, pressure_key=pk, model='BET', optimization_method='Powell'))

    # The PyIAST run for calculating of mixed adsorption isotherm
    # Initial guesses of adsorbed mole fractions do span broad range of values, because PyIAST might not find
    #  solution at certain values of mole fractions and through an exception
    guesses = [[a, 1 - a] for a in numpy.linspace(0.01, 0.99, 50)]
    for in_g in guesses:
        mix_loadings = []
        try:
            for p in gas_press:
                mix_loadings.append(list(pyiast.iast(p * numpy.array(mol_frac), isotherms,
                                                     verboseflag=False, adsorbed_mole_fraction_guess=in_g)))
            mix_loadings = numpy.array(mix_loadings)
            break
        except:
            print('Initial guess {:} had failed to converge'.format(in_g))
            continue

    mix_loadings = numpy.sum(mix_loadings, axis=1)
    mix_isotherm = pyiast.ModelIsotherm(pandas.DataFrame(zip(gas_press, mix_loadings), columns=[pk, lk]),
                                        loading_key=lk, pressure_key=pk, model='BET', optimization_method='L-BFGS-B')

    # Output: Graphing of constructed isotherms
    def _plot_isotherms(ax, loc_gp, loc_isoth, loc_mix_load, loc_mix_isoth):
        rng = numpy.linspace(min(loc_gp), max(loc_gp), 100)
        ax.plot(loc_gp, loadings[gas_names[0]], 'og', lw=2.5, label='{:} loadings'.format(gas_names[0].capitalize()))
        ax.plot(rng, [loc_isoth[0].loading(t) for t in rng], '--g', lw=2, label='BET fit of {:} loadings'.format(gas_names[0].capitalize()))
        ax.plot(loc_gp, loadings[gas_names[1]], 'or', lw=2.5, label='{:} loadings'.format(gas_names[1].capitalize()))
        ax.plot(rng,  [loc_isoth[1].loading(t) for t in rng], '--r', lw=2, label='BET fit of {:} loadings'.format(gas_names[1].capitalize()))
        ax.plot(loc_gp, loc_mix_load, 'ob', lw=2.5, label='1-to-1 mixture loadings')
        ax.plot(rng, [loc_mix_isoth.loading(t) for t in rng], '--b', lw=2,  label='BET fit of 1-to-1 mixture loadings')
        ax.set_xlabel('Gas pressure [bar]', fontsize=20)
        ax.set_ylabel('Loading [mmol / g]', fontsize=20)
        ax.tick_params(axis='both', labelsize=16)
        ax.grid(True)
        ax.legend(fontsize=16)
        mplp.tight_layout()

    if graphing.lower() == 'tofile':
        fig, axs = mplp.subplots(1, 1, figsize=(10, 5))
        _plot_isotherms(axs, gas_press, isotherms, mix_loadings, mix_isotherm)
        mplp.savefig('pim1_mix_adsorption.png', dpi=192)
    elif graphing.lower() == 'toscreen':
        mplp.figure()
        axs = mplp.gca()
        _plot_isotherms(axs, gas_press, isotherms, mix_loadings, mix_isotherm)
        mplp.show()

    with open('iast_loadings.dat', 'w') as pntr:
        pntr.write('{:}\t\t{:}\n'.format(pk, lk))
        pntr.write('{:}-{:} 1-to-1\n'.format(gas_names[0], gas_names[1]))
        for p, ml in zip(gas_press, mix_loadings):
            pntr.write('{:}\t\t{:}\n'.format(p, ml))

if __name__ == '__main__':
    run(False)

<commit_msg>correct capitalisation of text on plots<commit_after>from pysimm import cassandra
from pysimm import system
from os import path as osp
import numpy
import re
from matplotlib import pyplot as mplp

try:
    import pyiast
    import pandas
except ImportError:
    print('Either PyIAST or Pandas (that is PyIAST dependence) packages are not installed or cannot be found by this '
          'Python setup. \nThis example will not work properly. \nExiting...')
    exit(1)

def run(test=False):
    # Set to False if you **do not** want to recalculate pure gas adsorption isotherms
    is_simulate_loadings = False
    loadings_file = 'loadings.dat'

    # Option to draw the isotherms: it is either **'ToFile'** or **'ToScreen'** (case insensitive).
    # Any other value will be interpreted as no graphics
    graphing = 'none'

    # Gas names as they will be referred through simulations
    gas_names = ['ch4', 'co2']

    # Corresponding mole fractions of gases that will allow us to calculate their partial pressures through the Dalton's law
    mol_frac = [0.5, 0.5]

    # Calibrated previously functional forms of chemical potentials of gases for GCMC simulations as a functions of pressure
    chem_pots = [lambda x: 2.4153 * numpy.log(x) - 36.722,
                 lambda x: 2.40 * numpy.log(x) - 40.701]

    # Root directory for some data (For PySIMM examples it is )
    data_dir = osp.join('..', '09_cassandra_simulations', 'gcmc')

    # Setup of adsorbate model
    gases = []
    for gn in gas_names:
        gases.append(system.read_lammps(osp.join(data_dir, gn + '.lmps')))
        gases[-1].forcefield = 'trappe/amber'

    # Setup of adsorbent model
    frame = system.read_lammps('pim.lmps')
    frame.forcefield = 'trappe/amber'

    # Constant for loadings calculations
    molec2mmols_g = 1e+3 / frame.mass

    # Setup of the GCMC simulations
    css = cassandra.Cassandra(frame)
    sim_settings = css.read_input('run_props.inp')

    # This function in given context will calculate the loading from short GCMC simulations
    def calculate_isotherm_point(gas_name, press):
        run_fldr = osp.join(gas_name, str(press))
        idx = gas_names.index(gas_name)
        # sim_settings.update({'Run_Name':  'gcmc'})
        css.add_gcmc(species=gases[idx], is_new=True, chem_pot=chem_pots[idx](press),
                     out_folder=run_fldr, props_file='gcmc.inp', **sim_settings)
        css.run()
        full_prp = css.run_queue[0].get_prp()
        return molec2mmols_g * numpy.average(full_prp[3][int(len(2 * full_prp[3]) / 3):])

    # This function in given context will load the pre-calculated loading value from previously done GCMC simulations
    def load_isotherm_point(gas_name, press):
        with open(loadings_file, 'r') as pntr:
            stream = pntr.read()
            tmp = stream.split('\n' + gas_name)[1]
            idx = re.search('[a-zA-Z]|\Z', tmp)
            value = re.findall('\n{:}\s+\d+\.\d+'.format(press), tmp[:idx.start()])[0]
            return float(re.split('\s+', value)[-1])

    # Calculation of adsorption isotherms for pure CH4 and CO2 gases for further usage in IAST simulations.
    # This is the **MOST TIME CONSUMING STEP** in this example, if you want to skip it switch the key is_simulated to False
    # The IAST will be done using PyIAST package, thus isotherms are wrapped into the corresponding object
    gas_press = [0.1, 1, 5, 10, 25, 50]
    lk = 'Loading(mmol/g)'
    pk = 'Pressure(bar)'
    isotherms = []
    loadings = dict.fromkeys(gas_names)
    for gn in gas_names:
        loadings[gn] = []
        for p in gas_press:
            if is_simulate_loadings:
                data = calculate_isotherm_point(gn, p)
            else:
                data = load_isotherm_point(gn, p)
            loadings[gn].append(data)
        isotherms.append(pyiast.ModelIsotherm(pandas.DataFrame(zip(gas_press, loadings[gn]), columns=[pk, lk]),
                                              loading_key=lk, pressure_key=pk, model='BET', optimization_method='Powell'))

    # The PyIAST run for calculating of mixed adsorption isotherm
    # Initial guesses of adsorbed mole fractions do span broad range of values, because PyIAST might not find
    #  solution at certain values of mole fractions and through an exception
    guesses = [[a, 1 - a] for a in numpy.linspace(0.01, 0.99, 50)]
    for in_g in guesses:
        mix_loadings = []
        try:
            for p in gas_press:
                mix_loadings.append(list(pyiast.iast(p * numpy.array(mol_frac), isotherms,
                                                     verboseflag=False, adsorbed_mole_fraction_guess=in_g)))
            mix_loadings = numpy.array(mix_loadings)
            break
        except:
            print('Initial guess {:} had failed to converge'.format(in_g))
            continue

    mix_loadings = numpy.sum(mix_loadings, axis=1)
    mix_isotherm = pyiast.ModelIsotherm(pandas.DataFrame(zip(gas_press, mix_loadings), columns=[pk, lk]),
                                        loading_key=lk, pressure_key=pk, model='BET', optimization_method='L-BFGS-B')

    # Output: Graphing of constructed isotherms
    def _plot_isotherms(ax, loc_gp, loc_isoth, loc_mix_load, loc_mix_isoth):
        rng = numpy.linspace(min(loc_gp), max(loc_gp), 100)
        ax.plot(loc_gp, loadings[gas_names[0]], 'og', lw=2.5, label='{:} loadings'.format(gas_names[0].upper()))
        ax.plot(rng, [loc_isoth[0].loading(t) for t in rng], '--g', lw=2, label='BET fit of {:} loadings'.format(gas_names[0].upper()))
        ax.plot(loc_gp, loadings[gas_names[1]], 'or', lw=2.5, label='{:} loadings'.format(gas_names[1].upper()))
        ax.plot(rng,  [loc_isoth[1].loading(t) for t in rng], '--r', lw=2, label='BET fit of {:} loadings'.format(gas_names[1].upper()))
        ax.plot(loc_gp, loc_mix_load, 'ob', lw=2.5, label='1-to-1 mixture loadings')
        ax.plot(rng, [loc_mix_isoth.loading(t) for t in rng], '--b', lw=2,  label='BET fit of 1-to-1 mixture loadings')
        ax.set_xlabel('Gas pressure [bar]', fontsize=20)
        ax.set_ylabel('Loading [mmol / g]', fontsize=20)
        ax.tick_params(axis='both', labelsize=16)
        ax.grid(True)
        ax.legend(fontsize=16)
        mplp.tight_layout()

    if graphing.lower() == 'tofile':
        fig, axs = mplp.subplots(1, 1, figsize=(10, 5))
        _plot_isotherms(axs, gas_press, isotherms, mix_loadings, mix_isotherm)
        mplp.savefig('pim1_mix_adsorption.png', dpi=192)
    elif graphing.lower() == 'toscreen':
        mplp.figure()
        axs = mplp.gca()
        _plot_isotherms(axs, gas_press, isotherms, mix_loadings, mix_isotherm)
        mplp.show()

    with open('iast_loadings.dat', 'w') as pntr:
        pntr.write('{:}\t\t{:}\n'.format(pk, lk))
        pntr.write('{:}-{:} 1-to-1\n'.format(gas_names[0], gas_names[1]))
        for p, ml in zip(gas_press, mix_loadings):
            pntr.write('{:}\t\t{:}\n'.format(p, ml))

if __name__ == '__main__':
    run(False)

<|endoftext|>


<commit_before>import os
import subprocess
import sys

import pytest

thisdir = os.path.dirname(os.path.realpath(__file__))
metadata_dir = os.path.join(thisdir, "test-recipes/metadata")
fail_dir = os.path.join(thisdir, "test-recipes/fail")

def is_valid_dir(parent_dir, dirname):
    valid = os.path.isdir(os.path.join(parent_dir, dirname))
    valid &= not dirname.startswith("_")
    valid &= ('osx_is_app' != dirname or sys.platform == "darwin")
    return valid

@pytest.fixture(params=[dirname for dirname in os.listdir(metadata_dir) if is_valid_dir(metadata_dir, dirname)])
def recipe(request):
    cwd = os.getcwd()
    os.chdir(metadata_dir)
    def fin():
        os.chdir(cwd)
    request.addfinalizer(fin)
    return os.path.join(metadata_dir, request.param)

def test_recipe_builds(recipe):
    env = dict(os.environ)
    # These variables are defined solely for testing purposes,
    # so they can be checked within build scripts
    env["CONDA_TEST_VAR"] = "conda_test"
    env["CONDA_TEST_VAR_2"] = "conda_test_2"

    cmd = 'conda build --no-anaconda-upload {}'.format(recipe)
    subprocess.check_call(cmd.split(), env=env)

@pytest.mark.skipif(sys.platform=="win32",
                    reason="No windows symlinks")
def test_symlink_fail():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "symlinks"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert error.count("Error") == 6

def test_broken_conda_meta():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "conda-meta"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "Error: Untracked file(s) ('conda-meta/nope',)" in error

def test_recursive_fail():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "recursive-build"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "recursive-build2" in error

def test_jinja_typo():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "source_git_jinja2_oops"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "'GIT_DSECRIBE_TAG' is undefined" in error

<commit_msg>skip a Unix-only error on Windows<commit_after>import os
import subprocess
import sys

import pytest

thisdir = os.path.dirname(os.path.realpath(__file__))
metadata_dir = os.path.join(thisdir, "test-recipes/metadata")
fail_dir = os.path.join(thisdir, "test-recipes/fail")

def is_valid_dir(parent_dir, dirname):
    valid = os.path.isdir(os.path.join(parent_dir, dirname))
    valid &= not dirname.startswith("_")
    valid &= ('osx_is_app' != dirname or sys.platform == "darwin")
    return valid

@pytest.fixture(params=[dirname for dirname in os.listdir(metadata_dir) if is_valid_dir(metadata_dir, dirname)])
def recipe(request):
    cwd = os.getcwd()
    os.chdir(metadata_dir)
    def fin():
        os.chdir(cwd)
    request.addfinalizer(fin)
    return os.path.join(metadata_dir, request.param)

def test_recipe_builds(recipe):
    env = dict(os.environ)
    # These variables are defined solely for testing purposes,
    # so they can be checked within build scripts
    env["CONDA_TEST_VAR"] = "conda_test"
    env["CONDA_TEST_VAR_2"] = "conda_test_2"

    cmd = 'conda build --no-anaconda-upload {}'.format(recipe)
    subprocess.check_call(cmd.split(), env=env)

@pytest.mark.skipif(sys.platform=="win32",
                    reason="No windows symlinks")
def test_symlink_fail():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "symlinks"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert error.count("Error") == 6

@pytest.mark.skipif(sys.platform=="win32",
                    reason="Windows doesn't show this error")
def test_broken_conda_meta():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "conda-meta"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "Error: Untracked file(s) ('conda-meta/nope',)" in error

def test_recursive_fail():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "recursive-build"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "recursive-build2" in error

def test_jinja_typo():
    cmd = 'conda build --no-anaconda-upload {}'.format(os.path.join(fail_dir, "source_git_jinja2_oops"))
    process = subprocess.Popen(cmd.split(),
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output, error = process.communicate()
    error = error.decode('utf-8')
    assert "'GIT_DSECRIBE_TAG' is undefined" in error

<|endoftext|>


<commit_before>import datetime
import time

from django.contrib.auth.models import User
from django.core import mail
from django.core import signing
from django.core.urlresolvers import reverse
from django.test import override_settings, TestCase

from ..forms import RegistrationForm
from registration.backends.hmac.views import REGISTRATION_SALT

@override_settings(ROOT_URLCONF='registration.backends.hmac.urls',
                   ACCOUNT_ACTIVATION_DAYS=7,
                   REGISTRATION_OPEN=True)
class SigningBackendViewTests(TestCase):
    """
    Test the signed-token registration workflow.

    Running these tests successfully will require two templates to be
    created for the sending of activation emails; details on these
    templates and their contexts may be found in the documentation for
    the default backend.

    """
    def test_registration_open(self):
        """
        ``REGISTRATION_OPEN``, when ``True``, permits registration.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertEqual(200, resp.status_code)

    @override_settings(REGISTRATION_OPEN=False)
    def test_registration_closed(self):
        """
        ``REGISTRATION_OPEN``, when ``False``, disallows registration.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertRedirects(resp, reverse('registration_disallowed'))

        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})
        self.assertRedirects(resp, reverse('registration_disallowed'))

    def test_registration_get(self):
        """
        HTTP ``GET`` to the registration view uses the appropriate
        template and populates a registration form into the context.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp,
                                'registration/registration_form.html')
        self.assertTrue(isinstance(resp.context['form'],
                        RegistrationForm))

    def test_registration(self):
        """
        Registration creates a new inactive account and sends an
        activation email.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})
        self.assertRedirects(resp, reverse('registration_complete'))

        new_user = User.objects.get(username='bob')

        self.assertTrue(new_user.check_password('secret'))
        self.assertEqual(new_user.email, 'bob@example.com')

        # New user must not be active.
        self.assertFalse(new_user.is_active)

        # An activation email was sent.
        self.assertEqual(len(mail.outbox), 1)

    def test_registration_no_sites(self):
        """
        Registration still functions properly when
        ``django.contrib.sites`` is not installed; the fallback will
        be a ``RequestSite`` instance.

        """
        with self.modify_settings(INSTALLED_APPS={
            'remove': [
                'django.contrib.sites'
            ]
        }):
            resp = self.client.post(
                reverse('registration_register'),
                data={'username': 'bob',
                      'email': 'bob@example.com',
                      'password1': 'secret',
                      'password2': 'secret'})
            self.assertEqual(302, resp.status_code)

            new_user = User.objects.get(username='bob')

            self.assertTrue(new_user.check_password('secret'))
            self.assertEqual(new_user.email, 'bob@example.com')

            self.assertFalse(new_user.is_active)

            self.assertEqual(len(mail.outbox), 1)

    def test_registration_failure(self):
        """
        Registering with invalid data fails.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'notsecret'})
        self.assertEqual(200, resp.status_code)
        self.assertFalse(resp.context['form'].is_valid())
        self.assertEqual(0, len(mail.outbox))

    def test_activation(self):
        """
        Activation of an account functions properly.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})

        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('bob')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )
        self.assertRedirects(resp, reverse('registration_activation_complete'))

    def test_repeat_activation(self):
        """
        Once activated, attempting to re-activate an account (even
        with a valid key) does nothing.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})

        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('bob')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )
        # First activation redirects to success.
        self.assertRedirects(resp, reverse('registration_activation_complete'))

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )

        # Second activation fails.
        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')

    def test_activation_expired(self):
        """
        An expired account can't be activated.

        """
        self.client.post(reverse('registration_register'),
                         data={'username': 'bob',
                               'email': 'bob@example.com',
                               'password1': 'secret',
                               'password2': 'secret'})

        # We need to create an activation key valid for the username,
        # but with a timestamp > 7 days in the past. This requires
        # monkeypatching time.time() to return that timestamp, since
        # TimestampSigner uses time.time().
        #
        # On Python 3.3+ this is much easier because of the
        # timestamp() method of datetime objects, but since
        # django-registration has to run on Python 2.7, we manually
        # calculate it using a timedelta between the signup date and
        # the UNIX epoch.
        user = User.objects.get(username='bob')
        joined_timestamp = (
            user.date_joined.date() - datetime.date(1970, 1, 1)
        ).total_seconds()
        expired_timestamp = joined_timestamp - (7 * 86400) - 1
        _old_time = time.time
        time.time = lambda: expired_timestamp

        try:
            signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
            activation_key = signer.sign('bob')
        finally:
            time.time = _old_time

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key}))

        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')

    def test_nonexistent_activation(self):
        """
        A nonexistent username in an activation key will fail to
        activate.

        """
        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('parrot')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key}))

        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')
<commit_msg>Avoid hard-coded magic number.<commit_after>import datetime
import time

from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.core import signing
from django.core.urlresolvers import reverse
from django.test import override_settings, TestCase

from ..forms import RegistrationForm
from registration.backends.hmac.views import REGISTRATION_SALT

@override_settings(ROOT_URLCONF='registration.backends.hmac.urls',
                   ACCOUNT_ACTIVATION_DAYS=7,
                   REGISTRATION_OPEN=True)
class SigningBackendViewTests(TestCase):
    """
    Test the signed-token registration workflow.

    Running these tests successfully will require two templates to be
    created for the sending of activation emails; details on these
    templates and their contexts may be found in the documentation for
    the default backend.

    """
    def test_registration_open(self):
        """
        ``REGISTRATION_OPEN``, when ``True``, permits registration.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertEqual(200, resp.status_code)

    @override_settings(REGISTRATION_OPEN=False)
    def test_registration_closed(self):
        """
        ``REGISTRATION_OPEN``, when ``False``, disallows registration.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertRedirects(resp, reverse('registration_disallowed'))

        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})
        self.assertRedirects(resp, reverse('registration_disallowed'))

    def test_registration_get(self):
        """
        HTTP ``GET`` to the registration view uses the appropriate
        template and populates a registration form into the context.

        """
        resp = self.client.get(reverse('registration_register'))
        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp,
                                'registration/registration_form.html')
        self.assertTrue(isinstance(resp.context['form'],
                        RegistrationForm))

    def test_registration(self):
        """
        Registration creates a new inactive account and sends an
        activation email.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})
        self.assertRedirects(resp, reverse('registration_complete'))

        new_user = User.objects.get(username='bob')

        self.assertTrue(new_user.check_password('secret'))
        self.assertEqual(new_user.email, 'bob@example.com')

        # New user must not be active.
        self.assertFalse(new_user.is_active)

        # An activation email was sent.
        self.assertEqual(len(mail.outbox), 1)

    def test_registration_no_sites(self):
        """
        Registration still functions properly when
        ``django.contrib.sites`` is not installed; the fallback will
        be a ``RequestSite`` instance.

        """
        with self.modify_settings(INSTALLED_APPS={
            'remove': [
                'django.contrib.sites'
            ]
        }):
            resp = self.client.post(
                reverse('registration_register'),
                data={'username': 'bob',
                      'email': 'bob@example.com',
                      'password1': 'secret',
                      'password2': 'secret'})
            self.assertEqual(302, resp.status_code)

            new_user = User.objects.get(username='bob')

            self.assertTrue(new_user.check_password('secret'))
            self.assertEqual(new_user.email, 'bob@example.com')

            self.assertFalse(new_user.is_active)

            self.assertEqual(len(mail.outbox), 1)

    def test_registration_failure(self):
        """
        Registering with invalid data fails.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'notsecret'})
        self.assertEqual(200, resp.status_code)
        self.assertFalse(resp.context['form'].is_valid())
        self.assertEqual(0, len(mail.outbox))

    def test_activation(self):
        """
        Activation of an account functions properly.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})

        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('bob')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )
        self.assertRedirects(resp, reverse('registration_activation_complete'))

    def test_repeat_activation(self):
        """
        Once activated, attempting to re-activate an account (even
        with a valid key) does nothing.

        """
        resp = self.client.post(reverse('registration_register'),
                                data={'username': 'bob',
                                      'email': 'bob@example.com',
                                      'password1': 'secret',
                                      'password2': 'secret'})

        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('bob')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )
        # First activation redirects to success.
        self.assertRedirects(resp, reverse('registration_activation_complete'))

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key})
        )

        # Second activation fails.
        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')

    def test_activation_expired(self):
        """
        An expired account can't be activated.

        """
        self.client.post(reverse('registration_register'),
                         data={'username': 'bob',
                               'email': 'bob@example.com',
                               'password1': 'secret',
                               'password2': 'secret'})

        # We need to create an activation key valid for the username,
        # but with a timestamp > 7 days in the past. This requires
        # monkeypatching time.time() to return that timestamp, since
        # TimestampSigner uses time.time().
        #
        # On Python 3.3+ this is much easier because of the
        # timestamp() method of datetime objects, but since
        # django-registration has to run on Python 2.7, we manually
        # calculate it using a timedelta between the signup date and
        # the UNIX epoch.
        user = User.objects.get(username='bob')
        joined_timestamp = (
            user.date_joined.date() - datetime.date(1970, 1, 1)
        ).total_seconds()
        expired_timestamp = (
            joined_timestamp - (settings.ACCOUNT_ACTIVATION_DAYS * 86400) - 1
        )
        _old_time = time.time
        time.time = lambda: expired_timestamp

        try:
            signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
            activation_key = signer.sign('bob')
        finally:
            time.time = _old_time

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key}))

        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')

    def test_nonexistent_activation(self):
        """
        A nonexistent username in an activation key will fail to
        activate.

        """
        signer = signing.TimestampSigner(salt=REGISTRATION_SALT)
        activation_key = signer.sign('parrot')

        resp = self.client.get(reverse(
            'registration_activate',
            args=(),
            kwargs={'activation_key': activation_key}))

        self.assertEqual(200, resp.status_code)
        self.assertTemplateUsed(resp, 'registration/activate.html')
<|endoftext|>