19
19
from xsnippet_cli import api
20
20
21
21
22
-
23
22
def main ():
24
23
"""
25
- An entry point of the command line interface.
24
+ An entry point of the command line interface. The main
25
+ purpose of this function is a job dispatching.
26
26
"""
27
27
args = _parse_arguments ()
28
28
29
- if args .receive_id :
30
- return receive_snippet (args .receive_id )
29
+ if args .receive :
30
+ return _receive_snippet (args .receive )
31
+ return _post_snippet (args .filename , args .caption , args .language , args .tags )
31
32
32
- if args .filename :
33
- return post_file (args .filename , args .language , args .tags )
34
- else :
35
- return post_snippet (sys .stdin .read (), None , args .language , args .tags )
36
33
34
+ def _receive_snippet (snippet_id , _info = True ):
35
+ """
36
+ Retrieve from the server a snippet with a given id
37
+ and print it to ``stdout``.
37
38
38
- def post_file (filename , language , tags ):
39
- title = os .path .basename (filename )
39
+ .. note: The function prints snippet info if the ``_info``
40
+ argument is True.
41
+ """
42
+ snippet = api .get_snippet (snippet_id )
40
43
41
- if not language :
42
- language = get_lang_for_filename (filename )
44
+ for key , value in snippet .items ():
45
+ if _info and value and key != 'content' :
46
+ print (':{key}: {value}' .format (key = key , value = value ))
43
47
44
- with open (filename ) as f :
45
- post_snippet (f .read (), title , language , tags )
48
+ print (snippet ['content' ])
46
49
47
50
48
- def post_snippet (content , title , language , tags ):
49
- print (api .post_snippet (content , title , language , tags ))
51
+ def _post_snippet (filename = None , caption = None , language = None , tags = None ):
52
+ """
53
+ Send a snippet with a given attributes to the server. If ``filename``
54
+ is not specify the ``stdin`` will be post.
50
55
56
+ .. note: If ``filename`` is specified the language will be defined
57
+ automatically. Please note, this feature will be activated
58
+ only if you not specify a language manually.
59
+ """
60
+ def get_file_info (filename , caption , language ):
61
+ with open (filename ) as f :
62
+ content = f .read ()
63
+ if not caption :
64
+ caption = os .path .basename (filename )
65
+ if not language :
66
+ language = _get_lang_for_filename (filename )
67
+ return content , caption , language
68
+
69
+ if not filename :
70
+ content = sys .stdin .read ()
71
+ else :
72
+ content , caption , language = get_file_info (filename , caption , language )
51
73
52
- def receive_snippet ( id_ ):
53
- print (api .get_snippet ( id_ ))
74
+ # post snippet and print a link to the posted snippet
75
+ print (api .post_snippet ( content , caption , language , tags ))
54
76
55
77
56
- def get_lang_for_filename (filename ):
78
+ def _get_lang_for_filename (filename ):
57
79
try :
58
80
aliases = get_lexer_for_filename (filename ).aliases
59
81
sname = aliases [0 ] if aliases else None
@@ -63,28 +85,18 @@ def get_lang_for_filename(filename):
63
85
64
86
65
87
def _parse_arguments ():
88
+ """
89
+ Parse command line arguments and returns result.
90
+ Execute ``$ xsnippet --help`` for more details.
91
+ """
66
92
args = argparse .ArgumentParser (
67
93
description = 'A simple command line interface for the XSnippet service.'
68
94
)
69
95
70
- args .add_argument (
71
- 'filename' , nargs = '?'
72
- )
73
-
74
- args .add_argument (
75
- '-r' , '--receive' , dest = 'receive_id' ,
76
- help = 'receive a snippet with a given id'
77
- )
78
-
79
- args .add_argument (
80
- '-t' , '--tags' , dest = 'tags' , nargs = '*' ,
81
- help = 'attach tags to the snippet'
82
- )
83
-
84
- args .add_argument (
85
- '-l' , '--language' , dest = 'language' ,
86
- help = 'attach language to the snippet'
87
- )
96
+ args .add_argument ('filename' , nargs = '?' )
97
+ args .add_argument ('-c' , '--caption' , dest = 'caption' )
98
+ args .add_argument ('-t' , '--tags' , dest = 'tags' , nargs = '*' )
99
+ args .add_argument ('-l' , '--language' , dest = 'language' )
100
+ args .add_argument ('-r' , '--receive' , dest = 'receive' , metavar = 'ID' )
88
101
89
102
return args .parse_args ()
90
-
0 commit comments