-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code handling _scmsync.obsinfo to obs_api.scmsync_obsinfo.Sc…
…msyncObsinfo class
- Loading branch information
Showing
2 changed files
with
45 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import typing | ||
|
||
from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import | ||
Check notice Code scanning / CodeQL Cyclic import Note
Import of module
osc.util.models Error loading related location Loading |
||
|
||
|
||
class ScmsyncObsinfo(BaseModel): | ||
""" | ||
Class for handling _scmsync.obsinfo files | ||
""" | ||
|
||
mtime: int = Field() | ||
commit: str = Field() | ||
url: str = Field() | ||
revision: str = Field() | ||
|
||
@classmethod | ||
def from_string(cls, data: str) -> "ScmsyncObsinfo": | ||
kwargs = {} | ||
for line in data.splitlines(): | ||
line = line.strip() | ||
if not line: | ||
continue | ||
key, value = line.split(": ", 1) | ||
field = cls.__fields__.get(key, None) | ||
if field and field.type is int: | ||
value = int(value) | ||
kwargs[key] = value | ||
return cls(**kwargs) | ||
|
||
@classmethod | ||
def from_file(cls, file: Union[str, typing.IO]) -> "ScmsyncObsinfo": | ||
if isinstance(file, str): | ||
with open(file, "r", encoding="utf-8") as f: | ||
return cls.from_string(f.read()) | ||
return cls.from_string(file.read()) | ||
|
||
@classmethod | ||
def from_api(cls, apiurl: str, project: str, package: str, *, rev: str) -> "ScmsyncObsinfo": | ||
url_path = ["source", project, package, "_scmsync.obsinfo"] | ||
url_query = {"rev": rev} | ||
response = cls.xml_request("GET", apiurl, url_path, url_query) | ||
return cls.from_file(response, apiurl=apiurl) |