Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:02443124ed4ee0d8d724fefd38bf9b271361cc09
  • Code
  • Branches (3)
  • Releases (7)
    • Branches
    • Releases
    • HEAD
    • refs/heads/dev-mike
    • refs/heads/main
    • refs/tags/v2023.1
    • v2023.11
    • v2023.10
    • v2023.9
    • v2023.6
    • v2023.5
    • v2023.4
    • v2023.3
  • b2dfb87
  • /
  • src
  • /
  • chemfeat
  • /
  • csv_wrapper.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
  • release
content badge
swh:1:cnt:91d15a5c51a4aff528094da5f2a3a9ffec6ebcc8
directory badge
swh:1:dir:94ad56ed57ceb375735960554b1699e93fb62f82
revision badge
swh:1:rev:2f39f190cbe8fc85d0141288ae6ff107bfe475ec
snapshot badge
swh:1:snp:02443124ed4ee0d8d724fefd38bf9b271361cc09
release badge
swh:1:rel:74b590b879f451c8ab8b57d0cb2747b04ba20ef7

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
  • release
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Tip revision: 2f39f190cbe8fc85d0141288ae6ff107bfe475ec authored by Jan-Michael Rye on 02 October 2023, 09:43:41 UTC
Add options to `chemfeat configure`
Tip revision: 2f39f19
csv_wrapper.py
#!/usr/bin/env python3

'''
Functions for working with CSV files.
'''

import csv
import logging
import pathlib


LOGGER = logging.getLogger(__name__)


class CSVWrapper():
    '''
    Wrapper around a CSV file to faciliate appending rows in real-time via an
    iterable of dictionaries.
    '''
    ENCODING = 'utf-8'

    def __init__(self, path, **kwargs):
        self.path = pathlib.Path(path).resolve()
        self.kwargs = kwargs

    @property
    def headers(self):
        '''
        The current headers from the CSV file, or None if they do not exist.

        This assumes that the first row contains the headers.
        '''
        try:
            with self.path.open('r', encoding=self.ENCODING) as csvfile:
                reader = csv.reader(csvfile, **self.kwargs)
                for row in reader:
                    return row
        except FileNotFoundError:
            pass
        return None

    def __iter__(self):
        try:
            with self.path.open('r', encoding=self.ENCODING) as csvfile:
                reader = csv.DictReader(csvfile)
                for row in reader:
                    yield row
        except FileNotFoundError:
            pass

    def write_rows(self, rows, headers=None, append=False):
        '''
        Append rows to the CSV file.

        Args:
            rows:
                A generator of dicts.
        '''
        if append:
            existing_headers = self.headers
            if existing_headers is not None:
                if headers is not None and existing_headers != headers:
                    LOGGER.warning('Re-using existing headers: %s', existing_headers)
                headers = existing_headers

        first_row = None
        try:
            if headers is None:
                first_row = next(rows)
                headers = sorted(first_row)
        except StopIteration:
            LOGGER.warning('Attempting to append 0 rows to %s', self.path)

        mode = 'a' if append else 'w'
        self.path.parent.mkdir(parents=True, exist_ok=True)
        with self.path.open(mode, encoding='utf-8') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=headers, **self.kwargs)

            if first_row:
                writer.writeheader()
                writer.writerow(first_row)
                csvfile.flush()

            for row in rows:
                writer.writerow(row)
                csvfile.flush()

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API