<commit_before># Here's what I need to be able to do:
# 1.  Grab the desired version info from control file
# 2.  Determine the correct source version for each desired verion - Update
#     desired version to include latest source version information
# 3.  Determine if a changeset file fits the desired version
# 4.  Grab a matching version from the repository
from build import lookaside, recipe
import os
import os.path
from repository import changeset, repository
from pkgid import PkgId, thawPackage
import sys

class ControlFile:
    def __init__(self, group, repos, cfg, label):
        self.file = group + ':source'
        self.repos = repos
        self.label = label
        self._loaders = []
        self.cfg = cfg

    def loadControlFile(self):
        self.trove = self.repos.findTrove(self.label, self.file, None, None)[0]
        self.pkg = PkgId(self.file, self.trove.getVersion(), self.trove.getFlavor(), justName = True)
        self.controlClass = self.loadRecipe(self.pkg)
        branch = self.pkg.version.branch()
        self.controlObj = self.controlClass(self.repos, self.cfg, branch, None)
        self.controlObj.setup()
        self.troves = self.controlObj.addTroveList
        # XXX need to change variable names...
        self.packages = {}
        self.recipes = {}
        self.sources = {}
        print "Loading Recipes..."
        sys.stdout.flush()
        self.getSources()
        self.loadRecipes()

    def getSources(self):
        notfound = {}
        sources = {}
        flavor = None
        # XXX this might be faster if we tried to replicate findTrove behavior
        # e.g., if versionStrs are on the same label (90% of the time), we
        # can just get leaves on label
        for (troveName, versionStr) in self.troves:
            # remove potential :devel, etc, components from the components,
            # since we want to point to the source trove
            troveName = troveName.split(':', 1)[0]
            try:
                sourceTroveName = troveName + ':source'
                if troveName == 'icecream':
                    from lib import epdb
                    epdb.set_trace()
                sourceTroves = self.repos.findTrove(self.label, sourceTroveName, flavor, versionStr)
            except repository.PackageNotFound:
                notfound[troveName] = True
                continue
            for sourceTrove in sourceTroves:
                p = PkgId(troveName, sourceTrove.getVersion(), sourceTrove.getFlavor(), justName = True)
                if troveName not in sources:
                    sources[troveName] = []
                # its possible that multiple addTrove commands point to a single
                # source trove
                if p not in sources[troveName]:
                    sources[troveName].append(p)
        self.notfound = notfound
        self.sources = sources

    def loadRecipes(self):
        recipes = {}
        keys = self.sources.keys()
        keys.sort()
        count = 0
        lcache = lookaside.RepositoryCache(self.repos)
        for source in keys:
            for p in self.sources[source]:
                try:
                    srcdirs = [ self.cfg.sourceSearchDir % { 'pkgname' : p.name }  ]
                    recipeClass = self.loadRecipe(p)
                    # XXX Use flags can be checked during the execution
                    # of actions, so it's no good to try to figure
                    # out which ones this package cares about after
                    # just calling setup
                    #use.track(True)
                    recipeObj = recipeClass(self.cfg, lcache, srcdirs)
                    recipeObj.setup()
                    # one recipe can create several packages --
                    # get a list of them here
                    # XXX should probably also get components,
                    # and check components
                    for package in recipeObj.packages:
                        if not package in self.packages:
                            self.packages[package] = []
                        self.packages[package].append(p)
                except recipe.RecipeFileError, e:
                    print str(e)
                    sys.exit(1)
                else:
                    p.recipeClass = recipeClass
                    l = recipes.get(recipeClass.name, [])
                    l.append(p)
                    recipes[recipeClass.name] = l
                    sys.stdout.write('%d: %s\n' % (count, p))
                    count = count + 1
                    sys.stdout.flush()
        sys.stdout.write('\nDone.\n')
        sys.stdout.flush()
        self.recipes = recipes

    def loadRecipe(self, pkg, label=None):
        recipefile = pkg.name.split(':')[0]
        # ensure name has :source tacked on end
        name = recipefile + ':source'
        recipefile += '.recipe'
        loader = recipe.recipeLoaderFromSourceComponent(name, recipefile,
                                self.cfg, self.repos, pkg.versionStr,
                                label=label)
        recipeClass = loader[0].getRecipe()
        # we need to keep the loaders around so that they do not
        # get garbage collected -- their references are needed
        # later!!!
        self._loaders.append(loader)
        return recipeClass

    def getMatchedChangeSets(self, changesetpath):
        """ Try to match the changesets in changesetpath against the
            desired troves
            Successful changesets must match:
            1. Name
            2. Label
            3. Version (this may have been grabbed from repository
                        if not specified in control file)
            4. Source version
            Potentially (if specified):
            5. Use/Flag flavor 5. build count
        """
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)

        changesetNames =  [ x for x in os.listdir(changesetpath) if x.endswith('.ccs') ]
        for changesetName in changesetNames:
            cs = changeset.ChangeSetFromFile(os.path.join(changesetpath, changesetName))
            pkgs = cs.primaryTroveList
            for (name, version, flavor) in pkgs:
                cspkg = PkgId(name, version, flavor, justName=True)
                if cspkg.name in self.packages:
                    for pkg in self.packages[cspkg.name]:
                    # convert cspkg version to source version by removing
                    # buildCount

                        v = cspkg.version.copy()
                        v.trailingVersion().buildCount = None
                        # XXXXXXXXX big hack to deal with the fact that
                        # icecream version numbers are out of whack
                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((cspkg, changesetName))
                            del unmatched[pkg]
                            if len(matches[pkg]) > 1:
                                raise TypeError, "We don't handle multiple changesets matching a package yet"
                                # when we do, we'll sort them so that the
                                # highest version comes first

        return (matches, unmatched)

    def getDesiredCompiledVersion(self, pkg):
        # XXX this is faking basically what cooking a group does.
        # do I really not want to keep info in a group and recompile it before building?
        pv = pkg.version.copy()
        versions = self.repos.getTroveVersionList(pkg.version.branch().label(), [pkg.name])
        maxver = None
        maxcount = -1
        for version in versions[pkg.name]:
            bc = version.trailingVersion().buildCount
            pv.trailingVersion().buildCount = bc
            if pv == version:
                if bc > maxcount:
                    maxcount = bc
                    maxver = version
        return version

<commit_msg>Add get installed packages method<commit_after># Here's what I need to be able to do:
# 1.  Grab the desired version info from control file
# 2.  Determine the correct source version for each desired verion - Update
#     desired version to include latest source version information
# 3.  Determine if a changeset file fits the desired version
# 4.  Grab a matching version from the repository
from build import lookaside, recipe
from local import database
import os
import os.path
from repository import changeset, repository
from pkgid import PkgId, thawPackage
import sys

class ControlFile:
    def __init__(self, group, repos, cfg, label):
        self.file = group + ':source'
        self.repos = repos
        self.label = label
        self._loaders = []
        self.cfg = cfg

    def loadControlFile(self):
        self.trove = self.repos.findTrove(self.label, self.file, None, None)[0]
        self.pkg = PkgId(self.file, self.trove.getVersion(), self.trove.getFlavor(), justName = True)
        self.controlClass = self.loadRecipe(self.pkg)
        branch = self.pkg.version.branch()
        self.controlObj = self.controlClass(self.repos, self.cfg, branch, None)
        self.controlObj.setup()
        self.troves = self.controlObj.addTroveList
        # XXX need to change variable names...
        self.packages = {}
        self.recipes = {}
        self.sources = {}
        print "Loading Recipes..."
        sys.stdout.flush()
        self.getSources()
        self.loadRecipes()

    def getSources(self):
        notfound = {}
        sources = {}
        flavor = None
        # XXX this might be faster if we tried to replicate findTrove behavior
        # e.g., if versionStrs are on the same label (90% of the time), we
        # can just get leaves on label
        for (troveName, versionStr, flavor) in self.troves:
            # remove potential :devel, etc, components from the components,
            # since we want to point to the source trove
            troveName = troveName.split(':', 1)[0]
            try:
                sourceTroveName = troveName + ':source'
                sourceTroves = self.repos.findTrove(self.label, sourceTroveName, flavor, versionStr)
            except repository.PackageNotFound:
                notfound[troveName] = True
                continue
            for sourceTrove in sourceTroves:
                p = PkgId(troveName, sourceTrove.getVersion(), sourceTrove.getFlavor(), justName = True)
                if troveName not in sources:
                    sources[troveName] = []
                # its possible that multiple addTrove commands point to a single
                # source trove
                if p not in sources[troveName]:
                    sources[troveName].append(p)
        self.notfound = notfound
        self.sources = sources

    def loadRecipes(self):
        recipes = {}
        keys = self.sources.keys()
        keys.sort()
        count = 0
        lcache = lookaside.RepositoryCache(self.repos)
        for source in keys:
            for p in self.sources[source]:
                try:
                    srcdirs = [ self.cfg.sourceSearchDir % { 'pkgname' : p.name }  ]
                    recipeClass = self.loadRecipe(p)
                    # XXX Use flags can be checked during the execution
                    # of actions, so it's no good to try to figure
                    # out which ones this package cares about after
                    # just calling setup
                    #use.track(True)
                    recipeObj = recipeClass(self.cfg, lcache, srcdirs)
                    recipeObj.setup()
                    # one recipe can create several packages --
                    # get a list of them here
                    # XXX should probably also get components,
                    # and check components
                    for package in recipeObj.packages:
                        if not package in self.packages:
                            self.packages[package] = []
                        self.packages[package].append(p)
                except recipe.RecipeFileError, e:
                    print str(e)
                    sys.exit(1)
                else:
                    p.recipeClass = recipeClass
                    l = recipes.get(recipeClass.name, [])
                    l.append(p)
                    recipes[recipeClass.name] = l
                    sys.stdout.write('%d: %s\n' % (count, p))
                    count = count + 1
                    sys.stdout.flush()
        sys.stdout.write('\nDone.\n')
        sys.stdout.flush()
        self.recipes = recipes

    def loadRecipe(self, pkg, label=None):
        recipefile = pkg.name.split(':')[0]
        # ensure name has :source tacked on end
        name = recipefile + ':source'
        recipefile += '.recipe'
        loader = recipe.recipeLoaderFromSourceComponent(name, recipefile,
                                self.cfg, self.repos, pkg.versionStr,
                                label=label)
        recipeClass = loader[0].getRecipe()
        # we need to keep the loaders around so that they do not
        # get garbage collected -- their references are needed
        # later!!!
        self._loaders.append(loader)
        return recipeClass

    def getInstalledPkgs(self):
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)

        db = database.Database(self.cfg.root, self.cfg.dbPath)
        for troveName in db.iterAllTroveNames():
            if troveName not in self.packages:
                continue
            for version in  db.getTroveVersionList(troveName):
                for trove in db.findTrove(troveName, version.asString()):

                    dbpkg = PkgId(troveName, trove.getVersion(), trove.getFlavor(), justName=True)
                    for pkg in self.packages[dbpkg.name]:
                    # convert dbpkg version to source version by removing
                    # buildCount
                        v = dbpkg.version.copy()
                        v.trailingVersion().buildCount = None

                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((dbpkg, troveName))
                            try:
                                del unmatched[pkg]
                            except KeyError:
                                pass
        return (matches, unmatched)

    def getMatchedChangeSets(self, changesetpath):
        """ Try to match the changesets in changesetpath against the
            desired troves
            Successful changesets must match:
            1. Name
            2. Label
            3. Version (this may have been grabbed from repository
                        if not specified in control file)
            4. Source version
            Potentially (if specified):
            5. Use/Flag flavor 5. build count
        """
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)

        changesetNames =  [ x for x in os.listdir(changesetpath) if x.endswith('.ccs') ]
        for changesetName in changesetNames:
            cs = changeset.ChangeSetFromFile(os.path.join(changesetpath, changesetName))
            pkgs = cs.primaryTroveList
            for (name, version, flavor) in pkgs:
                cspkg = PkgId(name, version, flavor, justName=True)
                if cspkg.name in self.packages:
                    for pkg in self.packages[cspkg.name]:
                    # convert cspkg version to source version by removing
                    # buildCount

                        v = cspkg.version.copy()
                        v.trailingVersion().buildCount = None
                        # XXXXXXXXX big hack to deal with the fact that
                        # icecream version numbers are out of whack
                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((cspkg, changesetName))
                            del unmatched[pkg]
                            if len(matches[pkg]) > 1:
                                raise TypeError, "We don't handle multiple changesets matching a package yet"
                                # when we do, we'll sort them so that the
                                # highest version comes first

        return (matches, unmatched)

    def getDesiredCompiledVersion(self, pkg):
        # XXX this is faking basically what cooking a group does.
        # do I really not want to keep info in a group and recompile it before building?
        pv = pkg.version.copy()
        versions = self.repos.getTroveVersionList(pkg.version.branch().label(), [pkg.name])
        maxver = None
        maxcount = -1
        for version in versions[pkg.name]:
            bc = version.trailingVersion().buildCount
            pv.trailingVersion().buildCount = bc
            if pv == version:
                if bc > maxcount:
                    maxcount = bc
                    maxver = version
        return version

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""A very long book."""

import os

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

FHANDLER = open(os.path.join(DPATH, 'war_and_peace.txt'), 'r')

WORDS = FHANDLER.read()

FHANDLER.close()
<commit_msg>task 6<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""A very long book."""

import os

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

FHANDLER = open(os.path.join(DPATH, 'war_and_peace.txt'), 'r')

WORDS = FHANDLER.read()

FHANDLER.close()

WORDCT = WORDS.split()
<|endoftext|>


<commit_before>"""
Django settings for seniorproject project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'zb*lt6rada&5^!@%j5gyzrkde&1j#c2ia+0k548e7jj1gqr@#v'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'api',
    'site'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'seniorproject.urls'

WSGI_APPLICATION = 'wsgi.application'

STATIC_ROOT = "/var/www/static/"

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = [str(os.path.join(BASE_DIR, 'templates'))]

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
<commit_msg>Take two<commit_after>"""
Django settings for seniorproject project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'zb*lt6rada&5^!@%j5gyzrkde&1j#c2ia+0k548e7jj1gqr@#v'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'api',
    'site'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'seniorproject.urls'

WSGI_APPLICATION = 'wsgi.application'

STATIC_ROOT = "/static/"

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = [str(os.path.join(BASE_DIR, 'templates'))]

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
<|endoftext|>


<commit_before># Here's what I need to be able to do:
# 1.  Grab the desired version info from control file
# 2.  Determine the correct source version for each desired verion - Update
#     desired version to include latest source version information
# 3.  Determine if a changeset file fits the desired version
# 4.  Grab a matching version from the repository
from build import lookaside, recipe, use
from local import database
import os
import os.path
from repository import changeset, repository
from pkgid import PkgId, thawPackage
import sys

class ControlFile:
    def __init__(self, group, repos, cfg, label):
        self.file = group + ':source'
        self.repos = repos
        self.label = label
        self._loaders = []
        self.cfg = cfg

    def loadControlFile(self):
        self.trove = self.repos.findTrove(self.label, self.file, None, None)[0]
        self.pkg = PkgId(self.file, self.trove.getVersion(), self.trove.getFlavor(), justName = True)
        self.controlClass = self.loadRecipe(self.pkg)
        branch = self.pkg.version.branch()
        self.controlObj = self.controlClass(self.repos, self.cfg, branch, None)
        self.controlObj.setup()
        self.troves = self.controlObj.addTroveList
        # XXX need to change variable names...
        self.packages = {}
        self.recipes = {}
        self.sources = {}
        self.usedFlags = {}
        print "Loading Recipes..."
        sys.stdout.flush()
        self.getSources()
        self.loadRecipes()

    def getSources(self):
        notfound = {}
        sources = {}
        flavor = None
        # XXX this might be faster if we tried to replicate findTrove behavior
        # e.g., if versionStrs are on the same label (90% of the time), we
        # can just get leaves on label
        for (troveName, versionStr, flavor) in self.troves:
            # remove potential :devel, etc, components from the components,
            # since we want to point to the source trove
            troveName = troveName.split(':', 1)[0]
            try:
                sourceTroveName = troveName + ':source'
                sourceTroves = self.repos.findTrove(self.label, sourceTroveName, flavor, versionStr)
            except repository.PackageNotFound:
                notfound[troveName] = True
                continue
            for sourceTrove in sourceTroves:
                p = PkgId(troveName, sourceTrove.getVersion(), sourceTrove.getFlavor(), justName = True)
                if troveName not in sources:
                    sources[troveName] = []
                # its possible that multiple addTrove commands point to a single
                # source trove
                if p not in sources[troveName]:
                    sources[troveName].append(p)
        self.notfound = notfound
        self.sources = sources

    def loadRecipes(self):
        recipes = {}
        keys = self.sources.keys()
        keys.sort()
        count = 0
        lcache = lookaside.RepositoryCache(self.repos)
        for source in keys:
            for p in self.sources[source]:
                try:
                    srcdirs = [ self.cfg.sourceSearchDir % { 'pkgname' : p.name }  ]
                    recipeClass = self.loadRecipe(p)
                    recipeObj = recipeClass(self.cfg, lcache, srcdirs)
                    recipeObj.setup()
                    # one recipe can create several packages --
                    # get a list of them here
                    # XXX should probably also get components,
                    # and check components
                    for package in recipeObj.packages:
                        if not package in self.packages:
                            self.packages[package] = []
                        self.packages[package].append(p)
                except recipe.RecipeFileError, e:
                    print str(e)
                    sys.exit(1)
                else:
                    p.recipeClass = recipeClass
                    l = recipes.get(recipeClass.name, [])
                    l.append(p)
                    recipes[recipeClass.name] = l
                    sys.stdout.write('%d: %s\n' % (count, p))
                    count = count + 1
                    sys.stdout.flush()
        sys.stdout.write('\nDone.\n')
        sys.stdout.flush()
        self.recipes = recipes

    def loadRecipe(self, pkg, label=None):
        recipefile = pkg.name.split(':')[0]
        # ensure name has :source tacked on end
        name = recipefile + ':source'
        recipefile += '.recipe'
        use.resetUsed()
        loader = recipe.recipeLoaderFromSourceComponent(name, recipefile,
                                self.cfg, self.repos, pkg.versionStr,
                                label=label)
        pkg.usedFlags = use.getUsed()
        recipeClass = loader[0].getRecipe()
        # we need to keep the loaders around so that they do not
        # get garbage collected -- their references are needed
        # later!!!
        self._loaders.append(loader)
        return recipeClass

    def getInstalledPkgs(self):
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)

        db = database.Database(self.cfg.root, self.cfg.dbPath)
        for troveName in db.iterAllTroveNames():
            if troveName not in self.packages:
                continue
            for version in  db.getTroveVersionList(troveName):
                for trove in db.findTrove(troveName, version.asString()):

                    dbpkg = PkgId(troveName, trove.getVersion(), trove.getFlavor(), justName=True)
                    for pkg in self.packages[dbpkg.name]:
                    # convert dbpkg version to source version by removing
                    # buildCount
                        v = dbpkg.version.copy()
                        v.trailingVersion().buildCount = None

                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((dbpkg, troveName))
                            try:
                                del unmatched[pkg]
                            except KeyError:
                                pass
        return (matches, unmatched)

    def getMatchedChangeSets(self, changesetpath):
        """ Try to match the changesets in changesetpath against the
            desired troves
            Successful changesets must match:
            1. Name
            2. Label
            3. Version (this may have been grabbed from repository
                        if not specified in control file)
            4. Source version
            Potentially (if specified):
            5. Use/Flag flavor 5. build count
        """
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)
        if not os.path.exists(changesetpath):
            return matches, unmatched

        changesetNames =  [ x for x in os.listdir(changesetpath) if x.endswith('.ccs') ]
        for changesetName in changesetNames:
            cs = changeset.ChangeSetFromFile(os.path.join(changesetpath, changesetName))
            pkgs = cs.primaryTroveList
            for (name, version, flavor) in pkgs:
                cspkg = PkgId(name, version, flavor, justName=True)
                if cspkg.name in self.packages:
                    for pkg in self.packages[cspkg.name]:
                    # convert cspkg version to source version by removing
                    # buildCount

                        v = cspkg.version.copy()
                        v.trailingVersion().buildCount = None
                        # XXXXXXXXX big hack to deal with the fact that
                        # icecream version numbers are out of whack
                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((cspkg, changesetName))
                            del unmatched[pkg]
                            if len(matches[pkg]) > 1:
                                raise TypeError, "We don't handle multiple changesets matching a package yet"
                                # when we do, we'll sort them so that the
                                # highest version comes first

        return (matches, unmatched)

    def getDesiredCompiledVersion(self, pkg):
        # XXX this is faking basically what cooking a group does.
        # do I really not want to keep info in a group and recompile it before building?
        pv = pkg.version.copy()
        versions = self.repos.getTroveVersionList(pkg.version.branch().label(), [pkg.name])
        maxver = None
        maxcount = -1
        for version in versions[pkg.name]:
            bc = version.trailingVersion().buildCount
            pv.trailingVersion().buildCount = bc
            if pv == version:
                if bc > maxcount:
                    maxcount = bc
                    maxver = version
        return version

<commit_msg>* Fix up tests for new syntax  * BUGFIX - testsuite was not restoring stderr correctly  * Remove some sys.exits, raise errors instead  * Fix typo in chroot.py, remove stale __main__ handling from chroot<commit_after># Here's what I need to be able to do:
# 1.  Grab the desired version info from control file
# 2.  Determine the correct source version for each desired verion - Update
#     desired version to include latest source version information
# 3.  Determine if a changeset file fits the desired version
# 4.  Grab a matching version from the repository
from build import lookaside, recipe, use
from local import database
import os
import os.path
from repository import changeset, repository
from pkgid import PkgId, thawPackage
import sys

class ControlFile:
    def __init__(self, group, repos, cfg, label):
        self.file = group + ':source'
        self.repos = repos
        self.label = label
        self._loaders = []
        self.cfg = cfg

    def loadControlFile(self):
        self.trove = self.repos.findTrove(self.label, self.file, None, None)[0]
        self.pkg = PkgId(self.file, self.trove.getVersion(), self.trove.getFlavor(), justName = True)
        self.controlClass = self.loadRecipe(self.pkg)
        branch = self.pkg.version.branch()
        self.controlObj = self.controlClass(self.repos, self.cfg, branch, None)
        self.controlObj.setup()
        self.troves = self.controlObj.addTroveList
        # XXX need to change variable names...
        self.packages = {}
        self.recipes = {}
        self.sources = {}
        self.usedFlags = {}
        print "Loading Recipes..."
        sys.stdout.flush()
        self.getSources()
        self.loadRecipes()

    def getSources(self):
        notfound = {}
        sources = {}
        flavor = None
        # XXX this might be faster if we tried to replicate findTrove behavior
        # e.g., if versionStrs are on the same label (90% of the time), we
        # can just get leaves on label
        for (troveName, versionStr, flavor) in self.troves:
            # remove potential :devel, etc, components from the components,
            # since we want to point to the source trove
            troveName = troveName.split(':', 1)[0]
            try:
                sourceTroveName = troveName + ':source'
                sourceTroves = self.repos.findTrove(self.label, sourceTroveName, flavor, versionStr)
            except repository.PackageNotFound:
                notfound[troveName] = True
                continue
            for sourceTrove in sourceTroves:
                p = PkgId(troveName, sourceTrove.getVersion(), sourceTrove.getFlavor(), justName = True)
                if troveName not in sources:
                    sources[troveName] = []
                # its possible that multiple addTrove commands point to a single
                # source trove
                if p not in sources[troveName]:
                    sources[troveName].append(p)
        self.notfound = notfound
        self.sources = sources

    def loadRecipes(self):
        recipes = {}
        keys = self.sources.keys()
        keys.sort()
        count = 0
        lcache = lookaside.RepositoryCache(self.repos)
        for source in keys:
            for p in self.sources[source]:
                try:
                    srcdirs = [ self.cfg.sourceSearchDir % { 'pkgname' : p.name }  ]
                    recipeClass = self.loadRecipe(p)
                    recipeObj = recipeClass(self.cfg, lcache, srcdirs)
                    recipeObj.setup()
                    # one recipe can create several packages --
                    # get a list of them here
                    # XXX should probably also get components,
                    # and check components
                    for package in recipeObj.packages:
                        if not package in self.packages:
                            self.packages[package] = []
                        self.packages[package].append(p)
                except recipe.RecipeFileError, e:
                    raise
                else:
                    p.recipeClass = recipeClass
                    l = recipes.get(recipeClass.name, [])
                    l.append(p)
                    recipes[recipeClass.name] = l
                    sys.stdout.write('%d: %s\n' % (count, p))
                    count = count + 1
                    sys.stdout.flush()
        sys.stdout.write('\nDone.\n')
        sys.stdout.flush()
        self.recipes = recipes

    def loadRecipe(self, pkg, label=None):
        recipefile = pkg.name.split(':')[0]
        # ensure name has :source tacked on end
        name = recipefile + ':source'
        recipefile += '.recipe'
        use.resetUsed()
        loader = recipe.recipeLoaderFromSourceComponent(name, recipefile,
                                self.cfg, self.repos, pkg.versionStr,
                                label=label)
        pkg.usedFlags = use.getUsed()
        recipeClass = loader[0].getRecipe()
        # we need to keep the loaders around so that they do not
        # get garbage collected -- their references are needed
        # later!!!
        self._loaders.append(loader)
        return recipeClass

    def getInstalledPkgs(self):
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)

        db = database.Database(self.cfg.root, self.cfg.dbPath)
        for troveName in db.iterAllTroveNames():
            if troveName not in self.packages:
                continue
            for version in  db.getTroveVersionList(troveName):
                for trove in db.findTrove(troveName, version.asString()):

                    dbpkg = PkgId(troveName, trove.getVersion(), trove.getFlavor(), justName=True)
                    for pkg in self.packages[dbpkg.name]:
                    # convert dbpkg version to source version by removing
                    # buildCount
                        v = dbpkg.version.copy()
                        v.trailingVersion().buildCount = None

                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((dbpkg, troveName))
                            try:
                                del unmatched[pkg]
                            except KeyError:
                                pass
        return (matches, unmatched)

    def getMatchedChangeSets(self, changesetpath):
        """ Try to match the changesets in changesetpath against the
            desired troves
            Successful changesets must match:
            1. Name
            2. Label
            3. Version (this may have been grabbed from repository
                        if not specified in control file)
            4. Source version
            Potentially (if specified):
            5. Use/Flag flavor 5. build count
        """
        matches = {}
        unmatched = {}
        for name,pkgs in self.packages.iteritems():
            for pkg in pkgs:
                if pkg not in unmatched:
                    unmatched[pkg] = []
                unmatched[pkg].append(name)
        if not os.path.exists(changesetpath):
            return matches, unmatched

        changesetNames =  [ x for x in os.listdir(changesetpath) if x.endswith('.ccs') ]
        for changesetName in changesetNames:
            cs = changeset.ChangeSetFromFile(os.path.join(changesetpath, changesetName))
            pkgs = cs.primaryTroveList
            for (name, version, flavor) in pkgs:
                cspkg = PkgId(name, version, flavor, justName=True)
                if cspkg.name in self.packages:
                    for pkg in self.packages[cspkg.name]:
                    # convert cspkg version to source version by removing
                    # buildCount

                        v = cspkg.version.copy()
                        v.trailingVersion().buildCount = None
                        # XXXXXXXXX big hack to deal with the fact that
                        # icecream version numbers are out of whack
                        if pkg.version == v or pkg.name == 'icecream':
                            if pkg not in matches:
                                matches[pkg] = []
                            matches[pkg].append((cspkg, changesetName))
                            del unmatched[pkg]
                            if len(matches[pkg]) > 1:
                                raise TypeError, "We don't handle multiple changesets matching a package yet"
                                # when we do, we'll sort them so that the
                                # highest version comes first

        return (matches, unmatched)

    def getDesiredCompiledVersion(self, pkg):
        # XXX this is faking basically what cooking a group does.
        # do I really not want to keep info in a group and recompile it before building?
        pv = pkg.version.copy()
        versions = self.repos.getTroveVersionList(pkg.version.branch().label(), [pkg.name])
        maxver = None
        maxcount = -1
        for version in versions[pkg.name]:
            bc = version.trailingVersion().buildCount
            pv.trailingVersion().buildCount = bc
            if pv == version:
                if bc > maxcount:
                    maxcount = bc
                    maxver = version
        return version

<|endoftext|>


<commit_before>from __future__ import unicode_literals
from __future__ import print_function

from operator import itemgetter

import numpy
import pandas

"""
Helpers for derived features, particularly in Pandas.
"""

def add_lag_feature(dataframe, feature, time_steps, time_suffix, drop=False, data_type=None):
    """Derive a rolling mean with the specified time steps"""
    assert isinstance(dataframe, pandas.DataFrame)
    assert isinstance(dataframe.index, pandas.DatetimeIndex)

    name = feature + "_rolling_{}".format(time_suffix)
    dataframe[name] = dataframe[feature].rolling(window=time_steps).mean().fillna(method="backfill")

    if data_type:
        dataframe[name] = dataframe[name].astype(data_type)

    if drop:
        dataframe.drop([feature], axis=1, inplace=True)

def add_transformation_feature(dataframe, feature, transform, drop=False):
    """Derive a feature with a simple function like log, sqrt, etc"""
    assert isinstance(dataframe, pandas.DataFrame)
    new_name = feature + "_" + transform

    if transform == "log":
        transformed = numpy.log(dataframe[feature] + 1)
    elif transform == "square":
        transformed = numpy.square(dataframe[feature])
    elif transform == "sqrt":
        transformed = numpy.sqrt(dataframe[feature])
    elif transform == "gradient":
        transformed = numpy.gradient(dataframe[feature])
    else:
        raise ValueError("Unknown transform {} specified".format(transform))

    dataframe[new_name] = transformed

    if drop:
        dataframe.drop([feature], axis=1, inplace=True)

def get_event_series(datetime_index, event_ranges):
    """Create a boolean series showing when in the datetime_index we're in the time ranges in the event_ranges"""
    assert isinstance(datetime_index, pandas.DatetimeIndex)
    series = pandas.Series(data=0, index=datetime_index, dtype=numpy.int8)

    for event in event_ranges:
        assert isinstance(event, TimeRange)

        # Note: the side only matters for exact matches which are super rare in a datetime index
        closest_start = series.index.searchsorted(event.start, side="right")
        closest_end = series.index.searchsorted(event.end, side="right")
        series.loc[closest_start:closest_end] = 1

    return series

def find_best_features(dataset, model, scorer, n_jobs=1):
    import sklearn.cross_validation

    baseline = sklearn.cross_validation.cross_val_score(model, dataset.inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()
    print("Baseline: {}".format(baseline))

    # try deleting each feature
    loo_scores = [None for _ in dataset.feature_names]
    for i, name in enumerate(dataset.feature_names):
        included = [j for j in range(dataset.inputs.shape[1]) if j != i]

        reduced_inputs = dataset.inputs[:, included]
        loo_scores[i] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()
        print("Leaving out {}: {}".format(name, loo_scores[i]))

    # rank features by LOO scores
    ranked_features = sorted(enumerate(loo_scores), key=itemgetter(1), reverse=True)
    pruned_scores = [None for _ in ranked_features]
    pruned_scores[0] = baseline

    # assume that we dropping them in their LOO order is optimal (not generally true but it might work)
    prune_set = set()
    for i, _ in ranked_features[:-1]:
        prune_set.add(i)
        included = [j for j in range(dataset.inputs.shape[1]) if j not in prune_set]
        reduced_inputs = dataset.inputs[:, included]
        pruned_scores[len(prune_set)] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()

        print("Score with {} features: {}".format(len(included), pruned_scores[len(prune_set)]))
        print("\tDropped {}".format(dataset.feature_names[i]))

def get_loo_scores(dataset, model, scorer, exclude_set):
    import sklearn.cross_validation
    loo_scores = {}
    for i, name in enumerate(dataset.feature_names):
        if i in exclude_set:
            continue

        included = [j for j in range(dataset.inputs.shape[1]) if j != i and j not in exclude_set]

        reduced_inputs = dataset.inputs[:, included]
        loo_scores[i] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer,
                                                                 cv=dataset.splits).mean()
    return loo_scores

def rfe_slow(dataset, model, scorer):
    dropped_set = set()
    for num_dropped in range(dataset.inputs.shape[1] - 2):
        loo_scores = get_loo_scores(dataset, model, scorer, dropped_set)
        ranked_features = sorted(loo_scores.items(), key=itemgetter(1), reverse=True)
        worst_feature, left_out_score = ranked_features[0]

        dropped_set.add(worst_feature)

        print("Dropped {}: {}".format(dataset.feature_names[worst_feature], left_out_score))

class TimeRange(object):
    """Thin wrapper to help deal with events that span a range of time"""
    def __init__(self, start, end):
        self.start = start
        self.end = end
<commit_msg>Removing the mass ewma features<commit_after>from __future__ import unicode_literals
from __future__ import print_function

from operator import itemgetter

import numpy
import pandas

"""
Helpers for derived features, particularly in Pandas.
"""

def add_lag_feature(dataframe, feature, time_steps, time_suffix, drop=False, data_type=None, use_ewma=False):
    """Derive a rolling mean with the specified time steps"""
    assert isinstance(dataframe, pandas.DataFrame)
    assert isinstance(dataframe.index, pandas.DatetimeIndex)

    name = feature + "_rolling_{}".format(time_suffix)
    if use_ewma:
        name += "_ewma"

    if use_ewma:
        dataframe[name] = dataframe[feature].ewm(span=time_steps).mean().fillna(method="backfill")
    else:
        dataframe[name] = dataframe[feature].rolling(window=time_steps).mean().fillna(method="backfill")

    if data_type:
        dataframe[name] = dataframe[name].astype(data_type)

    if drop:
        dataframe.drop([feature], axis=1, inplace=True)

def add_transformation_feature(dataframe, feature, transform, drop=False):
    """Derive a feature with a simple function like log, sqrt, etc"""
    assert isinstance(dataframe, pandas.DataFrame)
    new_name = feature + "_" + transform

    if transform == "log":
        transformed = numpy.log(dataframe[feature] + 1)
    elif transform == "square":
        transformed = numpy.square(dataframe[feature])
    elif transform == "sqrt":
        transformed = numpy.sqrt(dataframe[feature])
    elif transform == "gradient":
        transformed = numpy.gradient(dataframe[feature])
    else:
        raise ValueError("Unknown transform {} specified".format(transform))

    dataframe[new_name] = transformed

    if drop:
        dataframe.drop([feature], axis=1, inplace=True)

def get_event_series(datetime_index, event_ranges):
    """Create a boolean series showing when in the datetime_index we're in the time ranges in the event_ranges"""
    assert isinstance(datetime_index, pandas.DatetimeIndex)
    series = pandas.Series(data=0, index=datetime_index, dtype=numpy.int8)

    for event in event_ranges:
        assert isinstance(event, TimeRange)

        # Note: the side only matters for exact matches which are super rare in a datetime index
        closest_start = series.index.searchsorted(event.start, side="right")
        closest_end = series.index.searchsorted(event.end, side="right")
        series.loc[closest_start:closest_end] = 1

    return series

def find_best_features(dataset, model, scorer, n_jobs=1):
    import sklearn.cross_validation

    baseline = sklearn.cross_validation.cross_val_score(model, dataset.inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()
    print("Baseline: {}".format(baseline))

    # try deleting each feature
    loo_scores = [None for _ in dataset.feature_names]
    for i, name in enumerate(dataset.feature_names):
        included = [j for j in range(dataset.inputs.shape[1]) if j != i]

        reduced_inputs = dataset.inputs[:, included]
        loo_scores[i] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()
        print("Leaving out {}: {}".format(name, loo_scores[i]))

    # rank features by LOO scores
    ranked_features = sorted(enumerate(loo_scores), key=itemgetter(1), reverse=True)
    pruned_scores = [None for _ in ranked_features]
    pruned_scores[0] = baseline

    # assume that we dropping them in their LOO order is optimal (not generally true but it might work)
    prune_set = set()
    for i, _ in ranked_features[:-1]:
        prune_set.add(i)
        included = [j for j in range(dataset.inputs.shape[1]) if j not in prune_set]
        reduced_inputs = dataset.inputs[:, included]
        pruned_scores[len(prune_set)] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer, cv=dataset.splits, n_jobs=n_jobs).mean()

        print("Score with {} features: {}".format(len(included), pruned_scores[len(prune_set)]))
        print("\tDropped {}".format(dataset.feature_names[i]))

def get_loo_scores(dataset, model, scorer, exclude_set):
    import sklearn.cross_validation
    loo_scores = {}
    for i, name in enumerate(dataset.feature_names):
        if i in exclude_set:
            continue

        included = [j for j in range(dataset.inputs.shape[1]) if j != i and j not in exclude_set]

        reduced_inputs = dataset.inputs[:, included]
        loo_scores[i] = sklearn.cross_validation.cross_val_score(model, reduced_inputs, dataset.outputs, scoring=scorer,
                                                                 cv=dataset.splits).mean()
    return loo_scores

def rfe_slow(dataset, model, scorer):
    dropped_set = set()
    for num_dropped in range(dataset.inputs.shape[1] - 2):
        loo_scores = get_loo_scores(dataset, model, scorer, dropped_set)
        ranked_features = sorted(loo_scores.items(), key=itemgetter(1), reverse=True)
        worst_feature, left_out_score = ranked_features[0]

        dropped_set.add(worst_feature)

        print("Dropped {}: {}".format(dataset.feature_names[worst_feature], left_out_score))

class TimeRange(object):
    """Thin wrapper to help deal with events that span a range of time"""
    def __init__(self, start, end):
        self.start = start
        self.end = end
<|endoftext|>


<commit_before>#!/usr/bin/python

#
# Simple test of GPS sensor
#

import raspberrypi

from time import sleep
from GPS_serial import GPS_AdafruitSensor

if __name__ == "__main__":
    print "Testing GPS sensor (running 5x with 5s pause)..."

    print "Initialising..."
    gps_sensor = GPS_AdafruitSensor(debug=True, serial_bus='/dev/ttyAMA0')
    print raspberrypi.serial_bus()

    # heading
    print "Reading 1..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    sleep(5)

    print "Reading 2..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    sleep(5)

    print "Reading 3..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    sleep(5)

    print "Reading 4..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    sleep(5)

    print "Reading 5..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    sleep(5)

    print "Done."

    # raw values - not currently implemented any differently
    # will move more fields over when correctly reading sensor
    # gpsSensor.read_sensor_raw()<commit_msg>raw added for debugging<commit_after>#!/usr/bin/python

#
# Simple test of GPS sensor
#

import raspberrypi

from time import sleep
from GPS_serial import GPS_AdafruitSensor

if __name__ == "__main__":
    print "Testing GPS sensor (running 5x with 5s pause)..."

    print "Initialising..."
    gps_sensor = GPS_AdafruitSensor(debug=True, serial_bus='/dev/ttyAMA0')
    print raspberrypi.serial_bus()

    # heading
    print "Reading 1..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    print gps_sensor.read_sensor_raw()
    sleep(5)

    print "Reading 2..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    print gps_sensor.read_sensor_raw()
    sleep(5)

    print "Reading 3..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    print gps_sensor.read_sensor_raw()
    sleep(5)

    print "Reading 4..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    print gps_sensor.read_sensor_raw()
    sleep(5)

    print "Reading 5..."
    (fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp) = gps_sensor.read_sensor()
    print fix, lat, lon, heading, speed, altitude, num_sat, timestamp, datestamp
    print gps_sensor.read_sensor_raw()
    sleep(5)

    print "Done."

    # raw values - not currently implemented any differently
    # will move more fields over when correctly reading sensor
    # gpsSensor.read_sensor_raw()<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from collections import defaultdict, namedtuple
from random import choice, randint, shuffle
from math import floor, log10

from bitcoin import COIN
from transaction import Transaction
from util import NotEnoughFunds, PrintError, profiler

Bucket = namedtuple('Bucket', ['desc', 'size', 'value', 'coins'])

def strip_unneeded(bkts, sufficient_funds):
    '''Remove buckets that are unnecessary in achieving the spend amount'''
    bkts = sorted(bkts, key = lambda bkt: bkt.value)
    for i in range(len(bkts)):
        if not sufficient_funds(bkts[i + 1:]):
            return bkts[i:]
    # Shouldn't get here
    return bkts

class CoinChooserBase(PrintError):

    def keys(self, coins):
        raise NotImplementedError

    def bucketize_coins(self, coins):
        keys = self.keys(coins)
        buckets = defaultdict(list)
        for key, coin in zip(keys, coins):
            buckets[key].append(coin)

        def make_Bucket(desc, coins):
            size = sum(Transaction.estimated_input_size(coin)
                       for coin in coins)
            value = sum(coin['value'] for coin in coins)
            return Bucket(desc, size, value, coins)

        return map(make_Bucket, buckets.keys(), buckets.values())

    def penalty_func(self, tx):
        def penalty(candidate):
            return 0
        return penalty

    def change_amounts(self, tx, count, fee_estimator, dust_threshold):
        # The amount left after adding 1 change output
        return [tx.get_fee() - fee_estimator(1)]

    def change_outputs(self, tx, change_addrs, fee_estimator, dust_threshold):
        amounts = self.change_amounts(tx, len(change_addrs), fee_estimator,
                                      dust_threshold)
        # If change is above dust threshold after accounting for the
        # size of the change output, add it to the transaction.
        dust = sum(amount for amount in amounts if amount < dust_threshold)
        amounts = [amount for amount in amounts if amount >= dust_threshold]
        change = [('address', addr, amount)
                  for addr, amount in zip(change_addrs, amounts)]
        self.print_error('change:', change)
        if dust:
            self.print_error('not keeping dust', dust)
        return change

    def make_tx(self, coins, outputs, change_addrs, fee_estimator,
                dust_threshold):
        '''Select unspent coins to spend to pay outputs.  If the change is
        greater than dust_threshold (after adding the change output to
        the transaction) it is kept, otherwise none is sent and it is
        added to the transaction fee.'''

        # Copy the ouputs so when adding change we don't modify "outputs"
        tx = Transaction.from_io([], outputs[:])
        # Size of the transaction with no inputs and no change
        base_size = tx.estimated_size()
        spent_amount = tx.output_value()

        def sufficient_funds(buckets):
            '''Given a list of buckets, return True if it has enough
            value to pay for the transaction'''
            total_input = sum(bucket.value for bucket in buckets)
            total_size = sum(bucket.size for bucket in buckets) + base_size
            return total_input >= spent_amount + fee_estimator(total_size)

        # Collect the coins into buckets, choose a subset of the buckets
        buckets = self.bucketize_coins(coins)
        buckets = self.choose_buckets(buckets, sufficient_funds,
                                      self.penalty_func(tx))

        tx.inputs = [coin for b in buckets for coin in b.coins]
        tx_size = base_size + sum(bucket.size for bucket in buckets)

        # This takes a count of change outputs and returns a tx fee;
        # each pay-to-bitcoin-address output serializes as 34 bytes
        fee = lambda count: fee_estimator(tx_size + count * 34)
        change = self.change_outputs(tx, change_addrs, fee, dust_threshold)
        tx.outputs.extend(change)

        self.print_error("using %d inputs" % len(tx.inputs))
        self.print_error("using buckets:", [bucket.desc for bucket in buckets])

        return tx

class CoinChooserClassic(CoinChooserBase):
    '''The classic electrum algorithm.  Chooses coins starting with the
    oldest that are sufficient to cover the spent amount, and then
    removes any unneeded starting with the smallest in value.'''

    def keys(self, coins):
        return [coin['prevout_hash'] + ':' + str(coin['prevout_n'])
                for coin in coins]

    def choose_buckets(self, buckets, sufficient_funds, penalty_func):
        '''Spend the oldest buckets first.'''
        # Unconfirmed coins are young, not old
        adj_height = lambda height: 99999999 if height == 0 else height
        buckets.sort(key = lambda b: max(adj_height(coin['height'])
                                         for coin in b.coins))
        selected = []
        for bucket in buckets:
            selected.append(bucket)
            if sufficient_funds(selected):
                return strip_unneeded(selected, sufficient_funds)
        else:
            raise NotEnoughFunds()

class CoinChooserRandom(CoinChooserBase):

    def bucket_candidates(self, buckets, sufficient_funds):
        '''Returns a list of bucket sets.'''
        candidates = set()

        # Add all singletons
        for n, bucket in enumerate(buckets):
            if sufficient_funds([bucket]):
                candidates.add((n, ))

        # And now some random ones
        attempts = min(100, (len(buckets) - 1) * 10 + 1)
        permutation = range(len(buckets))
        for i in range(attempts):
            # Get a random permutation of the buckets, and
            # incrementally combine buckets until sufficient
            shuffle(permutation)
            bkts = []
            for count, index in enumerate(permutation):
                bkts.append(buckets[index])
                if sufficient_funds(bkts):
                    candidates.add(tuple(sorted(permutation[:count + 1])))
                    break
            else:
                raise NotEnoughFunds()

        candidates = [[buckets[n] for n in c] for c in candidates]
        return [strip_unneeded(c, sufficient_funds) for c in candidates]

    def choose_buckets(self, buckets, sufficient_funds, penalty_func):
        candidates = self.bucket_candidates(buckets, sufficient_funds)
        penalties = [penalty_func(cand) for cand in candidates]
        winner = candidates[penalties.index(min(penalties))]
        self.print_error("Bucket sets:", len(buckets))
        self.print_error("Winning penalty:", min(penalties))
        return winner

class CoinChooserPrivacy(CoinChooserRandom):
    '''Attempts to better preserve user privacy.  First, if any coin is
    spent from a user address, all coins are.  Compared to spending
    from other addresses to make up an amount, this reduces
    information leakage about sender holdings.  It also helps to
    reduce blockchain UTXO bloat, and reduce future privacy loss that
    would come from reusing that address' remaining UTXOs.  Second, it
    penalizes change that is quite different to the sent amount.
    Third, it penalizes change that is too big. Fourth, it breaks
    large change up into amounts comparable to the spent amount.
    Finally, change is rounded to similar precision to sent amounts.
    Extra change outputs and rounding might raise the transaction fee
    slightly.'''

    def keys(self, coins):
        return [coin['address'] for coin in coins]

    def penalty_func(self, buckets, tx):
        '''Returns a penalty for a candidate set of buckets.'''
        raise NotImplementedError

    def penalty_func(self, tx):
        min_change = min(o[2] for o in tx.outputs) * 0.75
        max_change = max(o[2] for o in tx.outputs) * 1.33
        spent_amount = sum(o[2] for o in tx.outputs)

        def penalty(buckets):
            badness = len(buckets) - 1
            total_input = sum(bucket.value for bucket in buckets)
            change = float(total_input - spent_amount)
            # Penalize change not roughly in output range
            if change < min_change:
                badness += (min_change - change) / (min_change + 10000)
            elif change > max_change:
                badness += (change - max_change) / (max_change + 10000)
                # Penalize large change; 5 BTC excess ~= using 1 more input
                badness += change / (COIN * 5)
            return badness

        return penalty

    def change_amounts(self, tx, count, fee_estimator, dust_threshold):

        # Break change up if bigger than max_change
        output_amounts = [o[2] for o in tx.outputs]
        max_change = max(max(output_amounts) * 1.25, dust_threshold * 10)

        # Use N change outputs
        for n in range(1, count + 1):
            # How much is left if we add this many change outputs?
            change_amount = tx.get_fee() - fee_estimator(n)
            if change_amount // n < max_change:
                break

        # Get a handle on the precision of the output amounts; round our
        # change to look similar
        def trailing_zeroes(val):
            s = str(val)
            return len(s) - len(s.rstrip('0'))

        zeroes = map(trailing_zeroes, output_amounts)
        min_zeroes = min(zeroes)
        max_zeroes = max(zeroes)
        zeroes = range(max(0, min_zeroes - 1), min(max_zeroes + 1, 8) + 1)

        # Calculate change; randomize it a bit if using more than 1 output
        remaining = change_amount
        amounts = []
        while n > 1:
            average = remaining // n
            amount = randint(int(average * 0.7), int(average * 1.3))
            precision = min(choice(zeroes), int(floor(log10(amount))))
            amount = int(round(amount, -precision))
            amounts.append(amount)
            remaining -= amount
            n -= 1

        # Last change output.  Round down to maximum precision but lose
        # no more than 100 satoshis to fees (2dp)
        amount = remaining
        N = min(2, zeroes[0])
        if N:
            amount = int(round(amount, -N))
            if amount > remaining:
                amount -= pow(10, N)
        amounts.append(amount)

        assert sum(amounts) <= change_amount
        assert min(amounts) >= 0

        return amounts

COIN_CHOOSERS = {'Classic': CoinChooserClassic,
                 'Privacy': CoinChooserPrivacy}
<commit_msg>Description tweaks.<commit_after>#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2011 thomasv@gitorious
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from collections import defaultdict, namedtuple
from random import choice, randint, shuffle
from math import floor, log10

from bitcoin import COIN
from transaction import Transaction
from util import NotEnoughFunds, PrintError, profiler

Bucket = namedtuple('Bucket', ['desc', 'size', 'value', 'coins'])

def strip_unneeded(bkts, sufficient_funds):
    '''Remove buckets that are unnecessary in achieving the spend amount'''
    bkts = sorted(bkts, key = lambda bkt: bkt.value)
    for i in range(len(bkts)):
        if not sufficient_funds(bkts[i + 1:]):
            return bkts[i:]
    # Shouldn't get here
    return bkts

class CoinChooserBase(PrintError):

    def keys(self, coins):
        raise NotImplementedError

    def bucketize_coins(self, coins):
        keys = self.keys(coins)
        buckets = defaultdict(list)
        for key, coin in zip(keys, coins):
            buckets[key].append(coin)

        def make_Bucket(desc, coins):
            size = sum(Transaction.estimated_input_size(coin)
                       for coin in coins)
            value = sum(coin['value'] for coin in coins)
            return Bucket(desc, size, value, coins)

        return map(make_Bucket, buckets.keys(), buckets.values())

    def penalty_func(self, tx):
        def penalty(candidate):
            return 0
        return penalty

    def change_amounts(self, tx, count, fee_estimator, dust_threshold):
        # The amount left after adding 1 change output
        return [tx.get_fee() - fee_estimator(1)]

    def change_outputs(self, tx, change_addrs, fee_estimator, dust_threshold):
        amounts = self.change_amounts(tx, len(change_addrs), fee_estimator,
                                      dust_threshold)
        # If change is above dust threshold after accounting for the
        # size of the change output, add it to the transaction.
        dust = sum(amount for amount in amounts if amount < dust_threshold)
        amounts = [amount for amount in amounts if amount >= dust_threshold]
        change = [('address', addr, amount)
                  for addr, amount in zip(change_addrs, amounts)]
        self.print_error('change:', change)
        if dust:
            self.print_error('not keeping dust', dust)
        return change

    def make_tx(self, coins, outputs, change_addrs, fee_estimator,
                dust_threshold):
        '''Select unspent coins to spend to pay outputs.  If the change is
        greater than dust_threshold (after adding the change output to
        the transaction) it is kept, otherwise none is sent and it is
        added to the transaction fee.'''

        # Copy the ouputs so when adding change we don't modify "outputs"
        tx = Transaction.from_io([], outputs[:])
        # Size of the transaction with no inputs and no change
        base_size = tx.estimated_size()
        spent_amount = tx.output_value()

        def sufficient_funds(buckets):
            '''Given a list of buckets, return True if it has enough
            value to pay for the transaction'''
            total_input = sum(bucket.value for bucket in buckets)
            total_size = sum(bucket.size for bucket in buckets) + base_size
            return total_input >= spent_amount + fee_estimator(total_size)

        # Collect the coins into buckets, choose a subset of the buckets
        buckets = self.bucketize_coins(coins)
        buckets = self.choose_buckets(buckets, sufficient_funds,
                                      self.penalty_func(tx))

        tx.inputs = [coin for b in buckets for coin in b.coins]
        tx_size = base_size + sum(bucket.size for bucket in buckets)

        # This takes a count of change outputs and returns a tx fee;
        # each pay-to-bitcoin-address output serializes as 34 bytes
        fee = lambda count: fee_estimator(tx_size + count * 34)
        change = self.change_outputs(tx, change_addrs, fee, dust_threshold)
        tx.outputs.extend(change)

        self.print_error("using %d inputs" % len(tx.inputs))
        self.print_error("using buckets:", [bucket.desc for bucket in buckets])

        return tx

class CoinChooserOldestFirst(CoinChooserBase):
    '''The classic electrum algorithm.  Chooses coins starting with the
    oldest that are sufficient to cover the spent amount, and then
    removes any unneeded starting with the smallest in value.'''

    def keys(self, coins):
        return [coin['prevout_hash'] + ':' + str(coin['prevout_n'])
                for coin in coins]

    def choose_buckets(self, buckets, sufficient_funds, penalty_func):
        '''Spend the oldest buckets first.'''
        # Unconfirmed coins are young, not old
        adj_height = lambda height: 99999999 if height == 0 else height
        buckets.sort(key = lambda b: max(adj_height(coin['height'])
                                         for coin in b.coins))
        selected = []
        for bucket in buckets:
            selected.append(bucket)
            if sufficient_funds(selected):
                return strip_unneeded(selected, sufficient_funds)
        else:
            raise NotEnoughFunds()

class CoinChooserRandom(CoinChooserBase):

    def bucket_candidates(self, buckets, sufficient_funds):
        '''Returns a list of bucket sets.'''
        candidates = set()

        # Add all singletons
        for n, bucket in enumerate(buckets):
            if sufficient_funds([bucket]):
                candidates.add((n, ))

        # And now some random ones
        attempts = min(100, (len(buckets) - 1) * 10 + 1)
        permutation = range(len(buckets))
        for i in range(attempts):
            # Get a random permutation of the buckets, and
            # incrementally combine buckets until sufficient
            shuffle(permutation)
            bkts = []
            for count, index in enumerate(permutation):
                bkts.append(buckets[index])
                if sufficient_funds(bkts):
                    candidates.add(tuple(sorted(permutation[:count + 1])))
                    break
            else:
                raise NotEnoughFunds()

        candidates = [[buckets[n] for n in c] for c in candidates]
        return [strip_unneeded(c, sufficient_funds) for c in candidates]

    def choose_buckets(self, buckets, sufficient_funds, penalty_func):
        candidates = self.bucket_candidates(buckets, sufficient_funds)
        penalties = [penalty_func(cand) for cand in candidates]
        winner = candidates[penalties.index(min(penalties))]
        self.print_error("Bucket sets:", len(buckets))
        self.print_error("Winning penalty:", min(penalties))
        return winner

class CoinChooserPrivacy(CoinChooserRandom):
    '''Attempts to better preserve user privacy.  First, if any coin is
    spent from a user address, all coins are.  Compared to spending
    from other addresses to make up an amount, this reduces
    information leakage about sender holdings.  It also helps to
    reduce blockchain UTXO bloat, and reduce future privacy loss that
    would come from reusing that address' remaining UTXOs.  Second, it
    penalizes change that is quite different to the sent amount.
    Third, it penalizes change that is too big. Fourth, it breaks
    large change up into amounts comparable to the spent amount.
    Finally, change is rounded to similar precision to sent amounts.
    Extra change outputs and rounding might raise the transaction fee
    slightly.  Transaction priority might be less than if older coins
    were chosen.'''

    def keys(self, coins):
        return [coin['address'] for coin in coins]

    def penalty_func(self, buckets, tx):
        '''Returns a penalty for a candidate set of buckets.'''
        raise NotImplementedError

    def penalty_func(self, tx):
        min_change = min(o[2] for o in tx.outputs) * 0.75
        max_change = max(o[2] for o in tx.outputs) * 1.33
        spent_amount = sum(o[2] for o in tx.outputs)

        def penalty(buckets):
            badness = len(buckets) - 1
            total_input = sum(bucket.value for bucket in buckets)
            change = float(total_input - spent_amount)
            # Penalize change not roughly in output range
            if change < min_change:
                badness += (min_change - change) / (min_change + 10000)
            elif change > max_change:
                badness += (change - max_change) / (max_change + 10000)
                # Penalize large change; 5 BTC excess ~= using 1 more input
                badness += change / (COIN * 5)
            return badness

        return penalty

    def change_amounts(self, tx, count, fee_estimator, dust_threshold):

        # Break change up if bigger than max_change
        output_amounts = [o[2] for o in tx.outputs]
        max_change = max(max(output_amounts) * 1.25, dust_threshold * 10)

        # Use N change outputs
        for n in range(1, count + 1):
            # How much is left if we add this many change outputs?
            change_amount = tx.get_fee() - fee_estimator(n)
            if change_amount // n < max_change:
                break

        # Get a handle on the precision of the output amounts; round our
        # change to look similar
        def trailing_zeroes(val):
            s = str(val)
            return len(s) - len(s.rstrip('0'))

        zeroes = map(trailing_zeroes, output_amounts)
        min_zeroes = min(zeroes)
        max_zeroes = max(zeroes)
        zeroes = range(max(0, min_zeroes - 1), min(max_zeroes + 1, 8) + 1)

        # Calculate change; randomize it a bit if using more than 1 output
        remaining = change_amount
        amounts = []
        while n > 1:
            average = remaining // n
            amount = randint(int(average * 0.7), int(average * 1.3))
            precision = min(choice(zeroes), int(floor(log10(amount))))
            amount = int(round(amount, -precision))
            amounts.append(amount)
            remaining -= amount
            n -= 1

        # Last change output.  Round down to maximum precision but lose
        # no more than 100 satoshis to fees (2dp)
        amount = remaining
        N = min(2, zeroes[0])
        if N:
            amount = int(round(amount, -N))
            if amount > remaining:
                amount -= pow(10, N)
        amounts.append(amount)

        assert sum(amounts) <= change_amount
        assert min(amounts) >= 0

        return amounts

COIN_CHOOSERS = {'Oldest First': CoinChooserOldestFirst,
                 'Privacy': CoinChooserPrivacy}
<|endoftext|>


<commit_before>import re
import time, threading
import os

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability
from twilio.rest import TwilioRestClient
import datetime
import helper

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

db = {}

    "pcv4":0,
db["+16172099765"] = {
    "name":"Santa Claus",
    "dob":datetime.datetime(2014, 7, 05),
    "zipcode":"02139",
    "bcg":1,
    "hepb1":1,
    "hepb2":1,
    "hepb3":0,
    "polio1":1,
    "polio2":1,
    "polio3":1,
    "polio4":0,
    "polio5":0,
    "polio6":0,
    "polio7":0,
    "polio8":0,
    "dtp1":0,
    "dtp2":0,
    "dtp3":0,
    "dtp4":0,
    "dtp5":0,
    "tdap":0,
    "hib1":0,
    "hib2":0,
    "hib3":0,
    "hib4":0,
    "pcv1":1,
    "pcv2":1,
    "pcv3":0,
    "rv1":1,
    "rv2":1,
    "rv3":0,
    "measles":0,
    "mmr1":0,
    "mmr2":0,
    "var1":0,
    "var2":0,
    "hepa1":0,
    "hepa2":0,
    "typhoid":0,
    "hpv1":0,
    "hpv2":0,
    "hpv3":0,
}

vaccineCode = {
    "9e4f":"bcg",
    "3ifk":"hepb1",
    "36dg":"hepb2",
    "eijl":"hepb3",
    "q52y":"polio1",
    "u0sv":"polio2",
    "26s2":"dtp1",
    "qe10":"dtp2",
}

vaccineTimes = {
    "bcg":datetime.timedelta(days = 0),
    "hepb1":datetime.timedelta(days = 0),
    "hepb2":datetime.timedelta(days = 45),
    "hepb3":datetime.timedelta(days = 185),
    "polio1":datetime.timedelta(days = 0),
    "polio2":datetime.timedelta(days = 45),
    "polio3":datetime.timedelta(days = 70),
    "polio4":datetime.timedelta(days = 100),
    "polio5":datetime.timedelta(days = 185),
    "polio6":datetime.timedelta(days = 275),
    "polio7":datetime.timedelta(days = 365),
    "polio8":datetime.timedelta(days = 1460),
    "dtp1":datetime.timedelta(days = 45),
    "dtp2":datetime.timedelta(days = 70),
    "dtp3":datetime.timedelta(days = 100),
    "dtp4":datetime.timedelta(days = 455),
    "dtp5":datetime.timedelta(days = 1460),
    "tdap":datetime.timedelta(days = 4015),
    "hib1":datetime.timedelta(days = 45),
    "hib2":datetime.timedelta(days = 70),
    "hib3":datetime.timedelta(days = 100),
    "hib4":datetime.timedelta(days = 365),
    "pcv1":datetime.timedelta(days = 45),
    "pcv2":datetime.timedelta(days = 70),
    "pcv3":datetime.timedelta(days = 100),
    "pcv4":datetime.timedelta(days = 365),
    "rv1":datetime.timedelta(days = 45),
    "rv2":datetime.timedelta(days = 70),
    "rv3":datetime.timedelta(days = 100),
    "measles":datetime.timedelta(days = 270),
    "mmr1":datetime.timedelta(days = 365),
    "mmr2":datetime.timedelta(days = 1460),
    "var1":datetime.timedelta(days = 455),
    "var2":datetime.timedelta(days = 1460),
    "hepa1":datetime.timedelta(days = 365),
    "hepa2":datetime.timedelta(days = 365),
    "typhoid":datetime.timedelta(days = 730),
    "hpv1":datetime.timedelta(days = 4015),
    "hpv2":datetime.timedelta(days = 4015),
    "hpv3":datetime.timedelta(days = 4015),
}

def foo():
    client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID', None), os.environ.get('TWILIO_AUTH_TOKEN', None))
    print(time.ctime())
    #message = client.messages.create(to="+17034590011", from_="+14088053907", body="Hello there!")
    threading.Timer(10, foo).start()

foo()

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Congratulations! You deployed the Twilio Hackpack "
                 "for Heroku and Flask.")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    from_number = request.values.get('From', None)
    body = request.form['Body']
    reg = re.compile('[r|R]eg name:[\s|\S]+ dob:[\s|\S]+ zipcode:[\s|\S]+')
    if reg.match(body) is not None:
        if from_number in db:
            response.sms("User already exists!")
        else:
            input = re.split(' name:| dob:| zipcode:', body)
            name = input[1]
            dobarray = input[2].split('/')
            month = dobarray[0]
            day = dobarray[1]
            year = dobarray[2]
            dob = datetime.datetime(int(year), int(month), int(day))
            zipcode = input[3]
            db[from_number] = {
                "name":name,
                "dob":dob,
                "zipcode":zipcode,
                "bcg":0,
                "hepb1":0,
                "hepb2":0,
                "hepb3":0,
                "polio1":0,
                "polio2":0,
                "polio3":0,
                "polio4":0,
                "polio5":0,
                "polio6":0,
                "polio7":0,
                "polio8":0,
                "dtp1":0,
                "dtp2":0,
                "dtp3":0,
                "dtp4":0,
                "dtp5":0,
                "tdap":0,
                "hib1":0,
                "hib2":0,
                "hib3":0,
                "hib4":0,
                "pcv1":0,
                "pcv2":0,
                "pcv3":0,
                "pcv4":0,
                "rv1":0,
                "rv2":0,
                "rv3":0,
                "measles":0,
                "mmr1":0,
                "mmr2":0,
                "var1":0,
                "var2":0,
                "hepa1":0,
                "hepa2":0,
                "typhoid":0,
                "hpv1":0,
                "hpv2":0,
                "hpv3":0,
            }
            response.sms(helper.confirmationMsg(name, dob, zipcode))
    elif from_number not in db:
        response.sms("Please register first!")
    elif body in vaccineCode:
        if db[from_number][vaccineCode[body]] != 0:
            response.sms("Already received vaccine!")
        else:
            db[from_number][vaccineCode[body]] = datetime.datetime.now()
            response.sms(helper.responseFromVaccine(vaccineCode[body]))

    elif body == "info":
        response.sms(helper.info(db[from_number], vaccineTimes, dob))

    else:
        response.sms("Error: Ill-formed Submission")

    return str(response)

# Twilio Client demo template
@app.route('/client')
def client():
    configuration_error = None
    for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
                'TWILIO_CALLER_ID'):
        if not app.config.get(key, None):
            configuration_error = "Missing from local_settings.py: " \
                                  "{0}".format(key)
            token = None

    if not configuration_error:
        capability = TwilioCapability(app.config['TWILIO_ACCOUNT_SID'],
                                      app.config['TWILIO_AUTH_TOKEN'])
        capability.allow_client_incoming("joey_ramone")
        capability.allow_client_outgoing(app.config['TWILIO_APP_SID'])
        token = capability.generate()
    params = {'token': token}
    return render_template('client.html', params=params,
                           configuration_error=configuration_error)

@app.route('/client/incoming', methods=['POST'])
def client_incoming():
    try:
        from_number = request.values.get('PhoneNumber', None)

        resp = twiml.Response()

        if not from_number:
            resp.say("Your app is missing a Phone Number. "
                     "Make a request with a Phone Number to make outgoing "
                     "calls with the Twilio hack pack.")
            return str(resp)

        if 'TWILIO_CALLER_ID' not in app.config:
            resp.say(
                "Your app is missing a Caller ID parameter. "
                "Please add a Caller ID to make outgoing calls with Twilio "
                "Client")
            return str(resp)

        with resp.dial(callerId=app.config['TWILIO_CALLER_ID']) as r:
            # If we have a number, and it looks like a phone number:
            if from_number and re.search('^[\d\(\)\- \+]+$', from_number):
                r.number(from_number)
            else:
                r.say("We couldn't find a phone number to dial. Make sure "
                      "you are sending a Phone Number when you make a "
                      "request with Twilio Client")

        return str(resp)

    except:
        resp = twiml.Response()
        resp.say("An error occurred. Check your debugger at twilio dot com "
                 "for more information.")
        return str(resp)

# Installation success page
@app.route('/')
def index():
    params = {
        'Voice Request URL': url_for('.voice', _external=True),
        'SMS Request URL': url_for('.sms', _external=True),
        'Client URL': url_for('.client', _external=True)}
    return render_template('index.html', params=params,
                           configuration_error=None)
<commit_msg>fixed directory datetimes<commit_after>import re
import time, threading
import os

from flask import Flask
from flask import render_template
from flask import url_for
from flask import request

from twilio import twiml
from twilio.util import TwilioCapability
from twilio.rest import TwilioRestClient
import datetime
import helper

# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')

db = {}

db["+16172099765"] = {
    "name":"Santa Claus",
    "dob":datetime.datetime(2014, 7, 05),
    "zipcode":"02139",
    "bcg":datetime.datetime(2014, 7, 06),
    "hepb1":datetime.datetime(2014, 7, 06),
    "hepb2":datetime.datetime(2014, 8, 20),
    "hepb3":0,
    "polio1":datetime.datetime(2014, 7, 06),
    "polio2":datetime.datetime(2014, 8, 20),
    "polio3":datetime.datetime(2014, 9, 20),
    "polio4":0,
    "polio5":0,
    "polio6":0,
    "polio7":0,
    "polio8":0,
    "dtp1":0,
    "dtp2":0,
    "dtp3":0,
    "dtp4":0,
    "dtp5":0,
    "tdap":0,
    "hib1":0,
    "hib2":0,
    "hib3":0,
    "hib4":0,
    "pcv1":datetime.datetime(2014, 8, 20),
    "pcv2":datetime.datetime(2014, 9, 20),
    "pcv3":0,
    "pcv4":0,
    "rv1":datetime.datetime(2014, 8, 20),
    "rv2":datetime.datetime(2014, 9, 20),
    "rv3":0,
    "measles":0,
    "mmr1":0,
    "mmr2":0,
    "var1":0,
    "var2":0,
    "hepa1":0,
    "hepa2":0,
    "typhoid":0,
    "hpv1":0,
    "hpv2":0,
    "hpv3":0,
}

vaccineCode = {
    "9e4f":"bcg",
    "3ifk":"hepb1",
    "36dg":"hepb2",
    "eijl":"hepb3",
    "q52y":"polio1",
    "u0sv":"polio2",
    "26s2":"dtp1",
    "qe10":"dtp2",
}

vaccineTimes = {
    "bcg":datetime.timedelta(days = 0),
    "hepb1":datetime.timedelta(days = 0),
    "hepb2":datetime.timedelta(days = 45),
    "hepb3":datetime.timedelta(days = 185),
    "polio1":datetime.timedelta(days = 0),
    "polio2":datetime.timedelta(days = 45),
    "polio3":datetime.timedelta(days = 70),
    "polio4":datetime.timedelta(days = 100),
    "polio5":datetime.timedelta(days = 185),
    "polio6":datetime.timedelta(days = 275),
    "polio7":datetime.timedelta(days = 365),
    "polio8":datetime.timedelta(days = 1460),
    "dtp1":datetime.timedelta(days = 45),
    "dtp2":datetime.timedelta(days = 70),
    "dtp3":datetime.timedelta(days = 100),
    "dtp4":datetime.timedelta(days = 455),
    "dtp5":datetime.timedelta(days = 1460),
    "tdap":datetime.timedelta(days = 4015),
    "hib1":datetime.timedelta(days = 45),
    "hib2":datetime.timedelta(days = 70),
    "hib3":datetime.timedelta(days = 100),
    "hib4":datetime.timedelta(days = 365),
    "pcv1":datetime.timedelta(days = 45),
    "pcv2":datetime.timedelta(days = 70),
    "pcv3":datetime.timedelta(days = 100),
    "pcv4":datetime.timedelta(days = 365),
    "rv1":datetime.timedelta(days = 45),
    "rv2":datetime.timedelta(days = 70),
    "rv3":datetime.timedelta(days = 100),
    "measles":datetime.timedelta(days = 270),
    "mmr1":datetime.timedelta(days = 365),
    "mmr2":datetime.timedelta(days = 1460),
    "var1":datetime.timedelta(days = 455),
    "var2":datetime.timedelta(days = 1460),
    "hepa1":datetime.timedelta(days = 365),
    "hepa2":datetime.timedelta(days = 365),
    "typhoid":datetime.timedelta(days = 730),
    "hpv1":datetime.timedelta(days = 4015),
    "hpv2":datetime.timedelta(days = 4015),
    "hpv3":datetime.timedelta(days = 4015),
}

def foo():
    client = TwilioRestClient(os.environ.get('TWILIO_ACCOUNT_SID', None), os.environ.get('TWILIO_AUTH_TOKEN', None))
    print(time.ctime())
    #message = client.messages.create(to="+17034590011", from_="+14088053907", body="Hello there!")
    threading.Timer(10, foo).start()

foo()

# Voice Request URL
@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = twiml.Response()
    response.say("Congratulations! You deployed the Twilio Hackpack "
                 "for Heroku and Flask.")
    return str(response)

# SMS Request URL
@app.route('/sms', methods=['GET', 'POST'])
def sms():
    response = twiml.Response()
    from_number = request.values.get('From', None)
    body = request.form['Body']
    reg = re.compile('[r|R]eg name:[\s|\S]+ dob:[\s|\S]+ zipcode:[\s|\S]+')
    if reg.match(body) is not None:
        if from_number in db:
            response.sms("User already exists!")
        else:
            input = re.split(' name:| dob:| zipcode:', body)
            name = input[1]
            dobarray = input[2].split('/')
            month = dobarray[0]
            day = dobarray[1]
            year = dobarray[2]
            dob = datetime.datetime(int(year), int(month), int(day))
            zipcode = input[3]
            db[from_number] = {
                "name":name,
                "dob":dob,
                "zipcode":zipcode,
                "bcg":0,
                "hepb1":0,
                "hepb2":0,
                "hepb3":0,
                "polio1":0,
                "polio2":0,
                "polio3":0,
                "polio4":0,
                "polio5":0,
                "polio6":0,
                "polio7":0,
                "polio8":0,
                "dtp1":0,
                "dtp2":0,
                "dtp3":0,
                "dtp4":0,
                "dtp5":0,
                "tdap":0,
                "hib1":0,
                "hib2":0,
                "hib3":0,
                "hib4":0,
                "pcv1":0,
                "pcv2":0,
                "pcv3":0,
                "pcv4":0,
                "rv1":0,
                "rv2":0,
                "rv3":0,
                "measles":0,
                "mmr1":0,
                "mmr2":0,
                "var1":0,
                "var2":0,
                "hepa1":0,
                "hepa2":0,
                "typhoid":0,
                "hpv1":0,
                "hpv2":0,
                "hpv3":0,
            }
            response.sms(helper.confirmationMsg(name, dob, zipcode))
    elif from_number not in db:
        response.sms("Please register first!")
    elif body in vaccineCode:
        if db[from_number][vaccineCode[body]] != 0:
            response.sms("Already received vaccine!")
        else:
            db[from_number][vaccineCode[body]] = datetime.datetime.now()
            response.sms(helper.responseFromVaccine(vaccineCode[body]))

    elif body == "info":
        response.sms(helper.info(db[from_number], vaccineTimes, dob))

    else:
        response.sms("Error: Ill-formed Submission")

    return str(response)

# Twilio Client demo template
@app.route('/client')
def client():
    configuration_error = None
    for key in ('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN', 'TWILIO_APP_SID',
                'TWILIO_CALLER_ID'):
        if not app.config.get(key, None):
            configuration_error = "Missing from local_settings.py: " \
                                  "{0}".format(key)
            token = None

    if not configuration_error:
        capability = TwilioCapability(app.config['TWILIO_ACCOUNT_SID'],
                                      app.config['TWILIO_AUTH_TOKEN'])
        capability.allow_client_incoming("joey_ramone")
        capability.allow_client_outgoing(app.config['TWILIO_APP_SID'])
        token = capability.generate()
    params = {'token': token}
    return render_template('client.html', params=params,
                           configuration_error=configuration_error)

@app.route('/client/incoming', methods=['POST'])
def client_incoming():
    try:
        from_number = request.values.get('PhoneNumber', None)

        resp = twiml.Response()

        if not from_number:
            resp.say("Your app is missing a Phone Number. "
                     "Make a request with a Phone Number to make outgoing "
                     "calls with the Twilio hack pack.")
            return str(resp)

        if 'TWILIO_CALLER_ID' not in app.config:
            resp.say(
                "Your app is missing a Caller ID parameter. "
                "Please add a Caller ID to make outgoing calls with Twilio "
                "Client")
            return str(resp)

        with resp.dial(callerId=app.config['TWILIO_CALLER_ID']) as r:
            # If we have a number, and it looks like a phone number:
            if from_number and re.search('^[\d\(\)\- \+]+$', from_number):
                r.number(from_number)
            else:
                r.say("We couldn't find a phone number to dial. Make sure "
                      "you are sending a Phone Number when you make a "
                      "request with Twilio Client")

        return str(resp)

    except:
        resp = twiml.Response()
        resp.say("An error occurred. Check your debugger at twilio dot com "
                 "for more information.")
        return str(resp)

# Installation success page
@app.route('/')
def index():
    params = {
        'Voice Request URL': url_for('.voice', _external=True),
        'SMS Request URL': url_for('.sms', _external=True),
        'Client URL': url_for('.client', _external=True)}
    return render_template('index.html', params=params,
                           configuration_error=None)
<|endoftext|>