Skip to content

Commit

Permalink
Fix Typo and Section Args in Config Store Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
iguessthislldo committed Mar 30, 2024
1 parent 9a4204d commit 8fd9b85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/internal/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ Turns into:
:no-contents-entry:
:no-index-entry:

Sections with discriminators require them in the reference targets: cfg:sec:`server@linux`, :cfg:key:`[server@linux]distro`
Sections with discriminators require them in the reference targets: :cfg:sec:`server@linux`, :cfg:key:`[server@linux]distro`

.. cfg:sec:: server@linux/<name>
:no-contents-entry:
Expand Down
33 changes: 19 additions & 14 deletions docs/sphinx_extensions/config_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,25 @@ def key_text(sec_name, sec_disc, key_name, insert=''):


# This should be equivalent to ConfigPair::canonicalize
def key_canonicalize(*parts):
key = '_'.join(filter(None, parts))

def name_canonicalize(name):
# Replace everything that's not a letter, number, or underscore
key = re.sub(r'[^\w]', '_', key)
name = re.sub(r'[^\w]', '_', name)

# Convert CamelCase to camel_case
key = re.sub(r'([A-Z][a-z])', r'_\1', key)
key = re.sub(r'([a-z])([A-Z])', r'\1_\2', key)
name = re.sub(r'([A-Z][a-z])', r'_\1', name)
name = re.sub(r'([a-z])([A-Z])', r'\1_\2', name)

# Removed repeated underscores
key = re.sub(r'_+', r'_', key)
name = re.sub(r'_+', r'_', name)

return name.strip('_').upper()


return key.strip('_').upper()
def key_canonicalize(sec_name, sec_args, key_name):
sec = name_canonicalize(sec_name)
if sec_args:
sec += '_' + sec_args
return sec + '_' + name_canonicalize(key_name)


class ConfigKeyRefRole(XRefRole):
Expand Down Expand Up @@ -383,14 +388,14 @@ def test_parse_key_name(self):

def test_key_canonicalize(self):
cases = {
('~!abc.123__CamelCase/CAMELCase#$%',): 'ABC_123_CAMEL_CASE_CAMEL_CASE',
('CamelCase',): 'CAMEL_CASE',
('CamelCase', None, 'key'): 'CAMEL_CASE_KEY',
('##CamelCase##', None, 'key'): 'CAMEL_CASE_KEY',
('~!abc.123_''_CamelCase/CAMELCase#$%', None, 'key'): 'ABC_123_CAMEL_CASE_CAMEL_CASE_KEY',
('sect', None, 'UseXTypes',): 'SECT_USE_X_TYPES',
('sect', None, 'UseXYZTypes',): 'SECT_USE_XYZ_TYPES',
('TheSection', None, 'TheKey'): 'THE_SECTION_THE_KEY',
('TheSection', '', 'TheKey'): 'THE_SECTION_THE_KEY',
('TheSection', 'TheInstance', 'TheKey'): 'THE_SECTION_THE_INSTANCE_THE_KEY',
('##CamelCase##',): 'CAMEL_CASE',
('UseXTypes',): 'USE_X_TYPES',
('UseXYZTypes',): 'USE_XYZ_TYPES',
('TheSection', '<instance-name>', 'TheKey'): 'THE_SECTION_<instance-name>_THE_KEY',
}

for args, expected in cases.items():
Expand Down

0 comments on commit 8fd9b85

Please sign in to comment.