Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle punycode domains #22

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions recordmaster/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def import_records(self, data: dict, domain: str = "", root: str = ""):
setattr(self, key, val)
else:
logging.warning(
"Ignored importing record data for domain '%s'. Key: '%s', Value: '%s'",
"Ignored importing record data for domain '%s.%s'. Key: '%s', Value: '%s'",
domain,
self.name,
key,
val,
)
Expand Down Expand Up @@ -92,7 +93,7 @@ def to_local_conf_format(self, records: list[Record], ignore_types: list) -> dic

data[name].append(rec_yaml)

return {self.name: data}
return {convert_punycode(self.name, False): data}


def dc2json(domain: Domain) -> str:
Expand All @@ -119,3 +120,11 @@ def cache_data(domain: Domain, debug: bool):
if debug:
logging.debug("[%s] Current data of the domain after matching:", domain.name)
print(jsondc)


def convert_punycode(domain: str, is_punycode: bool = True) -> str:
"""Convert a domain name from human-readable to punycode, or vice versa"""
if is_punycode:
return domain.encode("idna").decode()

return domain.encode().decode("idna")
4 changes: 2 additions & 2 deletions recordmaster/_get_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def find_valid_local_records_files(configdir: str) -> list[str]:
file = path.abspath(path.join(configdir, file))
if file.endswith((".yaml", ".yml")):
dcfg_files_abs.append(file)
elif file.endswith(".sample"):
elif file.endswith((".sample", ".git")):
pass
else:
logging.warning(
Expand Down Expand Up @@ -62,7 +62,7 @@ def convert_dict_to_yaml(data: dict) -> str:
data_sorted[domain] = records_sorted

# Dump as YAML
return yaml.dump(data_sorted, sort_keys=False)
return yaml.dump(data_sorted, sort_keys=False, allow_unicode=True)


def convert_local_records_to_data(domain: Domain, records: dict) -> None:
Expand Down
2 changes: 1 addition & 1 deletion recordmaster/_sync_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def delete_unconfigured_at_remote(
# Run the deletion of the nameserver record with API
inwx_api(api, "nameserver.deleteRecord", interactive=interactive, dry=dry, id=rec.id)
else:
logging.info(
logging.debug(
"[%s] This remote record is not configured locally, but you "
"requested to not delete remote records of this type: %s",
domain.name,
Expand Down
7 changes: 3 additions & 4 deletions recordmaster/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from . import __version__, configure_logger
from ._api import api_login
from ._data import Domain, cache_data
from ._data import Domain, cache_data, convert_punycode
from ._get_records import (
check_local_records_config,
combine_local_records,
Expand Down Expand Up @@ -150,8 +150,7 @@ def sync(
# Initialise a new domain dataclass to hold the different local and
# remote records
domain = Domain()

domain.name = domainname
domain.name = convert_punycode(domainname)

# Read local configuration into domain dataclass
convert_local_records_to_data(domain, records)
Expand Down Expand Up @@ -225,7 +224,7 @@ def convert(
"""The convert command"""
# Create and initiate domain dataclass
domain = Domain()
domain.name = converted_domain
domain.name = convert_punycode(converted_domain)

# Read remote configuration into domain dataclass
convert_remote_records_to_data(api, domain, api_response)
Expand Down