Changeset 4af65b0 in flexoentity


Ignore:
Timestamp:
11/04/25 20:20:01 (2 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
73d392f
Parents:
bf30018
Message:

initial logging support

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • flexoentity/__init__.py

    rbf30018 r4af65b0  
    1010from .flexo_entity import FlexoEntity, EntityType, EntityState
    1111from .domain import Domain
     12import logging
     13import logging.config
     14import os
     15from pathlib import Path
    1216
    1317__all__ = [
     
    2024]
    2125
     26# FIXME: change the default log path to /var/log/ammos/ammos.log when system
     27# configuration created this dir with the appropriate rights
     28
     29log_dir = Path(os.environ.get('FLEXO_LOG_DIR', '/tmp/'))
     30
     31log_path = log_dir / 'flexo.log'
     32
     33if not log_path.exists():
     34    try:
     35        log_path.touch()
     36    except PermissionError:
     37        conf_file = Path(__file__).parent / 'flexo_logging.conf'
     38        print("Conf file", conf_file)
     39        logging.config.fileConfig(conf_file)
     40else:
     41    logging.basicConfig(filename=str(log_path), encoding='utf-8', level=logging.DEBUG)
     42
     43logger = logging.getLogger(__name__)
     44
     45logger.setLevel(os.environ.get('FLEXO_LOG_LEVEL', logger.getEffectiveLevel()))
    2246# Optional: keep dynamic version retrieval synced with pyproject.toml
    2347try:
  • flexoentity/flexo_entity.py

    rbf30018 r4af65b0  
    7575    """
    7676    I map a fixed number of entity types to a single unique letter,
    77     that structures different types 
     77    that structures different types
    7878    """
    79     GENERIC =  "G"
     79    GENERIC = "G"
    8080    DOMAIN = "D"
    8181    MEDIA = "M"
     
    288288            EntityState.PUBLISHED,
    289289        )
     290
    290291    def _compute_fingerprint(self) -> str:
    291292        """I always recompute the entity's content fingerprint."""
     
    302303            self.fingerprint = new_fp
    303304            self.flexo_id = FlexOID.safe_generate(self.domain_code(),
    304                                              self.entity_type.value,
    305                                              self.state.value,
    306                                              self.text_seed,
    307                                              self.flexo_id.version)
     305                                                  self.entity_type.value,
     306                                                  self.state.value,
     307                                                  self.text_seed,
     308                                                  self.flexo_id.version)
    308309            return True
    309310        return False
     
    367368        if self.state == EntityState.DRAFT:
    368369            new_fid = FlexOID.safe_generate(self.domain_code(),
    369                 self.entity_type.value,
    370                 EntityState.APPROVED.value,
    371                 self.text_seed,
    372                 version=self.version
    373             )
     370                                            self.entity_type.value,
     371                                            EntityState.APPROVED.value,
     372                                            self.text_seed,
     373                                            version=self.version
     374                                            )
    374375            self.origin = self.flexo_id  # optional: keep audit trail
    375376            self.flexo_id = new_fid
     
    377378        raise ValueError("Only drafts can be approved")
    378379
    379 
    380380    def sign(self):
    381381        """
     
    394394        """
    395395        I move from APPROVED or APPROVED_AND_SIGNED to PUBLISHED.
    396        
     396
    397397        Uses allowed_transitions() to verify legality,
    398398        then performs a version bump and lineage update.
     
    469469            info["stored_fingerprint"] = getattr(entity, "fingerprint", None)
    470470
    471             # Expected ID hash from current seed (should match fid.hash_part if content didn't change)
     471            # Expected ID hash from current seed (should match fid.hash_part
     472            # if content didn't change)
    472473            exp_hash = FlexOID._blake_hash(canonical_seed(f"{fid.domain}:{fid.entity_type}:{entity.text_seed}"))
    473474            info["expected_id_hash_from_seed"] = exp_hash
     
    507508            # --- Fingerprint validation ---
    508509            if hasattr(entity, "fingerprint") and entity.fingerprint:
    509                 seed = canonical_seed(entity.text_seed)
    510510                expected_fp = entity._compute_fingerprint()
    511511                if expected_fp != entity.fingerprint:
  • flexoentity/id_factory.py

    rbf30018 r4af65b0  
    7676import json
    7777from datetime import datetime, timezone
    78 from logging import Logger
    79 
    80 logger = Logger(__file__)
     78from flexoentity import logger
    8179
    8280def canonical_seed(obj) -> str:
  • tests/test_id_lifecycle.py

    rbf30018 r4af65b0  
    7979    # simulate tampering
    8080    q.text = "Tampered text"
    81     print(FlexoEntity.debug_integrity(q))
    8281    assert not FlexoEntity.verify_integrity(q)
    8382
  • tests/test_persistance_integrity.py

    rbf30018 r4af65b0  
    3434    """
    3535    json_str = approved_question.to_json()
    36     print("JSON", json_str)
    3736    loaded = SingleChoiceQuestion.from_json(json_str)
    3837
    39     print("Approved", approved_question.text_seed)
    40     print("Loaded", loaded.text_seed)
    4138    # Fingerprint and state should match — integrity must pass
    4239    assert SingleChoiceQuestion.verify_integrity(loaded)
Note: See TracChangeset for help on using the changeset viewer.