How to use the munch.DefaultMunch.fromDict function in munch

To help you get started, we’ve selected a few munch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github retorquere / zotero-better-bibtex / setup / item.py View on Github external
#!/usr/bin/env python3

import os
import urllib.request
import json
from munch import Munch, DefaultMunch
import copy

root = os.path.join(os.path.dirname(__file__), '..')

def load(url):
  with urllib.request.urlopen(url) as f:
    return json.loads(f.read().decode())

data = DefaultMunch.fromDict({
  'zotero': load('https://api.zotero.org/schema'),
  'jurism': load('https://raw.githubusercontent.com/Juris-M/zotero-schema/master/schema-jurism.json')
}, None)

print('Generating item field metadata...')
ValidFields = DefaultMunch(None, {})
ValidTypes = {}
Alias = {}
Itemfields = set()
for client in data.keys():
  for spec in data[client].itemTypes:
    if spec.itemType in ValidTypes:
      ValidTypes[spec.itemType] = 'true'
    else:
      ValidTypes[spec.itemType] = client
github retorquere / zotero-better-bibtex / setup / item.py View on Github external
delete creator.name
      }
      if (!jurism) delete creator.multi
    }
  }

  if (!jurism) delete item.multi

  return item
}''', file=f)

print('Generating field mapping for CSL variables...')
with open(os.path.join(root, 'gen', 'csl-mapping.json'), 'w') as f:
  schema = data.zotero
  mapping = Munch(
    field = DefaultMunch.fromDict(copy.deepcopy(schema.csl.fields.text.toDict())),
    creator = { v: k for k, v in schema.csl.names.items() },
  )
  for csl, zot in schema.csl.fields.date.items():
    if not mapping.field[csl]: mapping.field[csl] = []
    mapping.field[csl].append(zot)

  if mapping.field.status:
    mapping.field.status = [v for v in mapping.field.status if v != 'legalStatus'] # that's not right according to https://docs.citationstyles.org/en/stable/specification.html#appendix-iv-variables

  for section in list(mapping.keys()):
    for name in list(mapping[section].keys()):
      if len(mapping[section][name]) == 0:
        del mapping[section][name]

  json.dump(mapping, f, indent='  ')