Skip to content

Commit f71cc78

Browse files
committed
Initial commit
0 parents  commit f71cc78

File tree

5 files changed

+348
-0
lines changed

5 files changed

+348
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.*.swp
2+
*.pyc

README.rst

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SQLAlchemy-ImageAttach
2+
======================
3+
4+
SQLAlchemy-ImageAttach is a SQLAlchemy extension for attaching images to
5+
entity objects.
6+
7+
8+
Links
9+
-----
10+
11+
GitHub
12+
https://github.com/crosspop/sqlalchemy-imageattach
13+
14+
Author Website
15+
http://dahlia.kr/

ez_setup.py

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
#!python
2+
"""Bootstrap setuptools installation
3+
4+
If you want to use setuptools in your package's setup.py, just include this
5+
file in the same directory with it, and add this to the top of your setup.py::
6+
7+
from ez_setup import use_setuptools
8+
use_setuptools()
9+
10+
If you want to require a specific version of setuptools, set a download
11+
mirror, or use an alternate download directory, you can do so by supplying
12+
the appropriate options to ``use_setuptools()``.
13+
14+
This file can also be run as a script to install or upgrade setuptools.
15+
"""
16+
import os
17+
import shutil
18+
import sys
19+
import tempfile
20+
import tarfile
21+
import optparse
22+
23+
from distutils import log
24+
25+
try:
26+
from site import USER_SITE
27+
except ImportError:
28+
USER_SITE = None
29+
30+
try:
31+
import subprocess
32+
33+
def _python_cmd(*args):
34+
args = (sys.executable,) + args
35+
return subprocess.call(args) == 0
36+
37+
except ImportError:
38+
# will be used for python 2.3
39+
def _python_cmd(*args):
40+
args = (sys.executable,) + args
41+
# quoting arguments if windows
42+
if sys.platform == 'win32':
43+
def quote(arg):
44+
if ' ' in arg:
45+
return '"%s"' % arg
46+
return arg
47+
args = [quote(arg) for arg in args]
48+
return os.spawnl(os.P_WAIT, sys.executable, *args) == 0
49+
50+
DEFAULT_VERSION = "0.7b4"
51+
DEFAULT_URL = "http://pypi.python.org/packages/source/s/setuptools/"
52+
53+
54+
def _install(tarball, install_args=()):
55+
# extracting the tarball
56+
tmpdir = tempfile.mkdtemp()
57+
log.warn('Extracting in %s', tmpdir)
58+
old_wd = os.getcwd()
59+
try:
60+
os.chdir(tmpdir)
61+
tar = tarfile.open(tarball)
62+
_extractall(tar)
63+
tar.close()
64+
65+
# going in the directory
66+
subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
67+
os.chdir(subdir)
68+
log.warn('Now working in %s', subdir)
69+
70+
# installing
71+
log.warn('Installing Setuptools')
72+
if not _python_cmd('setup.py', 'install', *install_args):
73+
log.warn('Something went wrong during the installation.')
74+
log.warn('See the error message above.')
75+
# exitcode will be 2
76+
return 2
77+
finally:
78+
os.chdir(old_wd)
79+
shutil.rmtree(tmpdir)
80+
81+
82+
def _build_egg(egg, tarball, to_dir):
83+
# extracting the tarball
84+
tmpdir = tempfile.mkdtemp()
85+
log.warn('Extracting in %s', tmpdir)
86+
old_wd = os.getcwd()
87+
try:
88+
os.chdir(tmpdir)
89+
tar = tarfile.open(tarball)
90+
_extractall(tar)
91+
tar.close()
92+
93+
# going in the directory
94+
subdir = os.path.join(tmpdir, os.listdir(tmpdir)[0])
95+
os.chdir(subdir)
96+
log.warn('Now working in %s', subdir)
97+
98+
# building an egg
99+
log.warn('Building a Setuptools egg in %s', to_dir)
100+
_python_cmd('setup.py', '-q', 'bdist_egg', '--dist-dir', to_dir)
101+
102+
finally:
103+
os.chdir(old_wd)
104+
shutil.rmtree(tmpdir)
105+
# returning the result
106+
log.warn(egg)
107+
if not os.path.exists(egg):
108+
raise IOError('Could not build the egg.')
109+
110+
111+
def _do_download(version, download_base, to_dir, download_delay):
112+
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
113+
% (version, sys.version_info[0], sys.version_info[1]))
114+
if not os.path.exists(egg):
115+
tarball = download_setuptools(version, download_base,
116+
to_dir, download_delay)
117+
_build_egg(egg, tarball, to_dir)
118+
sys.path.insert(0, egg)
119+
import setuptools
120+
setuptools.bootstrap_install_from = egg
121+
122+
123+
def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
124+
to_dir=os.curdir, download_delay=15):
125+
# making sure we use the absolute path
126+
to_dir = os.path.abspath(to_dir)
127+
was_imported = 'pkg_resources' in sys.modules or \
128+
'setuptools' in sys.modules
129+
try:
130+
import pkg_resources
131+
except ImportError:
132+
return _do_download(version, download_base, to_dir, download_delay)
133+
try:
134+
pkg_resources.require("setuptools>=" + version)
135+
return
136+
except pkg_resources.VersionConflict:
137+
e = sys.exc_info()[1]
138+
if was_imported:
139+
sys.stderr.write(
140+
"The required version of setuptools (>=%s) is not available,\n"
141+
"and can't be installed while this script is running. Please\n"
142+
"install a more recent version first, using\n"
143+
"'easy_install -U setuptools'."
144+
"\n\n(Currently using %r)\n" % (version, e.args[0]))
145+
sys.exit(2)
146+
else:
147+
del pkg_resources, sys.modules['pkg_resources'] # reload ok
148+
return _do_download(version, download_base, to_dir,
149+
download_delay)
150+
except pkg_resources.DistributionNotFound:
151+
return _do_download(version, download_base, to_dir,
152+
download_delay)
153+
154+
155+
def download_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
156+
to_dir=os.curdir, delay=15):
157+
"""Download setuptools from a specified location and return its filename
158+
159+
`version` should be a valid setuptools version number that is available
160+
as an egg for download under the `download_base` URL (which should end
161+
with a '/'). `to_dir` is the directory where the egg will be downloaded.
162+
`delay` is the number of seconds to pause before an actual download
163+
attempt.
164+
"""
165+
# making sure we use the absolute path
166+
to_dir = os.path.abspath(to_dir)
167+
try:
168+
from urllib.request import urlopen
169+
except ImportError:
170+
from urllib2 import urlopen
171+
tgz_name = "setuptools-%s.tar.gz" % version
172+
url = download_base + tgz_name
173+
saveto = os.path.join(to_dir, tgz_name)
174+
src = dst = None
175+
if not os.path.exists(saveto): # Avoid repeated downloads
176+
try:
177+
log.warn("Downloading %s", url)
178+
src = urlopen(url)
179+
# Read/write all in one block, so we don't create a corrupt file
180+
# if the download is interrupted.
181+
data = src.read()
182+
dst = open(saveto, "wb")
183+
dst.write(data)
184+
finally:
185+
if src:
186+
src.close()
187+
if dst:
188+
dst.close()
189+
return os.path.realpath(saveto)
190+
191+
192+
def _extractall(self, path=".", members=None):
193+
"""Extract all members from the archive to the current working
194+
directory and set owner, modification time and permissions on
195+
directories afterwards. `path' specifies a different directory
196+
to extract to. `members' is optional and must be a subset of the
197+
list returned by getmembers().
198+
"""
199+
import copy
200+
import operator
201+
from tarfile import ExtractError
202+
directories = []
203+
204+
if members is None:
205+
members = self
206+
207+
for tarinfo in members:
208+
if tarinfo.isdir():
209+
# Extract directories with a safe mode.
210+
directories.append(tarinfo)
211+
tarinfo = copy.copy(tarinfo)
212+
tarinfo.mode = 448 # decimal for oct 0700
213+
self.extract(tarinfo, path)
214+
215+
# Reverse sort directories.
216+
if sys.version_info < (2, 4):
217+
def sorter(dir1, dir2):
218+
return cmp(dir1.name, dir2.name)
219+
directories.sort(sorter)
220+
directories.reverse()
221+
else:
222+
directories.sort(key=operator.attrgetter('name'), reverse=True)
223+
224+
# Set correct owner, mtime and filemode on directories.
225+
for tarinfo in directories:
226+
dirpath = os.path.join(path, tarinfo.name)
227+
try:
228+
self.chown(tarinfo, dirpath)
229+
self.utime(tarinfo, dirpath)
230+
self.chmod(tarinfo, dirpath)
231+
except ExtractError:
232+
e = sys.exc_info()[1]
233+
if self.errorlevel > 1:
234+
raise
235+
else:
236+
self._dbg(1, "tarfile: %s" % e)
237+
238+
239+
def _build_install_args(options):
240+
"""
241+
Build the arguments to 'python setup.py install' on the setuptools package
242+
"""
243+
install_args = []
244+
if options.user_install:
245+
if sys.version_info < (2, 6):
246+
log.warn("--user requires Python 2.6 or later")
247+
raise SystemExit(1)
248+
install_args.append('--user')
249+
return install_args
250+
251+
def _parse_args():
252+
"""
253+
Parse the command line for options
254+
"""
255+
parser = optparse.OptionParser()
256+
parser.add_option(
257+
'--user', dest='user_install', action='store_true', default=False,
258+
help='install in user site package (requires Python 2.6 or later)')
259+
parser.add_option(
260+
'--download-base', dest='download_base', metavar="URL",
261+
default=DEFAULT_URL,
262+
help='alternative URL from where to download the setuptools package')
263+
options, args = parser.parse_args()
264+
# positional arguments are ignored
265+
return options
266+
267+
def main(version=DEFAULT_VERSION):
268+
"""Install or upgrade setuptools and EasyInstall"""
269+
options = _parse_args()
270+
tarball = download_setuptools(download_base=options.download_base)
271+
return _install(tarball, _build_install_args(options))
272+
273+
if __name__ == '__main__':
274+
sys.exit(main())

setup.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os.path
2+
3+
try:
4+
from setuptools import setup, find_packages
5+
except ImportError:
6+
from ez_setup import use_setuptools
7+
use_setuptools()
8+
from setuptools import setup, find_packages
9+
10+
11+
def readme():
12+
try:
13+
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as f:
14+
return f.read()
15+
except (IOError, OSError):
16+
return ''
17+
18+
19+
setup(
20+
name='SQLAlchemy-ImageAttach',
21+
version='0.8.0a1',
22+
description='SQLAlchemy extension for attaching images to entity objects',
23+
long_description=readme(),
24+
author='Hong Minhee',
25+
author_email='minhee' '@' 'dahlia.kr',
26+
license='MIT License',
27+
packages=find_packages(exclude=['tests']),
28+
install_requires=['SQLAlchemy >= 0.8.0', 'Wand >= 0.2.0'],
29+
tests_require=['pytest >= 2.3.0'],
30+
classifiers=[
31+
'Development Status :: 3 - Alpha',
32+
'Intended Audience :: Developers',
33+
'License :: OSI Approved :: MIT License',
34+
'Operating System :: OS Independent',
35+
'Programming Language :: Python :: 2.6',
36+
'Programming Language :: Python :: 2.7',
37+
'Programming Language :: Python :: 3.3',
38+
'Programming Language :: Python :: Implementation :: CPython',
39+
'Programming Language :: Python :: Implementation :: PyPy',
40+
'Programming Language :: Python :: Implementation :: Stackless',
41+
'Topic :: Database :: Front-Ends',
42+
'Topic :: Multimedia :: Graphics'
43+
]
44+
)

sqlalchemy_imageattach/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
""":mod:`sqlalchemy_imageattach` --- SQLAlchemy-ImageAttach
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
This package provides a simple way to attach images to the other
5+
object-relationally mapped entities and store these into the physically
6+
agnostic backend storages.
7+
8+
For example, you can simply store image files into the filesystem, and
9+
then deploy your application into the production, make the production to
10+
use AWS S3 instead. The common backend interface concists of only
11+
essential operations, so you can easily implement a new storage backend.
12+
13+
"""

0 commit comments

Comments
 (0)