<commit_before>from __future__ import unicode_literals

import six
from six.moves import xrange

from scrapi.base import XMLHarvester
from scrapi.base.schemas import DOESCHEMA
from scrapi.linter.document import RawDocument
from scrapi.base.helpers import updated_schema, build_properties, single_result

RAW_DOC = {
    'doc': b'{}',
    'docID': 'someID',
    'timestamps': {
        'harvestFinished': '2012-11-30T17:05:48+00:00',
        'harvestStarted': '2012-11-30T17:05:48+00:00',
        'harvestTaskCreated': '2012-11-30T17:05:48+00:00'
    },
    'filetype': 'json',
    'source': 'test'
}

NORMALIZED_DOC = {
    'title': 'No',
    'contributors': [{'name': ''}],
    'source': 'test',
    'providerUpdatedDateTime': '2014-04-04T00:00:00',
    'uris': {
        'canonicalUri': 'http://example.com/direct'
    }
}

RECORD = {
    'title': 'Using Table Stable Carbon in Gold and STAR Isotopes',
    'contributors': [
        {
            'name': 'DEVON Get The Tables DUDLEY',
            'givenName': 'DEVON',
            'additionalName': 'Get The Tables',
            'familyName': 'DUDLEY',
            'email': 'dudley.boyz@email.uni.edu',
            'sameAs': ['http://example.com/me']
        }
    ],
    'uris': {
        'canonicalUri': 'http://www.plosone.org/article'
    },
    'otherProperties': [{
        'name': 'figures',
        'properties': {
            'figures': 'http://www.plosone.org/article/image.png',
        }
        }, {
        'name': 'type',
        'properties': {
            'type': 'text',
            'yep': 'A property'
        }
    }],
    'description': 'This study seeks to understand how humans impact\
            the dietary patterns of eight free-ranging vervet monkey\
            (Chlorocebus pygerythrus) groups in South Africa using stable\
            isotope analysis.',
    'providerUpdatedDateTime': '2015-02-23T00:00:00',
    'shareProperties': {
        'source': 'test'
    }
}

TEST_SCHEMA = updated_schema(DOESCHEMA, {
    "title": ("//dc:title/node()", lambda x: "Title overwritten"),
    "otherProperties": build_properties(
        ("title1", ("//dc:title/node()", single_result)),
        ("title2", ("//dc:title/node()", lambda x: single_result(x).lower())),
        ("title3", ("//dc:title/node()", "//dc:title/node()", lambda x, y: single_result(x) + single_result(y).lower()))
    )
})

TEST_NAMESPACES = {
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dcq': 'http://purl.org/dc/terms/'
}

TEST_XML_DOC = b'''
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcq="http://purl.org/dc/terms/">
        <records count="97" morepages="true" start="1" end="10">
            <record rownumber="1">
                <dc:title>Test</dc:title>
                <dc:creator>
                Raveh-Sadka, Tali; Thomas, Brian C; Singh, Andrea; Firek, Brian; Brooks,
                Brandon; Castelle, Cindy J; Sharon, Itai; Baker, Robyn; Good, Misty; Morowitz,
                Michael J; Banfield, Jillian F
                </dc:creator>
                <dc:subject/>
                <dc:subjectRelated/>
                <dc:description/>
                <dcq:publisher>
                    eLife Sciences Publications, Ltd.
                </dcq:publisher>
                <dcq:publisherAvailability/>
                <dcq:publisherResearch>
                    None
                </dcq:publisherResearch>
                <dcq:publisherSponsor>
                    USDOE
                </dcq:publisherSponsor>
                <dcq:publisherCountry>
                    Country unknown/Code not available
                </dcq:publisherCountry>
                <dc:date>
                    2015-03-03
                </dc:date>
                <dc:language>
                    English
                </dc:language>
                <dc:type>
                    Journal Article
                </dc:type>
                <dcq:typeQualifier/>
                <dc:relation>
                    Journal Name: eLife; Journal Volume: 4
                </dc:relation>
                <dc:coverage/>
                <dc:format>
                    Medium: X
                </dc:format>
                <dc:identifier>
                    OSTI ID: 1171761, Legacy ID: OSTI ID: 1171761
                </dc:identifier>
                <dc:identifierReport>
                    None
                </dc:identifierReport>
                <dcq:identifierDOEcontract>
                    5R01AI092531; Long term fellowship; SC0004918; ER65561; APSF-2012-10-05
                </dcq:identifierDOEcontract>
                <dc:identifierOther>Journal ID: ISSN 2050-084X</dc:identifierOther>
                <dc:doi>10.7554/eLife.05477</dc:doi><dc:rights/>
                <dc:dateEntry>2015-03-05</dc:dateEntry>
                <dc:dateAdded>2015-03-05</dc:dateAdded>
                <dc:ostiId>1171761</dc:ostiId>
                <dcq:identifier-purl type=""/>
                <dcq:identifier-citation>
                    http://www.osti.gov/pages/biblio/1171761
                </dcq:identifier-citation>
            </record>
        </records>
    </rdf:RDF>
'''

TEST_OAI_DOC = b'''
    <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>2015-03-30T18:31:29Z</responseDate>
    <request verb="ListRecords" metadataPrefix="oai_dc" from="2014-09-26T00:00:00Z">http://repository.stcloudstate.edu/do/oai/</request>
    <ListRecords>
    <record>
    <header>
    <identifier>oai:digitalcommons.calpoly.edu:aged_rpt-1085</identifier>
    <datestamp>2014-10-07T00:30:57Z</datestamp>
    <setSpec>publication:aged_rpt</setSpec>
    <setSpec>publication:students</setSpec>
    </header>
    <metadata>
    <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:title>Test</dc:title>
    <dc:creator>Mills, Donald W, Jr.</dc:creator>
    <dc:description>
    This internship report consists of two parts. The first section addresses the Quality Criteria in Agricultural Education for the program of agricultural instruction at Arvin High School. The second section explains the steps in remodeling the agriculture classroom and agriculture mechanics shop at Arvin High School. The remodeling of the facility was accomplished to address the occupational needs for students entering the work force immediately after high school. It was also designed to prepare students for opportunities in higher education.
    </dc:description>
    <dc:date>2014-05-01T07:00:00Z</dc:date>
    <dc:type>text</dc:type>
    <dc:format>application/pdf</dc:format>
    <dc:identifier>http://digitalcommons.calpoly.edu/aged_rpt/64</dc:identifier>
    <dc:identifier>
    http://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?article=1085&context=aged_rpt
    </dc:identifier>
    <dc:source>
    Graduate Internship Reports in Agricultural Education
    </dc:source>
    <dc:publisher>DigitalCommons@CalPoly</dc:publisher>
    </oai_dc:dc>
    </metadata>
    </record>
    </ListRecords>
    </OAI-PMH>
'''

class TestHarvester(XMLHarvester):
    long_name = 'TEST'
    short_name = 'test'
    url = 'TEST'
    schema = TEST_SCHEMA
    namespaces = TEST_NAMESPACES

    def harvest(self, days_back=1):
        return [RawDocument({
            'doc': TEST_XML_DOC,
            'source': 'test',
            'filetype': 'XML',
            'docID': "1"
        }) for _ in xrange(days_back)]
<commit_msg>change rawdoc to postgres one<commit_after>from __future__ import unicode_literals

import six
from six.moves import xrange

from scrapi.base import XMLHarvester
from scrapi.base.schemas import DOESCHEMA
from scrapi.linter.document import RawDocument
from scrapi.base.helpers import updated_schema, build_properties, single_result

RAW_DOC = {
    'doc': b'{}',
    'docID': 'someID',
    'timestamps': {
        'harvestFinished': '2012-11-30T17:05:48+00:00',
        'harvestStarted': '2012-11-30T17:05:48+00:00',
        'harvestTaskCreated': '2012-11-30T17:05:48+00:00'
    },
    'filetype': 'json',
    'source': 'test'
}

POSTGRES_RAW_DOC = {
    'doc': '{}',
    'docID': 'someID',
    'timestamps': {
        'harvestFinished': '2012-11-30T17:05:48+00:00',
        'harvestStarted': '2012-11-30T17:05:48+00:00',
        'harvestTaskCreated': '2012-11-30T17:05:48+00:00'
    },
    'filetype': 'json',
    'source': 'test'
}

NORMALIZED_DOC = {
    'title': 'No',
    'contributors': [{'name': ''}],
    'source': 'test',
    'providerUpdatedDateTime': '2014-04-04T00:00:00',
    'uris': {
        'canonicalUri': 'http://example.com/direct'
    }
}

RECORD = {
    'title': 'Using Table Stable Carbon in Gold and STAR Isotopes',
    'contributors': [
        {
            'name': 'DEVON Get The Tables DUDLEY',
            'givenName': 'DEVON',
            'additionalName': 'Get The Tables',
            'familyName': 'DUDLEY',
            'email': 'dudley.boyz@email.uni.edu',
            'sameAs': ['http://example.com/me']
        }
    ],
    'uris': {
        'canonicalUri': 'http://www.plosone.org/article'
    },
    'otherProperties': [{
        'name': 'figures',
        'properties': {
            'figures': 'http://www.plosone.org/article/image.png',
        }
        }, {
        'name': 'type',
        'properties': {
            'type': 'text',
            'yep': 'A property'
        }
    }],
    'description': 'This study seeks to understand how humans impact\
            the dietary patterns of eight free-ranging vervet monkey\
            (Chlorocebus pygerythrus) groups in South Africa using stable\
            isotope analysis.',
    'providerUpdatedDateTime': '2015-02-23T00:00:00',
    'shareProperties': {
        'source': 'test'
    }
}

TEST_SCHEMA = updated_schema(DOESCHEMA, {
    "title": ("//dc:title/node()", lambda x: "Title overwritten"),
    "otherProperties": build_properties(
        ("title1", ("//dc:title/node()", single_result)),
        ("title2", ("//dc:title/node()", lambda x: single_result(x).lower())),
        ("title3", ("//dc:title/node()", "//dc:title/node()", lambda x, y: single_result(x) + single_result(y).lower()))
    )
})

TEST_NAMESPACES = {
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'dcq': 'http://purl.org/dc/terms/'
}

TEST_XML_DOC = b'''
    <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcq="http://purl.org/dc/terms/">
        <records count="97" morepages="true" start="1" end="10">
            <record rownumber="1">
                <dc:title>Test</dc:title>
                <dc:creator>
                Raveh-Sadka, Tali; Thomas, Brian C; Singh, Andrea; Firek, Brian; Brooks,
                Brandon; Castelle, Cindy J; Sharon, Itai; Baker, Robyn; Good, Misty; Morowitz,
                Michael J; Banfield, Jillian F
                </dc:creator>
                <dc:subject/>
                <dc:subjectRelated/>
                <dc:description/>
                <dcq:publisher>
                    eLife Sciences Publications, Ltd.
                </dcq:publisher>
                <dcq:publisherAvailability/>
                <dcq:publisherResearch>
                    None
                </dcq:publisherResearch>
                <dcq:publisherSponsor>
                    USDOE
                </dcq:publisherSponsor>
                <dcq:publisherCountry>
                    Country unknown/Code not available
                </dcq:publisherCountry>
                <dc:date>
                    2015-03-03
                </dc:date>
                <dc:language>
                    English
                </dc:language>
                <dc:type>
                    Journal Article
                </dc:type>
                <dcq:typeQualifier/>
                <dc:relation>
                    Journal Name: eLife; Journal Volume: 4
                </dc:relation>
                <dc:coverage/>
                <dc:format>
                    Medium: X
                </dc:format>
                <dc:identifier>
                    OSTI ID: 1171761, Legacy ID: OSTI ID: 1171761
                </dc:identifier>
                <dc:identifierReport>
                    None
                </dc:identifierReport>
                <dcq:identifierDOEcontract>
                    5R01AI092531; Long term fellowship; SC0004918; ER65561; APSF-2012-10-05
                </dcq:identifierDOEcontract>
                <dc:identifierOther>Journal ID: ISSN 2050-084X</dc:identifierOther>
                <dc:doi>10.7554/eLife.05477</dc:doi><dc:rights/>
                <dc:dateEntry>2015-03-05</dc:dateEntry>
                <dc:dateAdded>2015-03-05</dc:dateAdded>
                <dc:ostiId>1171761</dc:ostiId>
                <dcq:identifier-purl type=""/>
                <dcq:identifier-citation>
                    http://www.osti.gov/pages/biblio/1171761
                </dcq:identifier-citation>
            </record>
        </records>
    </rdf:RDF>
'''

TEST_OAI_DOC = b'''
    <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
    <responseDate>2015-03-30T18:31:29Z</responseDate>
    <request verb="ListRecords" metadataPrefix="oai_dc" from="2014-09-26T00:00:00Z">http://repository.stcloudstate.edu/do/oai/</request>
    <ListRecords>
    <record>
    <header>
    <identifier>oai:digitalcommons.calpoly.edu:aged_rpt-1085</identifier>
    <datestamp>2014-10-07T00:30:57Z</datestamp>
    <setSpec>publication:aged_rpt</setSpec>
    <setSpec>publication:students</setSpec>
    </header>
    <metadata>
    <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
    <dc:title>Test</dc:title>
    <dc:creator>Mills, Donald W, Jr.</dc:creator>
    <dc:description>
    This internship report consists of two parts. The first section addresses the Quality Criteria in Agricultural Education for the program of agricultural instruction at Arvin High School. The second section explains the steps in remodeling the agriculture classroom and agriculture mechanics shop at Arvin High School. The remodeling of the facility was accomplished to address the occupational needs for students entering the work force immediately after high school. It was also designed to prepare students for opportunities in higher education.
    </dc:description>
    <dc:date>2014-05-01T07:00:00Z</dc:date>
    <dc:type>text</dc:type>
    <dc:format>application/pdf</dc:format>
    <dc:identifier>http://digitalcommons.calpoly.edu/aged_rpt/64</dc:identifier>
    <dc:identifier>
    http://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?article=1085&context=aged_rpt
    </dc:identifier>
    <dc:source>
    Graduate Internship Reports in Agricultural Education
    </dc:source>
    <dc:publisher>DigitalCommons@CalPoly</dc:publisher>
    </oai_dc:dc>
    </metadata>
    </record>
    </ListRecords>
    </OAI-PMH>
'''

class TestHarvester(XMLHarvester):
    long_name = 'TEST'
    short_name = 'test'
    url = 'TEST'
    schema = TEST_SCHEMA
    namespaces = TEST_NAMESPACES

    def harvest(self, days_back=1):
        return [RawDocument({
            'doc': TEST_XML_DOC,
            'source': 'test',
            'filetype': 'XML',
            'docID': "1"
        }) for _ in xrange(days_back)]
<|endoftext|>


<commit_before>import bottle
import threading
import socket
import time as _time
import sys
import nose.tools

class Server(bottle.WSGIRefServer):
    def run(self, handler): # pragma: no cover
        from wsgiref.simple_server import make_server, WSGIRequestHandler
        if self.quiet:
            class QuietHandler(WSGIRequestHandler):
                def log_request(*args, **kw): pass
            self.options['handler_class'] = QuietHandler
        self.srv = make_server(self.host, self.port, handler, **self.options)
        self.srv.serve_forever(poll_interval=0.1)

def start_bottle_server(app, port, **kwargs):
    server_thread = ServerThread(app, port, kwargs)
    server_thread.daemon = True
    server_thread.start()

    ok = False
    for i in range(10):
        try:
            conn = socket.create_connection(('127.0.0.1', port), 0.1)
        except socket.error as e:
            _time.sleep(0.1)
        else:
            conn.close()
            ok = True
            break
    if not ok:
        import warnings
        warnings.warn('Server did not start after 1 second')

    return server_thread.server

class ServerThread(threading.Thread):
    def __init__(self, app, port, server_kwargs):
        threading.Thread.__init__(self)
        self.app = app
        self.port = port
        self.server_kwargs = server_kwargs
        self.server = Server(host='localhost', port=self.port, **self.server_kwargs)

    def run(self):
        bottle.run(self.app, server=self.server)

def app_runner_setup(module_name, app, port):
    app_runner_setup_multiple(module_name, [[app, port]])

def app_runner_setup_multiple(module_name, specs):
    # take module_name rather than module for convenience, to avoid
    # requiring all tests to import sys
    module = sys.modules[module_name]

    def setup(self):
        self.servers = []
        for spec in specs:
            if len(spec) == 2:
                app, port = spec
                kwargs = {}
            else:
                app, port, kwargs = spec
            self.servers.append(start_bottle_server(app, port, **kwargs))

    def teardown(self):
        for server in self.servers:
            server.srv.shutdown()

    assert not hasattr(module, 'setup_module')
    assert not hasattr(module, 'teardown_module')

    module.setup_module = setup
    module.teardown_module = teardown

# http://code.activestate.com/recipes/106033-deep-list-to-convert-a-nested-tuple-of-tuples/
def listit(t):
    return list(map(listit, t)) if isinstance(t, (list, tuple)) else t

# XXX unused and untested currently
if sys.version_info[0] >= 2 or sys.version_info[0] == 2 and sys.version_info[1] >= 7:
    assert_raises = nose.tools.assert_raises
else:
    def assert_raises(exception, callable=None, *args, **kwargs):
        if callable is None:
            return webracer.case.AssertRaisesContextManager(exception)
        else:
            return nose.tools.assert_raises(exception, callable, *args, **kwargs)

def add_dicts(one, two):
    out = dict(one)
    for key in two:
        out[key] = two[key]
    return out
<commit_msg>Try quietly running servers<commit_after>import bottle
import threading
import socket
import time as _time
import sys
import nose.tools

class Server(bottle.WSGIRefServer):
    def run(self, handler): # pragma: no cover
        from wsgiref.simple_server import make_server, WSGIRequestHandler
        if self.quiet:
            base = self.options.get('handler_class', WSGIRequestHandler)
            class QuietHandler(base):
                def log_request(*args, **kw): pass
            self.options['handler_class'] = QuietHandler
        self.srv = make_server(self.host, self.port, handler, **self.options)
        self.srv.serve_forever(poll_interval=0.1)

def start_bottle_server(app, port, **kwargs):
    server_thread = ServerThread(app, port, kwargs)
    server_thread.daemon = True
    server_thread.start()

    ok = False
    for i in range(10):
        try:
            conn = socket.create_connection(('127.0.0.1', port), 0.1)
        except socket.error as e:
            _time.sleep(0.1)
        else:
            conn.close()
            ok = True
            break
    if not ok:
        import warnings
        warnings.warn('Server did not start after 1 second')

    return server_thread.server

class ServerThread(threading.Thread):
    def __init__(self, app, port, server_kwargs):
        threading.Thread.__init__(self)
        self.app = app
        self.port = port
        self.server_kwargs = server_kwargs
        self.server = Server(host='localhost', port=self.port, **self.server_kwargs)

    def run(self):
        bottle.run(self.app, server=self.server, quiet=True)

def app_runner_setup(module_name, app, port):
    app_runner_setup_multiple(module_name, [[app, port]])

def app_runner_setup_multiple(module_name, specs):
    # take module_name rather than module for convenience, to avoid
    # requiring all tests to import sys
    module = sys.modules[module_name]

    def setup(self):
        self.servers = []
        for spec in specs:
            if len(spec) == 2:
                app, port = spec
                kwargs = {}
            else:
                app, port, kwargs = spec
            self.servers.append(start_bottle_server(app, port, **kwargs))

    def teardown(self):
        for server in self.servers:
            server.srv.shutdown()

    assert not hasattr(module, 'setup_module')
    assert not hasattr(module, 'teardown_module')

    module.setup_module = setup
    module.teardown_module = teardown

# http://code.activestate.com/recipes/106033-deep-list-to-convert-a-nested-tuple-of-tuples/
def listit(t):
    return list(map(listit, t)) if isinstance(t, (list, tuple)) else t

# XXX unused and untested currently
if sys.version_info[0] >= 2 or sys.version_info[0] == 2 and sys.version_info[1] >= 7:
    assert_raises = nose.tools.assert_raises
else:
    def assert_raises(exception, callable=None, *args, **kwargs):
        if callable is None:
            return webracer.case.AssertRaisesContextManager(exception)
        else:
            return nose.tools.assert_raises(exception, callable, *args, **kwargs)

def add_dicts(one, two):
    out = dict(one)
    for key in two:
        out[key] = two[key]
    return out
<|endoftext|>


<commit_before>#textparsing.py
#Elise Meike September 2017
#experiments with nltk

import nltk
#nltk.download()
#from nltk.book import text6

#using already downloaded text (code modified from technaverbascripta.wordpress.com)
f = open('PridePrejudice.txt', mode = 'r')
text = f.read()
nltk_text = text.split()
final_text = nltk.Text(nltk_text)

#divides the number of unique words by the total word count of the text (code modified from nltk.org)
def lexical_diversity(text):
    return len(set(text)) / len(text)

def percentage(count, total):
    return 100 * count / total

print("Examining " + str(final_text))
print("The text is " + str(len(final_text)) + " characters long.")
print("Lexical diversity = " + str(lexical_diversity(final_text)))
print(str(percentage(final_text.count('the'), len(final_text))) + "% of the text is 'the'")

#finding the occurences of specific words in the text
words = {"marry", "Elizabeth", "Darcy"}

for word in words:
	print("'" + word  + "'" + " occurs " + str(final_text.count(word)) + " times.")

#concord = text6.concordance(word)
#print(word + " occurs " str(len(str(concord))) "in the following passages of \n" + text6)

<commit_msg>Fixing Formatting<commit_after>#textparsing.py
#Elise Meike September 2017
#experiments with nltk

import nltk
#nltk.download()
#from nltk.book import text6

#divides the number of unique words by the total word count of the text (code modified from nltk.org)
def lexical_diversity(text):
    return len(set(text)) / len(text)

def percentage(count, total):
    return 100 * count / total

#using already downloaded text (code modified from technaverbascripta.wordpress.com)
f = open('PridePrejudice.txt', mode = 'r')
text = f.read()
nltk_text = text.split()
final_text = nltk.Text(nltk_text)

print("Examining " + str(final_text))
print("The text is {:,} characters long.".format(len(final_text)))
print("Lexical diversity = {:,}".format(lexical_diversity(final_text)))
print("{:03.2f} % of the text is 'the'".format(percentage(final_text.count('the'), len(final_text))))

#finding the occurences of specific words in the text
words = {"marry", "Elizabeth", "Darcy"}

for word in words:
	print("'" + word  + "'" + " occurs " + str(final_text.count(word)) + " times.")

#concord = text6.concordance(word)
#print(word + " occurs " str(len(str(concord))) "in the following passages of \n" + text6)
<|endoftext|>


<commit_before>"""
TurboGears 2 is a reinvention of TurboGears and a return to TurboGears' roots.

TurboGears is a project that is built upon a foundation of reuse and building up.
In retrospect, much of the code that was home grown in the TurboGears project
should have been released as independent projects that integrate with TurboGears.
This would have allowed better growth of those pieces.

TurboGears 0.5 release was just a few hundred lines of Python code that
built upon thousands of lines of other libraries.

Now TurboGears2 is built on Pylons and allows you to use
your favorite Python components and libraries::

    * Models: SQLAlchemy, SQLObject, plain old DB-API
    * Template engines: Genshi, Kid, Myghty, Mako, Cheetah, or whatever you like - using Buffet
    * URL Dispatching: Object dispatch, Routes, or plug in your favorite
    * AJAX: ToscaWidgets, WebHelpers based on Prototype or Mochikit, Dojo, JQuery, Ext & more

The zen of TurboGears is::

    Keep simple things simple and complex things possible
    Give god defaults everywhere and allow choices where they matter
    Don't pretend that there's one obvious way, when there isn't

TurboGears 2 is to Pylons as Ubuntu is to Debian Debian.

We're focused on user experience, and creating a user friendly environment.
We make choices, setup sane defaults, and build on top of a powerfull, flexible core.
Pylons provide that core, and focuses on maximizing flexibility, stability.

we don't intend to hide that power and flexibility from advanced users, we just want
them to be able to get up and running quickly, and have a well lit development path
to walk.

"""
from controllers import TurboGearsController

from pylons.decorators import expose, new_validate as validate
from pylons.wsgiapp import BaseApp
from pylons import c as context
from pylons import g as app_globals
from pylons import request

class TurboGearsApplication(BaseApp):
    def __init__(self, root, **kwargs):
        BaseApp.__init__(self, **kwargs)
        self.root = root

    def __call__(self, environ, start_response):
        environ['pylons.routes_dict'] = {}
        self.setup_app_env(environ, start_response)
        return self.root(environ, start_response)

__all__ = [
    'expose', 'validate', 'TurboGearsController', 'context', 'app_globals',
    'request', 'TurboGearsApplication'
]<commit_msg>Aliases pylons.session to tg.session.<commit_after>"""
TurboGears 2 is a reinvention of TurboGears and a return to TurboGears' roots.

TurboGears is a project that is built upon a foundation of reuse and building up.
In retrospect, much of the code that was home grown in the TurboGears project
should have been released as independent projects that integrate with TurboGears.
This would have allowed better growth of those pieces.

TurboGears 0.5 release was just a few hundred lines of Python code that
built upon thousands of lines of other libraries.

Now TurboGears2 is built on Pylons and allows you to use
your favorite Python components and libraries::

    * Models: SQLAlchemy, SQLObject, plain old DB-API
    * Template engines: Genshi, Kid, Myghty, Mako, Cheetah, or whatever you like - using Buffet
    * URL Dispatching: Object dispatch, Routes, or plug in your favorite
    * AJAX: ToscaWidgets, WebHelpers based on Prototype or Mochikit, Dojo, JQuery, Ext & more

The zen of TurboGears is::

    Keep simple things simple and complex things possible
    Give god defaults everywhere and allow choices where they matter
    Don't pretend that there's one obvious way, when there isn't

TurboGears 2 is to Pylons as Ubuntu is to Debian Debian.

We're focused on user experience, and creating a user friendly environment.
We make choices, setup sane defaults, and build on top of a powerfull, flexible core.
Pylons provide that core, and focuses on maximizing flexibility, stability.

we don't intend to hide that power and flexibility from advanced users, we just want
them to be able to get up and running quickly, and have a well lit development path
to walk.

"""
from controllers import TurboGearsController

from pylons.decorators import expose, new_validate as validate
from pylons.wsgiapp import BaseApp
from pylons import c as context
from pylons import g as app_globals
from pylons import request
from pylons import session

class TurboGearsApplication(BaseApp):
    def __init__(self, root, **kwargs):
        BaseApp.__init__(self, **kwargs)
        self.root = root

    def __call__(self, environ, start_response):
        environ['pylons.routes_dict'] = {}
        self.setup_app_env(environ, start_response)
        return self.root(environ, start_response)

__all__ = [
    'expose', 'validate', 'TurboGearsController', 'context', 'app_globals',
    'request', 'TurboGearsApplication', 'session'
]<|endoftext|>


<commit_before>#!/usr/bin/env python3 -tt

import kerberos
from pyasn1.codec.ber import encoder, decoder
import glob

def crack(wordlist, enctickets):
	toremove = []
	while enctickets:
		word = wordlist.get()
		#print "trying %s" % word
		for et in enctickets:
			kdata, nonce = kerberos.decrypt(kerberos.ntlmhash(word), 2, et[0])
			if kdata:
				print('found password for ticket %i: %s  File: %s' % (et[1], word, et[2]))
				toremove.append(et)
		for et in toremove:
			try:
				enctickets.remove(et)
			except:
				return
			if not enctickets:
				return

print('''

    USE HASHCAT, IT'S HELLA FASTER!!

''')

import argparse
import sys

parser = argparse.ArgumentParser(description='Read kerberos ticket then modify it')
parser.add_argument('wordlistfile', action='store',
				metavar='dictionary.txt', type=argparse.FileType('rb'), # windows closes it in thread
				help='the word list to use with password cracking')
parser.add_argument('files', nargs='+', metavar='file.kirbi', type=str,
				help='File name to crack. Use asterisk \'*\' for many files.\n Files are exported with mimikatz or from extracttgsrepfrompcap.py')

args = parser.parse_args()

# load the tickets
enctickets = []
i = 0
for path in args.files:
	for filename in glob.glob(path):
		et = kerberos.extract_ticket_from_kirbi(filename)
		enctickets.append((et, i, filename))

if len(enctickets):
	print("Cracking %i tickets..." % len(enctickets))
else:
	print("No tickets found")
	sys.exit()

# load wordlist
for w in args.wordlistfile:
	word = w.decode('utf-8').strip()
	hash = kerberos.ntlmhash(word)
	for et in enctickets:
		kdata, nonce = kerberos.decrypt(hash, 2, et[0])
		if kdata:
			print('found password for ticket %i: %s  File: %s' % (et[1], word, et[2]))
			enctickets.remove(et)
			if len(enctickets) == 0:
				print('Successfully cracked all tickets')
				sys.exit()

if len(enctickets):
	print("Unable to crack %i tickets" % len(enctickets))
<commit_msg>Fix crashing with invalid UTF8 characters<commit_after>#!/usr/bin/env python3 -tt

import kerberos
from pyasn1.codec.ber import encoder, decoder
import glob

def crack(wordlist, enctickets):
	toremove = []
	while enctickets:
		word = wordlist.get()
		#print "trying %s" % word
		for et in enctickets:
			kdata, nonce = kerberos.decrypt(kerberos.ntlmhash(word), 2, et[0])
			if kdata:
				print('found password for ticket %i: %s  File: %s' % (et[1], word, et[2]))
				toremove.append(et)
		for et in toremove:
			try:
				enctickets.remove(et)
			except:
				return
			if not enctickets:
				return

print('''

    USE HASHCAT, IT'S HELLA FASTER!!

''')

import argparse
import sys

parser = argparse.ArgumentParser(description='Read kerberos ticket then modify it')
parser.add_argument('wordlistfile', action='store',
				metavar='dictionary.txt', type=argparse.FileType('rb'), # windows closes it in thread
				help='the word list to use with password cracking')
parser.add_argument('files', nargs='+', metavar='file.kirbi', type=str,
				help='File name to crack. Use asterisk \'*\' for many files.\n Files are exported with mimikatz or from extracttgsrepfrompcap.py')

args = parser.parse_args()

# load the tickets
enctickets = []
i = 0
for path in args.files:
	for filename in glob.glob(path):
		et = kerberos.extract_ticket_from_kirbi(filename)
		enctickets.append((et, i, filename))

if len(enctickets):
	print("Cracking %i tickets..." % len(enctickets))
else:
	print("No tickets found")
	sys.exit()

# load wordlist
for w in args.wordlistfile:
	word = w.decode('utf-8','ignore').strip()
	hash = kerberos.ntlmhash(word)
	for et in enctickets:
		kdata, nonce = kerberos.decrypt(hash, 2, et[0])
		if kdata:
			print('found password for ticket %i: %s  File: %s' % (et[1], word, et[2]))
			enctickets.remove(et)
			if len(enctickets) == 0:
				print('Successfully cracked all tickets')
				sys.exit()

if len(enctickets):
	print("Unable to crack %i tickets" % len(enctickets))
<|endoftext|>


<commit_before>from json import dumps

from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login as auth_login
from django.views.generic import TemplateView
from django.http import HttpResponse
from django.shortcuts import redirect
from django.contrib.messages import info
from django.utils.translation import ugettext_lazy as _
from django.db.models import Q
from django.db import transaction
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.mail import send_mail
from django.utils.http import int_to_base36

from mezzanine.conf import settings
from mezzanine.generic.views import initial_validation
from mezzanine.utils.views import set_cookie, is_spam
from mezzanine.utils.cache import add_cache_bypass
from mezzanine.utils.email import send_verification_mail, send_approve_mail, subject_template, \
    default_token_generator, send_mail_template
from mezzanine.utils.urls import login_redirect, next_url
from mezzanine.utils.views import render

from theme.forms import ThreadedCommentForm
from theme.forms import RatingForm, UserProfileForm, UserForm
from theme.models import UserProfile

from .forms import SignupForm

class UserProfileView(TemplateView):
    template_name='accounts/profile.html'

    def get_context_data(self, **kwargs):
        if 'user' in kwargs:
            try:
                u = User.objects.get(pk=int(kwargs['user']))
            except:
                u = User.objects.get(username=kwargs['user'])

        else:
            try:
                u = User.objects.get(pk=int(self.request.GET['user']))
            except:
                u = User.objects.get(username=self.request.GET['user'])

        # get all resources the profile user owns
        resources = u.uaccess.owned_resources

        # if requesting user is not the profile user, then show only resources that the requesting user has access
        if self.request.user != u:
            if self.request.user.is_authenticated():
                if self.request.user.is_superuser:
                    # admin can see all resources owned by profile user
                    pass
                else:
                    # filter out any resources the requesting user doesn't have access
                    resources = resources.filter(Q(pk__in=self.request.user.uaccess.view_resources) |
                                                 Q(raccess__public=True) | Q(raccess__discoverable=True))

            else:
                # for anonymous requesting user show only resources that are either public or discoverable
                resources = resources.filter(Q(raccess__public=True) | Q(raccess__discoverable=True))

        return {
            'profile_user': u,
            'resources': resources,
        }

# added by Hong Yi to address issue #186 to customize Mezzanine-based commenting form and view
def comment(request, template="generic/comments.html"):
    """
    Handle a ``ThreadedCommentForm`` submission and redirect back to its
    related object.
    """
    response = initial_validation(request, "comment")
    if isinstance(response, HttpResponse):
        return response
    obj, post_data = response
    form = ThreadedCommentForm(request, obj, post_data)
    if form.is_valid():
        url = obj.get_absolute_url()
        if is_spam(request, form, url):
            return redirect(url)
        comment = form.save(request)
        response = redirect(add_cache_bypass(comment.get_absolute_url()))
        # Store commenter's details in a cookie for 90 days.
        # for field in ThreadedCommentForm.cookie_fields:
        #     cookie_name = ThreadedCommentForm.cookie_prefix + field
        #     cookie_value = post_data.get(field, "")
        #     set_cookie(response, cookie_name, cookie_value)
        return response
    elif request.is_ajax() and form.errors:
        return HttpResponse(dumps({"errors": form.errors}))
    # Show errors with stand-alone comment form.
    context = {"obj": obj, "posted_comment_form": form}
    response = render(request, template, context)
    return response

# added by Hong Yi to address issue #186 to customize Mezzanine-based rating form and view
def rating(request):
    """
    Handle a ``RatingForm`` submission and redirect back to its
    related object.
    """
    response = initial_validation(request, "rating")
    if isinstance(response, HttpResponse):
        return response
    obj, post_data = response
    url = add_cache_bypass(obj.get_absolute_url().split("#")[0])
    response = redirect(url + "#rating-%s" % obj.id)
    rating_form = RatingForm(request, obj, post_data)
    if rating_form.is_valid():
        rating_form.save()
        if request.is_ajax():
            # Reload the object and return the rating fields as json.
            obj = obj.__class__.objects.get(id=obj.id)
            rating_name = obj.get_ratingfield_name()
            json = {}
            for f in ("average", "count", "sum"):
                json["rating_" + f] = getattr(obj, "%s_%s" % (rating_name, f))
            response = HttpResponse(dumps(json))
        ratings = ",".join(rating_form.previous + [rating_form.current])
        set_cookie(response, "mezzanine-rating", ratings)
    return response

def signup(request, template="accounts/account_signup.html"):
    """
    Signup form.
    """
    form = SignupForm(request, request.POST, request.FILES)
    if request.method == "POST" and form.is_valid():
        try:
            new_user = form.save()
        except ValidationError as e:
            # form.add_error(None, e.message)
            messages.error(request, e.message)
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        else:
            if not new_user.is_active:
                if settings.ACCOUNTS_APPROVAL_REQUIRED:
                    send_approve_mail(request, new_user)
                    info(request, _("Thanks for signing up! You'll receive "
                                    "an email when your account is activated."))
                else:
                    send_verification_mail(request, new_user, "signup_verify")
                    info(request, _("A verification email has been sent with "
                                    "a link for activating your account. If you "
                                    "do not receive this email please check your "
                                    "spam folder as sometimes the confirmation email "
                                    "gets flagged as spam. If you entered an incorrect "
                                    "email address, please request an account again."))
                return redirect(next_url(request) or "/")
            else:
                info(request, _("Successfully signed up"))
                auth_login(request, new_user)
                return login_redirect(request)
    context = {
        "form": form,
        "title": _("Sign up"),
    }
    return render(request, template, context)

@login_required
def update_user_profile(request):
    user = request.user
    old_email = user.email
    user_form = UserForm(request.POST, instance=user)
    user_profile = UserProfile.objects.filter(user=user).first()
    profile_form = UserProfileForm(request.POST, request.FILES, instance=user_profile)
    try:
        with transaction.atomic():
            if user_form.is_valid() and profile_form.is_valid():
                user_form.save()

                password1 = request.POST['password1']
                password2 = request.POST['password2']
                if len(password1) > 0:
                    if password1 == password2:
                        user.set_password(password1)
                        user.save()
                    else:
                        raise ValidationError("Passwords do not match.")

                profile = profile_form.save(commit=False)
                profile.user = request.user
                profile.save()
                messages.success(request, "Your profile has been successfully updated.")
                # if email was updated, reset to old email and send confirmation
                # email to the user's new email - email will be updated upon confirmation
                if old_email != request.POST['email']:
                    user.email = old_email
                    user.save()
                    new_email = request.POST['email']
                    # send a confirmation email to the new email address
                    send_verification_mail_for_email_update(request, user, new_email, "email_verify")
                    info(request, _("A verification email has been sent to your new email with "
                                    "a link for updating your email. If you "
                                    "do not receive this email please check your "
                                    "spam folder as sometimes the confirmation email "
                                    "gets flagged as spam. If you entered an incorrect "
                                    "email address, please request email update again. "
                                    ))
                    # send an email to the old address notifying the email change
                    send_mail(subject="Your HydroShare email.",
                              message="You have changed your HydroShare account email",
                              from_email= settings.DEFAULT_FROM_EMAIL, recipient_list=[old_email], fail_silently=False)
            else:
                errors = {}
                if not user_form.is_valid():
                    errors.update(user_form.errors)

                if not profile_form.is_valid():
                    errors.update(profile_form.errors)

                msg = ' '.join([err[0] for err in errors.values()])
                messages.error(request, msg)

    except Exception as ex:
        messages.error(request, ex.message)

    return HttpResponseRedirect(request.META['HTTP_REFERER'])

def send_verification_mail_for_email_update(request, user, new_email, verification_type):
    """
    Sends an email with a verification link to users when
    they update their email. The actual update of the email happens only after
    the verification link is clicked.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    verify_url = reverse(verification_type, kwargs={
        "uidb36": int_to_base36(user.id),
        "token": default_token_generator.make_token(user),
        "new_email": new_email
    }) + "?next=" + (next_url(request) or "/")
    context = {
        "request": request,
        "user": user,
        "new_email": new_email,
        "verify_url": verify_url,
    }
    subject_template_name = "email/%s_subject.txt" % verification_type
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject, "email/%s" % verification_type,
                       settings.DEFAULT_FROM_EMAIL, user.email,
                       context=context)

def email_verify(request, new_email, uidb36=None, token=None):
    """
    View for the link in the verification email sent to a user
    when they update their email as part of profile update.
    User email is set to new_email and logs them in,
    redirecting to the URL they tried to access when profile.
    """

    user = authenticate(uidb36=uidb36, token=token, is_active=True)
    if user is not None:
        user.email = new_email
        user.save()
        auth_login(request, user)
        messages.info(request, _("Successfully updated email"))
        # redirect to user profile page
        return HttpResponseRedirect('/user/{}/'.format(user.id))
    else:
        messages.error(request, _("The link you clicked is no longer valid."))
        return redirect("/")

@login_required
def deactivate_user(request):
    user = request.user
    user.is_active = False
    user.save()
    messages.success(request, "Your account has been successfully deactivated.")
    return HttpResponseRedirect('/accounts/logout/')<commit_msg>[#475] send email confirmation to new email address<commit_after>from json import dumps

from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login as auth_login
from django.views.generic import TemplateView
from django.http import HttpResponse
from django.shortcuts import redirect
from django.contrib.messages import info
from django.utils.translation import ugettext_lazy as _
from django.db.models import Q
from django.db import transaction
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.mail import send_mail
from django.utils.http import int_to_base36

from mezzanine.conf import settings
from mezzanine.generic.views import initial_validation
from mezzanine.utils.views import set_cookie, is_spam
from mezzanine.utils.cache import add_cache_bypass
from mezzanine.utils.email import send_verification_mail, send_approve_mail, subject_template, \
    default_token_generator, send_mail_template
from mezzanine.utils.urls import login_redirect, next_url
from mezzanine.utils.views import render

from theme.forms import ThreadedCommentForm
from theme.forms import RatingForm, UserProfileForm, UserForm
from theme.models import UserProfile

from .forms import SignupForm

class UserProfileView(TemplateView):
    template_name='accounts/profile.html'

    def get_context_data(self, **kwargs):
        if 'user' in kwargs:
            try:
                u = User.objects.get(pk=int(kwargs['user']))
            except:
                u = User.objects.get(username=kwargs['user'])

        else:
            try:
                u = User.objects.get(pk=int(self.request.GET['user']))
            except:
                u = User.objects.get(username=self.request.GET['user'])

        # get all resources the profile user owns
        resources = u.uaccess.owned_resources

        # if requesting user is not the profile user, then show only resources that the requesting user has access
        if self.request.user != u:
            if self.request.user.is_authenticated():
                if self.request.user.is_superuser:
                    # admin can see all resources owned by profile user
                    pass
                else:
                    # filter out any resources the requesting user doesn't have access
                    resources = resources.filter(Q(pk__in=self.request.user.uaccess.view_resources) |
                                                 Q(raccess__public=True) | Q(raccess__discoverable=True))

            else:
                # for anonymous requesting user show only resources that are either public or discoverable
                resources = resources.filter(Q(raccess__public=True) | Q(raccess__discoverable=True))

        return {
            'profile_user': u,
            'resources': resources,
        }

# added by Hong Yi to address issue #186 to customize Mezzanine-based commenting form and view
def comment(request, template="generic/comments.html"):
    """
    Handle a ``ThreadedCommentForm`` submission and redirect back to its
    related object.
    """
    response = initial_validation(request, "comment")
    if isinstance(response, HttpResponse):
        return response
    obj, post_data = response
    form = ThreadedCommentForm(request, obj, post_data)
    if form.is_valid():
        url = obj.get_absolute_url()
        if is_spam(request, form, url):
            return redirect(url)
        comment = form.save(request)
        response = redirect(add_cache_bypass(comment.get_absolute_url()))
        # Store commenter's details in a cookie for 90 days.
        # for field in ThreadedCommentForm.cookie_fields:
        #     cookie_name = ThreadedCommentForm.cookie_prefix + field
        #     cookie_value = post_data.get(field, "")
        #     set_cookie(response, cookie_name, cookie_value)
        return response
    elif request.is_ajax() and form.errors:
        return HttpResponse(dumps({"errors": form.errors}))
    # Show errors with stand-alone comment form.
    context = {"obj": obj, "posted_comment_form": form}
    response = render(request, template, context)
    return response

# added by Hong Yi to address issue #186 to customize Mezzanine-based rating form and view
def rating(request):
    """
    Handle a ``RatingForm`` submission and redirect back to its
    related object.
    """
    response = initial_validation(request, "rating")
    if isinstance(response, HttpResponse):
        return response
    obj, post_data = response
    url = add_cache_bypass(obj.get_absolute_url().split("#")[0])
    response = redirect(url + "#rating-%s" % obj.id)
    rating_form = RatingForm(request, obj, post_data)
    if rating_form.is_valid():
        rating_form.save()
        if request.is_ajax():
            # Reload the object and return the rating fields as json.
            obj = obj.__class__.objects.get(id=obj.id)
            rating_name = obj.get_ratingfield_name()
            json = {}
            for f in ("average", "count", "sum"):
                json["rating_" + f] = getattr(obj, "%s_%s" % (rating_name, f))
            response = HttpResponse(dumps(json))
        ratings = ",".join(rating_form.previous + [rating_form.current])
        set_cookie(response, "mezzanine-rating", ratings)
    return response

def signup(request, template="accounts/account_signup.html"):
    """
    Signup form.
    """
    form = SignupForm(request, request.POST, request.FILES)
    if request.method == "POST" and form.is_valid():
        try:
            new_user = form.save()
        except ValidationError as e:
            # form.add_error(None, e.message)
            messages.error(request, e.message)
            return HttpResponseRedirect(request.META['HTTP_REFERER'])
        else:
            if not new_user.is_active:
                if settings.ACCOUNTS_APPROVAL_REQUIRED:
                    send_approve_mail(request, new_user)
                    info(request, _("Thanks for signing up! You'll receive "
                                    "an email when your account is activated."))
                else:
                    send_verification_mail(request, new_user, "signup_verify")
                    info(request, _("A verification email has been sent with "
                                    "a link for activating your account. If you "
                                    "do not receive this email please check your "
                                    "spam folder as sometimes the confirmation email "
                                    "gets flagged as spam. If you entered an incorrect "
                                    "email address, please request an account again."))
                return redirect(next_url(request) or "/")
            else:
                info(request, _("Successfully signed up"))
                auth_login(request, new_user)
                return login_redirect(request)
    context = {
        "form": form,
        "title": _("Sign up"),
    }
    return render(request, template, context)

@login_required
def update_user_profile(request):
    user = request.user
    old_email = user.email
    user_form = UserForm(request.POST, instance=user)
    user_profile = UserProfile.objects.filter(user=user).first()
    profile_form = UserProfileForm(request.POST, request.FILES, instance=user_profile)
    try:
        with transaction.atomic():
            if user_form.is_valid() and profile_form.is_valid():
                user_form.save()

                password1 = request.POST['password1']
                password2 = request.POST['password2']
                if len(password1) > 0:
                    if password1 == password2:
                        user.set_password(password1)
                        user.save()
                    else:
                        raise ValidationError("Passwords do not match.")

                profile = profile_form.save(commit=False)
                profile.user = request.user
                profile.save()
                messages.success(request, "Your profile has been successfully updated.")
                # if email was updated, reset to old email and send confirmation
                # email to the user's new email - email will be updated upon confirmation
                if old_email != request.POST['email']:
                    user.email = old_email
                    user.save()
                    new_email = request.POST['email']
                    # send a confirmation email to the new email address
                    send_verification_mail_for_email_update(request, user, new_email, "email_verify")
                    info(request, _("A verification email has been sent to your new email with "
                                    "a link for updating your email. If you "
                                    "do not receive this email please check your "
                                    "spam folder as sometimes the confirmation email "
                                    "gets flagged as spam. If you entered an incorrect "
                                    "email address, please request email update again. "
                                    ))
                    # send an email to the old address notifying the email change
                    send_mail(subject="Your HydroShare email.",
                              message="You have changed your HydroShare account email",
                              from_email= settings.DEFAULT_FROM_EMAIL, recipient_list=[old_email], fail_silently=False)
            else:
                errors = {}
                if not user_form.is_valid():
                    errors.update(user_form.errors)

                if not profile_form.is_valid():
                    errors.update(profile_form.errors)

                msg = ' '.join([err[0] for err in errors.values()])
                messages.error(request, msg)

    except Exception as ex:
        messages.error(request, ex.message)

    return HttpResponseRedirect(request.META['HTTP_REFERER'])

def send_verification_mail_for_email_update(request, user, new_email, verification_type):
    """
    Sends an email with a verification link to users when
    they update their email. The email is sent to the new email.
    The actual update of the email happens only after
    the verification link is clicked.
    The ``verification_type`` arg is both the name of the urlpattern for
    the verification link, as well as the names of the email templates
    to use.
    """
    verify_url = reverse(verification_type, kwargs={
        "uidb36": int_to_base36(user.id),
        "token": default_token_generator.make_token(user),
        "new_email": new_email
    }) + "?next=" + (next_url(request) or "/")
    context = {
        "request": request,
        "user": user,
        "new_email": new_email,
        "verify_url": verify_url,
    }
    subject_template_name = "email/%s_subject.txt" % verification_type
    subject = subject_template(subject_template_name, context)
    send_mail_template(subject, "email/%s" % verification_type,
                       settings.DEFAULT_FROM_EMAIL, new_email,
                       context=context)

def email_verify(request, new_email, uidb36=None, token=None):
    """
    View for the link in the verification email sent to a user
    when they update their email as part of profile update.
    User email is set to new_email and logs them in,
    redirecting to the URL they tried to access when profile.
    """

    user = authenticate(uidb36=uidb36, token=token, is_active=True)
    if user is not None:
        user.email = new_email
        user.save()
        auth_login(request, user)
        messages.info(request, _("Successfully updated email"))
        # redirect to user profile page
        return HttpResponseRedirect('/user/{}/'.format(user.id))
    else:
        messages.error(request, _("The link you clicked is no longer valid."))
        return redirect("/")

@login_required
def deactivate_user(request):
    user = request.user
    user.is_active = False
    user.save()
    messages.success(request, "Your account has been successfully deactivated.")
    return HttpResponseRedirect('/accounts/logout/')<|endoftext|>


<commit_before># inspired from:

# https://python-packaging-user-guide.readthedocs.org/en/latest/single_source_version/
# https://github.com/pypa/warehouse/blob/master/warehouse/__about__.py

__name__ = "thinc"
__version__ = "7.3.0"
__summary__ = "Practical Machine Learning for NLP"
__uri__ = "https://github.com/explosion/thinc"
__author__ = "Matthew Honnibal"
__email__ = "matt@explosion.ai"
__license__ = "MIT"
__title__ = "thinc"
__release__ = True
<commit_msg>Set version to v7.3.1<commit_after># inspired from:

# https://python-packaging-user-guide.readthedocs.org/en/latest/single_source_version/
# https://github.com/pypa/warehouse/blob/master/warehouse/__about__.py

__name__ = "thinc"
__version__ = "7.3.1"
__summary__ = "Practical Machine Learning for NLP"
__uri__ = "https://github.com/explosion/thinc"
__author__ = "Matthew Honnibal"
__email__ = "matt@explosion.ai"
__license__ = "MIT"
__title__ = "thinc"
__release__ = True
<|endoftext|>


<commit_before>__version__ = "8.0.14"
__release__ = True
<commit_msg>Set version to 8.0.15<commit_after>__version__ = "8.0.15"
__release__ = True
<|endoftext|>


<commit_before>from collections import defaultdict, Sequence, Sized, Iterable
import inspect
import wrapt
from cytoolz import curry
from numpy import ndarray

from .exceptions import UndefinedOperatorError, DifferentLengthError
from .exceptions import ExpectedTypeError, ShapeMismatchError
from .exceptions import ConstraintError

def equal_length(*args):
    '''Check that a tuple of arguments has the same length.
    '''
    for i, arg in enumerate(args):
        if not isinstance(arg, Sized):
            raise ExpectedTypeError(arg, ['Sized'])
        if i >= 1 and len(arg) != len(args[0]):
            raise DifferentLengthError(args, arg_tuple, arg)

@curry
def has_shape(shape, arg_id, args, kwargs):
    '''Check that a particular argument is an array with a given shape. The
    shape may contain string attributes, which will be fetched from arg0 to
    the function (usually self).
    '''
    self = args[0]
    arg = args[arg_id]
    if not hasattr(arg, 'shape'):
        raise ExpectedTypeError(arg, ['array'])
    shape_values = []
    for dim in shape:
        if not isinstance(dim, int):
            dim = getattr(self, dim, None)
        shape_values.append(dim)
    if len(shape) != len(arg.shape):
        raise ShapeMismatchError(arg.shape, tuple(shape_values), shape)
    for i, dim in enumerate(shape_values):
        # Allow underspecified dimensions
        if dim != None and arg.shape[i] != dim:
            raise ShapeMismatchError(arg.shape, shape_values, shape)

def is_sequence(arg_id, args, kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])

def is_float(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, float):
        raise ExpectedTypeError(arg, ['float'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_shape(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])
    for value in arg:
        if value < 0 or not isinstance(value, int):
            raise ExpectedTypeError(arg, ['valid shape (positive ints)'])

def is_int(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, int):
        raise ExpectedTypeError(arg, ['int'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['min']))

def is_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray):
        raise ExpectedTypeError(arg, ['ndarray'])

def is_int_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray) and 'i' not in arg.dtype:
        raise ExpectedTypeError(arg, ['ndarray[int]'])

def operator_is_defined(op):
    @wrapt.decorator
    def checker(wrapped, instance, args, kwargs):
        if instance is None:
            instance = args[0]
        if instance is None:
            raise ExpectedTypeError(instance, ['Model'])
        if op not in instance._operators:
            raise UndefinedOperatorError(op, instance, args[0], instance._operators)
        else:
            return wrapped(*args, **kwargs)
    return checker

def arg(arg_id, *constraints):
    @wrapt.decorator
    def checked_function(wrapped, instance, args, kwargs):
        if not hasattr(wrapped, 'checks'):
            return wrapped(*args, **kwargs)
        if instance is not None:
            fix_args = [instance] + list(args)
        else:
            fix_args = list(args)
        for arg_id, checks in wrapped.checks.items():
            for check in checks:
                check(arg_id, fix_args, kwargs)
        return wrapped(*args, **kwargs)

    def arg_check_adder(func):
        if hasattr(func, 'checks'):
            func.checks.setdefault(arg_id, []).extend(constraints)
            return func
        else:
            wrapped = checked_function(func)
            wrapped.checks = {arg_id: list(constraints)}
            return wrapped
    return arg_check_adder

def args(*constraints):
    @wrapt.decorator
    def arg_check_adder(wrapped, instance, args, kwargs):
        for check in constraints:
            check(args)
        return wrapped(*args, **kwargs)
    return arg_check_adder
<commit_msg>Fix typos<commit_after>from collections import defaultdict, Sequence, Sized, Iterable
import inspect
import wrapt
from cytoolz import curry
from numpy import ndarray

from .exceptions import UndefinedOperatorError, DifferentLengthError
from .exceptions import ExpectedTypeError, ShapeMismatchError
from .exceptions import ConstraintError

def equal_length(*args):
    '''Check that a tuple of arguments has the same length.
    '''
    for i, arg in enumerate(args):
        if not isinstance(arg, Sized):
            raise ExpectedTypeError(arg, ['Sized'])
        if i >= 1 and len(arg) != len(args[0]):
            raise DifferentLengthError(args, arg)

@curry
def has_shape(shape, arg_id, args, kwargs):
    '''Check that a particular argument is an array with a given shape. The
    shape may contain string attributes, which will be fetched from arg0 to
    the function (usually self).
    '''
    self = args[0]
    arg = args[arg_id]
    if not hasattr(arg, 'shape'):
        raise ExpectedTypeError(arg, ['array'])
    shape_values = []
    for dim in shape:
        if not isinstance(dim, int):
            dim = getattr(self, dim, None)
        shape_values.append(dim)
    if len(shape) != len(arg.shape):
        raise ShapeMismatchError(arg.shape, tuple(shape_values), shape)
    for i, dim in enumerate(shape_values):
        # Allow underspecified dimensions
        if dim != None and arg.shape[i] != dim:
            raise ShapeMismatchError(arg.shape, shape_values, shape)

def is_sequence(arg_id, args, kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])

def is_float(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, float):
        raise ExpectedTypeError(arg, ['float'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['max']))

def is_shape(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, Iterable):
        raise ExpectedTypeError(arg, ['iterable'])
    for value in arg:
        if value < 0 or not isinstance(value, int):
            raise ExpectedTypeError(arg, ['valid shape (positive ints)'])

def is_int(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, int):
        raise ExpectedTypeError(arg, ['int'])
    if 'min' in kwargs and arg < kwargs['min']:
        raise ValueError("%s < min %s" % (arg, kwargs['min']))
    if 'max' in kwargs and arg > kwargs['max']:
        raise ValueError("%s > max %s" % (arg, kwargs['max']))

def is_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray):
        raise ExpectedTypeError(arg, ['ndarray'])

def is_int_array(arg_id, args, func_kwargs, **kwargs):
    arg = args[arg_id]
    if not isinstance(arg, ndarray) and 'i' not in arg.dtype:
        raise ExpectedTypeError(arg, ['ndarray[int]'])

def operator_is_defined(op):
    @wrapt.decorator
    def checker(wrapped, instance, args, kwargs):
        if instance is None:
            instance = args[0]
        if instance is None:
            raise ExpectedTypeError(instance, ['Model'])
        if op not in instance._operators:
            raise UndefinedOperatorError(op, instance, args[0], instance._operators)
        else:
            return wrapped(*args, **kwargs)
    return checker

def arg(arg_id, *constraints):
    @wrapt.decorator
    def checked_function(wrapped, instance, args, kwargs):
        if not hasattr(wrapped, 'checks'):
            return wrapped(*args, **kwargs)
        if instance is not None:
            fix_args = [instance] + list(args)
        else:
            fix_args = list(args)
        for arg_id, checks in wrapped.checks.items():
            for check in checks:
                check(arg_id, fix_args, kwargs)
        return wrapped(*args, **kwargs)

    def arg_check_adder(func):
        if hasattr(func, 'checks'):
            func.checks.setdefault(arg_id, []).extend(constraints)
            return func
        else:
            wrapped = checked_function(func)
            wrapped.checks = {arg_id: list(constraints)}
            return wrapped
    return arg_check_adder

def args(*constraints):
    @wrapt.decorator
    def arg_check_adder(wrapped, instance, args, kwargs):
        for check in constraints:
            check(args)
        return wrapped(*args, **kwargs)
    return arg_check_adder
<|endoftext|>


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

"""
Launch control
"""

import logging, os, shutil, stat, subprocess, tempfile, time

DATA = os.path.abspath('data')
CONF = os.path.abspath('conf')
LOGS = os.path.join(DATA, 'logs')

whoami = os.getlogin()

def run(args):
    log.info("running %s", args)
    return subprocess.Popen(args, stdin=subprocess.PIPE, stdout = subprocess.PIPE,
                            stderr=subprocess.PIPE).stdout.read()

class SH(object):
    def __getitem__(self, args):
        return run([args] if isinstance(args, string) else [str(x) for x in args])

sh = SH()

def ps_stat(pid, run):
    fields = ['%cpu', '%mem', 'etime', 'pid', 'start', 'cputime', 'rss', 'vsize']
    v = sh['ps', '-p', str(pid), '-o', ' '.join(fields)].splitlines()
    if len(v) <= 1: return {}
    return dict(zip(fields, v[-1].split()))

log_files = {'postgresql':'postgres.log',
             }

ports = {'postgresql':5432,  # also coded into postgresql.conf
         }

# Enable logging
logging.basicConfig()
log = logging.getLogger('')
log.setLevel(logging.DEBUG)   # WARNING, INFO

def init_data_directory():
    log.info("ensuring that the data directory exist")
    if not os.path.exists(DATA):
        os.makedirs(DATA)

    if not os.path.exists(LOGS):
        os.makedirs(LOGS)

    log.info("ensuring that the data directory has restrictive permissions")
    if os.stat(DATA)[stat.ST_MODE] != 0o40700:
        os.chmod(DATA, 0o40700)

    os.environ['PATH'] = os.path.join(DATA, 'local/bin/') + ':' + os.environ['PATH']

DATABASE = os.path.join(DATA, 'db')

def read_configuration_file():
    log.info('reading configuration file')

class Account(object):
    def __init__(self, username, hostname='localhost'):
        self._username = username
        self._hostname = hostname
        self._user_at = '%s@%s'%(self._username, self._hostname)

    def run(self, args):
        """Run command with given arguments using this account and return output."""
        if self._username == whoami and self._hostname == 'localhost':
            pre = []
        elif self._username == 'root' and whoami != 'root':
            pre = ['sudo']   # interactive
        else:
            pre = ['ssh', self._user_at]
        return sh[pre + args]

    def kill(self, pid, signal=15):
        self.run(['kill', '-%s'%signal, pid])

    def readfile(self, filename):
        if self._hostname == 'localhost':
            try:
                return open(filename).read()
            except IOError:
                pass

        path = tempfile.mkdtemp()  # secure
        try:
            dest = os.path.join(path, filename)
            if self._hostname == 'localhost' and self._username == 'root':
                # use sudo
                sh['sudo', 'cp', filename, dest]
            else:
                # use ssh
                sh['scp', '%s:"%s"'%(self._user_at, filename), dest]
            if os.path.exists(dest):
                return open(dest).read()
        finally:
            shutil.rmtree(path)

local_user = Account(username=whoami, hostname='localhost')

class Component(object):
    def __init__(self, processes):
        self._processes = processes

    def _match(self, ids):
        return [p for p in self._processes if ids is None or p.id() in ids]

    def start(self, ids=None):
        return [p.start() for p in self._match(ids)]

    def stop(self, ids=None):
        return [p.stop() for p in self._match(ids)]

    def reload(self, ids=None):
        return [p.reload() for p in self._match(ids)]

    def status(self, ids=None):
        return [p.status() for p in self._match(ids)]

class Process(object):
    def __init__(self, account, pidfile, start_cmd=None, stop_cmd=None, reload_cmd=None):
        self._account = account
        self._pidfile = pidfile
        self._start_cmd = start_cmd
        self._stop_cmd = stop_cmd
        self._reload_cmd = reload_cmd
        self._pid = None

    def pid(self):
        if self._pid is not None:
            return self._pid
        try:
            self._pid = self._account.readfile(self._pidfile)
        except IOError: # no file
            self._pid = None
        return self._pid

    def start(self):
        self._pid = None
        if self._start_cmd is not None:
            return self._account.run(self._start_cmd)

    def stop(self):
        self._pid = None
        if self._stop_cmd is not None:
            return self._account.run(self._stop_cmd)
        else:
            return self._account.kill(self.pid())

    def reload(self):
        self._pid = None
        if self._reload_cmd is not None:
            return self._account.run(self._reload_cmd)
        else:
            return 'reload not defined'

    def status(self):
        pid = self.pid()
        return ps_stat(pid, self._account.run) if pid else {}

nginx_cmd = ['nginx', '-c', os.path.join(CONF, 'nginx.conf')]
nginx_pid = os.path.join(DATA, 'local/logs/nginx.pid')
nginx = Component([Process(account = local_user,
                           pidfile = nginx_pid,
                           start_cmd = nginx_cmd,
                           stop_cmd = nginx_cmd + ['-s', 'stop'],
                           reload_cmd = nginx_cmd + ['-s', 'reload'])])

def launch_haproxy_servers():
    log.info('launching haproxy servers')
    sh['sudo', 'haproxy', '-f', 'haproxy.conf']

def start_postgresql():
    sh['pg_ctl', 'start', '-D', DATABASE, '-l', os.path.join('data/logs', log_files['postgresql'])]

def initialize_postgresql_database():
    # on OS X this initdb can fail.  The fix (see http://willbryant.net/software/mac_os_x/postgres_initdb_fatal_shared_memory_error_on_leopard) is to type "sudo sysctl -w kern.sysv.shmall=65536" and also create /etc/sysctl.conf with content "kern.sysv.shmall=65536".
    sh['initdb', '-D', DATABASE]
    os.unlink('data/db/postgresql.conf')
    os.symlink('conf/postgresql.conf', os.path.join('data/db', 'postgresql.conf'))
    start_postgresql()
    for i in range(5):  # race condition with server starting -- TODO: detect this better!
        time.sleep(0.5)
        try:
            sh['createdb', '-p', ports['postgresql'], 'sagews']
            break
        except:
            pass

def launch_postgresql_servers():
    log.info('launching postgresql servers')
    if not os.path.exists(DATABASE):
        initialize_postgresql_database()
    else:
        start_postgresql()

def launch_database_servers():
    log.info('launching database servers')

def launch_memcached_servers():
    log.info('launching memcached servers')

def launch_backend_servers():
    log.info('launching backend servers')

def launch_worker_servers():
    log.info('launching worker servers')

def launch_servers():
    launch_nginx_servers()
    launch_haproxy_servers()
    launch_database_servers()
    launch_memcached_servers()
    launch_backend_servers()
    launch_worker_servers()

def monitor_servers():
    # TODO
    import time
    time.sleep(1e6)

def quit_servers():
    # TODO
    return

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Launch components of sagews")

    parser.add_argument('--launch_nginx', dest='launch_nginx', action='store_const', const=True, default=False,
                        help="launch the NGINX server")

    parser.add_argument('--launch_haproxy', dest='launch_haproxy', action='store_const', const=True, default=False,
                        help="launch the haproxy server")

    parser.add_argument('--launch_postgresql', dest='launch_postgresql', action='store_const', const=True, default=False,
                        help="launch the postgresql database server")

    args = parser.parse_args()

    init_data_directory()
    read_configuration_file()

    if args.launch_nginx:
        launch_nginx_servers()

    if args.launch_haproxy:
        launch_haproxy_servers()

    if args.launch_postgresql:
        launch_postgresql_servers()
<commit_msg>working on admin management<commit_after>#!/usr/bin/env python

"""
Launch control
"""

import logging, os, shutil, signal, stat, subprocess, tempfile, time

DATA = 'data'
CONF = 'conf'
LOGS = os.path.join(DATA, 'logs')

whoami = os.getlogin()

def run(args, maxtime=5):
    args = [str(x) for x in args]
    def timeout(*a):
        raise KeyboardInterrupt("running '%s' took more than %s seconds, so killed"%(' '.join(args), maxtime))
    signal.signal(signal.SIGALRM, timeout)
    signal.alarm(maxtime)
    log.info("running '%s'", ' '.join(args))
    try:
        return subprocess.Popen(args, stdin=subprocess.PIPE, stdout = subprocess.PIPE,
                                stderr=subprocess.PIPE).stdout.read()
    finally:
        signal.signal(signal.SIGALRM, signal.SIG_IGN)  # cancel the alarm

class SH(object):
    def __getitem__(self, args):
        return run([args] if isinstance(args, str) else list(args))

sh = SH()

def ps_stat(pid, run):
    fields = ['%cpu', '%mem', 'etime', 'pid', 'start', 'cputime', 'rss', 'vsize']
    v = sh['ps', '-p', str(int(pid)), '-o', ' '.join(fields)].splitlines()
    if len(v) <= 1: return {}
    return dict(zip(fields, v[-1].split()))

log_files = {'postgresql':'postgres.log',
             }

ports = {'postgresql':5432,  # also coded into postgresql.conf
         }

# Enable logging
logging.basicConfig()
log = logging.getLogger('')
log.setLevel(logging.DEBUG)   # WARNING, INFO

def init_data_directory():
    log.info("ensuring that the data directory exist")
    if not os.path.exists(DATA):
        os.makedirs(DATA)

    if not os.path.exists(LOGS):
        os.makedirs(LOGS)

    log.info("ensuring that the data directory has restrictive permissions")
    if os.stat(DATA)[stat.ST_MODE] != 0o40700:
        os.chmod(DATA, 0o40700)

    os.environ['PATH'] = os.path.join(DATA, 'local/bin/') + ':' + os.environ['PATH']

DATABASE = os.path.join(DATA, 'db')

def read_configuration_file():
    log.info('reading configuration file')

class Account(object):
    def __init__(self, username, hostname='localhost', path='.'):
        self._username = username
        self._hostname = hostname
        self._path = path
        self._user_at = '%s@%s'%(self._username, self._hostname)

    def run(self, args):
        """Run command with given arguments using this account and return output."""
        if self._username == whoami and self._hostname == 'localhost':
            pre = []
        elif self._username == 'root' and whoami != 'root':
            pre = ['sudo']   # interactive
        else:
            pre = ['ssh', self._user_at]
        return sh[pre + args]

    def kill(self, pid, signal=15):
        self.run(['kill', '-%s'%signal, pid])

    def copyfile(self, src, target):
        if self._hostname == 'localhost':
            if self._username == whoami:
                open(filename).write(contents)
                return
            elif self._username = 'root':
                sh['sudo', 'cp', src, target]
                return
        sh['scp', src, self._user_at + ':' + os.path.join(self._path, target)]

    def readfile(self, filename):
        filename = os.path.join(self._path, filename)
        if self._hostname == 'localhost':
            if not os.path.exists(filename):
                raise IOError, "no such file or directory: '%s'"%filename
            try:
                return open(filename).read()
            except IOError:
                pass

        path = tempfile.mkdtemp()  # secure
        try:
            dest = os.path.join(path, filename)
            if self._hostname == 'localhost' and self._username == 'root':
                sh['sudo', 'cp', filename, dest]
            else:
                sh['scp', '%s:"%s"'%(self._user_at, filename), dest]
            return open(dest).read()
        finally:
            shutil.rmtree(path)

local_user = Account(username=whoami, hostname='localhost')
local_root = Account(username='root', hostname='localhost')

class Component(object):
    def __init__(self, id, processes):
        self._processes = processes
        self._id = id

    def __repr__(self):
        return "Component %s with %s processes"%(self._id, len(self._processes))

    def __getitem__(self, i):
        return self._processes[i]

    def _procs_with_id(self, ids):
        return [p for p in self._processes if ids is None or p.id() in ids]

    def start(self, ids=None):
        return [p.start() for p in self._procs_with_id(ids)]

    def stop(self, ids=None):
        return [p.stop() for p in self._procs_with_id(ids)]

    def reload(self, ids=None):
        return [p.reload() for p in self._procs_with_id(ids)]

    def status(self, ids=None):
        return [p.status() for p in self._procs_with_id(ids)]

class Process(object):
    def __init__(self, account, id, pidfile, start_cmd=None, stop_cmd=None, reload_cmd=None):
        self._account = account
        self._id = id
        self._pidfile = pidfile
        self._start_cmd = start_cmd
        self._stop_cmd = stop_cmd
        self._reload_cmd = reload_cmd
        self._pid = None

    def id(self):
        return self._id

    def _parse_pidfile(self, contents):
        return int(contents)

    def pid(self):
        if self._pid is not None:
            return self._pid
        try:
            self._pid = self._parse_pidfile(self._account.readfile(self._pidfile).strip())
        except IOError: # no file
            self._pid = None
        return self._pid

    def is_running(self):
        return len(self.status()) > 0

    def start(self):
        if self.is_running(): return
        self._pid = None
        if self._start_cmd is not None:
            return self._account.run(self._start_cmd)

    def stop(self):
        self._pid = None
        if self._stop_cmd is not None:
            return self._account.run(self._stop_cmd)
        else:
            return self._account.kill(self.pid())

    def reload(self):
        self._pid = None
        if self._reload_cmd is not None:
            return self._account.run(self._reload_cmd)
        else:
            return 'reload not defined'

    def status(self):
        pid = self.pid()
        return ps_stat(pid, self._account.run) if pid else {}

    def restart(self):
        self.stop()
        self.start()

####################
# Nginx
####################
class NginxProcess(Process):
    def __init__(self, account, id):
        nginx_cmd = ['nginx', '-c', os.path.join(CONF, 'nginx.conf')]
        Process.__init__(self, account, id,
                         pidfile    = os.path.join(DATA, 'local/nginx.pid'),
                         start_cmd  = nginx_cmd,
                         stop_cmd   = nginx_cmd + ['-s', 'stop'],
                         reload_cmd = nginx_cmd + ['-s', 'reload'])

    def __repr__(self):
        return "Nginx process %s at %s"%(self._id, self._account)

nginx = Component('nginx', [NginxProcess(local_user, 0)])

####################
# HAproxy
####################
class HAproxyProcess(Process):
    def __init__(self, account, id):
        pidfile = os.path.join(DATA, 'local/haproxy.pid')
        Process.__init__(self, account, id,
                         pidfile = pidfile,
                         start_cmd = ['haproxy', '-f', 'conf/haproxy.conf', '-p', pidfile])

    def _parse_pidfile(self, contents):
        return int(contents.splitlines()[0])

haproxy = Component('haproxy', [HAproxyProcess(local_root,0)])

####################
# PostgreSQL
####################
class PostgreSQLProcess(Process):
    def _cmd(self, name, *opts):
        return ['pg_ctl', name, '-D', self._db] + list(opts)

    def _parse_pidfile(self, contents):
        return int(contents.splitlines()[0])

    def __init__(self, account, id):
        self._db = os.path.join(DATA, 'db')
        self._log = os.path.join('data/logs', log_files['postgresql'])
        Process.__init__(self, account, id,
                         pidfile    = os.path.join(self._db, 'postmaster.pid'),
                         start_cmd  = self._cmd('start', '-l', self._log),
                         stop_cmd   = self._cmd('stop'),
                         reload_cmd = self._cmd('reload'))

    def restart(self):
        return self._account.run(self._cmd('restart'))

    def status2(self):
        return self._account.run(self._cmd('status'))

    def initdb(self):
        s = self._account.run(self._cmd('initdb'))
        target = os.path.join(self._db, 'postgresql.conf')
        s += '\n' + self._account.copyfile('conf/postgresql.conf', target)
        return s

    def createdb(self, name='sagews'):
        self._account.run(['createdb', '-p', ports['postgresql'], name])

postgresql = Component('postgreSQL', [PostgreSQLProcess(local_user, 0)])

## def launch_haproxy_servers():
##     log.info('launching haproxy servers')
##     sh['sudo', 'haproxy', '-f', 'haproxy.conf']

## def start_postgresql():
##     sh['pg_ctl', 'start', '-D', DATABASE, '-l', os.path.join('data/logs', log_files['postgresql'])]

## def initialize_postgresql_database():
##     # on OS X this initdb can fail.  The fix (see http://willbryant.net/software/mac_os_x/postgres_initdb_fatal_shared_memory_error_on_leopard) is to type "sudo sysctl -w kern.sysv.shmall=65536" and also create /etc/sysctl.conf with content "kern.sysv.shmall=65536".
##     sh['initdb', '-D', DATABASE]
##     os.unlink('data/db/postgresql.conf')
##     os.symlink('conf/postgresql.conf', os.path.join('data/db', 'postgresql.conf'))
##     start_postgresql()
##     for i in range(5):  # race condition with server starting -- TODO: detect this better!
##         time.sleep(0.5)
##         try:
##             sh['createdb', '-p', ports['postgresql'], 'sagews']
##             break
##         except:
##             pass

## def launch_postgresql_servers():
##     log.info('launching postgresql servers')
##     if not os.path.exists(DATABASE):
##         initialize_postgresql_database()
##     else:
##         start_postgresql()

## def launch_database_servers():
##     log.info('launching database servers')

## def launch_memcached_servers():
##     log.info('launching memcached servers')

## def launch_backend_servers():
##     log.info('launching backend servers')

## def launch_worker_servers():
##     log.info('launching worker servers')

## def launch_servers():
##     launch_nginx_servers()
##     launch_haproxy_servers()
##     launch_database_servers()
##     launch_memcached_servers()
##     launch_backend_servers()
##     launch_worker_servers()

## def monitor_servers():
##     # TODO
##     import time
##     time.sleep(1e6)

## def quit_servers():
##     # TODO
##     return

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Launch components of sagews")

    parser.add_argument('--launch_nginx', dest='launch_nginx', action='store_const', const=True, default=False,
                        help="launch the NGINX server")

    parser.add_argument('--launch_haproxy', dest='launch_haproxy', action='store_const', const=True, default=False,
                        help="launch the haproxy server")

    parser.add_argument('--launch_postgresql', dest='launch_postgresql', action='store_const', const=True, default=False,
                        help="launch the postgresql database server")

    args = parser.parse_args()

    init_data_directory()
    read_configuration_file()

    if args.launch_nginx:
        launch_nginx_servers()

    if args.launch_haproxy:
        launch_haproxy_servers()

    if args.launch_postgresql:
        launch_postgresql_servers()
<|endoftext|>


<commit_before># coding=utf-8
__author__ = 'had'

from django.conf.urls import patterns, url
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('ticket.views',

                       url(r'^$', 'home.home'),
                       url(r'^login/$', 'auth.user_login'),
                       url(r'^add_ticket/$', 'tickets.add_ticket'),
                       url(r'^ticket_list_new/$', 'tickets.ticket_list_new'),
                       url(r'^ticket_list_clos/$', 'tickets.ticket_list_clos'),
                       url(r'^ticket_list_work/$', 'tickets.ticket_list_work'),
                       url(r'^ticket_list_incomplete/$', 'tickets.ticket_list_incomplet'),
                       url(r'^ticket_list_resolved/$', 'tickets.ticket_list_resolved'),
                       url(r'^ticket/edit_id=(?P<id>\d+)$', 'tickets.ticket_edit'),
                       url(r'^ticket/id=(?P<id>\d+)$', 'tickets.view_ticket', name='view'),
                       url(r'^ticket_all/$', 'tickets.ticket_all'),
                       url(r'^my_ticket_assign/$', 'tickets.my_ticket_assign'),
                       url(r'^delete/id=(?P<id>\d+)$', 'tickets.delete_ticket'),
                       url(r'^set_incomplete/id=(?P<id>\d+)$', 'tickets.set_incomplete'),
                       url(r'^set_complete/id=(?P<id>\d+)$', 'tickets.set_complete'),
                       url(r'^statistiques/$', 'stats.statistic'),
                       url(r'^logout/$', 'auth.logout_view'),

                       ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<commit_msg>Ajout de flux rss<commit_after># coding=utf-8
__author__ = 'had'

from django.conf.urls import patterns, url
from django.conf import settings
from django.conf.urls.static import static
from ticket.views.rss import LatestEntriesFeed

urlpatterns = patterns('ticket.views',

                       url(r'^$', 'home.home'),
                       url(r'^login/$', 'auth.user_login'),
                       url(r'^add_ticket/$', 'tickets.add_ticket'),
                       url(r'^ticket_list_new/$', 'tickets.ticket_list_new'),
                       url(r'^ticket_list_clos/$', 'tickets.ticket_list_clos'),
                       url(r'^ticket_list_work/$', 'tickets.ticket_list_work'),
                       url(r'^ticket_list_incomplete/$', 'tickets.ticket_list_incomplet'),
                       url(r'^ticket_list_resolved/$', 'tickets.ticket_list_resolved'),
                       url(r'^ticket/edit_id=(?P<id>\d+)$', 'tickets.ticket_edit'),
                       url(r'^ticket/id=(?P<id>\d+)$', 'tickets.view_ticket', name='view'),
                       url(r'^ticket_all/$', 'tickets.ticket_all'),
                       url(r'^my_ticket_assign/$', 'tickets.my_ticket_assign'),
                       url(r'^delete/id=(?P<id>\d+)$', 'tickets.delete_ticket'),
                       url(r'^set_incomplete/id=(?P<id>\d+)$', 'tickets.set_incomplete'),
                       url(r'^set_complete/id=(?P<id>\d+)$', 'tickets.set_complete'),
                       url(r'^statistiques/$', 'stats.statistic'),
                       url(r'^logout/$', 'auth.logout_view'),
                       url(r'^latest/feed/$', LatestEntriesFeed()),

                       ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
<|endoftext|>