Skip to content

Commit

Permalink
Pulling updates from 'davglass/master', with some changes.
Browse files Browse the repository at this point in the history
* commit 'davglass/master':
  Updated README
  Added -d for debug and added support for passing the file in via STDIN
  Removed old Ruby file
  Added -e to supply an override file extension - To be used from STDIN (when supported)

Conflicts:

	README.markdown
	gist.py
  • Loading branch information
jeremyBanks committed Jan 29, 2009
2 parents 4e0c85d + daaa3df commit 067cc12
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
29 changes: 24 additions & 5 deletions README.markdown
@@ -1,6 +1,17 @@
Gist: The Python 3.0 Script
===========================

Works great with Gist: The Website.

Ported the Ruby script to Python..

Also supports:

* Passing multiple files as arguments
* Optionally Read from STDIN
* Auto file type detection via ext
* Read support and clone support

Installation
------------

Expand All @@ -12,15 +23,23 @@ As for real installation, you don't want to do that yet.
Usage
-----

_(Will be filled in when this version if more feature-complete.)_

# create a gist from one or more files
gist.py [-p] file [file2 file3...]

# create a gist from stdin, giving if a filename
cal | gist.py -i cal.txt

# clone the repostiories of one or more gists
gist.py -c id [id2 id3...]

# display the text contents of a gist
gist.py -r id

TODO
----

* Move significant code into module and out of `main()`.
* Support for reading private gists.
* Use official API wherever possible (ex. public creating/reading)
* Use official API wherever possible.
* Automatically detect if you attempt to clone one of your own gists,
and use the private clone URL if so. Give overrides to force private
or public clone attempts, though. Maybe. Think about this, at least.
Expand Down Expand Up @@ -54,4 +73,4 @@ License
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 changes: 48 additions & 7 deletions gist.py
Expand Up @@ -120,6 +120,8 @@ def write(self, files, private=False):
"""
Creates a new Gist from the specified files.
files should be provided as a list of tuples(contents, filename)
Returns the id of the newly-created Gist.
"""

Expand All @@ -131,15 +133,14 @@ def write(self, files, private=False):
if private:
post_data["private"] = "on"

for n, filename in enumerate(files, start=1):
if not os.path.isfile(filename):
raise(NotFileError("\"{filename}\" is not a real file.".format(filename=filename)))
for n, file_ in enumerate(files, start=1):
content, filename = file_

form_key = "gistfile{n}".format(n=n)

post_data["file_ext[{key}]".format(key=form_key)] = os.path.splitext(filename)[1] or ".txt"
post_data["file_name[{key}]".format(key=form_key)] = os.path.basename(filename)
post_data["file_contents[{key}]".format(key=form_key)] = open(filename).read()
post_data["file_name[{key}]".format(key=form_key)] = filename
post_data["file_contents[{key}]".format(key=form_key)] = content

if self.authentication:
post_data["login"], post_data["token"] = self.authentication
Expand Down Expand Up @@ -169,7 +170,18 @@ def write(self, files, private=False):
def main(*args):
import gist

optparser = optparse.OptionParser("usage: %prog\n\nBy default, specified files are posted uploaded to a new gist.")
optparser = optparse.OptionParser("\n".join([
"",
" create a gist from one or more files",
" %prog [-p] file [file2 file3...]",
" create a gist from stdin, giving if a filename",
" cal | %prog -i cal.txt",
" clone the repostiories of one or more gists",
" %prog -c id [id2 id3...]",
" display the text contents of a gist",
" %prog -r id",
]))

optparser.set_defaults(mode="post")
optparser.add_option("-p", "--private", dest="private",
action="store_true",
Expand All @@ -180,14 +192,26 @@ def main(*args):
optparser.add_option("-r", "--read", dest="mode", const="read",
action="store_const",
help="Provided with an ID, displays and copies the text contents of that gist.")
optparser.add_option("-i", "--stdin", dest="mode", const="stdin",
action="store_const",
help="Makes a new gist using a single file from stdin, giving it whatever filename is specified.")


(opts, files) = optparser.parse_args(list(args))

user = gist.GistUser()

if opts.mode == "post":
if files:
id = user.write(files, private=opts.private)
file_data = []

for n, filename in enumerate(files, start=1):
if not os.path.isfile(filename):
raise(NotFileError("\"{filename}\" is not a real file.".format(filename=filename)))

file_data.append((open(filename).read(), os.path.basename(filename)))

id = user.write(file_data, private=opts.private)

url = HTTP_GIST_PUBLIC.format(id=id)

Expand All @@ -198,6 +222,23 @@ def main(*args):
else:
sys.stderr.write("No files specified.\n")
return(1)
elif opts.mode == "stdin":
if len(files) > 1:
sys.stderr.write("Warning: Extra arguments ignored\n (stdin is named from the first filename specified, others are ignored)\n")

if files:
filename = files[0]
else:
filename = "gist.txt"

id = user.write([(sys.stdin.read(), filename)])

url = HTTP_GIST_PUBLIC.format(id=id)

if not clip(url):
sys.stderr.writeln("Warning: Unable to copy URL to clipboard.")

print(url)
elif opts.mode == "clone":
if files:
for id in files:
Expand Down

0 comments on commit 067cc12

Please sign in to comment.