-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
530 lines (431 loc) · 18.6 KB
/
main.py
File metadata and controls
530 lines (431 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from __future__ import annotations
"""(MSC) Morphological Source Code Framework – V0.0.12
================================================================================
<https://github.com/Phovos/msc> • MSC: Morphological Source Code © 2025 by Phovos
Standard Library Imports - 3.13 std libs **ONLY**"""
import re
import sys
import math
import enum
import logging
import logging.config
import argparse
import platform
import multiprocessing
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from collections.abc import Iterable
from functools import reduce
from typing import Any, Dict, List, Optional, Union, Callable, TypeVar, Set, Type
try:
if platform.system() == "Windows":
from ctypes import windll, byref, wintypes
# from ctypes.wintypes import HANDLE, DWORD, LPWSTR, LPVOID, BOOL
from pathlib import PureWindowsPath
class WindowsConsole:
"""Enable ANSI escape sequences on Windows consoles."""
@staticmethod
def enable_ansi() -> None:
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
kernel32 = windll.kernel32
handle = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
mode = wintypes.DWORD()
if kernel32.GetConsoleMode(handle, byref(mode)):
new_mode = mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING
kernel32.SetConsoleMode(handle, new_mode)
else:
raise RuntimeError("Failed to get console mode for enabling ANSI.")
try:
WindowsConsole.enable_ansi()
except Exception as e:
print(
f"Failed to enable ANSI escape codes on Windows console: {e}",
file=sys.stderr,
)
sys.exit(1)
except (ImportError, OSError, RuntimeError) as e:
# If enabling ANSI fails (e.g., not a real console), print a warning
# but don't exit. The program can run, just without colors.
print(
f"Warning: Failed to enable ANSI escape codes on Windows: {e}", file=sys.stderr
)
@dataclass
class LogAdapter:
"""
Sets up console + file + broadcast + (optional) queue handlers, and exposes a correlation-aware logger instance. You can customize handler levels and output filenames at instantiation time.
---
| Token | Meaning |
| ----------------- | -------------------------------------------- |
| `%(asctime)s` | Timestamp |
| `%(name)s` | Logger name (`__name__`) |
| `%(levelname)s` | Log level name |
| `%(message)s` | The actual message |
| `%(filename)s` | File name where the log is emitted |
| `%(lineno)d` | Line number of the log statement |
| `%(funcName)s` | Function name |
| `%(threadName)s` | Thread name (super useful w/ `threading`) |
| `%(processName)s` | Process name (helpful for `multiprocessing`) |
---
"""
console_level: str = "INFO"
file_filename: str = "app.log"
file_level: str = "INFO"
broadcast_filename: str = "broadcast.log"
broadcast_level: str = "INFO"
queue_size: Optional[int] = (
None # Set to -1 for infinite, or a positive int for a sized queue
)
correlation_id: str = "SYSTEM"
LOGGING_CONFIG: dict = field(init=False)
logger: logging.Logger = field(init=False)
def __post_init__(self):
self.LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[%(levelname)s] %(asctime)s | [%(filename)s:%(lineno)d]: %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
'color': {
'()': self.ColorFormatter,
'format': '[%(levelname)s] %(asctime)s | [%(filename)s:%(lineno)d]: %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'level': self.console_level,
'formatter': 'color',
'stream': 'ext://sys.stdout',
},
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'level': self.file_level,
'formatter': 'default',
'filename': self.file_filename,
'maxBytes': 10 * 1024 * 1024,
'backupCount': 5,
'encoding': 'utf-8',
},
'broadcast': {
'class': 'logging.handlers.RotatingFileHandler',
'level': self.broadcast_level,
'formatter': 'default',
'filename': self.broadcast_filename,
'maxBytes': 10 * 1024 * 1024,
'backupCount': 5,
'encoding': 'utf-8',
},
},
'root': {
'level': 'INFO',
# This will be determined by whether a queue is used or not
'handlers': [],
},
}
# The list of handlers that do the actual work (writing to console/file)
destination_handlers = ['console', 'file', 'broadcast']
if self.queue_size is not None:
# If a queue is used, configure it and make it the ONLY handler for the root logger.
# The QueueHandler itself will then dispatch to the destination handlers.
self.LOGGING_CONFIG['handlers']['queue'] = {
'class': 'logging.handlers.QueueHandler',
# This is the crucial missing piece:
'handlers': destination_handlers,
'queue': multiprocessing.Queue(self.queue_size),
}
self.LOGGING_CONFIG['root']['handlers'] = ['queue']
else:
# If no queue is used, the root logger sends directly to the destination handlers.
self.LOGGING_CONFIG['root']['handlers'] = destination_handlers
logging.config.dictConfig(self.LOGGING_CONFIG)
base_logger = logging.getLogger(__name__)
self.logger = self.CorrelationLogger(base_logger, {"cid": self.correlation_id})
self.logger.info("Logger initialized.")
class CorrelationLogger(logging.LoggerAdapter):
def process(self, msg: str, kwargs: Any) -> tuple[str, Any]:
cid = self.extra.get("cid", "SYSTEM")
return f"[{cid}] {msg}", kwargs
class ColorFormatter(logging.Formatter):
_COLORS = {
logging.DEBUG: "\033[34m", # Blue
logging.INFO: "\033[32m", # Green
logging.WARNING: "\033[33m", # Yellow
logging.ERROR: "\033[31m", # Red
logging.CRITICAL: "\033[41m", # Red background
}
_RESET = "\033[0m"
def format(self, record: logging.LogRecord) -> str:
base = super().format(record)
color = self._COLORS.get(record.levelno, self._COLORS[logging.DEBUG])
return f"{color}{base}{self._RESET}"
T = TypeVar('T')
V = TypeVar('V')
C = TypeVar('C')
# Global Registry for Morphological Classes and Functions
MSC_REGISTRY: Dict[str, Set[str]] = {'classes': set(), 'functions': set()}
# Exception for Morphodynamic Collapse
class MorphodynamicCollapse(Exception):
"""Raised when a morph object destabilizes under thermal pressure."""
pass
# MorphSpec Blueprint for Morphological Classes
@dataclass
class MorphSpec:
"""Blueprint for morphological classes."""
entropy: float
trigger_threshold: float
memory: dict
signature: str
# Morphology Decorator for Class Registration and Validation
def morphology(source_model: Type) -> Callable[[Type], Type]:
"""Decorator: register & validate a class against a MorphSpec."""
def decorator(target: Type) -> Type:
target.__msc_source__ = source_model
# Ensure target has all annotated fields from source_model
for field_name in getattr(source_model, '__annotations__', {}):
if field_name not in getattr(target, '__annotations__', {}):
raise TypeError(f"{target.__name__} missing field '{field_name}'")
MSC_REGISTRY['classes'].add(target.__name__)
return target
return decorator
# MorphicComplex: Complex Numbers with Morphic Properties
@dataclass
class MorphicComplex:
"""Represents a complex number with morphic properties."""
def __init__(self, real: float, imag: float):
self.real = real
self.imag = imag
def conjugate(self) -> 'MorphicComplex':
"""Return the complex conjugate."""
return MorphicComplex(self.real, -self.imag)
def __add__(self, other: 'MorphicComplex') -> 'MorphicComplex':
"""Add two MorphicComplex numbers."""
return MorphicComplex(self.real + other.real, self.imag + other.imag)
def __mul__(self, other: 'MorphicComplex') -> 'MorphicComplex':
"""Multiply two MorphicComplex numbers."""
return MorphicComplex(
self.real * other.real - self.imag * other.imag,
self.real * other.imag + self.imag * other.real,
)
def __eq__(self, other: 'MorphicComplex') -> bool:
"""Check equality between two MorphicComplex numbers."""
return (
isinstance(other, MorphicComplex)
and self.real == other.real
and self.imag == other.imag
)
def __repr__(self) -> str:
"""String representation of the MorphicComplex number."""
if self.imag == 0:
return f"{self.real}"
sign = "+" if self.imag >= 0 else ""
return f"{self.real}{sign}{self.imag}j"
# Morphological Rule for Symbolic Rewriting
class MorphologicalRule:
def __init__(self, symmetry: str, conservation: str, lhs: str, rhs: List[str]):
self.symmetry = symmetry
self.conservation = conservation
self.lhs = lhs
self.rhs = rhs
def apply(self, seq: List[str]) -> List[str]:
"""Apply the rule to a sequence."""
if self.lhs in seq:
idx = seq.index(self.lhs)
return seq[:idx] + self.rhs + seq[idx + 1 :]
return seq
# Enumerations for Quantum States and Entanglement Types
class QState(enum.Enum):
SUPERPOSITION = "superposition"
ENTANGLED = "entangled"
COLLAPSED = "collapsed"
DECOHERENT = "decoherent"
EIGENSTATE = "eigenstate"
class EntanglementType(enum.Enum):
CODE_LINEAGE = "code_lineage"
TEMPORAL_SYNC = "temporal_sync"
SEMANTIC_BRIDGE = "semantic_bridge"
PROBABILITY_FIELD = "probability_field"
# Kronecker Field Function for Quantum Coherence
def kronecker_field(q1: Any, q2: Any, temperature: float) -> float:
dot = sum(a * b for a, b in zip(q1.state.vector, q2.state.vector, strict=False))
if temperature > 0.5:
return math.cos(dot)
return 1.0 if dot > 0.99 else 0.0
# Elevate Function for Morphological Class Instantiation
def elevate(data: Any, cls: Type) -> object:
"""Raise a dict or object to a registered morphological class."""
if not hasattr(cls, '__msc_source__'):
raise TypeError(f"{cls.__name__} is not a morphological class.")
source = cls.__msc_source__
kwargs = {k: getattr(data, k, data.get(k)) for k in source.__annotations__}
return cls(**kwargs)
# Functional Programming Patterns - Transducers
class Reduced:
"""Sentinel class to signal early termination during reduction."""
def __init__(self, val: Any):
self.val = val
def ensure_reduced(x: Any) -> Union[Any, Reduced]:
"""Ensure the value is wrapped in a Reduced sentinel."""
return x if isinstance(x, Reduced) else Reduced(x)
def unreduced(x: Any) -> Any:
"""Unwrap a Reduced value or return the value itself."""
return x.val if isinstance(x, Reduced) else x
def reduce(
function: Callable[[Any, T], Any], iterable: Iterable[T], initializer: Any = None
) -> Any:
"""A custom reduce implementation that supports early termination with Reduced."""
accum_value = initializer if initializer is not None else function()
for x in iterable:
accum_value = function(accum_value, x)
if isinstance(accum_value, Reduced):
return accum_value.val
return accum_value
# Base Transducer Class
class Transducer(ABC):
"""Base class for defining transducers."""
@abstractmethod
def __call__(self, step: Callable[[Any, T], Any]) -> Callable[[Any, T], Any]:
"""The transducer's __call__ method allows it to be used as a decorator."""
pass
class Map(Transducer):
"""Transducer for mapping elements with a function."""
def __init__(self, f: Callable[[T], Any]):
self.f = f
def __call__(self, step: Callable[[Any, T], Any]) -> Callable[[Any, T], Any]:
def new_step(r, x):
return step(r, self.f(x))
return new_step
class Filter(Transducer):
"""Transducer for filtering elements based on a predicate."""
def __init__(self, pred: Callable[[T], bool]):
self.pred = pred
def __call__(self, step: Callable[[Any, T], Any]) -> Callable[[Any, T], Any]:
def new_step(r, x):
return step(r, x) if self.pred(x) else r
return new_step
class Cat(Transducer):
"""Transducer for flattening nested collections."""
def __call__(self, step: Callable[[Any, T], Any]) -> Callable[[Any, T], Any]:
def new_step(r, x):
if not hasattr(x, '__iter__'):
raise TypeError(f"Expected iterable, got {type(x)} with value {x}")
result = r
for item in x:
result = step(result, item)
if isinstance(result, Reduced):
return result
return result
return new_step
# Utility Functions for Transducers
def compose(*fns: Callable[[Any], Any]) -> Callable[[Any], Any]:
"""Compose functions in reverse order."""
return reduce(lambda f, g: lambda x: f(g(x)), reversed(fns))
def transduce(
xform: Transducer, f: Callable[[Any, T], Any], start: Any, coll: Iterable[T]
) -> Any:
"""Apply a transducer to a collection with an initial value."""
reducer = xform(f)
return reduce(reducer, coll, start)
def mapcat(f: Callable[[T], Iterable[Any]]) -> Transducer:
"""Map then flatten results into one collection."""
return compose(Map(f), Cat())
def into(target: Union[list, set], xducer: Transducer, coll: Iterable[T]) -> Any:
"""Apply transducer and collect results into a target container."""
def append(r, x):
if hasattr(r, 'append'):
r.append(x)
elif hasattr(r, 'add'):
r.add(x)
return r
return transduce(xducer, append, target, coll)
# Mapper Function for Transforming Input Data
def mapper(mapping_description: Dict[str, Any], input_data: Dict[str, Any]):
def transform(xform, value):
if callable(xform):
return xform(value)
elif isinstance(xform, dict):
return {k: transform(v, value) for k, v in xform.items()}
else:
raise ValueError(
f"Invalid transformation type: {type(xform)}. Expected callable or Mapping."
)
def get_value(key):
if isinstance(key, str) and key.startswith(":"):
return input_data.get(key[1:])
return input_data.get(key)
def process_mapping(mapping_description):
result = {}
for key, xform in mapping_description.items():
if isinstance(xform, str):
value = get_value(xform)
result[key] = value
elif isinstance(xform, dict):
if "key" in xform:
value = get_value(xform["key"])
if "xform" in xform:
result[key] = transform(xform["xform"], value)
elif "xf" in xform:
if isinstance(value, list):
transformed = [xform["xf"](v) for v in value]
if "f" in xform:
result[key] = xform["f"](transformed)
else:
result[key] = transformed
else:
result[key] = xform["xf"](value)
else:
result[key] = value
else:
result[key] = process_mapping(xform)
else:
result[key] = xform
return result
return process_mapping(mapping_description)
# Helper Function for Formatting Complex Matrices
def format_complex_matrix(matrix: List[List[complex]], precision: int = 3) -> str:
"""Helper function to format complex matrices for printing."""
result = []
for row in matrix:
formatted_row = []
for elem in row:
if not isinstance(elem, complex):
raise ValueError(f"Expected complex number, got {type(elem)}.")
real = round(elem.real, precision)
imag = round(elem.imag, precision)
if abs(imag) < 1e-10:
formatted_row.append(f"{real:6.3f}")
else:
formatted_row.append(
f"{real:6.3f}{'+' if imag >= 0 else ''}{imag:6.3f}j"
)
result.append("[" + ", ".join(formatted_row) + "]")
return "[\n " + "\n ".join(result) + "\n]"
if __name__ == "__main__":
import argparse
import re
def is_valid_semver(version: str) -> bool:
# Accepts optional leading 'v', e.g. "v1.2.3" or "1.2.3"
return re.match(r"^v?\d+\.\d+\.\d+$", version) is not None
parser = argparse.ArgumentParser(description="Run MSC with specified version.")
parser.add_argument(
"--version",
help="The semantic version to use (e.g. 0.0.12 or v0.0.12)",
required=True,
)
args = parser.parse_args()
version = args.version
if version.startswith("v"):
version = version[1:]
if not is_valid_semver(version):
print(f"Invalid semantic version format: {version}", file=sys.stderr)
sys.exit(1)
print(f"[INFO] Using version: {version}")
log = LogAdapter(correlation_id=version)
log.logger.debug("Debug level test; 'Hello world!'")
log.logger.warning("Warning with CID and colors")
log.logger.error("Error occurred in something")
log.logger.critical("Critical issue reported")
print(f'Find logs @ {log.broadcast_filename}')