11import hashlib
22import os
33import re
4+ from typing import Optional
45
56import k3git
67from k3handy import CMD_RAISE_STDOUT
1112
1213
1314class AssetRepo (object ):
14- is_local = False
15+ is_local : bool = False
1516
16- def __init__ (self , repo_url , cdn = True ):
17+ def __init__ (self , repo_url : str , cdn : bool = True ) -> None :
1718 # TODO: test rendering md rendering with pushed assets
1819
19- self .cdn = cdn
20+ self .cdn : bool = cdn
2021
2122 repo_url = self .parse_shortcut_repo_url (repo_url )
2223
@@ -35,7 +36,7 @@ def __init__(self, repo_url, cdn=True):
3536 f .get ("branch" ),
3637 )
3738
38- self .url = url
39+ self .url : str = url
3940
4041 url_patterns = {
4142 "github.com" : "https://raw.githubusercontent.com/{user}/{repo}/{branch}/{path}" ,
@@ -52,18 +53,18 @@ def __init__(self, repo_url, cdn=True):
5253 # strip '@'
5354 branch = branch [1 :]
5455
55- self .host = host
56- self .user = user
57- self .repo = repo
58- self .branch = branch
56+ self .host : Optional [ str ] = host
57+ self .user : Optional [ str ] = user
58+ self .repo : Optional [ str ] = repo
59+ self .branch : str = branch
5960
6061 ptn = url_patterns [host ]
6162 if self .cdn and host == "github.com" :
6263 ptn = cdn_patterns [host ]
6364
64- self .path_pattern = ptn .format (user = user , repo = repo , branch = branch , path = "{path}" )
65+ self .path_pattern : str = ptn .format (user = user , repo = repo , branch = branch , path = "{path}" )
6566
66- def parse_shortcut_repo_url (self , repo_url ) :
67+ def parse_shortcut_repo_url (self , repo_url : str ) -> str :
6768 """
6869 If repo_url is a shortcut specifying to use local git repo remote url,
6970 convert repo shortcut to url.
@@ -79,6 +80,7 @@ def parse_shortcut_repo_url(self, repo_url):
7980 g = k3git .Git (k3git .GitOpt (), cwd = "." )
8081
8182 is_shortcut = False
83+ u : str = ""
8284
8385 # ".": use cwd git
8486 # ".@foo_branch": use cwd git and specified branch
@@ -101,7 +103,7 @@ def parse_shortcut_repo_url(self, repo_url):
101103
102104 return repo_url
103105
104- def get_remote_url (self , remote = None ):
106+ def get_remote_url (self , remote : Optional [ str ] = None ) -> str :
105107 g = k3git .Git (k3git .GitOpt (), cwd = "." )
106108
107109 if remote is None :
@@ -111,10 +113,10 @@ def get_remote_url(self, remote=None):
111113 # `branch` has no remote configured.
112114 remote = g .cmdf ("remote" , flag = CMD_RAISE_STDOUT )[0 ]
113115
114- remote_url = g .remote_get (remote , flag = [CmdFlag .RAISE ])
116+ remote_url : str = g .remote_get (remote , flag = [CmdFlag .RAISE ])
115117 return remote_url
116118
117- def make_default_branch (self ):
119+ def make_default_branch (self ) -> str :
118120 cwd = os .getcwd ().split (os .path .sep )
119121 cwdmd5 = hashlib .md5 (to_bytes (os .getcwd ())).hexdigest ()
120122 branch = "_md2zhihu_{tail}_{md5}" .format (
0 commit comments