Beispiel #1
0
def get_client():
    """
    Caches a client instance for default parameters set in `presalytics.CONFIG`.

    DO NOT use in server-side operation
    """
    client = presalytics.Client()
    return client
Beispiel #2
0
 def map_color_type(
         self,
         color_map_name: str,
         color_map: typing.Dict,
         theme_colors: typing.Dict,
         color_types_list=None) -> str:
     if not color_types_list:
         client = presalytics.Client(**self.client_kwargs)
         color_types_list = client.ooxml_automation.shared_colortypes_get()
     color_id = color_map[color_map_name]
     color_name = next(x.name for x in color_types_list if x.type_id == color_id)
     key = color_name[0].lower() + color_name[1:]
     color = theme_colors.get(key, None)
     return color
def create_story_from_ooxml_file(filename: str, client_info={}) -> 'Story':
    """
    Utility Method for building stories into the Presalytics API directly from a Presentation or Spreadsheet file. 

    Parameters
    ----------
    filename : str
        A string contain the local path to a presenation or spreadsheet object.

    client_info : dict, optional
        A dictionary containing arguments that will be unpacked and passed to a `presalytics.client.api.Client` object on intialization. 
        This dictionary can include the `token`, `cache_tokens`, `delegate_login` values.  See `presalytics.client.api.Client` for more information.

    Returns:
    ----------
    A `presalytics.client.presalytics_story.models.story.Story` containing information about the Story object in the Presalytics API

    """

    story: 'Story'
    logger.info("Starting presalytics tool: create_story_from_ooxml_file")
    logger.info("Intializing presalytics client.")

    client = presalytics.Client(**client_info)
    logger.info(
        "Sending file to presalytics server for document processing and base story creation"
    )
    story = client.story.story_post_file(file=filename)
    logger.info("Creating local instances of file widgets")
    outline = presalytics.StoryOutline.load(story.outline)
    for i in range(0, len(outline.pages)):
        page = outline.pages[i]
        for j in range(0, len(page.widgets)):
            widget = page.widgets[j]
            logger.info('Creating OoxmlFileWidget with name "{}"'.format(
                widget.name))
            inst = presalytics.OoxmlFileWidget.deserialize(
                widget, **client_info)
            presalytics.COMPONENTS.register(inst)
            logger.info('Rewriting outline with widget: "{}"'.format(
                widget.name))
            outline.pages[i].widgets[j] = inst.serialize()
    story.outline = outline.dump()
    return story
def create_theme_from_ooxml_document(document_id: str, client_info={}):
    """
    Creates a `presalytics.story.outline.Theme` object from an Presalytics API ooxml_document object

    Parameters:
    ----------
    document_id: str
        A string containing a uuid that corresponds to a document in the Ooxml Automation service of the Presalytics API

    client_info : dict, optional
        A dictionary containing arguments that will be unpacked and passed to a `presalytics.client.api.Client` object on intialization. 
        This dictionary can include the `token`, `cache_tokens`, `delegate_login` values.  See `presalytics.client.api.Client` for more information.

    Returns:
    ----------
    A `presalytics.story.outline.Theme` object with formats extracted from the ooxml_document

    """
    client = presalytics.Client(**client_info)
    child_objects = client.ooxml_automation.documents_childobjects_get_id(
        document_id)
    themes = [x for x in child_objects if x.object_type == "Theme.Themes"]
    if len(themes) > 1:
        slide_no = None
        for theme_data in themes:
            theme_info = client.ooxml_automation.theme_themes_get_id(
                theme_data.entity_id)
            parent_slide = client.ooxml_automation.slides_slides_get_id(
                theme_info.slide_id)
            if slide_no is None:
                slide_no = parent_slide.number
                theme_meta = theme_info
            else:
                if parent_slide.number < slide_no:
                    theme_meta = theme_info
                    if parent_slide.number == 0:
                        break
    else:
        theme_meta = client.ooxml_automation.theme_themes_get_id(
            themes[0].entity_id)
    theme = presalytics.lib.themes.ooxml.OoxmlTheme(theme_meta.name,
                                                    theme_meta.id,
                                                    client_info=client_info)
    return theme.serialize().to_dict()
Beispiel #5
0
    def get_configuration(self):
        client = presalytics.Client(**self.client_kwargs)
        theme = client.ooxml_automation.theme_themes_details_get_id(self.ooxml_id)
        extra_params = ['dateCreated', 'dateModified', 'userCreated', 'userModified', 'id', 'themeId']
        colors = {k: v for k, v in theme.colors.to_dict().items() if k not in extra_params}
        fonts = {k: v for k, v in theme.fonts.to_dict().items() if k not in extra_params}
        slide_details = client.ooxml_automation.slides_slides_details_get_id(theme.slide_id)
        color_map_dict = slide_details.slide_master.to_dict()["color_map"]
        color_types = client.ooxml_automation.shared_colortypes_get()

        mapped_colors = {
            "background1": self.map_color_type("background1", color_map_dict, colors, color_types),
            "background2": self.map_color_type("background2", color_map_dict, colors, color_types),
            "text1": self.map_color_type("text1", color_map_dict, colors, color_types),
            "text2": self.map_color_type("text2", color_map_dict, colors, color_types)
        }
        color_params = {k: v for k, v in colors.items() if k not in extra_params}
        color_params.update(mapped_colors)
        params = {k: v for k, v in color_params.items() if k not in extra_params}
        params.update(fonts)

        self.plugin_config = params
Beispiel #6
0
                'fit_quality': fit_quality
            },
        },
        {
            'name': 'ChangeShapeColor',
            'function_params': {
                'hex_color': header_color,
                'object_name':
                "header"  # From the 'Selection Pane' of PowerPoint
            }
        }
    ]
}
# Collect data from the Presaltyics API so the OoxmlEditorWidget can dynamically update
story_id = presalytics.StoryOutline.import_yaml("story.yaml").story_id
client = presalytics.Client()
story = client.story.story_id_get(story_id, include_relationships=True)
document_ooxml_id = story.ooxml_documents[0].ooxml_automation_id
document_tree = client.ooxml_automation.documents_childobjects_get_id(
    document_ooxml_id)

takeaway_box_id = next(o.entity_id for o in document_tree
                       if o.entity_name == "TakeawayBox")

# Section 3: Creating a Widget instance
# Create an instance of `OoxmlEditorWidget` that will update each time the outline is pushed
template_widget = presalytics.OoxmlEditorWidget(
    "Takeaways Box",
    story_id,
    takeaway_box_id,
    presalytics.OoxmlEndpointMap.group(),
def create_pages_from_ooxml_document(story: 'Story',
                                     ooxml_document: 'Document',
                                     client_info={}):
    """
    Utility Method for building stories into the Presalytics API directly from a Presenation or Spreadsheet file. 

    Parameters
    ----------
    story : presalytics.client.presalytics_story.models.story.Story
        Representation for Presalytics API Story object

    ooxml_document: presalytics.client.presalytics_ooxml_automation.models.document.Document
        The ooxml_document on the story that you want to create pages from

    client_info : dict, optional
        A dictionary containing arguments that will be unpacked and passed to a `presalytics.client.api.Client` object on intialization. 
        This dictionary can include the `token`, `cache_tokens`, `delegate_login` values.  See `presalytics.client.api.Client` for more information.

    Returns:
    ----------
    A list of `presalytics.story.outline.Page` objects representing the slides, pages and charts in the source document

    """
    pages = []
    order = []
    pages_unordered = []
    client = presalytics.Client(**client_info)
    child_objects = client.ooxml_automation.documents_childobjects_get_id(
        ooxml_document.id)
    document_type = client.ooxml_automation.documents_documenttype_typeid_get_type_id(
        ooxml_document.document_type_id)
    if document_type.file_extension == "pptx":
        slides_meta = [
            x for x in child_objects if x.object_type == "Slide.Slides"
        ]
        ep_map = presalytics.OoxmlEndpointMap.slide()
        for slide in slides_meta:
            try:
                widget = presalytics.OoxmlFileWidget(
                    filename=ooxml_document.filename,
                    name=slide.entity_name,
                    endpoint_map=ep_map,
                    object_name=slide.entity_name,
                    object_ooxml_id=slide.entity_id,
                    document_ooxml_id=ooxml_document.id,
                    story_id=story.id,
                    client_info=client_info)
                widget_kind = "widget-page"
                widget_name = slide.entity_name
                widgets = [widget.serialize()]
                page = presalytics.story.outline.Page(kind=widget_kind,
                                                      name=widget_name,
                                                      widgets=widgets)

                this_slide_meta = client.ooxml_automation.slides_slides_get_id(
                    slide.entity_id)
                order.append(this_slide_meta.number - 1)
                pages_unordered.append(page)
            except:
                logger.error(
                    "Unable to add widget {0} to outline ooxml document {1}".
                    format(slide.entity_name, ooxml_document.id))
    # TODO: insert excel chart handling here
    for j in range(0, len(order)):
        idx = order.index(j)
        pages.append(pages_unordered[idx])
    return pages