Skip to content

Files and Paths

The fileio module keeps files organized. With the tools described in this page, you should never need to reference a file by constructing a path from scratch.

Part number and revision folders

Harnice requires a revision folder and a part number folder to be able to build any project.

# suppose your part number is "123456" and your revision is "A"

|-- A123456/
    L--   revA/

If you're starting a new project, click New project… in Harnice. Harnice creates the part-number folder, the first revision folder, and the right file structure.

Revision history data will be stored automatically inside the part number folder. That is the file that tells Harnice what kind of project you're currently working on, among other things.

If you need to make a new revision of a project, right-click the project and choose New rev….

Project file structures

Projects and macros have their own pre-defined file structure. It's stored as a function inside the Harnice source code. Here's an example of the Harness one:

def file_structure(): # example of a file structure of a project
    return {
        "build.py": "build instructions",
        "instances.json": "instances",
        "libhist.tsv": "library history",
        "flat.csv": "flattened network",
        "available.json": "available network",
        "chosen.json": "chosen network",
        "entities.json": "chosen entity list",
    }

A file structure is defined for each project or macro. It is automatically passed into the code when that project or macro is ran.

Here's the basics of how to read a file structure dictionary:

  • Files: each file is a key–value pair where the key is the filename and the value is a short, human-readable file key (e.g. "myfile.tsv": "signals list"). You use the file key with fileio.path("signals list").
  • Directories: a directory is a key whose value is another dict (empty {} or more key–value pairs). The key is the directory name and is what you pass to fileio.dirpath("dirname").

Finding a file

fileio.path("file key") returns the path of a file, and fileio.dirpath("dirname") returns the path of a folder. For most purposes, you pass only the key. Both take two optional arguments, structure_dict and base_directory:

structure_dict:

  • This is the file structure dictionary that is actually parsed to find the file path.
  • By default, the file structure of the project that's currently being run by Harnice is passed in at runtime.
  • Alternatively, if you need to pass a special structure dict in, you can do that here. You can do this, for example, if you have a custom macro or a custom file structure preference for an existing project.

base_directory:

  • This is the directory from which the relative paths from the file structure dictionary originates.
  • By default, this is set to the revision folder of the project you're working on. This means that the file structure dictionary of the project is in reference to, or added on top of, the revision file of the current project.
  • If you need to reference files in another project, for example, or recursively with a macro that's called as an instance, that has its own sub-instances, you can change the base directory of the file structure dictionary to point to wherever you want with this.

Parametric file names in your folder structure

You can add extra arguments to file_structure() (and pass a custom dict into fileio) for more complex layouts. For example, maybe you need a filename that is controlled by a variable, like in this example:

def macro_file_structure(page_name=None, page_counter=None):
    return {
        f"{artifact_id}-mastercontents.svg": "master contents svg",
        f"{artifact_id}.pdf": "output pdf",
        "page_svgs": {
            f"{page_counter}-{page_name}-user-editable.svg": "user editable page svg"
        },
    }

The current part number and revision

Harnice stores the current part number and revision in the module state.

The file keys of every project type are in the project layout schema, and the fileio and state calls are in the build API schema. See Schema.