Nathan Clack
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # (DRAFT) Design: File-type resolution in Napari Nathan Clack, Eric Perlman, 2021 Sep 3 ## Problem Napari has a pluggable interface for accessing persisted data. Data are stored in different file formats; one or more plugins may provide the ability to interact with these formats. This document concerns how Napari resolves which plugin to invoke when given a filename or uniform resource identifier (URI). Specifically, Readers will be considered. There are two main sub-problems: 1. Determining the filetype given a string. 2. Determining which plugin to invoke given the filetype. #### Concerns 1. Access to the resource via local or remote storage may be missing, limited, or slow. 2. The number of plugins under consideration may be large. 3. The number of file types under consideration may be large. 4. New file types and plugins may be added dynamically. 5. Users may prefer to use one plugin over another for a particular file type. 6. Users may have unique preferences about how file names or URI's are mapped to file type. 7. Sometimes file type can not be determined based on file name or URI alone. 8. The plugin interface is changing soon to support a way of declaring capabilities. #### Requirements 1. The problem should be solvable in constant time with respect to the number of plugins. 2. The problem should be solvable in constant time with respect to the number of file formats. 3. Accessing the resource to determine or validate the format should be done as a last resort. #### Current approach The current approach is to serially iterate through installed reader plugins calling `napari_get_reader()`. Plugins are expected to return `None` as quickly as possible to reject incompatible files. As a result the current approach is burdened with performance problems. It scales linearly in the number of plugins. Also, the current approach assumes an undue degree of trust in plugin authors; the current design affords plugins the ability to take an arbitrary amount of time. To manipulate which reader a user prefers for a format, the user is required to reorder plugins[^1] so their preference is found first. This forces a requirement on Napari to maintain a stable order for the plugin list [^2]. This seems hard for the user to maintain. [^1]: Verify this. I'm not sure how to actually do this. [^2]: Is this really a maintained/documented invariant? ## Background [Mime types](https://datatracker.ietf.org/doc/html/rfc2046) are a common standard for uniquely identifying file types. Plugins and users should be able to add new mime-types as long as they are unique. Operating systems usually have a facility for describing known mime types. These can be accessed using [`mimetypes`](https://docs.python.org/3/library/mimetypes.html), a standard python library. ## Design A two-step process is used to resolve the Reader to use for a given resource. 1. Map the resource to a mime type. 2. Then map the mime type to a Reader. #### Determining the filetype given a string representing resource name[^3] File type detection should occur in stages, attempting to match types using the cheapest method available and progressing to more expensive methods until a match is found. A resource name can be categorized as a URI if it is prefixed by a valid `scheme` (See [`RFC3986 Appendix A`](https://datatracker.ietf.org/doc/html/rfc3986#appendix-A)). Otherwise it is a file name. If the resource name is a URI, 1. Attempt to look up the `scheme` in a dictionary mapping registered scheme's to mime types. 2. If that fails and the scheme is `http` or `https` attempt a `HEAD` request, and use the `Content-Type` returned in the header of the response as the mime-type, if possible. 3. Failing that, depending on the nature of the error[^4], attempt a `GET` request and attempt to use the `Content-Type` in the response header. Use a `Range` request to attempt to limit the `GET` to the first few bytes of the resource; the first 64 bytes may be used for file-type validation later on. 4. If the `GET` succeeded in returning the first few bytes of content, but did not have `Content-Type` in the response, attempt to match the first few bytes against a registry of filetype signatures. See, for example, the [`filemagic`][] library. 5. If none of the methods above succeed, give up. If the resource name is a file name, 1. Attempt to look up the extension[^5] in a dictionary matching extensions to mime-type. 2. If that fails or there is no extension for the file name, attempt to read the first few bytes of the file and attempt to match the first few bytes against a registry of filetype signatures. See, for example, the [`filemagic`] library. 3. If none of the methods above succeed, give up. As detailed above, this requires the application to maintain two dictionaries: 1. URI scheme -> mime type 2. file extension -> mime type Additionally, it may be convenient to extend this scheme to allow users or plugins to register a set of regular expressions that can be matched against the resource name to force the mime type. If the mime type cannot be determined, then the application could attempt to ask the user for input. [^3]: the filename or URI. [^4]: for example, move to the next step if the error is `405 - Method not allowed`. Don't move forward if the error is a `404 - Not found`. Error types may not be reliable, so maybe always move to the next step. [^5]: The substring starting just after the last `.` character in the string and extending to the end of the filename. [`filemagic`]: https://filemagic.readthedocs.io/en/latest/guide.html #### Determining which plugin to invoke given the filetype The application should maintain a dictionary mapping mime types to a collection of plugins that can service that type. When a Reader is requested for a resource, its mime type is inferred (see above). That type is resolved to a collection of plugins that yield compatible Readers. The application will serially invoke each Reader in a defined order until one succeeds. The application should ensure a Reader fails if it belongs to a disabled plugin. If no readers are available for the mime type, the application should suggest compatible un-installed plugins present in the plugin index. The order that determines the sequence in which Reader's are tried should be controllable by the user. Many approaches may work. For example, the plugins could be stored together with a score determining the user's preference so the one with the highest score is selected. Plugins should declare the mime types they can handle. On installation of a plugin, the application should append a reference to the plugin to the corresponding mime type entries. ## Backwards compatibility Currently, plugins cannot declare their mime types. Napari could ship with a predefined set of associations for known plugins with this limitation. ## Limitations/Concerns - [ ] Mime types for zarr, n5? - [ ] What does it look like when someone registers a new type? - [ ] How does someone register a new scheme? - [ ] What does it look like when a plugin author encounters the problem of registering a new file type? Reader's are serially tried until one succeeds. Validation is probably part of the "try." As a result, errors from strict Readers may be hidden. Invalid files will invoke each of the Readers registered for a file type. ## NOTES/TODO - [ ] This is more about mapping to Readers (and maybe Writers). Plugins may provide Readers. Revise to make wording more consistent. Narrow to readers in intro. - [ ] Address non-http/https URIs. Should remote content be moved into a seperate doc?

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully