<commit_before># coding=utf-8

from __future__ import unicode_literals
from __future__ import print_function

import os
import sys
import argparse

from faker import Faker, Factory, documentor
from faker import VERSION
from faker.config import AVAILABLE_LOCALES, DEFAULT_LOCALE, META_PROVIDERS_MODULES

if sys.version < '3':
    text_type = unicode
    binary_type = str
else:
    text_type = str
    binary_type = bytes

__author__ = 'joke2k'

def print_provider(doc, provider, formatters, excludes=None, output=None):
    output = output or sys.stdout
    if excludes is None:
        excludes = []

    print(file=output)
    print("### {0}".format(
          doc.get_provider_name(provider)), file=output)
    print(file=output)

    for signature, example in formatters.items():
        if signature in excludes:
            continue
        try:
            lines = text_type(example).expandtabs().splitlines()
        except UnicodeDecodeError:
            # The example is actually made of bytes.
            # We could coerce to bytes, but that would fail anyway when we wiil
            # try to `print` the line.
            lines = ["<bytes>"]
        except UnicodeEncodeError:
            raise Exception('error on "{0}" with value "{1}"'.format(
                            signature, example))
        margin = max(30, doc.max_name_len+1)
        remains = 150 - margin
        separator = '#'
        for line in lines:
            for i in range(0, (len(line) // remains) + 1):
                print("\t{fake:<{margin}}{separator} {example}".format(
                    fake=signature,
                    separator=separator,
                    example=line[i*remains:(i+1)*remains],
                    margin=margin
                ), file=output)
                signature = separator = ' '

def print_doc(provider_or_field=None,
              args=None, lang=DEFAULT_LOCALE, output=None, includes=None):
    args = args or []
    output = output or sys.stdout
    fake = Faker(locale=lang, includes=includes)

    from faker.providers import BaseProvider
    base_provider_formatters = [f for f in dir(BaseProvider)]

    if provider_or_field:
        if '.' in provider_or_field:
            parts = provider_or_field.split('.')
            locale = parts[-2] if parts[-2] in AVAILABLE_LOCALES else lang
            fake = Factory.create(locale, providers=[provider_or_field], includes=includes)
            doc = documentor.Documentor(fake)
            doc.already_generated = base_provider_formatters
            print_provider(
                doc,
                fake.get_providers()[0],
                doc.get_provider_formatters(fake.get_providers()[0]),
                output=output)
        else:
            try:
                print(fake.format(provider_or_field, *args), end='', file=output)
            except AttributeError:
                raise ValueError('No faker found for "{0}({1})"'.format(
                    provider_or_field, args))

    else:
        doc = documentor.Documentor(fake)

        formatters = doc.get_formatters(with_args=True, with_defaults=True)

        for provider, fakers in formatters:

            print_provider(doc, provider, fakers, output=output)

        for language in AVAILABLE_LOCALES:
            if language == lang:
                continue
            print(file=output)
            print('## LANGUAGE {0}'.format(language), file=output)
            fake = Faker(locale=language)
            d = documentor.Documentor(fake)

            for p, fs in d.get_formatters(with_args=True, with_defaults=True,
                                          locale=language,
                                          excludes=base_provider_formatters):
                print_provider(d, p, fs, output=output)

class Command(object):

    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

    def execute(self):
        """
        Given the command-line arguments, this creates a parser appropriate
        to that command, and runs it.
        """

        # retrieve default language from system environment
        default_locale = os.environ.get('LANG', 'en_US').split('.')[0]
        if default_locale not in AVAILABLE_LOCALES:
            default_locale = DEFAULT_LOCALE

        formatter_class = argparse.RawDescriptionHelpFormatter
        parser = argparse.ArgumentParser(
            prog=self.prog_name,
            description='{0} version {1}'.format(self.prog_name, VERSION),
            formatter_class=formatter_class)

        parser.add_argument("--version", action="version",
                            version="%(prog)s {0}".format(VERSION))

        parser.add_argument('-o', metavar="output",
                            type=argparse.FileType('w'),
                            default=sys.stdout,
                            help="redirect output to a file")

        parser.add_argument('-l', '--lang',
                            choices=AVAILABLE_LOCALES,
                            default=default_locale)
        parser.add_argument('-r', '--repeat',
                            default=1, type=int)
        parser.add_argument('-s', '--sep',
                            default='\n')

        parser.add_argument('-i', '--include', default=META_PROVIDERS_MODULES, nargs='*')

        parser.add_argument('fake',
                            action='store',
                            nargs='?',
                            help="name of the fake to generate output for "
                                 "(e.g. profile)")

        parser.add_argument('fake_args',
                            metavar="fake argument",
                            action='store',
                            nargs='*',
                            help="optional arguments to pass to the fake "
                                 "(e.g. the profile fake takes an optional "
                                 "list of comma separated field names as the "
                                 "first argument)")

        arguments = parser.parse_args(self.argv[1:])

        for i in range(arguments.repeat):

            print_doc(arguments.fake,
                      arguments.fake_args,
                      lang=arguments.lang,
                      output=arguments.o,
                      includes=arguments.include
                      )
            print(arguments.sep, file=arguments.o)

            if not arguments.fake:
                # repeat not supported for all docs
                break

def execute_from_command_line(argv=None):
    """A simple method that runs a Command."""
    if sys.stdout.encoding is None:
        print('please set python env PYTHONIOENCODING=UTF-8, example: '
              'export PYTHONIOENCODING=UTF-8, when writing to stdout',
              file=sys.stderr)
        exit(1)

    command = Command(argv)
    command.execute()
<commit_msg>Specify help text for -l -r -s and -i<commit_after># coding=utf-8

from __future__ import unicode_literals
from __future__ import print_function

import os
import sys
import argparse

from faker import Faker, Factory, documentor
from faker import VERSION
from faker.config import AVAILABLE_LOCALES, DEFAULT_LOCALE, META_PROVIDERS_MODULES

if sys.version < '3':
    text_type = unicode
    binary_type = str
else:
    text_type = str
    binary_type = bytes

__author__ = 'joke2k'

def print_provider(doc, provider, formatters, excludes=None, output=None):
    output = output or sys.stdout
    if excludes is None:
        excludes = []

    print(file=output)
    print("### {0}".format(
          doc.get_provider_name(provider)), file=output)
    print(file=output)

    for signature, example in formatters.items():
        if signature in excludes:
            continue
        try:
            lines = text_type(example).expandtabs().splitlines()
        except UnicodeDecodeError:
            # The example is actually made of bytes.
            # We could coerce to bytes, but that would fail anyway when we wiil
            # try to `print` the line.
            lines = ["<bytes>"]
        except UnicodeEncodeError:
            raise Exception('error on "{0}" with value "{1}"'.format(
                            signature, example))
        margin = max(30, doc.max_name_len+1)
        remains = 150 - margin
        separator = '#'
        for line in lines:
            for i in range(0, (len(line) // remains) + 1):
                print("\t{fake:<{margin}}{separator} {example}".format(
                    fake=signature,
                    separator=separator,
                    example=line[i*remains:(i+1)*remains],
                    margin=margin
                ), file=output)
                signature = separator = ' '

def print_doc(provider_or_field=None,
              args=None, lang=DEFAULT_LOCALE, output=None, includes=None):
    args = args or []
    output = output or sys.stdout
    fake = Faker(locale=lang, includes=includes)

    from faker.providers import BaseProvider
    base_provider_formatters = [f for f in dir(BaseProvider)]

    if provider_or_field:
        if '.' in provider_or_field:
            parts = provider_or_field.split('.')
            locale = parts[-2] if parts[-2] in AVAILABLE_LOCALES else lang
            fake = Factory.create(locale, providers=[provider_or_field], includes=includes)
            doc = documentor.Documentor(fake)
            doc.already_generated = base_provider_formatters
            print_provider(
                doc,
                fake.get_providers()[0],
                doc.get_provider_formatters(fake.get_providers()[0]),
                output=output)
        else:
            try:
                print(fake.format(provider_or_field, *args), end='', file=output)
            except AttributeError:
                raise ValueError('No faker found for "{0}({1})"'.format(
                    provider_or_field, args))

    else:
        doc = documentor.Documentor(fake)

        formatters = doc.get_formatters(with_args=True, with_defaults=True)

        for provider, fakers in formatters:

            print_provider(doc, provider, fakers, output=output)

        for language in AVAILABLE_LOCALES:
            if language == lang:
                continue
            print(file=output)
            print('## LANGUAGE {0}'.format(language), file=output)
            fake = Faker(locale=language)
            d = documentor.Documentor(fake)

            for p, fs in d.get_formatters(with_args=True, with_defaults=True,
                                          locale=language,
                                          excludes=base_provider_formatters):
                print_provider(d, p, fs, output=output)

class Command(object):

    def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

    def execute(self):
        """
        Given the command-line arguments, this creates a parser appropriate
        to that command, and runs it.
        """

        # retrieve default language from system environment
        default_locale = os.environ.get('LANG', 'en_US').split('.')[0]
        if default_locale not in AVAILABLE_LOCALES:
            default_locale = DEFAULT_LOCALE

        formatter_class = argparse.RawDescriptionHelpFormatter
        parser = argparse.ArgumentParser(
            prog=self.prog_name,
            description='{0} version {1}'.format(self.prog_name, VERSION),
            formatter_class=formatter_class)

        parser.add_argument("--version", action="version",
                            version="%(prog)s {0}".format(VERSION))

        parser.add_argument('-o', metavar="output",
                            type=argparse.FileType('w'),
                            default=sys.stdout,
                            help="redirect output to a file")

        parser.add_argument('-l', '--lang',
                            choices=AVAILABLE_LOCALES,
                            default=default_locale,
                            help="specify the language for a localized "
                            "provider (e.g. de_DE)")
        parser.add_argument('-r', '--repeat',
                            default=1,
                            type=int,
                            help="generate the specified number of outputs")
        parser.add_argument('-s', '--sep',
                            default='\n',
                            help="use the specified separator after each "
                            "output")

        parser.add_argument('-i',
                            '--include',
                            default=META_PROVIDERS_MODULES,
                            nargs='*',
                            help="list of additional custom providers to "
                            "user, given as the import path of the module "
                            "containing your Provider class (not the provider "
                            "class itself)")

        parser.add_argument('fake',
                            action='store',
                            nargs='?',
                            help="name of the fake to generate output for "
                                 "(e.g. profile)")

        parser.add_argument('fake_args',
                            metavar="fake argument",
                            action='store',
                            nargs='*',
                            help="optional arguments to pass to the fake "
                                 "(e.g. the profile fake takes an optional "
                                 "list of comma separated field names as the "
                                 "first argument)")

        arguments = parser.parse_args(self.argv[1:])

        for i in range(arguments.repeat):

            print_doc(arguments.fake,
                      arguments.fake_args,
                      lang=arguments.lang,
                      output=arguments.o,
                      includes=arguments.include
                      )
            print(arguments.sep, file=arguments.o)

            if not arguments.fake:
                # repeat not supported for all docs
                break

def execute_from_command_line(argv=None):
    """A simple method that runs a Command."""
    if sys.stdout.encoding is None:
        print('please set python env PYTHONIOENCODING=UTF-8, example: '
              'export PYTHONIOENCODING=UTF-8, when writing to stdout',
              file=sys.stderr)
        exit(1)

    command = Command(argv)
    command.execute()
<|endoftext|>


<commit_before>from ops import *
import random
random.seed(0.2)

def getAllWords():
    with open('data/vocabulary.dat') as f:
        words = f.read().splitlines()
    wordList = list(words)
    wordDict = dict()
    for i in range(len(words)):
        wordDict[wordList[i]] = i
    return [wordList, wordDict]

ASCII_DIM = 8
WORD_LEN = 40
WORD_BATCH_SIZE = 20
EMBEDDING_SIZE = 64
EMBEDDINGS = loadFromFile("savedEmbeddings/embeddings.pkl")
CHAR_LIST = list("abcdefghijklmnopqrstuvwxyz")

WORDS, WORD_TO_NUM = getAllWords()
TEST_WORDS = random.sample(WORDS, 50)
WORDS = [word for word in WORDS if not word in TEST_WORDS]

VOCAB_SIZE = len(WORDS)

LAYER_SIZES = [WORD_LEN, 128, 512, 1024, 1536, 2048, 1536, 1024, 512, 128, 64]

with tf.variable_scope("vars"):
    weights = []
    biases = []

    for i in range(len(LAYER_SIZES)-1):
        weights.append(weightVariable([LAYER_SIZES[i], LAYER_SIZES[i+1]], name = "w%s"%i))
        biases.append(biasVariable([LAYER_SIZES[i+1]], name = "b%s"%i))

def get_placeholders():
    word_ascii = tf.placeholder(dtype = tf.float32, shape = [None, WORD_LEN])
    embed_true = tf.placeholder(dtype = tf.float32, shape=[None, EMBEDDING_SIZE])
    keep_prob = tf.placeholder(dtype = tf.float32)

    return [word_ascii, embed_true, keep_prob]

# convert a character to ascii code (8 bits)
def prepareChar(ch):
    return (CHAR_LIST.index(ch)+1)/len(CHAR_LIST)
    # if len(ch) != 1:
    #     raise ValueError("Please supply a single character for the ascii lookup")
    # binStr = "{0:08b}".format(ord(ch))
    # binList = []
    # for d in binStr:
    #     binList.append(int(d))

    # return binList

# returns an ascii matrix for word
def prepareWord(word):
    wordArr = []
    i = 0
    # print(word)
    while i < len(word) and len(wordArr) < WORD_LEN:
        ch = word[i]
        if not ch.isalpha():
            i += 1
            continue
        wordArr.append(prepareChar(ch))
        i += 1

    if len(wordArr) < WORD_LEN:
        wordArr += [0]*(WORD_LEN - len(wordArr))

    # return np.array(wordArr).reshape([1,-1])
    return wordArr

def prepWordBatch(words):
    wordArrs = []
    word_embeddings = []
    for word in words:
        mat = prepareWord(word)
        # print(word, mat.shape)
        wordArrs.append(mat)
        # if wordArrs is None:
        #     wordArrs = mat
        # else:
        #     wordArrs = np.concatenate([wordArrs, mat], axis=0)

        word_embeddings.append(EMBEDDINGS[WORD_TO_NUM[word]])

    return [np.array(wordArrs), np.array(word_embeddings)]

def reader_model(word_ascii, keep_prob):
    h = None
    for i in range(len(weights)):
        if i == 0:
            h = tf.matmul(word_ascii, weights[i])
        else:
            h = tf.matmul(h, weights[i])

        h += biases[i]

        if i == len(weights)-1:
            h = tf.nn.tanh(h)
        else:
            h = tf.nn.relu(h)

        norm = tf.sqrt(tf.reduce_sum(tf.square(h), 1, keep_dims=True))
        output = h/norm

    return output

def loss_optim(embed_predict, embed_true):
    # losses = tf.reduce_sum(tf.abs(embed_predict - embed_true),axis=0)
    # calculating loss based on dot product
    similarities = tf.diag_part(tf.matmul(embed_predict, tf.transpose(embed_true)))
    summarize(similarities, varName = "similarities")
    loss = 1/tf.reduce_sum(similarities)

    tf.summary.scalar("raw_loss", loss)

    # l2_loss = 0
    # all_vars = tf.trainable_variables()
    # for v in all_vars:
    #     l2_loss += alpha * tf.nn.l2_loss(v)

    # tf.summary.scalar("l2_loss", l2_loss)
    # loss += l2_loss
    # tf.summary.scalar("total_loss", loss)

    output_similarity = tf.matmul(embed_predict, tf.transpose(embed_predict))
    summarize(output_similarity, "output_similarity")
    # loss += 0.01 * tf.reduce_sum(output_similarity)

    optim = tf.train.AdamOptimizer(learning_rate, beta).minimize(loss)
    # optim = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    # optim = tf.train.AdagradOptimizer(0.1).minimize(loss)
    # optim = tf.train.ProximalGradientDescentOptimizer(learning_rate).minimize(loss)
    return [loss, optim]<commit_msg>tweak<commit_after>from ops import *
import random
random.seed(0.2)

def getAllWords():
    with open('data/vocabulary.dat') as f:
        words = f.read().splitlines()
    wordList = list(words)
    wordDict = dict()
    for i in range(len(words)):
        wordDict[wordList[i]] = i
    return [wordList, wordDict]

ASCII_DIM = 8
WORD_LEN = 40
WORD_BATCH_SIZE = 20
EMBEDDING_SIZE = 64
EMBEDDINGS = loadFromFile("savedEmbeddings/embeddings.pkl")
CHAR_LIST = list("abcdefghijklmnopqrstuvwxyz")

WORDS, WORD_TO_NUM = getAllWords()
TEST_WORDS = random.sample(WORDS, 50)
WORDS = [word for word in WORDS if not word in TEST_WORDS]

VOCAB_SIZE = len(WORDS)

LAYER_SIZES = [WORD_LEN, 128, 512, 1024, 1536, 2048, 1536, 1024, 512, 128, 64]

with tf.variable_scope("vars"):
    weights = []
    biases = []

    for i in range(len(LAYER_SIZES)-1):
        weights.append(weightVariable([LAYER_SIZES[i], LAYER_SIZES[i+1]], name = "w%s"%i))
        biases.append(biasVariable([LAYER_SIZES[i+1]], name = "b%s"%i))

def get_placeholders():
    word_ascii = tf.placeholder(dtype = tf.float32, shape = [None, WORD_LEN])
    embed_true = tf.placeholder(dtype = tf.float32, shape=[None, EMBEDDING_SIZE])
    keep_prob = tf.placeholder(dtype = tf.float32)

    return [word_ascii, embed_true, keep_prob]

# convert a character to ascii code (8 bits)
def prepareChar(ch):
    return (CHAR_LIST.index(ch)+1)/len(CHAR_LIST)
    # if len(ch) != 1:
    #     raise ValueError("Please supply a single character for the ascii lookup")
    # binStr = "{0:08b}".format(ord(ch))
    # binList = []
    # for d in binStr:
    #     binList.append(int(d))

    # return binList

# returns an ascii matrix for word
def prepareWord(word):
    wordArr = []
    i = 0
    # print(word)
    while i < len(word) and len(wordArr) < WORD_LEN:
        ch = word[i]
        if not ch.isalpha():
            i += 1
            continue
        wordArr.append(prepareChar(ch))
        i += 1

    if len(wordArr) < WORD_LEN:
        wordArr += [0]*(WORD_LEN - len(wordArr))

    # return np.array(wordArr).reshape([1,-1])
    return wordArr

def prepWordBatch(words):
    wordArrs = []
    word_embeddings = []
    for word in words:
        mat = prepareWord(word)
        # print(word, mat.shape)
        wordArrs.append(mat)
        # if wordArrs is None:
        #     wordArrs = mat
        # else:
        #     wordArrs = np.concatenate([wordArrs, mat], axis=0)

        word_embeddings.append(EMBEDDINGS[WORD_TO_NUM[word]])

    return [np.array(wordArrs), np.array(word_embeddings)]

def reader_model(word_ascii, keep_prob):
    h = None
    for i in range(len(weights)):
        if i == 0:
            h = tf.matmul(word_ascii, weights[i])
        else:
            h = tf.matmul(h, weights[i])

        h += biases[i]

        if i == len(weights)-1:
            h = tf.nn.tanh(h)
        else:
            h = tf.nn.relu(h)

        norm = tf.sqrt(tf.reduce_sum(tf.square(h), 1, keep_dims=True))
        output = h/norm

    return output

def loss_optim(embed_predict, embed_true):
    # losses = tf.reduce_sum(tf.abs(embed_predict - embed_true),axis=0)
    # calculating loss based on dot product
    similarities = tf.diag_part(tf.matmul(embed_predict, tf.transpose(embed_true)))
    summarize(similarities, varName = "similarities")
    loss = 1/tf.reduce_sum(similarities)

    tf.summary.scalar("raw_loss", loss)

    # l2_loss = 0
    # all_vars = tf.trainable_variables()
    # for v in all_vars:
    #     l2_loss += alpha * tf.nn.l2_loss(v)

    # tf.summary.scalar("l2_loss", l2_loss)
    # loss += l2_loss
    # tf.summary.scalar("total_loss", loss)

    output_similarity = tf.matmul(embed_predict, tf.transpose(embed_predict))
    summarize(output_similarity, "output_similarity")
    loss += 0.02 * tf.reduce_sum(output_similarity)

    optim = tf.train.AdamOptimizer(learning_rate, beta).minimize(loss)
    # optim = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    # optim = tf.train.AdagradOptimizer(0.1).minimize(loss)
    # optim = tf.train.ProximalGradientDescentOptimizer(learning_rate).minimize(loss)
    return [loss, optim]<|endoftext|>


<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-

from bootstrap import manager

from web import models
from web.views.api.v1 import processors
from web.views.api.v1.common import url_prefix

blueprint_cve = manager.create_api_blueprint(
    models.CVE,
    url_prefix=url_prefix,
    methods=['GET', 'POST', 'PUT', 'DELETE'],
    preprocessors=dict(
        POST=[processors.auth_func],
        PUT=[processors.auth_func],
        DELETE=[processors.auth_func]))
<commit_msg>Only authirizes GET method for the CVE API.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-

from bootstrap import manager

from web import models
from web.views.api.v1 import processors
from web.views.api.v1.common import url_prefix

blueprint_cve = manager.create_api_blueprint(
    models.CVE,
    url_prefix=url_prefix,
    methods=['GET'])
<|endoftext|>


<commit_before>from __future__ import unicode_literals, with_statement

import codecs
import os

from compressor.cache import (cache_get, cache_set, get_hexdigest, get_mtime,
                              get_offline_hexdigest, get_offline_manifest,
                              get_templatetag_cachekey)
from compressor.conf import settings
from compressor.exceptions import (CompressorError, FilterDoesNotExist,
                                   OfflineGenerationError,
                                   UncompressableFileError)
from compressor.filters import CompilerFilter
from compressor.signals import post_compress
from compressor.storage import compressor_file_storage
from compressor.utils import get_class, get_mod_func, staticfiles
from compressor.utils.decorators import cached_property
from django import template
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.template import Context
from django.template.loader import render_to_string
from django.utils import six
from django.utils.safestring import mark_safe

try:
    from importlib import import_module
except:
    from django.utils.importlib import import_module

try:
    from urllib.request import url2pathname
except ImportError:
    from urllib import url2pathname

# Some constants for nicer handling.
SOURCE_HUNK, SOURCE_FILE = 'inline', 'file'
METHOD_INPUT, METHOD_OUTPUT = 'input', 'output'

register = template.Library()

OUTPUT_FILE = 'file'
OUTPUT_INLINE = 'inline'
OUTPUT_MODES = (OUTPUT_FILE, OUTPUT_INLINE)

def compress_monkey_patch():
    """patch all compress

    we need access to variables from widget scss

    for example we have::

        /themes/bootswatch/cyborg/_variables

    but only if is cyborg active for this reasone we need
    dynamically append import to every scss file

    """
    from compressor.templatetags import compress as compress_tags
    from compressor import base as compress_base

    compress_base.Compressor.filter_input = filter_input
    compress_base.Compressor.output = output
    compress_base.Compressor.hunks = hunks

    compress_tags.CompressorMixin.render_compressed = render_compressed
    compress_tags.CompressorMixin.render_output = render_output

    from django_pyscss import compressor as pyscss_compressor

    pyscss_compressor.DjangoScssFilter.input = input

def render_compressed(self, context, kind, mode, forced=False):

    # See if it has been rendered offline
    cached_offline = self.render_offline(context, forced=forced)
    if cached_offline:
        return cached_offline

    # Take a shortcut if we really don't have anything to do
    if ((not settings.COMPRESS_ENABLED and
         not settings.COMPRESS_PRECOMPILERS) and not forced):
        return self.get_original_content(context)

    context['compressed'] = {'name': getattr(self, 'name', None)}
    compressor = self.get_compressor(context, kind)

    # Prepare the actual compressor and check cache
    cache_key, cache_content = self.render_cached(compressor, kind, mode, forced=forced)
    if cache_content is not None:
        return cache_content

    # call compressor output method and handle exceptions
    try:
        rendered_output = self.render_output(
            compressor, mode, forced=forced, request=context.get('request'))
        if cache_key:
            cache_set(cache_key, rendered_output)
        assert isinstance(rendered_output, six.string_types)
        return rendered_output
    except Exception:
        if settings.DEBUG or forced:
            raise

    # Or don't do anything in production
    return self.get_original_content(context)

def render_output(self, compressor, mode, forced=False, request=None):
    return compressor.output(mode, forced=forced, request=request)

def input(self, **kwargs):
    """main override which append variables import to all scss content
    """

    with_variables = """
    @import "/themes/bootswatch/{}/_variables";
    {}
    """.format(kwargs['request'].leonardo_page.color_scheme.name.lower(), self.content)

    return self.compiler.compile_string(with_variables, filename=self.filename)

def hunks(self, forced=False, request=None):
    """
    The heart of content parsing, iterates over the
    list of split contents and looks at its kind
    to decide what to do with it. Should yield a
    bunch of precompiled and/or rendered hunks.
    """
    enabled = settings.COMPRESS_ENABLED or forced

    for kind, value, basename, elem in self.split_contents():
        precompiled = False
        attribs = self.parser.elem_attribs(elem)
        charset = attribs.get("charset", self.charset)
        options = {
            'method': METHOD_INPUT,
            'elem': elem,
            'kind': kind,
            'basename': basename,
            'charset': charset,
            'request': request,
        }

        if kind == SOURCE_FILE:
            options = dict(options, filename=value)
            value = self.get_filecontent(value, charset)

        if self.all_mimetypes:
            precompiled, value = self.precompile(value, **options)

        if enabled:
            yield self.filter(value, **options)
        else:
            if precompiled:
                yield self.handle_output(kind, value, forced=True,
                                         basename=basename)
            else:
                yield self.parser.elem_str(elem)

def output(self, mode='file', forced=False, request=None):
    """
    The general output method, override in subclass if you need to do
    any custom modification. Calls other mode specific methods or simply
    returns the content directly.
    """
    output = '\n'.join(self.filter_input(forced, request=request))

    if not output:
        return ''

    if settings.COMPRESS_ENABLED or forced:
        filtered_output = self.filter_output(output)
        return self.handle_output(mode, filtered_output, forced)

    return output

def filter_input(self, forced=False, request=None):
    """
    Passes each hunk (file or code) to the 'input' methods
    of the compressor filters.
    """
    content = []
    for hunk in self.hunks(forced, request=request):
        content.append(hunk)
    return content
<commit_msg>#75 fix compress patch and make it optional<commit_after>from __future__ import unicode_literals, with_statement

import logging

from compressor.cache import cache_set
from compressor.conf import settings
from django import template
from django.utils import six

try:
    from importlib import import_module
except:
    from django.utils.importlib import import_module

try:
    from urllib.request import url2pathname
except ImportError:
    from urllib import url2pathname

LOG = logging.getLogger(__name__)

# Some constants for nicer handling.
SOURCE_HUNK, SOURCE_FILE = 'inline', 'file'
METHOD_INPUT, METHOD_OUTPUT = 'input', 'output'

register = template.Library()

OUTPUT_FILE = 'file'
OUTPUT_INLINE = 'inline'
OUTPUT_MODES = (OUTPUT_FILE, OUTPUT_INLINE)

def compress_monkey_patch():
    """patch all compress

    we need access to variables from widget scss

    for example we have::

        /themes/bootswatch/cyborg/_variables

    but only if is cyborg active for this reasone we need
    dynamically append import to every scss file

    """
    from compressor.templatetags import compress as compress_tags
    from compressor import base as compress_base

    compress_base.Compressor.filter_input = filter_input
    compress_base.Compressor.output = output
    compress_base.Compressor.hunks = hunks

    compress_tags.CompressorMixin.render_compressed = render_compressed
    compress_tags.CompressorMixin.render_output = render_output

    from django_pyscss import compressor as pyscss_compressor

    pyscss_compressor.DjangoScssFilter.input = input

def render_compressed(self, context, kind, mode, forced=False):

    # See if it has been rendered offline
    cached_offline = self.render_offline(context, forced=forced)
    if cached_offline:
        return cached_offline

    # Take a shortcut if we really don't have anything to do
    if ((not settings.COMPRESS_ENABLED and
         not settings.COMPRESS_PRECOMPILERS) and not forced):
        return self.get_original_content(context)

    context['compressed'] = {'name': getattr(self, 'name', None)}
    compressor = self.get_compressor(context, kind)

    # Prepare the actual compressor and check cache
    cache_key, cache_content = self.render_cached(
        compressor, kind, mode, forced=forced)
    if cache_content is not None:
        return cache_content

    # call compressor output method and handle exceptions
    try:
        rendered_output = self.render_output(
            compressor, mode, forced=forced, request=context.get('request'))
        if cache_key:
            cache_set(cache_key, rendered_output)
        assert isinstance(rendered_output, six.string_types)
        return rendered_output
    except Exception:
        if settings.DEBUG or forced:
            raise

    # Or don't do anything in production
    return self.get_original_content(context)

def render_output(self, compressor, mode, forced=False, request=None):
    return compressor.output(mode, forced=forced, request=request)

def input(self, **kwargs):
    """main override which append variables import to all scss content
    """

    with_variables = None

    try:
        page = kwargs['request'].leonardo_page
    except Exception as e:
        LOG.exception(str(e))
    else:
        with_variables = """
        @import "/themes/{}/{}/_variables";
        {}
        """.format(
            page.theme.name.lower(),
            page.color_scheme.name.lower(),
            self.content)

    return self.compiler.compile_string(
        with_variables or self.content,
        filename=self.filename)

def hunks(self, forced=False, request=None):
    """
    The heart of content parsing, iterates over the
    list of split contents and looks at its kind
    to decide what to do with it. Should yield a
    bunch of precompiled and/or rendered hunks.
    """
    enabled = settings.COMPRESS_ENABLED or forced

    for kind, value, basename, elem in self.split_contents():
        precompiled = False
        attribs = self.parser.elem_attribs(elem)
        charset = attribs.get("charset", self.charset)
        options = {
            'method': METHOD_INPUT,
            'elem': elem,
            'kind': kind,
            'basename': basename,
            'charset': charset,
            'request': request,
        }

        if kind == SOURCE_FILE:
            options = dict(options, filename=value)
            value = self.get_filecontent(value, charset)

        if self.all_mimetypes:
            precompiled, value = self.precompile(value, **options)

        if enabled:
            yield self.filter(value, **options)
        else:
            if precompiled:
                yield self.handle_output(kind, value, forced=True,
                                         basename=basename)
            else:
                yield self.parser.elem_str(elem)

def output(self, mode='file', forced=False, request=None):
    """
    The general output method, override in subclass if you need to do
    any custom modification. Calls other mode specific methods or simply
    returns the content directly.
    """
    output = '\n'.join(self.filter_input(forced, request=request))

    if not output:
        return ''

    if settings.COMPRESS_ENABLED or forced:
        filtered_output = self.filter_output(output)
        return self.handle_output(mode, filtered_output, forced)

    return output

def filter_input(self, forced=False, request=None):
    """
    Passes each hunk (file or code) to the 'input' methods
    of the compressor filters.
    """
    content = []
    for hunk in self.hunks(forced, request=request):
        content.append(hunk)
    return content
<|endoftext|>


<commit_before>##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
from tools.translate import _
import logging

class sale_advance_payment_inv(osv.osv_memory):
    _inherit = "sale.advance.payment.inv"
    _logger = logging.getLogger(__name__)

    def create_invoices(self, cr, uid, ids, context=None):
        """
             To create invoices.

             @param self: The object pointer.
             @param cr: A database cursor
             @param uid: ID of the user currently logged in
             @param ids: the ID or list of IDs if we want more than one
             @param context: A standard dictionary

             @return:

        """
        list_inv = []
        obj_sale = self.pool.get('sale.order')
        obj_lines = self.pool.get('account.invoice.line')
        inv_obj = self.pool.get('account.invoice')
        if context is None:
            context = {}

        for  sale_adv_obj in self.browse(cr, uid, ids, context=context):
            for sale in obj_sale.browse(cr, uid, context.get('active_ids', []), context=context):
                create_ids = []
                ids_inv = []
                if sale.order_policy == 'postpaid':
                    raise osv.except_osv(
                        _('Error'),
                        _("You cannot make an advance on a sales order \
                             that is defined as 'Automatic Invoice after delivery'."))
                val = obj_lines.product_id_change(cr, uid, [], sale_adv_obj.product_id.id,
                        uom_id = False, partner_id = sale.partner_id.id, fposition_id = sale.fiscal_position.id)

                self._logger.debug('advance val `%s`', val)
                product = self.pool.get('product.product').browse(cr, uid, sale_adv_obj.product_id.id, context)

                line_id = obj_lines.create(cr, uid, {
                    'name': val['value']['name'] or product.name ,
                    'account_id': product.property_account_income.id or product.categ_id.property_account_income_categ.id,
                    'price_unit': sale_adv_obj.amount,
                    'quantity': sale_adv_obj.qtty,
                    'discount': False,
                    'uos_id': val['value']['uos_id'],
                    'product_id': sale_adv_obj.product_id.id,
                    'invoice_line_tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
                    'account_analytic_id': sale.project_id.id or product.property_account_income.analytic_account_id.id \
                               or product.categ_id.property_account_income_categ.analytic_account_id.id or False,
                    #'note':'',
                })

                create_ids.append(line_id)
                inv = {
                    'name': sale.client_order_ref or sale.name,
                    'origin': sale.name,
                    'type': 'out_invoice',
                    'reference': False,
                    'account_id': sale.partner_id.property_account_receivable.id,
                    'partner_id': sale.partner_id.id,
                    'address_invoice_id': sale.partner_invoice_id.id,
                    'address_contact_id': sale.partner_id.id,
                    'invoice_line': [(6, 0, create_ids)],
                    'currency_id': sale.pricelist_id.currency_id.id,
                    'comment': '',
                    'payment_term': sale.payment_term.id,
                    'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
                }

                inv_id = inv_obj.create(cr, uid, inv)
                inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context)

                for inv in sale.invoice_ids:
                    ids_inv.append(inv.id)
                ids_inv.append(inv_id)
                obj_sale.write(cr, uid, sale.id, {'invoice_ids': [(6, 0, ids_inv)]})
                list_inv.append(inv_id)

        #
        # If invoice on picking: add the cost on the SO
        # If not, the advance will be deduced when generating the final invoice
        #
                if sale.order_policy == 'picking':
                    self.pool.get('sale.order.line').create(cr, uid, {
                        'order_id': sale.id,
                        'name': val['value']['name'],
                        'price_unit': -sale_adv_obj.amount,
                        'product_uom_qty': sale_adv_obj.qtty,
                        'product_uos_qty': sale_adv_obj.qtty,
                        'product_uos': val['value']['uos_id'],
                        'product_uom': val['value']['uos_id'],
                        'product_id': sale_adv_obj.product_id.id,
                        'discount': False,
                        'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
                    }, context)

        context.update({'invoice_id':list_inv})

        return {
            'name': 'Open Invoice',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'sale.open.invoice',
            'type': 'ir.actions.act_window',
            'target': 'new',
            'context': context
        }

sale_advance_payment_inv()
<commit_msg>[REF] advance invoice deaktivated incompatible code<commit_after>##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
from tools.translate import _
import logging

class sale_advance_payment_inv(osv.osv_memory):
    _inherit = "sale.advance.payment.inv"
    _logger = logging.getLogger(__name__)

    def create_invoices_wrong(self, cr, uid, ids, context=None):
        """
             NOT COMPATIBLE WITH V7

             To create invoices.

             @param self: The object pointer.
             @param cr: A database cursor
             @param uid: ID of the user currently logged in
             @param ids: the ID or list of IDs if we want more than one
             @param context: A standard dictionary

             @return:

        """
        list_inv = []
        obj_sale = self.pool.get('sale.order')
        obj_lines = self.pool.get('account.invoice.line')
        inv_obj = self.pool.get('account.invoice')
        if context is None:
            context = {}

        for  sale_adv_obj in self.browse(cr, uid, ids, context=context):
            for sale in obj_sale.browse(cr, uid, context.get('active_ids', []), context=context):
                create_ids = []
                ids_inv = []
                if sale.order_policy == 'postpaid':
                    raise osv.except_osv(
                        _('Error'),
                        _("You cannot make an advance on a sales order \
                             that is defined as 'Automatic Invoice after delivery'."))
                val = obj_lines.product_id_change(cr, uid, [], sale_adv_obj.product_id.id,
                        uom = False, partner_id = sale.partner_id.id, fposition_id = sale.fiscal_position.id)

                self._logger.debug('advance val `%s`', val)
                product = self.pool.get('product.product').browse(cr, uid, sale_adv_obj.product_id.id, context)

                line_id = obj_lines.create(cr, uid, {
                    'name': val['value']['name'] or product.name ,
                    'account_id': product.property_account_income.id or product.categ_id.property_account_income_categ.id,
                    'price_unit': sale_adv_obj.amount,
                    'quantity': sale_adv_obj.qtty,
                    'discount': False,
                    'uos_id': val['value']['uos_id'],
                    'product_id': sale_adv_obj.product_id.id,
                    'invoice_line_tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
                    'account_analytic_id': sale.project_id.id or product.property_account_income.analytic_account_id.id \
                               or product.categ_id.property_account_income_categ.analytic_account_id.id or False,
                    #'note':'',
                })

                create_ids.append(line_id)
                inv = {
                    'name': sale.client_order_ref or sale.name,
                    'origin': sale.name,
                    'type': 'out_invoice',
                    'reference': False,
                    'account_id': sale.partner_id.property_account_receivable.id,
                    'partner_id': sale.partner_id.id,
                    'address_invoice_id': sale.partner_invoice_id.id,
                    'address_contact_id': sale.partner_id.id,
                    'invoice_line': [(6, 0, create_ids)],
                    'currency_id': sale.pricelist_id.currency_id.id,
                    'comment': '',
                    'payment_term': sale.payment_term.id,
                    'fiscal_position': sale.fiscal_position.id or sale.partner_id.property_account_position.id
                }

                inv_id = inv_obj.create(cr, uid, inv)
                inv_obj.button_reset_taxes(cr, uid, [inv_id], context=context)

                for inv in sale.invoice_ids:
                    ids_inv.append(inv.id)
                ids_inv.append(inv_id)
                obj_sale.write(cr, uid, sale.id, {'invoice_ids': [(6, 0, ids_inv)]})
                list_inv.append(inv_id)

        #
        # If invoice on picking: add the cost on the SO
        # If not, the advance will be deduced when generating the final invoice
        #
                if sale.order_policy == 'picking':
                    self.pool.get('sale.order.line').create(cr, uid, {
                        'order_id': sale.id,
                        'name': val['value']['name'],
                        'price_unit': -sale_adv_obj.amount,
                        'product_uom_qty': sale_adv_obj.qtty,
                        'product_uos_qty': sale_adv_obj.qtty,
                        'product_uos': val['value']['uos_id'],
                        'product_uom': val['value']['uos_id'],
                        'product_id': sale_adv_obj.product_id.id,
                        'discount': False,
                        'tax_id': [(6, 0, val['value']['invoice_line_tax_id'])],
                    }, context)

        context.update({'invoice_id':list_inv})

#        return {
#            'name': 'Open Invoice',
#            'view_type': 'form',
#            'view_mode': 'form',
#            'res_model': 'account.invoice',
#            'type': 'ir.actions.act_window',
#            'target': 'new',
#            'context': context
#        }

        return {
            'name': _('Advance Invoice'),
            'view_type': 'form',
            'view_mode': 'form,tree',
            'res_model': 'account.invoice',
            'res_id': invoice_ids[0],
            'view_id': False,
            'views': [(form_id, 'form'), (tree_id, 'tree')],
            'context': "{'type': 'out_invoice'}",
            'type': 'ir.actions.act_window',
        }

sale_advance_payment_inv()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# Common tools:
#	pip/pip3 : install packages
#	pylint/pychecker : verify syntax
#	unittest package for xUnit.
""" This module helps setup a fresh install the way I like it. """

# Imports
from __future__ import print_function
import argparse
import glob
import os
import shutil
import subprocess
import sys
import urllib
import zipfile

# Classes

class Progress(object):
    """ Draw a simple progress bar. """
    def __init__(self, tick, empty, total_ticks):
        self.tick = tick
        self.empty = empty
        self.total_ticks = total_ticks
        self.num_ticks = 0
        self.old_percent = 0
    def check_percent(self, new_percent):
        """ Helper, check if passed a point. """
        if new_percent >= self.old_percent + self.tick_threshold():
            self.old_percent = new_percent
            return True
        else:
            return False
    def draw(self):
        """ Draw a bar for progress after incrementing. """
        self.num_ticks += 1
        num_empty = self.total_ticks - self.num_ticks
        line = "Download Progress: [%s%s]\n" % (self.tick * self.num_ticks,
            self.empty * num_empty)
        sys.stdout.write(line)
        sys.stdout.flush()
    def tick_threshold(self):
        """ Return number of % per tick on bar. """
        return 100 / self.total_ticks
    @staticmethod
    def default_prog():
        """ Factory method, makes a bar with defaults. """
        return Progress('*', '-', 20)

class PDir(object):
    """ Pushd analog for personal use. """
    dirs = []
    @staticmethod
    def push(new_dir):
        """ Push curdir to stack and change to new_dir. """
        curdir = os.path.realpath(os.curdir)
        PDir.dirs.append(curdir)
        os.chdir(new_dir)
        print("Changing to: " + new_dir)
    @staticmethod
    def pop():
        """ Pop the dirstack and return to it. """
        os.chdir(PDir.dirs.pop())

# Functions

def gen_report(progress):
    """ Report hook generator. """
    def report_down(block_count, bytes_per_block, total_size):
        """ Simple report hook. """
        if block_count == 0:
            print("Download Started")
        elif total_size < 0:
            print("Read %d blocks" % block_count)
        else:
            total_down = block_count * bytes_per_block
            percent = (total_down * 100.0) / total_size
            if total_down >= total_size or progress.check_percent(percent):
                progress.draw()
    return report_down

def get_code(command, target):
    """ Wrapper function to clone repos.
    Protects against overwriting if target exists.
    command: The command that would run in bash.
    target: Where to clone to.
    """
    cmd = command.split()
    cmd.append(target)
    if not os.path.exists(target):
        subprocess.call(cmd)

def num_jobs():
    """ Use BASH one liner to determine number of threads available. """
    jobs = subprocess.check_output('cat /proc/cpuinfo | grep "processor"\
            | wc -l', shell=True)
    return int(jobs)

def build_sdl(libdir):
    """ Build ack from source, move to target dir. """
    srcdir = 'sdl'
    srcdir2 = srcdir + '2'

    try:
        # Fetch code & copy for 2
        get_code('hg clone -u SDL-1.2 http://hg.libsdl.org/SDL', srcdir)
        cmd = 'cp -r {} {}'.format(srcdir, srcdir + '2').split()
        subprocess.call(cmd)

        # Build sdl1
        PDir.push(srcdir)
        subprocess.call('./autogen.sh')
        cmd = './configure --prefix={}'.format(libdir).split()
        subprocess.call(cmd)
        cmd = 'make -j{} install'.format(num_jobs()).split()
        subprocess.call(cmd)
        PDir.pop()

        # Build sdl2
        PDir.push(srcdir2)
        cmd = 'hg update default'.split()
        subprocess.call(cmd)
        cmd = './configure --prefix={}'.format(libdir).split()
        subprocess.call(cmd)
        cmd = 'make -j{} install'.format(num_jobs()).split()
        subprocess.call(cmd)
        PDir.pop()
    finally:
        shutil.rmtree(srcdir)
        shutil.rmtree(srcdir2)

def build_gtest(libdir):
    """ Build gtest from source and put in libs. """
    archive = 'gtest.zip'
    url = 'https://googletest.googlecode.com/files/gtest-1.7.0.zip'

    try:
        # Fetch program
        print("Downloading gtest source.")
        prog = Progress.default_prog()
        zfile = urllib.URLopener()
        zfile.retrieve(url, archive, gen_report(prog))
        zipfile.ZipFile(archive).extractall()
        srcdir = glob.glob('gtest-*')[0]

        # Build & clean
        PDir.push(srcdir)
        cmd = 'chmod u+x configure ./scripts/*'.split()
        subprocess.call(cmd)
        cmd = './configure --prefix={}'.format(libdir).split()
        subprocess.call(cmd)
        subprocess.call('make')

        # Copy out file structure
        os.mkdir(libdir + os.sep + 'lib')
        shutil.copytree('include', libdir + os.sep + 'include')
        for fil in glob.glob('lib' + os.sep + '.libs' + os.sep + '*.a'):
            shutil.copy(fil, libdir + os.sep + 'lib')
    finally:
        PDir.pop()
        os.remove(archive)
        shutil.rmtree(srcdir)

def build_cunit(libdir):
    """ Build classic cunit, good test lib for c code. """
    srcdir = 'cunit'
    get_code('svn checkout svn://svn.code.sf.net/p/cunit/code/trunk', srcdir)

    # Build & clean
    PDir.push(srcdir)
    # Shell true is due to some bug via normal way
    subprocess.call('./bootstrap {}'.format(libdir), shell=True)
    cmd = 'make -j{} install'.format(num_jobs()).split()
    subprocess.call(cmd)
    PDir.pop()

    shutil.rmtree(srcdir)

def main():
    """ Main function. """
    mesg = """This script sets up the libs for this project."""
    parser = argparse.ArgumentParser(description=mesg,
            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('target', nargs='?', default='libs',
            help='dir to put libs in')

    args = parser.parse_args()  # Default parses argv[1:]
    libdir = os.path.realpath(os.curdir + os.sep + args.target)

    if os.path.exists(libdir):
        raise OSError('Directory already exists. {}'.format(libdir))

    os.makedirs(libdir)

    build_gtest(libdir)
    build_sdl(libdir)
    build_cunit(libdir)

if __name__ == '__main__':
    main()
<commit_msg>Refactor to clarify.<commit_after>#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# Common tools:
#	pip/pip3 : install packages
#	pylint/pychecker : verify syntax
#	unittest package for xUnit.
""" This module helps setup a fresh install the way I like it. """

# Imports
from __future__ import print_function
import argparse
import glob
import os
import shutil
import subprocess
import sys
import urllib
import zipfile

# Classes

class Progress(object):
    """ Draw a simple progress bar. """
    def __init__(self, tick, empty, total_ticks):
        self.tick = tick
        self.empty = empty
        self.total_ticks = total_ticks
        self.num_ticks = 0
        self.old_percent = 0
    def check_percent(self, new_percent):
        """ Helper, check if passed a point. """
        if new_percent >= self.old_percent + self.tick_threshold():
            self.old_percent = new_percent
            return True
        else:
            return False
    def draw(self):
        """ Draw a bar for progress after incrementing. """
        self.num_ticks += 1
        num_empty = self.total_ticks - self.num_ticks
        line = "Download Progress: [%s%s]\n" % (self.tick * self.num_ticks,
            self.empty * num_empty)
        sys.stdout.write(line)
        sys.stdout.flush()
    def tick_threshold(self):
        """ Return number of % per tick on bar. """
        return 100 / self.total_ticks
    @staticmethod
    def default_prog():
        """ Factory method, makes a bar with defaults. """
        return Progress('*', '-', 20)

class PDir(object):
    """ Pushd analog for personal use. """
    dirs = []
    @staticmethod
    def push(new_dir):
        """ Push curdir to stack and change to new_dir. """
        curdir = os.path.realpath(os.curdir)
        PDir.dirs.append(curdir)
        os.chdir(new_dir)
        print("Changing to: " + new_dir)
    @staticmethod
    def pop():
        """ Pop the dirstack and return to it. """
        os.chdir(PDir.dirs.pop())

# Functions

def gen_report(progress):
    """ Report hook generator. """
    def report_down(block_count, bytes_per_block, total_size):
        """ Simple report hook. """
        if block_count == 0:
            print("Download Started")
        elif total_size < 0:
            print("Read %d blocks" % block_count)
        else:
            total_down = block_count * bytes_per_block
            percent = (total_down * 100.0) / total_size
            if total_down >= total_size or progress.check_percent(percent):
                progress.draw()
    return report_down

def get_code(command, target):
    """ Wrapper function to clone repos.
    Protects against overwriting if target exists.
    command: The command that would run in bash.
    target: Where to clone to.
    """
    cmd = command.split()
    cmd.append(target)
    if not os.path.exists(target):
        subprocess.call(cmd)

def num_jobs():
    """ Use BASH one liner to determine number of threads available. """
    jobs = subprocess.check_output('cat /proc/cpuinfo | grep "processor"\
            | wc -l', shell=True)
    return int(jobs)

def build_sdl(libdir):
    """ Build ack from source, move to target dir. """
    srcdir, srcdir2 = 'sdl', 'sdl2'
    jobs = num_jobs()
    cmds_sdl1 = ['./autogen.sh',
            './configure --prefix=%s' % libdir,
            'make -j%d install' % jobs
            ]
    cmds_sdl2 = ['hg update default',
            './configure --prefix=%s' % libdir,
            'make -j%d install' % jobs
            ]
    build = {srcdir: cmds_sdl1,
            srcdir2: cmds_sdl2
            }

    try:
        # Fetch code & copy for 2
        get_code('hg clone -u SDL-1.2 http://hg.libsdl.org/SDL', srcdir)
        cmd = ('cp -r %s %s' % (srcdir, srcdir)).split()
        subprocess.call(cmd)

        # Build sdl1 & 2
        for src in build.keys():
            PDir.push(src)
            for cmd in build[src]:
                subprocess.call(cmd.split())
            PDir.pop()
    finally:
        shutil.rmtree(srcdir)
        shutil.rmtree(srcdir2)

def build_gtest(libdir):
    """ Build gtest from source and put in libs. """
    archive = 'gtest.zip'
    url = 'https://googletest.googlecode.com/files/gtest-1.7.0.zip'

    try:
        # Fetch program
        print("Downloading gtest source.")
        prog = Progress.default_prog()
        zfile = urllib.URLopener()
        zfile.retrieve(url, archive, gen_report(prog))
        zipfile.ZipFile(archive).extractall()
        srcdir = glob.glob('gtest-*')[0]

        # Build & clean
        PDir.push(srcdir)
        cmd = 'chmod u+x configure ./scripts/*'.split()
        subprocess.call(cmd)
        cmd = './configure --prefix={}'.format(libdir).split()
        subprocess.call(cmd)
        subprocess.call('make')

        # Copy out file structure
        os.mkdir(libdir + os.sep + 'lib')
        shutil.copytree('include', libdir + os.sep + 'include')
        for fil in glob.glob('lib' + os.sep + '.libs' + os.sep + '*.a'):
            shutil.copy(fil, libdir + os.sep + 'lib')
    finally:
        PDir.pop()
        os.remove(archive)
        shutil.rmtree(srcdir)

def build_cunit(libdir):
    """ Build classic cunit, good test lib for c code. """
    srcdir = 'cunit'
    get_code('svn checkout svn://svn.code.sf.net/p/cunit/code/trunk', srcdir)

    # Build & clean
    PDir.push(srcdir)
    # Shell true is due to some bug via normal way
    subprocess.call('./bootstrap {}'.format(libdir), shell=True)
    cmd = 'make -j{} install'.format(num_jobs()).split()
    subprocess.call(cmd)
    PDir.pop()

    shutil.rmtree(srcdir)

def main():
    """ Main function. """
    mesg = """This script sets up the libs for this project."""
    parser = argparse.ArgumentParser(description=mesg,
            formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('target', nargs='?', default='libs',
            help='dir to put libs in')

    args = parser.parse_args()  # Default parses argv[1:]
    libdir = os.path.realpath(os.curdir + os.sep + args.target)

    if os.path.exists(libdir):
        raise OSError('Directory already exists. {}'.format(libdir))

    os.makedirs(libdir)

    build_gtest(libdir)
    build_sdl(libdir)
    build_cunit(libdir)

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


<commit_before>from datetime import datetime

from flask.ext.babel import lazy_gettext as _
from sqlalchemy import func, select
from sqlalchemy.ext.hybrid import hybrid_property

from ark import settings
from ark.exts import db, cache
from ark.ranking.models import GoalRankingBan

class Goal(db.Model):

    __tablename__ = 'goal'

    GOAL_STATES = {
        'ready': _('Ready'),
        'doing': _('Doing'),
        'canceled': _('Canceled'),
        'finished': _('Finished'),
        'expired': _('Expired'),
    }

    id = db.Column(db.Integer, primary_key=True)
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.String(300))
    image_file_id = db.Column(db.Integer, db.ForeignKey('goal_file.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    operate_at = db.Column(db.DateTime)
    state = db.Column(db.Enum(*(GOAL_STATES.keys())), default='ready')
    is_deleted = db.Column(db.Boolean, default=False)

    image = db.relationship('GoalFile', uselist=False)
    likes = db.relationship('GoalLikeLog', uselist=True, lazy='dynamic',
                            backref=db.backref('goal', uselist=False))
    activities = db.relationship('GoalActivity', uselist=True, lazy='dynamic',
                                 backref=db.backref('goal', uselist=False))

    @hybrid_property
    def last_activity(self):
        last_activity = self.activities.limit(1).first()
        if last_activity:
            return last_activity
        return None

    @last_activity.expression
    def last_activity(cls):
        return (select([GoalActivity]).where(GoalActivity.goal_id==cls.id)
                .order_by(GoalActivity.created.desc())
                .limit(1).label('last_activity'))

    @hybrid_property
    def last_activity_interval(self):
        if not self.last_activity:
            return 0
        delta = datetime.utcnow() - self.last_activity.created
        return delta.days

    @last_activity_interval.expression
    def last_activity_interval(cls):
        return (select([func.datediff('NOW()', GoalActivity.created)])
                .where(GoalActivity.goal_id==cls.id)
                .order_by(GoalActivity.created.desc())
                .limit(1).label('last_activity_interval'))

    @hybrid_property
    def like_count(self):
        return self.likes.filter(GoalLikeLog.is_deleted==False).count()

    @like_count.expression
    def like_count(cls):
        return (select([func.count(GoalLikeLog.id)])
                .where(GoalLikeLog.goal_id==cls.id)
                .where(GoalLikeLog.is_deleted==False).label('like_count'))

    @hybrid_property
    def activity_count(self):
        return self.activities.filter(GoalActivity.is_deleted==False).count()

    @activity_count.expression
    def activity_count(cls):
        return (select([func.count(GoalActivity.id)])
                .where(GoalActivity.goal_id==cls.id)
                .where(GoalActivity.is_deleted==False)
                .label('activity_count'))

    @hybrid_property
    def score(self):
        return (self.like_count * settings.GOAL_LIKE_SOCRE +
                self.activity_count * settings.GOAL_UPDATE_SCORE +
                self.last_activity_interval * settings.GOAL_UPDATE_DAY)

    @cache.memoize(3600)
    def cache_score(self):
        return self.score

    @hybrid_property
    def is_ban(self):
        return (self.bans.filter(GoalRankingBan.is_deleted==False).count() > 0)

    @is_ban.expression
    def is_ban(cls):
        return (select([func.count(GoalRankingBan.id) > 0])
                .where(GoalRankingBan.goal_id==cls.id).label('is_ban'))

class GoalActivity(db.Model):

    __tablename__ = 'goal_activity'

    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goal.id'))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    image_file_id = db.Column(db.Integer, db.ForeignKey('goal_file.id'))
    activity = db.Column(db.String(300))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

    image = db.relationship('GoalFile', uselist=False)
    author = db.relationship(
        'Account', uselist=False,
        backref=db.backref('updates', uselist=True, lazy='dynamic',
                           order_by='desc(GoalActivity.created)'))

class GoalLikeLog(db.Model):

    __tablename__ = 'goal_like_log'

    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goal.id'))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

class GoalFile(db.Model):

    __tablename__ = 'goal_file'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    file_url = db.Column(db.String(500))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

    def get_thumbnail(self, width=0, height=0):
        url = self.file_url
        return '%s?imageMogr2/thumbnail/%sx%s!' % (url, width, height)
<commit_msg>remove useless goal state<commit_after>from datetime import datetime

from flask.ext.babel import lazy_gettext as _
from sqlalchemy import func, select
from sqlalchemy.ext.hybrid import hybrid_property

from ark import settings
from ark.exts import db, cache
from ark.ranking.models import GoalRankingBan

class Goal(db.Model):

    __tablename__ = 'goal'

    GOAL_STATES = {
        'doing': _('Doing'),
        'canceled': _('Canceled'),
        'finished': _('Finished'),
    }

    id = db.Column(db.Integer, primary_key=True)
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.String(300))
    image_file_id = db.Column(db.Integer, db.ForeignKey('goal_file.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    operate_at = db.Column(db.DateTime)
    state = db.Column(db.Enum(*(GOAL_STATES.keys())), default='doing')
    is_deleted = db.Column(db.Boolean, default=False)

    image = db.relationship('GoalFile', uselist=False)
    likes = db.relationship('GoalLikeLog', uselist=True, lazy='dynamic',
                            backref=db.backref('goal', uselist=False))
    activities = db.relationship('GoalActivity', uselist=True, lazy='dynamic',
                                 backref=db.backref('goal', uselist=False))

    @hybrid_property
    def last_activity(self):
        last_activity = self.activities.limit(1).first()
        if last_activity:
            return last_activity
        return None

    @last_activity.expression
    def last_activity(cls):
        return (select([GoalActivity]).where(GoalActivity.goal_id==cls.id)
                .order_by(GoalActivity.created.desc())
                .limit(1).label('last_activity'))

    @hybrid_property
    def last_activity_interval(self):
        if not self.last_activity:
            return 0
        delta = datetime.utcnow() - self.last_activity.created
        return delta.days

    @last_activity_interval.expression
    def last_activity_interval(cls):
        return (select([func.datediff('NOW()', GoalActivity.created)])
                .where(GoalActivity.goal_id==cls.id)
                .order_by(GoalActivity.created.desc())
                .limit(1).label('last_activity_interval'))

    @hybrid_property
    def like_count(self):
        return self.likes.filter(GoalLikeLog.is_deleted==False).count()

    @like_count.expression
    def like_count(cls):
        return (select([func.count(GoalLikeLog.id)])
                .where(GoalLikeLog.goal_id==cls.id)
                .where(GoalLikeLog.is_deleted==False).label('like_count'))

    @hybrid_property
    def activity_count(self):
        return self.activities.filter(GoalActivity.is_deleted==False).count()

    @activity_count.expression
    def activity_count(cls):
        return (select([func.count(GoalActivity.id)])
                .where(GoalActivity.goal_id==cls.id)
                .where(GoalActivity.is_deleted==False)
                .label('activity_count'))

    @hybrid_property
    def score(self):
        return (self.like_count * settings.GOAL_LIKE_SOCRE +
                self.activity_count * settings.GOAL_UPDATE_SCORE +
                self.last_activity_interval * settings.GOAL_UPDATE_DAY)

    @cache.memoize(3600)
    def cache_score(self):
        return self.score

    @hybrid_property
    def is_ban(self):
        return (self.bans.filter(GoalRankingBan.is_deleted==False).count() > 0)

    @is_ban.expression
    def is_ban(cls):
        return (select([func.count(GoalRankingBan.id) > 0])
                .where(GoalRankingBan.goal_id==cls.id).label('is_ban'))

class GoalActivity(db.Model):

    __tablename__ = 'goal_activity'

    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goal.id'))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    image_file_id = db.Column(db.Integer, db.ForeignKey('goal_file.id'))
    activity = db.Column(db.String(300))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

    image = db.relationship('GoalFile', uselist=False)
    author = db.relationship(
        'Account', uselist=False,
        backref=db.backref('updates', uselist=True, lazy='dynamic',
                           order_by='desc(GoalActivity.created)'))

class GoalLikeLog(db.Model):

    __tablename__ = 'goal_like_log'

    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goal.id'))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

class GoalFile(db.Model):

    __tablename__ = 'goal_file'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    file_url = db.Column(db.String(500))
    account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
    created = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

    def get_thumbnail(self, width=0, height=0):
        url = self.file_url
        return '%s?imageMogr2/thumbnail/%sx%s!' % (url, width, height)
<|endoftext|>


<commit_before>#fib loop
def fibonacci(n):
    prev = 0
    now = 1
    while n:
        prev, now = now, now + prev
        n -= 1
    return prev

assert fibonacci(0)==0
assert fibonacci(1)==1
assert fibonacci(15)==610

#fib recursive
def rec_fib(n):
    return rec_fib(n-1) + rec_fib(n-2) if n > 1 else n

assert rec_fib(0)==0
assert rec_fib(1)==1
assert rec_fib(15)==610
<commit_msg>changed fib<commit_after>#fib loop
def fibonacci(n):
    prev = 0
    now = 1
    #for loop fails when X in range(X) is quite large HA HA HA.
    while n:
        prev, now = now, now + prev
        n -= 1
    return prev

assert fibonacci(0)==0
assert fibonacci(1)==1
assert fibonacci(15)==610

#fib recursive
def rec_fib(n):
    return rec_fib(n-1) + rec_fib(n-2) if n > 1 else n

assert rec_fib(0)==0
assert rec_fib(1)==1
assert rec_fib(15)==610
<|endoftext|>


<commit_before># from useless.pipes import worker
import inspect
from useless.pipes.pipes import Worker as _Worker
from functools import partial, wraps, update_wrapper

class Incompatible(Exception): pass

anything = '*'

class Worker(_Worker):
    def __ror__(self, left):
        try:
            accepts = self.accepts()
            yields  = left.yields()
            if accepts != anything and yields != anything and yields not in accepts:
                raise Incompatible('Incompatible left: %s to right: %s' % (yields, accepts))
        except AttributeError:
            pass

        return super(Worker, self).__ror__(left)

    def accepts(self, symbol=None):
        accepts = self.target.accepts #self.target.original_function.accepts
        return (symbol in accepts or accepts == anything) if symbol else accepts

    def yields(self, symbol=None):
        yields = self.target.yields #self.target.original_function.yields
        return (symbol in yields or yields == anything) if symbol else yields

    @property
    def __doc__(self):
        return self.target.__doc__

    @property
    def __name__(self):
        return self.target.__name__

def _worker(func, accepts=anything, yields=anything):
    spec = inspect.getargspec(func)
    args_with_defaults = spec.defaults and len(spec.defaults) or 0
    kw_args = spec.args[-args_with_defaults:]
    possible_one_step = len(spec.args) == 1

    @wraps(func)
    def bind(*a, **kw):
        # trigger three step process if kw has only keyword arguments
        # in python we can def f(a, b=1) => f(a=1, b=2)
        # t.i. a positional arg can be treated as if it were a kw argument
        # but this shouldn't trigger three-step, only e.g. f(b=2)
        if kw and not a and not set(kw).difference(kw_args):
            return wraps(bind) (partial(bind, *a, **kw))

        @wraps(bind)
        def apply(iter):
            return apply.original_function(iter, *a, **kw)

        return Worker(apply)

    bind.accepts = accepts
    bind.yields  = yields
    bind.original_function = func

    def decorate_with(f):
        bind.original_function = f(bind.original_function)
    bind.decorate_with = decorate_with

    return bind

def worker(func=None, accepts=anything, yields=anything):
    if func:
        return _worker(func)

    def wrap(f):
        return _worker(f, accepts, yields)

    return wrap

filter = worker

class Pipe(list):
    def __init__(self, seq):
        if not hasattr(seq, '__iter__'):
            seq = [seq]

        list.__init__(self, seq)

    def accepts(self, symbol=None):
        accepts = self[0].accepts
        return symbol in accepts if symbol else accepts

    def yields(self, symbol=None):
        yields = self[-1].yields
        return symbol in yields if symbol else yields

    def prepend(self, worker):
        self.insert(0, worker)

    def apply(self, iter, *a, **kw):
        p = iter
        for worker in self:
            p |= worker(*a, **kw)

        return p

<commit_msg>Factor out function argument inspection.<commit_after># from useless.pipes import worker
import inspect
from useless.pipes.pipes import Worker as _Worker
from functools import partial, wraps, update_wrapper

class Incompatible(Exception): pass

anything = '*'

class Worker(_Worker):
    def __ror__(self, left):
        try:
            accepts = self.accepts()
            yields  = left.yields()
            if accepts != anything and yields != anything and yields not in accepts:
                raise Incompatible('Incompatible left: %s to right: %s' % (yields, accepts))
        except AttributeError:
            pass

        return super(Worker, self).__ror__(left)

    def accepts(self, symbol=None):
        accepts = self.target.accepts #self.target.original_function.accepts
        return (symbol in accepts or accepts == anything) if symbol else accepts

    def yields(self, symbol=None):
        yields = self.target.yields #self.target.original_function.yields
        return (symbol in yields or yields == anything) if symbol else yields

    @property
    def __doc__(self):
        return self.target.__doc__

    @property
    def __name__(self):
        return self.target.__name__

def _get_kw_arguments(func):
    spec = inspect.getargspec(func)
    args_with_defaults = spec.defaults and len(spec.defaults) or 0
    return spec.args[-(args_with_defaults):]

def _worker(func, accepts=anything, yields=anything):
    kw_args = _get_kw_arguments(func)

    @wraps(func)
    def bind(*a, **kw):
        # trigger three step process if kw has only keyword arguments
        # in python we can def f(a, b=1) => f(a=1, b=2)
        # t.i. a positional arg can be treated as if it were a kw argument
        # but this shouldn't trigger three-step, only e.g. f(b=2)
        if kw and not a and not set(kw).difference(kw_args):
            return wraps(bind) (partial(bind, *a, **kw))

        @wraps(bind)
        def apply(iter):
            return apply.original_function(iter, *a, **kw)

        return Worker(apply)

    bind.accepts = accepts
    bind.yields  = yields
    bind.original_function = func

    def decorate_with(f):
        bind.original_function = f(bind.original_function)
    bind.decorate_with = decorate_with

    return bind

def worker(func=None, accepts=anything, yields=anything):
    if func:
        return _worker(func)

    def wrap(f):
        return _worker(f, accepts, yields)

    return wrap

filter = worker

class Pipe(list):
    def __init__(self, seq):
        if not hasattr(seq, '__iter__'):
            seq = [seq]

        list.__init__(self, seq)

    def accepts(self, symbol=None):
        accepts = self[0].accepts
        return symbol in accepts if symbol else accepts

    def yields(self, symbol=None):
        yields = self[-1].yields
        return symbol in yields if symbol else yields

    def prepend(self, worker):
        self.insert(0, worker)

    def apply(self, iter, *a, **kw):
        p = iter
        for worker in self:
            p |= worker(*a, **kw)

        return p

<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 	EXAMPLE:
#	$ gluster peer status --xml | ./xml.py connected <PEER1> <PEER2> <PEERn>
#	<BOOL>

# 	EXAMPLE:
#	$ gluster volume info --xml <VOLNAME> | ./xml.py property --key <KEY>
#	<VALUE>

# 	EXAMPLE:
#	$ gluster volume status --xml [<VOLNAME>] | ./xml.py port --volume <VOLUME> --host <HOST> --path <PATH>
#	<PORT>

# 	EXAMPLE:
#	$ gluster volume status --xml [<VOLNAME>] | ./xml.py ports [--volume <VOLUME>] [--host <HOST>]
#	<PORT1>[,<PORT2>[,<PORTn>]]

import sys
import argparse
import lxml.etree as etree

#	List of state codes:
#		<MESSAGE>
#	static char *glusterd_friend_sm_state_names[] = {	# glusterd-sm.c
#		"Establishing Connection",			# 0
#		"Probe Sent to Peer",				# 1
#		"Probe Received from Peer",			# 2
#		"Peer in Cluster",				# 3 (verified)
#		"Accepted peer request",			# 4
#		"Sent and Received peer request",		# 5
#		"Peer Rejected",				# 6 (verified)
#		"Peer detach in progress",			# 7
#		"Probe Received from peer",			# 8
#		"Connected to Peer",				# 9
#		"Peer is connected and Accepted",		# 10
#		"Invalid State"					# 11
#	};
valid_peered = ['3']

parser = argparse.ArgumentParser(description='gluster xml parsing tools')
#parser.add_argument('--debug', dest='debug', action='store_true', default=False)
subparsers = parser.add_subparsers(dest='mode')

#
#	'connected' parser
#
parser_connected = subparsers.add_parser('connected')
parser_connected.add_argument('peers', type=str, nargs='*', action='store')

#
#	'property' parser
#
parser_property = subparsers.add_parser('property')
parser_property.add_argument('--key', dest='key', action='store')

#
#	'port' parser
#
parser_port = subparsers.add_parser('port')
parser_port.add_argument('--volume', dest='volume', action='store', required=True)
parser_port.add_argument('--host', dest='host', action='store', required=True)
parser_port.add_argument('--path', dest='path', action='store', required=True)

#
#	'ports' parser
#
parser_ports = subparsers.add_parser('ports')
parser_ports.add_argument('--volume', dest='volume', action='store', required=False)
parser_ports.add_argument('--host', dest='host', action='store', required=False)

#
#	final setup...
#
args = parser.parse_args()
tree = etree.parse(sys.stdin)
root = tree.getroot()

# are all the hostnames in argv connected ?
if args.mode == 'connected':
	store = {}
	peers = args.peers

	for i in root.findall('.//peerStatus'):
		p = i.find('peer')
		h = p.find('hostname').text
		c = (str(p.find('connected').text) == '1')	# connected...?
		s = (str(p.find('state').text) in valid_peered)	# valid peering
		store[h] = c and s	# save for later...

	# if no peers specified, assume we should check all...
	if len(peers) == 0:
		peers = store.keys()

	for i in peers:
		if i in store.keys():
			if not store[i]:
				# someone is unconnected
				sys.exit(1)
		else:
			# we're looking for a peer that isn't peered yet
			sys.exit(2)

	# must be good!
	sys.exit(0)

elif args.mode == 'property':
	store = []
	for i in root.findall('.//option'):
		if str(i.find('name').text) == args.key:
			store.append(i.find('value').text)

	if len(store) == 1:
		print(store[0])
		sys.exit(0)
	else:			# more than one value found
		sys.exit(1)

elif args.mode == 'port':
	port = 0
	found = False
	#print args.volume	# volume
	#print args.host	# hostname
	#print args.path	# path
	for i in root.findall('.//volumes'):
		for j in i.findall('.//volume'):
			v = str(j.find('volName').text)
			#print v
			for k in j.findall('.//node'):
				h = str(k.find('hostname').text)
				p = str(k.find('path').text)
				#print h, p
				#if v == args.volume and h == args.host and p == args.path:
				if (v, h, p) == (args.volume, args.host, args.path):
					if found:
						# we have already found a match.
						# there's a bug somewhere...
						sys.exit(2)
					found = True
					port = int(k.find('port').text)

	if found and port > 0:
		print(port)
		sys.exit(0)
	else:			# no value found
		sys.exit(1)

# list all the ports used by one volume
elif args.mode == 'ports':
	ports = []
	found = False
	#print args.volume	# volume (optional)
	for i in root.findall('.//volumes'):
		for j in i.findall('.//volume'):
			v = str(j.find('volName').text)
			#print v
			# if no volume is specified, we use all of them...
			if args.volume is None or args.volume == v:
				for k in j.findall('.//node'):
					h = str(k.find('hostname').text)
					p = str(k.find('path').text)
					#print h, p
					if args.host is None or args.host == h:
						try:
							ports.append(int(k.find('port').text))
							found = True
						except ValueError, e:
							pass

	if found and len(ports) > 0:
		# NOTE: you may get duplicates if you lookup multiple hosts...
		# here we remove any duplicates and convert each int to strings
		print(','.join([str(x) for x in list(set(ports))]))
		sys.exit(0)
	else:			# no value found
		sys.exit(1)

# else:
sys.exit(3)

# vim: ts=8
<commit_msg>Globals should be in CAPS.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# 	EXAMPLE:
#	$ gluster peer status --xml | ./xml.py connected <PEER1> <PEER2> <PEERn>
#	<BOOL>

# 	EXAMPLE:
#	$ gluster volume info --xml <VOLNAME> | ./xml.py property --key <KEY>
#	<VALUE>

# 	EXAMPLE:
#	$ gluster volume status --xml [<VOLNAME>] | ./xml.py port --volume <VOLUME> --host <HOST> --path <PATH>
#	<PORT>

# 	EXAMPLE:
#	$ gluster volume status --xml [<VOLNAME>] | ./xml.py ports [--volume <VOLUME>] [--host <HOST>]
#	<PORT1>[,<PORT2>[,<PORTn>]]

import sys
import argparse
import lxml.etree as etree

#	List of state codes:
#		<MESSAGE>
#	static char *glusterd_friend_sm_state_names[] = {	# glusterd-sm.c
#		"Establishing Connection",			# 0
#		"Probe Sent to Peer",				# 1
#		"Probe Received from Peer",			# 2
#		"Peer in Cluster",				# 3 (verified)
#		"Accepted peer request",			# 4
#		"Sent and Received peer request",		# 5
#		"Peer Rejected",				# 6 (verified)
#		"Peer detach in progress",			# 7
#		"Probe Received from peer",			# 8
#		"Connected to Peer",				# 9
#		"Peer is connected and Accepted",		# 10
#		"Invalid State"					# 11
#	};
VALID_PEERED = ['3']

parser = argparse.ArgumentParser(description='gluster xml parsing tools')
#parser.add_argument('--debug', dest='debug', action='store_true', default=False)
subparsers = parser.add_subparsers(dest='mode')

#
#	'connected' parser
#
parser_connected = subparsers.add_parser('connected')
parser_connected.add_argument('peers', type=str, nargs='*', action='store')

#
#	'property' parser
#
parser_property = subparsers.add_parser('property')
parser_property.add_argument('--key', dest='key', action='store')

#
#	'port' parser
#
parser_port = subparsers.add_parser('port')
parser_port.add_argument('--volume', dest='volume', action='store', required=True)
parser_port.add_argument('--host', dest='host', action='store', required=True)
parser_port.add_argument('--path', dest='path', action='store', required=True)

#
#	'ports' parser
#
parser_ports = subparsers.add_parser('ports')
parser_ports.add_argument('--volume', dest='volume', action='store', required=False)
parser_ports.add_argument('--host', dest='host', action='store', required=False)

#
#	final setup...
#
args = parser.parse_args()
tree = etree.parse(sys.stdin)
root = tree.getroot()

# are all the hostnames in argv connected ?
if args.mode == 'connected':
	store = {}
	peers = args.peers

	for i in root.findall('.//peerStatus'):
		p = i.find('peer')
		h = p.find('hostname').text
		c = (str(p.find('connected').text) == '1')	# connected...?
		s = (str(p.find('state').text) in VALID_PEERED)	# valid peering
		store[h] = c and s	# save for later...

	# if no peers specified, assume we should check all...
	if len(peers) == 0:
		peers = store.keys()

	for i in peers:
		if i in store.keys():
			if not store[i]:
				# someone is unconnected
				sys.exit(1)
		else:
			# we're looking for a peer that isn't peered yet
			sys.exit(2)

	# must be good!
	sys.exit(0)

elif args.mode == 'property':
	store = []
	for i in root.findall('.//option'):
		if str(i.find('name').text) == args.key:
			store.append(i.find('value').text)

	if len(store) == 1:
		print(store[0])
		sys.exit(0)
	else:			# more than one value found
		sys.exit(1)

elif args.mode == 'port':
	port = 0
	found = False
	#print args.volume	# volume
	#print args.host	# hostname
	#print args.path	# path
	for i in root.findall('.//volumes'):
		for j in i.findall('.//volume'):
			v = str(j.find('volName').text)
			#print v
			for k in j.findall('.//node'):
				h = str(k.find('hostname').text)
				p = str(k.find('path').text)
				#print h, p
				#if v == args.volume and h == args.host and p == args.path:
				if (v, h, p) == (args.volume, args.host, args.path):
					if found:
						# we have already found a match.
						# there's a bug somewhere...
						sys.exit(2)
					found = True
					port = int(k.find('port').text)

	if found and port > 0:
		print(port)
		sys.exit(0)
	else:			# no value found
		sys.exit(1)

# list all the ports used by one volume
elif args.mode == 'ports':
	ports = []
	found = False
	#print args.volume	# volume (optional)
	for i in root.findall('.//volumes'):
		for j in i.findall('.//volume'):
			v = str(j.find('volName').text)
			#print v
			# if no volume is specified, we use all of them...
			if args.volume is None or args.volume == v:
				for k in j.findall('.//node'):
					h = str(k.find('hostname').text)
					p = str(k.find('path').text)
					#print h, p
					if args.host is None or args.host == h:
						try:
							ports.append(int(k.find('port').text))
							found = True
						except ValueError, e:
							pass

	if found and len(ports) > 0:
		# NOTE: you may get duplicates if you lookup multiple hosts...
		# here we remove any duplicates and convert each int to strings
		print(','.join([str(x) for x in list(set(ports))]))
		sys.exit(0)
	else:			# no value found
		sys.exit(1)

# else:
sys.exit(3)

# vim: ts=8
<|endoftext|>