Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user prefs #44

Merged
merged 1 commit into from
May 8, 2023
Merged

Conversation

MHendricks
Copy link
Member

Checklist

  • I have read the CONTRIBUTING.md document
  • I formatted my changes with black
  • I linted my changes with flake8
  • I have added documentation regarding my changes where necessary
  • Any pre-existing tests continue to pass
  • Additional tests were made covering my changes

Types of Changes

  • Bugfix (change that fixes an issue)
  • New Feature (change that adds functionality)
  • Documentation Update (if none of the other choices apply)

Proposed Changes

Add ability to save a uri to a user preference. Anywhere you would normally pass a uri, you can now pass - to use the saved uri value. This will be useful for creating desktop shortcuts to launch an alias for what ever the "current" uri is. These can be enabled by site configuration and you can force users to re-set the current URI after a timeout.

@MHendricks MHendricks force-pushed the user_prefs branch 2 times, most recently from 57792e8 to d11d7b5 Compare May 5, 2023 19:25
@MHendricks
Copy link
Member Author

This code shows how to decode a datetime/date objects encoded by HabJsonEncoder. I wrote it but decided to omit it from this PR because it seemed a little heavy handed and possibly expensive. It calls the date/datetime functions for every string that happens to be a specific length. I chose to leave the decoding up to the UserPrefs class that knows what json data needs to be a datetime object. I think if we wanted to do this auto-decoding we should probably make make the HabJsonEncoder class convert the date/datetime to a specific schema so we know what the original data type was when decoding it.

import json as _json
from datetime import date, datetime


class HabJsonDecoder(_json.JSONDecoder):
    def __init__(self, *args, **kwargs):
        super().__init__(object_hook=self.object_hook, *args, **kwargs)

    def object_hook(self, obj):
        return {key: self.decode_str(value) for key, value in obj.items()}

    @classmethod
    def decode_str(cls, obj):
        """Attempts to decode date and datetime objects from the given value."""
        count = len(obj)
        if count == 10:
            try:
                return date.fromisoformat(obj)
            except ValueError:
                pass
        elif count in (19, 26):
            try:
                return datetime.fromisoformat(obj)
            except ValueError:
                pass
        return obj

I often want an example of how to encode/decode custom objects with Json, so here is the complete example in one easy to access place. The current HabJsonEncoder class:

class HabJsonEncoder(_json.JSONEncoder):
    """JsonEncoder class that handles non-supported objects like hab.NotSet."""

    def default(self, obj):
        if obj is NotSet:
            # Convert NotSet to None for json storage
            return None
        # Handle date and datetime conversion
        if isinstance(obj, (datetime, date)):
            return obj.isoformat()

        # Let the base class default method raise the TypeError
        return _json.JSONEncoder.default(self, obj)

- URI is now required, but passing `-` indicates that you want to use a saved uri
- Remove --unfreeze options by instead passing those to the uri
- Add quickstart and install info to the README. It would be really nice
if github supported `.tabset` to allow for showing different shell interfaces
as tabs in the markdown.
@MHendricks MHendricks marked this pull request as ready for review May 8, 2023 17:34
@MHendricks MHendricks merged commit 3f9f9ed into blurstudio:main May 8, 2023
@MHendricks MHendricks deleted the user_prefs branch May 8, 2023 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant