11#!/usr/bin/env python3
2- # coding:utf8
2+ # -*- coding: utf-8 -*-
33
4+ import re
45import sys
56import argparse
67from code_counter import __version__
@@ -10,9 +11,32 @@ def split_args(args):
1011 return list (args .split (',' ))
1112
1213
14+ def remote_repo_parse (repo ):
15+ # check HTTPS link first
16+ res = re .search (r"^https://(github|gitee).com/([^/]+)/([^\.]+)\.git$" , repo , re .I )
17+ # if got none, then check SSH link
18+ if not res :
19+ res = re .search (r"^git@(github|gitee).com:([^/]+)/([^\.]+)\.git$" , repo , re .I )
20+ if not res :
21+ print ('Remote repository link parse error! please enter a right HTTPS or SSH link:' )
22+ print ('\t cocnt remote' , repo )
23+ exit (1 )
24+ if res .group (1 ).lower () == 'github' :
25+ # Github API
26+ return 'https://api.github.com/repos/{}/{}/contents/' .format (res .group (2 ), res .group (3 ))
27+ elif res .group (1 ).lower () == 'gitee' :
28+ # Gitee API
29+ return 'https://gitee.com/api/v5/repos/{}/{}/contents/' .format (res .group (2 ), res .group (3 ))
30+ else :
31+ print ('Remote repository link parse error! please enter a right HTTPS or SSH link:' )
32+ print ('\t cocnt remote' , repo )
33+ exit (1 )
34+
35+
1336class CodeCounterArgs :
1437 __SEARCH__ = 'search'
1538 __CONFIG__ = 'config'
39+ __REMOTE__ = 'remote'
1640
1741 def __init__ (self ):
1842 parser = argparse .ArgumentParser (
@@ -21,7 +45,8 @@ def __init__(self):
2145 "that can help you easily count code and display detailed results." ,
2246 usage = """cocnt <command> [<args>]
2347These are common Code-Counter commands used in various situations:
24- search Search code in the given path(s)
48+ search Search and count code lines for the given path(s)
49+ remote Search and count the remote repository
2550 config Configure Code-Counter
2651""" )
2752 parser .add_argument ('--version' , action = 'version' ,
@@ -38,16 +63,36 @@ def __init__(self):
3863 def has_search_args (self ):
3964 return self .__SEARCH__ in self .__args
4065
66+ def has_remote_args (self ):
67+ return self .__REMOTE__ in self .__args
68+
4169 def has_config_args (self ):
4270 return self .__CONFIG__ in self .__args
4371
72+ def is_github_repo (self ):
73+ if not self .has_remote_args ():
74+ return False
75+ return self .__args [self .__REMOTE__ ].input_path .startswith ('https://api.github.com/repos/' )
76+
77+ def is_gitee_repo (self ):
78+ if not self .has_remote_args ():
79+ return False
80+ return self .__args [self .__REMOTE__ ].input_path .startswith ('https://gitee.com/api/v5/repos/' )
81+
4482 def search (self ):
4583 if not self .has_search_args ():
4684 return None
4785 if self .__args [self .__SEARCH__ ] == argparse .Namespace ():
4886 self .__args [self .__SEARCH__ ] = self .__search ()
4987 return self .__args [self .__SEARCH__ ]
5088
89+ def remote (self ):
90+ if not self .has_remote_args ():
91+ return None
92+ if self .__args [self .__REMOTE__ ] == argparse .Namespace ():
93+ self .__args [self .__REMOTE__ ] = self .__remote ()
94+ return self .__args [self .__REMOTE__ ]
95+
5196 def config (self ):
5297 if not self .has_config_args ():
5398 return None
@@ -57,10 +102,10 @@ def config(self):
57102
58103 def __search (self ):
59104 parser = argparse .ArgumentParser (
60- description = "Search code in the given path(s)" ,
105+ description = "Search and count code lines for the given path(s)" ,
61106 usage = "cocnt search input_path [-h] [-v] [-g] "
62107 "[-o OUTPUT_PATH] [--suffix SUFFIX] [--comment COMMENT] [--ignore IGNORE]" )
63- parser .add_argument ('input_path' , type = split_args ,
108+ parser .add_argument ('input_path' , metavar = "paths" , type = split_args ,
64109 help = "counting the code lines according to the given path(s)" )
65110 parser .add_argument ('-v' , '--verbose' , dest = "verbose" , action = 'store_true' ,
66111 help = "show verbose information" )
@@ -76,6 +121,27 @@ def __search(self):
76121 help = "ignore some directories or files that you don't want to count" )
77122 return parser .parse_args (sys .argv [2 :])
78123
124+ def __remote (self ):
125+ parser = argparse .ArgumentParser (
126+ description = "Search and count the remote repository with a given Github or Gitee HTTP link" ,
127+ usage = "cocnt remote <repository> [-h] [-v] [-g] "
128+ "[-o OUTPUT_PATH] [--suffix SUFFIX] [--comment COMMENT] [--ignore IGNORE]" )
129+ parser .add_argument ('input_path' , metavar = "repository" , type = remote_repo_parse ,
130+ help = "search and count a remote repository" )
131+ parser .add_argument ('-v' , '--verbose' , dest = "verbose" , action = 'store_true' ,
132+ help = "show verbose information" )
133+ parser .add_argument ('-g' , '--graph' , dest = 'graph' , action = 'store_true' ,
134+ help = "choose to whether to visualize the result" )
135+ parser .add_argument ('-o' , '--output' , dest = 'output_path' ,
136+ help = "specify an output path if you want to store the result" )
137+ parser .add_argument ('--suffix' , dest = 'suffix' , type = split_args ,
138+ help = "what code files do you want to count" )
139+ parser .add_argument ('--comment' , dest = 'comment' , type = split_args ,
140+ help = "the comment symbol, which can be judged whether the current line is a comment" )
141+ parser .add_argument ('--ignore' , dest = 'ignore' , type = split_args ,
142+ help = "ignore some directories or files that you don't want to count" )
143+ return parser .parse_args (sys .argv [2 :])
144+
79145 def __config (self ):
80146 parser = argparse .ArgumentParser (
81147 prog = "code-counter" ,
0 commit comments