Skip to content

Commit 5ec7885

Browse files
committed
refactor: extract repo classes from config module
Move repository-related classes to dedicated modules for better separation of concerns. Changes: - Extract `AssetRepo` class to `config/asset_repo.py` - Extract `LocalRepo` class to `config/local_repo.py` - Update imports in tests
1 parent 646c1d7 commit 5ec7885

File tree

4 files changed

+156
-144
lines changed

4 files changed

+156
-144
lines changed

md2zhihu/config/__init__.py

Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,20 @@
11
"""Configuration classes for md2zhihu"""
22

33
import argparse
4-
import hashlib
54
import os
65
import re
76
import shutil
87
from typing import List
98

10-
import k3git
119
from k3color import darkred
1210
from k3color import darkyellow
13-
from k3handy import CMD_RAISE_STDOUT
14-
from k3handy import CmdFlag
1511
from k3handy import cmdpass
1612
from k3handy import pjoin
17-
from k3handy import to_bytes
1813

1914
from ..platform import platform_feature_dict
2015
from ..utils import msg
21-
22-
23-
class LocalRepo(object):
24-
is_local = True
25-
"""
26-
Create relative path for url in ``md_path` pointing to ``asset_dir_path``.
27-
"""
28-
29-
def __init__(self, md_path, asset_dir_path):
30-
md_base = os.path.split(md_path)[0]
31-
rel = os.path.relpath(
32-
asset_dir_path,
33-
start=md_base,
34-
)
35-
if rel == ".":
36-
rel = ""
37-
self.path_pattern = pjoin(rel, "{path}")
38-
39-
40-
class AssetRepo(object):
41-
is_local = False
42-
43-
def __init__(self, repo_url, cdn=True):
44-
# TODO: test rendering md rendering with pushed assets
45-
46-
self.cdn = cdn
47-
48-
repo_url = self.parse_shortcut_repo_url(repo_url)
49-
50-
gu = k3git.GitUrl.parse(repo_url)
51-
f = gu.fields
52-
53-
if f["scheme"] == "https" and "committer" in f and "token" in f:
54-
url = gu.fmt(scheme="https")
55-
else:
56-
url = gu.fmt(scheme="ssh")
57-
58-
host, user, repo, branch = (
59-
f.get("host"),
60-
f.get("user"),
61-
f.get("repo"),
62-
f.get("branch"),
63-
)
64-
65-
self.url = url
66-
67-
url_patterns = {
68-
"github.com": "https://raw.githubusercontent.com/{user}/{repo}/{branch}/{path}",
69-
"gitee.com": "https://gitee.com/{user}/{repo}/raw/{branch}/{path}",
70-
}
71-
72-
cdn_patterns = {
73-
"github.com": "https://cdn.jsdelivr.net/gh/{user}/{repo}@{branch}/{path}",
74-
}
75-
76-
if branch is None:
77-
branch = self.make_default_branch()
78-
else:
79-
# strip '@'
80-
branch = branch[1:]
81-
82-
self.host = host
83-
self.user = user
84-
self.repo = repo
85-
self.branch = branch
86-
87-
ptn = url_patterns[host]
88-
if self.cdn and host == "github.com":
89-
ptn = cdn_patterns[host]
90-
91-
self.path_pattern = ptn.format(user=user, repo=repo, branch=branch, path="{path}")
92-
93-
def parse_shortcut_repo_url(self, repo_url):
94-
"""
95-
If repo_url is a shortcut specifying to use local git repo remote url,
96-
convert repo shortcut to url.
97-
98-
md2zhihu --repo . # default remote, default branch
99-
md2zhihu --repo .@brach # default remote
100-
md2zhihu --repo remote@brach
101-
102-
"""
103-
104-
elts = repo_url.split("@", 1)
105-
first = elts.pop(0)
106-
g = k3git.Git(k3git.GitOpt(), cwd=".")
107-
108-
is_shortcut = False
109-
110-
# ".": use cwd git
111-
# ".@foo_branch": use cwd git and specified branch
112-
if first == ".":
113-
msg("Using current git to store assets...")
114-
115-
u = self.get_remote_url()
116-
is_shortcut = True
117-
118-
elif g.remote_get(first) is not None:
119-
msg("Using current git remote: {} to store assets...".format(first))
120-
u = self.get_remote_url(first)
121-
is_shortcut = True
122-
123-
if is_shortcut:
124-
if len(elts) > 0:
125-
u += "@" + elts[0]
126-
msg("Parsed shortcut {} to {}".format(repo_url, u))
127-
repo_url = u
128-
129-
return repo_url
130-
131-
def get_remote_url(self, remote=None):
132-
g = k3git.Git(k3git.GitOpt(), cwd=".")
133-
134-
if remote is None:
135-
branch = g.head_branch(flag=[CmdFlag.RAISE])
136-
remote = g.branch_default_remote(branch, flag=[CmdFlag.NONE])
137-
if remote is None:
138-
# `branch` has no remote configured.
139-
remote = g.cmdf("remote", flag=CMD_RAISE_STDOUT)[0]
140-
141-
remote_url = g.remote_get(remote, flag=[CmdFlag.RAISE])
142-
return remote_url
143-
144-
def make_default_branch(self):
145-
cwd = os.getcwd().split(os.path.sep)
146-
cwdmd5 = hashlib.md5(to_bytes(os.getcwd())).hexdigest()
147-
branch = "_md2zhihu_{tail}_{md5}".format(
148-
tail=cwd[-1],
149-
md5=cwdmd5[:8],
150-
)
151-
# escape special chars
152-
branch = re.sub(r"[^a-zA-Z0-9_\-=]+", "", branch)
153-
154-
return branch
16+
from .asset_reop import AssetRepo
17+
from .local_repo import LocalRepo
15518

15619

15720
class Config(object):

md2zhihu/config/asset_reop.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import hashlib
2+
import os
3+
import re
4+
5+
import k3git
6+
from k3handy import CMD_RAISE_STDOUT
7+
from k3handy import CmdFlag
8+
from k3str import to_bytes
9+
10+
from md2zhihu import msg
11+
12+
13+
class AssetRepo(object):
14+
is_local = False
15+
16+
def __init__(self, repo_url, cdn=True):
17+
# TODO: test rendering md rendering with pushed assets
18+
19+
self.cdn = cdn
20+
21+
repo_url = self.parse_shortcut_repo_url(repo_url)
22+
23+
gu = k3git.GitUrl.parse(repo_url)
24+
f = gu.fields
25+
26+
if f["scheme"] == "https" and "committer" in f and "token" in f:
27+
url = gu.fmt(scheme="https")
28+
else:
29+
url = gu.fmt(scheme="ssh")
30+
31+
host, user, repo, branch = (
32+
f.get("host"),
33+
f.get("user"),
34+
f.get("repo"),
35+
f.get("branch"),
36+
)
37+
38+
self.url = url
39+
40+
url_patterns = {
41+
"github.com": "https://raw.githubusercontent.com/{user}/{repo}/{branch}/{path}",
42+
"gitee.com": "https://gitee.com/{user}/{repo}/raw/{branch}/{path}",
43+
}
44+
45+
cdn_patterns = {
46+
"github.com": "https://cdn.jsdelivr.net/gh/{user}/{repo}@{branch}/{path}",
47+
}
48+
49+
if branch is None:
50+
branch = self.make_default_branch()
51+
else:
52+
# strip '@'
53+
branch = branch[1:]
54+
55+
self.host = host
56+
self.user = user
57+
self.repo = repo
58+
self.branch = branch
59+
60+
ptn = url_patterns[host]
61+
if self.cdn and host == "github.com":
62+
ptn = cdn_patterns[host]
63+
64+
self.path_pattern = ptn.format(user=user, repo=repo, branch=branch, path="{path}")
65+
66+
def parse_shortcut_repo_url(self, repo_url):
67+
"""
68+
If repo_url is a shortcut specifying to use local git repo remote url,
69+
convert repo shortcut to url.
70+
71+
md2zhihu --repo . # default remote, default branch
72+
md2zhihu --repo .@brach # default remote
73+
md2zhihu --repo remote@brach
74+
75+
"""
76+
77+
elts = repo_url.split("@", 1)
78+
first = elts.pop(0)
79+
g = k3git.Git(k3git.GitOpt(), cwd=".")
80+
81+
is_shortcut = False
82+
83+
# ".": use cwd git
84+
# ".@foo_branch": use cwd git and specified branch
85+
if first == ".":
86+
msg("Using current git to store assets...")
87+
88+
u = self.get_remote_url()
89+
is_shortcut = True
90+
91+
elif g.remote_get(first) is not None:
92+
msg("Using current git remote: {} to store assets...".format(first))
93+
u = self.get_remote_url(first)
94+
is_shortcut = True
95+
96+
if is_shortcut:
97+
if len(elts) > 0:
98+
u += "@" + elts[0]
99+
msg("Parsed shortcut {} to {}".format(repo_url, u))
100+
repo_url = u
101+
102+
return repo_url
103+
104+
def get_remote_url(self, remote=None):
105+
g = k3git.Git(k3git.GitOpt(), cwd=".")
106+
107+
if remote is None:
108+
branch = g.head_branch(flag=[CmdFlag.RAISE])
109+
remote = g.branch_default_remote(branch, flag=[CmdFlag.NONE])
110+
if remote is None:
111+
# `branch` has no remote configured.
112+
remote = g.cmdf("remote", flag=CMD_RAISE_STDOUT)[0]
113+
114+
remote_url = g.remote_get(remote, flag=[CmdFlag.RAISE])
115+
return remote_url
116+
117+
def make_default_branch(self):
118+
cwd = os.getcwd().split(os.path.sep)
119+
cwdmd5 = hashlib.md5(to_bytes(os.getcwd())).hexdigest()
120+
branch = "_md2zhihu_{tail}_{md5}".format(
121+
tail=cwd[-1],
122+
md5=cwdmd5[:8],
123+
)
124+
# escape special chars
125+
branch = re.sub(r"[^a-zA-Z0-9_\-=]+", "", branch)
126+
127+
return branch

md2zhihu/config/local_repo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
from k3handy import pjoin
4+
5+
6+
class LocalRepo(object):
7+
is_local = True
8+
"""
9+
Create relative path for url in ``md_path` pointing to ``asset_dir_path``.
10+
"""
11+
12+
def __init__(self, md_path, asset_dir_path):
13+
md_base = os.path.split(md_path)[0]
14+
rel = os.path.relpath(
15+
asset_dir_path,
16+
start=md_base,
17+
)
18+
if rel == ".":
19+
rel = ""
20+
self.path_pattern = pjoin(rel, "{path}")

test/test_md2zhihu.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from skimage.metrics import structural_similarity
1616

1717
import md2zhihu
18+
import md2zhihu.config.asset_reop
19+
import md2zhihu.config.local_repo
1820

1921
dd = k3ut.dd
2022

@@ -38,7 +40,7 @@ def test_asset_repo(self):
3840
"ssh://git@github.com/drmingdrmer/home",
3941
"https://github.com/drmingdrmer/home.git",
4042
):
41-
a = md2zhihu.AssetRepo(url, cdn=False)
43+
a = md2zhihu.config.asset_reop.AssetRepo(url, cdn=False)
4244
self.assertEqual(False, a.cdn)
4345
self.assertRegex(a.branch, "_md2zhihu_md2zhihu_[a-z0-9]{8}")
4446
b = a.branch
@@ -53,7 +55,7 @@ def test_asset_repo(self):
5355
# specify branch
5456

5557
url = "git@github.com:drmingdrmer/home.git@abc"
56-
a = md2zhihu.AssetRepo(url, cdn=False)
58+
a = md2zhihu.config.asset_reop.AssetRepo(url, cdn=False)
5759
self.assertEqual(False, a.cdn)
5860
self.assertEqual("abc", a.branch)
5961
self.assertEqual("github.com", a.host)
@@ -66,7 +68,7 @@ def test_asset_repo(self):
6668

6769
# with cdn
6870

69-
a = md2zhihu.AssetRepo("git@github.com:drmingdrmer/home.git")
71+
a = md2zhihu.config.asset_reop.AssetRepo("git@github.com:drmingdrmer/home.git")
7072
self.assertEqual(True, a.cdn)
7173
self.assertRegex(a.branch, "_md2zhihu_md2zhihu_[a-z0-9]{8}")
7274
b = a.branch
@@ -80,7 +82,7 @@ def test_asset_repo(self):
8082

8183
# https url with token
8284

83-
a = md2zhihu.AssetRepo("https://aa:bb@github.com/drmingdrmer/home.git")
85+
a = md2zhihu.config.asset_reop.AssetRepo("https://aa:bb@github.com/drmingdrmer/home.git")
8486
self.assertEqual(True, a.cdn)
8587
self.assertEqual("https://aa:bb@github.com/drmingdrmer/home.git", a.url)
8688
self.assertRegex(a.branch, "_md2zhihu_md2zhihu_[a-z0-9]{8}")
@@ -103,7 +105,7 @@ def test_local_repo(self):
103105
for c in cases:
104106
md_path, asset_path, want = c
105107
dd(c)
106-
local = md2zhihu.LocalRepo(md_path, asset_path)
108+
local = md2zhihu.config.local_repo.LocalRepo(md_path, asset_path)
107109
self.assertEqual(want, local.path_pattern)
108110

109111
def test_chunks(self):

0 commit comments

Comments
 (0)