<commit_before>import asyncio
import os
import tempfile
import typing
from typing import Text, Optional, List, Union, Dict

from rasa import model, data
from rasa.core.domain import Domain
from rasa.skill import SkillSelector

from rasa.cli.utils import (
    create_output_path,
    print_success,
    missing_config_keys,
    print_warning,
    get_validated_path,
    print_error,
)
from rasa.constants import (
    DEFAULT_MODELS_PATH,
    CONFIG_MANDATORY_KEYS,
    CONFIG_MANDATORY_KEYS_CORE,
    CONFIG_MANDATORY_KEYS_NLU,
    FALLBACK_CONFIG_PATH,
)

def train(
    domain: Text,
    config: Text,
    training_files: Union[Text, List[Text]],
    output: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        train_async(domain, config, training_files, output, force_training, kwargs)
    )

async def train_async(
    domain: Optional,
    config: Text,
    training_files: Optional[Union[Text, List[Text]]],
    output: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Rasa model (Core and NLU).

    Args:
        domain: Path to the domain file.
        config: Path to the config for Core and NLU.
        training_files: Paths to the training data for Core and NLU.
        output: Output path.
        force_training: If `True` retrain model even if data has not changed.
        kwargs: Additional training parameters.

    Returns:
        Path of the trained model archive.
    """
    config = get_valid_config(config, CONFIG_MANDATORY_KEYS)

    train_path = tempfile.mkdtemp()
    old_model = model.get_latest_model(output)
    retrain_core = True
    retrain_nlu = True

    skill_imports = SkillSelector.load(config)
    domain = Domain.load(domain, skill_imports)

    story_directory, nlu_data_directory = data.get_core_nlu_directories(
        training_files, skill_imports
    )
    new_fingerprint = model.model_fingerprint(
        config, domain, nlu_data_directory, story_directory
    )

    dialogue_data_not_present = not os.listdir(story_directory)
    nlu_data_not_present = not os.listdir(nlu_data_directory)

    if dialogue_data_not_present and nlu_data_not_present:
        print_error(
            "No training data given. Please provide dialogue and NLU data in "
            "order to train a Rasa model."
        )
        return

    if dialogue_data_not_present:
        print_warning(
            "No dialogue data present. Just a Rasa NLU model will be trained."
        )
        return train_nlu(config, nlu_data_directory, output, None)

    if nlu_data_not_present:
        print_warning("No NLU data present. Just a Rasa Core model will be trained.")
        return await train_core_async(
            domain, config, story_directory, output, None, kwargs
        )

    if not force_training and old_model:
        unpacked = model.unpack_model(old_model)
        old_core, old_nlu = model.get_model_subdirectories(unpacked)
        last_fingerprint = model.fingerprint_from_path(unpacked)

        if not model.core_fingerprint_changed(last_fingerprint, new_fingerprint):
            target_path = os.path.join(train_path, "core")
            retrain_core = not model.merge_model(old_core, target_path)

        if not model.nlu_fingerprint_changed(last_fingerprint, new_fingerprint):
            target_path = os.path.join(train_path, "nlu")
            retrain_nlu = not model.merge_model(old_nlu, target_path)

    if force_training or retrain_core:
        await train_core_async(
            domain, config, story_directory, output, train_path, kwargs
        )
    else:
        print (
            "Dialogue data / configuration did not change. "
            "No need to retrain dialogue model."
        )

    if force_training or retrain_nlu:
        train_nlu(config, nlu_data_directory, output, train_path)
    else:
        print ("NLU data / configuration did not change. No need to retrain NLU model.")

    if retrain_core or retrain_nlu:
        output = create_output_path(output)
        model.create_package_rasa(train_path, output, new_fingerprint)

        print_success("Your bot is trained and ready to take for a spin!")

        return output
    else:
        print_success(
            "Nothing changed. You can use the old model stored at '{}'"
            "".format(os.path.abspath(old_model))
        )

        return old_model

def train_core(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text],
    kwargs: Optional[Dict],
) -> Optional[Text]:
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        train_core_async(domain, config, stories, output, train_path, kwargs)
    )

async def train_core_async(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text] = None,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Core model.

    Args:
        domain: Path to the domain file.
        config: Path to the config file for Core.
        stories: Path to the Core training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.
        kwargs: Additional training parameters.

    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """
    import rasa.core.train

    config = get_valid_config(config, CONFIG_MANDATORY_KEYS_CORE)

    _train_path = train_path or tempfile.mkdtemp()

    if isinstance(Domain, str) or not train_path:
        skill_imports = SkillSelector.load(config)
        domain = Domain.load(domain, skill_imports)
        story_directory = data.get_core_directory(stories, skill_imports)
    else:
        story_directory = stories

    if not os.listdir(story_directory):
        print_error(
            "No dialogue data given. Please provide dialogue data in order to "
            "train a Rasa Core model."
        )
        return

    # normal (not compare) training
    await rasa.core.train(
        domain_file=domain,
        stories_file=story_directory,
        output_path=os.path.join(_train_path, "core"),
        policy_config=config,
        kwargs=kwargs,
    )

    if not train_path:
        # Only Core was trained.
        output_path = create_output_path(output, prefix="core-")
        new_fingerprint = model.model_fingerprint(
            config, domain, stories=story_directory
        )
        model.create_package_rasa(_train_path, output_path, new_fingerprint)
        print_success(
            "Your Rasa Core model is trained and saved at '{}'.".format(output_path)
        )

        return output_path

    return _train_path

def train_nlu(
    config: Text, nlu_data: Text, output: Text, train_path: Optional[Text]
) -> Optional[Text]:
    """Trains a NLU model.

    Args:
        config: Path to the config file for NLU.
        nlu_data: Path to the NLU training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.

    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """
    import rasa.nlu

    config = get_valid_config(config, CONFIG_MANDATORY_KEYS_NLU)

    if not train_path:
        # Training is not part of the stack model training, hence we have to load the
        # nlu data first
        skill_imports = SkillSelector.load(config)
        nlu_data_directory = data.get_nlu_directory(nlu_data, skill_imports)
    else:
        nlu_data_directory = nlu_data

    if not os.listdir(nlu_data_directory):
        print_error(
            "No NLU data given. Please provide NLU data in order to train "
            "a Rasa NLU model."
        )
        return

    _train_path = train_path or tempfile.mkdtemp()

    _, nlu_model, _ = rasa.nlu.train(
        config, nlu_data_directory, _train_path, fixed_model_name="nlu"
    )

    if not train_path:
        output_path = create_output_path(output, prefix="nlu-")
        new_fingerprint = model.model_fingerprint(config, nlu_data=nlu_data_directory)
        model.create_package_rasa(_train_path, output_path, new_fingerprint)
        print_success(
            "Your Rasa NLU model is trained and saved at '{}'.".format(output_path)
        )

        return output_path

    return _train_path

def enrich_config(config_path, missing_keys, FALLBACK_CONFIG_PATH):
    import rasa.utils.io

    config_data = rasa.utils.io.read_yaml_file(config_path)
    fallback_config_data = rasa.utils.io.read_yaml_file(FALLBACK_CONFIG_PATH)

    for k in missing_keys:
        config_data[k] = fallback_config_data[k]

    rasa.utils.io.write_yaml_file(config_data, config_path)

def get_valid_config(config: Text, mandatory_keys: List[Text]) -> Text:
    config_path = get_validated_path(config, "config", FALLBACK_CONFIG_PATH)

    missing_keys = missing_config_keys(config_path, mandatory_keys)

    if missing_keys:
        print_warning(
            "Configuration file '{}' is missing mandatory parameters: "
            "{}. Filling missing parameters from fallback configuration file: '{}'."
            "".format(config, ", ".join(missing_keys), FALLBACK_CONFIG_PATH)
        )
        enrich_config(config_path, missing_keys, FALLBACK_CONFIG_PATH)

    return config_path
<commit_msg>improved comment<commit_after>import asyncio
import os
import tempfile
import typing
from typing import Text, Optional, List, Union, Dict

from rasa import model, data
from rasa.core.domain import Domain
from rasa.skill import SkillSelector

from rasa.cli.utils import (
    create_output_path,
    print_success,
    missing_config_keys,
    print_warning,
    get_validated_path,
    print_error,
)
from rasa.constants import (
    DEFAULT_MODELS_PATH,
    CONFIG_MANDATORY_KEYS,
    CONFIG_MANDATORY_KEYS_CORE,
    CONFIG_MANDATORY_KEYS_NLU,
    FALLBACK_CONFIG_PATH,
)

def train(
    domain: Text,
    config: Text,
    training_files: Union[Text, List[Text]],
    output: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        train_async(domain, config, training_files, output, force_training, kwargs)
    )

async def train_async(
    domain: Optional,
    config: Text,
    training_files: Optional[Union[Text, List[Text]]],
    output: Text = DEFAULT_MODELS_PATH,
    force_training: bool = False,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Rasa model (Core and NLU).

    Args:
        domain: Path to the domain file.
        config: Path to the config for Core and NLU.
        training_files: Paths to the training data for Core and NLU.
        output: Output path.
        force_training: If `True` retrain model even if data has not changed.
        kwargs: Additional training parameters.

    Returns:
        Path of the trained model archive.
    """
    config = get_valid_config(config, CONFIG_MANDATORY_KEYS)

    train_path = tempfile.mkdtemp()
    old_model = model.get_latest_model(output)
    retrain_core = True
    retrain_nlu = True

    skill_imports = SkillSelector.load(config)
    domain = Domain.load(domain, skill_imports)

    story_directory, nlu_data_directory = data.get_core_nlu_directories(
        training_files, skill_imports
    )
    new_fingerprint = model.model_fingerprint(
        config, domain, nlu_data_directory, story_directory
    )

    dialogue_data_not_present = not os.listdir(story_directory)
    nlu_data_not_present = not os.listdir(nlu_data_directory)

    if dialogue_data_not_present and nlu_data_not_present:
        print_error(
            "No training data given. Please provide dialogue and NLU data in "
            "order to train a Rasa model."
        )
        return

    if dialogue_data_not_present:
        print_warning(
            "No dialogue data present. Just a Rasa NLU model will be trained."
        )
        return train_nlu(config, nlu_data_directory, output, None)

    if nlu_data_not_present:
        print_warning("No NLU data present. Just a Rasa Core model will be trained.")
        return await train_core_async(
            domain, config, story_directory, output, None, kwargs
        )

    if not force_training and old_model:
        unpacked = model.unpack_model(old_model)
        old_core, old_nlu = model.get_model_subdirectories(unpacked)
        last_fingerprint = model.fingerprint_from_path(unpacked)

        if not model.core_fingerprint_changed(last_fingerprint, new_fingerprint):
            target_path = os.path.join(train_path, "core")
            retrain_core = not model.merge_model(old_core, target_path)

        if not model.nlu_fingerprint_changed(last_fingerprint, new_fingerprint):
            target_path = os.path.join(train_path, "nlu")
            retrain_nlu = not model.merge_model(old_nlu, target_path)

    if force_training or retrain_core:
        await train_core_async(
            domain, config, story_directory, output, train_path, kwargs
        )
    else:
        print (
            "Dialogue data / configuration did not change. "
            "No need to retrain dialogue model."
        )

    if force_training or retrain_nlu:
        train_nlu(config, nlu_data_directory, output, train_path)
    else:
        print ("NLU data / configuration did not change. No need to retrain NLU model.")

    if retrain_core or retrain_nlu:
        output = create_output_path(output)
        model.create_package_rasa(train_path, output, new_fingerprint)

        print_success("Your bot is trained and ready to take for a spin!")

        return output
    else:
        print_success(
            "Nothing changed. You can use the old model stored at '{}'"
            "".format(os.path.abspath(old_model))
        )

        return old_model

def train_core(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text],
    kwargs: Optional[Dict],
) -> Optional[Text]:
    loop = asyncio.get_event_loop()
    return loop.run_until_complete(
        train_core_async(domain, config, stories, output, train_path, kwargs)
    )

async def train_core_async(
    domain: Union[Domain, Text],
    config: Text,
    stories: Text,
    output: Text,
    train_path: Optional[Text] = None,
    kwargs: Optional[Dict] = None,
) -> Optional[Text]:
    """Trains a Core model.

    Args:
        domain: Path to the domain file.
        config: Path to the config file for Core.
        stories: Path to the Core training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.
        kwargs: Additional training parameters.

    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """
    import rasa.core.train

    config = get_valid_config(config, CONFIG_MANDATORY_KEYS_CORE)

    _train_path = train_path or tempfile.mkdtemp()

    if isinstance(Domain, str) or not train_path:
        skill_imports = SkillSelector.load(config)
        domain = Domain.load(domain, skill_imports)
        story_directory = data.get_core_directory(stories, skill_imports)
    else:
        story_directory = stories

    if not os.listdir(story_directory):
        print_error(
            "No dialogue data given. Please provide dialogue data in order to "
            "train a Rasa Core model."
        )
        return

    # normal (not compare) training
    await rasa.core.train(
        domain_file=domain,
        stories_file=story_directory,
        output_path=os.path.join(_train_path, "core"),
        policy_config=config,
        kwargs=kwargs,
    )

    if not train_path:
        # Only Core was trained.
        output_path = create_output_path(output, prefix="core-")
        new_fingerprint = model.model_fingerprint(
            config, domain, stories=story_directory
        )
        model.create_package_rasa(_train_path, output_path, new_fingerprint)
        print_success(
            "Your Rasa Core model is trained and saved at '{}'.".format(output_path)
        )

        return output_path

    return _train_path

def train_nlu(
    config: Text, nlu_data: Text, output: Text, train_path: Optional[Text]
) -> Optional[Text]:
    """Trains a NLU model.

    Args:
        config: Path to the config file for NLU.
        nlu_data: Path to the NLU training data.
        output: Output path.
        train_path: If `None` the model will be trained in a temporary
            directory, otherwise in the provided directory.

    Returns:
        If `train_path` is given it returns the path to the model archive,
        otherwise the path to the directory with the trained model files.

    """
    import rasa.nlu

    config = get_valid_config(config, CONFIG_MANDATORY_KEYS_NLU)

    if not train_path:
        # training NLU only hence the training files still have to be selected
        skill_imports = SkillSelector.load(config)
        nlu_data_directory = data.get_nlu_directory(nlu_data, skill_imports)
    else:
        nlu_data_directory = nlu_data

    if not os.listdir(nlu_data_directory):
        print_error(
            "No NLU data given. Please provide NLU data in order to train "
            "a Rasa NLU model."
        )
        return

    _train_path = train_path or tempfile.mkdtemp()

    _, nlu_model, _ = rasa.nlu.train(
        config, nlu_data_directory, _train_path, fixed_model_name="nlu"
    )

    if not train_path:
        output_path = create_output_path(output, prefix="nlu-")
        new_fingerprint = model.model_fingerprint(config, nlu_data=nlu_data_directory)
        model.create_package_rasa(_train_path, output_path, new_fingerprint)
        print_success(
            "Your Rasa NLU model is trained and saved at '{}'.".format(output_path)
        )

        return output_path

    return _train_path

def enrich_config(config_path, missing_keys, FALLBACK_CONFIG_PATH):
    import rasa.utils.io

    config_data = rasa.utils.io.read_yaml_file(config_path)
    fallback_config_data = rasa.utils.io.read_yaml_file(FALLBACK_CONFIG_PATH)

    for k in missing_keys:
        config_data[k] = fallback_config_data[k]

    rasa.utils.io.write_yaml_file(config_data, config_path)

def get_valid_config(config: Text, mandatory_keys: List[Text]) -> Text:
    config_path = get_validated_path(config, "config", FALLBACK_CONFIG_PATH)

    missing_keys = missing_config_keys(config_path, mandatory_keys)

    if missing_keys:
        print_warning(
            "Configuration file '{}' is missing mandatory parameters: "
            "{}. Filling missing parameters from fallback configuration file: '{}'."
            "".format(config, ", ".join(missing_keys), FALLBACK_CONFIG_PATH)
        )
        enrich_config(config_path, missing_keys, FALLBACK_CONFIG_PATH)

    return config_path
<|endoftext|>


<commit_before>#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you 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 .
#
import uno
import traceback
import time
from datetime import date as dateTimeObject
from .ViewHandler import ViewHandler
from .TextFieldHandler import TextFieldHandler
from ..common.Desktop import Desktop
from ..common.Helper import Helper
from ..document.OfficeDocument import OfficeDocument
from ..common.Configuration import Configuration

from com.sun.star.container import NoSuchElementException
from com.sun.star.lang import WrappedTargetException
from com.sun.star.i18n.NumberFormatIndex import DATE_SYS_DDMMYY
from com.sun.star.view.DocumentZoomType import ENTIRE_PAGE
from com.sun.star.beans.PropertyState import DIRECT_VALUE

class TextDocument(object):

    xTextDocument = None

    def __init__(self, xMSF,listener=None,bShowStatusIndicator=None,
        FrameName=None,_sPreviewURL=None,_moduleIdentifier=None,
        _textDocument=None, xArgs=None):

        self.xMSF = xMSF

        if listener is not None:
            if FrameName is not None:
                '''creates an instance of TextDocument
                and creates a named frame.
                No document is actually loaded into this frame.'''
                self.xFrame = OfficeDocument.createNewFrame(
                    xMSF, listener, FrameName)
                return

            elif _sPreviewURL is not None:
                '''creates an instance of TextDocument by
                loading a given URL as preview'''
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
                TextDocument.xTextDocument = self.loadAsPreview(_sPreviewURL, True)

            elif xArgs is not None:
                '''creates an instance of TextDocument
                and creates a frame and loads a document'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
                TextDocument.xTextDocument = OfficeDocument.load(
                    xFrame, URL, "_self", xArgs);
                self.xWindowPeer = xFrame.getComponentWindow()
                self.m_xDocProps = TextDocument.xTextDocument.DocumentProperties
                CharLocale = Helper.getUnoStructValue(
                    TextDocument.xTextDocument, "CharLocale");
                return

            else:
                '''creates an instance of TextDocument from
                the desktop's current frame'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = self.xDesktop.getActiveFrame()
                TextDocument.xTextDocument = self.xFrame.getController().Model

        elif _moduleIdentifier is not None:
            try:
                '''create the empty document, and set its module identifier'''
                TextDocument.xTextDocument = xMSF.createInstance(
                    "com.sun.star.text.TextDocument")
                TextDocument.xTextDocument.initNew()
                TextDocument.xTextDocument.setIdentifier(
                    _moduleIdentifier.Identifier)
                # load the document into a blank frame
                xDesktop = Desktop.getDesktop(xMSF)
                loadArgs = range(1)
                loadArgs[0] = "Model"
                loadArgs[0] = -1
                loadArgs[0] = TextDocument.xTextDocument
                loadArgs[0] = DIRECT_VALUE
                xDesktop.loadComponentFromURL(
                    "private:object", "_blank", 0, loadArgs)
                # remember some things for later usage
                self.xFrame = TextDocument.xTextDocument.CurrentController.Frame
            except Exception, e:
                traceback.print_exc()

        elif _textDocument is not None:
            '''creates an instance of TextDocument
            from a given XTextDocument'''
            self.xFrame = _textDocument.CurrentController.Frame
            TextDocument.xTextDocument = _textDocument
        if bShowStatusIndicator:
            self.showStatusIndicator()
        self.init()

    def init(self):
        self.xWindowPeer = self.xFrame.getComponentWindow()
        self.m_xDocProps = TextDocument.xTextDocument.DocumentProperties
        self.CharLocale = Helper.getUnoStructValue(
            TextDocument.xTextDocument, "CharLocale")
        self.xText = TextDocument.xTextDocument.Text

    def showStatusIndicator(self):
        self.xProgressBar = self.xFrame.createStatusIndicator()
        self.xProgressBar.start("", 100)
        self.xProgressBar.setValue(5)

    def loadAsPreview(self, sDefaultTemplate, asTemplate):
        loadValues = range(3)
        #      open document in the Preview mode
        loadValues[0] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[0].Name = "ReadOnly"
        loadValues[0].Value = True
        loadValues[1] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[1].Name = "AsTemplate"
        if asTemplate:
            loadValues[1].Value = True
        else:
            loadValues[1].Value = False

        loadValues[2] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[2].Name = "Preview"
        loadValues[2].Value = True
        '''set the preview document to non-modified
        mode in order to avoid the 'do u want to save' box'''
        if TextDocument.xTextDocument is not None:
            try:
                TextDocument.xTextDocument.Modified = False
            except PropertyVetoException, e1:
                traceback.print_exc()

        TextDocument.xTextDocument = OfficeDocument.load(
            self.xFrame, sDefaultTemplate, "_self", loadValues)

        self.DocSize = self.getPageSize()

        myViewHandler = ViewHandler(TextDocument.xTextDocument, TextDocument.xTextDocument)
        try:
            myViewHandler.setViewSetting(
                "ZoomType", ENTIRE_PAGE)
        except Exception, e:
            traceback.print_exc()
        myFieldHandler = TextFieldHandler(self.xMSF, TextDocument.xTextDocument)
        myFieldHandler.updateDocInfoFields()
        return TextDocument.xTextDocument

    def getPageSize(self):
        try:
            xNameAccess = TextDocument.xTextDocument.StyleFamilies
            xPageStyleCollection = xNameAccess.getByName("PageStyles")
            xPageStyle = xPageStyleCollection.getByName("First Page")
            return Helper.getUnoPropertyValue(xPageStyle, "Size")
        except Exception, exception:
            traceback.print_exc()
            return None

    '''creates an instance of TextDocument and creates a
    frame and loads a document'''

    def createTextCursor(self, oCursorContainer):
        xTextCursor = oCursorContainer.createTextCursor()
        return xTextCursor

    # Todo: This method is  unsecure because the last index is not necessarily the last section
    # Todo: This Routine should be  modified, because I cannot rely on the last Table in the document to be the last in the TextTables sequence
    # to make it really safe you must acquire the Tablenames before the insertion and after the insertion of the new Table. By comparing the
    # two sequences of tablenames you can find out the tablename of the last inserted Table
    # Todo: This method is  unsecure because the last index is not necessarily the last section

    def getCharWidth(self, ScaleString):
        iScale = 200
        TextDocument.xTextDocument.lockControllers()
        iScaleLen = ScaleString.length()
        xTextCursor = createTextCursor(TextDocument.xTextDocument.Text)
        xTextCursor.gotoStart(False)
        com.sun.star.wizards.common.Helper.setUnoPropertyValue(
            xTextCursor, "PageDescName", "First Page")
        xTextCursor.String = ScaleString
        xViewCursor = TextDocument.xTextDocument.CurrentController
        xTextViewCursor = xViewCursor.ViewCursor
        xTextViewCursor.gotoStart(False)
        iFirstPos = xTextViewCursor.Position.X
        xTextViewCursor.gotoEnd(False)
        iLastPos = xTextViewCursor.Position.X
        iScale = (iLastPos - iFirstPos) / iScaleLen
        xTextCursor.gotoStart(False)
        xTextCursor.gotoEnd(True)
        xTextCursor.String = ""
        unlockallControllers()
        return iScale

    def unlockallControllers(self):
        while TextDocument.xTextDocument.hasControllersLocked():
            TextDocument.xTextDocument.unlockControllers()

    def refresh(self):
        TextDocument.xTextDocument.refresh()

    '''
    This method sets the Author of a Wizard-generated template correctly
    and adds a explanatory sentence to the template description.
    @param WizardName The name of the Wizard.
    @param TemplateDescription The old Description which is being
    appended with another sentence.
    @return void.
    '''

    def setWizardTemplateDocInfo(self, WizardName, TemplateDescription):
        try:
            xNA = Configuration.getConfigurationRoot(
                self.xMSF, "/org.openoffice.UserProfile/Data", False)
            gn = xNA.getByName("givenname")
            sn = xNA.getByName("sn")
            fullname = str(gn) + " " + str(sn)
            now = time.localtime(time.time())
            year = time.strftime("%Y", now)
            month = time.strftime("%m", now)
            day = time.strftime("%d", now)

            dateObject = dateTimeObject(int(year), int(month), int(day))
            du = Helper.DateUtils(self.xMSF, TextDocument.xTextDocument)
            ff = du.getFormat(DATE_SYS_DDMMYY)
            myDate = du.format(ff, dateObject)
            xDocProps2 = TextDocument.xTextDocument.DocumentProperties
            xDocProps2.Author = fullname
            xDocProps2.ModifiedBy = fullname
            description = xDocProps2.Description
            description = description + " " + TemplateDescription
            description = description.replace("<wizard_name>", WizardName)
            description = description.replace("<current_date>", myDate)
            xDocProps2.Description = description
        except NoSuchElementException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except WrappedTargetException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except Exception, e:
            # TODO Auto-generated catch block
            traceback.print_exc()

    '''
    Apparently there is no other way to get the
    page count of a text document other than using a cursor and
    making it jump to the last page...
    @param model the document model.
    @return the page count of the document.
    '''

    def getPageCount(self, model):
        xController = model.getCurrentController()
        xPC = xController.getViewCursor()
        xPC.jumpToLastPage()
        return xPC.getPage()

    @classmethod
    def getFrameByName(self, sFrameName, xTD):
        if xTD.TextFrames.hasByName(sFrameName):
            return xTD.TextFrames.getByName(sFrameName)

        return None
<commit_msg>pywizards: This code does nothing but an exception. Delete it<commit_after>#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you 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 .
#
import uno
import traceback
import time
from datetime import date as dateTimeObject
from .ViewHandler import ViewHandler
from .TextFieldHandler import TextFieldHandler
from ..common.Desktop import Desktop
from ..common.Helper import Helper
from ..document.OfficeDocument import OfficeDocument
from ..common.Configuration import Configuration

from com.sun.star.container import NoSuchElementException
from com.sun.star.lang import WrappedTargetException
from com.sun.star.i18n.NumberFormatIndex import DATE_SYS_DDMMYY
from com.sun.star.view.DocumentZoomType import ENTIRE_PAGE
from com.sun.star.beans.PropertyState import DIRECT_VALUE

class TextDocument(object):

    xTextDocument = None

    def __init__(self, xMSF,listener=None,bShowStatusIndicator=None,
        FrameName=None,_sPreviewURL=None,_moduleIdentifier=None,
        _textDocument=None, xArgs=None):

        self.xMSF = xMSF

        if listener is not None:
            if FrameName is not None:
                '''creates an instance of TextDocument
                and creates a named frame.
                No document is actually loaded into this frame.'''
                self.xFrame = OfficeDocument.createNewFrame(
                    xMSF, listener, FrameName)
                return

            elif _sPreviewURL is not None:
                '''creates an instance of TextDocument by
                loading a given URL as preview'''
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
                TextDocument.xTextDocument = self.loadAsPreview(_sPreviewURL, True)

            elif xArgs is not None:
                '''creates an instance of TextDocument
                and creates a frame and loads a document'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = OfficeDocument.createNewFrame(xMSF, listener)
                TextDocument.xTextDocument = OfficeDocument.load(
                    xFrame, URL, "_self", xArgs);
                self.xWindowPeer = xFrame.getComponentWindow()
                self.m_xDocProps = TextDocument.xTextDocument.DocumentProperties
                CharLocale = Helper.getUnoStructValue(
                    TextDocument.xTextDocument, "CharLocale");
                return

            else:
                '''creates an instance of TextDocument from
                the desktop's current frame'''
                self.xDesktop = Desktop.getDesktop(xMSF);
                self.xFrame = self.xDesktop.getActiveFrame()
                TextDocument.xTextDocument = self.xFrame.getController().Model

        elif _moduleIdentifier is not None:
            try:
                '''create the empty document, and set its module identifier'''
                TextDocument.xTextDocument = xMSF.createInstance(
                    "com.sun.star.text.TextDocument")
                TextDocument.xTextDocument.initNew()
                TextDocument.xTextDocument.setIdentifier(
                    _moduleIdentifier.Identifier)
                # load the document into a blank frame
                xDesktop = Desktop.getDesktop(xMSF)
                loadArgs = range(1)
                loadArgs[0] = "Model"
                loadArgs[0] = -1
                loadArgs[0] = TextDocument.xTextDocument
                loadArgs[0] = DIRECT_VALUE
                xDesktop.loadComponentFromURL(
                    "private:object", "_blank", 0, loadArgs)
                # remember some things for later usage
                self.xFrame = TextDocument.xTextDocument.CurrentController.Frame
            except Exception, e:
                traceback.print_exc()

        elif _textDocument is not None:
            '''creates an instance of TextDocument
            from a given XTextDocument'''
            self.xFrame = _textDocument.CurrentController.Frame
            TextDocument.xTextDocument = _textDocument
        if bShowStatusIndicator:
            self.showStatusIndicator()
        self.init()

    def init(self):
        self.xWindowPeer = self.xFrame.getComponentWindow()
        self.m_xDocProps = TextDocument.xTextDocument.DocumentProperties
        self.CharLocale = Helper.getUnoStructValue(
            TextDocument.xTextDocument, "CharLocale")
        self.xText = TextDocument.xTextDocument.Text

    def showStatusIndicator(self):
        self.xProgressBar = self.xFrame.createStatusIndicator()
        self.xProgressBar.start("", 100)
        self.xProgressBar.setValue(5)

    def loadAsPreview(self, sDefaultTemplate, asTemplate):
        loadValues = range(3)
        #      open document in the Preview mode
        loadValues[0] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[0].Name = "ReadOnly"
        loadValues[0].Value = True
        loadValues[1] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[1].Name = "AsTemplate"
        if asTemplate:
            loadValues[1].Value = True
        else:
            loadValues[1].Value = False

        loadValues[2] = uno.createUnoStruct(
            'com.sun.star.beans.PropertyValue')
        loadValues[2].Name = "Preview"
        loadValues[2].Value = True

        TextDocument.xTextDocument = OfficeDocument.load(
            self.xFrame, sDefaultTemplate, "_self", loadValues)

        self.DocSize = self.getPageSize()

        myViewHandler = ViewHandler(TextDocument.xTextDocument, TextDocument.xTextDocument)
        try:
            myViewHandler.setViewSetting(
                "ZoomType", ENTIRE_PAGE)
        except Exception, e:
            traceback.print_exc()
        myFieldHandler = TextFieldHandler(self.xMSF, TextDocument.xTextDocument)
        myFieldHandler.updateDocInfoFields()
        return TextDocument.xTextDocument

    def getPageSize(self):
        try:
            xNameAccess = TextDocument.xTextDocument.StyleFamilies
            xPageStyleCollection = xNameAccess.getByName("PageStyles")
            xPageStyle = xPageStyleCollection.getByName("First Page")
            return Helper.getUnoPropertyValue(xPageStyle, "Size")
        except Exception, exception:
            traceback.print_exc()
            return None

    '''creates an instance of TextDocument and creates a
    frame and loads a document'''

    def createTextCursor(self, oCursorContainer):
        xTextCursor = oCursorContainer.createTextCursor()
        return xTextCursor

    # Todo: This method is  unsecure because the last index is not necessarily the last section
    # Todo: This Routine should be  modified, because I cannot rely on the last Table in the document to be the last in the TextTables sequence
    # to make it really safe you must acquire the Tablenames before the insertion and after the insertion of the new Table. By comparing the
    # two sequences of tablenames you can find out the tablename of the last inserted Table
    # Todo: This method is  unsecure because the last index is not necessarily the last section

    def getCharWidth(self, ScaleString):
        iScale = 200
        TextDocument.xTextDocument.lockControllers()
        iScaleLen = ScaleString.length()
        xTextCursor = createTextCursor(TextDocument.xTextDocument.Text)
        xTextCursor.gotoStart(False)
        com.sun.star.wizards.common.Helper.setUnoPropertyValue(
            xTextCursor, "PageDescName", "First Page")
        xTextCursor.String = ScaleString
        xViewCursor = TextDocument.xTextDocument.CurrentController
        xTextViewCursor = xViewCursor.ViewCursor
        xTextViewCursor.gotoStart(False)
        iFirstPos = xTextViewCursor.Position.X
        xTextViewCursor.gotoEnd(False)
        iLastPos = xTextViewCursor.Position.X
        iScale = (iLastPos - iFirstPos) / iScaleLen
        xTextCursor.gotoStart(False)
        xTextCursor.gotoEnd(True)
        xTextCursor.String = ""
        unlockallControllers()
        return iScale

    def unlockallControllers(self):
        while TextDocument.xTextDocument.hasControllersLocked():
            TextDocument.xTextDocument.unlockControllers()

    def refresh(self):
        TextDocument.xTextDocument.refresh()

    '''
    This method sets the Author of a Wizard-generated template correctly
    and adds a explanatory sentence to the template description.
    @param WizardName The name of the Wizard.
    @param TemplateDescription The old Description which is being
    appended with another sentence.
    @return void.
    '''

    def setWizardTemplateDocInfo(self, WizardName, TemplateDescription):
        try:
            xNA = Configuration.getConfigurationRoot(
                self.xMSF, "/org.openoffice.UserProfile/Data", False)
            gn = xNA.getByName("givenname")
            sn = xNA.getByName("sn")
            fullname = str(gn) + " " + str(sn)
            now = time.localtime(time.time())
            year = time.strftime("%Y", now)
            month = time.strftime("%m", now)
            day = time.strftime("%d", now)

            dateObject = dateTimeObject(int(year), int(month), int(day))
            du = Helper.DateUtils(self.xMSF, TextDocument.xTextDocument)
            ff = du.getFormat(DATE_SYS_DDMMYY)
            myDate = du.format(ff, dateObject)
            xDocProps2 = TextDocument.xTextDocument.DocumentProperties
            xDocProps2.Author = fullname
            xDocProps2.ModifiedBy = fullname
            description = xDocProps2.Description
            description = description + " " + TemplateDescription
            description = description.replace("<wizard_name>", WizardName)
            description = description.replace("<current_date>", myDate)
            xDocProps2.Description = description
        except NoSuchElementException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except WrappedTargetException, e:
            # TODO Auto-generated catch block
            traceback.print_exc()
        except Exception, e:
            # TODO Auto-generated catch block
            traceback.print_exc()

    '''
    Apparently there is no other way to get the
    page count of a text document other than using a cursor and
    making it jump to the last page...
    @param model the document model.
    @return the page count of the document.
    '''

    def getPageCount(self, model):
        xController = model.getCurrentController()
        xPC = xController.getViewCursor()
        xPC.jumpToLastPage()
        return xPC.getPage()

    @classmethod
    def getFrameByName(self, sFrameName, xTD):
        if xTD.TextFrames.hasByName(sFrameName):
            return xTD.TextFrames.getByName(sFrameName)

        return None
<|endoftext|>


<commit_before>import os
import re
import sys
import time
import socket
import logging

from optparse import make_option

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django_extensions.management.utils import setup_logger, RedirectHandler,\
    signalcommand
from django_extensions.management.technical_response import null_technical_500_response

try:
    if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
        from django.contrib.staticfiles.handlers import StaticFilesHandler
        USE_STATICFILES = True
    elif 'staticfiles' in settings.INSTALLED_APPS:
        from staticfiles.handlers import StaticFilesHandler  # noqa
        USE_STATICFILES = True
    else:
        USE_STATICFILES = False
except ImportError:
    USE_STATICFILES = False

naiveip_re = re.compile(r"""^(?:
(?P<addr>
    (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) |         # IPv4 address
    (?P<ipv6>\[[a-fA-F0-9:]+\]) |               # IPv6 address
    (?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
):)?(?P<port>\d+)$""", re.X)
DEFAULT_PORT = "8000"

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--ipv6', '-6', action='store_true', dest='use_ipv6', default=False,
                    help='Tells Django to use a IPv6 address.'),
        make_option('--noreload', action='store_false', dest='use_reloader', default=True,
                    help='Tells Django to NOT use the auto-reloader.'),
        make_option('--browser', action='store_true', dest='open_browser',
                    help='Tells Django to open a browser.'),
        make_option('--adminmedia', dest='admin_media_path', default='',
                    help='Specifies the directory from which to serve admin media.'),
        make_option('--nothreading', action='store_false', dest='threaded',
                    help='Do not run in multithreaded mode.'),
        make_option('--threaded', action='store_true', dest='threaded',
                    help='Run in multithreaded mode.'),
        make_option('--output', dest='output_file', default=None,
                    help='Specifies an output file to send a copy of all messages (not flushed immediately).'),
        make_option('--print-sql', action='store_true', default=False,
                    help="Print SQL queries as they're executed"),
        make_option('--cert', dest='cert_path', action="store", type="string",
                    help='To use SSL, specify certificate path.'),
        make_option('--extra-files', dest='extra_files', action="store", type="string",
                    help='auto-reload whenever the given file changes too'),

    )
    if USE_STATICFILES:
        option_list += (
            make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
                        help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
            make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
                        help='Allows serving static files even if DEBUG is False.'),
        )
    help = "Starts a lightweight Web server for development."
    args = '[optional port number, or ipaddr:port]'

    # Validation is called explicitly each time the server is reloaded.
    requires_model_validation = False

    @signalcommand
    def handle(self, addrport='', *args, **options):
        import django

        # Do not use default ending='\n', because StreamHandler() takes care of it
        if hasattr(self.stderr, 'ending'):
            self.stderr.ending = None

        setup_logger(logger, self.stderr, filename=options.get('output_file', None))  # , fmt="[%(name)s] %(message)s")
        logredirect = RedirectHandler(__name__)

        # Redirect werkzeug log items
        werklogger = logging.getLogger('werkzeug')
        werklogger.setLevel(logging.INFO)
        werklogger.addHandler(logredirect)
        werklogger.propagate = False

        if options.get("print_sql", False):
            try:
                # Django 1.7 onwards
                from django.db.backends import utils
            except ImportError:
                # Django 1.6 below
                from django.db.backends import util as utils

            try:
                import sqlparse
            except ImportError:
                sqlparse = None  # noqa

            class PrintQueryWrapper(utils.CursorDebugWrapper):
                def execute(self, sql, params=()):
                    starttime = time.time()
                    try:
                        return self.cursor.execute(sql, params)
                    finally:
                        raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
                        execution_time = time.time() - starttime
                        therest = ' -- [Execution time: %.6fs] [Database: %s]' % (execution_time, self.db.alias)
                        if sqlparse:
                            logger.info(sqlparse.format(raw_sql, reindent=True) + therest)
                        else:
                            logger.info(raw_sql + therest)

            utils.CursorDebugWrapper = PrintQueryWrapper

        try:
            from django.core.servers.basehttp import AdminMediaHandler
            USE_ADMINMEDIAHANDLER = True
        except ImportError:
            USE_ADMINMEDIAHANDLER = False

        try:
            from django.core.servers.basehttp import get_internal_wsgi_application as WSGIHandler
        except ImportError:
            from django.core.handlers.wsgi import WSGIHandler  # noqa

        try:
            from werkzeug import run_simple, DebuggedApplication

            # Set colored output
            if settings.DEBUG:
                try:
                    set_werkzeug_log_color()
                except:     # We are dealing with some internals, anything could go wrong
                    print("Wrapping internal werkzeug logger for color highlighting has failed!")
                    pass

        except ImportError:
            raise CommandError("Werkzeug is required to use runserver_plus.  Please visit http://werkzeug.pocoo.org/ or install via pip. (pip install Werkzeug)")

        # usurp django's handler
        from django.views import debug
        debug.technical_500_response = null_technical_500_response

        self.use_ipv6 = options.get('use_ipv6')
        if self.use_ipv6 and not socket.has_ipv6:
            raise CommandError('Your Python does not support IPv6.')
        self._raw_ipv6 = False
        if not addrport:
            try:
                addrport = settings.RUNSERVERPLUS_SERVER_ADDRESS_PORT
            except AttributeError:
                pass
        if not addrport:
            self.addr = ''
            self.port = DEFAULT_PORT
        else:
            m = re.match(naiveip_re, addrport)
            if m is None:
                raise CommandError('"%s" is not a valid port number '
                                   'or address:port pair.' % addrport)
            self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
            if not self.port.isdigit():
                raise CommandError("%r is not a valid port number." %
                                   self.port)
            if self.addr:
                if _ipv6:
                    self.addr = self.addr[1:-1]
                    self.use_ipv6 = True
                    self._raw_ipv6 = True
                elif self.use_ipv6 and not _fqdn:
                    raise CommandError('"%s" is not a valid IPv6 address.'
                                       % self.addr)
        if not self.addr:
            self.addr = '::1' if self.use_ipv6 else '127.0.0.1'

        threaded = options.get('threaded', True)
        use_reloader = options.get('use_reloader', True)
        open_browser = options.get('open_browser', False)
        cert_path = options.get("cert_path")
        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
        bind_url = "http://%s:%s/" % (
            self.addr if not self._raw_ipv6 else '[%s]' % self.addr, self.port)
        extra_files = [options.get('extra_files')] if options.get('extra_files') else []

        def inner_run():
            print("Validating models...")
            self.validate(display_num_errors=True)
            print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE))
            print("Development server is running at %s" % (bind_url,))
            print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)")
            print("Quit the server with %s." % quit_command)
            path = options.get('admin_media_path', '')
            if not path:
                admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin')
                if os.path.isdir(admin_media_path):
                    path = admin_media_path
                else:
                    path = os.path.join(django.__path__[0], 'contrib/admin/media')
            handler = WSGIHandler()
            if USE_ADMINMEDIAHANDLER:
                handler = AdminMediaHandler(handler, path)
            if USE_STATICFILES:
                use_static_handler = options.get('use_static_handler', True)
                insecure_serving = options.get('insecure_serving', False)
                if use_static_handler and (settings.DEBUG or insecure_serving):
                    handler = StaticFilesHandler(handler)
            if open_browser:
                import webbrowser
                webbrowser.open(bind_url)
            if cert_path:
                """
                OpenSSL is needed for SSL support.

                This will make flakes8 throw warning since OpenSSL is not used
                directly, alas, this is the only way to show meaningful error
                messages. See:
                http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/
                for more information on python imports.
                """
                try:
                    import OpenSSL  # NOQA
                except ImportError:
                    raise CommandError("Python OpenSSL Library is "
                                       "required to use runserver_plus with ssl support. "
                                       "Install via pip (pip install pyOpenSSL).")

                dir_path, cert_file = os.path.split(cert_path)
                if not dir_path:
                    dir_path = os.getcwd()
                root, ext = os.path.splitext(cert_file)
                certfile = os.path.join(dir_path, root + ".crt")
                keyfile = os.path.join(dir_path, root + ".key")
                try:
                    from werkzeug.serving import make_ssl_devcert
                    if os.path.exists(certfile) and \
                            os.path.exists(keyfile):
                                ssl_context = (certfile, keyfile)
                    else:  # Create cert, key files ourselves.
                        ssl_context = make_ssl_devcert(
                            os.path.join(dir_path, root), host='localhost')
                except ImportError:
                    print("Werkzeug version is less than 0.9, trying adhoc certificate.")
                    ssl_context = "adhoc"

            else:
                ssl_context = None

            extra_files = []
            if use_reloader and settings.USE_I18N:
                try:
                    from django.utils.autoreload import gen_filenames
                except ImportError:
                    pass
                else:
                    extra_files = filter(lambda filename: filename.endswith('.mo'), gen_filenames())

            run_simple(
                self.addr,
                int(self.port),
                DebuggedApplication(handler, True),
                use_reloader=use_reloader,
                use_debugger=True,
                extra_files=extra_files,
                threaded=threaded,
                ssl_context=ssl_context,
                extra_files=extra_files
            )
        inner_run()

def set_werkzeug_log_color():
    """Try to set color to the werkzeug log.
    """
    from django.core.management.color import color_style
    from werkzeug.serving import WSGIRequestHandler
    from werkzeug._internal import _log

    _style = color_style()
    _orig_log = WSGIRequestHandler.log

    def werk_log(self, type, message, *args):
        try:
            msg = '%s - - [%s] %s' % (
                self.address_string(),
                self.log_date_time_string(),
                message % args,
            )
            http_code = str(args[1])
        except:
            return _orig_log(type, message, *args)

        # Utilize terminal colors, if available
        if http_code[0] == '2':
            # Put 2XX first, since it should be the common case
            msg = _style.HTTP_SUCCESS(msg)
        elif http_code[0] == '1':
            msg = _style.HTTP_INFO(msg)
        elif http_code == '304':
            msg = _style.HTTP_NOT_MODIFIED(msg)
        elif http_code[0] == '3':
            msg = _style.HTTP_REDIRECT(msg)
        elif http_code == '404':
            msg = _style.HTTP_NOT_FOUND(msg)
        elif http_code[0] == '4':
            msg = _style.HTTP_BAD_REQUEST(msg)
        else:
            # Any 5XX, or any other response
            msg = _style.HTTP_SERVER_ERROR(msg)

        _log(type, msg)

    WSGIRequestHandler.log = werk_log
<commit_msg>allow extra-file to be specified multiple times<commit_after>import os
import re
import sys
import time
import socket
import logging

from optparse import make_option

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django_extensions.management.utils import setup_logger, RedirectHandler,\
    signalcommand
from django_extensions.management.technical_response import null_technical_500_response

try:
    if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
        from django.contrib.staticfiles.handlers import StaticFilesHandler
        USE_STATICFILES = True
    elif 'staticfiles' in settings.INSTALLED_APPS:
        from staticfiles.handlers import StaticFilesHandler  # noqa
        USE_STATICFILES = True
    else:
        USE_STATICFILES = False
except ImportError:
    USE_STATICFILES = False

naiveip_re = re.compile(r"""^(?:
(?P<addr>
    (?P<ipv4>\d{1,3}(?:\.\d{1,3}){3}) |         # IPv4 address
    (?P<ipv6>\[[a-fA-F0-9:]+\]) |               # IPv6 address
    (?P<fqdn>[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*) # FQDN
):)?(?P<port>\d+)$""", re.X)
DEFAULT_PORT = "8000"

logger = logging.getLogger(__name__)

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--ipv6', '-6', action='store_true', dest='use_ipv6', default=False,
                    help='Tells Django to use a IPv6 address.'),
        make_option('--noreload', action='store_false', dest='use_reloader', default=True,
                    help='Tells Django to NOT use the auto-reloader.'),
        make_option('--browser', action='store_true', dest='open_browser',
                    help='Tells Django to open a browser.'),
        make_option('--adminmedia', dest='admin_media_path', default='',
                    help='Specifies the directory from which to serve admin media.'),
        make_option('--nothreading', action='store_false', dest='threaded',
                    help='Do not run in multithreaded mode.'),
        make_option('--threaded', action='store_true', dest='threaded',
                    help='Run in multithreaded mode.'),
        make_option('--output', dest='output_file', default=None,
                    help='Specifies an output file to send a copy of all messages (not flushed immediately).'),
        make_option('--print-sql', action='store_true', default=False,
                    help="Print SQL queries as they're executed"),
        make_option('--cert', dest='cert_path', action="store", type="string",
                    help='To use SSL, specify certificate path.'),
        make_option('--extra-file', dest='extra_files', action="append", type="string",
                    help='auto-reload whenever the given file changes too (can be specified multiple times)'),

    )
    if USE_STATICFILES:
        option_list += (
            make_option('--nostatic', action="store_false", dest='use_static_handler', default=True,
                        help='Tells Django to NOT automatically serve static files at STATIC_URL.'),
            make_option('--insecure', action="store_true", dest='insecure_serving', default=False,
                        help='Allows serving static files even if DEBUG is False.'),
        )
    help = "Starts a lightweight Web server for development."
    args = '[optional port number, or ipaddr:port]'

    # Validation is called explicitly each time the server is reloaded.
    requires_model_validation = False

    @signalcommand
    def handle(self, addrport='', *args, **options):
        import django

        # Do not use default ending='\n', because StreamHandler() takes care of it
        if hasattr(self.stderr, 'ending'):
            self.stderr.ending = None

        setup_logger(logger, self.stderr, filename=options.get('output_file', None))  # , fmt="[%(name)s] %(message)s")
        logredirect = RedirectHandler(__name__)

        # Redirect werkzeug log items
        werklogger = logging.getLogger('werkzeug')
        werklogger.setLevel(logging.INFO)
        werklogger.addHandler(logredirect)
        werklogger.propagate = False

        if options.get("print_sql", False):
            try:
                # Django 1.7 onwards
                from django.db.backends import utils
            except ImportError:
                # Django 1.6 below
                from django.db.backends import util as utils

            try:
                import sqlparse
            except ImportError:
                sqlparse = None  # noqa

            class PrintQueryWrapper(utils.CursorDebugWrapper):
                def execute(self, sql, params=()):
                    starttime = time.time()
                    try:
                        return self.cursor.execute(sql, params)
                    finally:
                        raw_sql = self.db.ops.last_executed_query(self.cursor, sql, params)
                        execution_time = time.time() - starttime
                        therest = ' -- [Execution time: %.6fs] [Database: %s]' % (execution_time, self.db.alias)
                        if sqlparse:
                            logger.info(sqlparse.format(raw_sql, reindent=True) + therest)
                        else:
                            logger.info(raw_sql + therest)

            utils.CursorDebugWrapper = PrintQueryWrapper

        try:
            from django.core.servers.basehttp import AdminMediaHandler
            USE_ADMINMEDIAHANDLER = True
        except ImportError:
            USE_ADMINMEDIAHANDLER = False

        try:
            from django.core.servers.basehttp import get_internal_wsgi_application as WSGIHandler
        except ImportError:
            from django.core.handlers.wsgi import WSGIHandler  # noqa

        try:
            from werkzeug import run_simple, DebuggedApplication

            # Set colored output
            if settings.DEBUG:
                try:
                    set_werkzeug_log_color()
                except:     # We are dealing with some internals, anything could go wrong
                    print("Wrapping internal werkzeug logger for color highlighting has failed!")
                    pass

        except ImportError:
            raise CommandError("Werkzeug is required to use runserver_plus.  Please visit http://werkzeug.pocoo.org/ or install via pip. (pip install Werkzeug)")

        # usurp django's handler
        from django.views import debug
        debug.technical_500_response = null_technical_500_response

        self.use_ipv6 = options.get('use_ipv6')
        if self.use_ipv6 and not socket.has_ipv6:
            raise CommandError('Your Python does not support IPv6.')
        self._raw_ipv6 = False
        if not addrport:
            try:
                addrport = settings.RUNSERVERPLUS_SERVER_ADDRESS_PORT
            except AttributeError:
                pass
        if not addrport:
            self.addr = ''
            self.port = DEFAULT_PORT
        else:
            m = re.match(naiveip_re, addrport)
            if m is None:
                raise CommandError('"%s" is not a valid port number '
                                   'or address:port pair.' % addrport)
            self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
            if not self.port.isdigit():
                raise CommandError("%r is not a valid port number." %
                                   self.port)
            if self.addr:
                if _ipv6:
                    self.addr = self.addr[1:-1]
                    self.use_ipv6 = True
                    self._raw_ipv6 = True
                elif self.use_ipv6 and not _fqdn:
                    raise CommandError('"%s" is not a valid IPv6 address.'
                                       % self.addr)
        if not self.addr:
            self.addr = '::1' if self.use_ipv6 else '127.0.0.1'

        threaded = options.get('threaded', True)
        use_reloader = options.get('use_reloader', True)
        open_browser = options.get('open_browser', False)
        cert_path = options.get("cert_path")
        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
        bind_url = "http://%s:%s/" % (
            self.addr if not self._raw_ipv6 else '[%s]' % self.addr, self.port)
        extra_files = options.get('extra_files', None) or []

        def inner_run():
            print("Validating models...")
            self.validate(display_num_errors=True)
            print("\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE))
            print("Development server is running at %s" % (bind_url,))
            print("Using the Werkzeug debugger (http://werkzeug.pocoo.org/)")
            print("Quit the server with %s." % quit_command)
            path = options.get('admin_media_path', '')
            if not path:
                admin_media_path = os.path.join(django.__path__[0], 'contrib/admin/static/admin')
                if os.path.isdir(admin_media_path):
                    path = admin_media_path
                else:
                    path = os.path.join(django.__path__[0], 'contrib/admin/media')
            handler = WSGIHandler()
            if USE_ADMINMEDIAHANDLER:
                handler = AdminMediaHandler(handler, path)
            if USE_STATICFILES:
                use_static_handler = options.get('use_static_handler', True)
                insecure_serving = options.get('insecure_serving', False)
                if use_static_handler and (settings.DEBUG or insecure_serving):
                    handler = StaticFilesHandler(handler)
            if open_browser:
                import webbrowser
                webbrowser.open(bind_url)
            if cert_path:
                """
                OpenSSL is needed for SSL support.

                This will make flakes8 throw warning since OpenSSL is not used
                directly, alas, this is the only way to show meaningful error
                messages. See:
                http://lucumr.pocoo.org/2011/9/21/python-import-blackbox/
                for more information on python imports.
                """
                try:
                    import OpenSSL  # NOQA
                except ImportError:
                    raise CommandError("Python OpenSSL Library is "
                                       "required to use runserver_plus with ssl support. "
                                       "Install via pip (pip install pyOpenSSL).")

                dir_path, cert_file = os.path.split(cert_path)
                if not dir_path:
                    dir_path = os.getcwd()
                root, ext = os.path.splitext(cert_file)
                certfile = os.path.join(dir_path, root + ".crt")
                keyfile = os.path.join(dir_path, root + ".key")
                try:
                    from werkzeug.serving import make_ssl_devcert
                    if os.path.exists(certfile) and \
                            os.path.exists(keyfile):
                                ssl_context = (certfile, keyfile)
                    else:  # Create cert, key files ourselves.
                        ssl_context = make_ssl_devcert(
                            os.path.join(dir_path, root), host='localhost')
                except ImportError:
                    print("Werkzeug version is less than 0.9, trying adhoc certificate.")
                    ssl_context = "adhoc"

            else:
                ssl_context = None

            if use_reloader and settings.USE_I18N:
                try:
                    from django.utils.autoreload import gen_filenames
                except ImportError:
                    pass
                else:
                    extra_files.extend(filter(lambda filename: filename.endswith('.mo'), gen_filenames()))

            run_simple(
                self.addr,
                int(self.port),
                DebuggedApplication(handler, True),
                use_reloader=use_reloader,
                use_debugger=True,
                extra_files=extra_files,
                threaded=threaded,
                ssl_context=ssl_context,
            )
        inner_run()

def set_werkzeug_log_color():
    """Try to set color to the werkzeug log.
    """
    from django.core.management.color import color_style
    from werkzeug.serving import WSGIRequestHandler
    from werkzeug._internal import _log

    _style = color_style()
    _orig_log = WSGIRequestHandler.log

    def werk_log(self, type, message, *args):
        try:
            msg = '%s - - [%s] %s' % (
                self.address_string(),
                self.log_date_time_string(),
                message % args,
            )
            http_code = str(args[1])
        except:
            return _orig_log(type, message, *args)

        # Utilize terminal colors, if available
        if http_code[0] == '2':
            # Put 2XX first, since it should be the common case
            msg = _style.HTTP_SUCCESS(msg)
        elif http_code[0] == '1':
            msg = _style.HTTP_INFO(msg)
        elif http_code == '304':
            msg = _style.HTTP_NOT_MODIFIED(msg)
        elif http_code[0] == '3':
            msg = _style.HTTP_REDIRECT(msg)
        elif http_code == '404':
            msg = _style.HTTP_NOT_FOUND(msg)
        elif http_code[0] == '4':
            msg = _style.HTTP_BAD_REQUEST(msg)
        else:
            # Any 5XX, or any other response
            msg = _style.HTTP_SERVER_ERROR(msg)

        _log(type, msg)

    WSGIRequestHandler.log = werk_log
<|endoftext|>


<commit_before>import sys
import os
import re

filename = 'vwoptimize.py'
__version__ = '0.4.0dev'
version_regex = r"__version__ = '([^']+)'"
versiondev_regex = r"(\.\d)dev(')"

def write(filename, data):
    fobj = open(filename, 'wb')
    fobj.write(data)
    fobj.flush()
    os.fsync(fobj.fileno())
    fobj.close()

def system(cmd):
    sys.stderr.write('+ %s\n' % cmd)
    if os.system(cmd) != 0:
        sys.exit('%r failed' % cmd)

git_describe = os.popen('git describe --tags --dirty').read().strip()
print 'Version from git: %r' % git_describe
version_from_git = git_describe.split('-')[0]

data = open(filename, 'rb').read()
header, rest = data[:200], data[200:]
version_from_source = re.findall(version_regex, header)
assert len(version_from_source) == 1, version_from_source
version_from_source = version_from_source[0]
print 'Version from source: %r' % version_from_source

assert 'dev' in version_from_source, version_from_source
version_from_source = version_from_source.replace('dev', '')

if version_from_git != version_from_source:
    sys.exit('Version in the source (%r) is not the same as from git tag (%r), perhaps you forgot "git tag"' %
             (version_from_source, version_from_git))

system("cp %s %s.backup" % (filename, filename))
header, count = re.subn(versiondev_regex, r"\1\2", header)
assert count == 1, (count, version_regex, header)
header, count = re.subn(re.escape("GIT"), git_describe, header)

write(filename, header + rest)
os.system('diff -U 1 %s.backup %s' % (filename, filename))

if 'dirty' in git_describe or 'upload' not in sys.argv:
    system("%s setup.py sdist" % sys.executable)
else:
    system("%s setup.py sdist upload" % sys.executable)

system("cp %s.backup %s" % (filename, filename))
<commit_msg>rm unused line<commit_after>import sys
import os
import re

filename = 'vwoptimize.py'
version_regex = r"__version__ = '([^']+)'"
versiondev_regex = r"(\.\d)dev(')"

def write(filename, data):
    fobj = open(filename, 'wb')
    fobj.write(data)
    fobj.flush()
    os.fsync(fobj.fileno())
    fobj.close()

def system(cmd):
    sys.stderr.write('+ %s\n' % cmd)
    if os.system(cmd) != 0:
        sys.exit('%r failed' % cmd)

git_describe = os.popen('git describe --tags --dirty').read().strip()
print 'Version from git: %r' % git_describe
version_from_git = git_describe.split('-')[0]

data = open(filename, 'rb').read()
header, rest = data[:200], data[200:]
version_from_source = re.findall(version_regex, header)
assert len(version_from_source) == 1, version_from_source
version_from_source = version_from_source[0]
print 'Version from source: %r' % version_from_source

assert 'dev' in version_from_source, version_from_source
version_from_source = version_from_source.replace('dev', '')

if version_from_git != version_from_source:
    sys.exit('Version in the source (%r) is not the same as from git tag (%r), perhaps you forgot "git tag"' %
             (version_from_source, version_from_git))

system("cp %s %s.backup" % (filename, filename))
header, count = re.subn(versiondev_regex, r"\1\2", header)
assert count == 1, (count, version_regex, header)
header, count = re.subn(re.escape("GIT"), git_describe, header)

write(filename, header + rest)
os.system('diff -U 1 %s.backup %s' % (filename, filename))

if 'dirty' in git_describe or 'upload' not in sys.argv:
    system("%s setup.py sdist" % sys.executable)
else:
    system("%s setup.py sdist upload" % sys.executable)

system("cp %s.backup %s" % (filename, filename))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on 18 juil. 2013
"""
from __future__ import unicode_literals
import logging
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
import sys
import os
from django.template import loader
from django.template.context import Context
import shutil
from pprint import pprint
import codecs
import itertools
import re
logger = logging.getLogger(__name__)

from optparse import make_option

class Command(BaseCommand):
    args = '<destination>'
    help = 'generate a ready-to-use nginx config for the current project'
    option_list = BaseCommand.option_list + (
        make_option('--socket',
            action='store',
            dest='socket',
            default="/var/run/django/{fqdn}.sock",
            help='the socket to use to contact gunicorn. can add {fqdn} to insert FQDN. default to /var/run/django/{fqdn}.sock'),
        make_option('--workon_home',
            action='store',
            dest='workon_home',
            default=None,
            help='the directory of the workon home (where is located all virtualenv)'),

        make_option('--forcesetting',
            action='append',
            dest='extra_settings',
            default=[],
            help='overide a value defaulted by the settings value. ie: --forcesetting=FQDN=myproj.exemple.com'),
        make_option('--type',
            action='store',
            dest='type',
            default="all",
            help='the type of files to create. on of [ nginx, systemd, init, all ]'),
        make_option('--no-buildout',
            action='store_true',
            dest='buildout',
            default=False,
            help="don't set path as if project was in a buildout cookpot"),
        make_option('--no-upload-progress',
            action='store_true',
            dest='no_upload',
            default=False,
            help="disable upload-progress directive for nginx if this one don't support upload-progress module"),
         make_option('--log-dir',
            action='store',
            dest='log-dir',
            default=None,
            help="l'emplacement des fichiers log a génére [/var/log/nginx/{FQDN}/]"),
        )  #

    taken_from_settings = (
                           # (settings name, default value),
                           ("ADMINISTRATOR_IP", "127.0.0.1"),
                           ("FQDN"),
                           ("SITE_NAME"),
                           ("DOMAIN_NAME"),
                           ("ADMINISTRATOR_IP"),
                           ("DJANGO_ROOT"),
                           ("SECURE_PREFIX"),
                           ("STATIC_ROOT"),
                           ("MEDIA_ROOT"),
                           )

    template_files = dict(

                      nginx=["sub.domain.ext",
                      "sub.domain.ext.d/dynamic.conf",
                      "sub.domain.ext.d/static.conf",
                      "sub.domain.ext.d/static_location.conf",
                      "sub.domain.ext.d/gunicorn.conf",
                      "sub.domain.ext.d/extra/di.maintenance.conf",
                      "sub.domain.ext.d/extra/di.xsendfile.conf",
                      "sub.domain.ext.d/switch_maintenance.sh"],
                      init=["django_sub.domain.ext"],
                      systemd=["sub.domain.ext.service",
                      "sub.domain.ext.socket"],

                      )

    def handle(self, *args, **options):
        if len(args) > 0:
            dest = args[0]
        else:
            dest = "nginx_conf"
        socket = options["socket"]
        buildout = not options["buildout"]
        workon_home = options["workon_home"]
        if workon_home is None and not buildout:
            try:
                workon_home = os.environ["WORKON_HOME"]
                self.stderr.write("guesing workon home with environ : %s" % workon_home)
            except:
                raise CommandError("impossible to get workon_home. pleas set an environement or with --workon_home")

        # constructing overriden settings
        extra_settings = {}
        for opt_settings in options["extra_settings"]:
            splited = opt_settings.split("=")
            extra_settings[splited[0]] = "=".join(splited[1:])
        regexp_external_url = re.compile("(https?)?://(?P<domain>[^/]+)/")
        match_static = regexp_external_url.match(settings.STATIC_URL)
        match_media = regexp_external_url.match(settings.MEDIA_URL)
        print settings.STATIC_URL
        print settings.MEDIA_URL
        if (match_static is not None) != (match_media is not None):
            raise CommandError("impossible de détérminer le domaine pour les fichiers static. votre MEDIA_URL et STATIC_URL ne concorde pas")
        if match_static is not None:
            if match_static.groupdict()["domain"] != match_media.groupdict()["domain"]:
                raise CommandError("impossible de détérminer le domaine pour les fichiers static. votre MEDIA_URL et STATIC_URL ne concorde pas")
            static_domain = match_media.groupdict()["domain"]
        else:
            static_domain = False

        context = {
            "buildout": buildout,
            "WORKON_HOME": workon_home,
            "ROOT_NGINX_PATH": os.path.abspath(dest),
            "static_domain": static_domain
        }

        errors = False
        for res in self.taken_from_settings:
            if isinstance(res, (tuple, list)):
                if extra_settings.has_key(res[0]):
                    context[res[0]] = extra_settings[res[0]]
                    continue
                try:
                    settingsname, default = res
                    context[settingsname] = getattr(settings, settingsname, default)
                except ValueError:
                    # dont have default value
                    try:
                        context[settingsname] = getattr(settings, settingsname)
                    except AttributeError:
                        errors = True
                        self.stderr.write('setting {0} absent from settings. try to ovenride it with --setting={0}=FOO'.format(settingsname))

            else:
                if extra_settings.has_key(res):
                    context[res] = extra_settings[res]
                    continue
                try:
                    settingsname = res
                    context[settingsname] = getattr(settings, settingsname)
                except AttributeError:
                    errors = True
                    self.stderr.write('setting {0} absent from settings. try to ovenride it with --setting={0}=FOO'.format(settingsname))

        if errors:
            raise CommandError("dont continu because of previous settings missing")

        socket = socket.format(fqdn=context["FQDN"])
        context["socket_path"] = socket
        if socket.startswith("/"):
            context["socket"] = "unix:%s" % socket

        else:
            context["socket"] = socket
        context["NGINX_LOG_DIR"] = "/var/log/nginx/{fqdn}/".format(fqdn=context["FQDN"])
        if options["log-dir"]:
            context["NGINX_LOG_DIR"] = options["log-dir"]
        context["upload"] = not options["no_upload"]

        self.stdout.write("context variable used :")
        pprint(context)

        tmpl_context = Context(context)

        #  create arbo
        try:
            os.chdir(dest)
        except:
            os.makedirs(dest)
            os.chdir(dest)

        try:
            os.mkdir("%s.d" % context["FQDN"])
        except OSError:
            pass
        try:
            os.mkdir(os.path.join("%s.d" % context["FQDN"], "extra"))
        except OSError:
            pass

        files = []
        if options["type"] == "all":
            files = itertools.chain(*self.template_files.values())
        else:
            try:
                files = self.template_files[options["type"]]
            except KeyError:
                raise CommandError("ce type de fichie n'existe pas : %s n'est pas parmis %s" % (options["type"], self.template_files.keys()))
        for template_file in files:
            with codecs.open(template_file.replace("sub.domain.ext", context["FQDN"]), "w", encoding="utf-8") as output:
                template = loader.get_template("django_nginx/%s" % template_file)
                print("writing %s" % os.path.join(dest, output.name))
                output.write(template.render(tmpl_context))

        self.stderr.write("done")

<commit_msg>corrected regexp to detect domain name<commit_after># -*- coding: utf-8 -*-
"""
Created on 18 juil. 2013
"""
from __future__ import unicode_literals
import logging
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
import sys
import os
from django.template import loader
from django.template.context import Context
import shutil
from pprint import pprint
import codecs
import itertools
import re
logger = logging.getLogger(__name__)

from optparse import make_option

class Command(BaseCommand):
    args = '<destination>'
    help = 'generate a ready-to-use nginx config for the current project'
    option_list = BaseCommand.option_list + (
        make_option('--socket',
            action='store',
            dest='socket',
            default="/var/run/django/{fqdn}.sock",
            help='the socket to use to contact gunicorn. can add {fqdn} to insert FQDN. default to /var/run/django/{fqdn}.sock'),
        make_option('--workon_home',
            action='store',
            dest='workon_home',
            default=None,
            help='the directory of the workon home (where is located all virtualenv)'),

        make_option('--forcesetting',
            action='append',
            dest='extra_settings',
            default=[],
            help='overide a value defaulted by the settings value. ie: --forcesetting=FQDN=myproj.exemple.com'),
        make_option('--type',
            action='store',
            dest='type',
            default="all",
            help='the type of files to create. on of [ nginx, systemd, init, all ]'),
        make_option('--no-buildout',
            action='store_true',
            dest='buildout',
            default=False,
            help="don't set path as if project was in a buildout cookpot"),
        make_option('--no-upload-progress',
            action='store_true',
            dest='no_upload',
            default=False,
            help="disable upload-progress directive for nginx if this one don't support upload-progress module"),
         make_option('--log-dir',
            action='store',
            dest='log-dir',
            default=None,
            help="l'emplacement des fichiers log a génére [/var/log/nginx/{FQDN}/]"),
        )  #

    taken_from_settings = (
                           # (settings name, default value),
                           ("ADMINISTRATOR_IP", "127.0.0.1"),
                           ("FQDN"),
                           ("SITE_NAME"),
                           ("DOMAIN_NAME"),
                           ("ADMINISTRATOR_IP"),
                           ("DJANGO_ROOT"),
                           ("SECURE_PREFIX"),
                           ("STATIC_ROOT"),
                           ("MEDIA_ROOT"),
                           )

    template_files = dict(

                      nginx=["sub.domain.ext",
                      "sub.domain.ext.d/dynamic.conf",
                      "sub.domain.ext.d/static.conf",
                      "sub.domain.ext.d/static_location.conf",
                      "sub.domain.ext.d/gunicorn.conf",
                      "sub.domain.ext.d/extra/di.maintenance.conf",
                      "sub.domain.ext.d/extra/di.xsendfile.conf",
                      "sub.domain.ext.d/switch_maintenance.sh"],
                      init=["django_sub.domain.ext"],
                      systemd=["sub.domain.ext.service",
                      "sub.domain.ext.socket"],

                      )

    def handle(self, *args, **options):
        if len(args) > 0:
            dest = args[0]
        else:
            dest = "nginx_conf"
        socket = options["socket"]
        buildout = not options["buildout"]
        workon_home = options["workon_home"]
        if workon_home is None and not buildout:
            try:
                workon_home = os.environ["WORKON_HOME"]
                self.stderr.write("guesing workon home with environ : %s" % workon_home)
            except:
                raise CommandError("impossible to get workon_home. pleas set an environement or with --workon_home")

        # constructing overriden settings
        extra_settings = {}
        for opt_settings in options["extra_settings"]:
            splited = opt_settings.split("=")
            extra_settings[splited[0]] = "=".join(splited[1:])
        regexp_external_url = re.compile("(https?:)?//(?P<domain>[^/]+)/")
        match_static = regexp_external_url.match(settings.STATIC_URL)
        match_media = regexp_external_url.match(settings.MEDIA_URL)

        if (match_static is not None) != (match_media is not None):
            raise CommandError("impossible de détérminer le domaine pour les fichiers static. votre MEDIA_URL et STATIC_URL ne concorde pas")
        if match_static is not None:
            if match_static.groupdict()["domain"] != match_media.groupdict()["domain"]:
                raise CommandError("impossible de détérminer le domaine pour les fichiers static. votre MEDIA_URL et STATIC_URL ne concorde pas")
            static_domain = match_media.groupdict()["domain"]
        else:
            static_domain = False

        context = {
            "buildout": buildout,
            "WORKON_HOME": workon_home,
            "ROOT_NGINX_PATH": os.path.abspath(dest),
            "static_domain": static_domain
        }

        errors = False
        for res in self.taken_from_settings:
            if isinstance(res, (tuple, list)):
                if extra_settings.has_key(res[0]):
                    context[res[0]] = extra_settings[res[0]]
                    continue
                try:
                    settingsname, default = res
                    context[settingsname] = getattr(settings, settingsname, default)
                except ValueError:
                    # dont have default value
                    try:
                        context[settingsname] = getattr(settings, settingsname)
                    except AttributeError:
                        errors = True
                        self.stderr.write('setting {0} absent from settings. try to ovenride it with --setting={0}=FOO'.format(settingsname))

            else:
                if extra_settings.has_key(res):
                    context[res] = extra_settings[res]
                    continue
                try:
                    settingsname = res
                    context[settingsname] = getattr(settings, settingsname)
                except AttributeError:
                    errors = True
                    self.stderr.write('setting {0} absent from settings. try to ovenride it with --setting={0}=FOO'.format(settingsname))

        if errors:
            raise CommandError("dont continu because of previous settings missing")

        socket = socket.format(fqdn=context["FQDN"])
        context["socket_path"] = socket
        if socket.startswith("/"):
            context["socket"] = "unix:%s" % socket

        else:
            context["socket"] = socket
        context["NGINX_LOG_DIR"] = "/var/log/nginx/{fqdn}/".format(fqdn=context["FQDN"])
        if options["log-dir"]:
            context["NGINX_LOG_DIR"] = options["log-dir"]
        context["upload"] = not options["no_upload"]

        self.stdout.write("context variable used :")
        pprint(context)

        tmpl_context = Context(context)

        #  create arbo
        try:
            os.chdir(dest)
        except:
            os.makedirs(dest)
            os.chdir(dest)

        try:
            os.mkdir("%s.d" % context["FQDN"])
        except OSError:
            pass
        try:
            os.mkdir(os.path.join("%s.d" % context["FQDN"], "extra"))
        except OSError:
            pass

        files = []
        if options["type"] == "all":
            files = itertools.chain(*self.template_files.values())
        else:
            try:
                files = self.template_files[options["type"]]
            except KeyError:
                raise CommandError("ce type de fichie n'existe pas : %s n'est pas parmis %s" % (options["type"], self.template_files.keys()))
        for template_file in files:
            with codecs.open(template_file.replace("sub.domain.ext", context["FQDN"]), "w", encoding="utf-8") as output:
                template = loader.get_template("django_nginx/%s" % template_file)
                print("writing %s" % os.path.join(dest, output.name))
                output.write(template.render(tmpl_context))

        self.stderr.write("done")

<|endoftext|>


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

# Copyright 2014,  Digital Reasoning
#
# 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.
#

__version_info__ = (0, 7, 0, 'dev', 0)

def get_version(version=None):
    """
    Returns a PEP 386-compliant version number from VERSION.

    Created by modifying django.utils.version.get_version
    """

    # Now build the two parts of the version number:
    # major = X.Y[.Z]
    # sub = .devN - for pre-alpha releases
    #     | {a|b|c}N - for alpha, beta and rc releases

    major = get_major_version(version)

    sub = ''

    if version[3] != 'final':
        if version[3] == 'dev':
            sub = 'dev'
        else:
            sub = version[3] + str(version[4])

    return '%s-%s' % (major, sub)

def get_major_version(version):
    """
    Returns major version from VERSION.
    """
    major = '.'.join(str(x) for x in version[:3])
    return major

__version__ = get_version(__version_info__)
<commit_msg>Fixed issue where final versions had a dash at the end<commit_after># -*- coding: utf-8 -*-

# Copyright 2014,  Digital Reasoning
#
# 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.
#

__version_info__ = (0, 7, 0, 'dev', 0)

def get_version(version=None):
    """
    Returns a PEP 386-compliant version number from VERSION.

    Created by modifying django.utils.version.get_version
    """

    # Now build the two parts of the version number:
    # major = X.Y[.Z]
    # sub = .devN - for pre-alpha releases
    #     | {a|b|c}N - for alpha, beta and rc releases

    major = get_major_version(version)

    sub = ''

    if version[3] != 'final':
        if version[3] == 'dev':
            sub = 'dev'
        else:
            sub = version[3] + str(version[4])

    return '%s-%s' % (major, sub) if sub else major

def get_major_version(version):
    """
    Returns major version from VERSION.
    """
    major = '.'.join(str(x) for x in version[:3])
    return major

__version__ = get_version(__version_info__)
<|endoftext|>


<commit_before>from bs4 import BeautifulSoup

from gouda.utils.web import get_page_contents

commands = ['cotd', 'cotdinfo']

def cotd(*args, **kwargs):
    writer = kwargs.pop('writer')
    data = get_page_contents("http://www.cheese.com/")
    try:
        soup = BeautifulSoup(data, "html.parser")
        cotd = soup.find('div', attrs={'class': 'top-offer'}).a.get('href').replace('/', '').replace('-', ' ').title()
        if kwargs.get('suppress', None):
            # return the cotd rather that writing it
            return cotd
        writer("The cheese of the day is %s." % cotd)
    except AttributeError:
        # no cheesy
        pass

def cotdinfo(*args, **kwargs):
    writer = kwargs.pop('writer')
    cheese = cotd(suppress=True, writer=None)
    data = get_page_contents("http://www.cheese.com/%s/" % cheese)
    try:
        soup = BeautifulSoup(data, "html.parser")
        info = soup.find('div', attrs={'class': 'summary'}).find('p', attrs={'style': 'text-align: justify;'}).text
        writer('. '.join(info.split('. ')[:2]))
    except:
        pass
<commit_msg>cotd formatting should be explcit only<commit_after>from bs4 import BeautifulSoup

from gouda.utils.web import get_page_contents

commands = ['cotd', 'cotdinfo']

def cotd(*args, **kwargs):
    writer = kwargs.pop('writer')
    data = get_page_contents("http://www.cheese.com/")
    try:
        soup = BeautifulSoup(data, "html.parser")
        cotd = soup.find('div', attrs={'class': 'top-offer'}).a.get('href').replace('/', '')
        if kwargs.get('suppress', None):
            # return the cotd rather that writing it
            return cotd
        writer("The cheese of the day is %s." % cotd.replace('-', ' ').title())
    except AttributeError:
        # no cheesy
        pass

def cotdinfo(*args, **kwargs):
    writer = kwargs.pop('writer')
    cheese = cotd(suppress=True, writer=None)
    data = get_page_contents("http://www.cheese.com/%s/" % cheese)
    try:
        soup = BeautifulSoup(data, "html.parser")
        info = soup.find('div', attrs={'class': 'summary'}).find('p', attrs={'style': 'text-align: justify;'}).text
        writer('. '.join(info.split('. ')[:2]))
    except:
        pass
<|endoftext|>