Skip to content

Commit

Permalink
Removed dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
sesquideus committed Dec 10, 2023
1 parent ff63aef commit b097a6c
Show file tree
Hide file tree
Showing 22 changed files with 51 additions and 178 deletions.
8 changes: 6 additions & 2 deletions core/builder/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from enschema import Schema, SchemaMissingKeyError, SchemaError, And
from typing import Any, Self

from core.utilities import dicts, colour as c, crawler, logger
from core.utilities import colour as c, crawler, logger

log = logger.setupLog('dgs')

Expand Down Expand Up @@ -80,7 +80,11 @@ def adopt(self, **ctxs: 'Context') -> Self:
""" Adopt a new child context `ctx` under the key `key` """
for key, ctx in ctxs.items():
assert isinstance(ctx, Context)
self.data[key] = dicts.merge(self.data.get(key), ctx.data)

if key in self.data:
self.data[key] |= ctx.data
else:
self.data[key] = copy.deepcopy(ctx.data)

if self._schema is not None:
# If child has no schema, accept anything, otherwise merge
Expand Down
2 changes: 1 addition & 1 deletion core/builder/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, root, language):
self.populate(language)

def populate(self, language):
self.data = {'i18n': i18n.languages[language].as_dict()}
self._data = {'i18n': i18n.languages[language].as_dict()}
self.add_id(language)


Expand Down
2 changes: 1 addition & 1 deletion core/builder/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from pathlib import Path

from core.utilities import dicts, filters, colour as c
from core.utilities import filters, colour as c

log = logging.getLogger('dgs')

Expand Down
2 changes: 1 addition & 1 deletion core/templates/override.jtt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
\selectlanguage{(* i18n.name *)}
\setquotestyle[(* i18n.quotes.extra|default('') *)]{(* i18n.quotes.id *)}
\setquotestyle[(* i18n.quotes.extra|default('') *)]{(* i18n.quotes.babel_id *)}

\sisetup{
output-decimal-marker = {(* i18n.siunitx.output_decimal_marker *)},
Expand Down
110 changes: 0 additions & 110 deletions core/utilities/context.py

This file was deleted.

26 changes: 0 additions & 26 deletions core/utilities/dicts.py

This file was deleted.

14 changes: 5 additions & 9 deletions core/utilities/lists.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import itertools
from typing import Iterable, Callable, List, Dict, Any

from core.utilities import dicts


def add_numbers(items: List[Any], numbers: Iterable=itertools.count()) -> List[Dict]:
def add_numbers(items: List[Any], numbers: Iterable = itertools.count()) -> List[Dict]:
"""
Take a list `items` and return a list of dictionaries with number included.
Expand All @@ -20,7 +18,7 @@ def add_numbers(items: List[Any], numbers: Iterable=itertools.count()) -> List[D
List[Dict]
A list of dictionaries in format {'number': int, 'id': item}
"""
assert type(items) == list
assert isinstance(items, list), f"Cannot add numbers to {type(list)}"

result = []
for item in items:
Expand All @@ -31,12 +29,10 @@ def add_numbers(items: List[Any], numbers: Iterable=itertools.count()) -> List[D
return result


def numerate(items: Dict, numbers: Iterable=itertools.count()) -> List[Dict]:
def numerate(items: dict, numbers: Iterable = itertools.count()) -> dict:
for item in items:
assert type(item) == dict
dicts.merge(item, {
'number': next(numbers)
})
assert isinstance(item, dict)
item |= {'number': next(numbers)}
return items


Expand Down
3 changes: 2 additions & 1 deletion modules/naboj/builder/contexts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from core.builder import context
from core.builder.builder import get_last_commit_hash, get_branch
from core.utilities.schema import valid_language, CommitHash
from core.utilities.schema import valid_language
from core.builder.validator import CommitHash, String


class ContextNaboj(context.FileSystemContext):
Expand Down
11 changes: 6 additions & 5 deletions modules/naboj/builder/contexts/hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

from core import i18n
from core.utilities import lists
from core.utilities.schema import string, valid_language, valid_language_name
from core.utilities.schema import valid_language, valid_language_name
from core.builder.validator import String
from .base import ContextNaboj


class ContextCompetition(ContextNaboj):
_schema = Schema({
'id': string,
'id': String,
'tearoff': {
'per_page': int,
'height': int,
Expand All @@ -22,8 +23,8 @@ class ContextCompetition(ContextNaboj):
'inner': int,
},
'organisation': {
'name': string,
'address': string,
'name': String,
'address': String,
},
'constants': {
str: {
Expand All @@ -33,7 +34,7 @@ class ContextCompetition(ContextNaboj):
Optional('siextra'): str,
}
},
'URL': string,
'URL': String,
'hacks': dict,
})

Expand Down
2 changes: 1 addition & 1 deletion modules/naboj/builder/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def add_arguments(self):
super().add_arguments()
self.parser.add_argument('language', type=str)

def id(self):
def ident(self):
return self.args.competition, self.args.volume, self.args.language

def path(self):
Expand Down
8 changes: 5 additions & 3 deletions modules/naboj/module.mk
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ build/naboj/%/pdf-prerequisites: \
$$(subst source/,build/,$$(subst .gp,.pdf,$$(wildcard source/naboj/$$*/problems/*/*.gp))) \
$$(subst source/,build/,$$(subst .gp,.pdf,$$(wildcard source/naboj/$$*/problems/*/*/*.gp))) \
$$(wildcard source/naboj/$$*/meta.yaml) \
$$(subst $$(cdir),,$$(abspath source/naboj/$$*/../meta.yaml)) ;
$$(subst $$(cdir),,$$(abspath source/naboj/$$*/../meta.yaml)) \
build/core/i18n ;

# All problems, solutions and answers for every language, and overall
# <competition>/<volume>
Expand Down Expand Up @@ -328,8 +329,9 @@ output/naboj/%: \

# All targets for all venues
# <competition>/<volume>
output/naboj/%/venues: \
$$(foreach dir,$$(subst source/,output/,$$(wildcard source/naboj/$$*/venues/*)), $$(dir)) ;
output/naboj/%/venues:
# $$(subst source/,output/,$$(wildcard source/naboj/$$*/venues/*)) ;
@echo $(subst source/,output/,$(wildcard source/naboj/$*/venues/*)) ;

output/naboj/%/all: \
output/naboj/%/languages \
Expand Down
2 changes: 1 addition & 1 deletion modules/poetry/builder/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

sys.path.append('.')

from core.utilities import context
from core.builder import context


class ContextPoetry(context.Context):
Expand Down
3 changes: 2 additions & 1 deletion modules/scholar/builder/contexts/buildable.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def populate(self, course: str, year: int, issue: int):
module=ContextModule('scholar'),
course=ContextCourse(self.root, course),
year=ContextYear(self.root, course, year),
i18n=ContextI18n(self.root, self.data['course']['language']),
issue=self._issue_context_class(self.root, course, year, issue),
).adopt(
i18n=ContextI18n(self.root, self.data['course']['language']),
)
2 changes: 1 addition & 1 deletion modules/scholar/builder/lecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def add_arguments(self):
self.parser.add_argument('course', type=str)
self.parser.add_argument('lecture', type=str)

def id(self):
def ident(self):
return (self.args.course, self.args.lecture)


Expand Down
3 changes: 2 additions & 1 deletion modules/scholar/module.mk
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ build/scholar/%/pdf-prerequisites: \
$$(subst source/,build/,$$(subst .svg,.pdf,$$(wildcard source/scholar/$$*/*/*.svg))) \
$$(subst source/,build/,$$(subst .gp,.pdf,$$(wildcard source/scholar/$$*/*.gp))) \
$$(subst source/,build/,$$(subst .gp,.pdf,$$(wildcard source/scholar/$$*/*/*.gp))) \
source/scholar/$$*/meta.yaml ;
source/scholar/$$*/meta.yaml \
build/core/i18n ;

build/scholar/%/handout: \
$$(subst source/,build/,$$(subst .md,.tex,$$(wildcard source/scholar/$$*/*.md))) \
Expand Down
3 changes: 2 additions & 1 deletion modules/seminar/module.mk
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ build/seminar/%/pdf-prerequisites: \
$$(subst source/,build/,$$(subst .svg,.pdf,$$(wildcard source/seminar/$$*/*/*.svg))) \
$$(subst source/,build/,$$(subst .gp,.pdf,$$(wildcard source/seminar/$$*/*/*.gp))) \
$$(wildcard source/seminar/$$*/*/meta.yaml) \
source/seminar/$$*/meta.yaml ;
source/seminar/$$*/meta.yaml \
build/core/i18n ;

output/seminar/%/html-prerequisites: \
$$(subst source/,output/,$$(wildcard source/seminar/$$*/*/*.jpg)) \
Expand Down
6 changes: 3 additions & 3 deletions modules/simple/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def create_argument_parser(self):
self.parser.add_argument('year', type=int)
self.parser.add_argument('issue', type=int)

def id(self):
return (self.args.course, self.args.year, self.args.issue)
def ident(self):
return self.args.course, self.args.year, self.args.issue

def path(self):
return (self.args.course, f'{self.args.year:04d}', self.subdir, f'{self.args.issue:02d}')
return self.args.course, f'{self.args.year:04d}', self.subdir, f'{self.args.issue:02d}'


class BuilderSingle(builder.BaseBuilder):
Expand Down
14 changes: 8 additions & 6 deletions modules/simple/builder/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

sys.path.append('.')

from core.utilities import context
from core.builder import context


class ContextScholar(context.Context):
Expand All @@ -20,21 +20,23 @@ def node_path(self, root, course=None, year=None, target_type=None, issue=None):
class ContextScholarBase(ContextScholar):
def __init__(self, root, course, year):
super().__init__()
self.absorb('module', ContextModule('scholar'))
self.absorb('course', ContextCourse(root, course))
self.absorb('year', ContextYear(root, course, year))
self.adopt(
module=ContextModule('scholar'),
course=ContextCourse(root, course),
year=ContextYear(root, course, year),
)


class ContextHomework(ContextScholarBase):
def __init__(self, root, course, year, issue):
super().__init__(root, course, year)
self.absorb('issue', ContextIssue(root, course, year, 'homework', issue))
self.adopt(issue=ContextIssue(root, course, year, 'homework', issue))


class ContextHandout(ContextScholarBase):
def __init__(self, root, course, year, issue):
super().__init__(root, course, year)
self.absorb('issue', ContextIssue(root, course, year, 'handouts', issue))
self.adopt(issue=ContextIssue(root, course, year, 'handouts', issue))


class ContextModule(ContextScholar):
Expand Down
Loading

0 comments on commit b097a6c

Please sign in to comment.