<commit_before>#!/usr/bin/env python
import os, sys, json, distutils.core

# Get user settings.
config = json.load(open(os.path.join(os.path.dirname(__file__), 'configuration.json')))

# Define help text displayed for different flags.
text_space              = '--> '
text_help               = 'Help text'
text_list               = 'List text'
text_about              = 'About text'
text_version            = 'Cogen version: 1.0'
text_default            = 'Command not found. Type "cogen -h" to see help.'
text_invalid_path       = 'Template does not exist or "cogen.json" file is missing.'
text_replacing          = 'Starting to replace value: '
text_path_not_found     = 'Warning: path "%s" does not contain cogen.json file.'
text_cogen_not_found    = 'Warning: cogen.json file does not exist in "%s"'
text_general_not_found  = 'General variable "%s" not found.'
text_generated          = 'Template "%s" successfully generated.'

# Argument dictionaries.
args_out     = ['-o', '--o', '--output']
args_help    = ['-h', '--h', '--help']
args_list    = ['-l', '--l', '--list']
args_check   = ['-c', '--c', '--check']
args_version = ['-v', '--v', '--version']

# Main function for displaying the output. For now it just spaces out all text
# with dashes/arrow so it's easier to read and see on the screen.
def output(text):
  print text_space + text

# Replace content of the file.
def replace_file_contents(destination, variable, value):
  for path, dirs, files in os.walk(destination):
    for filename in files:
      file_name_temp, file_extension = os.path.splitext(filename)
      file_content = ''
      for line in open(path + '/' + filename, 'r+'):
        line_utf = line.encode('utf-8')
        file_content += line_utf.replace(variable['pattern'], value)
      file_opened = open(path + '/' + filename, 'w+')
      file_opened.write(file_content)
      file_opened.close()

# Rename files.
def replace_file_names(destination, variable, value):
  for path, dirs, files in os.walk(destination):
    for filename in files:
      if variable['pattern'] in filename:
        os.rename(path + '/' + filename, path + '/' + filename.replace(variable['pattern'], value))

# Remove the first argument, which is always "cogen".
argument = sys.argv[1]

# Global flags.
if argument[0] == '-':
  if argument in args_help:
    output(text_help)
  elif argument in args_version:
    output(text_version)
  elif argument in args_list:
    output(text_list)
  else:
    output(text_general_not_found)

# Template generation.
else:
  for path_to_template in config.get('paths_to_templates'):
    if not os.path.exists(path_to_template + '/' + argument):
      output(text_template_not_found % path_to_template)
    elif not os.path.isfile(path_to_template + '/cogen.json'):
      output(text_cogen_not_found % path_to_template)
    else:
      project_config = json.load(open(path_to_template + '/' + argument + '/cogen.json'))
      output_directory_name = raw_input('Enter folder name')
      src = path_to_template + '/' + argument
      destination = os.getcwd() + '/' + argument
      distutils.dir_util.copy_tree(src, destination)
      if project_config.get('variables'):
        for variable in project_config['variables']:
          value = raw_input(variable['name'] + ': ')
          if variable['type'] == 'all':
            replace_file_names(destination, variable, value)
            replace_file_contents(destination, variable, value)
          elif variable['type'] == 'replace':
            replace_file_contents(destination, variable, value)
          elif variable['type'] == 'rename':
            replace_file_names(destination, variable, value)
      output(text_generated % project_config['name'])
<commit_msg>adding files to skip functionality; fixes #11<commit_after>#!/usr/bin/env python
import os, sys, json, distutils.core

# Get user settings.
config = json.load(open(os.path.join(os.path.dirname(__file__), 'configuration.json')))

# Define help text displayed for different flags.
text_space              = '--> '
text_help               = 'Help text'
text_list               = 'List text'
text_about              = 'About text'
text_version            = 'Cogen version: 1.0'
text_default            = 'Command not found. Type "cogen -h" to see help.'
text_invalid_path       = 'Template does not exist or "cogen.json" file is missing.'
text_replacing          = 'Starting to replace value: '
text_path_not_found     = 'Warning: path "%s" does not contain cogen.json file.'
text_cogen_not_found    = 'Warning: cogen.json file does not exist in "%s"'
text_general_not_found  = 'General variable "%s" not found.'
text_generated          = 'Template "%s" successfully generated.'

# Argument dictionaries.
args_out     = ['-o', '--o', '--output']
args_help    = ['-h', '--h', '--help']
args_list    = ['-l', '--l', '--list']
args_check   = ['-c', '--c', '--check']
args_version = ['-v', '--v', '--version']

# Define filetypes which will be skipped from replacing. Note: this will not
# affect changing the filenames.
files_to_skip   = config.get('exclude_files_from_editing')

# Main function for displaying the output. For now it just spaces out all text
# with dashes/arrow so it's easier to read and see on the screen.
def output(text):
  print text_space + text

# Replace content of the file.
def replace_file_contents(destination, variable, value):
  for path, dirs, files in os.walk(destination):
    for filename in files:
      file_name_temp, file_extension = os.path.splitext(filename)
      # Note: file_extension will have the dot in front of the extension name,
      # so when comparing we have to use the string starting from the second
      # character.
      if file_extension[1:] not in files_to_skip:
        file_content = ''
        for line in open(path + '/' + filename, 'r+'):
          line_utf = line.encode('utf-8')
          file_content += line_utf.replace(variable['pattern'], value)
        file_opened = open(path + '/' + filename, 'w+')
        file_opened.write(file_content)
        file_opened.close()

# Rename files.
def replace_file_names(destination, variable, value):
  for path, dirs, files in os.walk(destination):
    for filename in files:
      if variable['pattern'] in filename:
        os.rename(path + '/' + filename, path + '/' + filename.replace(variable['pattern'], value))

# Remove the first argument, which is always "cogen".
argument = sys.argv[1]

# Global flags.
if argument[0] == '-':
  if argument in args_help:
    output(text_help)
  elif argument in args_version:
    output(text_version)
  elif argument in args_list:
    output(text_list)
  else:
    output(text_general_not_found)

# Template generation.
else:
  for path_to_template in config.get('paths_to_templates'):
    if not os.path.exists(path_to_template + '/' + argument):
      output(text_template_not_found % path_to_template)
    elif not os.path.isfile(path_to_template + '/cogen.json'):
      output(text_cogen_not_found % path_to_template)
    else:
      project_config = json.load(open(path_to_template + '/' + argument + '/cogen.json'))
      output_directory_name = raw_input('Enter folder name')
      src = path_to_template + '/' + argument
      destination = os.getcwd() + '/' + argument
      distutils.dir_util.copy_tree(src, destination)
      if project_config.get('variables'):
        for variable in project_config['variables']:
          value = raw_input(variable['name'] + ': ')
          if variable['type'] == 'all':
            replace_file_names(destination, variable, value)
            replace_file_contents(destination, variable, value)
          elif variable['type'] == 'replace':
            replace_file_contents(destination, variable, value)
          elif variable['type'] == 'rename':
            replace_file_names(destination, variable, value)
      output(text_generated % project_config['name'])
<|endoftext|>


<commit_before>import hashlib

from .. import functions, types
from ... import utils

class InlineBuilder:
    """
    Helper class to allow defining `InlineQuery
    <telethon.events.inlinequery.InlineQuery>` ``results``.

    Common arguments to all methods are
    explained here to avoid repetition:

        text (`str`, optional):
            If present, the user will send a text
            message with this text upon being clicked.

        link_preview (`bool`, optional):
            Whether to show a link preview in the sent
            text message or not.

        geo (:tl:`InputGeoPoint`, :tl:`GeoPoint`, :tl:`InputMediaVenue`, :tl:`MessageMediaVenue`, optional):
            If present, it may either be a geo point or a venue.

        period (int, optional):
            The period in seconds to be used for geo points.

        contact (:tl:`InputMediaContact`, :tl:`MessageMediaContact`, optional):
            If present, it must be the contact information to send.

        game (`bool`, optional):
            May be `True` to indicate that the game will be sent.

        buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`, :tl:`KeyboardButton`, optional):
            Same as ``buttons`` for `client.send_message()
            <telethon.client.messages.MessageMethods.send_message>`.

        parse_mode (`str`, optional):
            Same as ``parse_mode`` for `client.send_message()
            <telethon.client.messageparse.MessageParseMethods.parse_mode>`.

        id (`str`, optional):
            The string ID to use for this result. If not present, it
            will be the SHA256 hexadecimal digest of converting the
            created :tl:`InputBotInlineResult` with empty ID to ``bytes()``,
            so that the ID will be deterministic for the same input.

            .. note::

                If two inputs are exactly the same, their IDs will be the same
                too. If you send two articles with the same ID, it will raise
                ``ResultIdDuplicateError``. Consider giving them an explicit
                ID if you need to send two results that are the same.
    """
    def __init__(self, client):
        self._client = client

    # noinspection PyIncorrectDocstring
    async def article(
            self, title, description=None,
            *, url=None, thumb=None, content=None,
            id=None, text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates new inline result of article type.

        Args:
            title (`str`):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            url (`str`, optional):
                The URL to be shown for this result.

            thumb (:tl:`InputWebDocument`, optional):
                The thumbnail to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.

            content (:tl:`InputWebDocument`, optional):
                The content to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.
        """
        # TODO Does 'article' work always?
        # article, photo, gif, mpeg4_gif, video, audio,
        # voice, document, location, venue, contact, game
        result = types.InputBotInlineResult(
            id=id or '',
            type='article',
            send_message=await self._message(
                text=text, parse_mode=parse_mode, link_preview=link_preview,
                geo=geo, period=period,
                contact=contact,
                game=game,
                buttons=buttons
            ),
            title=title,
            description=description,
            url=url,
            thumb=thumb,
            content=content
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def photo(
            self, file, *, id=None, include_media=True,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates a new inline result of photo type.

        Args:
            include_media (`bool`, optional):
                Whether the photo file used to display the result should be
                included in the message itself or not. By default, the photo
                is included, and the text parameter alters the caption.

            file (`obj`, optional):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.
        """
        try:
            fh = utils.get_input_photo(file)
        except TypeError:
            _, media, _ = await self._client._file_to_media(
                file, allow_cache=True, as_image=True
            )
            if isinstance(media, types.InputPhoto):
                fh = media
            else:
                r = await self._client(functions.messages.UploadMediaRequest(
                    types.InputPeerSelf(), media=media
                ))
                fh = utils.get_input_photo(r.photo)

        result = types.InputBotInlineResultPhoto(
            id=id or '',
            type='photo',
            photo=fh,
            send_message=await self._message(
                text=text or '',
                parse_mode=parse_mode,
                link_preview=link_preview,
                media=include_media,
                geo=geo,
                period=period,
                contact=contact,
                game=game,
                buttons=buttons
            )
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def document(
            self, file, title=None, *, description=None, type=None,
            mime_type=None, attributes=None, force_document=False,
            voice_note=False, video_note=False, use_cache=True, id=None,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None,
            include_media=True
    ):
        """
        Creates a new inline result of document type.

        `use_cache`, `mime_type`, `attributes`, `force_document`,
        `voice_note` and `video_note` are described in `client.send_file
        <telethon.client.uploads.UploadMethods.send_file>`.

        Args:
            file (`obj`):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.

            title (`str`, optional):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            type (`str`, optional):
                The type of the document. May be one of: photo, gif,
                mpeg4_gif, video, audio, voice, document, sticker.

                See "Type of the result" in https://core.telegram.org/bots/api.

            include_media (`bool`, optional):
                Whether the document file used to display the result should be
                included in the message itself or not. By default, the document
                is included, and the text parameter alters the caption.
        """
        if type is None:
            if voice_note:
                type = 'voice'
            else:
                type = 'document'

        try:
            fh = utils.get_input_document(file)
        except TypeError:
            _, media, _ = await self._client._file_to_media(
                file,
                mime_type=mime_type,
                attributes=attributes,
                force_document=True,
                voice_note=voice_note,
                video_note=video_note,
                allow_cache=use_cache
            )
            if isinstance(media, types.InputDocument):
                fh = media
            else:
                r = await self._client(functions.messages.UploadMediaRequest(
                    types.InputPeerSelf(), media=media
                ))
                fh = utils.get_input_document(r.document)

        result = types.InputBotInlineResultDocument(
            id=id or '',
            type=type,
            document=fh,
            send_message=await self._message(
                # Empty string for text if there's media but text is None.
                # We may want to display a document but send text; however
                # default to sending the media (without text, i.e. stickers).
                text=text or '',
                parse_mode=parse_mode,
                link_preview=link_preview,
                media=include_media,
                geo=geo,
                period=period,
                contact=contact,
                game=game,
                buttons=buttons
            ),
            title=title,
            description=description
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def game(
            self, short_name, *, id=None,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates a new inline result of game type.

        Args:
            short_name (`str`):
                The short name of the game to use.
        """
        result = types.InputBotInlineResultGame(
            id=id or '',
            short_name=short_name,
            send_message=await self._message(
                text=text, parse_mode=parse_mode, link_preview=link_preview,
                geo=geo, period=period,
                contact=contact,
                game=game,
                buttons=buttons
            )
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    async def _message(
            self, *,
            text=None, parse_mode=(), link_preview=True, media=False,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        # Empty strings are valid but false-y; if they're empty use dummy '\0'
        args = ('\0' if text == '' else text, geo, contact, game)
        if sum(1 for x in args if x is not None and x is not False) != 1:
            raise ValueError(
                'Must set exactly one of text, geo, contact or game (set {})'
                .format(', '.join(x[0] for x in zip(
                    'text geo contact game'.split(), args) if x[1]) or 'none')
            )

        markup = self._client.build_reply_markup(buttons, inline_only=True)
        if text is not None:
            text, msg_entities = await self._client._parse_message_text(
                text, parse_mode
            )
            if media:
                # "MediaAuto" means it will use whatever media the inline
                # result itself has (stickers, photos, or documents), while
                # respecting the user's text (caption) and formatting.
                return types.InputBotInlineMessageMediaAuto(
                    message=text,
                    entities=msg_entities,
                    reply_markup=markup
                )
            else:
                return types.InputBotInlineMessageText(
                    message=text,
                    no_webpage=not link_preview,
                    entities=msg_entities,
                    reply_markup=markup
                )
        elif isinstance(geo, (types.InputGeoPoint, types.GeoPoint)):
            return types.InputBotInlineMessageMediaGeo(
                geo_point=utils.get_input_geo(geo),
                period=period,
                reply_markup=markup
            )
        elif isinstance(geo, (types.InputMediaVenue, types.MessageMediaVenue)):
            if isinstance(geo, types.InputMediaVenue):
                geo_point = geo.geo_point
            else:
                geo_point = geo.geo

            return types.InputBotInlineMessageMediaVenue(
                geo_point=geo_point,
                title=geo.title,
                address=geo.address,
                provider=geo.provider,
                venue_id=geo.venue_id,
                venue_type=geo.venue_type,
                reply_markup=markup
            )
        elif isinstance(contact, (
                types.InputMediaContact, types.MessageMediaContact)):
            return types.InputBotInlineMessageMediaContact(
                phone_number=contact.phone_number,
                first_name=contact.first_name,
                last_name=contact.last_name,
                vcard=contact.vcard,
                reply_markup=markup
            )
        elif game:
            return types.InputBotInlineMessageGame(
                reply_markup=markup
            )
        else:
            raise ValueError('No text, game or valid geo or contact given')
<commit_msg>Fix sending of documents in inline results<commit_after>import hashlib

from .. import functions, types
from ... import utils

class InlineBuilder:
    """
    Helper class to allow defining `InlineQuery
    <telethon.events.inlinequery.InlineQuery>` ``results``.

    Common arguments to all methods are
    explained here to avoid repetition:

        text (`str`, optional):
            If present, the user will send a text
            message with this text upon being clicked.

        link_preview (`bool`, optional):
            Whether to show a link preview in the sent
            text message or not.

        geo (:tl:`InputGeoPoint`, :tl:`GeoPoint`, :tl:`InputMediaVenue`, :tl:`MessageMediaVenue`, optional):
            If present, it may either be a geo point or a venue.

        period (int, optional):
            The period in seconds to be used for geo points.

        contact (:tl:`InputMediaContact`, :tl:`MessageMediaContact`, optional):
            If present, it must be the contact information to send.

        game (`bool`, optional):
            May be `True` to indicate that the game will be sent.

        buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`, :tl:`KeyboardButton`, optional):
            Same as ``buttons`` for `client.send_message()
            <telethon.client.messages.MessageMethods.send_message>`.

        parse_mode (`str`, optional):
            Same as ``parse_mode`` for `client.send_message()
            <telethon.client.messageparse.MessageParseMethods.parse_mode>`.

        id (`str`, optional):
            The string ID to use for this result. If not present, it
            will be the SHA256 hexadecimal digest of converting the
            created :tl:`InputBotInlineResult` with empty ID to ``bytes()``,
            so that the ID will be deterministic for the same input.

            .. note::

                If two inputs are exactly the same, their IDs will be the same
                too. If you send two articles with the same ID, it will raise
                ``ResultIdDuplicateError``. Consider giving them an explicit
                ID if you need to send two results that are the same.
    """
    def __init__(self, client):
        self._client = client

    # noinspection PyIncorrectDocstring
    async def article(
            self, title, description=None,
            *, url=None, thumb=None, content=None,
            id=None, text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates new inline result of article type.

        Args:
            title (`str`):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            url (`str`, optional):
                The URL to be shown for this result.

            thumb (:tl:`InputWebDocument`, optional):
                The thumbnail to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.

            content (:tl:`InputWebDocument`, optional):
                The content to be shown for this result.
                For now it has to be a :tl:`InputWebDocument` if present.
        """
        # TODO Does 'article' work always?
        # article, photo, gif, mpeg4_gif, video, audio,
        # voice, document, location, venue, contact, game
        result = types.InputBotInlineResult(
            id=id or '',
            type='article',
            send_message=await self._message(
                text=text, parse_mode=parse_mode, link_preview=link_preview,
                geo=geo, period=period,
                contact=contact,
                game=game,
                buttons=buttons
            ),
            title=title,
            description=description,
            url=url,
            thumb=thumb,
            content=content
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def photo(
            self, file, *, id=None, include_media=True,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates a new inline result of photo type.

        Args:
            include_media (`bool`, optional):
                Whether the photo file used to display the result should be
                included in the message itself or not. By default, the photo
                is included, and the text parameter alters the caption.

            file (`obj`, optional):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.
        """
        try:
            fh = utils.get_input_photo(file)
        except TypeError:
            _, media, _ = await self._client._file_to_media(
                file, allow_cache=True, as_image=True
            )
            if isinstance(media, types.InputPhoto):
                fh = media
            else:
                r = await self._client(functions.messages.UploadMediaRequest(
                    types.InputPeerSelf(), media=media
                ))
                fh = utils.get_input_photo(r.photo)

        result = types.InputBotInlineResultPhoto(
            id=id or '',
            type='photo',
            photo=fh,
            send_message=await self._message(
                text=text or '',
                parse_mode=parse_mode,
                link_preview=link_preview,
                media=include_media,
                geo=geo,
                period=period,
                contact=contact,
                game=game,
                buttons=buttons
            )
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def document(
            self, file, title=None, *, description=None, type=None,
            mime_type=None, attributes=None, force_document=False,
            voice_note=False, video_note=False, use_cache=True, id=None,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None,
            include_media=True
    ):
        """
        Creates a new inline result of document type.

        `use_cache`, `mime_type`, `attributes`, `force_document`,
        `voice_note` and `video_note` are described in `client.send_file
        <telethon.client.uploads.UploadMethods.send_file>`.

        Args:
            file (`obj`):
                Same as ``file`` for `client.send_file()
                <telethon.client.uploads.UploadMethods.send_file>`.

            title (`str`, optional):
                The title to be shown for this result.

            description (`str`, optional):
                Further explanation of what this result means.

            type (`str`, optional):
                The type of the document. May be one of: photo, gif,
                mpeg4_gif, video, audio, voice, document, sticker.

                See "Type of the result" in https://core.telegram.org/bots/api.

            include_media (`bool`, optional):
                Whether the document file used to display the result should be
                included in the message itself or not. By default, the document
                is included, and the text parameter alters the caption.
        """
        if type is None:
            if voice_note:
                type = 'voice'
            else:
                type = 'file'

        try:
            fh = utils.get_input_document(file)
        except TypeError:
            _, media, _ = await self._client._file_to_media(
                file,
                mime_type=mime_type,
                attributes=attributes,
                force_document=True,
                voice_note=voice_note,
                video_note=video_note,
                allow_cache=use_cache
            )
            if isinstance(media, types.InputDocument):
                fh = media
            else:
                r = await self._client(functions.messages.UploadMediaRequest(
                    types.InputPeerSelf(), media=media
                ))
                fh = utils.get_input_document(r.document)

        result = types.InputBotInlineResultDocument(
            id=id or '',
            type=type,
            document=fh,
            send_message=await self._message(
                # Empty string for text if there's media but text is None.
                # We may want to display a document but send text; however
                # default to sending the media (without text, i.e. stickers).
                text=text or '',
                parse_mode=parse_mode,
                link_preview=link_preview,
                media=include_media,
                geo=geo,
                period=period,
                contact=contact,
                game=game,
                buttons=buttons
            ),
            title=title,
            description=description
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    # noinspection PyIncorrectDocstring
    async def game(
            self, short_name, *, id=None,
            text=None, parse_mode=(), link_preview=True,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        """
        Creates a new inline result of game type.

        Args:
            short_name (`str`):
                The short name of the game to use.
        """
        result = types.InputBotInlineResultGame(
            id=id or '',
            short_name=short_name,
            send_message=await self._message(
                text=text, parse_mode=parse_mode, link_preview=link_preview,
                geo=geo, period=period,
                contact=contact,
                game=game,
                buttons=buttons
            )
        )
        if id is None:
            result.id = hashlib.sha256(bytes(result)).hexdigest()

        return result

    async def _message(
            self, *,
            text=None, parse_mode=(), link_preview=True, media=False,
            geo=None, period=60, contact=None, game=False, buttons=None
    ):
        # Empty strings are valid but false-y; if they're empty use dummy '\0'
        args = ('\0' if text == '' else text, geo, contact, game)
        if sum(1 for x in args if x is not None and x is not False) != 1:
            raise ValueError(
                'Must set exactly one of text, geo, contact or game (set {})'
                .format(', '.join(x[0] for x in zip(
                    'text geo contact game'.split(), args) if x[1]) or 'none')
            )

        markup = self._client.build_reply_markup(buttons, inline_only=True)
        if text is not None:
            text, msg_entities = await self._client._parse_message_text(
                text, parse_mode
            )
            if media:
                # "MediaAuto" means it will use whatever media the inline
                # result itself has (stickers, photos, or documents), while
                # respecting the user's text (caption) and formatting.
                return types.InputBotInlineMessageMediaAuto(
                    message=text,
                    entities=msg_entities,
                    reply_markup=markup
                )
            else:
                return types.InputBotInlineMessageText(
                    message=text,
                    no_webpage=not link_preview,
                    entities=msg_entities,
                    reply_markup=markup
                )
        elif isinstance(geo, (types.InputGeoPoint, types.GeoPoint)):
            return types.InputBotInlineMessageMediaGeo(
                geo_point=utils.get_input_geo(geo),
                period=period,
                reply_markup=markup
            )
        elif isinstance(geo, (types.InputMediaVenue, types.MessageMediaVenue)):
            if isinstance(geo, types.InputMediaVenue):
                geo_point = geo.geo_point
            else:
                geo_point = geo.geo

            return types.InputBotInlineMessageMediaVenue(
                geo_point=geo_point,
                title=geo.title,
                address=geo.address,
                provider=geo.provider,
                venue_id=geo.venue_id,
                venue_type=geo.venue_type,
                reply_markup=markup
            )
        elif isinstance(contact, (
                types.InputMediaContact, types.MessageMediaContact)):
            return types.InputBotInlineMessageMediaContact(
                phone_number=contact.phone_number,
                first_name=contact.first_name,
                last_name=contact.last_name,
                vcard=contact.vcard,
                reply_markup=markup
            )
        elif game:
            return types.InputBotInlineMessageGame(
                reply_markup=markup
            )
        else:
            raise ValueError('No text, game or valid geo or contact given')
<|endoftext|>


<commit_before># Copyright 2014 Cisco Systems, Inc.
# All Rights Reserved.
#
# 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.
import functools

from oslo_log import log as logging
import six

from tempest import config
from tempest.scenario import manager
from tempest import test

CONF = config.CONF
LOG = logging.getLogger(__name__)

class TestGettingAddress(manager.NetworkScenarioTest):
    """Create network with subnets: one IPv4 and
    one or few IPv6 in a given address mode
    Boot 2 VMs on this network
    Allocate and assign 2 FIP4
    Check that vNICs of all VMs gets all addresses actually assigned
    Ping4 to one VM from another one
    If ping6 available in VM, do ping6 to all v6 addresses
    """

    @classmethod
    def skip_checks(cls):
        super(TestGettingAddress, cls).skip_checks()
        if not (CONF.network_feature_enabled.ipv6
                and CONF.network_feature_enabled.ipv6_subnet_attributes):
            raise cls.skipException('IPv6 or its attributes not supported')
        if not (CONF.network.tenant_networks_reachable
                or CONF.network.public_network_id):
            msg = ('Either tenant_networks_reachable must be "true", or '
                   'public_network_id must be defined.')
            raise cls.skipException(msg)
        if CONF.baremetal.driver_enabled:
            msg = ('Baremetal does not currently support network isolation')
            raise cls.skipException(msg)

    @classmethod
    def setup_credentials(cls):
        # Create no network resources for these tests.
        cls.set_network_resources()
        super(TestGettingAddress, cls).setup_credentials()

    def setUp(self):
        super(TestGettingAddress, self).setUp()
        self.keypair = self.create_keypair()
        self.sec_grp = self._create_security_group(tenant_id=self.tenant_id)
        self.srv_kwargs = {
            'key_name': self.keypair['name'],
            'security_groups': [{'name': self.sec_grp['name']}]}

    def prepare_network(self, address6_mode, n_subnets6=1):
        """Creates network with
         given number of IPv6 subnets in the given mode and
         one IPv4 subnet
         Creates router with ports on all subnets
        """
        self.network = self._create_network(tenant_id=self.tenant_id)
        sub4 = self._create_subnet(network=self.network,
                                   namestart='sub4',
                                   ip_version=4,)

        router = self._get_router(tenant_id=self.tenant_id)
        sub4.add_to_router(router_id=router['id'])
        self.addCleanup(sub4.delete)

        for _ in range(n_subnets6):
            sub6 = self._create_subnet(network=self.network,
                                       namestart='sub6',
                                       ip_version=6,
                                       ipv6_ra_mode=address6_mode,
                                       ipv6_address_mode=address6_mode)

            sub6.add_to_router(router_id=router['id'])
            self.addCleanup(sub6.delete)

    @staticmethod
    def define_server_ips(srv):
        ips = {'4': None, '6': []}
        for net_name, nics in six.iteritems(srv['addresses']):
            for nic in nics:
                if nic['version'] == 6:
                    ips['6'].append(nic['addr'])
                else:
                    ips['4'] = nic['addr']
        return ips

    def prepare_server(self):
        username = CONF.compute.image_ssh_user

        create_kwargs = self.srv_kwargs
        create_kwargs['networks'] = [{'uuid': self.network.id}]

        srv = self.create_server(create_kwargs=create_kwargs)
        fip = self.create_floating_ip(thing=srv)
        ips = self.define_server_ips(srv=srv)
        ssh = self.get_remote_client(
            server_or_ip=fip.floating_ip_address,
            username=username)
        return ssh, ips

    def _prepare_and_test(self, address6_mode, n_subnets6=1):
        self.prepare_network(address6_mode=address6_mode,
                             n_subnets6=n_subnets6)

        sshv4_1, ips_from_api_1 = self.prepare_server()
        sshv4_2, ips_from_api_2 = self.prepare_server()

        def guest_has_address(ssh, addr):
            return addr in ssh.get_ip_list()

        # get addresses assigned to vNIC as reported by 'ip address' utility
        ips_from_ip_1 = sshv4_1.get_ip_list()
        ips_from_ip_2 = sshv4_2.get_ip_list()
        self.assertIn(ips_from_api_1['4'], ips_from_ip_1)
        self.assertIn(ips_from_api_2['4'], ips_from_ip_2)
        for i in range(n_subnets6):
            # v6 should be configured since the image supports it
            # It can take time for ipv6 automatic address to get assigned
            srv1_v6_addr_assigned = functools.partial(
                guest_has_address, sshv4_1, ips_from_api_1['6'][i])

            srv2_v6_addr_assigned = functools.partial(
                guest_has_address, sshv4_2, ips_from_api_2['6'][i])

            self.assertTrue(test.call_until_true(srv1_v6_addr_assigned,
                                                 CONF.compute.ping_timeout, 1))

            self.assertTrue(test.call_until_true(srv2_v6_addr_assigned,
                                                 CONF.compute.ping_timeout, 1))

        result = sshv4_1.ping_host(ips_from_api_2['4'])
        self.assertIn('0% packet loss', result)
        result = sshv4_2.ping_host(ips_from_api_1['4'])
        self.assertIn('0% packet loss', result)

        # Some VM (like cirros) may not have ping6 utility
        result = sshv4_1.exec_command('whereis ping6')
        is_ping6 = False if result == 'ping6:\n' else True
        if is_ping6:
            for i in range(n_subnets6):
                result = sshv4_1.ping_host(ips_from_api_2['6'][i])
                self.assertIn('0% packet loss', result)
                result = sshv4_2.ping_host(ips_from_api_1['6'][i])
                self.assertIn('0% packet loss', result)
        else:
            LOG.warning('Ping6 is not available, skipping')

    @test.idempotent_id('2c92df61-29f0-4eaa-bee3-7c65bef62a43')
    @test.services('compute', 'network')
    def test_slaac_from_os(self):
        self._prepare_and_test(address6_mode='slaac')

    @test.idempotent_id('d7e1f858-187c-45a6-89c9-bdafde619a9f')
    @test.services('compute', 'network')
    def test_dhcp6_stateless_from_os(self):
        self._prepare_and_test(address6_mode='dhcpv6-stateless')

    @test.idempotent_id('7ab23f41-833b-4a16-a7c9-5b42fe6d4123')
    @test.services('compute', 'network')
    def test_multi_prefix_dhcpv6_stateless(self):
        self._prepare_and_test(address6_mode='dhcpv6-stateless', n_subnets6=2)

    @test.idempotent_id('dec222b1-180c-4098-b8c5-cc1b8342d611')
    @test.services('compute', 'network')
    def test_multi_prefix_slaac(self):
        self._prepare_and_test(address6_mode='slaac', n_subnets6=2)
<commit_msg>Enhance IPv6 scenario - Ping default GW<commit_after># Copyright 2014 Cisco Systems, Inc.
# All Rights Reserved.
#
# 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.
import functools

from oslo_log import log as logging
import six

from tempest import config
from tempest.scenario import manager
from tempest import test

CONF = config.CONF
LOG = logging.getLogger(__name__)

class TestGettingAddress(manager.NetworkScenarioTest):
    """Test Summary:

    1. Create network with subnets:
        1.1. one IPv4 and
        1.2. one or more IPv6 in a given address mode
    2. Boot 2 VMs on this network
    3. Allocate and assign 2 FIP4
    4. Check that vNICs of all VMs gets all addresses actually assigned
    5. Each VM will ping the other's v4 private address
    6. If ping6 available in VM, each VM will ping all of the other's  v6
       addresses as well as the router's
    """

    @classmethod
    def skip_checks(cls):
        super(TestGettingAddress, cls).skip_checks()
        if not (CONF.network_feature_enabled.ipv6
                and CONF.network_feature_enabled.ipv6_subnet_attributes):
            raise cls.skipException('IPv6 or its attributes not supported')
        if not (CONF.network.tenant_networks_reachable
                or CONF.network.public_network_id):
            msg = ('Either tenant_networks_reachable must be "true", or '
                   'public_network_id must be defined.')
            raise cls.skipException(msg)
        if CONF.baremetal.driver_enabled:
            msg = ('Baremetal does not currently support network isolation')
            raise cls.skipException(msg)

    @classmethod
    def setup_credentials(cls):
        # Create no network resources for these tests.
        cls.set_network_resources()
        super(TestGettingAddress, cls).setup_credentials()

    def setUp(self):
        super(TestGettingAddress, self).setUp()
        self.keypair = self.create_keypair()
        self.sec_grp = self._create_security_group(tenant_id=self.tenant_id)
        self.srv_kwargs = {
            'key_name': self.keypair['name'],
            'security_groups': [{'name': self.sec_grp['name']}]}

    def prepare_network(self, address6_mode, n_subnets6=1):
        """Creates network with
         given number of IPv6 subnets in the given mode and
         one IPv4 subnet
         Creates router with ports on all subnets
        """
        self.network = self._create_network(tenant_id=self.tenant_id)
        sub4 = self._create_subnet(network=self.network,
                                   namestart='sub4',
                                   ip_version=4)

        router = self._get_router(tenant_id=self.tenant_id)
        sub4.add_to_router(router_id=router['id'])
        self.addCleanup(sub4.delete)

        self.subnets_v6 = []
        for _ in range(n_subnets6):
            sub6 = self._create_subnet(network=self.network,
                                       namestart='sub6',
                                       ip_version=6,
                                       ipv6_ra_mode=address6_mode,
                                       ipv6_address_mode=address6_mode)

            sub6.add_to_router(router_id=router['id'])
            self.addCleanup(sub6.delete)
            self.subnets_v6.append(sub6)

    @staticmethod
    def define_server_ips(srv):
        ips = {'4': None, '6': []}
        for net_name, nics in six.iteritems(srv['addresses']):
            for nic in nics:
                if nic['version'] == 6:
                    ips['6'].append(nic['addr'])
                else:
                    ips['4'] = nic['addr']
        return ips

    def prepare_server(self):
        username = CONF.compute.image_ssh_user

        create_kwargs = self.srv_kwargs
        create_kwargs['networks'] = [{'uuid': self.network.id}]

        srv = self.create_server(create_kwargs=create_kwargs)
        fip = self.create_floating_ip(thing=srv)
        ips = self.define_server_ips(srv=srv)
        ssh = self.get_remote_client(
            server_or_ip=fip.floating_ip_address,
            username=username)
        return ssh, ips

    def _prepare_and_test(self, address6_mode, n_subnets6=1):
        self.prepare_network(address6_mode=address6_mode,
                             n_subnets6=n_subnets6)

        sshv4_1, ips_from_api_1 = self.prepare_server()
        sshv4_2, ips_from_api_2 = self.prepare_server()

        def guest_has_address(ssh, addr):
            return addr in ssh.get_ip_list()

        # get addresses assigned to vNIC as reported by 'ip address' utility
        ips_from_ip_1 = sshv4_1.get_ip_list()
        ips_from_ip_2 = sshv4_2.get_ip_list()
        self.assertIn(ips_from_api_1['4'], ips_from_ip_1)
        self.assertIn(ips_from_api_2['4'], ips_from_ip_2)
        for i in range(n_subnets6):
            # v6 should be configured since the image supports it
            # It can take time for ipv6 automatic address to get assigned
            srv1_v6_addr_assigned = functools.partial(
                guest_has_address, sshv4_1, ips_from_api_1['6'][i])

            srv2_v6_addr_assigned = functools.partial(
                guest_has_address, sshv4_2, ips_from_api_2['6'][i])

            self.assertTrue(test.call_until_true(srv1_v6_addr_assigned,
                                                 CONF.compute.ping_timeout, 1))

            self.assertTrue(test.call_until_true(srv2_v6_addr_assigned,
                                                 CONF.compute.ping_timeout, 1))

        self._check_connectivity(sshv4_1, ips_from_api_2['4'])
        self._check_connectivity(sshv4_2, ips_from_api_1['4'])

        # Some VM (like cirros) may not have ping6 utility
        result = sshv4_1.exec_command('whereis ping6')
        is_ping6 = False if result == 'ping6:\n' else True
        if is_ping6:
            for i in range(n_subnets6):
                self._check_connectivity(sshv4_1,
                                         ips_from_api_2['6'][i])
                self._check_connectivity(sshv4_1,
                                         self.subnets_v6[i].gateway_ip)
                self._check_connectivity(sshv4_2,
                                         ips_from_api_1['6'][i])
                self._check_connectivity(sshv4_2,
                                         self.subnets_v6[i].gateway_ip)
        else:
            LOG.warning('Ping6 is not available, skipping')

    def _check_connectivity(self, source, dest):
        self.assertTrue(
            self._check_remote_connectivity(source, dest),
            "Timed out waiting for %s to become reachable from %s" %
            (dest, source.ssh_client.host)
        )

    @test.idempotent_id('2c92df61-29f0-4eaa-bee3-7c65bef62a43')
    @test.services('compute', 'network')
    def test_slaac_from_os(self):
        self._prepare_and_test(address6_mode='slaac')

    @test.idempotent_id('d7e1f858-187c-45a6-89c9-bdafde619a9f')
    @test.services('compute', 'network')
    def test_dhcp6_stateless_from_os(self):
        self._prepare_and_test(address6_mode='dhcpv6-stateless')

    @test.idempotent_id('7ab23f41-833b-4a16-a7c9-5b42fe6d4123')
    @test.services('compute', 'network')
    def test_multi_prefix_dhcpv6_stateless(self):
        self._prepare_and_test(address6_mode='dhcpv6-stateless', n_subnets6=2)

    @test.idempotent_id('dec222b1-180c-4098-b8c5-cc1b8342d611')
    @test.services('compute', 'network')
    def test_multi_prefix_slaac(self):
        self._prepare_and_test(address6_mode='slaac', n_subnets6=2)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Revision $Id$

from __future__ import with_statement

from subprocess import Popen, PIPE
import tempfile

import roslib.msgs
import roslib.srvs
import roslib.rospack

from rosdoc.rdcore import *

doxy_template = load_tmpl('doxy.template')
external_template = load_tmpl('external.html')
header_template = load_tmpl('header.html')
footer_template = load_tmpl('footer.html')
manifest_template = load_tmpl('manifest.html')

# other templates

## @param ext str: extension (msg or srv)
## @param type str: type name
## @param text str: full text definition of type
def _msg_srv_tmpl(ext, type, text):
    tmpl = '
%s.%s

'%(type, ext)
    for line in text.split('\n'):
        parts = line.split('#')
        if len(parts) > 1:
            tmpl = tmpl + parts[0]+'#%s
'%('#'.join(parts[1:]))
        else:
            tmpl = tmpl + "%s
"%parts[0]
    return tmpl+"

"

## create include files for messages and services
def generate_msg_srv_includes(package, tmp, to_delete):
    for ext, list_types, spec_file in [('msg', roslib.msgs.list_msg_types, roslib.msgs.msg_file),\
                                       ('srv', roslib.srvs.list_srv_types, roslib.srvs.srv_file)]:
        for type_ in list_types(package, False):
            with open(spec_file(package, type_), 'r') as f:
                p = os.path.join(tmp, '%s.%s.html'%(type_, ext))
                with open(p, 'w') as html_file:
                    to_delete.append(p)
                    html_file.write(_msg_srv_tmpl(ext, type_, f.read()))

## @param package str: package name
## @param rd_config dict: rosdoc configuration parameters for this doxygen build
## @param m Manifest : package manifest
## @param html_dir str: directory to store doxygen files
def create_package_template(package, rd_config, m, path, html_dir,
                            header_filename, footer_filename):
    # TODO: allow rd_config to specify excludes and whatnot

    # TODO: replace with general purpose key/value parser/substitution to enable <export><doxygen key="foo" val="var"></export> feature

    # determine the value of overridable keys
    file_patterns = '*.c *.cpp *.h *.cc *.hh *.py *.dox'
    excludes = '%s/build/'%path
    # last one wins
    for e in m.get_export('doxygen', 'excludes'):
        # prepend the packages path
        excludes = '%s/%s'%(path, e)
    # rd_config wins
    if rd_config and 'excludes' in rd_config:
        excludes = rd_config['excludes']

    # last one wins
    for e in m.get_export('doxygen', 'file-patterns'):
        file_patterns = e
    # rd_config wins
    if rd_config and 'file_patterns' in rd_config:
        file_patterns = rd_config['file_patterns']

    vars = { '$INPUT':  path, '$PROJECT_NAME': package,
             '$EXCLUDE_PROP': excludes, '$FILE_PATTERNS': file_patterns,
             '$HTML_OUTPUT': os.path.abspath(html_dir),
             '$HTML_HEADER': header_filename, '$HTML_FOOTER': footer_filename,
             '$OUTPUT_DIRECTORY': html_dir}
    return instantiate_template(doxy_template, vars)

## Processes manifest for package and then generates templates for
## header, footer, and manifest include file
## @param package: package to create templates for
## @type  package: str
## @param rd_config: rosdoc configuration dictionary
## @type  rd_config: dict
## @param path: file path to package
## @type  path: str
## @param m: package manifest or None
## @type  m: manifest.Manifest
## @return: header, footer, manifest
## @rtype: (str, str, str)
def load_manifest_vars(ctx, rd_config, package, path, docdir, package_htmldir, m):
    author = license = dependencies = description = usedby = status = notes = li_vc = li_url = brief = ''

    # by default, assume that packages are on wiki
    home_url = 'http://ros.org/wiki/%s'%package

    if rd_config:
        if 'homepage' in rd_config:
            home_url = rd_config['homepage']
            print "HOMEPAGE", home_url

    project_link = '%s'%(home_url, package)
    if m:
        license = m.license or ''
        author = m.author or ''
        description = m.description or ''
        status = m.status or ''
        notes = m.notes or ''

        if m.brief:
            brief = ": "+m.brief

        li_url = '
Homepage: %s
'%(m.url, m.url)
        if m.versioncontrol:
            vcurl = m.versioncontrol.url
            li_vc = '
Version Control (%s): %s
'%(m.versioncontrol.type, vcurl, vcurl)

        if m.depends:
            dependencies = "
\n" + \
                           li_package_links(ctx, package, [d.package for d in m.depends], docdir, package_htmldir)
        else:
            dependencies = "None
"

    dependson1 = roslib.rospack.rospackexec(['depends-on1', package]).split('\n')
    # filter depends by what we're actually documenting
    dependson1 = [d for d in dependson1 if d and ctx.should_document(d)]
    if dependson1:
        usedby = "
\n"+li_package_links(ctx, package, dependson1, docdir, package_htmldir)
    else:
        usedby = "None
"

    # include links to msgs/srvs
    msgs = roslib.msgs.list_msg_types(package, False)
    srvs = roslib.srvs.list_srv_types(package, False)

    return {'$package': package,
            '$projectlink': project_link, '$license': license,
            '$dependencies': dependencies, '$usedby': usedby,
            '$description': description, '$brief': brief,
            '$author': author, '$status':status,
            '$notes':notes, '$li_vc': li_vc, '$li_url': li_url,
            }

## utility to write string data to files and handle unicode
def _write_to_file(f, tmpl):
    try:
        if type(tmpl) == str:
            f.write(tmpl)
        else: #iso-8859-1 is the declared encoding of doxygen
            f.write(tmpl.encode('utf-8'))
        f.flush()
    except:
        print "ERROR, f[%s], tmpl[%s]"%(f, tmpl)
        raise

def run_doxygen(package, doxygen_file, quiet=False):
    try:
        command = ['doxygen', doxygen_file]
        print "doxygen-ating %s [%s]"%(package, ' '.join(command))
        if quiet:
            Popen(command, stdout=PIPE, stderr=PIPE).communicate()
        else:
            Popen(command, stdout=PIPE).communicate()
    except OSError, (errno, strerr):
        #fatal
        print """\nERROR: It appears that you do not have doxygen installed.
If you are on Ubuntu/Debian, you can install doxygen by typing:

   sudo apt-get install doxygen
"""
        sys.exit(1)

#TODO: move elsewhere
def run_rxdeps(package, pkg_doc_dir):
    try:
        command = ['rxdeps', '-s', '--target=%s'%package, '--cluster', '-o', os.path.join(pkg_doc_dir, '%s_deps.pdf'%package)]
        print "rxdeping %s [%s]"%(package, ' '.join(command))
        Popen(command, stdout=PIPE).communicate()
    except OSError, (errno, strerr):
        print >> sys.stderr, """\nERROR: It appears that you do not have rxdeps installed.
Package dependency tree links will not work properly.
"""
    except:
        print >> sys.stderr, "ERROR: rxdeps failed"

## Main entrypoint into creating doxygen files
## @param disable_rxdeps: if True, don't generate rxdeps documenation (note: this parameter is volatile as rxdeps generation will be moved outside of doxygenator)
## @type  disable_rxdeps: bool
## @param quiet: suppress most stdout output
## @type  quiet: bool
## @return [str]: list of packages that were successfully generated
def generate_doxygen(ctx, quiet=False, disable_rxdeps=False):

    #TODO: move external generator into its own generator
    #TODO: move rxdeps into its own generator

    # setup temp directory
    tmp = 'tmp'
    if not os.path.exists(tmp):
        os.mkdir(tmp)

    success = []

    dir = ctx.docdir
    # dictionary mapping packages to paths
    packages = ctx.packages
    # list of packages that we are documenting
    doc_packages = ctx.doc_packages
    external_docs = ctx.external_docs
    rd_configs = ctx.rd_configs
    manifests = ctx.manifests

    tmpls = [header_template, footer_template, manifest_template]
    try:
        for package, path in packages.iteritems():
            if not package in doc_packages or \
                   not ctx.has_builder(package, 'doxygen'):
                continue

            print "doxygenate", package

            # the logic for the doxygen builder is different from
            # others as doxygen is the default builder if no config is
            # declared
            rd_config = rd_configs.get(package, None)
            if rd_config:
                # currently only allow one doxygen build per package. This is not inherent, it
                # just requires rewriting higher-level logic
                rd_config = [d for d in ctx.rd_configs[package] if d['builder'] == 'doxygen'][0]

            # Configuration (all are optional)
            #
            # name: Documentation set name (e.g. C++ API)
            # output_dir: Directory to store files (default '.')
            # file_patterns: override FILE_PATTERNS
            # excludes: override EXCLUDES

            # doxygenator currently does some non-doxygen work.
            # pkg_doc_dir is the pointer to the directory for these non-doxygen
            # tools. html_dir is the path for doxygen
            pkg_doc_dir = html_path(package, ctx.docdir)

            # compute the html directory for doxygen
            html_dir = html_path(package, ctx.docdir)
            if rd_config and 'output_dir' in rd_config:
                html_dir = os.path.join(html_dir, rd_config['output_dir'])

            # have to makedirs for external packages
            if not os.path.exists(pkg_doc_dir):
                os.makedirs(pkg_doc_dir)

            files = []
            try:
                header_file = tempfile.NamedTemporaryFile('w+')
                footer_file = tempfile.NamedTemporaryFile('w+')
                doxygen_file = tempfile.NamedTemporaryFile('w+')
                manifest_file = open(os.path.join(tmp, 'manifest.html'), 'w')
                files = [header_file, footer_file, manifest_file, doxygen_file]
                to_delete = [manifest_file]

                # create the doxygen templates and wiki header

                generate_msg_srv_includes(package, tmp, to_delete)

                # - instantiate the templates
                manifest_ = manifests[package] if package in manifests else None
                vars = load_manifest_vars(ctx, rd_config, package, path, dir, html_dir, manifest_)
                header, footer, manifest_html = [instantiate_template(t, vars) for t in tmpls]

                if not disable_rxdeps:
                    run_rxdeps(package, pkg_doc_dir)
                if package not in external_docs:
                    doxy = \
                        create_package_template(package, rd_config, manifest_,
                                                path, html_dir,
                                                header_file.name, footer_file.name)
                    for f, tmpl in zip(files, [header, footer, manifest_html, doxy]):
                        _write_to_file(f, tmpl)
                    # doxygenate
                    run_doxygen(package, doxygen_file.name, quiet=quiet)
                else:
                    # for external packages, we generate a landing page that is
                    # similar to the doxygen, but we don't actually run doxygen as
                    # it is time consuming for packages that provide their own docs

                    external_link = ctx.external_docs[package]

                    # Override mainpage title if 'name' is in config
                    title = 'Main Page'
                    if rd_config:
                        title = rd_config.get('name', title)
                    vars = { '$package': package, '$external_link': external_link,
                             '$header': header, '$footer': footer,
                             '$manifest': manifest_html,
                             # doxygen vars
                             '$relpath$': '../../',
                             '$title': package+': '+title,
                             }

                    with open(os.path.join(pkg_doc_dir, 'index.html'), 'w') as ext_html_file:
                        _write_to_file(ext_html_file, instantiate_template(external_template, vars))

                # support files (stylesheets)
                import shutil
                dstyles_in = os.path.join(ctx.template_dir, 'doxygen.css')
                dstyles_css = os.path.join(html_dir, 'doxygen.css')
                shutil.copyfile(dstyles_in, dstyles_css)

                success.append(package)
            finally:
                for f in files:
                    f.close()
    finally:
        pass
    return success
<commit_msg>rosdoc: disabling rxdeps for now<commit_after>#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Revision $Id$

from __future__ import with_statement

from subprocess import Popen, PIPE
import tempfile

import roslib.msgs
import roslib.srvs
import roslib.rospack

from rosdoc.rdcore import *

doxy_template = load_tmpl('doxy.template')
external_template = load_tmpl('external.html')
header_template = load_tmpl('header.html')
footer_template = load_tmpl('footer.html')
manifest_template = load_tmpl('manifest.html')

# other templates

## @param ext str: extension (msg or srv)
## @param type str: type name
## @param text str: full text definition of type
def _msg_srv_tmpl(ext, type, text):
    tmpl = '
%s.%s

'%(type, ext)
    for line in text.split('\n'):
        parts = line.split('#')
        if len(parts) > 1:
            tmpl = tmpl + parts[0]+'#%s
'%('#'.join(parts[1:]))
        else:
            tmpl = tmpl + "%s
"%parts[0]
    return tmpl+"

"

## create include files for messages and services
def generate_msg_srv_includes(package, tmp, to_delete):
    for ext, list_types, spec_file in [('msg', roslib.msgs.list_msg_types, roslib.msgs.msg_file),\
                                       ('srv', roslib.srvs.list_srv_types, roslib.srvs.srv_file)]:
        for type_ in list_types(package, False):
            with open(spec_file(package, type_), 'r') as f:
                p = os.path.join(tmp, '%s.%s.html'%(type_, ext))
                with open(p, 'w') as html_file:
                    to_delete.append(p)
                    html_file.write(_msg_srv_tmpl(ext, type_, f.read()))

## @param package str: package name
## @param rd_config dict: rosdoc configuration parameters for this doxygen build
## @param m Manifest : package manifest
## @param html_dir str: directory to store doxygen files
def create_package_template(package, rd_config, m, path, html_dir,
                            header_filename, footer_filename):
    # TODO: allow rd_config to specify excludes and whatnot

    # TODO: replace with general purpose key/value parser/substitution to enable <export><doxygen key="foo" val="var"></export> feature

    # determine the value of overridable keys
    file_patterns = '*.c *.cpp *.h *.cc *.hh *.py *.dox'
    excludes = '%s/build/'%path
    # last one wins
    for e in m.get_export('doxygen', 'excludes'):
        # prepend the packages path
        excludes = '%s/%s'%(path, e)
    # rd_config wins
    if rd_config and 'excludes' in rd_config:
        excludes = rd_config['excludes']

    # last one wins
    for e in m.get_export('doxygen', 'file-patterns'):
        file_patterns = e
    # rd_config wins
    if rd_config and 'file_patterns' in rd_config:
        file_patterns = rd_config['file_patterns']

    vars = { '$INPUT':  path, '$PROJECT_NAME': package,
             '$EXCLUDE_PROP': excludes, '$FILE_PATTERNS': file_patterns,
             '$HTML_OUTPUT': os.path.abspath(html_dir),
             '$HTML_HEADER': header_filename, '$HTML_FOOTER': footer_filename,
             '$OUTPUT_DIRECTORY': html_dir}
    return instantiate_template(doxy_template, vars)

## Processes manifest for package and then generates templates for
## header, footer, and manifest include file
## @param package: package to create templates for
## @type  package: str
## @param rd_config: rosdoc configuration dictionary
## @type  rd_config: dict
## @param path: file path to package
## @type  path: str
## @param m: package manifest or None
## @type  m: manifest.Manifest
## @return: header, footer, manifest
## @rtype: (str, str, str)
def load_manifest_vars(ctx, rd_config, package, path, docdir, package_htmldir, m):
    author = license = dependencies = description = usedby = status = notes = li_vc = li_url = brief = ''

    # by default, assume that packages are on wiki
    home_url = 'http://ros.org/wiki/%s'%package

    if rd_config:
        if 'homepage' in rd_config:
            home_url = rd_config['homepage']
            print "HOMEPAGE", home_url

    project_link = '%s'%(home_url, package)
    if m:
        license = m.license or ''
        author = m.author or ''
        description = m.description or ''
        status = m.status or ''
        notes = m.notes or ''

        if m.brief:
            brief = ": "+m.brief

        li_url = '
Homepage: %s
'%(m.url, m.url)
        if m.versioncontrol:
            vcurl = m.versioncontrol.url
            li_vc = '
Version Control (%s): %s
'%(m.versioncontrol.type, vcurl, vcurl)

        if m.depends:
            dependencies = "
\n" + \
                           li_package_links(ctx, package, [d.package for d in m.depends], docdir, package_htmldir)
        else:
            dependencies = "None
"

    dependson1 = roslib.rospack.rospackexec(['depends-on1', package]).split('\n')
    # filter depends by what we're actually documenting
    dependson1 = [d for d in dependson1 if d and ctx.should_document(d)]
    if dependson1:
        usedby = "
\n"+li_package_links(ctx, package, dependson1, docdir, package_htmldir)
    else:
        usedby = "None
"

    # include links to msgs/srvs
    msgs = roslib.msgs.list_msg_types(package, False)
    srvs = roslib.srvs.list_srv_types(package, False)

    return {'$package': package,
            '$projectlink': project_link, '$license': license,
            '$dependencies': dependencies, '$usedby': usedby,
            '$description': description, '$brief': brief,
            '$author': author, '$status':status,
            '$notes':notes, '$li_vc': li_vc, '$li_url': li_url,
            }

## utility to write string data to files and handle unicode
def _write_to_file(f, tmpl):
    try:
        if type(tmpl) == str:
            f.write(tmpl)
        else: #iso-8859-1 is the declared encoding of doxygen
            f.write(tmpl.encode('utf-8'))
        f.flush()
    except:
        print "ERROR, f[%s], tmpl[%s]"%(f, tmpl)
        raise

def run_doxygen(package, doxygen_file, quiet=False):
    try:
        command = ['doxygen', doxygen_file]
        print "doxygen-ating %s [%s]"%(package, ' '.join(command))
        if quiet:
            Popen(command, stdout=PIPE, stderr=PIPE).communicate()
        else:
            Popen(command, stdout=PIPE).communicate()
    except OSError, (errno, strerr):
        #fatal
        print """\nERROR: It appears that you do not have doxygen installed.
If you are on Ubuntu/Debian, you can install doxygen by typing:

   sudo apt-get install doxygen
"""
        sys.exit(1)

#TODO: move elsewhere
def run_rxdeps(package, pkg_doc_dir):
    if 1:
        return

    try:
        command = ['rxdeps', '-s', '--target=%s'%package, '--cluster', '-o', os.path.join(pkg_doc_dir, '%s_deps.pdf'%package)]
        print "rxdeping %s [%s]"%(package, ' '.join(command))
        Popen(command, stdout=PIPE).communicate()
    except OSError, (errno, strerr):
        print >> sys.stderr, """\nERROR: It appears that you do not have rxdeps installed.
Package dependency tree links will not work properly.
"""
    except:
        print >> sys.stderr, "ERROR: rxdeps failed"

## Main entrypoint into creating doxygen files
## @param disable_rxdeps: if True, don't generate rxdeps documenation (note: this parameter is volatile as rxdeps generation will be moved outside of doxygenator)
## @type  disable_rxdeps: bool
## @param quiet: suppress most stdout output
## @type  quiet: bool
## @return [str]: list of packages that were successfully generated
def generate_doxygen(ctx, quiet=False, disable_rxdeps=False):

    #TODO: move external generator into its own generator
    #TODO: move rxdeps into its own generator

    # setup temp directory
    tmp = 'tmp'
    if not os.path.exists(tmp):
        os.mkdir(tmp)

    success = []

    dir = ctx.docdir
    # dictionary mapping packages to paths
    packages = ctx.packages
    # list of packages that we are documenting
    doc_packages = ctx.doc_packages
    external_docs = ctx.external_docs
    rd_configs = ctx.rd_configs
    manifests = ctx.manifests

    tmpls = [header_template, footer_template, manifest_template]
    try:
        for package, path in packages.iteritems():
            if not package in doc_packages or \
                   not ctx.has_builder(package, 'doxygen'):
                continue

            print "doxygenate", package

            # the logic for the doxygen builder is different from
            # others as doxygen is the default builder if no config is
            # declared
            rd_config = rd_configs.get(package, None)
            if rd_config:
                # currently only allow one doxygen build per package. This is not inherent, it
                # just requires rewriting higher-level logic
                rd_config = [d for d in ctx.rd_configs[package] if d['builder'] == 'doxygen'][0]

            # Configuration (all are optional)
            #
            # name: Documentation set name (e.g. C++ API)
            # output_dir: Directory to store files (default '.')
            # file_patterns: override FILE_PATTERNS
            # excludes: override EXCLUDES

            # doxygenator currently does some non-doxygen work.
            # pkg_doc_dir is the pointer to the directory for these non-doxygen
            # tools. html_dir is the path for doxygen
            pkg_doc_dir = html_path(package, ctx.docdir)

            # compute the html directory for doxygen
            html_dir = html_path(package, ctx.docdir)
            if rd_config and 'output_dir' in rd_config:
                html_dir = os.path.join(html_dir, rd_config['output_dir'])

            # have to makedirs for external packages
            if not os.path.exists(pkg_doc_dir):
                os.makedirs(pkg_doc_dir)

            files = []
            try:
                header_file = tempfile.NamedTemporaryFile('w+')
                footer_file = tempfile.NamedTemporaryFile('w+')
                doxygen_file = tempfile.NamedTemporaryFile('w+')
                manifest_file = open(os.path.join(tmp, 'manifest.html'), 'w')
                files = [header_file, footer_file, manifest_file, doxygen_file]
                to_delete = [manifest_file]

                # create the doxygen templates and wiki header

                generate_msg_srv_includes(package, tmp, to_delete)

                # - instantiate the templates
                manifest_ = manifests[package] if package in manifests else None
                vars = load_manifest_vars(ctx, rd_config, package, path, dir, html_dir, manifest_)
                header, footer, manifest_html = [instantiate_template(t, vars) for t in tmpls]

                if not disable_rxdeps:
                    run_rxdeps(package, pkg_doc_dir)
                if package not in external_docs:
                    doxy = \
                        create_package_template(package, rd_config, manifest_,
                                                path, html_dir,
                                                header_file.name, footer_file.name)
                    for f, tmpl in zip(files, [header, footer, manifest_html, doxy]):
                        _write_to_file(f, tmpl)
                    # doxygenate
                    run_doxygen(package, doxygen_file.name, quiet=quiet)
                else:
                    # for external packages, we generate a landing page that is
                    # similar to the doxygen, but we don't actually run doxygen as
                    # it is time consuming for packages that provide their own docs

                    external_link = ctx.external_docs[package]

                    # Override mainpage title if 'name' is in config
                    title = 'Main Page'
                    if rd_config:
                        title = rd_config.get('name', title)
                    vars = { '$package': package, '$external_link': external_link,
                             '$header': header, '$footer': footer,
                             '$manifest': manifest_html,
                             # doxygen vars
                             '$relpath$': '../../',
                             '$title': package+': '+title,
                             }

                    with open(os.path.join(pkg_doc_dir, 'index.html'), 'w') as ext_html_file:
                        _write_to_file(ext_html_file, instantiate_template(external_template, vars))

                # support files (stylesheets)
                import shutil
                dstyles_in = os.path.join(ctx.template_dir, 'doxygen.css')
                dstyles_css = os.path.join(html_dir, 'doxygen.css')
                shutil.copyfile(dstyles_in, dstyles_css)

                success.append(package)
            finally:
                for f in files:
                    f.close()
    finally:
        pass
    return success
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
""" \file identity/settings/__init__.py
\brief Defines base project settings.

\author Erich Healy (cactuscommander) ErichRHealy@gmail.com
\author Ryan Leckey (mehcode) leckey.ryan@gmail.com

\copyright Copyright © 2012 Concordus Applications, Inc.
           \n \n
           Permission is hereby granted, free of charge, to any person
           obtaining a copy of this software and associated documentation
           files (the "Software"), to deal in the Software without restriction,
           including without limitation the rights to use, copy, modify, merge,
           publish, distribute, sublicense, and/or sell copies of the Software,
           and to permit persons to whom the Software is furnished to do so,
           subject to the following conditions:
           \n \n
           The above copyright notice and this permission notice shall be
           included in all copies or substantial portions of the Software.
           \n \n
           THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
           EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
           MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
           NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
           BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
           ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
           CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
           SOFTWARE.
"""
from os import path
from django.core.urlresolvers import reverse
from django.conf import global_settings

# Project directories
PROJECT_NAME = 'identity'
PROJECT_ROOT = path.abspath(path.join(__file__, '..', '..'))

# Root directories
SITE_ROOT = path.abspath(path.join(PROJECT_ROOT, '..'))

# Debugging settings
DEBUG = True
TEMPLATE_DEBUG = DEBUG
TASTYPIE_FULL_DEBUG = DEBUG

# Forces debugging to false if server is accessed from an IP not listed here
INTERNAL_IPS = ('127.0.0.1',)

# Debugging tool bar configuration
DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False}

# Site administrators; used for logging
ADMINS = ()

# Same as ADMINS but in a different group
MANAGERS = ADMINS

# Database configuration.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': path.join(SITE_ROOT, 'sqlite3.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
}

# Local time zone for this installation.
TIME_ZONE = 'America/Los_Angeles'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT.
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
STATIC_ROOT = ''

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = ()

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '58yno^b5t^wvr)e4s8tet4&#odp+9@+tu*nrq#*#d*gds%06w+'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',)

# Middleware classes
MIDDLEWARE_CLASSES = ('django.middleware.gzip.GZipMiddleware',)

if DEBUG:
    MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

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

# Root URL configuration
ROOT_URLCONF = '{}.urls'.format(PROJECT_NAME)

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = '{}.wsgi.application'.format(PROJECT_NAME)

# Template directories
TEMPLATE_DIRS = (
    path.join(PROJECT_ROOT, 'templates'))

# Installed applications
INSTALLED_APPS = (
    # Django core
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Django administration
    'django.contrib.admin',
    'django.contrib.admindocs',

    # Project
    PROJECT_NAME,

    # Accounts
    '{}.account'.format(PROJECT_NAME),)

if DEBUG:
    INSTALLED_APPS += (
        'django_extensions',
        'debug_toolbar',)

# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {},
    'handlers': {},
    'loggers': {}}

# Define the extended user profile
AUTH_PROFILE_MODULE = 'account.Profile'

# Authentication backends
AUTHENTICATION_BACKENDS = (
    'identity.account.backends.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# LDAP Settings
LDAP = {
    'VERSION': '3',
    'HOST': 'spock',
    'DN': 'dc=concordusapps,dc=com',
    'ADMIN': {
        'DN': '',
        'PASS': ''
    }
}

# Identity settings
IDENTITY = {
    # Containing a SERVER key here indicates that this can be used as a central
    # server of authetnication and identity management.
    'SERVER': {
    },

    # Containing a CLIENT key here indicates that this can be used as a
    # client that connects to an identity server (either django-identity or
    # another that is SAML and SCIM compliant).
#    'CLIENT': {
#    }
}

# Attempt to include local settings
try:
    from .local import *
except ImportError:
    pass
<commit_msg>Removed uneccessary setting variables.<commit_after># -*- coding: utf-8 -*-
""" \file identity/settings/__init__.py
\brief Defines base project settings.

\author Erich Healy (cactuscommander) ErichRHealy@gmail.com
\author Ryan Leckey (mehcode) leckey.ryan@gmail.com

\copyright Copyright © 2012 Concordus Applications, Inc.
           \n \n
           Permission is hereby granted, free of charge, to any person
           obtaining a copy of this software and associated documentation
           files (the "Software"), to deal in the Software without restriction,
           including without limitation the rights to use, copy, modify, merge,
           publish, distribute, sublicense, and/or sell copies of the Software,
           and to permit persons to whom the Software is furnished to do so,
           subject to the following conditions:
           \n \n
           The above copyright notice and this permission notice shall be
           included in all copies or substantial portions of the Software.
           \n \n
           THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
           EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
           MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
           NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
           BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
           ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
           CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
           SOFTWARE.
"""
from os import path
from django.core.urlresolvers import reverse
from django.conf import global_settings

# Project directories
PROJECT_NAME = 'identity'
PROJECT_ROOT = path.abspath(path.join(__file__, '..', '..'))

# Root directories
SITE_ROOT = path.abspath(path.join(PROJECT_ROOT, '..'))

# Debugging settings
DEBUG = True
TEMPLATE_DEBUG = DEBUG
TASTYPIE_FULL_DEBUG = DEBUG

# Forces debugging to false if server is accessed from an IP not listed here
INTERNAL_IPS = ('127.0.0.1',)

# Debugging tool bar configuration
DEBUG_TOOLBAR_CONFIG = {
    'INTERCEPT_REDIRECTS': False}

# Site administrators; used for logging
ADMINS = ()

# Same as ADMINS but in a different group
MANAGERS = ADMINS

# Database configuration.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': path.join(SITE_ROOT, 'sqlite3.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
}

# Local time zone for this installation.
TIME_ZONE = 'America/Los_Angeles'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT.
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
STATIC_ROOT = ''

# URL prefix for static files.
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = ()

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '58yno^b5t^wvr)e4s8tet4&#odp+9@+tu*nrq#*#d*gds%06w+'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',)

# Middleware classes
MIDDLEWARE_CLASSES = ('django.middleware.gzip.GZipMiddleware',)

if DEBUG:
    MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)

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

# Root URL configuration
ROOT_URLCONF = '{}.urls'.format(PROJECT_NAME)

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = '{}.wsgi.application'.format(PROJECT_NAME)

# Template directories
TEMPLATE_DIRS = (
    path.join(PROJECT_ROOT, 'templates'))

# Installed applications
INSTALLED_APPS = (
    # Django core
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Django administration
    'django.contrib.admin',
    'django.contrib.admindocs',

    # Project
    PROJECT_NAME,

    # Accounts
    '{}.account'.format(PROJECT_NAME),)

if DEBUG:
    INSTALLED_APPS += (
        'django_extensions',
        'debug_toolbar',)

# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {},
    'handlers': {},
    'loggers': {}}

# Define the extended user profile
AUTH_PROFILE_MODULE = 'account.Profile'

# Authentication backends
AUTHENTICATION_BACKENDS = (
    'identity.account.backends.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# LDAP Settings
LDAP = {
    'VERSION': '3',
    'HOST': 'spock',
    'DN': 'dc=concordusapps,dc=com',
    'ADMIN': {
        'DN': '',
        'PASS': ''
    }
}

# Attempt to include local settings
try:
    from .local import *
except ImportError:
    pass
<|endoftext|>


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

# Copyright (c) 2018 shmilee

'''
Contains pickled cache loader class.
'''

from ..glogger import getGLogger
from .base import BasePckLoader

__all__ = ['CachePckLoader']
log = getGLogger('L')

class CachePckLoader(BasePckLoader):
    '''
    Load datasets from cache dict. Return a dictionary-like object.

    Notes
    -----
    1. *path*: a name of cache loader, is 'dict.cache'.
    2. keys: (k1, k2, g1/k3, g1/k4, g2/k5).
       invalid: (/k0, /g2/k6, /g2/k7/, /g2/k8/k9)
    '''
    __slots__ = ['_cache_dict']

    def _check_path_access(self, path):
        self._cache_dict = path
        return True

    def _special_check_path(self):
        self.path = 'dict.cache'
        return True

    def _special_open(self):
        return self._cache_dict

    def _special_close(self, tmpobj):
        pass

    def _special_getkeys(self, tmpobj):
        mykeys = []
        for k in tmpobj.keys():
            if isinstance(tmpobj[k], dict):
                mykeys.extend([k + '/' + kk for kk in tmpobj[k]])
            else:
                mykeys.append(k)
        return mykeys

    def _special_get(self, tmpobj, key):
        keyl = key.strip('/').split('/')
        if len(keyl) == 1:
            return tmpobj[key]
        elif len(keyl) == 2:
            return tmpobj[keyl[0]][keyl[1]]
        else:
            raise ValueError('Wrong key "%s"!' % key)
<commit_msg>fix CachePckLoader, _special_check_path()<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2018 shmilee

'''
Contains pickled cache loader class.
'''

from ..glogger import getGLogger
from .base import BasePckLoader

__all__ = ['CachePckLoader']
log = getGLogger('L')

class CachePckLoader(BasePckLoader):
    '''
    Load datasets from cache dict. Return a dictionary-like object.

    Notes
    -----
    1. *path*: a name of cache loader, is 'dict.cache'.
    2. keys: (k1, k2, g1/k3, g1/k4, g2/k5).
       invalid: (/k0, /g2/k6, /g2/k7/, /g2/k8/k9)
    '''
    __slots__ = ['_cache_dict']

    def _check_path_access(self, path):
        self._cache_dict = path
        return True

    def _special_check_path(self):
        if isinstance(self.path, dict):
            self.path = 'dict.cache'
            return True
        else:
            log.error("'%s' is not a dict!" % self.path)
            return False

    def _special_open(self):
        return self._cache_dict

    def _special_close(self, tmpobj):
        pass

    def _special_getkeys(self, tmpobj):
        mykeys = []
        for k in tmpobj.keys():
            if isinstance(tmpobj[k], dict):
                mykeys.extend([k + '/' + kk for kk in tmpobj[k]])
            else:
                mykeys.append(k)
        return mykeys

    def _special_get(self, tmpobj, key):
        keyl = key.strip('/').split('/')
        if len(keyl) == 1:
            return tmpobj[key]
        elif len(keyl) == 2:
            return tmpobj[keyl[0]][keyl[1]]
        else:
            raise ValueError('Wrong key "%s"!' % key)
<|endoftext|>