@@ -447,127 +447,127 @@ def load(cls, filename: str | Path, default: dict | Obj | bool | None = None) ->
447447
448448 return obj
449449
450- @classmethod
451- def from_dict (cls , data : dict , default_section = "not sectioned" ) -> Obj :
452- """
453- Create Obj from a dictionary.
454-
455- Rules:
456- - Top-level dict values become sections
457- - Top-level non-dict values go into `default_section`
458- - Values may be:
459- (type, value) tuples
460- or plain values (type inferred)
461- """
462- obj = cls ()
450+ @classmethod
451+ def from_dict (cls , data : dict , default_section = "not sectioned" ) -> Obj :
452+ """
453+ Create Obj from a dictionary.
454+
455+ Rules:
456+ - Top-level dict values become sections
457+ - Top-level non-dict values go into `default_section`
458+ - Values may be:
459+ (type, value) tuples
460+ or plain values (type inferred)
461+ """
462+ obj = cls ()
463463
464- if not isinstance (data , dict ):
465- raise TypeError ("Input data must be a dictionary" )
464+ if not isinstance (data , dict ):
465+ raise TypeError ("Input data must be a dictionary" )
466466
467- for key , value in data .items ():
467+ for key , value in data .items ():
468468
469- # -------- Case 1: Proper section --------
470- if isinstance (value , dict ):
471- section = obj .section (key )
469+ # -------- Case 1: Proper section --------
470+ if isinstance (value , dict ):
471+ section = obj .section (key )
472472
473- for subkey , entry in value .items ():
473+ for subkey , entry in value .items ():
474474
475- # (type, value)
476- if (
477- isinstance (entry , tuple )
478- and len (entry ) == 2
479- and isinstance (entry [0 ], type )
480- ):
481- typ , val = entry
482- else :
483- typ = type (entry )
484- val = entry
475+ # (type, value)
476+ if (
477+ isinstance (entry , tuple )
478+ and len (entry ) == 2
479+ and isinstance (entry [0 ], type )
480+ ):
481+ typ , val = entry
482+ else :
483+ typ = type (entry )
484+ val = entry
485485
486- section .set (subkey , typ , val )
486+ section .set (subkey , typ , val )
487487
488- # -------- Case 2: Top-level value --------
489- else :
490- section = obj .section (default_section )
491-
492- if (
493- isinstance (value , tuple )
494- and len (value ) == 2
495- and isinstance (value [0 ], type )
496- ):
497- typ , val = value
488+ # -------- Case 2: Top-level value --------
498489 else :
499- typ = type (value )
500- val = value
490+ section = obj .section (default_section )
501491
502- section .set (key , typ , val )
492+ if (
493+ isinstance (value , tuple )
494+ and len (value ) == 2
495+ and isinstance (value [0 ], type )
496+ ):
497+ typ , val = value
498+ else :
499+ typ = type (value )
500+ val = value
503501
504- return obj
502+ section . set ( key , typ , val )
505503
506- @classmethod
507- def convert_file (cls , filename : str | Path , default_section = "not sectioned" , overwrite = False ) -> Obj :
508- """
509- Convert JSON, TOML, YAML, or INI into .rdm format.
504+ return obj
510505
511- If overwrite=True, deletes original file after conversion.
512- """
506+ @classmethod
507+ def convert_file (cls , filename : str | Path , default_section = "not sectioned" , overwrite = False ) -> Obj :
508+ """
509+ Convert JSON, TOML, YAML, or INI into .rdm format.
513510
514- path = Path (filename )
511+ If overwrite=True, deletes original file after conversion.
512+ """
515513
516- if not path .exists ():
517- raise FileNotFoundError (path )
514+ path = Path (filename )
518515
519- suffix = path .suffix .lower ()
516+ if not path .exists ():
517+ raise FileNotFoundError (path )
520518
521- # -------- Load Data --------
519+ suffix = path . suffix . lower ()
522520
523- if suffix == ".json" :
524- with open (path , "r" , encoding = "utf-8" ) as f :
525- data = json .load (f )
521+ # -------- Load Data --------
526522
527- elif suffix == ".toml" :
528- if tomllib :
529- with open (path , "rb" ) as f :
530- data = tomllib .load (f )
531- elif toml :
523+ if suffix == ".json" :
532524 with open (path , "r" , encoding = "utf-8" ) as f :
533- data = toml .load (f )
534- else :
535- raise RuntimeError ("TOML requires Python 3.11+ or 'toml' package." )
525+ data = json .load (f )
526+
527+ elif suffix == ".toml" :
528+ if tomllib :
529+ with open (path , "rb" ) as f :
530+ data = tomllib .load (f )
531+ elif toml :
532+ with open (path , "r" , encoding = "utf-8" ) as f :
533+ data = toml .load (f )
534+ else :
535+ raise RuntimeError ("TOML requires Python 3.11+ or 'toml' package." )
536536
537- elif suffix in (".yaml" , ".yml" ):
538- if not yaml :
539- raise RuntimeError ("YAML support requires PyYAML." )
540- with open (path , "r" , encoding = "utf-8" ) as f :
541- data = yaml .safe_load (f )
537+ elif suffix in (".yaml" , ".yml" ):
538+ if not yaml :
539+ raise RuntimeError ("YAML support requires PyYAML." )
540+ with open (path , "r" , encoding = "utf-8" ) as f :
541+ data = yaml .safe_load (f )
542542
543- elif suffix == ".ini" :
544- parser = configparser .ConfigParser ()
545- parser .read (path )
543+ elif suffix == ".ini" :
544+ parser = configparser .ConfigParser ()
545+ parser .read (path )
546546
547- data = {}
547+ data = {}
548548
549- # Sections
550- for section in parser .sections ():
551- data [section ] = dict (parser [section ])
549+ # Sections
550+ for section in parser .sections ():
551+ data [section ] = dict (parser [section ])
552552
553- # Handle DEFAULT section
554- if parser .defaults ():
555- data ["default" ] = dict (parser .defaults ())
553+ # Handle DEFAULT section
554+ if parser .defaults ():
555+ data ["default" ] = dict (parser .defaults ())
556556
557- else :
558- raise ValueError (f"Unsupported file type: { suffix } " )
557+ else :
558+ raise ValueError (f"Unsupported file type: { suffix } " )
559559
560- if not isinstance (data , dict ):
561- raise TypeError ("Top-level structure must be a dictionary" )
560+ if not isinstance (data , dict ):
561+ raise TypeError ("Top-level structure must be a dictionary" )
562562
563- # -------- Convert --------
563+ # -------- Convert --------
564564
565- obj = cls .from_dict (data , default_section )
565+ obj = cls .from_dict (data , default_section )
566566
567- new_path = path .with_suffix (".rdm" )
568- obj .save (new_path )
567+ new_path = path .with_suffix (".rdm" )
568+ obj .save (new_path )
569569
570- if overwrite :
571- path .unlink ()
570+ if overwrite :
571+ path .unlink ()
572572
573- return obj
573+ return obj
0 commit comments