<commit_before>#
# Jasy - Web Tooling Framework
# Copyright 2013 Sebastian Werner
#

import copy, re
import jasy.style.parse.Node as Node
import jasy.core.Console as Console

RE_INLINE_VARIABLE = re.compile("\$\{([a-zA-Z0-9\-_\.]+)\}")

class VariableError(Exception):
    def __init__(self, message, node):
        Exception.__init__(self, "Variable Error: %s for node type=%s in %s at line %s!" % (message, node.type, node.getFileName(), node.line))

def compute(tree):
    Console.info("Resolving variables...")
    Console.indent()

    __computeRecurser(tree, None, {})

    Console.outdent()

def __processOperator(node, values):
    Console.debug("Process operator: %s", node.type)

    # Resolve first child of operation
    first = node[0]
    if first.type == "variable":
        first = values[first.name]

    # Resolve second child of operation
    second = node[1]
    if second.type == "variable":
        second = values[second.name]

    return __computeOperation(first, second, node, node.type, values)

def __computeOperation(first, second, parent, operator, values):

    # Support for default set operator "?=" when variable was not defined before
    if first is None and operator == "questionmark":
        return second

    # Compare operation types
    if first.type == second.type:
        # print("Same type: %s" % first.type)

        if first.type == "number":
            firstUnit = getattr(first, "unit", None)
            secondUnit = getattr(second, "unit", None)

            if firstUnit == secondUnit or firstUnit is None or secondUnit is None:
                repl = Node.Node(type="number")

                if firstUnit is not None:
                    repl.unit = firstUnit
                elif secondUnit is not None:
                    repl.unit = secondUnit

                if operator == "plus":
                    repl.value = first.value + second.value
                elif operator == "minus":
                    repl.value = first.value - second.value
                elif operator == "mul":
                    repl.value = first.value * second.value
                elif operator == "div":
                    repl.value = first.value / second.value
                elif operator == "mod":
                    repl.value = first.value % second.value
                elif operator == "questionmark":
                    return first
                else:
                    raise VariableError("Unsupported number operation", parent)

                return repl

            elif firstUnit == "%" or secondUnit == "%":

                if operator in ("mul", "div"):
                    repl = Node.Node(type="number")

                    if operator == "mul":
                        repl.value = first.value * second.value / 100
                    elif operator == "mul":
                        repl.value = first.value / second.value / 100

                    if firstUnit == "%":
                        repl.unit = secondUnit
                    else:
                        repl.unit = firstUnit

                    return repl

                else:
                    raise VariableError("Could not compute mixed percent operations for operators other than \"*\" and \"/\"", node)

            else:
                raise VariableError("Could not compute result from numbers of different units: %s vs %s" % (first.unit, second.unit), parent)

        elif first.type == "string":
            repl = Node.Node(type="string")

            if operator == "plus":
                repl.value = first.value + second.value
                return repl
            else:
                raise VariableError("Unsupported string operation", parent)

        elif first.type == "list":
            if len(first) == len(second):
                repl = Node.Node(type="list")
                for pos, child in enumerate(first):
                    childRepl = __computeOperation(child, second[pos], parent, operator, values)
                    if childRepl is None:
                        raise VariableError("Got no valid return value to replace operation", child)

                    repl.append(childRepl)

                return repl

            else:
                raise VariableError("For list operations both lists have to have the same length!", parent)

        else:
            raise VariableError("Unsupported operation", parent)

    elif first.type == "list" and second.type != "list":
        repl = Node.Node(type="list")
        for child in first:
            childRepl = __computeOperation(child, second, parent, operator, values)
            if childRepl is None:
                raise VariableError("Got no valid return value to replace operation", child)

            repl.append(childRepl)

        return repl

    elif first.type != "list" and second.type == "list":
        repl = Node.Node(type="list")
        for child in second:
            childRepl = __computeOperation(first, child, parent, operator, values)
            if childRepl is None:
                raise VariableError("Got no valid return value to replace operation", child)

            repl.append(childRepl)

        return repl

    elif first.type == "string" or second.type == "string":
        repl = Node.Node(type="string")

        if operator == "plus":
            repl.value = str(first.value) + str(second.value)
            return repl
        else:
            raise VariableError("Unsupported string operation", parent)

    else:
        raise VariableError("Different types in operation: %s vs %s" % (first.type, second.type), parent)

def __computeRecurser(node, scope, values):

    # Update scope of new block starts
    if hasattr(node, "scope"):
        scope = node.scope
        values = copy.copy(values)

        # Reset all local variables to None
        # which enforces not to keep values from outer scope
        for name in scope.modified:
            values[name] = None

    # Worked on copy to prevent issues during length changes (due removing declarations, etc.)
    for child in list(node):
        if child is not None:
            __computeRecurser(child, scope, values)

    # Support typical operators
    if node.type in ("plus", "minus", "mul", "div", "mod"):
        repl = __processOperator(node, values)
        if repl:
            node.parent.replace(node, repl)
        else:
            raise VariableError("Got no valid return value to replace operation", node)

    # Not operator support
    elif node.type == "not":
        child = node[0]
        if child.type == "true":
            child.type = "false"
        elif child.type == "false":
            child.type = "true"
        else:
            raise VariableError("Could not apply not operator to non boolean variable", node)

        node.parent.replace(node, child)

    # Update values of variable
    elif (node.type == "declaration" and hasattr(node, "initializer")) or node.type == "assign":

        if node.type == "declaration":
            name = node.name
            init = node.initializer
            Console.debug("Found declaration of %s at line %s", name, node.line)

        else:
            name = node[0].name
            init = node[1]
            Console.debug("Found assignment of %s at line %s", name, node.line)

        # Modify value instead of replace when assign operator is set
        if hasattr(node, "assignOp") and node.assignOp is not None:
            if not name in values:
                raise VariableError("Assign operator is not supported as left hand variable is missing: %s" % name, node)

            repl = __computeOperation(values[name], init, node, node.assignOp, values)
            if repl:
                values[name] = repl
            else:
                raise VariableError("Got no valid return value to replace operation", node)

        else:
            # Update internal variable mapping
            values[name] = init

        # Remove declaration node from tree
        node.parent.remove(node)

    # Replace variable with actual value
    elif node.type == "variable" and not (node.parent.type == "assign" and node.parent[0] is node):
        name = node.name
        if not name in values:
            raise VariableError("Could not resolve variable %s! Missing value!" % name, node)

        value = values[name]
        if value is None:
            raise VariableError("Could not resolve variable %s! Value is none!" % name, node)

        Console.debug("Resolving variable: %s at line %s with %s from %s", name, node.line, values[name].type, values[name].line)
        node.parent.replace(node, copy.deepcopy(values[name]))

    elif node.type in ("property", "selector") and getattr(node, "dynamic", False):
        def replacer(matchObj):
            name = matchObj.group(1)

            if not name in values:
                raise VariableError("Could not resolve variable %s! Missing value!" % name, node)

            value = values[name]
            if value is None:
                raise VariableError("Could not resolve variable %s! Value is none!" % name, node)

            if value.type == "identifier":
                return value.value
            elif value.type == "string":
                return value.value
            elif value.type == "number":
                return "%s%s" % (value.value, getattr(value, "unit", ""))
            else:
                raise VariableError("Could not replace property inline variable with value of type: %s" % value.type, node)

        # Fix all selectors
        if node.type == "selector":
            selectors = node.name
            for pos, selector in enumerate(selectors):
                selectors[pos] = RE_INLINE_VARIABLE.sub(replacer, selector)

        else:
            node.name = RE_INLINE_VARIABLE.sub(replacer, node.name)

<commit_msg>Changed approach to not throw errors in cases variable could not be resolved yet - instead keep structure intact for next try (e.g. after method runs.<commit_after>#
# Jasy - Web Tooling Framework
# Copyright 2013 Sebastian Werner
#

import copy, re
import jasy.style.parse.Node as Node
import jasy.core.Console as Console

RE_INLINE_VARIABLE = re.compile("\$\{([a-zA-Z0-9\-_\.]+)\}")

class VariableError(Exception):
    def __init__(self, message, node):
        Exception.__init__(self, "Variable Error: %s for node type=%s in %s at line %s!" % (message, node.type, node.getFileName(), node.line))

def compute(tree):
    Console.info("Resolving variables...")
    Console.indent()

    __computeRecurser(tree, None, {})

    Console.outdent()

def __processOperator(node, values):
    Console.debug("Process operator: %s", node.type)

    # Resolve first child of operation
    first = node[0]
    if first.type == "variable":
        first = values[first.name]

    # Resolve second child of operation
    second = node[1]
    if second.type == "variable":
        second = values[second.name]

    return __computeOperation(first, second, node, node.type, values)

def __computeOperation(first, second, parent, operator, values):

    # Support for default set operator "?=" when variable was not defined before
    if first is None and operator == "questionmark":
        return second

    # Compare operation types
    if first.type == second.type:
        # print("Same type: %s" % first.type)

        if first.type == "number":
            firstUnit = getattr(first, "unit", None)
            secondUnit = getattr(second, "unit", None)

            if firstUnit == secondUnit or firstUnit is None or secondUnit is None:
                repl = Node.Node(type="number")

                if firstUnit is not None:
                    repl.unit = firstUnit
                elif secondUnit is not None:
                    repl.unit = secondUnit

                if operator == "plus":
                    repl.value = first.value + second.value
                elif operator == "minus":
                    repl.value = first.value - second.value
                elif operator == "mul":
                    repl.value = first.value * second.value
                elif operator == "div":
                    repl.value = first.value / second.value
                elif operator == "mod":
                    repl.value = first.value % second.value
                elif operator == "questionmark":
                    return first
                else:
                    raise VariableError("Unsupported number operation", parent)

                return repl

            elif firstUnit == "%" or secondUnit == "%":

                if operator in ("mul", "div"):
                    repl = Node.Node(type="number")

                    if operator == "mul":
                        repl.value = first.value * second.value / 100
                    elif operator == "mul":
                        repl.value = first.value / second.value / 100

                    if firstUnit == "%":
                        repl.unit = secondUnit
                    else:
                        repl.unit = firstUnit

                    return repl

                else:
                    raise VariableError("Could not compute mixed percent operations for operators other than \"*\" and \"/\"", node)

            else:
                raise VariableError("Could not compute result from numbers of different units: %s vs %s" % (first.unit, second.unit), parent)

        elif first.type == "string":
            repl = Node.Node(type="string")

            if operator == "plus":
                repl.value = first.value + second.value
                return repl
            else:
                raise VariableError("Unsupported string operation", parent)

        elif first.type == "list":
            if len(first) == len(second):
                repl = Node.Node(type="list")
                for pos, child in enumerate(first):
                    childRepl = __computeOperation(child, second[pos], parent, operator, values)
                    if childRepl is not None:
                        repl.append(childRepl)

                return repl

            else:
                raise VariableError("For list operations both lists have to have the same length!", parent)

        else:
            raise VariableError("Unsupported operation", parent)

    elif first.type == "list" and second.type != "list":
        repl = Node.Node(type="list")
        for child in first:
            childRepl = __computeOperation(child, second, parent, operator, values)
            if childRepl is not None:
                repl.append(childRepl)

        return repl

    elif first.type != "list" and second.type == "list":
        repl = Node.Node(type="list")
        for child in second:
            childRepl = __computeOperation(first, child, parent, operator, values)
            if childRepl is not None:
                repl.append(childRepl)

        return repl

    elif first.type == "string" or second.type == "string":
        repl = Node.Node(type="string")

        if operator == "plus":
            repl.value = str(first.value) + str(second.value)
            return repl
        else:
            raise VariableError("Unsupported string operation", parent)

    # Waiting for system method execution
    elif first.type == "system" or second.type == "system":
        return None

    else:
        raise VariableError("Different types in operation: %s vs %s" % (first.type, second.type), parent)

def __computeRecurser(node, scope, values):

    # Update scope of new block starts
    if hasattr(node, "scope"):
        scope = node.scope
        values = copy.copy(values)

        # Reset all local variables to None
        # which enforces not to keep values from outer scope
        for name in scope.modified:
            values[name] = None

    # Worked on copy to prevent issues during length changes (due removing declarations, etc.)
    for child in list(node):
        if child is not None:
            __computeRecurser(child, scope, values)

    # Support typical operators
    if node.type in ("plus", "minus", "mul", "div", "mod"):
        repl = __processOperator(node, values)
        if repl is not None:
            node.parent.replace(node, repl)

    # Not operator support
    elif node.type == "not":
        child = node[0]
        if child.type == "true":
            child.type = "false"
        elif child.type == "false":
            child.type = "true"
        else:
            raise VariableError("Could not apply not operator to non boolean variable", node)

        node.parent.replace(node, child)

    # Update values of variable
    elif (node.type == "declaration" and hasattr(node, "initializer")) or node.type == "assign":

        if node.type == "declaration":
            name = node.name
            init = node.initializer
            Console.debug("Found declaration of %s at line %s", name, node.line)

        else:
            name = node[0].name
            init = node[1]
            Console.debug("Found assignment of %s at line %s", name, node.line)

        # Modify value instead of replace when assign operator is set
        if hasattr(node, "assignOp") and node.assignOp is not None:
            if not name in values:
                raise VariableError("Assign operator is not supported as left hand variable is missing: %s" % name, node)

            repl = __computeOperation(values[name], init, node, node.assignOp, values)
            if repl is not None:
                values[name] = repl

        else:
            # Update internal variable mapping
            values[name] = init

        # Remove declaration node from tree
        node.parent.remove(node)

    # Replace variable with actual value
    elif node.type == "variable" and not (node.parent.type == "assign" and node.parent[0] is node):
        name = node.name
        if not name in values:
            raise VariableError("Could not resolve variable %s! Missing value!" % name, node)

        value = values[name]
        if value is None:
            raise VariableError("Could not resolve variable %s! Value is none!" % name, node)

        Console.debug("Resolving variable: %s at line %s with %s from %s", name, node.line, values[name].type, values[name].line)
        node.parent.replace(node, copy.deepcopy(values[name]))

    elif node.type in ("property", "selector") and getattr(node, "dynamic", False):
        def replacer(matchObj):
            name = matchObj.group(1)

            if not name in values:
                raise VariableError("Could not resolve variable %s! Missing value!" % name, node)

            value = values[name]
            if value is None:
                raise VariableError("Could not resolve variable %s! Value is none!" % name, node)

            if value.type == "identifier":
                return value.value
            elif value.type == "string":
                return value.value
            elif value.type == "number":
                return "%s%s" % (value.value, getattr(value, "unit", ""))
            else:
                raise VariableError("Could not replace property inline variable with value of type: %s" % value.type, node)

        # Fix all selectors
        if node.type == "selector":
            selectors = node.name
            for pos, selector in enumerate(selectors):
                selectors[pos] = RE_INLINE_VARIABLE.sub(replacer, selector)

        else:
            node.name = RE_INLINE_VARIABLE.sub(replacer, node.name)

<|endoftext|>


<commit_before>import subprocess
import time
from threading import Thread as thread

def merge_dict(a,b):
	for key in b:
		if a[key] is not None and isinstance(b[key], dict):
			merge_dict(a[key], b[key])
		else:
			a[key] = b[key]

def findProcess(processId ):
  ps= subprocess.Popen("ps -ef | grep "+processId, shell=True, stdout=subprocess.PIPE)
  output = ps.stdout.read()
  ps.stdout.close()
  ps.wait()
  return output

def isProcessRunning(processId):
  nameProcess = processId

  lastChar = processId[len(processId)-1]
  processId = processId[:-1]
  processId = processId + '[' + lastChar + ']'

  output = findProcess( processId )

  if nameProcess in output:
    return True
  else:
    return False

class interval(thread):
  def __init__(self,period, callback,params=None):
    self.time = period
    self.originalCallback = callback
    self.params = params or []
    self.isRunning = None
    thread.__init__(self)
    self.daemon = True

  def run(self):
    self.isRunning = True
    while self.isRunning:
      time.sleep(self.time)
      if self.isRunning:
        self.originalCallback(*self.params)

  def cancel(self):
    self.isRunning = False
<commit_msg>check if key exists in a<commit_after>import subprocess
import time
from threading import Thread as thread

def merge_dict(a,b):
	for key in b:
		if isinstance(b[key], dict) and key in a and isinstance(a[key], dict):
			merge_dict(a[key], b[key])
		else:
			a[key] = b[key]

def findProcess(processId ):
  ps= subprocess.Popen("ps -ef | grep "+processId, shell=True, stdout=subprocess.PIPE)
  output = ps.stdout.read()
  ps.stdout.close()
  ps.wait()
  return output

def isProcessRunning(processId):
  nameProcess = processId

  lastChar = processId[len(processId)-1]
  processId = processId[:-1]
  processId = processId + '[' + lastChar + ']'

  output = findProcess( processId )

  if nameProcess in output:
    return True
  else:
    return False

class interval(thread):
  def __init__(self,period, callback,params=None):
    self.time = period
    self.originalCallback = callback
    self.params = params or []
    self.isRunning = None
    thread.__init__(self)
    self.daemon = True

  def run(self):
    self.isRunning = True
    while self.isRunning:
      time.sleep(self.time)
      if self.isRunning:
        self.originalCallback(*self.params)

  def cancel(self):
    self.isRunning = False
<|endoftext|>


<commit_before>from django import forms
from django.core.exceptions import ValidationError
from django.conf import settings

from django.contrib.auth.models import Group
from .models import Poll, Category, Rule
from rapidsms.models import Contact
from mptt.forms import TreeNodeChoiceField

import re

class NewPollForm(forms.Form): # pragma: no cover

    TYPE_YES_NO = 'yn'

    type = forms.ChoiceField(
               required=True,
               choices=(
                    (TYPE_YES_NO, 'Yes/No Question'),
                ))

    def updateTypes(self):
        self.fields['type'].widget.choices += [(choice['type'], choice['label']) for choice in Poll.TYPE_CHOICES.values()]

    name = forms.CharField(max_length=32, required=True)
    question = forms.CharField(max_length=160, required=True)
    default_response = forms.CharField(max_length=160, required=True)
    start_immediately = forms.BooleanField(required=False)

    # This may seem like a hack, but this allows time for the Contact model's
    # default manage to be replaced at run-time.  There are many applications
    # for that, such as filtering contacts by site_id (as is done in the
    # authsites app, see github.com/daveycrockett/authsites).
    # This does, however, also make the polling app independent of authsites.
    def __init__(self, data=None, **kwargs):
        if data:
            forms.Form.__init__(self, data, **kwargs)
        else:
            forms.Form.__init__(self, **kwargs)
        self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all(), required=False)
        if hasattr(Contact, 'groups'):
            self.fields['groups'] = forms.ModelMultipleChoiceField(queryset=Group.objects.all(), required=False)

    def clean(self):
        cleaned_data = self.cleaned_data
        contacts = cleaned_data.get('contacts')
        groups = cleaned_data.get('groups')
        cleaned_data['question'] = cleaned_data.get('question').replace('%', '%%')
        cleaned_data['default_response'] = cleaned_data.get('default_response').replace('%', '%%')

        if not contacts and not groups:
            raise forms.ValidationError("You must provide a set of recipients (either a group or a contact)")

        return cleaned_data

class EditPollForm(forms.ModelForm): # pragma: no cover
    class Meta:
        model = Poll
        fields = ('name', 'default_response')

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_data['default_response'] = cleaned_data.get('default_response').replace('%', '%%')
        return cleaned_data

    # This may seem like a hack, but this allows time for the Contact model's
    # default manage to be replaced at run-time.  There are many applications
    # for that, such as filtering contacts by site_id (as is done in the
    # authsites app, see github.com/daveycrockett/authsites).
    # This does, however, also make the polling app independent of authsites.
    def __init__(self, data=None, **kwargs):
        if data:
            forms.ModelForm.__init__(self, data, **kwargs)
        else:
            forms.ModelForm.__init__(self, **kwargs)
        if 'instance' in kwargs:
            self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all(), initial=kwargs['instance'].contacts.all())
        else:
            self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all())

class CategoryForm(forms.ModelForm):
    name = forms.CharField(max_length=50, required=True)
    default = forms.BooleanField(required=False)
    response = forms.CharField(max_length=160, required=False)
    priority = forms.IntegerField(required=False, widget=forms.Select(
            choices=tuple([('', '---')] + [(i,"%d" % i) for i in range(1,11)])))
    color = forms.CharField(required=False, max_length=6, widget=forms.Select(choices=(
                (None, '---'),
                ('ff9977', 'red'),
                ('99ff77', 'green'),
                ('7799ff', 'blue'),
                ('ffff77', 'yellow'))))

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_data['response'] = cleaned_data.get('response').replace('%', '%%')
        return cleaned_data

    class Meta:
        model = Category
        fields = ('name', 'priority', 'color', 'default', 'response')

class RuleForm(forms.ModelForm):
    rule_string = forms.CharField(max_length=256, required=True)
    rule_type = forms.CharField(max_length=2, required=False, widget=forms.Select(choices=Rule.RULE_CHOICES))
    class Meta:
        model = Rule
        fields = ('rule_type', 'rule_string')

    def clean(self):
        cleaned_data = self.cleaned_data
        rule_string = cleaned_data.get('rule_string')
        rule_type = cleaned_data.get('rule_type')
        print rule_string
        if rule_type == 'r':
            try:
                re.compile(rule_string)
            except:
                self._errors['rule_string'] = self.error_class([u"You must provide a valid regular expression"])
                del cleaned_data['rule_string']

        # Always return the full collection of cleaned data.
        return cleaned_data<commit_msg>minor fix to default response cleaning<commit_after>from django import forms
from django.core.exceptions import ValidationError
from django.conf import settings

from django.contrib.auth.models import Group
from .models import Poll, Category, Rule
from rapidsms.models import Contact
from mptt.forms import TreeNodeChoiceField

import re

class NewPollForm(forms.Form): # pragma: no cover

    TYPE_YES_NO = 'yn'

    type = forms.ChoiceField(
               required=True,
               choices=(
                    (TYPE_YES_NO, 'Yes/No Question'),
                ))

    def updateTypes(self):
        self.fields['type'].widget.choices += [(choice['type'], choice['label']) for choice in Poll.TYPE_CHOICES.values()]

    name = forms.CharField(max_length=32, required=True)
    question = forms.CharField(max_length=160, required=True)
    default_response = forms.CharField(max_length=160, required=True)
    start_immediately = forms.BooleanField(required=False)

    # This may seem like a hack, but this allows time for the Contact model's
    # default manage to be replaced at run-time.  There are many applications
    # for that, such as filtering contacts by site_id (as is done in the
    # authsites app, see github.com/daveycrockett/authsites).
    # This does, however, also make the polling app independent of authsites.
    def __init__(self, data=None, **kwargs):
        if data:
            forms.Form.__init__(self, data, **kwargs)
        else:
            forms.Form.__init__(self, **kwargs)
        self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all(), required=False)
        if hasattr(Contact, 'groups'):
            self.fields['groups'] = forms.ModelMultipleChoiceField(queryset=Group.objects.all(), required=False)

    def clean(self):
        cleaned_data = self.cleaned_data
        contacts = cleaned_data.get('contacts')
        groups = cleaned_data.get('groups')
        cleaned_data['question'] = cleaned_data.get('question').replace('%', '%%')
        d = cleaned_data['default_response']
        if d:
            cleaned_data['default_response'] = d.replace('%', '%%')

        if not contacts and not groups:
            raise forms.ValidationError("You must provide a set of recipients (either a group or a contact)")

        return cleaned_data

class EditPollForm(forms.ModelForm): # pragma: no cover
    class Meta:
        model = Poll
        fields = ('name', 'default_response')

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_data['default_response'] = cleaned_data.get('default_response').replace('%', '%%')
        return cleaned_data

    # This may seem like a hack, but this allows time for the Contact model's
    # default manage to be replaced at run-time.  There are many applications
    # for that, such as filtering contacts by site_id (as is done in the
    # authsites app, see github.com/daveycrockett/authsites).
    # This does, however, also make the polling app independent of authsites.
    def __init__(self, data=None, **kwargs):
        if data:
            forms.ModelForm.__init__(self, data, **kwargs)
        else:
            forms.ModelForm.__init__(self, **kwargs)
        if 'instance' in kwargs:
            self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all(), initial=kwargs['instance'].contacts.all())
        else:
            self.fields['contacts'] = forms.ModelMultipleChoiceField(queryset=Contact.objects.all())

class CategoryForm(forms.ModelForm):
    name = forms.CharField(max_length=50, required=True)
    default = forms.BooleanField(required=False)
    response = forms.CharField(max_length=160, required=False)
    priority = forms.IntegerField(required=False, widget=forms.Select(
            choices=tuple([('', '---')] + [(i,"%d" % i) for i in range(1,11)])))
    color = forms.CharField(required=False, max_length=6, widget=forms.Select(choices=(
                (None, '---'),
                ('ff9977', 'red'),
                ('99ff77', 'green'),
                ('7799ff', 'blue'),
                ('ffff77', 'yellow'))))

    def clean(self):
        cleaned_data = self.cleaned_data
        cleaned_data['response'] = cleaned_data.get('response').replace('%', '%%')
        return cleaned_data

    class Meta:
        model = Category
        fields = ('name', 'priority', 'color', 'default', 'response')

class RuleForm(forms.ModelForm):
    rule_string = forms.CharField(max_length=256, required=True)
    rule_type = forms.CharField(max_length=2, required=False, widget=forms.Select(choices=Rule.RULE_CHOICES))
    class Meta:
        model = Rule
        fields = ('rule_type', 'rule_string')

    def clean(self):
        cleaned_data = self.cleaned_data
        rule_string = cleaned_data.get('rule_string')
        rule_type = cleaned_data.get('rule_type')
        print rule_string
        if rule_type == 'r':
            try:
                re.compile(rule_string)
            except:
                self._errors['rule_string'] = self.error_class([u"You must provide a valid regular expression"])
                del cleaned_data['rule_string']

        # Always return the full collection of cleaned data.
        return cleaned_data
<|endoftext|>


<commit_before>__author__ = 'aberklotz'

import html
import ssl
import re

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import requests

from geopy.geocoders import Nominatim

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)

class Parser:
    def retrieve(self, link):
        session = requests.Session()
        session.mount('https://', MyAdapter())

        page = session.get(link)
        return page.text

    def parse(self, text):
        crimes = []

        for match in re.finditer('
(.*?)
\n
(.*?)
(.*?)
\n
(.*?)
', text):
            title = self.__cleantext(match.group(1))
            date = self.__cleantext(match.group(2)[len("Zeit:"):])
            place = self.__cleantext(match.group(3)[len("Ort:"):])
            message = self.__cleantext(match.group(4))
            crime = Crime(title, date, place, message)
            crimes.append(crime)

        return crimes

    def __cleantext(self, text):
        clean_text = text.replace(" ", "")
        clean_text = clean_text.strip()
        return html.unescape(clean_text)

class Crime:
    def __init__(self, title, date, place, message, latitude, longitude):
        self.title = title
        self.date = date
        self.place = place
        self.message = message
        self.latitude = latitude
        self.longitude = longitude

    def has_geo_position(self):
        return self.latitude is not None and self.longitude is not None

    def __str__(self):
        return "Title: " + self.title + "\n" + "Date: " + self.date + "\n" + "Place: " + self.place + "\n"

if __name__ == "__main__":
    text_file = open("Output.txt", "r")
    text = text_file.read()
    text_file.close()

    parser = Parser()
    # text = parser.retrieve('https://www.polizei.sachsen.de/de/MI_2015_37021.htm')
    crimes = parser.parse(text)

    import database

    database2 = database.SillyFileSaver()
    database2.store_crimes(crimes)
    print(database2.load_crimes())
<commit_msg>bugfix wrong function signature<commit_after>__author__ = 'aberklotz'

import html
import ssl
import re

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import requests

from geopy.geocoders import Nominatim

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)

class Parser:
    def retrieve(self, link):
        session = requests.Session()
        session.mount('https://', MyAdapter())

        page = session.get(link)
        return page.text

    def parse(self, text):
        crimes = []

        for match in re.finditer('
(.*?)
\n
(.*?)
(.*?)
\n
(.*?)
', text):
            title = self.__cleantext(match.group(1))
            date = self.__cleantext(match.group(2)[len("Zeit:"):])
            place = self.__cleantext(match.group(3)[len("Ort:"):])
            message = self.__cleantext(match.group(4))
            crime = Crime(title, date, place, message, None, None)
            crimes.append(crime)

        return crimes

    def __cleantext(self, text):
        clean_text = text.replace(" ", "")
        clean_text = clean_text.strip()
        return html.unescape(clean_text)

class Crime:
    def __init__(self, title, date, place, message, latitude, longitude):
        self.title = title
        self.date = date
        self.place = place
        self.message = message
        self.latitude = latitude
        self.longitude = longitude

    def has_geo_position(self):
        return self.latitude is not None and self.longitude is not None

    def __str__(self):
        return "Title: " + self.title + "\n" + "Date: " + self.date + "\n" + "Place: " + self.place + "\n"

if __name__ == "__main__":
    text_file = open("Output.txt", "r")
    text = text_file.read()
    text_file.close()

    parser = Parser()
    # text = parser.retrieve('https://www.polizei.sachsen.de/de/MI_2015_37021.htm')
    crimes = parser.parse(text)

    import database

    database2 = database.SillyFileSaver()
    database2.store_crimes(crimes)
    print(database2.load_crimes())
<|endoftext|>


<commit_before>from pprint import pprint

from django import forms

from fo2.connections import db_cursor_so

from comercial.queries import get_tabela_preco
from systextil.models import Colecao

from utils.functions.strings import is_only_digits

class DisponibilidadeForm(forms.Form):
    CHOICES = [
        ('g', 'Todas as grades'),
        ('d', 'Apenas grades de disponibilidade'),
        ('t', 'Apenas grades totais'),
    ]
    apresenta = forms.ChoiceField(
        choices=CHOICES, initial='t')

    colecao = forms.ChoiceField(
        label='Coleção da referência',
        required=False, initial=None)

    tabela = forms.ChoiceField(
        label='Modelos da tabela de preços',
        required=False, initial=None)

    modelo = forms.CharField(
        required=False,
        min_length=1,
        max_length=5,
        widget=forms.TextInput(
            attrs={
                'size': 5,
                'type': 'number',
                'placeholder': '0',
            }
        )
    )

    referencia = forms.CharField(
        label='Referência',
        required=False,
        min_length=1,
        max_length=5,
        widget=forms.TextInput(
            attrs={
                'size': 5,
                'type': 'string',
                'style': 'text-transform:uppercase;',
                'placeholder': '0...',
            }
        )
    )

    CHOICES = [
        ('p', 'Lotes em paletes com quantidade no 63-CD'),
        ('n', 'Lotes independente de ser paletizado com quantidade em qualquer estágio'),
    ]
    tipo_inventario = forms.ChoiceField(
        label='Inventário',
        choices=CHOICES,
        initial='p'
    )

    corte_de = forms.DateField(
        label='Data de corte - De', required=False,
        widget=forms.DateInput(attrs={'type': 'date'}))

    corte_ate = forms.DateField(
        label='Até', required=False,
        widget=forms.DateInput(attrs={'type': 'date'}))

    CHOICES_PAGINADOR = [
        ('s', 'Sim'),
        ('n', 'Não'),
    ]
    usa_paginador = forms.ChoiceField(
        label='Utiliza paginador',
        choices=CHOICES_PAGINADOR, initial='s')

    page = forms.IntegerField(
        required=False, widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(DisponibilidadeForm, self).__init__(*args, **kwargs)

        cursor = db_cursor_so(self.request)

        CHOICES = [(None, '(Todas)')]
        colecoes = Colecao.objects.all().order_by('colecao')
        for colecao in colecoes:
            CHOICES.append((
                colecao.colecao,
                f"{colecao.colecao}-{colecao.descr_colecao}"
            ))
        self.fields['colecao'].choices = CHOICES

        CHOICES_TABELA = [(None, '--')]
        tabelas = get_tabela_preco(cursor, col=1, mes=1, order='d')
        for tabela in tabelas:
            codigo_tabela = "{:02d}.{:02d}.{:02d}".format(
                tabela['col_tabela_preco'],
                tabela['mes_tabela_preco'],
                tabela['seq_tabela_preco'],
            )
            CHOICES_TABELA.append((
                codigo_tabela,
                f"{codigo_tabela}-{tabela['descricao']}"
            ))
        self.fields['tabela'].choices = CHOICES_TABELA

    def clean_referencia(self):
        cleaned = self.cleaned_data['referencia']
        if len(cleaned) == 0:
            cleaned = ''
        else:
            if (
                len(cleaned) > 1 and
                cleaned[0].isalpha() and
                is_only_digits(cleaned[1:])
            ):
                cleaned = cleaned[0].upper() + cleaned[1:].upper().zfill(4)
            else:
                cleaned = cleaned.upper().zfill(5)

        data = self.data.copy()
        data['referencia'] = cleaned
        self.data = data
        return cleaned
<commit_msg>label<commit_after>from pprint import pprint

from django import forms

from fo2.connections import db_cursor_so

from comercial.queries import get_tabela_preco
from systextil.models import Colecao

from utils.functions.strings import is_only_digits

class DisponibilidadeForm(forms.Form):
    CHOICES = [
        ('g', 'Todas as grades'),
        ('d', 'Apenas grades de disponibilidade'),
        ('t', 'Apenas grades totais'),
    ]
    apresenta = forms.ChoiceField(
        choices=CHOICES, initial='t')

    colecao = forms.ChoiceField(
        label='Coleção da referência',
        required=False, initial=None)

    tabela = forms.ChoiceField(
        label='Modelos da tabela de preços',
        required=False, initial=None)

    modelo = forms.CharField(
        required=False,
        min_length=1,
        max_length=5,
        widget=forms.TextInput(
            attrs={
                'size': 5,
                'type': 'number',
                'placeholder': '0',
            }
        )
    )

    referencia = forms.CharField(
        label='Referência',
        required=False,
        min_length=1,
        max_length=5,
        widget=forms.TextInput(
            attrs={
                'size': 5,
                'type': 'string',
                'style': 'text-transform:uppercase;',
                'placeholder': '0...',
            }
        )
    )

    CHOICES = [
        ('p', 'Lotes em paletes com quantidade no 63-CD'),
        ('n', 'Lotes independente de ser paletizado com quantidade em qualquer estágio'),
    ]
    tipo_inventario = forms.ChoiceField(
        label='Inventário',
        choices=CHOICES,
        initial='p'
    )

    corte_de = forms.DateField(
        label='Data de corte inicial', required=False,
        widget=forms.DateInput(attrs={'type': 'date'}))

    corte_ate = forms.DateField(
        label='Data de corte final', required=False,
        widget=forms.DateInput(attrs={'type': 'date'}))

    CHOICES_PAGINADOR = [
        ('s', 'Sim'),
        ('n', 'Não'),
    ]
    usa_paginador = forms.ChoiceField(
        label='Utiliza paginador',
        choices=CHOICES_PAGINADOR, initial='s')

    page = forms.IntegerField(
        required=False, widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(DisponibilidadeForm, self).__init__(*args, **kwargs)

        cursor = db_cursor_so(self.request)

        CHOICES = [(None, '(Todas)')]
        colecoes = Colecao.objects.all().order_by('colecao')
        for colecao in colecoes:
            CHOICES.append((
                colecao.colecao,
                f"{colecao.colecao}-{colecao.descr_colecao}"
            ))
        self.fields['colecao'].choices = CHOICES

        CHOICES_TABELA = [(None, '--')]
        tabelas = get_tabela_preco(cursor, col=1, mes=1, order='d')
        for tabela in tabelas:
            codigo_tabela = "{:02d}.{:02d}.{:02d}".format(
                tabela['col_tabela_preco'],
                tabela['mes_tabela_preco'],
                tabela['seq_tabela_preco'],
            )
            CHOICES_TABELA.append((
                codigo_tabela,
                f"{codigo_tabela}-{tabela['descricao']}"
            ))
        self.fields['tabela'].choices = CHOICES_TABELA

    def clean_referencia(self):
        cleaned = self.cleaned_data['referencia']
        if len(cleaned) == 0:
            cleaned = ''
        else:
            if (
                len(cleaned) > 1 and
                cleaned[0].isalpha() and
                is_only_digits(cleaned[1:])
            ):
                cleaned = cleaned[0].upper() + cleaned[1:].upper().zfill(4)
            else:
                cleaned = cleaned.upper().zfill(5)

        data = self.data.copy()
        data['referencia'] = cleaned
        self.data = data
        return cleaned
<|endoftext|>


<commit_before>'''
Created on Feb 25, 2016

Support for Cyrillic Numerals
See UTN 41 for implementation information
http://www.unicode.org/notes/tn41/

This is a direct port of C++ OpenOffice code by Alexander Andreev: https://gerrit.libreoffice.org/#/c/20013/

@author: mike
'''

CU_THOUSAND = '\u0482'
CU_TITLO    = '\u0483'
CU_TEN      = '\u0456'
CU_800      = '\u047f'

CyrillicNumberCharArray = [
    ('\u0446', 900),
    ('\u047f', 800),
    ('\u0471', 700),
    ('\u0445', 600),
    ('\u0444', 500),
    ('\u0443', 400),
    ('\u0442', 300),
    ('\u0441', 200),
    ('\u0440', 100),
    ('\u0447',  90),
    ('\u043f',  80),
    ('\u047b',  70),
    ('\u046f',  60),
    ('\u043d',  50),
    ('\u043c',  40),
    ('\u043b',  30),
    ('\u043a',  20),
    ('\u0456',  10),
    ('\u0473',   9),
    ('\u0438',   8),
    ('\u0437',   7),
    ('\u0455',   6),
    ('\u0454',   5),
    ('\u0434',   4),
    ('\u0433',   3),
    ('\u0432',   2),
    ('\u0430',   1)
]

def numeral_string(value, *, add_titlo=True):

    if value < 0:
        return '-' + numeral_string(-value, add_titlo=add_titlo)

    if value == 0:
        if add_titlo:
            return '0' + CU_TITLO
        else:
            return '0'

    groups = _make_groups_of_thousands(value)

    if value < 10000:
        group = groups[0]
        if value >= 1000:
            group = groups[1] + groups[0]
        if add_titlo:
            _insert_titlo(group)
        if value >= 1000:
            group.insert(0, CU_THOUSAND)
        return ''.join(group)

    if add_titlo:
        for group in groups:
            _insert_titlo(group)

    if not groups[0]:
        # potential ambiguity - lets insert CU_THOUSAND before each digit in groups[1]
        _insert_thousand_before_each_numeral(groups[1])

    out = []
    while groups:
        group = groups.pop()
        if group:
            group = [CU_THOUSAND] * len(groups) + group
            out.append(''.join(group))

    return ' '.join(out)

def _insert_thousand_before_each_numeral(group):
    for i in reversed(range(1, len(group))):
        if group[i] != CU_TITLO:
            group.insert(i, CU_THOUSAND)

def _make_small_number(value):
    ''' generates array of characters representing small Church Slavonic
        number. No titlo.'''
    assert 0 <= value < 1000, value

    out = []
    for c,x in CyrillicNumberCharArray:
        if value <= 0:
            break

        if 10 < value < 20:
            out.extend(_make_small_number(value - 10))
            out.append(CU_TEN)
            break

        if x <= value:
            out.append(c)
            value -= x

    return out

def _make_groups_of_thousands(value):
    '''returns groups of thousands as a list (in reverse order):
       Decimal  123456789 is split like this:
       123 456 789 then reversed to 789 456 123
       and finally each group is represented as Church Slavonic number:
       [ [789], [456], [123] ]
       reversal of order is just for technical convenience
    '''
    assert value > 0

    groups = []

    num = value
    while num > 0:
        groups.append(_make_small_number(num % 1000))
        num //= 1000

    return groups

def _insert_titlo(group):

    if len(group) == 0:
        return

    elif len(group) == 1 or group[-2] == CU_800:
        group.append(CU_TITLO)

    else:
        group.insert(-1, CU_TITLO)

<commit_msg>better comment<commit_after>'''
Created on Feb 25, 2016

Support for Cyrillic Numerals
See UTN 41 for implementation information
http://www.unicode.org/notes/tn41/

Code was based on C++ OpenOffice code by Aleksandr Andreev: https://gerrit.libreoffice.org/#/c/20013/
but eventually rewritten to better matched desired behavior on large numbers

@author: mike
'''

CU_THOUSAND = '\u0482'
CU_TITLO    = '\u0483'
CU_TEN      = '\u0456'
CU_800      = '\u047f'

CyrillicNumberCharArray = [
    ('\u0446', 900),
    ('\u047f', 800),
    ('\u0471', 700),
    ('\u0445', 600),
    ('\u0444', 500),
    ('\u0443', 400),
    ('\u0442', 300),
    ('\u0441', 200),
    ('\u0440', 100),
    ('\u0447',  90),
    ('\u043f',  80),
    ('\u047b',  70),
    ('\u046f',  60),
    ('\u043d',  50),
    ('\u043c',  40),
    ('\u043b',  30),
    ('\u043a',  20),
    ('\u0456',  10),
    ('\u0473',   9),
    ('\u0438',   8),
    ('\u0437',   7),
    ('\u0455',   6),
    ('\u0454',   5),
    ('\u0434',   4),
    ('\u0433',   3),
    ('\u0432',   2),
    ('\u0430',   1)
]

def numeral_string(value, *, add_titlo=True):

    if value < 0:
        return '-' + numeral_string(-value, add_titlo=add_titlo)

    if value == 0:
        if add_titlo:
            return '0' + CU_TITLO
        else:
            return '0'

    groups = _make_groups_of_thousands(value)

    if value < 10000:
        group = groups[0]
        if value >= 1000:
            group = groups[1] + groups[0]
        if add_titlo:
            _insert_titlo(group)
        if value >= 1000:
            group.insert(0, CU_THOUSAND)
        return ''.join(group)

    if add_titlo:
        for group in groups:
            _insert_titlo(group)

    if not groups[0]:
        # potential ambiguity - lets insert CU_THOUSAND before each digit in groups[1]
        _insert_thousand_before_each_numeral(groups[1])

    out = []
    while groups:
        group = groups.pop()
        if group:
            group = [CU_THOUSAND] * len(groups) + group
            out.append(''.join(group))

    return ' '.join(out)

def _insert_thousand_before_each_numeral(group):
    for i in reversed(range(1, len(group))):
        if group[i] != CU_TITLO:
            group.insert(i, CU_THOUSAND)

def _make_small_number(value):
    ''' generates array of characters representing small Church Slavonic
        number. No titlo.'''
    assert 0 <= value < 1000, value

    out = []
    for c,x in CyrillicNumberCharArray:
        if value <= 0:
            break

        if 10 < value < 20:
            out.extend(_make_small_number(value - 10))
            out.append(CU_TEN)
            break

        if x <= value:
            out.append(c)
            value -= x

    return out

def _make_groups_of_thousands(value):
    '''returns groups of thousands as a list (in reverse order):
       Decimal  123456789 is split like this:
       123 456 789 then reversed to 789 456 123
       and finally each group is represented as Church Slavonic number:
       [ [789], [456], [123] ]
       reversal of order is just for technical convenience
    '''
    assert value > 0

    groups = []

    num = value
    while num > 0:
        groups.append(_make_small_number(num % 1000))
        num //= 1000

    return groups

def _insert_titlo(group):

    if len(group) == 0:
        return

    elif len(group) == 1 or group[-2] == CU_800:
        group.append(CU_TITLO)

    else:
        group.insert(-1, CU_TITLO)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# ctypeslib package

from pkg_resources import get_distribution, DistributionNotFound
import os.path

try:
    _dist = get_distribution('ctypeslib2')
    # Normalize case for Windows systems
    # if you are in a virtualenv, ./local/* are aliases to ./*
    dist_loc = os.path.normcase(os.path.realpath(_dist.location))
    here = os.path.normcase(os.path.realpath(__file__))
    if not here.startswith(os.path.join(dist_loc, 'ctypeslib')):
        # not installed, but there is another version that *is*
        raise DistributionNotFound
except DistributionNotFound:
    __version__ = 'Please install this project with setup.py'
else:
    __version__ = _dist.version

# configure python-clang to use the local clang library
try:
    from ctypes.util import find_library
    # debug for python-haystack travis-ci
    for version in ["libclang", "clang", "clang-6.0", "clang-5.0", "clang-4.0", "clang-3.9", "clang-3.8", "clang-3.7"]:
        if find_library(version) is not None:
            from clang import cindex
            cindex.Config.set_library_file(find_library(version))
            break
except ImportError as e:
    print(e)

<commit_msg>quick patch to load clang11<commit_after># -*- coding: utf-8 -*-
# ctypeslib package

from pkg_resources import get_distribution, DistributionNotFound
import os.path

try:
    _dist = get_distribution('ctypeslib2')
    # Normalize case for Windows systems
    # if you are in a virtualenv, ./local/* are aliases to ./*
    dist_loc = os.path.normcase(os.path.realpath(_dist.location))
    here = os.path.normcase(os.path.realpath(__file__))
    if not here.startswith(os.path.join(dist_loc, 'ctypeslib')):
        # not installed, but there is another version that *is*
        raise DistributionNotFound
except DistributionNotFound:
    __version__ = 'Please install this project with setup.py'
else:
    __version__ = _dist.version

# configure python-clang to use the local clang library
try:
    from ctypes.util import find_library
    # debug for python-haystack travis-ci
    for version in ["libclang", "clang", "clang-11", "clang-5.0", "clang-4.0", "clang-3.9", "clang-3.8", "clang-3.7"]:
        if find_library(version) is not None:
            from clang import cindex
            cindex.Config.set_library_file(find_library(version))
            break
except ImportError as e:
    print(e)

<|endoftext|>


<commit_before>
from collections import OrderedDict

from PIL import Image
import sdl2 as sdl

from cubix.core.pycompat import *
from cubix.core.opengl import gl, pgl

class BaseTexture(object):
    ''' Texture manager'''
    # Just for clarity
    # This is basically a static variable
    # It will be for assigning each texture unit id easily.
    # We use the id for calculating GL_TEXTUREi. This value propagates
    # down into the class instances also, even as it is changed. :D
    textureCount = 0
    def _set_unit_id(self):
        self.texUnitID = self.textureCount
        Texture.textureCount += 1

    def load_gl(self):

        self.texSampID = self.program.new_uniform(b'textureSampler')

        # Load image into new opengl texture
        self.texID = pgl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texID)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)

        pgl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, self.width, self.height,
                         0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, self.data)

    def bind(self):
        gl.glActiveTexture(gl.GL_TEXTURE0 + self.texUnitID)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texID)
        self.program.set_uniform(self.texSampID, self.texUnitID)

class Texture(BaseTexture):

    def __init__(self, path, program, type=''):

        self.path = path
        self.program = program

        self._set_unit_id()

        self.load_image()
        self.load_gl()

    def load_image(self):
        img = Image.open(self.path)

        # Verify that the image is in RGBA format
        if ''.join(img.getbands()) != 'RGBA':
            img = img.convert('RGBA')

        # Get image data as a list
        self.data = list(img.getdata())

        self.width, self.height = img.size

class TextureAtlas(BaseTexture):
    def __init__(self, program):

        self.program = program

        self._set_unit_id()

        # Data format will be as follows:
        #    Indexed by textureID
        #    value:
        #        - a second dict with information about that texture
        #            - x, y position in texture, width and height, texture data/uvcoords
        self.textures = []
        self.data = 0
        self.maxWidth = 1024

    def add_texture(self, width, height, texData):
        textureID = len(self.textures)

        self.textures.append({
                'x1':0, 'x2':0, 'y1':0, 'y2':0,      # Calculated by calc_image
                'width': width, 'height': height,
                'texData': textData, 'uvCoords': None, # uvCords Calculated by calc_image
        })
        return textureID

    def get_uvcoords(self):
        coordData = []
        # This will return a list of all of the uvcoords indexed by textureID
        for tex in self.textures:
            x1 = tex['x1'] / self.width
            x2 = tex['x2'] / self.width
            y1 = tex['y1'] / self.height
            y2 = tex['y2'] / self.height

            coordData.append((x1, x2, y1, y2))

        return coordData

    def get_coords(self):
        coordData = []
        # This will return a list of all of the coord data indexed by textureID
        for tex in self.textures:
            x1 = tex['x1']
            x2 = tex['x2']
            y1 = tex['y1']
            y2 = tex['y2']

            coordData.append((x1, x2, y1, y2))

        return coordData

    def get_vertex_coords(self):
        coordData = []
        # This will return a list of all of the coord data indexed by textureID
        #for tex in self.textures:
            width = tex['width']
            height = tex['height']

            aspectRatio = width / height

            y1 = tex['y1']
            y2 = tex['y2']

            #coordData.append((x1, x2, y1, y2))

        return coordData

    def calc_image(self):

        # Go through all registered textures and calculate the total
        # image size for the atlas plus the coords for image location.

        self.width = 0
        self.height = 0
        cursorPosX = 0
        cursorPosY = 0

        for tex in self.textures:
            imgWidth = tex['width'] + 1
            imgHeight = tex['height'] + 1

            if cursorPosX + imgWidth >= self.maxWidth:

                self.width = max(self.width, cursorPosX)
                self.height += cursorPosY

                cursorPosY = 0
                cursorPosX = 0

            tex['x1'] = cursorPosX
            tex['x2'] = cursorPosX + imgWidth
            tex['y1'] = cursorPosY
            tex['y2'] = cursorPosY + imgHeight

            cursorPosX += imgWidth
            cursorPosY += max(cursorPosY, imgHeight)

        self.width = max(self.width, cursorPosX)
        self.height += cursorPosY

    def gen_atlas(self):

        self.calc_image()
        self.load_gl()

        # add data to blank gl texture with
        # glTexSubImage2D here
        for tex in self.textures:
            x1 = tex['x1']
            y1 = tex['y1']
            width = tex['width']
            height = tex['height']

            texData = tex['texData']

            pgl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, x1, y1, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texData)
<commit_msg>Fixes an nameing issue on my texture counter.<commit_after>
from collections import OrderedDict

from PIL import Image
import sdl2 as sdl

from cubix.core.pycompat import *
from cubix.core.opengl import gl, pgl

class BaseTexture(object):
    ''' Texture manager'''
    # Just for clarity
    # This is basically a static variable
    # It will be for assigning each texture unit id easily.
    # We use the id for calculating GL_TEXTUREi. This value propagates
    # down into the class instances also, even as it is changed. :D
    _textureCount = 0
    def _set_unit_id(self):
        self.texUnitID = self._textureCount
        BaseTexture._textureCount += 1

    def load_gl(self):

        self.texSampID = self.program.new_uniform(b'textureSampler')

        # Load image into new opengl texture
        self.texID = pgl.glGenTextures(1)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texID)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT, 1)

        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)

        pgl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, self.width, self.height,
                         0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, self.data)

    def bind(self):
        gl.glActiveTexture(gl.GL_TEXTURE0 + self.texUnitID)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.texID)
        self.program.set_uniform(self.texSampID, self.texUnitID)

class Texture(BaseTexture):

    def __init__(self, path, program, type=''):

        self.path = path
        self.program = program

        self._set_unit_id()

        self.load_image()
        self.load_gl()

    def load_image(self):
        img = Image.open(self.path)

        # Verify that the image is in RGBA format
        if ''.join(img.getbands()) != 'RGBA':
            img = img.convert('RGBA')

        # Get image data as a list
        self.data = list(img.getdata())

        self.width, self.height = img.size

class TextureAtlas(BaseTexture):
    def __init__(self, program):

        self.program = program

        self._set_unit_id()

        # Data format will be as follows:
        #    Indexed by textureID
        #    value:
        #        - a second dict with information about that texture
        #            - x, y position in texture, width and height, texture data/uvcoords
        self.textures = []
        self.data = 0
        self.maxWidth = 1024

    def add_texture(self, width, height, texData):
        textureID = len(self.textures)

        self.textures.append({
                'x1':0, 'x2':0, 'y1':0, 'y2':0,      # Calculated by calc_image
                'width': width, 'height': height,
                'texData': textData, 'uvCoords': None, # uvCords Calculated by calc_image
        })
        return textureID

    def get_uvcoords(self):
        coordData = []
        # This will return a list of all of the uvcoords indexed by textureID
        for tex in self.textures:
            x1 = tex['x1'] / self.width
            x2 = tex['x2'] / self.width
            y1 = tex['y1'] / self.height
            y2 = tex['y2'] / self.height

            coordData.append((x1, x2, y1, y2))

        return coordData

    def get_coords(self):
        coordData = []
        # This will return a list of all of the coord data indexed by textureID
        for tex in self.textures:
            x1 = tex['x1']
            x2 = tex['x2']
            y1 = tex['y1']
            y2 = tex['y2']

            coordData.append((x1, x2, y1, y2))

        return coordData

    def get_vertex_coords(self):
        coordData = []
        # This will return a list of all of the coord data indexed by textureID
        for tex in self.textures:
            width = tex['width']
            height = tex['height']

            aspectRatio = width / height

            y1 = tex['y1']
            y2 = tex['y2']

            #coordData.append((x1, x2, y1, y2))

        return coordData

    def calc_image(self):

        # Go through all registered textures and calculate the total
        # image size for the atlas plus the coords for image location.

        self.width = 0
        self.height = 0
        cursorPosX = 0
        cursorPosY = 0

        for tex in self.textures:
            imgWidth = tex['width'] + 1
            imgHeight = tex['height'] + 1

            if cursorPosX + imgWidth >= self.maxWidth:

                self.width = max(self.width, cursorPosX)
                self.height += cursorPosY

                cursorPosY = 0
                cursorPosX = 0

            tex['x1'] = cursorPosX
            tex['x2'] = cursorPosX + imgWidth
            tex['y1'] = cursorPosY
            tex['y2'] = cursorPosY + imgHeight

            cursorPosX += imgWidth
            cursorPosY += max(cursorPosY, imgHeight)

        self.width = max(self.width, cursorPosX)
        self.height += cursorPosY

    def gen_atlas(self):

        self.calc_image()
        self.load_gl()

        # add data to blank gl texture with
        # glTexSubImage2D here
        for tex in self.textures:
            x1 = tex['x1']
            y1 = tex['y1']
            width = tex['width']
            height = tex['height']

            texData = tex['texData']

            pgl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, x1, y1, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texData)
<|endoftext|>


<commit_before># Copyright: (c) 2018, Huawei Inc.
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

class ModuleDocFragment(object):

    # HWC doc fragment.
    DOCUMENTATION = '''
options:
    identity_endpoint:
        description:
            - The Identity authentication URL.
        type: str
        required: true
    user:
        description:
            - The user name to login with (currently only user names are
              supported, and not user IDs).
        type: str
        required: true
    password:
        description:
            - The password to login with.
        type: str
        required: true
    domain:
        description:
            - The name of the Domain to scope to (Identity v3, currently only
              domain names are supported, and not domain IDs).
        type: str
        required: true
    project:
        description:
            - The name of the Tenant (Identity v2) or Project (Identity v3).
              (currently only project names are supported, and not
               project IDs).
        type: str
        required: true
    region:
        description:
            - The region to which the project belongs.
        type: str
        required: true
    timeouts:
        description:
            - The timeouts for create/update/delete operation.
        type: dict
        suboptions:
            create:
                description:
                    - The timeouts for create operation.
                type: str
                default: '10m'
            update:
                description:
                    - The timeouts for update operation.
                type: str
                default: '10m'
            delete:
                description:
                    - The timeouts for delete operation.
                type: str
    id:
        description:
            - The id of resource to be managed.
        type: str
notes:
  - For authentication, you can set identity_endpoint using the
    C(ANSIBLE_HWC_IDENTITY_ENDPOINT) env variable.
  - For authentication, you can set user using the
    C(ANSIBLE_HWC_USER) env variable.
  - For authentication, you can set password using the C(ANSIBLE_HWC_PASSWORD) env
    variable.
  - For authentication, you can set domain using the C(ANSIBLE_HWC_DOMAIN) env
    variable.
  - For authentication, you can set project using the C(ANSIBLE_HWC_PROJECT) env
    variable.
  - For authentication, you can set region using the C(ANSIBLE_HWC_REGION) env variable.
  - Environment variables values will only be used if the playbook values are
    not set.
'''
<commit_msg>typo fix (#55424)<commit_after># Copyright: (c) 2018, Huawei Inc.
# GNU General Public License v3.0+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

class ModuleDocFragment(object):

    # HWC doc fragment.
    DOCUMENTATION = '''
options:
    identity_endpoint:
        description:
            - The Identity authentication URL.
        type: str
        required: true
    user:
        description:
            - The user name to login with (currently only user names are
              supported, and not user IDs).
        type: str
        required: true
    password:
        description:
            - The password to login with.
        type: str
        required: true
    domain:
        description:
            - The name of the Domain to scope to (Identity v3).
              (currently only domain names are supported, and not domain IDs).
        type: str
        required: true
    project:
        description:
            - The name of the Tenant (Identity v2) or Project (Identity v3).
              (currently only project names are supported, and not
               project IDs).
        type: str
        required: true
    region:
        description:
            - The region to which the project belongs.
        type: str
        required: true
    timeouts:
        description:
            - The timeouts for create/update/delete operation.
        type: dict
        suboptions:
            create:
                description:
                    - The timeouts for create operation.
                type: str
                default: '10m'
            update:
                description:
                    - The timeouts for update operation.
                type: str
                default: '10m'
            delete:
                description:
                    - The timeouts for delete operation.
                type: str
    id:
        description:
            - The id of resource to be managed.
        type: str
notes:
  - For authentication, you can set identity_endpoint using the
    C(ANSIBLE_HWC_IDENTITY_ENDPOINT) env variable.
  - For authentication, you can set user using the
    C(ANSIBLE_HWC_USER) env variable.
  - For authentication, you can set password using the C(ANSIBLE_HWC_PASSWORD) env
    variable.
  - For authentication, you can set domain using the C(ANSIBLE_HWC_DOMAIN) env
    variable.
  - For authentication, you can set project using the C(ANSIBLE_HWC_PROJECT) env
    variable.
  - For authentication, you can set region using the C(ANSIBLE_HWC_REGION) env variable.
  - Environment variables values will only be used if the playbook values are
    not set.
'''
<|endoftext|>


<commit_before>from __future__ import absolute_import
import collections
import logging
import os
import stat
import uuid
from functools import partial
from tempfile import NamedTemporaryFile

import requests
from typing import Any, Callable, Dict, Iterable, List, Set, Text, Tuple, Union

import schema_salad.validate as validate
from schema_salad.ref_resolver import uri_file_path
from schema_salad.sourceline import SourceLine
from six.moves import urllib

from .utils import convert_pathsep_to_unix

from .stdfsaccess import StdFsAccess, abspath

_logger = logging.getLogger("cwltool")

MapperEnt = collections.namedtuple("MapperEnt", ["resolved", "target", "type", "staged"])

def adjustFiles(rec, op):  # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply a mapping function to each File path in the object `rec`."""

    if isinstance(rec, dict):
        if rec.get("class") == "File":
            rec["path"] = op(rec["path"])
        for d in rec:
            adjustFiles(rec[d], op)
    if isinstance(rec, list):
        for d in rec:
            adjustFiles(d, op)

def visit_class(rec, cls, op):  # type: (Any, Iterable, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply a function to with "class" in cls."""

    if isinstance(rec, dict):
        if "class" in rec and rec.get("class") in cls:
            op(rec)
        for d in rec:
            visit_class(rec[d], cls, op)
    if isinstance(rec, list):
        for d in rec:
            visit_class(d, cls, op)

def adjustFileObjs(rec, op):  # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply an update function to each File object in the object `rec`."""
    visit_class(rec, ("File",), op)

def adjustDirObjs(rec, op):
    # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply an update function to each Directory object in the object `rec`."""
    visit_class(rec, ("Directory",), op)

def normalizeFilesDirs(job):
    # type: (Union[List[Dict[Text, Any]], Dict[Text, Any]]) -> None
    def addLocation(d):
        if "location" not in d:
            if d["class"] == "File" and ("contents" not in d):
                raise validate.ValidationException("Anonymous file object must have 'contents' and 'basename' fields.")
            if d["class"] == "Directory" and ("listing" not in d or "basename" not in d):
                raise validate.ValidationException(
                    "Anonymous directory object must have 'listing' and 'basename' fields.")
            d["location"] = "_:" + Text(uuid.uuid4())
            if "basename" not in d:
                d["basename"] = d["location"][2:]

        parse = urllib.parse.urlparse(d["location"])
        path = parse.path
        # strip trailing slash
        if path.endswith("/"):
            if d["class"] != "Directory":
                raise validate.ValidationException(
                    "location '%s' ends with '/' but is not a Directory" % d["location"])
            path = path.rstrip("/")
            d["location"] = urllib.parse.urlunparse((parse.scheme, parse.netloc, path, parse.params, parse.query, parse.fragment))

        if "basename" not in d:
            d["basename"] = os.path.basename(urllib.request.url2pathname(path))

        if d["class"] == "File":
            d["nameroot"], d["nameext"] = os.path.splitext(d["basename"])

    visit_class(job, ("File", "Directory"), addLocation)

def dedup(listing):  # type: (List[Any]) -> List[Any]
    marksub = set()

    def mark(d):
        marksub.add(d["location"])

    for l in listing:
        if l["class"] == "Directory":
            for e in l.get("listing", []):
                adjustFileObjs(e, mark)
                adjustDirObjs(e, mark)

    dd = []
    markdup = set()  # type: Set[Text]
    for r in listing:
        if r["location"] not in marksub and r["location"] not in markdup:
            dd.append(r)
            markdup.add(r["location"])

    return dd

def get_listing(fs_access, rec, recursive=True):
    # type: (StdFsAccess, Dict[Text, Any], bool) -> None
    if "listing" in rec:
        return
    listing = []
    loc = rec["location"]
    for ld in fs_access.listdir(loc):
        parse = urllib.parse.urlparse(ld)
        bn = os.path.basename(urllib.request.url2pathname(parse.path))
        if fs_access.isdir(ld):
            ent = {u"class": u"Directory",
                   u"location": ld,
                   u"basename": bn}
            if recursive:
                get_listing(fs_access, ent, recursive)
            listing.append(ent)
        else:
            listing.append({"class": "File", "location": ld, "basename": bn})
    rec["listing"] = listing

def trim_listing(obj):
    """Remove 'listing' field from Directory objects that are file references.

    It redundant and potentially expensive to pass fully enumerated Directory
    objects around if not explicitly needed, so delete the 'listing' field when
    it is safe to do so.

    """

    if obj.get("location", "").startswith("file://") and "listing" in obj:
        del obj["listing"]

# Download http Files
def downloadHttpFile(httpurl):
        r = requests.get(httpurl, stream=True)
        with NamedTemporaryFile(mode='wb', delete=False) as f:
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)
        r.close()
        return f.name

class PathMapper(object):
    """Mapping of files from relative path provided in the file to a tuple of
    (absolute local path, absolute container path)

    The tao of PathMapper:

    The initializer takes a list of File and Directory objects, a base
    directory (for resolving relative references) and a staging directory
    (where the files are mapped to).

    The purpose of the setup method is to determine where each File or
    Directory should be placed on the target file system (relative to
    stagedir).

    If separatedirs=True, unrelated files will be isolated in their own
    directories under stagedir. If separatedirs=False, files and directories
    will all be placed in stagedir (with the possibility for name
    collisions...)

    The path map maps the "location" of the input Files and Directory objects
    to a tuple (resolved, target, type). The "resolved" field is the "real"
    path on the local file system (after resolving relative paths and
    traversing symlinks). The "target" is the path on the target file system
    (under stagedir). The type is the object type (one of File, Directory,
    CreateFile, WritableFile).

    The latter two (CreateFile, WritableFile) are used by
    InitialWorkDirRequirement to indicate files that are generated on the fly
    (CreateFile, in this case "resolved" holds the file contents instead of the
    path because they file doesn't exist) or copied into the output directory
    so they can be opened for update ("r+" or "a") (WritableFile).

    """

    def __init__(self, referenced_files, basedir, stagedir, separateDirs=True):
        # type: (List[Any], Text, Text, bool) -> None
        self._pathmap = {}  # type: Dict[Text, MapperEnt]
        self.stagedir = stagedir
        self.separateDirs = separateDirs
        self.setup(dedup(referenced_files), basedir)

    def visitlisting(self, listing, stagedir, basedir, copy=False, staged=False):
        # type: (List[Dict[Text, Any]], Text, Text, bool, bool) -> None
        for ld in listing:
            self.visit(ld, stagedir, basedir, copy=ld.get("writable", copy), staged=staged)

    def visit(self, obj, stagedir, basedir, copy=False, staged=False):
        # type: (Dict[Text, Any], Text, Text, bool, bool) -> None
        tgt = convert_pathsep_to_unix(
            os.path.join(stagedir, obj["basename"]))
        if obj["location"] in self._pathmap:
            return
        if obj["class"] == "Directory":
            if obj["location"].startswith("file://"):
                resolved = uri_file_path(obj["location"])
            else:
                resolved = obj["location"]
            self._pathmap[obj["location"]] = MapperEnt(resolved, tgt, "WritableDirectory" if copy else "Directory", staged)
            if obj["location"].startswith("file://"):
                staged = False
            self.visitlisting(obj.get("listing", []), tgt, basedir, copy=copy, staged=staged)
        elif obj["class"] == "File":
            path = obj["location"]
            ab = abspath(path, basedir)
            if "contents" in obj and obj["location"].startswith("_:"):
                self._pathmap[obj["location"]] = MapperEnt(obj["contents"], tgt, "CreateFile", staged)
            else:
                with SourceLine(obj, "location", validate.ValidationException):
                    deref = ab
                    if urllib.parse.urlsplit(deref).scheme in ['http','https']:
                        deref = downloadHttpFile(path)
                    else:
                        # Dereference symbolic links
                        st = os.lstat(deref)
                        while stat.S_ISLNK(st.st_mode):
                            rl = os.readlink(deref)
                            deref = rl if os.path.isabs(rl) else os.path.join(
                                os.path.dirname(deref), rl)
                            st = os.lstat(deref)

                    self._pathmap[path] = MapperEnt(deref, tgt, "WritableFile" if copy else "File", staged)
                    self.visitlisting(obj.get("secondaryFiles", []), stagedir, basedir, copy=copy, staged=staged)

    def setup(self, referenced_files, basedir):
        # type: (List[Any], Text) -> None

        # Go through each file and set the target to its own directory along
        # with any secondary files.
        stagedir = self.stagedir
        for fob in referenced_files:
            if self.separateDirs:
                stagedir = os.path.join(self.stagedir, "stg%s" % uuid.uuid4())
            self.visit(fob, stagedir, basedir, copy=fob.get("writable"), staged=True)

    def mapper(self, src):  # type: (Text) -> MapperEnt
        if u"#" in src:
            i = src.index(u"#")
            p = self._pathmap[src[:i]]
            return MapperEnt(p.resolved, p.target + src[i:], p.type, p.staged)
        else:
            return self._pathmap[src]

    def files(self):  # type: () -> List[Text]
        return list(self._pathmap.keys())

    def items(self):  # type: () -> List[Tuple[Text, MapperEnt]]
        return list(self._pathmap.items())

    def reversemap(self, target):  # type: (Text) -> Tuple[Text, Text]
        for k, v in self._pathmap.items():
            if v[1] == target:
                return (k, v[0])
        return None

    def update(self, key, resolved, target, type, stage):  # type: (Text, Text, Text, Text, bool) -> None
        self._pathmap[key] = MapperEnt(resolved, target, type, stage)

    def __contains__(self, key):
        return key in self._pathmap
<commit_msg>changing chunksize to 16 kb<commit_after>from __future__ import absolute_import
import collections
import logging
import os
import stat
import uuid
from functools import partial
from tempfile import NamedTemporaryFile

import requests
from typing import Any, Callable, Dict, Iterable, List, Set, Text, Tuple, Union

import schema_salad.validate as validate
from schema_salad.ref_resolver import uri_file_path
from schema_salad.sourceline import SourceLine
from six.moves import urllib

from .utils import convert_pathsep_to_unix

from .stdfsaccess import StdFsAccess, abspath

_logger = logging.getLogger("cwltool")

MapperEnt = collections.namedtuple("MapperEnt", ["resolved", "target", "type", "staged"])

def adjustFiles(rec, op):  # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply a mapping function to each File path in the object `rec`."""

    if isinstance(rec, dict):
        if rec.get("class") == "File":
            rec["path"] = op(rec["path"])
        for d in rec:
            adjustFiles(rec[d], op)
    if isinstance(rec, list):
        for d in rec:
            adjustFiles(d, op)

def visit_class(rec, cls, op):  # type: (Any, Iterable, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply a function to with "class" in cls."""

    if isinstance(rec, dict):
        if "class" in rec and rec.get("class") in cls:
            op(rec)
        for d in rec:
            visit_class(rec[d], cls, op)
    if isinstance(rec, list):
        for d in rec:
            visit_class(d, cls, op)

def adjustFileObjs(rec, op):  # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply an update function to each File object in the object `rec`."""
    visit_class(rec, ("File",), op)

def adjustDirObjs(rec, op):
    # type: (Any, Union[Callable[..., Any], partial[Any]]) -> None
    """Apply an update function to each Directory object in the object `rec`."""
    visit_class(rec, ("Directory",), op)

def normalizeFilesDirs(job):
    # type: (Union[List[Dict[Text, Any]], Dict[Text, Any]]) -> None
    def addLocation(d):
        if "location" not in d:
            if d["class"] == "File" and ("contents" not in d):
                raise validate.ValidationException("Anonymous file object must have 'contents' and 'basename' fields.")
            if d["class"] == "Directory" and ("listing" not in d or "basename" not in d):
                raise validate.ValidationException(
                    "Anonymous directory object must have 'listing' and 'basename' fields.")
            d["location"] = "_:" + Text(uuid.uuid4())
            if "basename" not in d:
                d["basename"] = d["location"][2:]

        parse = urllib.parse.urlparse(d["location"])
        path = parse.path
        # strip trailing slash
        if path.endswith("/"):
            if d["class"] != "Directory":
                raise validate.ValidationException(
                    "location '%s' ends with '/' but is not a Directory" % d["location"])
            path = path.rstrip("/")
            d["location"] = urllib.parse.urlunparse((parse.scheme, parse.netloc, path, parse.params, parse.query, parse.fragment))

        if "basename" not in d:
            d["basename"] = os.path.basename(urllib.request.url2pathname(path))

        if d["class"] == "File":
            d["nameroot"], d["nameext"] = os.path.splitext(d["basename"])

    visit_class(job, ("File", "Directory"), addLocation)

def dedup(listing):  # type: (List[Any]) -> List[Any]
    marksub = set()

    def mark(d):
        marksub.add(d["location"])

    for l in listing:
        if l["class"] == "Directory":
            for e in l.get("listing", []):
                adjustFileObjs(e, mark)
                adjustDirObjs(e, mark)

    dd = []
    markdup = set()  # type: Set[Text]
    for r in listing:
        if r["location"] not in marksub and r["location"] not in markdup:
            dd.append(r)
            markdup.add(r["location"])

    return dd

def get_listing(fs_access, rec, recursive=True):
    # type: (StdFsAccess, Dict[Text, Any], bool) -> None
    if "listing" in rec:
        return
    listing = []
    loc = rec["location"]
    for ld in fs_access.listdir(loc):
        parse = urllib.parse.urlparse(ld)
        bn = os.path.basename(urllib.request.url2pathname(parse.path))
        if fs_access.isdir(ld):
            ent = {u"class": u"Directory",
                   u"location": ld,
                   u"basename": bn}
            if recursive:
                get_listing(fs_access, ent, recursive)
            listing.append(ent)
        else:
            listing.append({"class": "File", "location": ld, "basename": bn})
    rec["listing"] = listing

def trim_listing(obj):
    """Remove 'listing' field from Directory objects that are file references.

    It redundant and potentially expensive to pass fully enumerated Directory
    objects around if not explicitly needed, so delete the 'listing' field when
    it is safe to do so.

    """

    if obj.get("location", "").startswith("file://") and "listing" in obj:
        del obj["listing"]

# Download http Files
def downloadHttpFile(httpurl):
        r = requests.get(httpurl, stream=True)
        with NamedTemporaryFile(mode='wb', delete=False) as f:
            for chunk in r.iter_content(chunk_size=16384):
                if chunk:  # filter out keep-alive new chunks
                    f.write(chunk)
        r.close()
        return f.name

class PathMapper(object):
    """Mapping of files from relative path provided in the file to a tuple of
    (absolute local path, absolute container path)

    The tao of PathMapper:

    The initializer takes a list of File and Directory objects, a base
    directory (for resolving relative references) and a staging directory
    (where the files are mapped to).

    The purpose of the setup method is to determine where each File or
    Directory should be placed on the target file system (relative to
    stagedir).

    If separatedirs=True, unrelated files will be isolated in their own
    directories under stagedir. If separatedirs=False, files and directories
    will all be placed in stagedir (with the possibility for name
    collisions...)

    The path map maps the "location" of the input Files and Directory objects
    to a tuple (resolved, target, type). The "resolved" field is the "real"
    path on the local file system (after resolving relative paths and
    traversing symlinks). The "target" is the path on the target file system
    (under stagedir). The type is the object type (one of File, Directory,
    CreateFile, WritableFile).

    The latter two (CreateFile, WritableFile) are used by
    InitialWorkDirRequirement to indicate files that are generated on the fly
    (CreateFile, in this case "resolved" holds the file contents instead of the
    path because they file doesn't exist) or copied into the output directory
    so they can be opened for update ("r+" or "a") (WritableFile).

    """

    def __init__(self, referenced_files, basedir, stagedir, separateDirs=True):
        # type: (List[Any], Text, Text, bool) -> None
        self._pathmap = {}  # type: Dict[Text, MapperEnt]
        self.stagedir = stagedir
        self.separateDirs = separateDirs
        self.setup(dedup(referenced_files), basedir)

    def visitlisting(self, listing, stagedir, basedir, copy=False, staged=False):
        # type: (List[Dict[Text, Any]], Text, Text, bool, bool) -> None
        for ld in listing:
            self.visit(ld, stagedir, basedir, copy=ld.get("writable", copy), staged=staged)

    def visit(self, obj, stagedir, basedir, copy=False, staged=False):
        # type: (Dict[Text, Any], Text, Text, bool, bool) -> None
        tgt = convert_pathsep_to_unix(
            os.path.join(stagedir, obj["basename"]))
        if obj["location"] in self._pathmap:
            return
        if obj["class"] == "Directory":
            if obj["location"].startswith("file://"):
                resolved = uri_file_path(obj["location"])
            else:
                resolved = obj["location"]
            self._pathmap[obj["location"]] = MapperEnt(resolved, tgt, "WritableDirectory" if copy else "Directory", staged)
            if obj["location"].startswith("file://"):
                staged = False
            self.visitlisting(obj.get("listing", []), tgt, basedir, copy=copy, staged=staged)
        elif obj["class"] == "File":
            path = obj["location"]
            ab = abspath(path, basedir)
            if "contents" in obj and obj["location"].startswith("_:"):
                self._pathmap[obj["location"]] = MapperEnt(obj["contents"], tgt, "CreateFile", staged)
            else:
                with SourceLine(obj, "location", validate.ValidationException):
                    deref = ab
                    if urllib.parse.urlsplit(deref).scheme in ['http','https']:
                        deref = downloadHttpFile(path)
                    else:
                        # Dereference symbolic links
                        st = os.lstat(deref)
                        while stat.S_ISLNK(st.st_mode):
                            rl = os.readlink(deref)
                            deref = rl if os.path.isabs(rl) else os.path.join(
                                os.path.dirname(deref), rl)
                            st = os.lstat(deref)

                    self._pathmap[path] = MapperEnt(deref, tgt, "WritableFile" if copy else "File", staged)
                    self.visitlisting(obj.get("secondaryFiles", []), stagedir, basedir, copy=copy, staged=staged)

    def setup(self, referenced_files, basedir):
        # type: (List[Any], Text) -> None

        # Go through each file and set the target to its own directory along
        # with any secondary files.
        stagedir = self.stagedir
        for fob in referenced_files:
            if self.separateDirs:
                stagedir = os.path.join(self.stagedir, "stg%s" % uuid.uuid4())
            self.visit(fob, stagedir, basedir, copy=fob.get("writable"), staged=True)

    def mapper(self, src):  # type: (Text) -> MapperEnt
        if u"#" in src:
            i = src.index(u"#")
            p = self._pathmap[src[:i]]
            return MapperEnt(p.resolved, p.target + src[i:], p.type, p.staged)
        else:
            return self._pathmap[src]

    def files(self):  # type: () -> List[Text]
        return list(self._pathmap.keys())

    def items(self):  # type: () -> List[Tuple[Text, MapperEnt]]
        return list(self._pathmap.items())

    def reversemap(self, target):  # type: (Text) -> Tuple[Text, Text]
        for k, v in self._pathmap.items():
            if v[1] == target:
                return (k, v[0])
        return None

    def update(self, key, resolved, target, type, stage):  # type: (Text, Text, Text, Text, bool) -> None
        self._pathmap[key] = MapperEnt(resolved, target, type, stage)

    def __contains__(self, key):
        return key in self._pathmap
<|endoftext|>


<commit_before>#-*-coding:utf8;-*-
#qpy:2

import sys,os

reload(sys)

sys.setdefaultencoding('utf8')

def lang(self):
        if self == "pt":
            return "português"
        elif self == "en":
            return "inglês"
        elif self == "ar":
            return "árabe"
        elif self == "af":
            return "africanês"
        elif self == "es":
            return "espanhol"
        elif self == "de":
            return "alemão"
        elif self == "ca":
            return "Catalão"
        elif self == "zn-CN":
            return "Chinês Simples"
        elif self == "zn-TW":
            return "Chinês Tradicional"
        elif self == "tl":
            return "Filipino"
        elif self == "fr":
            return "Francês"
        elif self == "haw":
            return "Havaiano"
        elif self == 'it':
            return "Italiano"
        elif self == "ja":
            return "Japonês"
        elif self == "kn":
            return "Canadense"
        elif self == "la":
            return "Latin"
        elif self == "ne":
            return "Nepali"
        elif self == "fa":
            return "Persa"
        elif self == "ru":
            return "Russo"
        elif self == "sl":
            return "eslovaca"
        else:
            return "🙇"
<commit_msg>Delete Taylang.py<commit_after><|endoftext|>