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

Improve save and load repocard metadata #355

Merged
merged 19 commits into from
Sep 27, 2021
37 changes: 25 additions & 12 deletions src/huggingface_hub/repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ def metadata_load(local_path: Union[str, Path]) -> Optional[Dict]:


def metadata_save(local_path: Union[str, Path], data: Dict) -> None:
data_yaml = yaml.dump(data, sort_keys=False)
# sort_keys: keep dict order
content = Path(local_path).read_text() if Path(local_path).is_file() else ""
match = REGEX_YAML_BLOCK.search(content)
if match:
output = (
content[: match.start()] + f"---\n{data_yaml}---\n" + content[match.end() :]
)
else:
output = f"---\n{data_yaml}---\n{content}"

Path(local_path).write_text(output)
# try to preserve newlines
elishowk marked this conversation as resolved.
Show resolved Hide resolved
linebrk = "\n"
elishowk marked this conversation as resolved.
Show resolved Hide resolved
# this is known not to work with ^M linebreaks, so ^M are replaced by \n
with open(local_path, "r") as readme:
elishowk marked this conversation as resolved.
Show resolved Hide resolved
if type(readme.newlines) is tuple:
linebrk = readme.newlines[0]
if type(readme.newlines) is str:
linebrk = readme.newlines
content = readme.read()

if content:
with open(local_path, "w", newline="") as readme:
elishowk marked this conversation as resolved.
Show resolved Hide resolved
data_yaml = yaml.dump(data, sort_keys=False, line_break=linebrk)
elishowk marked this conversation as resolved.
Show resolved Hide resolved
match = REGEX_YAML_BLOCK.search(content)
if match:
output = (
content[: match.start()]
+ f"---{linebrk}{data_yaml}---{linebrk}"
+ content[match.end() :]
)
else:
output = f"---{linebrk}{data_yaml}---{linebrk}{content}"

readme.write(output)
readme.close()