Documentation

files

Functions

function
abspath(path: str) str
Parameters
  • path: str
function
annotateDir(app: Any, tool: str) tuple[(str, str)]

Return the input and output and report directories for a specific annotation tool.

Return the input and output and report directories for a specific annotation tool. * The input directory is located next to the CF data of the corpus * The output directory is located in the `_temp` directory next to the CF data of the corpus of the corpus Parameters ---------- app: object the CF app tool: string The name of the annotation tool Returns ------- string The path of the working directory for that tool and that corpus
Parameters
  • app: Any
  • tool: str
function
backendRep(be: str | None, kind: str, default: str | None = None) str | None

Various back-end dependent values.

Various back-end dependent values. First of all, the back-end value is normalized. Then related values are computed. Parameters ---------- be: string or None The raw back-end value. It will be normalized first, where missing, undefined, empty values are converted to the string `github`, and other values will be lower-cased. Also, `github.com` and `gitlab.com` will be shortened to `github` and `gitlab`. kind: string Indicates what kind of related value should be returned: * `norm`: the normalized value as described above * `tech`: technology of the back-end: either `github` or `gitlab` or None; we assume that there is only one GitHub; that there are many GitLabs; any back-end that is not `github` is an instance of `gitlab`. * `name`: lowercase shortest name of the back-end: `github` or `gitlab` or a server name like `gitlab.huc.knaw.nl` * `machine`: lowercase machine name of the back-end: `github.com` or `gitlab.com` or a server name like `gitlab.huc.knaw.nl` * `spec`: enclosed in `<` and `>`. Depending on the parameter `default` the empty string is returned instead. * `clone`: base directory where clones of repos in this back-end are stored `~/github`, etc. * `cache`: base directory where data downloads from this back-end are stored: `~/text-fabric-data/github`, etc. * `url`: URL of the online back-end * `urlnb`: URL of notebooks from the online back-end, rendered on NB-Viewer * `pages`: base URL of the Pages service of the back-end default: boolean, optional False Only relevant for `kind` = `rep`. If `default` is passed and not None and `be` is equal to `default`, then the empty string is returned. Explanation: this is used to supply a back-end specifier to a module but only if that module has a different back-end than the main module. Returns ------- string
Parameters
  • be: str | None
  • kind: str
  • default: str | None= None
function
chDir(directory: str) None

Change to other directory.

Change to other directory. Parameters ---------- directory: string The directory to change to.
Parameters
  • directory: str
function
clearTree(path: str) None

Remove all files from a directory, recursively, but leave subdirectories.

Remove all files from a directory, recursively, but leave subdirectories. Reason: we want to inspect output in an editor. But if we remove the directories, the editor looses its current directory all the time. Parameters ---------- path: The directory in question. A leading `~` will be expanded to the user's home directory.
Parameters
  • path: str
function
dirAllFiles(path: str, ignore: set[str] | None = None) list[str] | tuple[(str, ...)]

Gets all the files found by `path`.

Gets all the files found by `path`. The result is just `[path]` if `path` is a file, otherwise the list of files under `path`, recursively. The files are sorted alphabetically by path name. Parameters ---------- path: string The path to the file or directory on the file system. ignore: set Names of directories that must be skipped Returns ------- tuple of string The names of the files under `path`, starting with `path`, followed by the bit relative to `path`.
Parameters
  • path: str
  • ignore: set[str] | None= None
function
dirContents(path: str) tuple[(tuple[(str, ...)], tuple[(str, ...)])]

Gets the contents of a directory.

Gets the contents of a directory. Only the direct entries in the directory (not recursively), and only real files and folders. The list of files and folders will be returned separately. There is no attempt to sort the files. Parameters ---------- path: string The path to the directory on the file system. Returns ------- tuple of tuple The subdirectories and the files.
Parameters
  • path: str
function
dirCopy(pathSrc: str, pathDst: str, noclobber: bool = False) bool

Copies a directory if it exists as directory.

Copies a directory if it exists as directory. Wipes the destination directory, if it exists.
Parameters
  • pathSrc: str
  • pathDst: str
  • noclobber: bool= False
function
dirEmpty(target: str) bool
Parameters
  • target: str
function
dirExists(path: str | None) bool

Whether a path exists as directory on the file system.

Parameters
  • path: str | None
function
dirMake(path: str) None

Creates a directory if it does not already exist as directory.

Parameters
  • path: str
function
dirMove(pathSrc: str, pathDst: str) bool

Moves a directory if it exists as directory.

Moves a directory if it exists as directory. Refuses the operation in the target exists.
Parameters
  • pathSrc: str
  • pathDst: str
function
dirNm(path: str) str

Get the directory part of a file name.

Parameters
  • path: str
function
dirRemove(path: str) None

Removes a directory if it exists as directory.

Parameters
  • path: str
function
expandDir(obj: Any, dirName: str) str
Parameters
  • obj: Any
  • dirName: str
function
expanduser(path: str) str
Parameters
  • path: str
function
extNm(path: str) str

Get the extension part of a file name.

Get the extension part of a file name. The dot is not included. If there is no extension, the empty string is returned.
Parameters
  • path: str
function
fileCopy(pathSrc: str, pathDst: str) None

Copies a file if it exists as file.

Copies a file if it exists as file. Wipes the destination file, if it exists.
Parameters
  • pathSrc: str
  • pathDst: str
function
fileCopyExpr(dirSrc: str, dirDst: str) None

Copies the `__checkout__.txt` file from one directory to an other.

Copies the `__checkout__.txt` file from one directory to an other. Wipes the destination file, if it exists.
Parameters
  • dirSrc: str
  • dirDst: str
function
fileExists(path: str) bool

Whether a path exists as file on the file system.

Parameters
  • path: str
function
fileMake(path: str, force: bool = False) None

Create a new empty file.

Create a new empty file. If necessary, create intermediate subdirectories. If the file already exists, do nothing, unless `force` is True. If the file exists as a directory, do nothing Parameters ---------- path: string The path to the new file force: boolean, optional False If `False`, nothing is done if the file already exists. Otherwise, an existing file is truncated.
Parameters
  • path: str
  • force: bool= False
function
fileMove(pathSrc: str, pathDst: str) None

Moves a file if it exists as file.

Moves a file if it exists as file. Wipes the destination file, if it exists.
Parameters
  • pathSrc: str
  • pathDst: str
function
fileNm(path: str) str

Get the file part of a file name.

Parameters
  • path: str
function
fileOpen(args: Any = (), kwargs: Any = {}) TextIO | BinaryIO

Wrapper around `open()`, making sure `encoding="utf8" is passed.

Wrapper around `open()`, making sure `encoding="utf8" is passed. This function calls `open()` with the same arguments, but if the optional argument `encoding` is missing and the mode argument does not contain a `b` (binary file), then `encoding="utf8"` is supplied.
Parameters
  • args: Any= ()
  • kwargs: Any= {}
function
fileRemove(path: str) None

Removes a file if it exists as file.

Parameters
  • path: str
function
getCwd() str

Get current directory.

Get current directory. Returns ------- string The current directory.
function
getLocation(targetDir: str | None = None) tuple[(str | None, str | None, str | None, str | None)]

Get back-end, org, repo, relative of directory.

Get back-end, org, repo, relative of directory. Parameters ---------- targetDir: string, optional None If None, we take the current directory. Otherwise, if it starts with a `/` we take it as the absolute target directory. Otherwise, we append it to the absolute path of the current directory, with a `/` in between. We assume the target directory is somewhere inside `~/backend/org/repo` If it is immediately inside this, we set `relative` to `""`. If it is deeper down, we assume the reference directory is the parent of the current directory, and the path of this parent, relative to the repo directory goes into the `relative` component, preceded with a backslash if it is non-empty. Returns ------- tuple Consisting of `backend`, `org`, `repo`, `relative`. `relative` is either empty or it starts with a "/" plus a non-empty path.
Parameters
  • targetDir: str | None= None
function
initTree(path: str, fresh: bool = False, gentle: bool = False) None

Make sure a directory exists, optionally clean it.

Make sure a directory exists, optionally clean it. Parameters ---------- path: The directory in question. A leading `~` will be expanded to the user's home directory. If the directory does not exist, it will be created. fresh: boolean, optional False If True, existing contents will be removed, more or less gently. gentle: boolean, optional False When existing content is removed, only files are recursively removed, not subdirectories.
Parameters
  • path: str
  • fresh: bool= False
  • gentle: bool= False
function
isDir(path: str) bool

Whether path exists and is a directory.

Parameters
  • path: str
function
isFile(path: str) bool

Whether path exists and is a file.

Parameters
  • path: str
function
normpath(path: str | None) str | None
Parameters
  • path: str | None
function
prefixSlash(path: str) str

Prefix a / before a path if it is non-empty and not already starts with it.

Parameters
  • path: str
function
readJson(text: str | None = None, plain: bool = False, asFile: str | TextIO | None = None, preferTuples: bool = False) dict[(str, Any)] | AttrDict

Read a JSON file or string.

Read a JSON file or string. The input data is either a text string or a file name or a file handle. Exactly one of the optional parameters `text` and `asFile` should be `None`. Parameters ---------- text: string, optional None The input text if it is a string. asFile: string | object, optional None The input text if it is a file. If the value of `asFile` is a string, it is taken as a file name to read. Otherwise, it is taken as a file handle from which data can be read. plain: boolean, optional False If True, it return a dictionary, otherwise it wraps the data structure recursively in an AttrDict. preferTuples: boolean, optional False If the resulting data structure is to be wrapped in an AttrDict, we will represent lists as tuples. Returns ------- object The resulting data structure.
Parameters
  • text: str | None= None
  • plain: bool= False
  • asFile: str | TextIO | None= None
  • preferTuples: bool= False
function
readYaml(text: str | None = None, plain: bool = False, asFile: str | TextIO | None = None, preferTuples: bool = True, preferLists: bool = True) dict[(str, Any)] | AttrDict | None

Read a YAML file or string.

Read a YAML file or string. The input data is either a text string or a file name or a file handle. Exactly one of the optional parameters `text` and `asFile` should be `None`. Parameters ---------- text: string, optional None The input text if it is a string. asFile: string | object, optional None The input text if it is a file. If the value of `asFile` is a string, it is taken as a file name to read. Otherwise, it is taken as a file handle from which data can be read. plain: boolean, optional False If True, it return a dictionary, otherwise it wraps the data structure recursively in an AttrDict. preferTuples: boolean, optional False If the resulting data structure is to be wrapped in an AttrDict, we will represent lists as tuples. Returns ------- object The resulting data structure.
Parameters
  • text: str | None= None
  • plain: bool= False
  • asFile: str | TextIO | None= None
  • preferTuples: bool= True
  • preferLists: bool= True
function
replaceExt(path: str, newExt: str) str

Replace the extension of a path by another one. Specify it without dot.

Parameters
  • path: str
  • newExt: str
function
setDir(obj: Any) None
Parameters
  • obj: Any
function
splitPath(path: str) tuple[(str, str)]

Split a file name in a directory part and a file part.

Parameters
  • path: str
function
str_presenter(dumper, data: str)

configures yaml for dumping multiline strings Ref: https://stackoverflow.com/questions/8640959

Parameters
  • dumper
  • data: str
function
stripExt(path: str) str

Strip the extension of a file name, if there is one.

Parameters
  • path: str
function
unexpanduser(path: str) str
Parameters
  • path: str
function
writeJson(data: Any, asFile: str | TextIO | None = None, kwargs: Any = {}) str | None

Write data as JSON.

Write data as JSON. The output is either delivered as string or written to a file. Parameters ---------- data: object The input data. asFile: string | object, optional None The output destination. If `None`, the output text is delivered as the function result. If the value of `asFile` is a string, it is taken as a file name to write to. Otherwise, it is taken as a file handle to which text can be written. kwargs: dict, optional {} Additional paramters for the underlying json.dump method. By default, we use `indent=1, ensure_ascii=False`. Returns ------- str | void If asFile is not None, the function returns None and the result is written to a file. Otherwise, the result string is returned.
Parameters
  • data: Any
  • asFile: str | TextIO | None= None
  • kwargs: Any= {}
function
writeYaml(data: Any, asFile: str | TextIO | None = None, sorted: bool = False) str | None

Write data as YAML.

Write data as YAML. The output is either delivered as string or written to a file. Parameters ---------- data: object The input data. asFile: string | object, optional None The output destination. If `None`, the output text is delivered as the function result. If the value of `asFile` is a string, it is taken as a file name to write to. Otherwise, it is taken as a file handle to which text can be written. sorted: boolean, optional False If True, when writing out a dictionary, its keys will be sorted. Returns ------- str | void If asFile is not None, the function returns None and the result is written to a file. Otherwise, the result string is returned.
Parameters
  • data: Any
  • asFile: str | TextIO | None= None
  • sorted: bool= False