Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
675 commits
Select commit Hold shift + click to select a range
1586bcb
Test GET dataset with metadata
summerhenson Apr 14, 2025
f166df1
Add comments to serializer classes
summerhenson Apr 14, 2025
cb9ab83
Fix serialization of DataSet with files
summerhenson Apr 14, 2025
2003e0e
Check access to files in a dataset
summerhenson Apr 14, 2025
299d236
Test DataSet creation with files
summerhenson Apr 14, 2025
ceac365
Filter files in dataset by access
summerhenson Apr 14, 2025
1943f93
Test loading dataset with files
summerhenson Apr 14, 2025
7e86678
Start session tests
summerhenson Apr 14, 2025
36a94ba
Copy session is_public value to composing datasets
summerhenson Apr 14, 2025
f90bafa
Test listing sessions
summerhenson Apr 14, 2025
816ef95
Fix bugs in DataSet creation nested in a Session
summerhenson Apr 15, 2025
051cb0f
Include username and authentication status in all post responses
summerhenson Apr 15, 2025
67a58d6
Disallow creating private sessions without an owner
summerhenson Apr 15, 2025
a015017
Test Session post method
summerhenson Apr 15, 2025
1ebefe8
Use the right serializer for sessions and return id with get
summerhenson Apr 15, 2025
5d0e25c
Test get method for individual sessions
summerhenson Apr 15, 2025
618a174
Test put method for individual sessions
summerhenson Apr 15, 2025
03a7b20
Test Session delete method
summerhenson Apr 15, 2025
9f2e100
Propagate access changes from session to composing datasets
summerhenson Apr 15, 2025
3f76d39
Test Session access management
summerhenson Apr 15, 2025
c5ba40f
Return current_user as username not id
summerhenson Apr 16, 2025
9b7c753
Comments of session testing
summerhenson Apr 16, 2025
1229ab2
Add field for history references to Quantity
summerhenson Apr 16, 2025
9d2e2e7
Serialization for Quantity variable references
summerhenson Apr 16, 2025
2c76297
Change serialization of QuantityHistory references to match API
summerhenson Apr 16, 2025
e85c67c
Serialize Quantity operation_tree and references as history
summerhenson Apr 16, 2025
a932e52
Remove infinite serialization loop of quantities and histories
summerhenson Apr 16, 2025
aa0f88c
Separate model for history reference quantities
summerhenson Apr 16, 2025
d778173
Test references in quantity history
summerhenson Apr 16, 2025
9793c62
Add tests to test updating a DataSet
summerhenson Apr 16, 2025
9b6c977
Propagate update to session is_public to datasets
summerhenson Apr 16, 2025
9d0e7b5
PublishedState serialization and minor serializer create method simpl…
summerhenson Apr 17, 2025
af2bbcc
Outline of PublishedState views
summerhenson Apr 17, 2025
8d26332
PublishedState list method
summerhenson Apr 17, 2025
c253c15
Serializer to restrict PublishedState update to published field
summerhenson Apr 17, 2025
a3347cc
Post method for PublishedState
summerhenson Apr 17, 2025
bc7c530
Get method for single PublishedState
summerhenson Apr 17, 2025
8725314
Update method for PublishedState
summerhenson Apr 17, 2025
e499ea3
Delete method for PublishedState
summerhenson Apr 17, 2025
e0ed9c0
Planning for PublishedState testing
summerhenson Apr 17, 2025
45402c4
PublishedState urls
summerhenson Apr 17, 2025
1e1c7b1
Test listing published states
summerhenson Apr 17, 2025
c3deba3
Fix bugs in PublishedState creation
summerhenson Apr 17, 2025
e1953f5
Check that session doesn't already have a published state
summerhenson Apr 17, 2025
d1a9099
Test creating a published state
summerhenson Apr 17, 2025
c213f6c
Add fields to PublishedState get response
summerhenson Apr 17, 2025
5a67a81
Test individual PublishedState get method
summerhenson Apr 17, 2025
4888486
Test PublishedState update method
summerhenson Apr 17, 2025
1fb3ef6
Test PublishedState delete method
summerhenson Apr 17, 2025
2055ad7
Session creation example script
summerhenson Apr 21, 2025
9aa8e25
Add models to admin interface
summerhenson Apr 21, 2025
66ef37e
Account for json strings in post requests
summerhenson Apr 21, 2025
636a0aa
Enable session-based authentication
summerhenson Apr 21, 2025
b115876
Comments for PublishedState tests
summerhenson Apr 21, 2025
5afd16c
Test that metadata is required for dataset creation
summerhenson Apr 21, 2025
68866a2
Minor changes suggested by Jeff
summerhenson Apr 21, 2025
cbe15ef
Nested PublishedState update in Session
summerhenson Apr 21, 2025
426f6d7
Test nested PublishedState
summerhenson Apr 21, 2025
d9bc070
Test listing published states by username
summerhenson Apr 22, 2025
e00b5d9
Deserialization methods for sasdata structures
summerhenson Apr 22, 2025
5998beb
Data sketch
lucas-wilkins Jul 31, 2024
e9a5d2b
Some units
lucas-wilkins Aug 5, 2024
233f383
Work towards outline
lucas-wilkins Aug 6, 2024
529bedc
Units now available and grouped
lucas-wilkins Aug 6, 2024
a7a77c5
More units
lucas-wilkins Aug 6, 2024
bb489d2
one d in shadow
lucas-wilkins Aug 6, 2024
7619dbc
Fixed density units
lucas-wilkins Aug 6, 2024
96e1f49
Use alias list to remove duplicates
lucas-wilkins Aug 6, 2024
f6863a5
More units, towards formatting
lucas-wilkins Aug 6, 2024
ebf6aa1
Units and accessors draft ready to begin tests on
lucas-wilkins Aug 7, 2024
a82e88d
Some tests
lucas-wilkins Aug 7, 2024
5903d8e
SI unit module
lucas-wilkins Aug 7, 2024
4cc3d2c
si unit list
lucas-wilkins Aug 7, 2024
f10d810
More tests, added names
lucas-wilkins Aug 7, 2024
1dc82d7
More units
lucas-wilkins Aug 7, 2024
6966098
Notes
lucas-wilkins Aug 7, 2024
c01fbf8
Notes
lucas-wilkins Aug 8, 2024
10eaeac
start of metadata structure
lucas-wilkins Aug 8, 2024
9645fbd
Metadata work, and unit groups
lucas-wilkins Aug 13, 2024
3fac420
More metadata stuff
lucas-wilkins Aug 13, 2024
ee6d8a9
Named units in unit groups
lucas-wilkins Aug 13, 2024
d276e99
More metadata, added absolute temperature stuff
lucas-wilkins Aug 14, 2024
9599d68
Metadata objects complete for now
lucas-wilkins Aug 14, 2024
1c0c0eb
Percent test and fix
lucas-wilkins Aug 14, 2024
29bed57
Work towards new data object
lucas-wilkins Aug 21, 2024
6b6c97a
Basic reading
lucas-wilkins Aug 28, 2024
3e4fd6b
Work towards structuring inputs with uncertainties
lucas-wilkins Aug 29, 2024
499af49
Work on uncertainty propagation
lucas-wilkins Sep 9, 2024
dd9615e
Added some code to enable test driven development.
Sep 11, 2024
a19f531
Some minor changes to stop my editor from crying.
Sep 11, 2024
727b30e
Pass in the dimensions so this code is correct.
Sep 11, 2024
2b06236
Wrote some tests ahead.
Sep 11, 2024
e7e7830
Parse using a slant as well.
Sep 11, 2024
ad8adda
Found a regex for splitting up the string.
Sep 11, 2024
7f9124d
Implemented the parse_single_unit function.
Sep 11, 2024
489a326
Use two functions for parsing.
Sep 11, 2024
524483f
Use list comprehension to get potential symbols.
Sep 12, 2024
9212720
parse unit strs function.
Sep 12, 2024
62350f4
Created a function to pass in a stack of units.
Sep 12, 2024
de6e4f5
Multiply dimensions function.
Sep 12, 2024
e42baf3
Use the new multiply function.
Sep 12, 2024
69c2124
Nvm I'm blind; there already was a multiply method.
Sep 12, 2024
d42eb27
Parse in a whole unit.
Sep 12, 2024
ffb47b1
I still need this multply for parse_unit_stack.
Sep 12, 2024
b83fe9d
System for combining units.
Sep 16, 2024
02306d7
Removed not implemented comment.
Sep 16, 2024
2dc8d87
Parse in a named unit.
Sep 16, 2024
43439cd
Avoid mutating state.
Sep 16, 2024
cf7b65b
Replace the unit on the stack.
Sep 17, 2024
18a5a94
Fixed the logic around combining units.
Sep 17, 2024
b5f5c7f
Parse_name_unit can take in an already parsed unit.
Sep 17, 2024
2c827fc
Added a todo comment.
Sep 17, 2024
cc0477a
Take a unit from the command line.
Sep 17, 2024
a4eb3d3
Added whitespace on input.
Sep 17, 2024
b1b19d1
Fixed typo.
Sep 17, 2024
e1ad375
Only multiply scale by 1, or -1.
Sep 17, 2024
5817d5a
Look for slashes in the string.
Sep 17, 2024
9450879
Got fraction units working as well :)
Sep 17, 2024
60b9587
Configure how ambiguities are dealt with.
Sep 18, 2024
1a25256
Only break if we have found a symbol.
Sep 18, 2024
a8f2622
Take in longest unit across the whole file.
Sep 18, 2024
0c0a8fb
Take in a unit group in parse_singe_unit.
Sep 19, 2024
106aa71
Parse a unit from a specific group.
Sep 19, 2024
e75fb07
Equivalent function for from group.
Sep 19, 2024
c5c6d8a
Is none not equal to none.
Sep 19, 2024
350e736
Removed old TODO comment.
Sep 19, 2024
8952fe4
Catch key errors.
Sep 19, 2024
729825d
Expand the try block.
Sep 19, 2024
3b44869
Removed an old todo comment.
Sep 19, 2024
7fd7eed
New unit test in pytest.
Sep 20, 2024
dae132c
Raise an exception if the unit can't be parsed.
Sep 20, 2024
64a877f
Added some unit tests that should error.
Sep 20, 2024
9f4c2ba
Created a regex validator for the unit str.
Sep 20, 2024
837c285
Throw an exception if the validation fails.
Sep 20, 2024
ece9561
Update unit test to reflect new error.
Sep 20, 2024
ab18563
Unit test for what I was originally testing for.
Sep 20, 2024
218bf09
Added more tests for slants.
Sep 23, 2024
368b9e5
Slants should be valid unit strings as well.
Sep 23, 2024
0d2c5d9
Parse in newton as its defined value.
Sep 23, 2024
5f8b174
Remove the old testing file.
Sep 23, 2024
a582638
This function isn't being used.
Sep 23, 2024
e51d38c
Added to the doc string about unit groups.
Sep 23, 2024
d8009b1
Moved the unit group to first.
Sep 23, 2024
76c92ef
Small rename.
Sep 23, 2024
baa63cd
Use destructuring to make this a bit cleaner.
Sep 23, 2024
91e3b86
Fixed function call.
Sep 23, 2024
4196cc6
Added some docstrings.
Sep 23, 2024
1c4b2ef
Refactored parse named unit so it just takes one arg.
Sep 25, 2024
1b5e73b
Removed old todo comment.
Sep 25, 2024
bd7687a
Added some docstrings.
Sep 25, 2024
3ecbad9
Stop linter from moaning.
Sep 25, 2024
550619f
Accept spaces in the unit str.
Sep 30, 2024
d19824b
Split by dots as well.
Sep 30, 2024
aad07a8
Work on adding uncertainties, adding non-integer powers
lucas-wilkins Sep 23, 2024
86bd404
Integer unit powers now work
lucas-wilkins Sep 23, 2024
c88b808
Quantities now have histories, and variance could work, needs testing…
lucas-wilkins Sep 25, 2024
63b8c41
Quantities ready for testing
lucas-wilkins Sep 26, 2024
2caadc7
Quantity combining seems to work
lucas-wilkins Sep 27, 2024
a90a92d
Fixed error in helper function
lucas-wilkins Sep 27, 2024
40b7209
Fixed error formatting bug
lucas-wilkins Sep 27, 2024
7edc510
Tests for error propagation
lucas-wilkins Sep 30, 2024
6a34c4a
More aliases for units
lucas-wilkins Sep 30, 2024
fd5a6d5
Made file for target object for metadata
lucas-wilkins Sep 30, 2024
b8424c9
Main data reading for HDF5 prototype
lucas-wilkins Sep 30, 2024
2dabda2
integrating the units stuff
lucas-wilkins Oct 1, 2024
6a6f0bd
Parsing of units in HDF5 reader
lucas-wilkins Oct 1, 2024
93bc62a
Fixed moles potentially
lucas-wilkins Oct 2, 2024
f615426
Unit name fixes
lucas-wilkins Oct 2, 2024
4af3da3
Filling in some of the working for the accessors
lucas-wilkins Oct 3, 2024
d0cf6b1
Fixed bug where ohms, and angstroms were forbidden
Oct 2, 2024
bda8b9a
Accept the ^ char but don't do anything with it.
Oct 2, 2024
b5c1409
Connecting metadata
lucas-wilkins Oct 7, 2024
297a2ae
Remove target data object attempt
lucas-wilkins Oct 3, 2024
5a2bf05
Accessor changes
lucas-wilkins Oct 7, 2024
74864f0
Merge tidying
lucas-wilkins Oct 7, 2024
f6c30d6
Metadata linked up, just not pointing in the right place right now
lucas-wilkins Oct 7, 2024
49e1db1
Added some debugging info to the summary
lucas-wilkins Oct 7, 2024
e286cc7
Debugging metadata
lucas-wilkins Oct 7, 2024
8d13974
Better reference methods
lucas-wilkins Oct 7, 2024
ec3c459
Added matrix operations, needs tests
lucas-wilkins Oct 10, 2024
ecc81b5
Numpy import
lucas-wilkins Oct 10, 2024
a86fa70
Added __matmul__ and __rmatmul__ to quantities
lucas-wilkins Oct 10, 2024
5316f79
Entrypoint for rebinning
lucas-wilkins Oct 10, 2024
f574609
Some better commenting on Quantity
lucas-wilkins Oct 10, 2024
7681e95
Work towards rebinning methods
lucas-wilkins Oct 10, 2024
1a1fc0c
Zeroth order rebinning sketch
lucas-wilkins Oct 11, 2024
6d54f27
First order rebinning
lucas-wilkins Oct 11, 2024
6ead275
Rebinning tests and extensions
lucas-wilkins Oct 15, 2024
9785da5
Work towards fraction binning
lucas-wilkins Sep 20, 2023
47c08e6
Mesh merging and some refactoring
lucas-wilkins Sep 22, 2023
3e43907
Triangulated mesh
lucas-wilkins Sep 24, 2023
3ac0415
Mesh merging works
lucas-wilkins Sep 25, 2023
67039a8
Implementation of Rebinner base class
lucas-wilkins Sep 28, 2023
5eb1169
Work towards demo
lucas-wilkins Sep 28, 2023
b130f4a
Voronoi mesh edges and ordering
lucas-wilkins Sep 28, 2023
c821709
It works, needs benchmarking
lucas-wilkins Sep 28, 2023
b0fecd7
Much faster assignment/merge method
lucas-wilkins Sep 29, 2023
bde9885
Significantly faster edge crossing algorithm
lucas-wilkins Oct 5, 2023
52b5edd
Demo
lucas-wilkins Oct 5, 2023
875818d
Notes
lucas-wilkins Oct 15, 2024
ccf9337
No error
lucas-wilkins Oct 15, 2024
5ff8041
Interpolation stuff
lucas-wilkins Oct 22, 2024
5c2ffcb
Moving some things around
lucas-wilkins Oct 17, 2024
59e6913
Move math and operations into quantity
lucas-wilkins Oct 21, 2024
f536d4e
Fixes from move
lucas-wilkins Oct 21, 2024
a688dd6
Tensor product implementation
lucas-wilkins Oct 21, 2024
0c3071d
Extended transpose, and tensor tests
lucas-wilkins Oct 21, 2024
cdc99fe
Encodings for numerical values
lucas-wilkins Oct 22, 2024
e58e6f8
Tidying up
lucas-wilkins Oct 22, 2024
a702e67
Work on sparse matrix serialisation
lucas-wilkins Oct 23, 2024
25db055
Is anyone capable of putting sensible things in HDF5 files? Correctio…
lucas-wilkins Oct 9, 2024
06910b3
Line endings :)
lucas-wilkins Oct 23, 2024
26a6f9c
Updated final line endings
lucas-wilkins Oct 23, 2024
1df8506
Fix test import
lucas-wilkins Oct 23, 2024
bfca9f5
Work on tests for sparse matrix encoding
lucas-wilkins Oct 25, 2024
5ea206c
Fix tests
lucas-wilkins Jan 9, 2025
7b5956a
Disable machine specific mesh test for MacOS
lucas-wilkins Jan 9, 2025
72247b0
Make import paths absolute.
jamescrake-merani Jan 28, 2025
bdafc0b
Fix import of _units_base by _build_tables.py
rprospero Feb 17, 2025
f6e4e9b
Add latex symbols to units which need them
rprospero Feb 17, 2025
16d5dd9
Started with the basic ascii reader func signature
Oct 18, 2024
9763bd8
Fixed import errors.
Oct 21, 2024
8759b98
Moving internal metadata to sasdata.
Dec 3, 2024
cd6850a
A very basic skeleton of the trend object.
Dec 20, 2024
0b757ec
Implemented equality based on the hash.
Dec 20, 2024
1baa900
Wrote a test for the trend.
Dec 20, 2024
fa771df
Use full function name here.
jamescrake-merani Jan 15, 2025
6a6971e
Add with standard error for quantities.
jamescrake-merani Jan 17, 2025
4e853aa
Wrote a test for 1d data.
jamescrake-merani Mar 5, 2025
ebac6a0
Bring guess into sasdata.
Feb 12, 2025
0f51347
Bring default logic from SasView here.
Feb 12, 2025
747a6ad
Wrote a test for the ASCII reader.
Feb 12, 2025
29c5867
Create test option for displaying diagnostic plots
rprospero Feb 17, 2025
19ab285
More interpolation test into main test directory
rprospero Feb 17, 2025
cc26e1b
Add tests for dataset
rprospero Mar 4, 2025
eaa0595
Parse source
rprospero Mar 17, 2025
db349ee
Instrument is a data class
rprospero Mar 17, 2025
b093660
Metadata is a dataclass
rprospero Mar 17, 2025
029b02f
Start adding official tests for xml file loading
rprospero Apr 4, 2025
d34c74a
Better work at handling comments
rprospero Apr 4, 2025
a4d58d3
More tests of xml
rprospero Apr 4, 2025
b7bd79b
Mark weird units, but continue parsing
rprospero Apr 4, 2025
8b130c3
Add last xml test
rprospero Apr 4, 2025
554ca72
Fix parsing of BeamSize
rprospero Apr 4, 2025
796e65e
Skeleton framework for SESANS data
rprospero May 6, 2025
1f6dae0
Multiple SESANS files in reader test
rprospero May 6, 2025
79c973f
SESANS metadata as a process, not an aperture
rprospero May 7, 2025
87ca591
More tests for quantities
rprospero Jun 10, 2025
1c4aacc
Applies auto fixes for ruff rule F401
DrPaulSharp Jul 17, 2025
b6c0eae
[pre-commit.ci lite] apply automatic fixes for ruff linting errors
pre-commit-ci-lite[bot] Mar 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/test-fair-database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Tests

on:
[push, pull_request]

defaults:
run:
shell: bash

jobs:
unit-test:

runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ['3.12']
fail-fast: false

steps:

- name: Obtain SasData source from git
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
cache-dependency-path: |
**/test.yml
**/requirements*.txt

### Installation of build-dependencies

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install wheel setuptools
python -m pip install -r requirements.txt
python -m pip install -r sasdata/fair_database/requirements.txt

### Build and test sasdata

- name: Build sasdata
run: |
# BUILD SASDATA
python -m pip install -e .

### Build documentation (if enabled)

- name: Test with Django tests
run: |
python sasdata/fair_database/manage.py test sasdata.fair_database
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
**/build
/dist
.mplconfig
**/db.sqlite3

# INSTALL.md recommends a venv that should not be committed
venv
Expand Down
27 changes: 18 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
default_install_hook_types: [pre-commit, pre-push]

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.9
hooks:
# Run the linter, applying any available fixes
- id: ruff-check
stages: [ pre-commit, pre-push ]
args: [ --fix-only ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
files: "sasdata/fair_database/.*"
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.2
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
files: "sasdata/fair_database/.*"
- id: ruff-format
files: "sasdata/fair_database/.*"
Comment on lines +10 to +14
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
- id: codespell
files: "sasdata/fair_database/.*"
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ html5lib

# Other stuff
matplotlib
pre-commit
86 changes: 4 additions & 82 deletions sasdata/ascii_reader_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from dataclasses import dataclass, field
from re import split as re_split
from typing import TypeVar

initial_metadata = {
Expand All @@ -10,27 +10,11 @@
'process': ['name', 'date', 'description', 'term', 'notes'],
'sample': ['name', 'sample_id', 'thickness', 'transmission', 'temperature', 'position', 'orientation', 'details'],
'transmission_spectrum': ['name', 'timestamp', 'transmission', 'transmission_deviation'],
'magnetic': ['demagnetizing_field', 'saturation_magnetization', 'applied_magnetic_field', 'counting_index'],
'other': ['title', 'run', 'definition']
}

CASING_REGEX = r'[A-Z][a-z]*'

# First item has the highest precedence.
SEPARATOR_PRECEDENCE = [
'_',
'-',
]
# If none of these characters exist in that string, use casing. See init_separator

T = TypeVar('T')

# TODO: There may be a better place for this.
pairings = {'I': 'dI', 'Q': 'dQ', 'Qx': 'dQx', 'Qy': 'dQy', 'Qz': 'dQz'}
pairing_error = {value: key for key, value in pairings.items()}
# Allows this to be bidirectional.
bidirectional_pairings = pairings | pairing_error

@dataclass
class AsciiMetadataCategory[T]:
values: dict[str, T] = field(default_factory=dict)
Expand All @@ -42,68 +26,13 @@ def default_categories() -> dict[str, AsciiMetadataCategory[str | int]]:
class AsciiReaderMetadata:
# Key is the filename.
filename_specific_metadata: dict[str, dict[str, AsciiMetadataCategory[str]]] = field(default_factory=dict)
# True instead of str means use the casing to separate the filename.
filename_separator: dict[str, str | bool] = field(default_factory=dict)
filename_separator: dict[str, str] = field(default_factory=dict)
master_metadata: dict[str, AsciiMetadataCategory[int]] = field(default_factory=default_categories)

def init_separator(self, filename: str):
separator = next(filter(lambda c: c in SEPARATOR_PRECEDENCE, filename), True)
self.filename_separator[filename] = separator

def filename_components(self, filename: str, cut_off_extension: bool = True, capture: bool = False) -> list[str]:
"""Split the filename into several components based on the current separator for that file."""
separator = self.filename_separator[filename]
# FIXME: This sort of string construction may be an issue. Might need an alternative.
base_str = '({})' if capture else '{}'
if isinstance(separator, str):
splitted = re.split(base_str.replace('{}', separator), filename)
else:
splitted = re.findall(base_str.replace('{}', CASING_REGEX), filename)
# If the last component has a file extensions, remove it.
last_component = splitted[-1]
if cut_off_extension and '.' in last_component:
pos = last_component.index('.')
last_component = last_component[:pos]
splitted[-1] = last_component
return splitted

def purge_unreachable(self, filename: str):
"""This is used when the separator has changed. If lets say we now have 2 components when there were 5 but the
3rd component was selected, this will now produce an index out of range exception. Thus we'll need to purge this
to stop exceptions from happening."""
components = self.filename_components(filename)
component_length = len(components)
# Converting to list as this mutates the dictionary as it goes through it.
for category_name, category in list(self.master_metadata.items()):
for key, value in list(category.values.items()):
if value >= component_length:
del self.master_metadata[category_name].values[key]
def filename_components(self, filename: str) -> list[str]:
return re_split(self.filename_separator[filename], filename)

def all_file_metadata(self, filename: str) -> dict[str, AsciiMetadataCategory[str]]:
"""Return all of the metadata for known for the specified filename. This
will combin the master metadata specified for all files with the
metadata specific to that filename."""
file_metadata = self.filename_specific_metadata[filename]
components = self.filename_components(filename)
# The ordering here is important. If there are conflicts, the second dictionary will override the first one.
# Conflicts shouldn't really be happening anyway but if they do, we're gonna go with the master metadata taking
# precedence for now.
return_metadata: dict[str, AsciiMetadataCategory[str]] = {}
for category_name, category in (file_metadata | self.master_metadata).items():
combined_category_dict = category.values | self.master_metadata[category_name].values
new_category_dict: dict[str, str] = {}
for key, value in combined_category_dict.items():
if isinstance(value, str):
new_category_dict[key] = value
elif isinstance(value, int):
new_category_dict[key] = components[value]
else:
raise TypeError(f'Invalid value for {key} in {category_name}')
new_category = AsciiMetadataCategory(new_category_dict)
return_metadata[category_name] = new_category
return return_metadata
def get_metadata(self, category: str, value: str, filename: str, error_on_not_found=False) -> str | None:
"""Get a particular piece of metadata for the filename."""
components = self.filename_components(filename)

# We prioritise the master metadata.
Expand All @@ -122,10 +51,6 @@ def get_metadata(self, category: str, value: str, filename: str, error_on_not_fo
return None

def update_metadata(self, category: str, key: str, filename: str, new_value: str | int):
"""Update the metadata for a filename. If the new_value is a string,
then this new metadata will be specific to that file. Otherwise, if
new_value is an integer, then this will represent the component of the
filename that this metadata applies to all."""
if isinstance(new_value, str):
self.filename_specific_metadata[filename][category].values[key] = new_value
# TODO: What about the master metadata? Until that's gone, that still takes precedence.
Expand All @@ -135,15 +60,12 @@ def update_metadata(self, category: str, key: str, filename: str, new_value: str
raise TypeError('Invalid type for new_value')

def clear_metadata(self, category: str, key: str, filename: str):
"""Remove any metadata recorded for a certain filename."""
category_obj = self.filename_specific_metadata[filename][category]
if key in category_obj.values:
del category_obj.values[key]
if key in self.master_metadata[category].values:
del self.master_metadata[category].values[key]

def add_file(self, new_filename: str):
"""Add a filename to the metadata, filling it with some default
categories."""
# TODO: Fix typing here. Pyright is showing errors.
self.filename_specific_metadata[new_filename] = default_categories()
Loading
Loading