11from paths_cli .parsing .core import Parser , InstanceBuilder
22from paths_cli .parsing .plugins import ParserPlugin
33
4+ import logging
5+ logger = logging .getLogger (__name__ )
46
7+ class ParserRegistrationError (Exception ):
8+ pass
9+
10+
11+ # TODO: I think this is the only OPS-specific thing in here
512_DEFAULT_PARSE_ORDER = [
613 'engine' ,
714 'cv' ,
1320
1421PARSE_ORDER = _DEFAULT_PARSE_ORDER .copy ()
1522
16- PARSERS = {}
17- ALIASES = {}
23+ def clean_input_key (key ):
24+ # TODO: move this to core
25+ """
26+ Canonical approach is to treat everything as lowercase with underscore
27+ separators. This will make everything lowercase, and convert
28+ whitespace/hyphens to underscores.
29+ """
30+ key = key .lower ()
31+ key = "_" .join (key .split ()) # whitespace to underscore
32+ key .replace ("-" , "_" )
33+ return key
34+
35+ ### Managing known parsers and aliases to the known parsers ################
36+
37+ _PARSERS = {} # mapping: {canonical_name: Parser}
38+ _ALIASES = {} # mapping: {alias: canonical_name}
39+ # NOTE: _ALIASES does *not* include self-mapping of the canonical names
40+
41+ def _canonical_name (alias ):
42+ """Take an alias or a parser name and return the parser name
43+
44+ This also cleans user input (using the canonical form generated by
45+ :meth:`.clean_input_key`).
46+ """
47+ alias = clean_input_key (alias )
48+ alias_to_canonical = _ALIASES .copy ()
49+ alias_to_canonical .update ({pname : pname for pname in _PARSERS })
50+ return alias_to_canonical .get (alias , None )
51+
52+ def _get_parser (parser_name ):
53+ """
54+ _get_parser must only be used after the parsers have been registered. It
55+ will automatically create a parser for any unknown ``parser_name.``
56+ """
57+ canonical_name = _canonical_name (parser_name )
58+ # create a new parser if none exists
59+ if canonical_name is None :
60+ canonical_name = parser_name
61+ _PARSERS [parser_name ] = Parser (None , parser_name )
62+ return _PARSERS [canonical_name ]
63+
64+ def _register_parser_plugin (plugin ):
65+ DUPLICATE_ERROR = RuntimeError (f"The name { plugin .name } has been "
66+ "reserved by another parser" )
67+ if plugin .name in _PARSERS :
68+ raise DUPLICATE_ERROR
69+
70+ parser = _get_parser (plugin .name )
71+
72+ # register aliases
73+ new_aliases = set (plugin .aliases ) - set ([plugin .name ])
74+ for alias in new_aliases :
75+ if alias in _PARSERS or alias in _ALIASES :
76+ raise DUPLICATE_ERROR
77+ _ALIASES [alias ] = plugin .name
78+
1879
19- class ParserProxy :
80+ ### Handling delayed loading of parsers ####################################
81+ #
82+ # Many objects need to use parsers to create their input parameters. In
83+ # order for them to be able to access dynamically-loaded plugins, we delay
84+ # the loading of the parser by using a proxy object.
85+
86+ class _ParserProxy :
2087 def __init__ (self , parser_name ):
2188 self .parser_name = parser_name
2289
2390 @property
2491 def _proxy (self ):
25- return PARSERS [self .parser_name ]
92+ canonical_name = _canonical_name (self .parser_name )
93+ if canonical_name is None :
94+ raise RuntimeError ("No parser registered for "
95+ f"'{ self .parser_name } '" )
96+ return _get_parser (canonical_name )
2697
2798 @property
2899 def named_objs (self ):
@@ -41,18 +112,10 @@ def parser_for(parser_name):
41112 parser_name : str
42113 the name of the parser to use
43114 """
44- return ParserProxy (parser_name )
115+ return _ParserProxy (parser_name )
45116
46- def _get_parser (parser_name ):
47- """
48- _get_parser must only be used after the parsers have been registered. It
49- will automatically create a parser for any unknown ``parser_name.``
50- """
51- parser = PARSERS .get (parser_name , None )
52- if parser is None :
53- parser = Parser (None , parser_name )
54- PARSERS [parser_name ] = parser
55- return parser
117+
118+ ### Registering builder plugins and user-facing register_plugins ###########
56119
57120def _get_registration_names (plugin ):
58121 """This is a trick to ensure that the names appear in the desired order.
@@ -70,6 +133,11 @@ def _get_registration_names(plugin):
70133 found_names .add (name )
71134 return ordered_names
72135
136+ def _register_builder_plugin (plugin ):
137+ parser = _get_parser (plugin .parser_name )
138+ for name in _get_registration_names (plugin ):
139+ parser .register_builder (plugin , name )
140+
73141def register_plugins (plugins ):
74142 builders = []
75143 parsers = []
@@ -85,31 +153,31 @@ def register_plugins(plugins):
85153 for plugin in builders :
86154 _register_builder_plugin (plugin )
87155
88- def _register_builder_plugin (plugin ):
89- parser = _get_parser (plugin .parser_name )
90- for name in _get_registration_names (plugin ):
91- parser .register_builder (plugin , name )
92-
93- def _register_parser_plugin (plugin ):
94- DUPLICATE_ERROR = RuntimeError (f"The name { plugin .name } has been "
95- "reserved by another parser" )
96- if plugin .name in PARSERS :
97- raise DUPLICATE_ERROR
156+ ### Performing the parsing of user input ###################################
98157
99- parser = _get_parser (plugin .name )
158+ def _sort_user_categories (user_categories ):
159+ """Organize user input categories into parse order.
100160
101- # register aliases
102- new_aliases = set (plugin .aliases ) - set ([plugin .name ])
103- for alias in new_aliases :
104- if alias in PARSERS or alias in ALIASES :
105- raise DUPLICATE_ERROR
106- ALIASES [alias ] = plugin .name
161+ "Cateogories" are the first-level keys in the user input file (e.g.,
162+ 'engines', 'cvs', etc.) There must be one Parser per category.
163+ """
164+ user_to_canonical = {user_key : _canonical_name (user_key )
165+ for user_key in user_categories }
166+ sorted_keys = sorted (
167+ user_categories ,
168+ key = lambda x : PARSE_ORDER .index (user_to_canonical [x ])
169+ )
170+ return sorted_keys
107171
108172def parse (dct ):
173+ """Main function for compiling user input to objects.
174+ """
109175 objs = []
110- for category in PARSE_ORDER :
111- func = PARSERS [category ]
176+ for category in _sort_user_categories (dct ):
177+ # func = PARSERS[category]
178+ func = _get_parser (category )
112179 yaml_objs = dct .get (category , [])
180+ print (f"{ yaml_objs } " )
113181 new = [func (obj ) for obj in yaml_objs ]
114182 objs .extend (new )
115183 return objs
0 commit comments