# Path: crosscompute/constants.py
# FUNCTION_BY_NAME = {
#     'slug': format_slug,
#     'title': str.title,
# }
# 
# MAXIMUM_FILE_CACHE_LENGTH = 256
# 
# VARIABLE_ID_PATTERN = re.compile(r'{\s*([^}]+?)\s*}')
# 
# Path: crosscompute/exceptions.py
# class CrossComputeConfigurationError(CrossComputeError):
#     pass
# 
# class CrossComputeDataError(CrossComputeError):
#     pass
# 
# Path: crosscompute/macros/disk.py
# class FileCache(LRUDict):
# 
#     def __init__(self, *args, load_file_data, maximum_length: int, **kwargs):
#         super().__init__(*args, maximum_length=maximum_length, **kwargs)
#         self._load_file_data = load_file_data
# 
#     def __getitem__(self, path):
#         if path in self:
#             file_time, file_data = super().__getitem__(path)
#             if getmtime(path) == file_time:
#                 return file_data
#         file_data = self._load_file_data(path)
#         self.__setitem__(path, file_data)
#         return file_data
# 
#     def __setitem__(self, path, data):
#         value = (getmtime(path), data)
#         super().__setitem__(path, value)
# 
# Path: crosscompute/macros/package.py
# def import_attribute(attribute_string):
#     module_string, attribute_name = attribute_string.rsplit('.', maxsplit=1)
#     return getattr(import_module(module_string), attribute_name)
# 
# Path: crosscompute/macros/web.py
# def get_html_from_markdown(text):
#     html = markdown(text)
#     if '</p>\n<p>' not in html:
#         html = html.removeprefix('<p>')
#         html = html.removesuffix('</p>')
#     return html
# 
# Path: crosscompute/routines/interface.py
# class Batch(ABC):
# 
#     def get_data(self, variable_definition):
#         '''
#         Get the data of the variable in one of the following formats:
#         {}
#         {'value': 1}
#         {'path': '/a/b/c.png'}
#         {'uri': 'upload:xyz'}
#         {'error': 'message'}
#         '''
#         return {}
# 
#     def get_data_uri(self, variable_definition):
#         'Get the resolved variable data uri'
#         return ''
# 
#     def get_data_configuration(self, variable_definition):
#         'Get the resolved variable configuration'
#         return {}


import csv
import json
import shutil
from dataclasses import dataclass
from importlib_metadata import entry_points
from invisibleroads_macros_log import format_path
from logging import getLogger
from os.path import basename, exists
from string import Template
from ..constants import (
    FUNCTION_BY_NAME,
    MAXIMUM_FILE_CACHE_LENGTH,
    VARIABLE_ID_PATTERN)
from ..exceptions import (
    CrossComputeConfigurationError,
    CrossComputeDataError)
from ..macros.disk import FileCache
from ..macros.package import import_attribute
from ..macros.web import get_html_from_markdown
from .interface import Batch

class LinkView(VariableView):

    view_name = 'link'

    def render_output(self, b: Batch, x: Element):
        variable_definition = self.variable_definition
        data_uri = b.get_data_uri(variable_definition, x)
        c = b.get_variable_configuration(variable_definition)
        name = c.get('name', basename(self.variable_path))
        text = c.get('text', name)
        body_text = (
            f'<a id="{x.id}" href="{data_uri}" '
            f'class="{self.mode_name} {self.view_name} {self.variable_id}" '
            f'download="{name}">'
            f'{text}</a>')
        return {
            'css_uris': [],
            'js_uris': [],
            'body_text': body_text,
            'js_texts': [],
        }


class StringView(VariableView):

    view_name = 'string'
    input_type = 'text'
    function_by_name = FUNCTION_BY_NAME


# Path: crosscompute/constants.py
# FUNCTION_BY_NAME = {
#     'slug': format_slug,
#     'title': str.title,
# }
# 
# MAXIMUM_FILE_CACHE_LENGTH = 256
# 
# VARIABLE_ID_PATTERN = re.compile(r'{\s*([^}]+?)\s*}')
# 
# Path: crosscompute/exceptions.py
# class CrossComputeConfigurationError(CrossComputeError):
#     pass
# 
# class CrossComputeDataError(CrossComputeError):
#     pass
# 
# Path: crosscompute/macros/disk.py
# class FileCache(LRUDict):
# 
#     def __init__(self, *args, load_file_data, maximum_length: int, **kwargs):
#         super().__init__(*args, maximum_length=maximum_length, **kwargs)
#         self._load_file_data = load_file_data
# 
#     def __getitem__(self, path):
#         if path in self:
#             file_time, file_data = super().__getitem__(path)
#             if getmtime(path) == file_time:
#                 return file_data
#         file_data = self._load_file_data(path)
#         self.__setitem__(path, file_data)
#         return file_data
# 
#     def __setitem__(self, path, data):
#         value = (getmtime(path), data)
#         super().__setitem__(path, value)
# 
# Path: crosscompute/macros/package.py
# def import_attribute(attribute_string):
#     module_string, attribute_name = attribute_string.rsplit('.', maxsplit=1)
#     return getattr(import_module(module_string), attribute_name)
# 
# Path: crosscompute/macros/web.py
# def get_html_from_markdown(text):
#     html = markdown(text)
#     if '</p>\n<p>' not in html:
#         html = html.removeprefix('<p>')
#         html = html.removesuffix('</p>')
#     return html
# 
# Path: crosscompute/routines/interface.py
# class Batch(ABC):
# 
#     def get_data(self, variable_definition):
#         '''
#         Get the data of the variable in one of the following formats:
#         {}
#         {'value': 1}
#         {'path': '/a/b/c.png'}
#         {'uri': 'upload:xyz'}
#         {'error': 'message'}
#         '''
#         return {}
# 
#     def get_data_uri(self, variable_definition):
#         'Get the resolved variable data uri'
#         return ''
# 
#     def get_data_configuration(self, variable_definition):
#         'Get the resolved variable configuration'
#         return {}


# Path: crosscompute/routines/variable.py
import csv
import json
import shutil
from dataclasses import dataclass
from importlib_metadata import entry_points
from invisibleroads_macros_log import format_path
from logging import getLogger
from os.path import basename, exists
from string import Template
from ..constants import (
    FUNCTION_BY_NAME,
    MAXIMUM_FILE_CACHE_LENGTH,
    VARIABLE_ID_PATTERN)
from ..exceptions import (
    CrossComputeConfigurationError,
    CrossComputeDataError)
from ..macros.disk import FileCache
from ..macros.package import import_attribute
from ..macros.web import get_html_from_markdown
from .interface import Batch


class LinkView(VariableView):

    view_name = 'link'

    def render_output(self, b: Batch, x: Element):
        variable_definition = self.variable_definition
        data_uri = b.get_data_uri(variable_definition, x)
        c = b.get_variable_configuration(variable_definition)
        name = c.get('name', basename(self.variable_path))
        text = c.get('text', name)
        body_text = (
            f'<a id="{x.id}" href="{data_uri}" '
            f'class="{self.mode_name} {self.view_name} {self.variable_id}" '
            f'download="{name}">'
            f'{text}</a>')
        return {
            'css_uris': [],
            'js_uris': [],
            'body_text': body_text,
            'js_texts': [],
        }


class StringView(VariableView):

    view_name = 'string'
    input_type = 'text'
    function_by_name = FUNCTION_BY_NAME


    def get_value(self, b: Batch):