Skip to content

Commit d4edfe6

Browse files
committed
Python: Change method of finding the 'common' directory so that the script's directory doesn't have to be the current working directory.
1 parent 775c8e5 commit d4edfe6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

python/emblem.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@
77

88
import datetime
99
import math
10+
import os
1011
import struct
1112
from PIL import Image
1213

1314
YEAR_2000 = datetime.datetime(2000, 1, 1)
1415

16+
# Find this script's directory, then navigate from there to the common
17+
# directory, which has some files we need to read.
18+
# This way of specifying the directory is better than '../common' since the
19+
# script's directory doesn't have to be the current working directory.
20+
# http://stackoverflow.com/a/9350788/
21+
script_dir = os.path.dirname(os.path.realpath(__file__))
22+
common_dir = os.path.join(script_dir, os.pardir, 'common')
23+
24+
1525
def short_filename(filename, seconds_since_start_of_2000):
1626
if not filename:
1727
return "fze0200002000{:14X}.dat".format(int(seconds_since_start_of_2000 * 40500000))
@@ -143,7 +153,8 @@ def banner(img32, alpha_threshold):
143153
emblem file. (The left 2/3rds of the banner are the same for
144154
every emblem.)
145155
"""
146-
banner_base_file = open("../common/emblem_banner_base", 'rb')
156+
banner_base_file = open(
157+
os.path.join(common_dir, 'emblem_banner_base'), 'rb')
147158
banner_bytes = bytearray()
148159
img32_data = img32.getdata()
149160

@@ -176,7 +187,7 @@ def icon():
176187
emblem file's icon, in the same pixel format as any emblem file.
177188
(The icon is the same for every emblem.)
178189
"""
179-
return open("../common/emblem_icon", 'rb').read()
190+
return open(os.path.join(common_dir, 'emblem_icon'), 'rb').read()
180191

181192
def edge_options(img, edge_option):
182193
# Image.LANCZOS constant requires Pillow 2.7 or higher.

0 commit comments

Comments
 (0)