aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPython Deployment User <python-deploy@dezibel.jaseg.net>2013-07-29 21:45:30 +0200
committerPython Deployment User <python-deploy@dezibel.jaseg.net>2013-07-29 21:45:30 +0200
commite34c5035ae4ae49c1bba0a061e2c1b46c4e9b271 (patch)
treeeb73ab2d4c3194718326d9ebd8fb0a76c1fd73f3
parent72ee91cc54f84d763e0a8c8e4c75758c3105f1bc (diff)
downloadponysay-e34c5035ae4ae49c1bba0a061e2c1b46c4e9b271.tar.gz
ponysay-e34c5035ae4ae49c1bba0a061e2c1b46c4e9b271.tar.bz2
ponysay-e34c5035ae4ae49c1bba0a061e2c1b46c4e9b271.zip
Now using pkg_resources for resource access
-rw-r--r--ponysay.py25
-rwxr-xr-xsetup.py10
2 files changed, 15 insertions, 20 deletions
diff --git a/ponysay.py b/ponysay.py
index 779ad17..c70382e 100644
--- a/ponysay.py
+++ b/ponysay.py
@@ -2,6 +2,7 @@
import os, sys, random
from os.path import dirname, realpath, exists
+from pkg_resources import resource_string, resource_listdir, resource_exists
import argparse, textwrap
try:
import re2 as re
@@ -19,24 +20,20 @@ balloonstyles= {'cowsay': (((' ', '', '< '), (' ', '', '> ')), ((' /', '|', '\\
'round': ((('╭││', '', '│╰ '), ('╮││', '', '│╯ ')), (('╭││', '│', '││╰'), ('╮││', '│', '││╯')), '─', '─', '╲', '╱'),
'linux-vt': ((('┌││', '', '│└ '), ('┐││', '', '│┘ ')), (('┌││', '│', '││└'), ('┐││', '│', '││┘')), '─', '─', '\\', '/')}
-ponypath=realpath(dirname(__file__)+'/ponies')
-if not exists(ponypath):
- ponypath=realpath(dirname(__file__)+'/../../ponies')
-
def list_ponies(markQuotes=False):
quotes = lambda n: ' (quotes)' if markQuotes and exists(ponypath+'/'+n+'.quotes') else ''
- return [ f[:-5]+quotes(f[:-5]) for f in os.listdir(ponypath) if not f.endswith('quotes') ]
+ return [ f[:-5]+quotes(f[:-5]) for f in resource_listdir(__name__, 'ponies') if not f.endswith('.quotes') ]
-def list_ponies_with_quotes(markQuotes=False):
- return [ f[:-7] for f in os.listdir(ponypath) if f.endswith('quotes') ]
+def list_ponies_with_quotes():
+ return [ f[:-7] for f in resource_listdir(__name__, 'ponies') if f.endswith('.quotes') ]
def load_pony(name):
- return open(ponypath+'/'+name+'.pony').readlines()
+ return str(resource_string(__name__, 'ponies/'+name+'.pony'), 'utf-8').split('\n')
def random_quote(name):
- quotepath=ponypath+'/'+name+'.quotes'
- if exists(quotepath):
- return random.choice(open(quotepath).read().split('\n\n'))
+ quotepath='ponies/'+name+'.quotes'
+ if resource_exists(__name__, quotepath):
+ return random.choice(str(resource_string(__name__, quotepath), 'utf-8').split('\n\n'))
else:
return None
@@ -54,7 +51,7 @@ def render_balloon(text, balloonstyle, minwidth=0, maxwidth=40, pad=str.center):
return [ l+m+r for l,m,r in zip(leftside, lines, rightside) ]
def render_pony(name, text, balloonstyle, width=80, center=False, centertext=False):
- pony = load_pony(name) #CAUTION: these lines already end with '\n'
+ pony = load_pony(name)
balloon = link_l = link_r = ''
if text:
[link_l, link_r] = balloonstyle[-2:]
@@ -65,8 +62,8 @@ def render_pony(name, text, balloonstyle, width=80, center=False, centertext=Fal
pony[i:i+1] = render_balloon(text, balloonstyle, minwidth=minwidth, maxwidth=int(width/2), pad=str.center if centertext else str.ljust)
break
try:
- first = pony.index('$$$\n')
- second = pony[first+1:].index('$$$\n')
+ first = pony.index('$$$')
+ second = pony[first+1:].index('$$$')
pony[first:] = pony[first+1+second+1:]
except:
pass
diff --git a/setup.py b/setup.py
index 8e26d93..7088f59 100755
--- a/setup.py
+++ b/setup.py
@@ -10,10 +10,8 @@ ver = "1.0"
def read(filename):
return open(os.path.join(os.path.dirname(__file__), filename)).read()
-def dir_copy(dirname, src=None):
- if not src:
- src = dirname
- return (dirname, [src+'/'+f for f in os.listdir(src)])
+def dir_copy(src, dst):
+ return (dst, [src+'/'+f for f in os.listdir(src)])
if sys.version_info < (3,0):
@@ -30,8 +28,8 @@ setup(name = 'ponysay',
author_email = 'ponysay@jaseg.net',
url = 'https://github.com/jaseg/ponysay',
py_modules = ['ponysay'],
- data_files = [dir_copy('quotes'),
- dir_copy('ponies', src='genponies')],
+ data_files = [dir_copy('quotes', 'ponies'),
+ dir_copy('genponies', 'ponies')],
scripts = ['ponysay',
'ponythink',
'termcenter',