Skip to content

Commit fcf91cb

Browse files
committed
Fix packaging and loading of csv data files.
1 parent 42eae45 commit fcf91cb

File tree

3 files changed

+72
-75
lines changed

3 files changed

+72
-75
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
include LICENSE.txt
22
include CONTRIBUTORS.txt
33
include README.txt
4+
recursive-include sc2reader *.csv

sc2reader/data/__init__.py

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
from __future__ import absolute_import
22

3-
import os
4-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
5-
6-
unit_file_format = '{}_units.csv'
7-
abil_file_format = '{}_abilities.csv'
3+
import pkgutil
84

95
ABIL_LOOKUP = dict()
10-
with open(os.path.join(BASE_DIR, 'ability_lookup.csv'), 'r') as data_file:
11-
for entry in data_file:
12-
str_id, abilities = entry.split(',',1)
13-
ABIL_LOOKUP[str_id] = abilities.split(',')
6+
for entry in pkgutil.get_data('sc2reader.data', 'ability_lookup.csv').split('\n'):
7+
if not entry: continue
8+
str_id, abilities = entry.split(',',1)
9+
ABIL_LOOKUP[str_id] = abilities.split(',')
1410

1511
UNIT_LOOKUP = dict()
16-
with open(os.path.join(BASE_DIR, 'unit_lookup.csv'), 'r') as data_file:
17-
for entry in data_file:
18-
str_id, title = entry.strip().split(',')
19-
UNIT_LOOKUP[str_id] = title
20-
12+
for entry in pkgutil.get_data('sc2reader.data', 'unit_lookup.csv').split('\n'):
13+
if not entry: continue
14+
str_id, title = entry.strip().split(',')
15+
UNIT_LOOKUP[str_id] = title
2116

2217
# TODO: Costs need to be version stats. Not here
2318
unit_lookup = {'Protoss':{
@@ -297,81 +292,81 @@ def __repr__(self):
297292
class Ability(object):
298293
pass
299294

300-
def load_build(data_dir, version):
295+
def load_build(expansion, version):
301296
print("Loading build {}".format(version))
302-
unit_file = os.path.join(data_dir,unit_file_format.format(version))
303-
abil_file = os.path.join(data_dir,abil_file_format.format(version))
297+
unit_file = '{}/{}_units.csv'.format(expansion,version)
298+
abil_file = '{}/{}_abilities.csv'.format(expansion,version)
304299

305300
units = dict()
306-
with open(unit_file, 'r') as data_file:
307-
for entry in data_file:
308-
int_id, str_id = entry.strip().split(',')
309-
unit_type = int(int_id,10)
310-
title = UNIT_LOOKUP[str_id]
311-
312-
values = dict(cost=[0,0,0], race='Neutral',is_army=False, is_building=False, is_worker=False)
313-
race, minerals, vespene, supply = "Neutral", 0, 0, 0
314-
for race in ('Protoss','Terran','Zerg'):
315-
if title.lower() in unit_lookup[race]:
316-
values.update(unit_lookup[race][title.lower()])
317-
values['race']=race
318-
break
319-
320-
units[unit_type] = type(title,(Unit,), dict(
321-
type=unit_type,
322-
name=title,
323-
title=title,
324-
race=values['race'],
325-
minerals=values['cost'][0],
326-
vespene=values['cost'][1],
327-
supply=values['cost'][2],
328-
is_building=values['is_building'],
329-
is_worker=values['is_worker'],
330-
is_army=values['is_army'],
331-
))
301+
for entry in pkgutil.get_data('sc2reader.data', unit_file).split('\n'):
302+
if not entry: continue
303+
int_id, str_id = entry.strip().split(',')
304+
unit_type = int(int_id,10)
305+
title = UNIT_LOOKUP[str_id]
306+
307+
values = dict(cost=[0,0,0], race='Neutral',is_army=False, is_building=False, is_worker=False)
308+
race, minerals, vespene, supply = "Neutral", 0, 0, 0
309+
for race in ('Protoss','Terran','Zerg'):
310+
if title.lower() in unit_lookup[race]:
311+
values.update(unit_lookup[race][title.lower()])
312+
values['race']=race
313+
break
314+
315+
units[unit_type] = type(title,(Unit,), dict(
316+
type=unit_type,
317+
name=title,
318+
title=title,
319+
race=values['race'],
320+
minerals=values['cost'][0],
321+
vespene=values['cost'][1],
322+
supply=values['cost'][2],
323+
is_building=values['is_building'],
324+
is_worker=values['is_worker'],
325+
is_army=values['is_army'],
326+
))
332327

333328
# TODO: Should RightClick be in the main data files?
334329
abilities = {0:type('RightClick',(Ability,), dict(type=0, name='RightClick', title='Right Click', is_build=False, build_time=None, build_unit=None))}
335-
with open(abil_file, 'r') as data_file:
336-
for entry in data_file:
337-
int_id_base, str_id = entry.strip().split(',')
338-
int_id_base = int(int_id_base,10) << 5
339-
340-
abils = ABIL_LOOKUP[str_id] # The entry must exist
341-
real_abils = [(int_id_base|i,abil) for i,abil in enumerate(abils) if abil.strip()!='']
342-
343-
if len(real_abils) == 0:
344-
# TODO: Should we issue a warning?
345-
# A: We'd have to fill in all the blanks, probably not worth it
346-
abilities[int_id_base] = type(str_id,(Ability,), dict(
347-
type=int_id_base,
348-
name=str_id,
349-
title=str_id,
330+
for entry in pkgutil.get_data('sc2reader.data', abil_file).split('\n'):
331+
if not entry: continue
332+
int_id_base, str_id = entry.strip().split(',')
333+
int_id_base = int(int_id_base,10) << 5
334+
335+
abils = ABIL_LOOKUP[str_id] # The entry must exist
336+
real_abils = [(int_id_base|i,abil) for i,abil in enumerate(abils) if abil.strip()!='']
337+
338+
if len(real_abils) == 0:
339+
# TODO: Should we issue a warning?
340+
# A: We'd have to fill in all the blanks, probably not worth it
341+
abilities[int_id_base] = type(str_id,(Ability,), dict(
342+
type=int_id_base,
343+
name=str_id,
344+
title=str_id,
345+
is_build=False,
346+
build_time=None,
347+
build_unit=None
348+
))
349+
350+
else:
351+
for index, ability in real_abils:
352+
int_id = int_id_base | index
353+
abilities[int_id] = type(ability,(Ability,), dict(
354+
type=int_id,
355+
name=ability,
356+
title=ability,
350357
is_build=False,
351358
build_time=None,
352359
build_unit=None
353360
))
354361

355-
else:
356-
for index, ability in real_abils:
357-
int_id = int_id_base | index
358-
abilities[int_id] = type(ability,(Ability,), dict(
359-
type=int_id,
360-
name=ability,
361-
title=ability,
362-
is_build=False,
363-
build_time=None,
364-
build_unit=None
365-
))
366-
367362
data = Build(version, units, abilities)
368363
for unit in units.values():
369364
setattr(data, unit.name, unit)
370365
for ability in abilities.values():
371366
if ability.name in train_commands:
372367
unit_name, build_time = train_commands[ability.name]
373368
# Side affect of using the same ability lookup for all versions
374-
# BuildBattleHellion will register for all versions because if FactoryTrain
369+
# BuildBattleHellion will register for all versions because of FactoryTrain
375370
# This shouldn't hurt though because the ability can't actually be used
376371
# and will never be looked up in the ability dictionary.
377372
if hasattr(data,unit_name):
@@ -383,16 +378,15 @@ def load_build(data_dir, version):
383378
return data
384379

385380

381+
386382
# Load the WoL Data
387383
wol_builds = dict()
388-
data_dir = os.path.join(BASE_DIR, 'WoL')
389384
for version in ('16117','17326','18092','19458','22612'):
390-
wol_builds[version] = load_build(data_dir, version)
385+
wol_builds[version] = load_build('WoL', version)
391386

392387
# Load HotS Data
393388
hots_builds = dict()
394-
data_dir = os.path.join(BASE_DIR, 'HotS')
395389
for version in ('base',):
396-
hots_builds[version] = load_build(data_dir, version)
390+
hots_builds[version] = load_build('HotS', version)
397391

398392
builds = {'WoL':wol_builds,'HotS':hots_builds}

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@
3737
},
3838
install_requires=['mpyq','argparse'] if float(sys.version[:3]) < 2.7 else ['mpyq'],
3939
packages=['sc2reader', 'sc2reader.scripts', 'sc2reader.plugins', 'sc2reader.data'],
40+
package_data={'sc2reader.data':['*.csv']},
41+
include_package_data=True,
4042
zip_safe=True
4143
)

0 commit comments

Comments
 (0)