Exemplo n.º 1
0
    def __init__(self, doc=None, post=None):
        # type: (Optional[Document], QueryDict) -> None
        self._doc = doc  # type: Optional[Document]
        initial_data = None
        if doc:
            initial_data = {
                'prefix': doc.prefix,
                'sep': doc.sep,
                'digits': doc.digits,
                'yaml': ''
            }
            with open(doc.config, 'r') as stream:
                yaml_data = load_yaml(stream, doc.config)
                if 'attributes' in yaml_data:
                    initial_data['yaml'] = yaml.dump(yaml_data['attributes'])

        if post is not None:
            initial_data['sep'] = post['sep']
            initial_data['digits'] = post['digits']
            initial_data['yaml'] = post['yaml']

        super().__init__(data=initial_data, initial=initial_data)
        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            Row(Column('prefix', css_class='form-group col-md-4 mb-0'),
                Column('sep', css_class='form-group col-md-4 mb-0'),
                Column('digits', css_class='form-group col-md-4 mb-0'),
                css_class='form-row'), 'yaml', Submit('submit', 'Submit'))
Exemplo n.º 2
0
    def _load(text, path):
        """Load YAML data from text.

        :param text: text read from a file
        :param path: path to the file (for displaying errors)

        :return: dictionary of YAML data

        """
        return common.load_yaml(text, path)
Exemplo n.º 3
0
 def save(self):
     self._doc.sep = self.cleaned_data['sep']
     self._doc.digits = self.cleaned_data['digits']
     self._doc.save()
     with open(self._doc.config, 'r') as stream:
         yaml_data = load_yaml(stream, self._doc.config)
         yaml_data_new = yaml.safe_load(self.cleaned_data['yaml'])
         yaml_data['attributes'] = yaml_data_new
         text = self._doc._dump(yaml_data)
         self._doc._write(text, self._doc.config)
Exemplo n.º 4
0
 def _reorder_from_index(document, path):
     """Reorder a document's item from the index."""
     # Load and parse index
     text = common.read_text(path)
     data = common.load_yaml(text, path)
     # Read updated values
     initial = data.get('initial', 1.0)
     outline = data.get('outline', [])
     # Update levels
     level = Level(initial)
     Document._reorder_section(outline, level, document)
Exemplo n.º 5
0
 def _reorder_from_index(document, path):
     """Reorder a document's item from the index."""
     # Load and parse index
     text = common.read_text(path)
     data = common.load_yaml(text, path)
     # Read updated values
     initial = data.get('initial', 1.0)
     outline = data.get('outline', [])
     # Update levels
     level = Level(initial)
     Document._reorder_section(outline, level, document)
Exemplo n.º 6
0
 def _read_index(path):
     """Load the index, converting comments to text entries for each item."""
     with open(path, 'r', encoding='utf-8') as stream:
         text = stream.read()
     yaml_text = []
     for line in text.split('\n'):
         m = re.search(r'(\s+)(- [\w\d-]+\s*): # (.+)$', line)
         if m:
             prefix = m.group(1)
             uid = m.group(2)
             item_text = m.group(3).replace('"', '\\"')
             yaml_text.append('{p}{u}:'.format(p=prefix, u=uid))
             yaml_text.append('    {p}- text: "{t}"'.format(p=prefix, t=item_text))
         else:
             yaml_text.append(line)
     return common.load_yaml('\n'.join(yaml_text), path)
Exemplo n.º 7
0
 def _read_index(path):
     """Load the index, converting comments to text entries for each item."""
     with open(path, 'r', encoding='utf-8') as stream:
         text = stream.read()
     yaml_text = []
     for line in text.split('\n'):
         m = re.search(r'(\s+)(- [\w\d-]+\s*): # (.+)$', line)
         if m:
             prefix = m.group(1)
             uid = m.group(2)
             item_text = m.group(3).replace('"', '\\"')
             yaml_text.append('{p}{u}:'.format(p=prefix, u=uid))
             yaml_text.append('    {p}- text: "{t}"'.format(p=prefix, t=item_text))
         else:
             yaml_text.append(line)
     return common.load_yaml('\n'.join(yaml_text), path)
Exemplo n.º 8
0
def _file_yml(path, document, **_):
    """Import items from a YAML export to a document.

    :param path: input file location
    :param document: document to import items

    """
    # Parse the file
    log.info("reading items in {}...".format(path))
    text = common.read_text(path)
    # Load the YAML data
    data = common.load_yaml(text, path)
    # Add items
    for uid, attrs in data.items():
        try:
            item = document.find_item(uid)
        except DoorstopError:
            pass  # no matching item
        else:
            item.delete()
        add_item(document.prefix, uid, attrs=attrs, document=document)
Exemplo n.º 9
0
def _file_yml(path, document, **_):
    """Import items from a YAML export to a document.

    :param path: input file location
    :param document: document to import items

    """
    # Parse the file
    log.info("reading items in {}...".format(path))
    text = common.read_text(path)
    # Load the YAML data
    data = common.load_yaml(text, path)
    # Add items
    for uid, attrs in data.items():
        try:
            item = document.find_item(uid)
        except DoorstopError:
            pass  # no matching item
        else:
            item.delete()
        add_item(document.prefix, uid, attrs=attrs, document=document)