<commit_before>import json
import logging

from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from django.contrib.auth.models import User
from xmodule.modulestore.django import modulestore
from capa.xqueue_interface import qinterface
from mitxmako.shortcuts import render_to_string
from models import StudentModule, StudentModuleCache
from static_replace import replace_urls
from xmodule.exceptions import NotFoundError
from xmodule.x_module import ModuleSystem
from xmodule_modifiers import replace_static_urls, add_histogram, wrap_xmodule

from courseware.courses import has_staff_access_to_course

log = logging.getLogger("mitx.courseware")

def make_track_function(request):
    '''
    Make a tracking function that logs what happened.
    For use in ModuleSystem.
    '''
    import track.views

    def f(event_type, event):
        return track.views.server_track(request, event_type, event, page='x_module')
    return f

def toc_for_course(user, request, course, active_chapter, active_section):
    '''
    Create a table of contents from the module store

    Return format:
    [ {'display_name': name, 'url_name': url_name,
       'sections': SECTIONS, 'active': bool}, ... ]

    where SECTIONS is a list
    [ {'display_name': name, 'url_name': url_name,
       'format': format, 'due': due, 'active' : bool}, ...]

    active is set for the section and chapter corresponding to the passed
    parameters.  Everything else comes from the xml, or defaults to "".

    chapters with name 'hidden' are skipped.
    '''

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(user, course, depth=2)
    course = get_module(user, request, course.location, student_module_cache)

    chapters = list()
    for chapter in course.get_display_items():
        sections = list()
        for section in chapter.get_display_items():

            active = (chapter.display_name == active_chapter and
                      section.display_name == active_section)
            hide_from_toc = section.metadata.get('hide_from_toc', 'false').lower() == 'true'

            if not hide_from_toc:
                sections.append({'display_name': section.display_name,
                                 'url_name': section.url_name,
                                 'format': section.metadata.get('format', ''),
                                 'due': section.metadata.get('due', ''),
                                 'active': active})

        chapters.append({'display_name': chapter.display_name,
                         'url_name': chapter.url_name,
                         'sections': sections,
                         'active': chapter.display_name == active_chapter})
    return chapters

def get_section(course_module, chapter, section):
    """
    Returns the xmodule descriptor for the name course > chapter > section,
    or None if this doesn't specify a valid section

    course: Course url
    chapter: Chapter url_name
    section: Section url_name
    """

    if course_module is None:
        return

    chapter_module = None
    for _chapter in course_module.get_children():
        if _chapter.url_name == chapter:
            chapter_module = _chapter
            break

    if chapter_module is None:
        return

    section_module = None
    for _section in chapter_module.get_children():
        if _section.url_name == section:
            section_module = _section
            break

    return section_module

def get_module(user, request, location, student_module_cache, position=None):
    ''' Get an instance of the xmodule class identified by location,
    setting the state based on an existing StudentModule, or creating one if none
    exists.

    Arguments:
      - user                  : current django User
      - request               : current django HTTPrequest
      - location              : A Location-like object identifying the module to load
      - student_module_cache  : a StudentModuleCache
      - position              : extra information from URL for user-specified
                                position within module

    Returns: xmodule instance

    '''
    descriptor = modulestore().get_item(location)

    #TODO Only check the cache if this module can possibly have state
    if user.is_authenticated():
        instance_module = student_module_cache.lookup(descriptor.category,
                                              descriptor.location.url())

        shared_state_key = getattr(descriptor, 'shared_state_key', None)
        if shared_state_key is not None:
            shared_module = student_module_cache.lookup(descriptor.category,
                                                        shared_state_key)
        else:
            shared_module = None
    else:
        instance_module = None
        shared_module = None

    instance_state = instance_module.state if instance_module is not None else None
    shared_state = shared_module.state if shared_module is not None else None

    # TODO (vshnayder): fix hardcoded urls (use reverse)
    # Setup system context for module instance
    ajax_url = settings.MITX_ROOT_URL + '/modx/' + descriptor.location.url() + '/'

    # Fully qualified callback URL for external queueing system
    xqueue_callback_url = (request.build_absolute_uri('/') + settings.MITX_ROOT_URL +
                          'xqueue/' + str(user.id) + '/' + descriptor.location.url() + '/' +
                          'score_update')

    # Default queuename is course-specific and is derived from the course that
    #   contains the current module.
    # TODO: Queuename should be derived from 'course_settings.json' of each course
    xqueue_default_queuename = descriptor.location.org + '-' + descriptor.location.course

    xqueue = { 'interface': qinterface,
               'callback_url': xqueue_callback_url,
               'default_queuename': xqueue_default_queuename.replace(' ','_') }

    def _get_module(location):
        return get_module(user, request, location,
                                       student_module_cache, position)

    # TODO (cpennington): When modules are shared between courses, the static
    # prefix is going to have to be specific to the module, not the directory
    # that the xml was loaded from
    system = ModuleSystem(track_function=make_track_function(request),
                          render_template=render_to_string,
                          ajax_url=ajax_url,
                          xqueue=xqueue,
                          # TODO (cpennington): Figure out how to share info between systems
                          filestore=descriptor.system.resources_fs,
                          get_module=_get_module,
                          user=user,
                          # TODO (cpennington): This should be removed when all html from
                          # a module is coming through get_html and is therefore covered
                          # by the replace_static_urls code below
                          replace_urls=replace_urls,
                          is_staff=user.is_staff,
                          )
    # pass position specified in URL to module through ModuleSystem
    system.set('position', position)

    module = descriptor.xmodule_constructor(system)(instance_state, shared_state)

    module.get_html = replace_static_urls(
        wrap_xmodule(module.get_html, module, 'xmodule_display.html'),
        module.metadata['data_dir']
    )

    if settings.MITX_FEATURES.get('DISPLAY_HISTOGRAMS_TO_STAFF'):
        if has_staff_access_to_course(user, module.location.course):
            module.get_html = add_histogram(module.get_html, module)

    return module

def get_instance_module(user, module, student_module_cache):
    """
    Returns instance_module is a StudentModule specific to this module for this student,
        or None if this is an anonymous user
    """
    if user.is_authenticated():
        instance_module = student_module_cache.lookup(module.category,
                                              module.location.url())

        if not instance_module:
            instance_module = StudentModule(
                student=user,
                module_type=module.category,
                module_state_key=module.id,
                state=module.get_instance_state(),
                max_grade=module.max_score())
            instance_module.save()
            student_module_cache.append(instance_module)

        return instance_module
    else:
        return None

def get_shared_instance_module(user, module, student_module_cache):
    """
    Return shared_module is a StudentModule specific to all modules with the same
        'shared_state_key' attribute, or None if the module does not elect to
        share state
    """
    if user.is_authenticated():
        # To get the shared_state_key, we need to descriptor
        descriptor = modulestore().get_item(module.location)

        shared_state_key = getattr(module, 'shared_state_key', None)
        if shared_state_key is not None:
            shared_module = student_module_cache.lookup(module.category,
                                                        shared_state_key)
            if not shared_module:
                shared_module = StudentModule(
                    student=user,
                    module_type=descriptor.category,
                    module_state_key=shared_state_key,
                    state=module.get_shared_state())
                shared_module.save()
                student_module_cache.append(shared_module)
        else:
            shared_module = None

        return shared_module
    else:
        return None

@csrf_exempt
def xqueue_callback(request, userid, id, dispatch):
    '''
    Entry point for graded results from the queueing system.
    '''
    # Parse xqueue response
    get = request.POST.copy()
    try:
        header = json.loads(get['xqueue_header'])
    except Exception as err:
        msg = "Error in xqueue_callback %s: Invalid return format" % err
        raise Exception(msg)

    # Retrieve target StudentModule
    user = User.objects.get(id=userid)

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(user, modulestore().get_item(id))
    instance = get_module(request.user, request, id, student_module_cache)

    instance_module = get_instance_module(request.user, instance, student_module_cache)

    if instance_module is None:
        log.debug("Couldn't find module '%s' for user '%s'",
                  id, request.user)
        raise Http404

    oldgrade = instance_module.grade
    old_instance_state = instance_module.state

    # Transfer 'queuekey' from xqueue response header to 'get'. This is required to
    #   use the interface defined by 'handle_ajax'
    get.update({'queuekey': header['lms_key']})

    # We go through the "AJAX" path
    #   So far, the only dispatch from xqueue will be 'score_update'
    try:
        ajax_return = instance.handle_ajax(dispatch, get)  # Can ignore the "ajax" return in 'xqueue_callback'
    except:
        log.exception("error processing ajax call")
        raise

    # Save state back to database
    instance_module.state = instance.get_instance_state()
    if instance.get_score():
        instance_module.grade = instance.get_score()['score']
    if instance_module.grade != oldgrade or instance_module.state != old_instance_state:
        instance_module.save()

    return HttpResponse("")

def modx_dispatch(request, dispatch=None, id=None):
    ''' Generic view for extensions. This is where AJAX calls go.

    Arguments:

      - request -- the django request.
      - dispatch -- the command string to pass through to the module's handle_ajax call
           (e.g. 'problem_reset').  If this string contains '?', only pass
           through the part before the first '?'.
      - id -- the module id. Used to look up the XModule instance
    '''
    # ''' (fix emacs broken parsing)
    # Check for submitted files
    p = request.POST.copy()
    if request.FILES:
        for inputfile_id in request.FILES.keys():
            p[inputfile_id] = request.FILES[inputfile_id]

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(request.user, modulestore().get_item(id))
    instance = get_module(request.user, request, id, student_module_cache)

    instance_module = get_instance_module(request.user, instance, student_module_cache)
    shared_module = get_shared_instance_module(request.user, instance, student_module_cache)

    # Don't track state for anonymous users (who don't have student modules)
    if instance_module is not None:
        oldgrade = instance_module.grade
        old_instance_state = instance_module.state
        old_shared_state = shared_module.state if shared_module is not None else None

    # Let the module handle the AJAX
    try:
        ajax_return = instance.handle_ajax(dispatch, p)
    except NotFoundError:
        log.exception("Module indicating to user that request doesn't exist")
        raise Http404
    except:
        log.exception("error processing ajax call")
        raise

    # Save the state back to the database
    # Don't track state for anonymous users (who don't have student modules)
    if instance_module is not None:
        instance_module.state = instance.get_instance_state()
        if instance.get_score():
            instance_module.grade = instance.get_score()['score']
        if instance_module.grade != oldgrade or instance_module.state != old_instance_state:
            instance_module.save()

    if shared_module is not None:
        shared_module.state = instance.get_shared_state()
        if shared_module.state != old_shared_state:
            shared_module.save()

    # Return whatever the module wanted to return to the client/caller
    return HttpResponse(ajax_return)
<commit_msg>StudentModule lookups in queue callback use target user, rather than request.user<commit_after>import json
import logging

from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from django.contrib.auth.models import User
from xmodule.modulestore.django import modulestore
from capa.xqueue_interface import qinterface
from mitxmako.shortcuts import render_to_string
from models import StudentModule, StudentModuleCache
from static_replace import replace_urls
from xmodule.exceptions import NotFoundError
from xmodule.x_module import ModuleSystem
from xmodule_modifiers import replace_static_urls, add_histogram, wrap_xmodule

from courseware.courses import has_staff_access_to_course

log = logging.getLogger("mitx.courseware")

def make_track_function(request):
    '''
    Make a tracking function that logs what happened.
    For use in ModuleSystem.
    '''
    import track.views

    def f(event_type, event):
        return track.views.server_track(request, event_type, event, page='x_module')
    return f

def toc_for_course(user, request, course, active_chapter, active_section):
    '''
    Create a table of contents from the module store

    Return format:
    [ {'display_name': name, 'url_name': url_name,
       'sections': SECTIONS, 'active': bool}, ... ]

    where SECTIONS is a list
    [ {'display_name': name, 'url_name': url_name,
       'format': format, 'due': due, 'active' : bool}, ...]

    active is set for the section and chapter corresponding to the passed
    parameters.  Everything else comes from the xml, or defaults to "".

    chapters with name 'hidden' are skipped.
    '''

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(user, course, depth=2)
    course = get_module(user, request, course.location, student_module_cache)

    chapters = list()
    for chapter in course.get_display_items():
        sections = list()
        for section in chapter.get_display_items():

            active = (chapter.display_name == active_chapter and
                      section.display_name == active_section)
            hide_from_toc = section.metadata.get('hide_from_toc', 'false').lower() == 'true'

            if not hide_from_toc:
                sections.append({'display_name': section.display_name,
                                 'url_name': section.url_name,
                                 'format': section.metadata.get('format', ''),
                                 'due': section.metadata.get('due', ''),
                                 'active': active})

        chapters.append({'display_name': chapter.display_name,
                         'url_name': chapter.url_name,
                         'sections': sections,
                         'active': chapter.display_name == active_chapter})
    return chapters

def get_section(course_module, chapter, section):
    """
    Returns the xmodule descriptor for the name course > chapter > section,
    or None if this doesn't specify a valid section

    course: Course url
    chapter: Chapter url_name
    section: Section url_name
    """

    if course_module is None:
        return

    chapter_module = None
    for _chapter in course_module.get_children():
        if _chapter.url_name == chapter:
            chapter_module = _chapter
            break

    if chapter_module is None:
        return

    section_module = None
    for _section in chapter_module.get_children():
        if _section.url_name == section:
            section_module = _section
            break

    return section_module

def get_module(user, request, location, student_module_cache, position=None):
    ''' Get an instance of the xmodule class identified by location,
    setting the state based on an existing StudentModule, or creating one if none
    exists.

    Arguments:
      - user                  : current django User
      - request               : current django HTTPrequest
      - location              : A Location-like object identifying the module to load
      - student_module_cache  : a StudentModuleCache
      - position              : extra information from URL for user-specified
                                position within module

    Returns: xmodule instance

    '''
    descriptor = modulestore().get_item(location)

    #TODO Only check the cache if this module can possibly have state
    if user.is_authenticated():
        instance_module = student_module_cache.lookup(descriptor.category,
                                              descriptor.location.url())

        shared_state_key = getattr(descriptor, 'shared_state_key', None)
        if shared_state_key is not None:
            shared_module = student_module_cache.lookup(descriptor.category,
                                                        shared_state_key)
        else:
            shared_module = None
    else:
        instance_module = None
        shared_module = None

    instance_state = instance_module.state if instance_module is not None else None
    shared_state = shared_module.state if shared_module is not None else None

    # TODO (vshnayder): fix hardcoded urls (use reverse)
    # Setup system context for module instance
    ajax_url = settings.MITX_ROOT_URL + '/modx/' + descriptor.location.url() + '/'

    # Fully qualified callback URL for external queueing system
    xqueue_callback_url = (request.build_absolute_uri('/') + settings.MITX_ROOT_URL +
                          'xqueue/' + str(user.id) + '/' + descriptor.location.url() + '/' +
                          'score_update')

    # Default queuename is course-specific and is derived from the course that
    #   contains the current module.
    # TODO: Queuename should be derived from 'course_settings.json' of each course
    xqueue_default_queuename = descriptor.location.org + '-' + descriptor.location.course

    xqueue = { 'interface': qinterface,
               'callback_url': xqueue_callback_url,
               'default_queuename': xqueue_default_queuename.replace(' ','_') }

    def _get_module(location):
        return get_module(user, request, location,
                                       student_module_cache, position)

    # TODO (cpennington): When modules are shared between courses, the static
    # prefix is going to have to be specific to the module, not the directory
    # that the xml was loaded from
    system = ModuleSystem(track_function=make_track_function(request),
                          render_template=render_to_string,
                          ajax_url=ajax_url,
                          xqueue=xqueue,
                          # TODO (cpennington): Figure out how to share info between systems
                          filestore=descriptor.system.resources_fs,
                          get_module=_get_module,
                          user=user,
                          # TODO (cpennington): This should be removed when all html from
                          # a module is coming through get_html and is therefore covered
                          # by the replace_static_urls code below
                          replace_urls=replace_urls,
                          is_staff=user.is_staff,
                          )
    # pass position specified in URL to module through ModuleSystem
    system.set('position', position)

    module = descriptor.xmodule_constructor(system)(instance_state, shared_state)

    module.get_html = replace_static_urls(
        wrap_xmodule(module.get_html, module, 'xmodule_display.html'),
        module.metadata['data_dir']
    )

    if settings.MITX_FEATURES.get('DISPLAY_HISTOGRAMS_TO_STAFF'):
        if has_staff_access_to_course(user, module.location.course):
            module.get_html = add_histogram(module.get_html, module)

    return module

def get_instance_module(user, module, student_module_cache):
    """
    Returns instance_module is a StudentModule specific to this module for this student,
        or None if this is an anonymous user
    """
    if user.is_authenticated():
        instance_module = student_module_cache.lookup(module.category,
                                              module.location.url())

        if not instance_module:
            instance_module = StudentModule(
                student=user,
                module_type=module.category,
                module_state_key=module.id,
                state=module.get_instance_state(),
                max_grade=module.max_score())
            instance_module.save()
            student_module_cache.append(instance_module)

        return instance_module
    else:
        return None

def get_shared_instance_module(user, module, student_module_cache):
    """
    Return shared_module is a StudentModule specific to all modules with the same
        'shared_state_key' attribute, or None if the module does not elect to
        share state
    """
    if user.is_authenticated():
        # To get the shared_state_key, we need to descriptor
        descriptor = modulestore().get_item(module.location)

        shared_state_key = getattr(module, 'shared_state_key', None)
        if shared_state_key is not None:
            shared_module = student_module_cache.lookup(module.category,
                                                        shared_state_key)
            if not shared_module:
                shared_module = StudentModule(
                    student=user,
                    module_type=descriptor.category,
                    module_state_key=shared_state_key,
                    state=module.get_shared_state())
                shared_module.save()
                student_module_cache.append(shared_module)
        else:
            shared_module = None

        return shared_module
    else:
        return None

from datetime import datetime
@csrf_exempt
def xqueue_callback(request, userid, id, dispatch):
    '''
    Entry point for graded results from the queueing system.
    '''
    # Parse xqueue response
    get = request.POST.copy()
    try:
        header = json.loads(get['xqueue_header'])
    except Exception as err:
        msg = "Error in xqueue_callback %s: Invalid return format" % err
        raise Exception(msg)

    # Retrieve target StudentModule
    user = User.objects.get(id=userid)

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(user, modulestore().get_item(id))
    instance = get_module(user, request, id, student_module_cache)
    instance_module = get_instance_module(user, instance, student_module_cache)

    if instance_module is None:
        log.debug("Couldn't find module '%s' for user '%s'",
                  id, user)
        raise Http404

    oldgrade = instance_module.grade
    old_instance_state = instance_module.state

    # Transfer 'queuekey' from xqueue response header to 'get'. This is required to
    #   use the interface defined by 'handle_ajax'
    get.update({'queuekey': header['lms_key']})

    # We go through the "AJAX" path
    #   So far, the only dispatch from xqueue will be 'score_update'
    try:
        ajax_return = instance.handle_ajax(dispatch, get)  # Can ignore the "ajax" return in 'xqueue_callback'
    except:
        log.exception("error processing ajax call")
        raise

    # Save state back to database
    instance_module.state = instance.get_instance_state()
    if instance.get_score():
        instance_module.grade = instance.get_score()['score']
    if instance_module.grade != oldgrade or instance_module.state != old_instance_state:
        instance_module.save()

    return HttpResponse("")

def modx_dispatch(request, dispatch=None, id=None):
    ''' Generic view for extensions. This is where AJAX calls go.

    Arguments:

      - request -- the django request.
      - dispatch -- the command string to pass through to the module's handle_ajax call
           (e.g. 'problem_reset').  If this string contains '?', only pass
           through the part before the first '?'.
      - id -- the module id. Used to look up the XModule instance
    '''
    # ''' (fix emacs broken parsing)
    # Check for submitted files
    p = request.POST.copy()
    if request.FILES:
        for inputfile_id in request.FILES.keys():
            p[inputfile_id] = request.FILES[inputfile_id]

    student_module_cache = StudentModuleCache.cache_for_descriptor_descendents(request.user, modulestore().get_item(id))
    instance = get_module(request.user, request, id, student_module_cache)

    instance_module = get_instance_module(request.user, instance, student_module_cache)
    shared_module = get_shared_instance_module(request.user, instance, student_module_cache)

    # Don't track state for anonymous users (who don't have student modules)
    if instance_module is not None:
        oldgrade = instance_module.grade
        old_instance_state = instance_module.state
        old_shared_state = shared_module.state if shared_module is not None else None

    # Let the module handle the AJAX
    try:
        ajax_return = instance.handle_ajax(dispatch, p)
    except NotFoundError:
        log.exception("Module indicating to user that request doesn't exist")
        raise Http404
    except:
        log.exception("error processing ajax call")
        raise

    # Save the state back to the database
    # Don't track state for anonymous users (who don't have student modules)
    if instance_module is not None:
        instance_module.state = instance.get_instance_state()
        if instance.get_score():
            instance_module.grade = instance.get_score()['score']
        if instance_module.grade != oldgrade or instance_module.state != old_instance_state:
            instance_module.save()

    if shared_module is not None:
        shared_module.state = instance.get_shared_state()
        if shared_module.state != old_shared_state:
            shared_module.save()

    # Return whatever the module wanted to return to the client/caller
    return HttpResponse(ajax_return)
<|endoftext|>


<commit_before>from django.contrib import admin
from django.utils.translation import ugettext as _
from models import *

class LibertyProviderAdmin(admin.ModelAdmin):
    list_display = ('name', 'entity_id', 'protocol_conformance')
    fieldsets = (
            (None, { 'fields' : ('name', ) }),
            (_('Metadata files'),
                { 'fields': ('metadata', 'public_key', 'ssl_certificate', 'ca_cert_chain') }))

admin.site.register(LibertyProvider, LibertyProviderAdmin)
<commit_msg>SAML common: add customization to LibertyProvider admin view<commit_after>from django.contrib import admin
from django.utils.translation import ugettext as _
from models import *

class LibertyProviderAdmin(admin.ModelAdmin):
    list_display = ('name', 'entity_id', 'protocol_conformance')
    list_display_links = ('entity_id',)
    list_editable = ('name',)
    search_fields = ('name', 'entity_id')
    readonly_fields = ('entity_id','protocol_conformance')
    fieldsets = (
            (None, { 'fields' : ('name', 'entity_id') }),
            (_('Metadata files'),
                { 'fields': ('metadata', 'public_key', 'ssl_certificate', 'ca_cert_chain') }))

class LibertyAttributeMappingInline(admin.TabularInline):
    model = LibertyAttributeMapping

class LibertyAttributeMapAdmin(admin.ModelAdmin):
    inlines = [
            LibertyAttributeMappingInline,
    ]

admin.site.register(LibertyProvider, LibertyProviderAdmin)
admin.site.register(LibertyAttributeMap, LibertyAttributeMapAdmin)
<|endoftext|>


<commit_before>#!/bin/env python

import discord
import json
from discord.ext import commands
import tmdbsimple as tmdb

# TMDb key
with open('data/apikeys.json') as f:
    tmdb.API_KEY = json.load(f)['tmdb']

class MyTMDb:
    def __init__(self, bot):
        self.bot = bot

    def sync_get_tmdb(query, type_search):
        """ Non async tmdb library function """

        # Initialize the search
        search = tmdb.Search()

        # TMDb response & store
        if type_search == 'tv':
            response = search.tv(query=query)['results'][0]
        elif type_search == 'movie':
            response = search.movie(query=query)['results'][0]
        else:
            raise NameError('type_search must be "tv" or "movie".')

        return response

    @commands.command(name='show', aliases=['ss', 'tv'])
    async def get_show(self, ctx, *, query):
        """ Get TV show information """

        # Must run_in_executor for blocking libraries
        result = await self.bot.loop.run_in_executor(None, MyTMDb.sync_get_tmdb, query, 'tv')

        # If no result are found
        if not result:
            return await ctx.send("Sorry, couldn't find that one.")

        # Get qtbot rating
        rating = float(result['vote_average'])
        if (rating > 7.0):
            rec = 'This is a qtbot™ recommended show.'
        else:
            rec = 'This is not a qtbot™ recommmended show.'

        # For getting poster
        base_image_uri = 'https://image.tmdb.org/t/p/w185{}'

        # Create embed
        em = discord.Embed()
        em.title = f'{result["name"]} ({result["first_air_date"].split("-")[0]})'
        em.description = result['overview']
        em.set_thumbnail(url=base_image_uri.format(
            result['poster_path']))
        em.add_field(name='Rating', value=str(rating))
        em.set_footer(text=rec)

        await ctx.send(embed=em)

    @commands.command(name='movie', aliases=['mov'])
    async def get_movie(self, ctx, *, query):
        """ Get movie information """

        # Must run_in_executor for blocking libraries
        result = await self.bot.loop.run_in_executor(None, MyTMDb.sync_get_tmdb, query, 'movie')

        if not result:
            return await ctx.send("Sorry, couldn't find that one.")

        rating = float(result['vote_average'])
        if (rating < 7.0):
            rec = 'This is not a qtbot™ recommmended film.'
        else:
            rec = 'This is a qtbot™ recommended film.'

        # For getting poster
        base_image_uri = 'https://image.tmdb.org/t/p/w185{}'

        # Create embed
        em = discord.Embed()
        em.title = f'{result["title"]} ({result["release_date"].split("-")[0]})'
        em.description = result['overview']
        em.set_thumbnail(url=base_image_uri.format(
            result['poster_path']))
        em.add_field(name='Rating', value=str(rating))
        em.set_footer(text=rec)

        await ctx.send(embed=em)

def setup(bot):
    bot.add_cog(MyTMDb(bot))
<commit_msg>add some color to tmdb embeds<commit_after>#!/bin/env python

import discord
import json
from discord.ext import commands
import tmdbsimple as tmdb

# TMDb key
with open('data/apikeys.json') as f:
    tmdb.API_KEY = json.load(f)['tmdb']

class MyTMDb:
    def __init__(self, bot):
        self.bot = bot

    def sync_get_tmdb(query, type_search):
        """ Non async tmdb library function """

        # Initialize the search
        search = tmdb.Search()

        # TMDb response & store
        if type_search == 'tv':
            response = search.tv(query=query)['results'][0]
        elif type_search == 'movie':
            response = search.movie(query=query)['results'][0]
        else:
            raise NameError('type_search must be "tv" or "movie".')

        return response

    @commands.command(name='show', aliases=['ss', 'tv'])
    async def get_show(self, ctx, *, query):
        """ Get TV show information """

        # Must run_in_executor for blocking libraries
        result = await self.bot.loop.run_in_executor(None, MyTMDb.sync_get_tmdb, query, 'tv')

        # If no result are found
        if not result:
            return await ctx.send("Sorry, couldn't find that one.")

        # Get qtbot rating
        rating = float(result['vote_average'])
        if (rating > 7.0):
            rec = 'This is a qtbot™ recommended show.'
        else:
            rec = 'This is not a qtbot™ recommmended show.'

        # For getting poster
        base_image_uri = 'https://image.tmdb.org/t/p/w185{}'

        # Create embed
        em = discord.Embed(color=discord.Color.greyple())
        em.title = f'{result["name"]} ({result["first_air_date"].split("-")[0]})'
        em.description = result['overview']
        em.set_thumbnail(url=base_image_uri.format(
            result['poster_path']))
        em.add_field(name='Rating', value=str(rating))
        em.set_footer(text=rec)

        await ctx.send(embed=em)

    @commands.command(name='movie', aliases=['mov'])
    async def get_movie(self, ctx, *, query):
        """ Get movie information """

        # Must run_in_executor for blocking libraries
        result = await self.bot.loop.run_in_executor(None, MyTMDb.sync_get_tmdb, query, 'movie')

        if not result:
            return await ctx.send("Sorry, couldn't find that one.")

        rating = float(result['vote_average'])
        if (rating < 7.0):
            rec = 'This is not a qtbot™ recommmended film.'
        else:
            rec = 'This is a qtbot™ recommended film.'

        # For getting poster
        base_image_uri = 'https://image.tmdb.org/t/p/w185{}'

        # Create embed
        em = discord.Embed(color=discord.Color.greyple())
        em.title = f'{result["title"]} ({result["release_date"].split("-")[0]})'
        em.description = result['overview']
        em.set_thumbnail(url=base_image_uri.format(
            result['poster_path']))
        em.add_field(name='Rating', value=str(rating))
        em.set_footer(text=rec)

        await ctx.send(embed=em)

def setup(bot):
    bot.add_cog(MyTMDb(bot))
<|endoftext|>


<commit_before># Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import os
# import sys

import openstackdocstheme

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
# TODO(ajaeger): enable PDF building, for example add 'rst2pdf.pdfbuilder'
# extensions =

# Add any paths that contain templates here, relative to this directory.
# templates_path = ['_templates']

# The suffix of source filenames.
source_suffix = '.rst'

# The encoding of source files.
# source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'Deployment Guide for Kolla Service'
bug_tag = u'deploy-guide'
copyright = u'2016, OpenStack contributors'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.1'
# The full version, including alpha/beta/rc tags.
release = '0.1'

# A few variables have to be set for the log-a-bug feature.
#   giturl: The location of conf.py on Git. Must be set manually.
#   gitsha: The SHA checksum of the bug description. Automatically extracted
#           from git log.
#   bug_tag: Tag for categorizing the bug. Must be set manually.
# These variables are passed to the logabug code via html_context.
giturl = u'http://git.openstack.org/cgit/openstack/Kolla/tree/deploy-guide/source'  # noqa
git_cmd = "/usr/bin/git log | head -n1 | cut -f2 -d' '"
gitsha = os.popen(git_cmd).read().strip('\n')
html_context = {"gitsha": gitsha, "bug_tag": bug_tag,
                "giturl": giturl,
                "bug_project": "Kolla"}

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ["common_prerequisites.rst", "common_configure.rst"]

# The reST default role (used for this markup: `text`) to use for all
# documents.
# default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'openstackdocs'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
# html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
html_theme_path = [openstackdocstheme.get_html_theme_path()]

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
# html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
# html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
# html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
# html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = []

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
# html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# So that we can enable "log-a-bug" links from each output HTML page, this
# variable must be set to a format that includes year, month, day, hours and
# minutes.
html_last_updated_fmt = '%Y-%m-%d %H:%M'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}

# If false, no module index is generated.
# html_domain_indices = True

# If false, no index is generated.
html_use_index = False

# If true, the index is split into individual pages for each letter.
# html_split_index = False

# If true, links to the reST sources are added to the pages.
html_show_sourcelink = False

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'deploy-guide'

# If true, publish source files
html_copy_source = False

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    # 'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    ('index', 'DeployGuide.tex', u'Deploy Guide',
     u'OpenStack contributors', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
# latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
# latex_use_parts = False

# If true, show page references after internal links.
# latex_show_pagerefs = False

# If true, show URL addresses after external links.
# latex_show_urls = False

# Documents to append as an appendix to all manuals.
# latex_appendices = []

# If false, no module index is generated.
# latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'installguide', u'Deploy Guide',
     [u'OpenStack contributors'], 1)
]

# If true, show URL addresses after external links.
# man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    ('index', 'DeployGuide', u'Deploy Guide',
     u'OpenStack contributors', 'DeployGuide',
     'This guide shows OpenStack end users how to deploy '
     'an OpenStack cloud.', 'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
# texinfo_appendices = []

# If false, no module index is generated.
# texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
# texinfo_no_detailmenu = False

# -- Options for Internationalization output ------------------------------
locale_dirs = ['locale/']

# -- Options for PDF output --------------------------------------------------

pdf_documents = [
    ('index', u'DeployGuide', u'Deploy Guide',
     u'OpenStack contributors')
]
<commit_msg>Using https instead of http<commit_after># Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import os
# import sys

import openstackdocstheme

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
# TODO(ajaeger): enable PDF building, for example add 'rst2pdf.pdfbuilder'
# extensions =

# Add any paths that contain templates here, relative to this directory.
# templates_path = ['_templates']

# The suffix of source filenames.
source_suffix = '.rst'

# The encoding of source files.
# source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'Deployment Guide for Kolla Service'
bug_tag = u'deploy-guide'
copyright = u'2016, OpenStack contributors'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.1'
# The full version, including alpha/beta/rc tags.
release = '0.1'

# A few variables have to be set for the log-a-bug feature.
#   giturl: The location of conf.py on Git. Must be set manually.
#   gitsha: The SHA checksum of the bug description. Automatically extracted
#           from git log.
#   bug_tag: Tag for categorizing the bug. Must be set manually.
# These variables are passed to the logabug code via html_context.
giturl = u'https://git.openstack.org/cgit/openstack/Kolla/tree/deploy-guide/source'  # noqa
git_cmd = "/usr/bin/git log | head -n1 | cut -f2 -d' '"
gitsha = os.popen(git_cmd).read().strip('\n')
html_context = {"gitsha": gitsha, "bug_tag": bug_tag,
                "giturl": giturl,
                "bug_project": "Kolla"}

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ["common_prerequisites.rst", "common_configure.rst"]

# The reST default role (used for this markup: `text`) to use for all
# documents.
# default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'openstackdocs'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
# html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
html_theme_path = [openstackdocstheme.get_html_theme_path()]

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
# html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
# html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
# html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
# html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = []

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
# html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
# So that we can enable "log-a-bug" links from each output HTML page, this
# variable must be set to a format that includes year, month, day, hours and
# minutes.
html_last_updated_fmt = '%Y-%m-%d %H:%M'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
# html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}

# If false, no module index is generated.
# html_domain_indices = True

# If false, no index is generated.
html_use_index = False

# If true, the index is split into individual pages for each letter.
# html_split_index = False

# If true, links to the reST sources are added to the pages.
html_show_sourcelink = False

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'deploy-guide'

# If true, publish source files
html_copy_source = False

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    # 'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    ('index', 'DeployGuide.tex', u'Deploy Guide',
     u'OpenStack contributors', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
# latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
# latex_use_parts = False

# If true, show page references after internal links.
# latex_show_pagerefs = False

# If true, show URL addresses after external links.
# latex_show_urls = False

# Documents to append as an appendix to all manuals.
# latex_appendices = []

# If false, no module index is generated.
# latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'installguide', u'Deploy Guide',
     [u'OpenStack contributors'], 1)
]

# If true, show URL addresses after external links.
# man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    ('index', 'DeployGuide', u'Deploy Guide',
     u'OpenStack contributors', 'DeployGuide',
     'This guide shows OpenStack end users how to deploy '
     'an OpenStack cloud.', 'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
# texinfo_appendices = []

# If false, no module index is generated.
# texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
# texinfo_no_detailmenu = False

# -- Options for Internationalization output ------------------------------
locale_dirs = ['locale/']

# -- Options for PDF output --------------------------------------------------

pdf_documents = [
    ('index', u'DeployGuide', u'Deploy Guide',
     u'OpenStack contributors')
]
<|endoftext|>


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

"""
Convert PGF files to an image file using pdflatex, pdfcrop, (from
texlive) and convert (from ImageMagick).
"""

import os
import sys
import tempfile
import time
import subprocess

def __find_exec(executable):
    '''Try to find an executable in the system path.'''

    if os.path.isfile(executable):
        return executable
    else:
        paths = os.environ['PATH'].split(os.pathsep)
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return ''

def __check_for_exec(executable, msg):
    '''Exit on error if the specified executable cannot be
    found. Otherwise, return the path to the executable.'''

    path = __find_exec(executable)
    if path == '':
        print msg
        sys.exit()
    else:
        return path

def __run_cmd(cmd, msg, cwd=None, wait=30):
    '''Run a system command; display an error message if it returns a
    nonzero exit code or it stalls for more than the specified number
    of seconds.'''

    dev_null = open('/dev/null', 'w')
    p = subprocess.Popen(cmd, stdout=dev_null, stderr=dev_null, shell=True, cwd=cwd)
    tic = time.time()
    while p.returncode == None and time.time() < tic+wait:
        try:
            p.poll()
        except KeyboardInterrupt:
            print 'manually killing command %s' % cmd
            p.kill()
            sys.exit(1)
    if p.returncode == None:
        print 'killing stalled command %s ' % cmd
        p.kill()
    if p.returncode < 0:
        print msg
        sys.exit(1)

# Check for required executables:
PDFLATEX = __check_for_exec('pdflatex', 'cannot find pdflatex')
PDFCROP = __check_for_exec('pdfcrop', 'cannot find pdfcrop')
CONVERT = __check_for_exec('convert', 'cannot find convert')
RM = __check_for_exec('rm', 'cannot find rm')

# Used to redirect program output to /dev/null:
redirect_output = ' 1>/dev/null 2>&1'

# Defaults:
default_template = """\\documentclass[10pt]{article}
\\usepackage{amsmath,amssymb,amsbsy,amsfonts,amsthm}
\\usepackage{cmbright}
\\usepackage{tikz}
\\pagestyle{empty}
\\begin{document}
<>
\\end{document}
"""
default_density = 200

def pgf2img(input_filename, output_filename,
            template=default_template, density=default_density):
    """Convert a PGF/TikZ file to an image file.

    Parameters
    ----------
    input_filename : str
        Name of input PGF/TikZ file. The file must contain a
        tikzpicture environment.
    output_filename : str
        Name of output file. The image format is determined
        by the filename extension.
    template : str
        LaTeX template used to generate image.
    density : int
        Output image density (in DPI).

    """

    # Open the input file:
    try:
        input_file = open(input_filename, 'r')
    except IOError:
        print 'error opening input file %s' % input_filename
        sys.exit(1)
    else:
        input_data = ''.join(input_file.readlines())

    # Combine the template and input file:
    temp_data = template.replace('<>',input_data)

    # Write the output to a temporary LaTeX file:
    try:
        temp_dirname = tempfile.mkdtemp()+os.sep
    except IOError:
        print 'error creating temporary directory %s' % temp_dirname
        sys.exit(1)
    else:
        temp_latex_filename = temp_dirname + 'temp.tex'

    try:
        temp_latex_file = open(temp_latex_filename,'w')
    except IOError:
        print 'error opening temporary LaTeX file %s' % temp_latex_filename
        sys.exit(1)
    else:
        temp_latex_file.writelines(temp_data.splitlines(True))
        temp_latex_file.close()

    # Process the temporary file with pdflatex:
    __run_cmd(PDFLATEX + ' ' +
              temp_latex_filename,
              'error running pdflatex', temp_dirname)

    # Crop the file with pdfcrop:
    temp_latex_basename = os.path.splitext(temp_latex_filename)[0]
    temp_pdf_filename = temp_latex_basename + '.pdf'
    temp_pdf_cropped_filename = temp_latex_basename + '_cropped.pdf'
    __run_cmd(PDFCROP + ' ' +
              temp_pdf_filename + ' ' +
              temp_pdf_cropped_filename,
              'error running pdfcrop',temp_dirname)

    # If the specified output file format is pdf, there is no need to run
    # the generated file through convert:
    output_ext = os.path.splitext(output_filename)[1]
    if output_ext.lower() == '.pdf':
        os.rename(temp_pdf_cropped_filename, output_filename)
    else:
        __run_cmd(CONVERT + ' -density ' + str(density) + ' ' +
                  temp_pdf_cropped_filename + ' ' +
                  output_filename,
                  'error running convert')

    # Clean up the temporary work directory:
    __run_cmd(RM + ' -rf ' + temp_dirname,
              'error removing temporary directory %s' % temp_dirname)

<commit_msg>Update pgf2img.py to properly convert very wide diagrams.<commit_after>#!/usr/bin/env python

"""
Convert PGF files to an image file using pdflatex, pdfcrop, (from
texlive) and convert (from ImageMagick).
"""

import os
import sys
import tempfile
import time
import subprocess

def __find_exec(executable):
    '''Try to find an executable in the system path.'''

    if os.path.isfile(executable):
        return executable
    else:
        paths = os.environ['PATH'].split(os.pathsep)
        for p in paths:
            f = os.path.join(p, executable)
            if os.path.isfile(f):
                return f
        return ''

def __check_for_exec(executable, msg):
    '''Exit on error if the specified executable cannot be
    found. Otherwise, return the path to the executable.'''

    path = __find_exec(executable)
    if path == '':
        print msg
        sys.exit()
    else:
        return path

def __run_cmd(cmd, msg, cwd=None, wait=30):
    '''Run a system command; display an error message if it returns a
    nonzero exit code or it stalls for more than the specified number
    of seconds.'''

    dev_null = open('/dev/null', 'w')
    p = subprocess.Popen(cmd, stdout=dev_null, stderr=dev_null, shell=True, cwd=cwd)
    tic = time.time()
    while p.returncode == None and time.time() < tic+wait:
        try:
            p.poll()
        except KeyboardInterrupt:
            print 'manually killing command %s' % cmd
            p.kill()
            sys.exit(1)
    if p.returncode == None:
        print 'killing stalled command %s ' % cmd
        p.kill()
    if p.returncode < 0:
        print msg
        sys.exit(1)

# Check for required executables:
PDFLATEX = __check_for_exec('pdflatex', 'cannot find pdflatex')
PDFCROP = __check_for_exec('pdfcrop', 'cannot find pdfcrop')
CONVERT = __check_for_exec('convert', 'cannot find convert')
RM = __check_for_exec('rm', 'cannot find rm')

# Used to redirect program output to /dev/null:
redirect_output = ' 1>/dev/null 2>&1'

# Defaults:
default_template = """\\documentclass[10pt]{article}
\\usepackage{amsmath,amssymb,amsbsy,amsfonts,amsthm}
\\usepackage[landscape]{geometry}
\\usepackage{cmbright}
\\usepackage{tikz}
\\pagestyle{empty}
\\begin{document}
<>
\\end{document}
"""
default_density = 200

def pgf2img(input_filename, output_filename,
            template=default_template, density=default_density):
    """Convert a PGF/TikZ file to an image file.

    Parameters
    ----------
    input_filename : str
        Name of input PGF/TikZ file. The file must contain a
        tikzpicture environment.
    output_filename : str
        Name of output file. The image format is determined
        by the filename extension.
    template : str
        LaTeX template used to generate image.
    density : int
        Output image density (in DPI).

    """

    # Open the input file:
    try:
        input_file = open(input_filename, 'r')
    except IOError:
        print 'error opening input file %s' % input_filename
        sys.exit(1)
    else:
        input_data = ''.join(input_file.readlines())

    # Combine the template and input file:
    temp_data = template.replace('<>',input_data)

    # Write the output to a temporary LaTeX file:
    try:
        temp_dirname = tempfile.mkdtemp()+os.sep
    except IOError:
        print 'error creating temporary directory %s' % temp_dirname
        sys.exit(1)
    else:
        temp_latex_filename = temp_dirname + 'temp.tex'

    try:
        temp_latex_file = open(temp_latex_filename,'w')
    except IOError:
        print 'error opening temporary LaTeX file %s' % temp_latex_filename
        sys.exit(1)
    else:
        temp_latex_file.writelines(temp_data.splitlines(True))
        temp_latex_file.close()

    # Process the temporary file with pdflatex:
    __run_cmd(PDFLATEX + ' ' +
              temp_latex_filename,
              'error running pdflatex', temp_dirname)

    # Crop the file with pdfcrop:
    temp_latex_basename = os.path.splitext(temp_latex_filename)[0]
    temp_pdf_filename = temp_latex_basename + '.pdf'
    temp_pdf_cropped_filename = temp_latex_basename + '_cropped.pdf'
    __run_cmd(PDFCROP + ' ' +
              temp_pdf_filename + ' ' +
              temp_pdf_cropped_filename,
              'error running pdfcrop',temp_dirname)

    # If the specified output file format is pdf, there is no need to run
    # the generated file through convert:
    output_ext = os.path.splitext(output_filename)[1]
    if output_ext.lower() == '.pdf':
        os.rename(temp_pdf_cropped_filename, output_filename)
    else:
        __run_cmd(CONVERT + ' -density ' + str(density) + ' ' +
                  temp_pdf_cropped_filename + ' ' +
                  output_filename,
                  'error running convert')

    # Clean up the temporary work directory:
    __run_cmd(RM + ' -rf ' + temp_dirname,
              'error removing temporary directory %s' % temp_dirname)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Motech documentation build configuration file, created by
# sphinx-quickstart on Mon Mar 17 11:40:57 2014.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../../platform'))
sys.path.insert(0, os.path.abspath('../../modules'))
# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
    'sphinx.ext.pngmath',
    'sphinx.ext.viewcode',
]

#primary domain
primary_domain='java'

# Add any paths that contain templates here, relative to this directory.
templates_path = ['Motechtemplates']

# The suffix of source filenames.
source_suffix = '.txt'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'Motech'
copyright = u'2014, Motech'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0'
# The full version, including alpha/beta/rc tags.
release = '1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = ['Motechstatic']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'Motechdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
  ('index', 'Motech.tex', u'Motech Documentation',
   u'motech', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'motech', u'Motech Documentation',
     [u'Motechstatic'], 1)
]

# If true, show URL addresses after external links.
man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
  ('index', 'Motech', u'Motech Documentation',
   u'Motech', 'Motech', 'One line description of project.',
   'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'http://docs.python.org/': None}
<commit_msg>MOTECH-803 Check in Sphinx configuration and scripts to platform<commit_after># -*- coding: utf-8 -*-
#
# Motech documentation build configuration file, created by
# sphinx-quickstart on Mon Mar 17 11:40:57 2014.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../../platform'))
sys.path.insert(0, os.path.abspath('../../modules'))
# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
    'sphinx.ext.pngmath',
    'sphinx.ext.viewcode',
]

#primary domain
primary_domain='java'

# Add any paths that contain templates here, relative to this directory.
templates_path = ['Motechtemplates']

# The suffix of source filenames.
source_suffix = '.txt'

# The encoding of source files.
#source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'MOTECH'
copyright = u'2014, Grameen Foundation'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0'
# The full version, including alpha/beta/rc tags.
release = '1.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []

# The reST default role (used for this markup: `text`) to use for all
# documents.
#default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []

# If true, keep warnings as "system message" paragraphs in the built documents.
#keep_warnings = False

# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = 'default'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
#html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
#html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# html_static_path = ['Motechstatic']

# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#html_extra_path = []

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True

# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}

# If false, no module index is generated.
#html_domain_indices = True

# If false, no index is generated.
#html_use_index = True

# If true, the index is split into individual pages for each letter.
#html_split_index = False

# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = 'Motechdoc'

# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
  ('index', 'Motech.tex', u'MOTECH Documentation',
   u'Grameen Foundation', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None

# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False

# If true, show page references after internal links.
#latex_show_pagerefs = False

# If true, show URL addresses after external links.
#latex_show_urls = False

# Documents to append as an appendix to all manuals.
#latex_appendices = []

# If false, no module index is generated.
#latex_domain_indices = True

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    ('index', 'MOTECH', u'MOTECH Documentation',
     [u'Motechstatic'], 1)
]

# If true, show URL addresses after external links.
man_show_urls = False

# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
  ('index', 'MOTECH', u'MOTECH Documentation',
   u'Grameen Foundation', 'MOTECH', 'One line description of project.',
   'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []

# If false, no module index is generated.
#texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'

# If true, do not generate a @detailmenu in the "Top" node's menu.
#texinfo_no_detailmenu = False

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'http://docs.python.org/': None}
<|endoftext|>


<commit_before>#lol

def lol():
    return "sarah"<commit_msg>Delete sarahtest1.py<commit_after><|endoftext|>