Skip to content

Commit

Permalink
Replacing local implementation of SuperDict by super-collections version
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Franceschetti committed Oct 5, 2024
1 parent 38b9681 commit 59f82db
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 95 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ View the [mkdocs-macro documentation](https://mkdocs-macros-plugin.readthedocs.i
markdown-toc -i README.md
-->

<!-- toc -->

## Overview
**mkdocs-macros-plugin** is a plugin that makes it easier for contributors
of an [MkDocs](https://www.mkdocs.org/) website to produce richer and more beautiful pages. It transforms the markdown pages
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_macros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# from .plugin import MacrosPlugin
# for fixing URLS in macros
from .context import fix_url, is_relative as is_relative_url
from .util import SuperDict
# from .util import SuperDict, SuperList
11 changes: 7 additions & 4 deletions mkdocs_macros/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# --------------------------------------------
# Main part of the plugin
# Defines the VariablesPlugin class
# Defines the MacrosPlugin class
#
# Laurent Franceschetti (c) 2018
# MIT License
Expand All @@ -12,11 +12,13 @@
import pathspec
import json
from datetime import datetime

import yaml

from jinja2 import (
Environment, FileSystemLoader, Undefined, DebugUndefined, StrictUndefined,
)
from super_collections import SuperDict

from mkdocs.config import config_options
from mkdocs.config.config_options import Type as PluginType
from mkdocs.plugins import BasePlugin
Expand All @@ -26,8 +28,9 @@
from mkdocs_macros.context import define_env
from mkdocs_macros.util import (
install_package, parse_package, trace, debug,
update, SuperDict, import_local_module, format_chatter, LOG, get_log_level,
setup_directory, CustomEncoder
update, import_local_module, format_chatter, LOG, get_log_level,
setup_directory, CustomEncoder,
# SuperDict,
)

# ------------------------------------------
Expand Down
81 changes: 0 additions & 81 deletions mkdocs_macros/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,87 +236,6 @@ def update(d1, d2):
# if it is any kind of object
d1 = deepcopy(d2)

class SuperDict(dict):
"""
A dictionary accessible with the dot notation
a['foo'] <=> a.foo
except for standard methods
"""

def __init__(self, *args, **kwargs):
# Call the superclass's __init__ method
super().__init__(*args, **kwargs)
self.__post_init__()

def __post_init__(self):
"Recursively transform sub-dictionary"
for key, value in self.items():
if isinstance(value, dict):
self[key] = SuperDict(value)

def __getattr__(self, name:str):
"Allow dot notation on reading"
ERR_MSG = "Cannot find attribute '%s'" % name
# if name.startswith('_'):
# raise AttributeError(ERR_MSG)
try:
return self[name]
except KeyError:
raise AttributeError(ERR_MSG)

def __setattr__(self, name, value):
"Allow dot notation on writing"
# ERR_MSG = "Cannot assign an attribute starting with _ ('%s')" % name
# if name.startswith('_'):
# raise AttributeError(ERR_MSG)
self[name] = value

@property
def _attributes(self):
"Make a list of the valid attributes"
return list(self.keys())

def _codewords(self):
"Make a list of the codewords"
return

def __dir__(self):
"List all attributes (for autocompletion, etc.)"
return super().__dir__() + self._attributes



# -------------------------------------
# Output
# -------------------------------------

def to_json(self):
"Convert to json"
return json.dumps(self, cls=CustomEncoder)

def to_hjson(self):
"""
Convert to hjson
"""
python_dict = json.loads(self.to_json())
return hjson.dumps(python_dict)


def __str__(self):
"Print a superdict"
return self.to_hjson()
return self.to_yaml()
# r = [f"{self.__class__.__name__}:"]
# r.extend([f" - {key}: {value}" for key, value in self.items()])
# return("\n".join(r))

def __rich__(self):
"Print a superdict (for rich)"
r = [f"[bold red]{self.__class__.__name__}:[/]"]
r.append(self.to_hjson())
return("\n".join(r))



Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Initialization
# --------------------

VERSION_NUMBER = '1.3.2'
VERSION_NUMBER = '1.3.3'

# required if you want to run document/test
# pip install 'mkdocs-macros-plugin[test]'
Expand Down Expand Up @@ -49,7 +49,8 @@ def read_file(fname):
'hjson',
'pathspec',
'python-dateutil',
'packaging'
'packaging',
'super-collections'
],
extras_require={
'test': TEST_REQUIRE,
Expand All @@ -67,7 +68,7 @@ def read_file(fname):
packages=find_packages(exclude=['*.tests']),
entry_points={
'mkdocs.plugins': [
'macros = mkdocs_macros.plugin:MacrosPlugin'
'macros = mkdocs_macros.plugin:MacrosPlugin',
]
}
)
4 changes: 1 addition & 3 deletions test/docproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from typing import Any, List

from bs4 import BeautifulSoup
from super_collections import SuperDict


"A dictionary where the keys are also accessible with the dot notation"
from mkdocs_macros.util import SuperDict
from .fixture_util import (get_frontmatter, markdown_to_html, get_first_h1,
find_in_html, find_after, list_markdown_files, find_page,
run_command)
Expand Down
5 changes: 3 additions & 2 deletions test/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This is the two classes:
- DocProject
- MacrosDocProject
- TestMarkdownPage
Expand All @@ -18,7 +18,8 @@
from typing import Any, List


from mkdocs_macros.util import SuperDict
from super_collections import SuperDict

from .fixture_util import list_markdown_files, find_after
from .docproject import (MarkdownPage, DocProject, REF_DIR,
DOCS_DEFAULT_DIRNAME)
Expand Down
2 changes: 1 addition & 1 deletion test/fixture_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pandas as pd
from bs4 import BeautifulSoup

from mkdocs_macros.util import SuperDict
from super_collections import SuperDict

# ---------------------------
# Print functions
Expand Down

0 comments on commit 59f82db

Please sign in to comment.