Skip to content

Commit 126c5f2

Browse files
author
Igor Kalnitsky
committed
Make code more cleary and readable.
1 parent 1a45b9f commit 126c5f2

File tree

3 files changed

+70
-51
lines changed

3 files changed

+70
-51
lines changed

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
setup(
88
name='xsnippet-cli',
99
version=version,
10+
url='https://github.com/xsnippet/xsnippet-cli',
11+
license='BSD License',
1012
author='Igor Kalnitsky',
1113
author_email='[email protected]',
1214
description='A simple command line interface for the XSnippet service.',
1315
long_description=open('README.rst').read(),
14-
license='BSD License',
1516
packages=['xsnippet_cli'],
1617
entry_points={
1718
'console_scripts': ['xsnippet = xsnippet_cli.cli:main'],
@@ -29,5 +30,4 @@
2930
'Programming Language :: Python :: 3.2',
3031
'Programming Language :: Python :: 3.3',
3132
],
32-
url='https://github.com/xsnippet/xsnippet-cli',
3333
)

xsnippet_cli/__init__.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@
33
xsnippet_cli
44
~~~~~~~~~~~~
55
6-
XSnippet CLI is a simple command line interface for interacting
7-
with XSnippet service. Currently, it provides only posting and
8-
receiving snippets as a Guest user.
6+
**xsnippet-cli** is a simple command line interface for interacting with
7+
Xsnippet_ service. By means this script, you can easily post and receive
8+
Snippets directly from your terminal.
99
10-
You can send a file in the following way::
10+
11+
Usage
12+
-----
13+
14+
It's very easy to use. You can paste the snippet this way ::
1115
1216
$ xsnippet /path/to/file
1317
14-
For sending a some command output, you can use a unix pipe system::
18+
Or this way ::
19+
20+
$ cat /path/to/file | xsnippet
21+
22+
As you can see the last method posts a some command output. It's very
23+
usefull To post the last few lines from logfile this way ::
1524
16-
$ ls -la | xsnippet
25+
$ tail -n 5 nginx.log | xsnippet
1726
18-
If you want to retrieve a some snippet from the server, just do the
19-
following command::
27+
It's important to note that you can specify a snippet language or tags.
28+
Thats can be done by the following command ::
2029
21-
$ xsnippet -r <snippet id>
30+
$ cat setup.py | xsnippet -l python -t setuptools test
2231
23-
The script provides a some useful options such as tags or language.
24-
Use ``$ xsnippet --help`` for more details.
2532
2633
:copyright: (c) 2013 by Igor Kalnitsky.
2734
:license: BSD, see LICENSE for more details.

xsnippet_cli/cli.py

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,63 @@
1919
from xsnippet_cli import api
2020

2121

22-
2322
def main():
2423
"""
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.
2626
"""
2727
args = _parse_arguments()
2828

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)
3132

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)
3633

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``.
3738
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)
4043

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))
4347

44-
with open(filename) as f:
45-
post_snippet(f.read(), title, language, tags)
48+
print(snippet['content'])
4649

4750

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.
5055
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)
5173

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))
5476

5577

56-
def get_lang_for_filename(filename):
78+
def _get_lang_for_filename(filename):
5779
try:
5880
aliases = get_lexer_for_filename(filename).aliases
5981
sname = aliases[0] if aliases else None
@@ -63,28 +85,18 @@ def get_lang_for_filename(filename):
6385

6486

6587
def _parse_arguments():
88+
"""
89+
Parse command line arguments and returns result.
90+
Execute ``$ xsnippet --help`` for more details.
91+
"""
6692
args = argparse.ArgumentParser(
6793
description='A simple command line interface for the XSnippet service.'
6894
)
6995

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')
88101

89102
return args.parse_args()
90-

0 commit comments

Comments
 (0)