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

'''Preprocess a C source file using gcc and convert the result into
   a token stream

Reference is C99:
  * http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: preprocessor.py 663 2007-02-17 05:52:03Z Alex.Holkner $'

import os, re, shlex, sys, compiler, errno, lex, yacc, traceback
import ctypes
from lex import TOKEN

tokens = (
    'HEADER_NAME', 'IDENTIFIER', 'PP_NUMBER', 'CHARACTER_CONSTANT',
    'STRING_LITERAL', 'OTHER',

    'PTR_OP', 'INC_OP', 'DEC_OP', 'LEFT_OP', 'RIGHT_OP', 'LE_OP', 'GE_OP',
    'EQ_OP', 'NE_OP', 'AND_OP', 'OR_OP', 'MUL_ASSIGN', 'DIV_ASSIGN',
    'MOD_ASSIGN', 'ADD_ASSIGN', 'SUB_ASSIGN', 'LEFT_ASSIGN', 'RIGHT_ASSIGN',
    'AND_ASSIGN', 'XOR_ASSIGN', 'OR_ASSIGN',  'PERIOD', 'ELLIPSIS',

    'LPAREN', 'NEWLINE'
)

subs = {
    'D': '[0-9]',
    'L': '[a-zA-Z_]',
    'H': '[a-fA-F0-9]',
    'E': '[Ee][+-]?{D}+',
    'FS': '[FflL]',
    'IS': '[uUlL]*',
}
# Helper: substitute {foo} with subs[foo] in string (makes regexes more lexy)
sub_pattern = re.compile('{([^}]*)}')
def sub_repl_match(m):
    return subs[m.groups()[0]]
def sub(s):
    return sub_pattern.sub(sub_repl_match, s)
CHARACTER_CONSTANT = sub(r"L?'(\\.|[^\\'])+'")
STRING_LITERAL = sub(r'L?"(\\.|[^\\"])*"')
IDENTIFIER = sub('{L}({L}|{D})*')

# --------------------------------------------------------------------------
# Token value types
# --------------------------------------------------------------------------

# Numbers represented as int and float types.
# For all other tokens, type is just str representation.

class StringLiteral(str):
    def __new__(cls, value):
        assert value[0] == '"' and value[-1] == '"'
        # Unescaping probably not perfect but close enough.
        value = value[1:-1].decode('string_escape')
        return str.__new__(cls, value)

class SystemHeaderName(str):
    def __new__(cls, value):
        assert value[0] == '<' and value[-1] == '>'
        return str.__new__(cls, value[1:-1])

    def __repr__(self):
        return '<%s>' % (str(self))

# --------------------------------------------------------------------------
# Token declarations
# --------------------------------------------------------------------------

punctuators = {
    # value: (regex, type)
    r'...': (r'\.\.\.', 'ELLIPSIS'),
    r'>>=': (r'>>=', 'RIGHT_ASSIGN'),
    r'<<=': (r'<<=', 'LEFT_ASSIGN'),
    r'+=': (r'\+=', 'ADD_ASSIGN'),
    r'-=': (r'-=', 'SUB_ASSIGN'),
    r'*=': (r'\*=', 'MUL_ASSIGN'),
    r'/=': (r'/=', 'DIV_ASSIGN'),
    r'%=': (r'%=', 'MOD_ASSIGN'),
    r'&=': (r'&=', 'AND_ASSIGN'),
    r'^=': (r'\^=', 'XOR_ASSIGN'),
    r'|=': (r'\|=', 'OR_ASSIGN'),
    r'>>': (r'>>', 'RIGHT_OP'),
    r'<<': (r'<<', 'LEFT_OP'),
    r'++': (r'\+\+', 'INC_OP'),
    r'--': (r'--', 'DEC_OP'),
    r'->': (r'->', 'PTR_OP'),
    r'&&': (r'&&', 'AND_OP'),
    r'||': (r'\|\|', 'OR_OP'),
    r'<=': (r'<=', 'LE_OP'),
    r'>=': (r'>=', 'GE_OP'),
    r'==': (r'==', 'EQ_OP'),
    r'!=': (r'!=', 'NE_OP'),
    r'<:': (r'<:', '['),
    r':>': (r':>', ']'),
    r'<%': (r'<%', '{'),
    r'%>': (r'%>', '}'),
    r';': (r';', ';'),
    r'{': (r'{', '{'),
    r'}': (r'}', '}'),
    r',': (r',', ','),
    r':': (r':', ':'),
    r'=': (r'=', '='),
    r')': (r'\)', ')'),
    r'[': (r'\[', '['),
    r']': (r']', ']'),
    r'.': (r'\.', 'PERIOD'),
    r'&': (r'&', '&'),
    r'!': (r'!', '!'),
    r'~': (r'~', '~'),
    r'-': (r'-', '-'),
    r'+': (r'\+', '+'),
    r'*': (r'\*', '*'),
    r'/': (r'/', '/'),
    r'%': (r'%', '%'),
    r'<': (r'<', '<'),
    r'>': (r'>', '>'),
    r'^': (r'\^', '^'),
    r'|': (r'\|', '|'),
    r'?': (r'\?', '?')
}

def punctuator_regex(punctuators):
    punctuator_regexes = [v[0] for v in punctuators.values()]
    punctuator_regexes.sort(lambda a, b: -cmp(len(a), len(b)))
    return '(%s)' % '|'.join(punctuator_regexes)

# Process line-number directives from the preprocessor
# See http://docs.freebsd.org/info/cpp/cpp.info.Output.html
def t_directive(t):
    r'\#\s+(\d+)\s+"([^"]+)"[ \d]*\n'
    t.lexer.filename = t.groups[2]
    t.lexer.lineno = int(t.groups[1])
    return None

DIRECTIVE = re.compile(t_directive.__doc__)

@TOKEN(punctuator_regex(punctuators))
def t_punctuator(t):
    t.type = punctuators[t.value][1]
    return t

@TOKEN(IDENTIFIER)
def t_identifier(t):
    if t.value == 'defined':
        t.type = 'DEFINED'
    else:
        t.type = 'IDENTIFIER'
    return t

    # missing: universal-character-constant
@TOKEN(sub(r'({D}|\.{D})({D}|{L}|e[+-]|E[+-]|p[+-]|P[+-]|\.)*'))
def t_pp_number(t):
    t.type = 'PP_NUMBER'
    return t

@TOKEN(CHARACTER_CONSTANT)
def t_character_constant(t):
    t.type = 'CHARACTER_CONSTANT'
    return t

@TOKEN(STRING_LITERAL)
def t_string_literal(t):
    t.type = 'STRING_LITERAL'
    t.value = StringLiteral(t.value)
    return t

def t_lparen(t):
    r'\('
    if t.lexpos == 0 or t.lexer.lexdata[t.lexpos-1] not in (' \t\f\v\n'):
        t.type = 'LPAREN'
    else:
        t.type = '('
    return t

def t_newline(t):
    r'\n'
    t.lexer.lineno += 1
    return None

def t_error(t):
    t.type = 'OTHER'
    return t

t_ignore = ' \t\v\f\r'

# --------------------------------------------------------------------------
# Lexers
# --------------------------------------------------------------------------

class PreprocessorLexer(lex.Lexer):
    def __init__(self):
        lex.Lexer.__init__(self)
        self.filename = '<input>'

    def input(self, data, filename=None):
        if filename:
            self.filename = filename
        self.lasttoken = None
        self.input_stack = []

        lex.Lexer.input(self, data)

    def push_input(self, data, filename):
        self.input_stack.append(
            (self.lexdata, self.lexpos, self.filename, self.lineno))
        self.lexdata = data
        self.lexpos = 0
        self.lineno = 1
        self.filename = filename
        self.lexlen = len(self.lexdata)

    def pop_input(self):
        self.lexdata, self.lexpos, self.filename, self.lineno = \
            self.input_stack.pop()
        self.lexlen = len(self.lexdata)

    def token(self):
        result = lex.Lexer.token(self)
        while result is None and self.input_stack:
            self.pop_input()
            result = lex.Lexer.token(self)

        if result:
            self.lasttoken = result.type
            result.filename = self.filename
        else:
            self.lasttoken = None

        return result

class TokenListLexer(object):
    def __init__(self, tokens):
        self.tokens = tokens
        self.pos = 0

    def token(self):
        if self.pos < len(self.tokens):
            t = self.tokens[self.pos]
            self.pos += 1
            return t
        else:
            return None

def symbol_to_token(sym):
    if isinstance(sym, yacc.YaccSymbol):
        return sym.value
    elif isinstance(sym, lex.LexToken):
        return sym
    else:
        assert False, 'Not a symbol: %r' % sym

def create_token(type, value, production=None):
    '''Create a token of type and value, at the position where 'production'
    was reduced.  Don't specify production if the token is built-in'''
    t = lex.LexToken()
    t.type = type
    t.value = value
    t.lexpos = -1
    if production:
        t.lineno = production.slice[1].lineno
        t.filename = production.slice[1].filename
    else:
        t.lineno = -1
        t.filename = '<builtin>'
    return t

# --------------------------------------------------------------------------
# Grammars
# --------------------------------------------------------------------------

DEFINE = re.compile("#define\s+(\w+)(?:\(([^)]+)\))?(?:\s+(.+))?")

# Extract variable names and/or strings from input data
ADJACENT_STRINGS = re.compile(r'"(?:\\.|[^\\"])*"|'
                              r'\b(\w+)\s+(?=["\w])|'
                              r'(?<=["\w])\s+(\w+)\b((?= "))?')

class PreprocessorDefine(object):
    def __init__(self, name, code, vars):
        self.deps = []
        self.argnames = []
        self.name = name
        self.emitted = False
        self.succeeded = False
        self.vars = vars
        self.code = code
        self.node = compiler.parse(code)
        try:
            compiler.walk(self.node, self)
        except:
            #print "Could not parse %s: %s" % (name, self.code)
            #traceback.print_exc(limit=0, file=sys.stdout)
            self.emitted = True

    def visitName(self, node):
        if node.name not in self.argnames:
            self.deps.append(self.vars[node.name])

    def visitLambda(self, node):
        self.argnames = node.argnames
        compiler.walk(node.code, self)

    def emit(self, file):
        if errno.__dict__.has_key(self.name):
            print >>file, 'from errno import %s' % self.name
            self.succeeded = True
        else:
            try:
                if not self.emitted:
                    self.emitted = True
                    for d in self.deps:
                        if d and not d.emit(file):
                            return False
                    print >>file, '%s = %s' % (self.name, self.code)
                    self.succeeded = True
            except:
                pass
        return self.succeeded

class PreprocessorParser(object):
    def __init__(self):
        self.cpp = "gcc -E"
        self.flags = ""
        self.defines = []
        self.include_path = []
        self.matches = []
        self.output = []
        self.lexer = lex.lex(cls=PreprocessorLexer)

    def parse(self, filename):
        """Parse a file and save its output"""

        for path in self.include_path:
            self.flags += " -I%s" % path

        for define in self.defines:
            self.flags += " -D%s" % define

        print "%s -U __GNUC__ -dD %s %s" % (self.cpp, self.flags, filename)

        f = os.popen("%s -U __GNUC__ -dD %s %s" % (self.cpp, self.flags, filename))

        code = []
        for line in f:
            m = DEFINE.match(line)
            if m:
                self.matches.append((filename,) + m.groups())

            m = DIRECTIVE.match(line)
            if m:
                filename = m.group(2)

            if line.startswith("#") and not line.startswith("# "):
                # Replace any directives other than line numbers
                # with placeholder empty lines
                code.append("\n")
            else:
                code.append(line)

        self.lexer.input("".join(code))
        self.output = []
        while True:
            token = self.lexer.token()
            if token is not None:
                self.output.append(token)
            else:
                break

    def emit(self, file, regex, all_names, all_headers, headers,
             imported_symbols):
        """Emit all of the preprocessor symbols which match the
           specified REGEX, along with their dependencies"""

        defines = {}
        vars = {}
        replacements = []
        value = None

        # Anything defined in ctypes or imported_symbols is fair game
        for container in ctypes.__dict__, imported_symbols, all_names:
            for key in container:
                defines[key] = None
                vars[key] = None

        # NULL in C is just 'None' in Python
        defines['None'] = None
        defines['NULL'] = PreprocessorDefine('NULL', 'None', defines)

        QC_CONDITIONAL = re.compile(r" \? (.+?) : ")

        for (filename, name, params, code) in self.matches:

            # If not specified, define to "1"
            if code is None:
                code = "1"

            # Convert the value into a function if necessary
            if params:
                code = "lambda %s: (%s)" % (params, code)

            # Try to execute the code
            try:
                value = eval(code, vars)
            except:
                # Looks like the code doesn't execute. Let's fix any string
                # variables which pop up adjacent to strings, and make sure
                # that they're added together correctly.
                if not params:
                    def repl(m):
                        (a,b,c) = m.groups()
                        if (a and a in vars and isinstance(vars[a], str)):
                            return r"%s + " % a
                        elif (b and b in vars and isinstance(vars[b], str)):
                            if c is not None:
                                return r" + %s +" % b
                            else:
                                return r" + %s" % b
                        else:
                            return m.group()

                    code = ADJACENT_STRINGS.sub(repl, code)

                code = code.replace(" || ", " or ")
                code = code.replace(" && ", " and ")
                code = QC_CONDITIONAL.sub(r" and \1 or ", code)

                # Try to execute the modified code
                try:
                    value = eval(code, vars)
                except Exception, e:
                    # It still didn't work. Next!
                    continue

            # Save the calculated value
            vars[name] = value

            # Save this definition
            defines[name] = PreprocessorDefine(name, code, defines)

            # If this is a match, output it
            if (name not in imported_symbols and
                (not regex or regex.match(name)) and
                (all_headers or filename in headers)):
                defines[name].emit(file)

<commit_msg>* preprocessor.py (PreprocessorParser.__init__): Add some defines to help us   parse C99 headers on Mac OS X<commit_after>#!/usr/bin/env python

'''Preprocess a C source file using gcc and convert the result into
   a token stream

Reference is C99:
  * http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: preprocessor.py 663 2007-02-17 05:52:03Z Alex.Holkner $'

import os, re, shlex, sys, compiler, errno, lex, yacc, traceback
import ctypes
from lex import TOKEN

tokens = (
    'HEADER_NAME', 'IDENTIFIER', 'PP_NUMBER', 'CHARACTER_CONSTANT',
    'STRING_LITERAL', 'OTHER',

    'PTR_OP', 'INC_OP', 'DEC_OP', 'LEFT_OP', 'RIGHT_OP', 'LE_OP', 'GE_OP',
    'EQ_OP', 'NE_OP', 'AND_OP', 'OR_OP', 'MUL_ASSIGN', 'DIV_ASSIGN',
    'MOD_ASSIGN', 'ADD_ASSIGN', 'SUB_ASSIGN', 'LEFT_ASSIGN', 'RIGHT_ASSIGN',
    'AND_ASSIGN', 'XOR_ASSIGN', 'OR_ASSIGN',  'PERIOD', 'ELLIPSIS',

    'LPAREN', 'NEWLINE'
)

subs = {
    'D': '[0-9]',
    'L': '[a-zA-Z_]',
    'H': '[a-fA-F0-9]',
    'E': '[Ee][+-]?{D}+',
    'FS': '[FflL]',
    'IS': '[uUlL]*',
}
# Helper: substitute {foo} with subs[foo] in string (makes regexes more lexy)
sub_pattern = re.compile('{([^}]*)}')
def sub_repl_match(m):
    return subs[m.groups()[0]]
def sub(s):
    return sub_pattern.sub(sub_repl_match, s)
CHARACTER_CONSTANT = sub(r"L?'(\\.|[^\\'])+'")
STRING_LITERAL = sub(r'L?"(\\.|[^\\"])*"')
IDENTIFIER = sub('{L}({L}|{D})*')

# --------------------------------------------------------------------------
# Token value types
# --------------------------------------------------------------------------

# Numbers represented as int and float types.
# For all other tokens, type is just str representation.

class StringLiteral(str):
    def __new__(cls, value):
        assert value[0] == '"' and value[-1] == '"'
        # Unescaping probably not perfect but close enough.
        value = value[1:-1].decode('string_escape')
        return str.__new__(cls, value)

class SystemHeaderName(str):
    def __new__(cls, value):
        assert value[0] == '<' and value[-1] == '>'
        return str.__new__(cls, value[1:-1])

    def __repr__(self):
        return '<%s>' % (str(self))

# --------------------------------------------------------------------------
# Token declarations
# --------------------------------------------------------------------------

punctuators = {
    # value: (regex, type)
    r'...': (r'\.\.\.', 'ELLIPSIS'),
    r'>>=': (r'>>=', 'RIGHT_ASSIGN'),
    r'<<=': (r'<<=', 'LEFT_ASSIGN'),
    r'+=': (r'\+=', 'ADD_ASSIGN'),
    r'-=': (r'-=', 'SUB_ASSIGN'),
    r'*=': (r'\*=', 'MUL_ASSIGN'),
    r'/=': (r'/=', 'DIV_ASSIGN'),
    r'%=': (r'%=', 'MOD_ASSIGN'),
    r'&=': (r'&=', 'AND_ASSIGN'),
    r'^=': (r'\^=', 'XOR_ASSIGN'),
    r'|=': (r'\|=', 'OR_ASSIGN'),
    r'>>': (r'>>', 'RIGHT_OP'),
    r'<<': (r'<<', 'LEFT_OP'),
    r'++': (r'\+\+', 'INC_OP'),
    r'--': (r'--', 'DEC_OP'),
    r'->': (r'->', 'PTR_OP'),
    r'&&': (r'&&', 'AND_OP'),
    r'||': (r'\|\|', 'OR_OP'),
    r'<=': (r'<=', 'LE_OP'),
    r'>=': (r'>=', 'GE_OP'),
    r'==': (r'==', 'EQ_OP'),
    r'!=': (r'!=', 'NE_OP'),
    r'<:': (r'<:', '['),
    r':>': (r':>', ']'),
    r'<%': (r'<%', '{'),
    r'%>': (r'%>', '}'),
    r';': (r';', ';'),
    r'{': (r'{', '{'),
    r'}': (r'}', '}'),
    r',': (r',', ','),
    r':': (r':', ':'),
    r'=': (r'=', '='),
    r')': (r'\)', ')'),
    r'[': (r'\[', '['),
    r']': (r']', ']'),
    r'.': (r'\.', 'PERIOD'),
    r'&': (r'&', '&'),
    r'!': (r'!', '!'),
    r'~': (r'~', '~'),
    r'-': (r'-', '-'),
    r'+': (r'\+', '+'),
    r'*': (r'\*', '*'),
    r'/': (r'/', '/'),
    r'%': (r'%', '%'),
    r'<': (r'<', '<'),
    r'>': (r'>', '>'),
    r'^': (r'\^', '^'),
    r'|': (r'\|', '|'),
    r'?': (r'\?', '?')
}

def punctuator_regex(punctuators):
    punctuator_regexes = [v[0] for v in punctuators.values()]
    punctuator_regexes.sort(lambda a, b: -cmp(len(a), len(b)))
    return '(%s)' % '|'.join(punctuator_regexes)

# Process line-number directives from the preprocessor
# See http://docs.freebsd.org/info/cpp/cpp.info.Output.html
def t_directive(t):
    r'\#\s+(\d+)\s+"([^"]+)"[ \d]*\n'
    t.lexer.filename = t.groups[2]
    t.lexer.lineno = int(t.groups[1])
    return None

DIRECTIVE = re.compile(t_directive.__doc__)

@TOKEN(punctuator_regex(punctuators))
def t_punctuator(t):
    t.type = punctuators[t.value][1]
    return t

@TOKEN(IDENTIFIER)
def t_identifier(t):
    if t.value == 'defined':
        t.type = 'DEFINED'
    else:
        t.type = 'IDENTIFIER'
    return t

    # missing: universal-character-constant
@TOKEN(sub(r'({D}|\.{D})({D}|{L}|e[+-]|E[+-]|p[+-]|P[+-]|\.)*'))
def t_pp_number(t):
    t.type = 'PP_NUMBER'
    return t

@TOKEN(CHARACTER_CONSTANT)
def t_character_constant(t):
    t.type = 'CHARACTER_CONSTANT'
    return t

@TOKEN(STRING_LITERAL)
def t_string_literal(t):
    t.type = 'STRING_LITERAL'
    t.value = StringLiteral(t.value)
    return t

def t_lparen(t):
    r'\('
    if t.lexpos == 0 or t.lexer.lexdata[t.lexpos-1] not in (' \t\f\v\n'):
        t.type = 'LPAREN'
    else:
        t.type = '('
    return t

def t_newline(t):
    r'\n'
    t.lexer.lineno += 1
    return None

def t_error(t):
    t.type = 'OTHER'
    return t

t_ignore = ' \t\v\f\r'

# --------------------------------------------------------------------------
# Lexers
# --------------------------------------------------------------------------

class PreprocessorLexer(lex.Lexer):
    def __init__(self):
        lex.Lexer.__init__(self)
        self.filename = '<input>'

    def input(self, data, filename=None):
        if filename:
            self.filename = filename
        self.lasttoken = None
        self.input_stack = []

        lex.Lexer.input(self, data)

    def push_input(self, data, filename):
        self.input_stack.append(
            (self.lexdata, self.lexpos, self.filename, self.lineno))
        self.lexdata = data
        self.lexpos = 0
        self.lineno = 1
        self.filename = filename
        self.lexlen = len(self.lexdata)

    def pop_input(self):
        self.lexdata, self.lexpos, self.filename, self.lineno = \
            self.input_stack.pop()
        self.lexlen = len(self.lexdata)

    def token(self):
        result = lex.Lexer.token(self)
        while result is None and self.input_stack:
            self.pop_input()
            result = lex.Lexer.token(self)

        if result:
            self.lasttoken = result.type
            result.filename = self.filename
        else:
            self.lasttoken = None

        return result

class TokenListLexer(object):
    def __init__(self, tokens):
        self.tokens = tokens
        self.pos = 0

    def token(self):
        if self.pos < len(self.tokens):
            t = self.tokens[self.pos]
            self.pos += 1
            return t
        else:
            return None

def symbol_to_token(sym):
    if isinstance(sym, yacc.YaccSymbol):
        return sym.value
    elif isinstance(sym, lex.LexToken):
        return sym
    else:
        assert False, 'Not a symbol: %r' % sym

def create_token(type, value, production=None):
    '''Create a token of type and value, at the position where 'production'
    was reduced.  Don't specify production if the token is built-in'''
    t = lex.LexToken()
    t.type = type
    t.value = value
    t.lexpos = -1
    if production:
        t.lineno = production.slice[1].lineno
        t.filename = production.slice[1].filename
    else:
        t.lineno = -1
        t.filename = '<builtin>'
    return t

# --------------------------------------------------------------------------
# Grammars
# --------------------------------------------------------------------------

DEFINE = re.compile("#define\s+(\w+)(?:\(([^)]+)\))?(?:\s+(.+))?")

# Extract variable names and/or strings from input data
ADJACENT_STRINGS = re.compile(r'"(?:\\.|[^\\"])*"|'
                              r'\b(\w+)\s+(?=["\w])|'
                              r'(?<=["\w])\s+(\w+)\b((?= "))?')

class PreprocessorDefine(object):
    def __init__(self, name, code, vars):
        self.deps = []
        self.argnames = []
        self.name = name
        self.emitted = False
        self.succeeded = False
        self.vars = vars
        self.code = code
        self.node = compiler.parse(code)
        try:
            compiler.walk(self.node, self)
        except:
            #print "Could not parse %s: %s" % (name, self.code)
            #traceback.print_exc(limit=0, file=sys.stdout)
            self.emitted = True

    def visitName(self, node):
        if node.name not in self.argnames:
            self.deps.append(self.vars[node.name])

    def visitLambda(self, node):
        self.argnames = node.argnames
        compiler.walk(node.code, self)

    def emit(self, file):
        if errno.__dict__.has_key(self.name):
            print >>file, 'from errno import %s' % self.name
            self.succeeded = True
        else:
            try:
                if not self.emitted:
                    self.emitted = True
                    for d in self.deps:
                        if d and not d.emit(file):
                            return False
                    print >>file, '%s = %s' % (self.name, self.code)
                    self.succeeded = True
            except:
                pass
        return self.succeeded

class PreprocessorParser(object):
    def __init__(self):
        self.cpp = "gcc -E"
        self.flags = ""
        self.defines = ["inline=", "__inline__=", "__extension__=",
                        "_Bool=uint8_t"]
        self.include_path = []
        self.matches = []
        self.output = []
        self.lexer = lex.lex(cls=PreprocessorLexer)

    def parse(self, filename):
        """Parse a file and save its output"""

        for path in self.include_path:
            self.flags += " -I%s" % path

        for define in self.defines:
            self.flags += " -D%s" % define

        print "%s -U __GNUC__ -dD %s %s" % (self.cpp, self.flags, filename)

        f = os.popen("%s -U __GNUC__ -dD %s %s" % (self.cpp, self.flags, filename))

        code = []
        for line in f:
            m = DEFINE.match(line)
            if m:
                self.matches.append((filename,) + m.groups())

            m = DIRECTIVE.match(line)
            if m:
                filename = m.group(2)

            if line.startswith("#") and not line.startswith("# "):
                # Replace any directives other than line numbers
                # with placeholder empty lines
                code.append("\n")
            else:
                code.append(line)

        self.lexer.input("".join(code))
        self.output = []
        while True:
            token = self.lexer.token()
            if token is not None:
                self.output.append(token)
            else:
                break

    def emit(self, file, regex, all_names, all_headers, headers,
             imported_symbols):
        """Emit all of the preprocessor symbols which match the
           specified REGEX, along with their dependencies"""

        defines = {}
        vars = {}
        replacements = []
        value = None

        # Anything defined in ctypes or imported_symbols is fair game
        for container in ctypes.__dict__, imported_symbols, all_names:
            for key in container:
                defines[key] = None
                vars[key] = None

        # NULL in C is just 'None' in Python
        defines['None'] = None
        defines['NULL'] = PreprocessorDefine('NULL', 'None', defines)

        QC_CONDITIONAL = re.compile(r" \? (.+?) : ")

        for (filename, name, params, code) in self.matches:

            # If not specified, define to "1"
            if code is None:
                code = "1"

            # Convert the value into a function if necessary
            if params:
                code = "lambda %s: (%s)" % (params, code)

            # Try to execute the code
            try:
                value = eval(code, vars)
            except:
                # Looks like the code doesn't execute. Let's fix any string
                # variables which pop up adjacent to strings, and make sure
                # that they're added together correctly.
                if not params:
                    def repl(m):
                        (a,b,c) = m.groups()
                        if (a and a in vars and isinstance(vars[a], str)):
                            return r"%s + " % a
                        elif (b and b in vars and isinstance(vars[b], str)):
                            if c is not None:
                                return r" + %s +" % b
                            else:
                                return r" + %s" % b
                        else:
                            return m.group()

                    code = ADJACENT_STRINGS.sub(repl, code)

                code = code.replace(" || ", " or ")
                code = code.replace(" && ", " and ")
                code = QC_CONDITIONAL.sub(r" and \1 or ", code)

                # Try to execute the modified code
                try:
                    value = eval(code, vars)
                except Exception, e:
                    # It still didn't work. Next!
                    continue

            # Save the calculated value
            vars[name] = value

            # Save this definition
            defines[name] = PreprocessorDefine(name, code, defines)

            # If this is a match, output it
            if (name not in imported_symbols and
                (not regex or regex.match(name)) and
                (all_headers or filename in headers)):
                defines[name].emit(file)

<|endoftext|>


<commit_before>"""
Tools for the instructor dashboard
"""
import dateutil
import json

from django.conf import settings
from django.contrib.auth.models import User
from django.http import HttpResponseBadRequest
from django.utils.timezone import utc
from django.utils.translation import ugettext as _

from courseware.models import StudentModule
from xmodule.fields import Date
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore

from bulk_email.models import CourseAuthorization

DATE_FIELD = Date()

class DashboardError(Exception):
    """
    Errors arising from use of the instructor dashboard.
    """
    def response(self):
        """
        Generate an instance of HttpResponseBadRequest for this error.
        """
        error = unicode(self)
        return HttpResponseBadRequest(json.dumps({'error': error}))

def handle_dashboard_error(view):
    """
    Decorator which adds seamless DashboardError handling to a view.  If a
    DashboardError is raised during view processing, an HttpResponseBadRequest
    is sent back to the client with JSON data about the error.
    """
    def wrapper(request, course_id):
        """
        Wrap the view.
        """
        try:
            return view(request, course_id=course_id)
        except DashboardError, error:
            return error.response()

    return wrapper

def bulk_email_is_enabled_for_course(course_id):
    """
    Staff can only send bulk email for a course if all the following conditions are true:
    1. Bulk email feature flag is on.
    2. It is a studio course.
    3. Bulk email is enabled for the course.
    """

    bulk_email_enabled_globally = (settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] == True)
    is_studio_course = (modulestore().get_modulestore_type(course_id) != ModuleStoreEnum.Type.xml)
    bulk_email_enabled_for_course = CourseAuthorization.instructor_email_enabled(course_id)

    if bulk_email_enabled_globally and is_studio_course and bulk_email_enabled_for_course:
        return True

    return False

def strip_if_string(value):
    if isinstance(value, basestring):
        return value.strip()
    return value

def get_student_from_identifier(unique_student_identifier):
    """
    Gets a student object using either an email address or username.

    Returns the student object associated with `unique_student_identifier`

    Raises User.DoesNotExist if no user object can be found.
    """
    unique_student_identifier = strip_if_string(unique_student_identifier)
    if "@" in unique_student_identifier:
        student = User.objects.get(email=unique_student_identifier)
    else:
        student = User.objects.get(username=unique_student_identifier)
    return student

def parse_datetime(datestr):
    """
    Convert user input date string into an instance of `datetime.datetime` in
    UTC.
    """
    try:
        return dateutil.parser.parse(datestr).replace(tzinfo=utc)
    except ValueError:
        raise DashboardError(_("Unable to parse date: ") + datestr)

def find_unit(course, url):
    """
    Finds the unit (block, module, whatever the terminology is) with the given
    url in the course tree and returns the unit.  Raises DashboardError if no
    unit is found.
    """
    def find(node, url):
        """
        Find node in course tree for url.
        """
        if node.location.to_deprecated_string() == url:
            return node
        for child in node.get_children():
            found = find(child, url)
            if found:
                return found
        return None

    unit = find(course, url)
    if unit is None:
        raise DashboardError(_("Couldn't find module for url: {0}").format(url))
    return unit

def get_units_with_due_date(course):
    """
    Returns all top level units which have due dates.  Does not return
    descendents of those nodes.
    """
    units = []

    def visit(node):
        """
        Visit a node.  Checks to see if node has a due date and appends to
        `units` if it does.  Otherwise recurses into children to search for
        nodes with due dates.
        """
        if getattr(node, 'due', None):
            units.append(node)
        else:
            for child in node.get_children():
                visit(child)
    visit(course)
    #units.sort(key=_title_or_url)
    return units

def title_or_url(node):
    """
    Returns the `display_name` attribute of the passed in node of the course
    tree, if it has one.  Otherwise returns the node's url.
    """
    title = getattr(node, 'display_name', None)
    if not title:
        title = node.location.to_deprecated_string()
    return title

def set_due_date_extension(course, unit, student, due_date):
    """
    Sets a due date extension. Raises DashboardError if the extended
    due date is invalid.
    """
    if due_date:
        # Check that the new due date is valid:
        original_due_date = getattr(unit, 'due', None)

        if due_date < original_due_date:
            raise DashboardError(_("An extended due date must be later than the original due date."))

    def set_due_date(node):
        """
        Recursively set the due date on a node and all of its children.
        """
        try:
            student_module = StudentModule.objects.get(
                student_id=student.id,
                course_id=course.id,
                module_state_key=node.location
            )
            state = json.loads(student_module.state)

        except StudentModule.DoesNotExist:
            # Normally, a StudentModule is created as a side effect of assigning
            # a value to a property in an XModule or XBlock which has a scope
            # of 'Scope.user_state'.  Here, we want to alter user state but
            # can't use the standard XModule/XBlock machinery to do so, because
            # it fails to take into account that the state being altered might
            # belong to a student other than the one currently logged in.  As a
            # result, in our work around, we need to detect whether the
            # StudentModule has been created for the given student on the given
            # unit and create it if it is missing, so we can use it to store
            # the extended due date.
            student_module = StudentModule.objects.create(
                student_id=student.id,
                course_id=course.id,
                module_state_key=node.location,
                module_type=node.category
            )
            state = {}

        state['extended_due'] = DATE_FIELD.to_json(due_date)
        student_module.state = json.dumps(state)
        student_module.save()

        for child in node.get_children():
            set_due_date(child)

    set_due_date(unit)

def dump_module_extensions(course, unit):
    """
    Dumps data about students with due date extensions for a particular module,
    specified by 'url', in a particular course.
    """
    data = []
    header = [_("Username"), _("Full Name"), _("Extended Due Date")]
    query = StudentModule.objects.filter(
        course_id=course.id,
        module_state_key=unit.location)
    for module in query:
        state = json.loads(module.state)
        extended_due = state.get("extended_due")
        if not extended_due:
            continue
        extended_due = DATE_FIELD.from_json(extended_due)
        extended_due = extended_due.strftime("%Y-%m-%d %H:%M")
        fullname = module.student.profile.name
        data.append(dict(zip(
            header,
            (module.student.username, fullname, extended_due))))
    data.sort(key=lambda x: x[header[0]])
    return {
        "header": header,
        "title": _("Users with due date extensions for {0}").format(
            title_or_url(unit)),
        "data": data
    }

def dump_student_extensions(course, student):
    """
    Dumps data about the due date extensions granted for a particular student
    in a particular course.
    """
    data = []
    header = [_("Unit"), _("Extended Due Date")]
    units = get_units_with_due_date(course)
    units = dict([(u.location, u) for u in units])
    query = StudentModule.objects.filter(
        course_id=course.id,
        student_id=student.id)
    for module in query:
        state = json.loads(module.state)
        # temporary hack: module_state_key is missing the run but units are not. fix module_state_key
        module_loc = module.module_state_key.map_into_course(module.course_id)
        if module_loc not in units:
            continue
        extended_due = state.get("extended_due")
        if not extended_due:
            continue
        extended_due = DATE_FIELD.from_json(extended_due)
        extended_due = extended_due.strftime("%Y-%m-%d %H:%M")
        title = title_or_url(units[module_loc])
        data.append(dict(zip(header, (title, extended_due))))
    return {
        "header": header,
        "title": _("Due date extensions for {0} {1} ({2})").format(
            student.first_name, student.last_name, student.username),
        "data": data}
<commit_msg>Fix: API allowed an individual due date extension on units with no due date<commit_after>"""
Tools for the instructor dashboard
"""
import dateutil
import json

from django.conf import settings
from django.contrib.auth.models import User
from django.http import HttpResponseBadRequest
from django.utils.timezone import utc
from django.utils.translation import ugettext as _

from courseware.models import StudentModule
from xmodule.fields import Date
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import modulestore

from bulk_email.models import CourseAuthorization

DATE_FIELD = Date()

class DashboardError(Exception):
    """
    Errors arising from use of the instructor dashboard.
    """
    def response(self):
        """
        Generate an instance of HttpResponseBadRequest for this error.
        """
        error = unicode(self)
        return HttpResponseBadRequest(json.dumps({'error': error}))

def handle_dashboard_error(view):
    """
    Decorator which adds seamless DashboardError handling to a view.  If a
    DashboardError is raised during view processing, an HttpResponseBadRequest
    is sent back to the client with JSON data about the error.
    """
    def wrapper(request, course_id):
        """
        Wrap the view.
        """
        try:
            return view(request, course_id=course_id)
        except DashboardError, error:
            return error.response()

    return wrapper

def bulk_email_is_enabled_for_course(course_id):
    """
    Staff can only send bulk email for a course if all the following conditions are true:
    1. Bulk email feature flag is on.
    2. It is a studio course.
    3. Bulk email is enabled for the course.
    """

    bulk_email_enabled_globally = (settings.FEATURES['ENABLE_INSTRUCTOR_EMAIL'] == True)
    is_studio_course = (modulestore().get_modulestore_type(course_id) != ModuleStoreEnum.Type.xml)
    bulk_email_enabled_for_course = CourseAuthorization.instructor_email_enabled(course_id)

    if bulk_email_enabled_globally and is_studio_course and bulk_email_enabled_for_course:
        return True

    return False

def strip_if_string(value):
    if isinstance(value, basestring):
        return value.strip()
    return value

def get_student_from_identifier(unique_student_identifier):
    """
    Gets a student object using either an email address or username.

    Returns the student object associated with `unique_student_identifier`

    Raises User.DoesNotExist if no user object can be found.
    """
    unique_student_identifier = strip_if_string(unique_student_identifier)
    if "@" in unique_student_identifier:
        student = User.objects.get(email=unique_student_identifier)
    else:
        student = User.objects.get(username=unique_student_identifier)
    return student

def parse_datetime(datestr):
    """
    Convert user input date string into an instance of `datetime.datetime` in
    UTC.
    """
    try:
        return dateutil.parser.parse(datestr).replace(tzinfo=utc)
    except ValueError:
        raise DashboardError(_("Unable to parse date: ") + datestr)

def find_unit(course, url):
    """
    Finds the unit (block, module, whatever the terminology is) with the given
    url in the course tree and returns the unit.  Raises DashboardError if no
    unit is found.
    """
    def find(node, url):
        """
        Find node in course tree for url.
        """
        if node.location.to_deprecated_string() == url:
            return node
        for child in node.get_children():
            found = find(child, url)
            if found:
                return found
        return None

    unit = find(course, url)
    if unit is None:
        raise DashboardError(_("Couldn't find module for url: {0}").format(url))
    return unit

def get_units_with_due_date(course):
    """
    Returns all top level units which have due dates.  Does not return
    descendents of those nodes.
    """
    units = []

    def visit(node):
        """
        Visit a node.  Checks to see if node has a due date and appends to
        `units` if it does.  Otherwise recurses into children to search for
        nodes with due dates.
        """
        if getattr(node, 'due', None):
            units.append(node)
        else:
            for child in node.get_children():
                visit(child)
    visit(course)
    #units.sort(key=_title_or_url)
    return units

def title_or_url(node):
    """
    Returns the `display_name` attribute of the passed in node of the course
    tree, if it has one.  Otherwise returns the node's url.
    """
    title = getattr(node, 'display_name', None)
    if not title:
        title = node.location.to_deprecated_string()
    return title

def set_due_date_extension(course, unit, student, due_date):
    """
    Sets a due date extension. Raises DashboardError if the unit or extended
    due date is invalid.
    """
    if due_date:
        # Check that the new due date is valid:
        original_due_date = getattr(unit, 'due', None)

        if not original_due_date:
            raise DashboardError(_("Unit {0} has no due date to extend.").format(unit.location))
        if due_date < original_due_date:
            raise DashboardError(_("An extended due date must be later than the original due date."))

    def set_due_date(node):
        """
        Recursively set the due date on a node and all of its children.
        """
        try:
            student_module = StudentModule.objects.get(
                student_id=student.id,
                course_id=course.id,
                module_state_key=node.location
            )
            state = json.loads(student_module.state)

        except StudentModule.DoesNotExist:
            # Normally, a StudentModule is created as a side effect of assigning
            # a value to a property in an XModule or XBlock which has a scope
            # of 'Scope.user_state'.  Here, we want to alter user state but
            # can't use the standard XModule/XBlock machinery to do so, because
            # it fails to take into account that the state being altered might
            # belong to a student other than the one currently logged in.  As a
            # result, in our work around, we need to detect whether the
            # StudentModule has been created for the given student on the given
            # unit and create it if it is missing, so we can use it to store
            # the extended due date.
            student_module = StudentModule.objects.create(
                student_id=student.id,
                course_id=course.id,
                module_state_key=node.location,
                module_type=node.category
            )
            state = {}

        state['extended_due'] = DATE_FIELD.to_json(due_date)
        student_module.state = json.dumps(state)
        student_module.save()

        for child in node.get_children():
            set_due_date(child)

    set_due_date(unit)

def dump_module_extensions(course, unit):
    """
    Dumps data about students with due date extensions for a particular module,
    specified by 'url', in a particular course.
    """
    data = []
    header = [_("Username"), _("Full Name"), _("Extended Due Date")]
    query = StudentModule.objects.filter(
        course_id=course.id,
        module_state_key=unit.location)
    for module in query:
        state = json.loads(module.state)
        extended_due = state.get("extended_due")
        if not extended_due:
            continue
        extended_due = DATE_FIELD.from_json(extended_due)
        extended_due = extended_due.strftime("%Y-%m-%d %H:%M")
        fullname = module.student.profile.name
        data.append(dict(zip(
            header,
            (module.student.username, fullname, extended_due))))
    data.sort(key=lambda x: x[header[0]])
    return {
        "header": header,
        "title": _("Users with due date extensions for {0}").format(
            title_or_url(unit)),
        "data": data
    }

def dump_student_extensions(course, student):
    """
    Dumps data about the due date extensions granted for a particular student
    in a particular course.
    """
    data = []
    header = [_("Unit"), _("Extended Due Date")]
    units = get_units_with_due_date(course)
    units = dict([(u.location, u) for u in units])
    query = StudentModule.objects.filter(
        course_id=course.id,
        student_id=student.id)
    for module in query:
        state = json.loads(module.state)
        # temporary hack: module_state_key is missing the run but units are not. fix module_state_key
        module_loc = module.module_state_key.map_into_course(module.course_id)
        if module_loc not in units:
            continue
        extended_due = state.get("extended_due")
        if not extended_due:
            continue
        extended_due = DATE_FIELD.from_json(extended_due)
        extended_due = extended_due.strftime("%Y-%m-%d %H:%M")
        title = title_or_url(units[module_loc])
        data.append(dict(zip(header, (title, extended_due))))
    return {
        "header": header,
        "title": _("Due date extensions for {0} {1} ({2})").format(
            student.first_name, student.last_name, student.username),
        "data": data}
<|endoftext|>


<commit_before>#!/usr/bin/python

import ConfigParser
import time
import feedparser
import os
import pywapi
import string

from core.voicesynthetizer import VoiceSynthetizer

class Weather(object):

    def __init__(self, voicesynthetizer):

        print '[Cancun] Weather'

        self.conf = ConfigParser.ConfigParser()
        self.path = "configuration/general.configuration"
        self.conf.read(self.path)

        self.agent = self.conf.get("weather", "agent")

        self.speaker = voicesynthetizer

    def yahoo(self):

        yahoocityid = self.conf.get("weather", "yahoocityid")
        self.url = "http://weather.yahooapis.com/forecastrss?w=" + yahoocityid  + "&u=c"

        data = feedparser.parse(self.url)

	# {'city': u'Guadalajara', 'region': u'JA', 'country': u'Mexico'}
	# {'direction': u'0', 'speed': u'0', 'chill': u'22'}
	# {'pressure': u'1008.2', 'rising': u'1', 'visibility': u'12.87', 'humidity': u'73'}
	# {'sunset': u'8:32 pm', 'sunrise': u'7:12 am'}

        self.location = data.feed.yweather_location
        self.wind = data.feed.yweather_wind
        self.atmosphere = data.feed.yweather_atmosphere
        self.astronomy = data.feed.yweather_astronomy

        sunrise=self.astronomy['sunrise']
        sunset=self.astronomy['sunset']

        summary=data.entries[0].summary
        temp=summary.split("High: ")
        temp=temp[1].split(" Low: ")
        self.high=temp[0]
        self.low=temp[1].split("
")[0]

        self.speaker.speechit("Reporte del clima en la ciudad de " + self.location['city'] + ", " + self.location['country'])
        self.speaker.speechit("Temperatura maxima, " + self.high + " grados centigrados")
        self.speaker.speechit("Temperatura minima, " + self.low + " grados centigrados")
        self.speaker.speechit("Presion Atmosferica, " + self.atmosphere['pressure'] + " milibares")
        self.speaker.speechit("Visibilidad, " + self.atmosphere['visibility'] + " kilometros")
        self.speaker.speechit("Humedad, " + self.atmosphere['humidity'] + " por ciento")
        self.speaker.speechit("El Sol se oculta a las " + self.astronomy['sunset'].replace(":", " "))

    def noaa(self):

        #pp = pprint.PrettyPrinter(indent=4)
        noaa_result = pywapi.get_weather_from_noaa('MMGL')
        #pp.pprint(noaa_result)
        noaa_text = "Los resultados NOAA son: " + string.lower(noaa_result['weather']) + " y " + noaa_result['temp_c'] + "C ahora en Guadalajara.\n"
	self.speaker.speechit(noaa_text)

    def report(self):

        self.noaa()
        return
        if self.agent == "yahoo":
                self.yahoo()

        return

if __name__ == '__main__':

    test = Weather("temp")
    test.report()
<commit_msg>Modules: Weather Texst Fix<commit_after>#!/usr/bin/python

import ConfigParser
import time
import feedparser
import os
import pywapi
import string

from core.voicesynthetizer import VoiceSynthetizer

class Weather(object):

    def __init__(self, voicesynthetizer):

        print '[Cancun] Weather'

        self.conf = ConfigParser.ConfigParser()
        self.path = "configuration/general.configuration"
        self.conf.read(self.path)

        self.agent = self.conf.get("weather", "agent")

        self.speaker = voicesynthetizer

    def yahoo(self):

        yahoocityid = self.conf.get("weather", "yahoocityid")
        self.url = "http://weather.yahooapis.com/forecastrss?w=" + yahoocityid  + "&u=c"

        data = feedparser.parse(self.url)

	# {'city': u'Guadalajara', 'region': u'JA', 'country': u'Mexico'}
	# {'direction': u'0', 'speed': u'0', 'chill': u'22'}
	# {'pressure': u'1008.2', 'rising': u'1', 'visibility': u'12.87', 'humidity': u'73'}
	# {'sunset': u'8:32 pm', 'sunrise': u'7:12 am'}

        self.location = data.feed.yweather_location
        self.wind = data.feed.yweather_wind
        self.atmosphere = data.feed.yweather_atmosphere
        self.astronomy = data.feed.yweather_astronomy

        sunrise=self.astronomy['sunrise']
        sunset=self.astronomy['sunset']

        summary=data.entries[0].summary
        temp=summary.split("High: ")
        temp=temp[1].split(" Low: ")
        self.high=temp[0]
        self.low=temp[1].split("
")[0]

        self.speaker.speechit("Reporte del clima en la ciudad de " + self.location['city'] + ", " + self.location['country'])
        self.speaker.speechit("Temperatura maxima, " + self.high + " grados centigrados")
        self.speaker.speechit("Temperatura minima, " + self.low + " grados centigrados")
        self.speaker.speechit("Presion Atmosferica, " + self.atmosphere['pressure'] + " milibares")
        self.speaker.speechit("Visibilidad, " + self.atmosphere['visibility'] + " kilometros")
        self.speaker.speechit("Humedad, " + self.atmosphere['humidity'] + " por ciento")
        self.speaker.speechit("El Sol se oculta a las " + self.astronomy['sunset'].replace(":", " "))

    def noaa(self):

        #pp = pprint.PrettyPrinter(indent=4)
        noaa_result = pywapi.get_weather_from_noaa('MMGL')
        #pp.pprint(noaa_result)
        noaa_text = "Los resultados NOAA son: " + string.lower(noaa_result['weather']) + " y " + noaa_result['temp_c'] + "C ahora en Guadalajara"
        print noaa_text
	self.speaker.speechit(noaa_text)

    def report(self):

        if self.agent == "yahoo":
                self.yahoo()
        elif self.agent == "noaa":
                self.noaa()

        return

if __name__ == '__main__':

    test = Weather("temp")
    test.report()
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
youtube.py - Jenni YouTube Module
Copyright 2012, Dimitri Molenaars, Tyrope.nl.
Licensed under the Eiffel Forum License 2.

More info:
 * Jenni: https://github.com/myano/jenni/
 * Phenny: http://inamidst.com/phenny/

This module will respond to .yt and .youtube commands and searches the youtubes.
"""

import urllib2, re

def ytsearch(jenni, input):
    """YouTube search module"""
    #modified from ytinfo: Copyright 2010-2011, Michael Yanovich, yanovich.net, Kenneth Sham.

    #Right now, this uses a parsing script from rscript.org. Eventually, I'd
    #like to use the YouTube API directly.

    #Before actually loading this in, let's see what input actually is so we can parse it right.

    #Grab info from rscript
    uri = 'http://rscript.org/lookup.php?type=ytinfo&term=' + input.group(2).encode('utf-8')
    uri = uri.replace(' ', '%20')
    redirects = 0
    while True:
        req = urllib2.Request(uri, headers={'Accept':'text/html'})
        req.add_header('User-Agent', 'OpenAnything/1.0 +http://diveintopython.org/')
    try: u = urllib2.urlopen(req, None, 0.5)
    except:
        jenni.say('Something went wrong when accessing the rscript.org parser.')
        return
        info = u.info()
        u.close()
        # info = web.head(uri)
        if not isinstance(info, list):
            status = '200'
        else:
            status = str(info[1])
            info = info[0]
        if status.startswith('3'):
            uri = urlparse.urljoin(uri, info['Location'])
        else: break
        redirects += 1
        if redirects >= 50:
            return "Too many re-directs."
    try: mtype = info['content-type']
    except:
        return
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return
    u = urllib2.urlopen(req)
    bytes = u.read(262144)
    u.close()

    #Parse rscript info.
    rtitle = re.search('(TITLE: )(.*)', bytes)
    title = rtitle.group(2)

    rauthor = re.search('(AUTHOR: )(\S*) (20\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d).*', bytes)
    author = rauthor.group(2)
    if not author:
        jenni.say('I couldn\'t find information for that video.')
        return

    duration = int(re.search('(DURATION: )(.*)', bytes).group(2))
    if duration < 1: length = 'LIVE'
    else:
        hours = duration / (60 * 60)
        minutes = duration / 60 - (hours * 60)
        seconds = duration % 60

        length = ''
        if hours:
            length = str(hours) + 'hours'
            if minutes or seconds:
                length = length + ' '
        if minutes:
            length = length + str(minutes) + 'mins'
            if seconds:
                length = length + ' '
        if seconds: length = length + str(seconds) + 'secs'

    views = str('{:20,d}'.format(int(re.search('(VIEWS: )(.*)', bytes).group(2)))).lstrip(' ')

    rurl = re.search('(URL: )(.*)', bytes)
    url = rurl.group(2)

    message = '[YT Search] Title: ' +title+ \
              ' | Author: ' +author+ \
              ' | Duration: ' +length+ \
              ' | Views: ' +views+ \
              ' | Link: ' +url

    jenni.say(message)
ytsearch.commands = ['yt','youtube']
ytsearch.example = '.yt how to be a nerdfighter FAQ'

def ytinfo(jenni, input):
    #Grab info from YT API
    uri = 'http://gdata.youtube.com/feeds/api/videos/' + input.group(2) + '?v=2'
    redirects = 0
    while True:
        req = urllib2.Request(uri, headers={'Accept':'*/*', 'User-Agent':'OpenAnything/1.0 +http://diveintopython.org/'})
        try: u = urllib2.urlopen(req, None, 0.5)
        except:
            jenni.say('Something went wrong when accessing the YouTube API.')
            return
        info = u.info()
        u.close()
        # info = web.head(uri)
        if not isinstance(info, list):
            status = '200'
            info = info[0]
        else:
            status = str(info[1])
        try: info = info[0]
        except e: jenni.msg(input.devchan,"[DEVMSG]Line 120: info= "+str(info)+" exception: "+str(e))
        jenni.msg(input.devchan,"[DEVMSG]YT API Result: ["+status+"]"+info)
        if status.startswith('3'):
            uri = urlparse.urljoin(uri, info['Location'])
        else: break
        redirects += 1
        if redirects >= 50:
            return "Too many re-directs."
    try: mtype = info['content-type']
    except: return
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return
    try: u = urllib2.urlopen(req, None, 0.5)
    except:
        jenni.say('Something went wrong when accessing the YouTube API.')
        return
    bytes = u.read(262144)
    u.close()

    #Parse YouTube API info (XML)

    #get title
    title_result = re.search('(?:<media:title type=\'plain\'>)(.*)(?:</media:title>)', bytes)
    title = title_result.group(1)

    #get youtube channel
    uploader_result = re.search('(?:<author><name>)(.*)(?:</name>)', bytes)
    uploader = uploader_result.group(1)

    #get upload time in format: yyyy-MM-ddThh:mm:ss.sssZ
    uploaded_result = re.search('(?:<yt:uploaded>)(.*)(?:</yt:uploaded>)', bytes)
    upraw = uploaded_result.group(1)
    #parse from current format to output format: DD/MM/yyyy, hh:mm
    uploaded = upraw[8:10]+"/"+upraw[5:7]+"/"+upraw[0:4]+", "+upraw[11:13]+":"+upraw[14:16]

    #get duration in seconds
    length_result = re.search('(?:<yt:duration seconds=\')(.*)(?:\'/>)', bytes)
    duration = length_result.group(1)

    #Detect liveshow + parse duration into proper time format.
    if duration < 1: length = 'LIVE'
    else:
        hours = duration / (60 * 60)
        minutes = duration / 60 - (hours * 60)
        seconds = duration % 60

        length = ''
        if hours:
            length = str(hours) + 'hours'
            if minutes or seconds:
                length = length + ' '
        if minutes:
            length = length + str(minutes) + 'mins'
            if seconds:
                length = length + ' '
        if seconds: length = length + str(seconds) + 'secs'

    #get views
    views_result = re.search('(?:<yt:statistics favoriteCount=\')([0-9]*)(?:\' viewCount=\')([0-9]*)(?:\'/>)', bytes)
    views = views_result.group(2)

    #get favourites (for future use?)
    favs = views_result.group(1)

    #get comment count
    comments_result = re.search('(?:<gd:comments><gd:feedLink)(?:.*)(?:countHint=\')(.*)(?:\'/></gd:comments>)', bytes)
    comments = comments_result.group(1)

    #get likes & dislikes
    liking_result = re.search('(?:<yt:rating numDislikes=\')(.*)(?:\' numLikes=\')(.*)(?:\'/>)',bytes)
    likes = liking_result.group(2)
    dislikes = liking_result.group(1)

    #combine variables and print
    message = '[YouTube] Title: ' + title + ' | Uploader: ' + uploader + \
              ' | Uploaded: ' + uploaded + ' | Length: ' + length + \
              ' | Views: ' + views + ' | Comments: ' + comments + ' | Likes: '\
              + likes + ' | Dislikes: ' + dislikes

    jenni.say(message)
ytinfo.rule = '.*(youtube.com/watch\S*v=|youtu.be/)([\w-]+).*'

if __name__ == '__main__':
    print __doc__.strip()
<commit_msg>even MORE indenterrors.<commit_after>#!/usr/bin/env python
"""
youtube.py - Jenni YouTube Module
Copyright 2012, Dimitri Molenaars, Tyrope.nl.
Licensed under the Eiffel Forum License 2.

More info:
 * Jenni: https://github.com/myano/jenni/
 * Phenny: http://inamidst.com/phenny/

This module will respond to .yt and .youtube commands and searches the youtubes.
"""

import urllib2, re

def ytsearch(jenni, input):
    """YouTube search module"""
    #modified from ytinfo: Copyright 2010-2011, Michael Yanovich, yanovich.net, Kenneth Sham.

    #Right now, this uses a parsing script from rscript.org. Eventually, I'd
    #like to use the YouTube API directly.

    #Before actually loading this in, let's see what input actually is so we can parse it right.

    #Grab info from rscript
    uri = 'http://rscript.org/lookup.php?type=ytinfo&term=' + input.group(2).encode('utf-8')
    uri = uri.replace(' ', '%20')
    redirects = 0
    while True:
        req = urllib2.Request(uri, headers={'Accept':'text/html'})
        req.add_header('User-Agent', 'OpenAnything/1.0 +http://diveintopython.org/')
        try: u = urllib2.urlopen(req, None, 0.5)
        except:
            jenni.say('Something went wrong when accessing the rscript.org parser.')
            return
            info = u.info()
            u.close()
            # info = web.head(uri)
            if not isinstance(info, list):
                status = '200'
            else:
                status = str(info[1])
                info = info[0]
            if status.startswith('3'):
                uri = urlparse.urljoin(uri, info['Location'])
            else: break
            redirects += 1
            if redirects >= 50:
                return "Too many re-directs."
    try: mtype = info['content-type']
    except:
        return
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return
    u = urllib2.urlopen(req)
    bytes = u.read(262144)
    u.close()

    #Parse rscript info.
    rtitle = re.search('(TITLE: )(.*)', bytes)
    title = rtitle.group(2)

    rauthor = re.search('(AUTHOR: )(\S*) (20\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d).*', bytes)
    author = rauthor.group(2)
    if not author:
        jenni.say('I couldn\'t find information for that video.')
        return

    duration = int(re.search('(DURATION: )(.*)', bytes).group(2))
    if duration < 1: length = 'LIVE'
    else:
        hours = duration / (60 * 60)
        minutes = duration / 60 - (hours * 60)
        seconds = duration % 60

        length = ''
        if hours:
            length = str(hours) + 'hours'
            if minutes or seconds:
                length = length + ' '
        if minutes:
            length = length + str(minutes) + 'mins'
            if seconds:
                length = length + ' '
        if seconds: length = length + str(seconds) + 'secs'

    views = str('{:20,d}'.format(int(re.search('(VIEWS: )(.*)', bytes).group(2)))).lstrip(' ')

    rurl = re.search('(URL: )(.*)', bytes)
    url = rurl.group(2)

    message = '[YT Search] Title: ' +title+ \
              ' | Author: ' +author+ \
              ' | Duration: ' +length+ \
              ' | Views: ' +views+ \
              ' | Link: ' +url

    jenni.say(message)
ytsearch.commands = ['yt','youtube']
ytsearch.example = '.yt how to be a nerdfighter FAQ'

def ytinfo(jenni, input):
    #Grab info from YT API
    uri = 'http://gdata.youtube.com/feeds/api/videos/' + input.group(2) + '?v=2'
    redirects = 0
    while True:
        req = urllib2.Request(uri, headers={'Accept':'*/*', 'User-Agent':'OpenAnything/1.0 +http://diveintopython.org/'})
        try: u = urllib2.urlopen(req, None, 0.5)
        except:
            jenni.say('Something went wrong when accessing the YouTube API.')
            return
        info = u.info()
        u.close()
        # info = web.head(uri)
        if not isinstance(info, list):
            status = '200'
            info = info[0]
        else:
            status = str(info[1])
        try: info = info[0]
        except e: jenni.msg(input.devchan,"[DEVMSG]Line 120: info= "+str(info)+" exception: "+str(e))
        jenni.msg(input.devchan,"[DEVMSG]YT API Result: ["+status+"]"+info)
        if status.startswith('3'):
            uri = urlparse.urljoin(uri, info['Location'])
        else: break
        redirects += 1
        if redirects >= 50:
            return "Too many re-directs."
    try: mtype = info['content-type']
    except: return
    if not (('/html' in mtype) or ('/xhtml' in mtype)):
        return
    try: u = urllib2.urlopen(req, None, 0.5)
    except:
        jenni.say('Something went wrong when accessing the YouTube API.')
        return
    bytes = u.read(262144)
    u.close()

    #Parse YouTube API info (XML)

    #get title
    title_result = re.search('(?:<media:title type=\'plain\'>)(.*)(?:</media:title>)', bytes)
    title = title_result.group(1)

    #get youtube channel
    uploader_result = re.search('(?:<author><name>)(.*)(?:</name>)', bytes)
    uploader = uploader_result.group(1)

    #get upload time in format: yyyy-MM-ddThh:mm:ss.sssZ
    uploaded_result = re.search('(?:<yt:uploaded>)(.*)(?:</yt:uploaded>)', bytes)
    upraw = uploaded_result.group(1)
    #parse from current format to output format: DD/MM/yyyy, hh:mm
    uploaded = upraw[8:10]+"/"+upraw[5:7]+"/"+upraw[0:4]+", "+upraw[11:13]+":"+upraw[14:16]

    #get duration in seconds
    length_result = re.search('(?:<yt:duration seconds=\')(.*)(?:\'/>)', bytes)
    duration = length_result.group(1)

    #Detect liveshow + parse duration into proper time format.
    if duration < 1: length = 'LIVE'
    else:
        hours = duration / (60 * 60)
        minutes = duration / 60 - (hours * 60)
        seconds = duration % 60

        length = ''
        if hours:
            length = str(hours) + 'hours'
            if minutes or seconds:
                length = length + ' '
        if minutes:
            length = length + str(minutes) + 'mins'
            if seconds:
                length = length + ' '
        if seconds: length = length + str(seconds) + 'secs'

    #get views
    views_result = re.search('(?:<yt:statistics favoriteCount=\')([0-9]*)(?:\' viewCount=\')([0-9]*)(?:\'/>)', bytes)
    views = views_result.group(2)

    #get favourites (for future use?)
    favs = views_result.group(1)

    #get comment count
    comments_result = re.search('(?:<gd:comments><gd:feedLink)(?:.*)(?:countHint=\')(.*)(?:\'/></gd:comments>)', bytes)
    comments = comments_result.group(1)

    #get likes & dislikes
    liking_result = re.search('(?:<yt:rating numDislikes=\')(.*)(?:\' numLikes=\')(.*)(?:\'/>)',bytes)
    likes = liking_result.group(2)
    dislikes = liking_result.group(1)

    #combine variables and print
    message = '[YouTube] Title: ' + title + ' | Uploader: ' + uploader + \
              ' | Uploaded: ' + uploaded + ' | Length: ' + length + \
              ' | Views: ' + views + ' | Comments: ' + comments + ' | Likes: '\
              + likes + ' | Dislikes: ' + dislikes

    jenni.say(message)
ytinfo.rule = '.*(youtube.com/watch\S*v=|youtu.be/)([\w-]+).*'

if __name__ == '__main__':
    print __doc__.strip()
<|endoftext|>


<commit_before>"""The base Controller API

Provides the BaseController class for subclassing.
"""

import logging
import pkg_resources

from inspect import isclass
from tw.api import Widget, CSSLink, JSLink
from tg import TGController, tmpl_context, request, url
from tg.render import render
from pylons.i18n import _, ungettext, N_

import moksha.model as model

from moksha.api.widgets.stomp import stomp_widget
from moksha.lib.helpers import eval_and_check_predicates

log = logging.getLogger(__name__)

class GlobalResourceInjectionWidget(Widget):
    """
    Injects all global resources, such as JavaScript and CSS, on every page.
    This widget will pull in all JSLink and CSLink widgets that are listed
    on the `[moksha.global]` entry-point.
    """
    javascript = []
    css = []

    def __init__(self):
        super(GlobalResourceInjectionWidget, self).__init__()
        for widget_entry in pkg_resources.iter_entry_points('moksha.global'):
            log.info('Loading global resource: %s' % widget_entry.name)
            loaded = widget_entry.load()
            if isclass(loaded):
                loaded = loaded(widget_entry.name)
            if isinstance(loaded, JSLink):
                self.javascript.append(loaded)
            elif isinstance(loaded, CSSLink):
                self.css.append(loaded)
            else:
                raise Exception("Unknown global resource: %s.  Should be "
                                "either a JSLink or CSSLink." %
                                widget_entry.name)

global_resources = GlobalResourceInjectionWidget()

class Controller(object):
    """Base class for a web application's controller.

    Currently, this provides positional parameters functionality
    via a standard default method.
    """

class BaseController(TGController):
    """Base class for the root of a web application.

    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.
    """

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # TGController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        tmpl_context.stomp = stomp_widget
        request.identity = request.environ.get('repoze.who.identity')
        tmpl_context.identity = request.identity

        # we alias this for easy use in templates
        tmpl_context.auth = eval_and_check_predicates

        # url is already taken
        tmpl_context.get_url = url

        # Inject our global resources
        global_resources.register_resources()

        return TGController.__call__(self, environ, start_response)
<commit_msg>Don't inject the global resource for /appz requests<commit_after>"""The base Controller API

Provides the BaseController class for subclassing.
"""

import logging
import pkg_resources

from inspect import isclass
from tw.api import Widget, CSSLink, JSLink
from tg import TGController, tmpl_context, request, url
from tg.render import render
from pylons.i18n import _, ungettext, N_

import moksha.model as model

from moksha.api.widgets.stomp import stomp_widget
from moksha.lib.helpers import eval_and_check_predicates

log = logging.getLogger(__name__)

class GlobalResourceInjectionWidget(Widget):
    """
    Injects all global resources, such as JavaScript and CSS, on every page.
    This widget will pull in all JSLink and CSLink widgets that are listed
    on the `[moksha.global]` entry-point.
    """
    javascript = []
    css = []

    def __init__(self):
        super(GlobalResourceInjectionWidget, self).__init__()
        for widget_entry in pkg_resources.iter_entry_points('moksha.global'):
            log.info('Loading global resource: %s' % widget_entry.name)
            loaded = widget_entry.load()
            if isclass(loaded):
                loaded = loaded(widget_entry.name)
            if isinstance(loaded, JSLink):
                self.javascript.append(loaded)
            elif isinstance(loaded, CSSLink):
                self.css.append(loaded)
            else:
                raise Exception("Unknown global resource: %s.  Should be "
                                "either a JSLink or CSSLink." %
                                widget_entry.name)

global_resources = GlobalResourceInjectionWidget()

class Controller(object):
    """Base class for a web application's controller.

    Currently, this provides positional parameters functionality
    via a standard default method.
    """

class BaseController(TGController):
    """Base class for the root of a web application.

    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.
    """

    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # TGController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']
        tmpl_context.stomp = stomp_widget
        request.identity = request.environ.get('repoze.who.identity')
        tmpl_context.identity = request.identity

        # we alias this for easy use in templates
        tmpl_context.auth = eval_and_check_predicates

        # url is already taken
        tmpl_context.get_url = url

        # Inject our global resources
        if not request.path.startswith('/appz'):
            global_resources.register_resources()

        return TGController.__call__(self, environ, start_response)
<|endoftext|>


<commit_before>import datetime, logging
import flask_login
import mediacloud

from server import db, mc, login_manager

logger = logging.getLogger(__name__)

# User class
class User(flask_login.UserMixin):
    def __init__(self, username, key, active=True):
        self.name = username
        self.id = key
        self.active = active
        self.created = datetime.datetime.now()
        self.is_anonymous = False

    def is_active(self):
        return self.active

    def is_authenticated(self):
        return True

    def create_in_db_if_needed(self):
        if self.exists_in_db():
            logger.debug("user %s already in db" % self.name)
            return
        logger.debug("user %s created in db" % self.name)
        db.users.insert({'username':self.name})

    def exists_in_db(self):
        return db.users.find_one({'username':self.name}) is not None

    @classmethod
    def get(cls, userid):
        try:
            return User.cached[userid]
        except KeyError:
            return None

User.cached = {}

@login_manager.user_loader
def load_user(userid):
    '''
    flask-login uses this method to lookup users to see if they are logged in already
    '''
    logger.debug("trying to load_user %s" % userid)
    return User.get(userid)

def login_user(user):
    flask_login.login_user(user,remember=True)
    user.create_in_db_if_needed()
    logger.debug("  login succeeded")

def create_and_cache_user(username, key):
    user = User(username, key)
    User.cached[user.id] = user
    logger.debug("  added to user cache %s" % user.id)
    return user

def load_from_db_by_username(username):
    return db.users.find_one({'username':username})

def authenticate_by_key(username, key):
    logger.debug("user %s want to log in with key" % username)
    user_mc = mediacloud.MediaCloud(key)
    if user_mc.verifyAuthToken():
        logger.debug("succeeded")
        return create_and_cache_user(username, key)
    logger.debug("failed")
    return flask_login.AnonymousUserMixin()

def authenticate_by_password(username, password):
    logger.debug("user %s want to log in with password" % username)
    try:
        key = mc.userAuthToken(username, password)
        return create_and_cache_user(username, key)
    except Exception:
        return flask_login.AnonymousUserMixin()

<commit_msg>make is_anonymous a property like it should be<commit_after>import datetime, logging
import flask_login
import mediacloud

from server import db, mc, login_manager

logger = logging.getLogger(__name__)

# User class
class User(flask_login.UserMixin):
    def __init__(self, username, key, active=True):
        self.name = username
        self.id = key
        self.active = active
        self.created = datetime.datetime.now()

    def is_active(self):
        return self.active

    @property
    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True

    def create_in_db_if_needed(self):
        if self.exists_in_db():
            logger.debug("user %s already in db" % self.name)
            return
        logger.debug("user %s created in db" % self.name)
        db.users.insert({'username':self.name})

    def exists_in_db(self):
        return db.users.find_one({'username':self.name}) is not None

    @classmethod
    def get(cls, userid):
        try:
            return User.cached[userid]
        except KeyError:
            return None

User.cached = {}

@login_manager.user_loader
def load_user(userid):
    '''
    flask-login uses this method to lookup users to see if they are logged in already
    '''
    logger.debug("trying to load_user %s" % userid)
    return User.get(userid)

def login_user(user):
    flask_login.login_user(user,remember=True)
    user.create_in_db_if_needed()
    logger.debug("  login succeeded")

def create_and_cache_user(username, key):
    user = User(username, key)
    User.cached[user.id] = user
    logger.debug("  added to user cache %s" % user.id)
    return user

def load_from_db_by_username(username):
    return db.users.find_one({'username':username})

def authenticate_by_key(username, key):
    logger.debug("user %s want to log in with key" % username)
    user_mc = mediacloud.MediaCloud(key)
    if user_mc.verifyAuthToken():
        user = create_and_cache_user(username, key)
        logger.debug("  succeeded - got a key (user.is_anonymous=%s)" % user.is_anonymous)
        return user
    logger.debug("failed")
    return flask_login.AnonymousUserMixin()

def authenticate_by_password(username, password):
    logger.debug("user %s want to log in with password" % username)
    try:
        key = mc.userAuthToken(username, password)
        user = create_and_cache_user(username, key)
        logger.debug("  succeeded - got a key (user.is_anonymous=%s)" % user.is_anonymous)
        return user
    except Exception:
        logging.exception("authenticate_by_password failed for %s" % username)
        return flask_login.AnonymousUserMixin()

<|endoftext|>


<commit_before>from __future__ import unicode_literals, division

from django.db import models
from django.utils import timezone
from django.utils.timezone import utc
from django.contrib import auth
from datetime import datetime, timedelta, time
import pytz

class Goal(models.Model):
    STATUS = (
        ('active', 'Active'),
        ('archived', 'Archived'),
        ('abandoned', 'Abandoned'),
    )

    name = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, null=True, blank=True)
    status = models.CharField(max_length=255, choices=STATUS)
    priority = models.PositiveSmallIntegerField(null=False, default=0)
    owner = models.ForeignKey(auth.models.User, related_name='goals', null=False)

    # Examples:
    # target_amount=15, type=minutes, period=day
    # target_amount=1, type=times, period=day
    # target_amount=10, type=times, period=year
    # target_amount=50000, type=words
    target_amount = models.PositiveSmallIntegerField(null=True, default=None, blank=True)
    type = models.CharField(max_length=255) # times/minutes/hours/words/pages/etc.
    period = models.CharField(max_length=255, blank=True, null=True) # day/week/month/year, optional

    # deadline

    def __unicode__(self):
        return self.name

    def in_progress(self):
        if len(self.entries.all()) > 0:
            last_entry = list(self.entries.all())[-1]
            if not last_entry.stop_time:
                return True
            else:
                return False
        else:
            return False

    def convert_to_resolution(self, duration):
        if self.type == "minutes":
            return (duration / 3600.0) * 60.0
        elif self.type == "hours":
            return duration / 3600.0
        else:
            return duration

    def get_current_elapsed_time(self):
        """ Returns current elapsed time in seconds. """

        if len(self.entries.all()) > 0:
            now = timezone.now()
            last_entry = list(self.entries.all())[-1]

            # If we're stopped, there's no current elapsed time
            if last_entry.stop_time:
                return 0

            return (now - last_entry.time).total_seconds()

        return 0

    def get_current_elapsed_time_converted(self):
        return self.convert_to_resolution(self.get_current_elapsed_time())

    def get_current_amount(self):
        # TODO: expand to week/month/year

        if self.period == "day":
            # Get all entries for this goal today and sum up the amounts

            now = timezone.localtime(timezone.now(), timezone.get_current_timezone())
            today = now.date()
            tomorrow = today + timedelta(1)
            today_start = datetime.combine(today, time())
            today_end = datetime.combine(tomorrow, time())

            # Convert back to UTC
            tz = timezone.get_current_timezone()
            d_tz = tz.normalize(tz.localize(today_start))
            today_start_utc = d_tz.astimezone(utc)
            d_tz = tz.normalize(tz.localize(today_end))
            today_end_utc = d_tz.astimezone(utc)

            entries = self.entries.filter(time__lte=today_end_utc, time__gte=today_start_utc)

            total_time = 0
            for entry in entries:
                if entry.amount:
                    total_time += entry.amount
                else:
                    # No stop time yet, so use now, converting to the resolution
                    total_time += self.get_current_elapsed_time()

            return total_time

        return 0

    def get_current_amount_converted(self):
        return self.convert_to_resolution(self.get_current_amount())

    def get_current_percentage(self):
        return min((self.get_current_amount_converted() / self.target_amount) * 100.0, 100.0)

    def get_amount_for_day(self, day):
        # Get all entries for this goal on this day and sum up the amounts

        tomorrow = day + timedelta(1)
        today_start = datetime.combine(day, time())
        today_end = datetime.combine(tomorrow, time())

        # Convert back to UTC
        tz = timezone.get_current_timezone()
        d_tz = tz.normalize(tz.localize(today_start))
        today_start_utc = d_tz.astimezone(utc)
        d_tz = tz.normalize(tz.localize(today_end))
        today_end_utc = d_tz.astimezone(utc)

        entries = self.entries.filter(time__lte=today_end_utc, time__gte=today_start_utc)

        total_time = 0
        for entry in entries:
            if entry.amount:
                total_time += entry.amount
            else:
                # No stop time yet, so use now, converting to the resolution
                #total_time += self.get_current_elapsed_time()
                pass

        return total_time

    def get_amount_for_day_converted(self, day):
        return self.convert_to_resolution(self.get_amount_for_day(day))

    def get_percentage_for_day(self, day):
        return min((self.get_amount_for_day_converted(day) / self.target_amount) * 100.0, 100.0)

    def get_days(self):
        # Returns list of days that have entries
        day_list = []

        for x in self.entries.all().order_by('-time'):
            date = x.time
            local_date = timezone.localtime(date, timezone.get_current_timezone()).date()
            if local_date not in day_list:
                day_list.append(local_date)

        return day_list

    def get_entries_by_day(self):
        # Returns list of days with entries for each
        days = self.get_days()

        day_list = []

        for day in days:
            next_day = day + timedelta(1)
            this_day_start = datetime.combine(day, time())
            this_day_end = datetime.combine(next_day, time())

            entries = self.entries.filter(time__lte=this_day_end, time__gte=this_day_start)

            day_list.append({
                'date': day,
                'entries': entries,
                'amount': self.get_amount_for_day_converted(day),
                'percentage': self.get_percentage_for_day(day),
            })

        return day_list

class Entry(models.Model):
    goal = models.ForeignKey(Goal, related_name='entries')
    amount = models.PositiveSmallIntegerField(null=True, default=None, blank=True)
    time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, default=None, blank=True)

    def save(self, *args, **kwargs):
        if not self.time:
            self.time = timezone.now()

        return super(Entry, self).save(*args, **kwargs)

    def __unicode__(self):
        return "Entry"

    def convert_to_resolution(self, duration):
        if self.goal.type == "minutes":
            return (duration / 3600.0) * 60.0
        elif self.goal.type == "hours":
            return duration / 3600.0
        else:
            return duration

    def get_elapsed_time(self):
        # If there's already a stop time then we're good
        if self.stop_time:
            return self.amount

        now = timezone.now()
        return (now - self.time).total_seconds()

    def get_elapsed_time_converted(self):
        return self.convert_to_resolution(self.get_elapsed_time())

    class Meta:
        verbose_name_plural = "entries"
<commit_msg>Updated amount on save entry<commit_after>from __future__ import unicode_literals, division

from django.db import models
from django.utils import timezone
from django.utils.timezone import utc
from django.contrib import auth
from datetime import datetime, timedelta, time
import pytz

class Goal(models.Model):
    STATUS = (
        ('active', 'Active'),
        ('archived', 'Archived'),
        ('abandoned', 'Abandoned'),
    )

    name = models.CharField(max_length=255)
    slug = models.CharField(max_length=255, null=True, blank=True)
    status = models.CharField(max_length=255, choices=STATUS)
    priority = models.PositiveSmallIntegerField(null=False, default=0)
    owner = models.ForeignKey(auth.models.User, related_name='goals', null=False)

    # Examples:
    # target_amount=15, type=minutes, period=day
    # target_amount=1, type=times, period=day
    # target_amount=10, type=times, period=year
    # target_amount=50000, type=words
    target_amount = models.PositiveSmallIntegerField(null=True, default=None, blank=True)
    type = models.CharField(max_length=255) # times/minutes/hours/words/pages/etc.
    period = models.CharField(max_length=255, blank=True, null=True) # day/week/month/year, optional

    # deadline

    def __unicode__(self):
        return self.name

    def in_progress(self):
        if len(self.entries.all()) > 0:
            last_entry = list(self.entries.all())[-1]
            if not last_entry.stop_time:
                return True
            else:
                return False
        else:
            return False

    def convert_to_resolution(self, duration):
        if self.type == "minutes":
            return (duration / 3600.0) * 60.0
        elif self.type == "hours":
            return duration / 3600.0
        else:
            return duration

    def get_current_elapsed_time(self):
        """ Returns current elapsed time in seconds. """

        if len(self.entries.all()) > 0:
            now = timezone.now()
            last_entry = list(self.entries.all())[-1]

            # If we're stopped, there's no current elapsed time
            if last_entry.stop_time:
                return 0

            return (now - last_entry.time).total_seconds()

        return 0

    def get_current_elapsed_time_converted(self):
        return self.convert_to_resolution(self.get_current_elapsed_time())

    def get_current_amount(self):
        # TODO: expand to week/month/year

        if self.period == "day":
            # Get all entries for this goal today and sum up the amounts

            now = timezone.localtime(timezone.now(), timezone.get_current_timezone())
            today = now.date()
            tomorrow = today + timedelta(1)
            today_start = datetime.combine(today, time())
            today_end = datetime.combine(tomorrow, time())

            # Convert back to UTC
            tz = timezone.get_current_timezone()
            d_tz = tz.normalize(tz.localize(today_start))
            today_start_utc = d_tz.astimezone(utc)
            d_tz = tz.normalize(tz.localize(today_end))
            today_end_utc = d_tz.astimezone(utc)

            entries = self.entries.filter(time__lte=today_end_utc, time__gte=today_start_utc)

            total_time = 0
            for entry in entries:
                if entry.amount:
                    total_time += entry.amount
                else:
                    # No stop time yet, so use now, converting to the resolution
                    total_time += self.get_current_elapsed_time()

            return total_time

        return 0

    def get_current_amount_converted(self):
        return self.convert_to_resolution(self.get_current_amount())

    def get_current_percentage(self):
        return min((self.get_current_amount_converted() / self.target_amount) * 100.0, 100.0)

    def get_amount_for_day(self, day):
        # Get all entries for this goal on this day and sum up the amounts

        tomorrow = day + timedelta(1)
        today_start = datetime.combine(day, time())
        today_end = datetime.combine(tomorrow, time())

        # Convert back to UTC
        tz = timezone.get_current_timezone()
        d_tz = tz.normalize(tz.localize(today_start))
        today_start_utc = d_tz.astimezone(utc)
        d_tz = tz.normalize(tz.localize(today_end))
        today_end_utc = d_tz.astimezone(utc)

        entries = self.entries.filter(time__lte=today_end_utc, time__gte=today_start_utc)

        total_time = 0
        for entry in entries:
            if entry.amount:
                total_time += entry.amount
            else:
                # No stop time yet, so use now, converting to the resolution
                #total_time += self.get_current_elapsed_time()
                pass

        return total_time

    def get_amount_for_day_converted(self, day):
        return self.convert_to_resolution(self.get_amount_for_day(day))

    def get_percentage_for_day(self, day):
        return min((self.get_amount_for_day_converted(day) / self.target_amount) * 100.0, 100.0)

    def get_days(self):
        # Returns list of days that have entries
        day_list = []

        for x in self.entries.all().order_by('-time'):
            date = x.time
            local_date = timezone.localtime(date, timezone.get_current_timezone()).date()
            if local_date not in day_list:
                day_list.append(local_date)

        return day_list

    def get_entries_by_day(self):
        # Returns list of days with entries for each
        days = self.get_days()

        day_list = []

        for day in days:
            next_day = day + timedelta(1)
            this_day_start = datetime.combine(day, time())
            this_day_end = datetime.combine(next_day, time())

            entries = self.entries.filter(time__lte=this_day_end, time__gte=this_day_start)

            day_list.append({
                'date': day,
                'entries': entries,
                'amount': self.get_amount_for_day_converted(day),
                'percentage': self.get_percentage_for_day(day),
            })

        return day_list

class Entry(models.Model):
    goal = models.ForeignKey(Goal, related_name='entries')
    amount = models.PositiveSmallIntegerField(null=True, default=None, blank=True)
    time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, default=None, blank=True)

    def save(self, *args, **kwargs):
        if not self.time:
            self.time = timezone.now()

        if self.stop_time:
            self.amount = (self.stop_time - self.time).total_seconds()

        return super(Entry, self).save(*args, **kwargs)

    def __unicode__(self):
        return "Entry"

    def convert_to_resolution(self, duration):
        if self.goal.type == "minutes":
            return (duration / 3600.0) * 60.0
        elif self.goal.type == "hours":
            return duration / 3600.0
        else:
            return duration

    def get_elapsed_time(self):
        # If there's already a stop time then we're good
        if self.stop_time:
            return self.amount

        now = timezone.now()
        return (now - self.time).total_seconds()

    def get_elapsed_time_converted(self):
        return self.convert_to_resolution(self.get_elapsed_time())

    class Meta:
        verbose_name_plural = "entries"
<|endoftext|>


<commit_before>class ClusterExistsError(Exception):

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class Clusters(object):

    def __init__(self):
        self.config = cm_config()
        self.mongodb = cm_mongo()
        self.user = cm_user()
        pass

    def defaults(self, default_dict):
        """setes image, cloud, flavor, key, username, ..."""
        pass

    def list(self):
        """ returns the list of the clusters in json format. This includes the status"""
        return None

    def vms(self, name):
        """returns in json the information about the vms of the named cluster"""
        return None

    def ips(self, name):
        """returns a list of ips belonging to the named cluster"""
        return None

    def names(self, name):
        """returns a lsit of names of the vms belonging to the cluster"""
        return None

    def group(self, name):
        """returns the group belonging to the cluster"""
        return None

    def delete(self, name):
        """deletes the named cluster"""

    def info(self, name):
        """returns a simplified information about the cluster in json format"""
        """each vm contains the ips and the name, as well as the status of the vm"""
        return None

    def create(self, name, n):
        """creates a cluster with the given name and the number of vms.
        If the cluster exists an exception is thrown. Returns the json of the cluster."""

        # raise ClusterExistsError('Cluster ' + name + ' exists')

<commit_msg>cluster api<commit_after>class ClusterExistsError(Exception):

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class Clusters(object):

    def __init__(self):
        self.config = cm_config()
        self.mongodb = cm_mongo()
        self.user = self.config.username()
        pass

    def defaults(self, default_dict):
        """setes image, cloud, flavor, key, username, ..."""
        pass

    def list(self):
        """ returns the list of the clusters in json format. This includes the status"""
        return None

    def vms(self, name):
        """returns in json the information about the vms of the named cluster"""
        return None

    def ips(self, name):
        """returns a list of ips belonging to the named cluster"""
        return None

    def names(self, name):
        """returns a lsit of names of the vms belonging to the cluster"""
        return None

    def group(self, name):
        """returns the group belonging to the cluster"""
        return None

    def delete(self, name):
        """deletes the named cluster"""

    def info(self, name):
        """returns a simplified information about the cluster in json format"""
        """each vm contains the ips and the name, as well as the status of the vm"""
        return None

    def create(self, name, n):
        """creates a cluster with the given name and the number of vms.
        If the cluster exists an exception is thrown. Returns the json of the cluster."""

        # raise ClusterExistsError('Cluster ' + name + ' exists')

<|endoftext|>


<commit_before>__author__ = 'abdul'

###############################################################################
# Mongoctl Exception class
###############################################################################
class MongoctlException(Exception):
    def __init__(self, message,cause=None):
        self.message  = message
        self.cause = cause

    def __str__(self):
        return self.message<commit_msg>Remove ref to deprecated "message" property of BaseException<commit_after>__author__ = 'abdul'

###############################################################################
# Mongoctl Exception class
###############################################################################
class MongoctlException(Exception):
    def __init__(self, message, cause=None):
        super(MongoctlException, self).__init__(message)
        self._cause = cause<|endoftext|>