Esempio n. 1
0
 def test_deep_reference_error(self):
     """Missing keys raise error."""
     obj = structures.DeepReferenceDict({
         'key': {},
     })
     with self.assertRaises(KeyError):
         _ = obj['key.sub_key.value']
Esempio n. 2
0
 def func(path):
     if '.' not in path:
         return None
     main, reference = path.split('.', 1)
     path = '/content/strings/{}.yaml'.format(main)
     locale = str(doc.locale_safe) if doc else loader_locale
     if doc:
         pod.podcache.dependency_graph.add(doc.pod_path, path)
     if reference:
         data = structures.DeepReferenceDict(
             self.read_yaml(path, locale=locale))
         try:
             value = data[reference]
             if value is None:
                 if doc:
                     pod.logger.warning(
                         'Missing {}.{} in {}'.format(
                             main, reference, doc.pod_path))
                 else:
                     pod.logger.warning('Missing {}.{}'.format(
                         main, reference))
             return value
         except KeyError:
             return None
     return None
Esempio n. 3
0
 def read_string(cls, path):
     if '.' not in path:
         return None
     main, reference = path.split('.', 1)
     path = '/content/strings/{}.yaml'.format(main)
     tracking_func(path)
     if reference:
         data = structures.DeepReferenceDict(
             cls.read_yaml(path, locale=cls.loader_locale()))
         try:
             allow_draft = pod.podspec.fields.get('strings',
                                                  {}).get('allow_draft')
             if allow_draft is False and data.get(DRAFT_KEY):
                 raise DraftStringError(
                     'Encountered string in draft -> {}?{}'.format(
                         path, reference))
             value = data[reference]
             if value is None:
                 if cls.pod_path():
                     pod.logger.warning('Missing {}.{} in {}'.format(
                         main, reference, cls.pod_path()))
                 pod.logger.warning('Missing {}.{}'.format(
                     main, reference))
             return value
         except KeyError:
             return None
     return None
Esempio n. 4
0
def yaml(path, _pod, _doc=None):
    """Retrieves a yaml file from the pod."""
    locale = str(_doc.locale_safe) if _doc else None
    if '?' in path:
        path, reference = path.split('?')
        data = structures.DeepReferenceDict(_read_yaml(_pod, path, locale=locale))
        return data[reference]
    return _read_yaml(_pod, path, locale=locale)
Esempio n. 5
0
 def test_deep_reference(self):
     """Delimited keys are accessible."""
     obj = structures.DeepReferenceDict({
         'key': {
             'sub_key': {
                 'value': 'foo',
             }
         },
     })
     self.assertEqual('foo', obj['key']['sub_key']['value'])
     self.assertEqual('foo', obj['key.sub_key.value'])
Esempio n. 6
0
 def func(path):
     if '?' in path:
         path, reference = path.split('?')
         tracking_func(path)
         data = structures.DeepReferenceDict(
             self.read_yaml(path, locale=self.loader_locale()))
         try:
             return data[reference]
         except KeyError:
             return None
     tracking_func(path)
     return self.read_yaml(path, locale=self.loader_locale())
Esempio n. 7
0
 def func(path):
     locale = str(doc.locale_safe) if doc else loader_locale
     if '?' in path:
         path, reference = path.split('?')
         if doc:
             pod.podcache.dependency_graph.add(doc.pod_path, path)
         data = structures.DeepReferenceDict(self.read_yaml(path, locale=locale))
         try:
             return data[reference]
         except KeyError:
             return None
     if doc:
         pod.podcache.dependency_graph.add(doc.pod_path, path)
     return self.read_yaml(path, locale=locale)
Esempio n. 8
0
 def func(path):
     if '.' not in path:
         return None
     main, reference = path.split('.', 1)
     path = '/content/strings/{}.yaml'.format(main)
     locale = str(doc.locale_safe) if doc else loader_locale
     if doc:
         pod.podcache.dependency_graph.add(doc.pod_path, path)
     if reference:
         data = structures.DeepReferenceDict(self.read_yaml(path, locale=locale))
         try:
             return data[reference]
         except KeyError:
             return None
     return None
Esempio n. 9
0
    def doc(self):
        """Doc for the controller."""
        if not self._doc:
            partial = self.params['partial']
            ext_config = structures.DeepReferenceDict(
                self.pod.extensions_controller.extension_config(
                    'extensions.editor.EditorExtension'))

            collection_path = ext_config['screenshots.partials.collection']

            if not collection_path:
                raise werkzeug_exceptions.BadRequest(
                    'No collection path defined for partial screenshots')

            col = self.pod.get_collection(collection_path)

            pod_partial = self.pod.partials.get_partial(partial)
            partial_example = pod_partial.config.get('editor', {}).get(
                'examples', {}).get(self.params['key'])
            if not partial_example:
                raise werkzeug_exceptions.NotFound(
                    'Unable to find example in partial: {}'.format(
                        self.params['key']))

            partial_example['partial'] = partial

            doc_fields = {
                '$view': ext_config['screenshots.partials.view'],
                'partials': [
                    partial_example,
                ],
            }

            locale = self.route_info.meta.get('locale',
                                              self.params.get('locale'))
            pod_path = os.path.join(collection_path, '_partial.yaml')
            self._doc = document.Document(pod_path,
                                          locale=locale,
                                          _pod=self.pod,
                                          _collection=col)
            self._doc.format.update(fields=doc_fields)
        return self._doc
Esempio n. 10
0
 def deep_reference(data, reference):
     data = structures.DeepReferenceDict(data)
     try:
         return data[reference]
     except KeyError:
         return None