Skip to content

Commit 6b4f529

Browse files
Merge pull request #309 from ricardoquesada/better_templates
gettext
2 parents b278065 + 20c7fcb commit 6b4f529

File tree

5 files changed

+124
-22
lines changed

5 files changed

+124
-22
lines changed

bin/cocos.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import cocos_project
2323
import shutil
2424
import string
25+
import locale
26+
import gettext
2527

28+
# FIXME: MultiLanguage should be deprecated in favor of gettext
2629
from MultiLanguage import MultiLanguage
2730

2831
COCOS2D_CONSOLE_VERSION = '2.0'
@@ -859,6 +862,18 @@ def _check_python_version():
859862
if __name__ == "__main__":
860863
DataStatistic.stat_event('cocos', 'start', 'invoked')
861864

865+
# gettext
866+
locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
867+
language, encoding = locale.getlocale()
868+
if language is not None:
869+
filename = "language_%s.mo" % language[0:2]
870+
try:
871+
trans = gettext.GNUTranslations(open(filename, "rb"))
872+
except IOError:
873+
trans = gettext.NullTranslations()
874+
trans.install()
875+
_ = trans.gettext
876+
862877
# Parse the arguments, specify the language
863878
language_arg = '--ol'
864879
if language_arg in sys.argv:

bin/cocos2d.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ plugin_generate.SimulatorCompiler
2828
plugin_generate.TemplateGenerator
2929
plugin_package.CCPluginPackage
3030
plugin_framework.CCPluginFramework
31+
plugin_gui.CCPluginGUI
3132
#plugin_version.CCPluginVersion
3233
#plugin_install.CCPluginInstall
3334
#plugin_update.CCPluginUpdate

plugins/plugin_gui.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/python
2+
# ----------------------------------------------------------------------------
3+
# cocos2d "gui" plugin
4+
#
5+
# ----------------------------------------------------------------------------
6+
'''
7+
"gui" plugin for cocos2d command line tool
8+
'''
9+
from __future__ import print_function
10+
from __future__ import unicode_literals
11+
12+
__docformat__ = 'restructuredtext'
13+
14+
import cocos
15+
import subprocess
16+
17+
18+
class CCPluginGUI(cocos.CCPlugin):
19+
"""
20+
launches the cocos2d console gui
21+
"""
22+
23+
@staticmethod
24+
def plugin_name():
25+
return "gui"
26+
27+
@staticmethod
28+
def brief_description():
29+
return "shows the GUI"
30+
31+
def run(self, argv, dependencies):
32+
subprocess.Popen(['open', '-a', self.get_console_path() + '/' + 'Cocos2d-Console-GUI.app'])

plugins/plugin_new/project_new.py

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
99
'''
1010
"new" plugin for cocos command line tool
1111
'''
12+
from __future__ import print_function
13+
from __future__ import unicode_literals
1214

1315
__docformat__ = 'restructuredtext'
1416

1517
# python
1618
import os
1719
import sys
18-
import getopt
19-
import ConfigParser
2020
import json
2121
import shutil
2222
import cocos
23-
from MultiLanguage import MultiLanguage
2423
import cocos_project
2524
import re
2625
import utils
2726
from collections import OrderedDict
28-
27+
from MultiLanguage import MultiLanguage
2928

3029
#
3130
# Plugins should be a sublass of CCJSPlugin
@@ -74,9 +73,28 @@ def init(self, args):
7473
self._mac_bundleid = args.mac_bundleid
7574
self._ios_bundleid = args.ios_bundleid
7675

77-
self._templates = Templates(args.language, self._templates_paths, args.template)
78-
if self._templates.none_active():
79-
self._templates.select_one()
76+
self._tpdir = ''
77+
# old way to choose a template from args:
78+
# --language + --template (???)
79+
# new way to choose a template from args:
80+
# --template-name
81+
if args.template_name:
82+
# New Way
83+
dic = Templates.list(self.get_templates_paths())
84+
template_key = args.template_name
85+
if template_key in dic:
86+
self._tpdir = dic[template_key]["path"]
87+
else:
88+
raise cocos.CCPluginError(
89+
"Template name '%s' not found. Available templates: %s" %
90+
(template_key, dic.keys()),
91+
cocos.CCPluginError.ERROR_PATH_NOT_FOUND)
92+
else:
93+
# Old way
94+
self._templates = Templates(args.language, self._templates_paths, args.template)
95+
if self._templates.none_active():
96+
self._templates.select_one()
97+
self._tpdir = self._templates.template_path()
8098

8199
# parse arguments
82100
def parse_args(self, argv):
@@ -85,18 +103,13 @@ def parse_args(self, argv):
85103
from argparse import ArgumentParser
86104
# set the parser to parse input params
87105
# the correspond variable name of "-x, --xxx" is parser.xxx
88-
name = CCPluginNew.plugin_name()
89-
category = CCPluginNew.plugin_category()
90106
parser = ArgumentParser(prog="cocos %s" % self.__class__.plugin_name(),
91107
description=self.__class__.brief_description())
108+
92109
parser.add_argument(
93110
"name", metavar="PROJECT_NAME", nargs='?', help=MultiLanguage.get_string('NEW_ARG_NAME'))
94111
parser.add_argument(
95112
"-p", "--package", metavar="PACKAGE_NAME", help=MultiLanguage.get_string('NEW_ARG_PACKAGE'))
96-
parser.add_argument("-l", "--language",
97-
required=True,
98-
choices=["cpp", "lua", "js"],
99-
help=MultiLanguage.get_string('NEW_ARG_LANG'))
100113
parser.add_argument("-d", "--directory", metavar="DIRECTORY",
101114
help=MultiLanguage.get_string('NEW_ARG_DIR'))
102115
parser.add_argument("-t", "--template", metavar="TEMPLATE_NAME",
@@ -109,16 +122,29 @@ def parse_args(self, argv):
109122
help=MultiLanguage.get_string('NEW_ARG_ENGINE_PATH'))
110123
parser.add_argument("--portrait", action="store_true", dest="portrait",
111124
help=MultiLanguage.get_string('NEW_ARG_PORTRAIT'))
112-
113125
group = parser.add_argument_group(MultiLanguage.get_string('NEW_ARG_GROUP_SCRIPT'))
114126
group.add_argument(
115127
"--no-native", action="store_true", dest="no_native",
116128
help=MultiLanguage.get_string('NEW_ARG_NO_NATIVE'))
117129

130+
# -l | --list-templates
131+
group = parser.add_mutually_exclusive_group(required=True)
132+
group.add_argument("-l", "--language",
133+
choices=["cpp", "lua", "js"],
134+
help=MultiLanguage.get_string('NEW_ARG_LANG'))
135+
group.add_argument("--list-templates", action="store_true",
136+
help='List available templates. To be used with --template option.')
137+
group.add_argument("-k", "--template-name",
138+
help='Name of the template to be used to create the game. To list available names, use --list-templates.')
139+
118140
# parse the params
119141
args = parser.parse_args(argv)
120142

121-
if args.name is None:
143+
if args.list_templates:
144+
print(json.dumps(Templates.list(self.get_templates_paths())))
145+
sys.exit(0)
146+
147+
if args.name is None and args.language is not None:
122148
args.name = CCPluginNew.DEFAULT_PROJ_NAME[args.language]
123149

124150
if not args.package:
@@ -190,10 +216,8 @@ def _create_from_cmd(self):
190216
message = MultiLanguage.get_string('NEW_ERROR_FOLDER_EXISTED_FMT', self._projdir)
191217
raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND)
192218

193-
tp_dir = self._templates.template_path()
194-
195219
creator = TPCreator(self._lang, self._cocosroot, self._projname, self._projdir,
196-
self._tpname, tp_dir, self._package, self._mac_bundleid, self._ios_bundleid)
220+
self._tpname, self._tpdir, self._package, self._mac_bundleid, self._ios_bundleid)
197221
# do the default creating step
198222
creator.do_default_step()
199223

@@ -267,6 +291,34 @@ def replace_string(filepath, src_string, dst_string):
267291

268292
class Templates(object):
269293

294+
@staticmethod
295+
def list(paths):
296+
dirs = []
297+
298+
# enumerate directories and append then into 'dirs'
299+
for template_dir in paths:
300+
try:
301+
dirs += [os.path.join(template_dir, name) for name in os.listdir(template_dir) if os.path.isdir(
302+
os.path.join(template_dir, name))]
303+
except Exception:
304+
continue
305+
306+
# if dir contains the template_metadata.json file, then it is a valid template
307+
valid_templates = {}
308+
for d in dirs:
309+
try:
310+
f = open(os.path.join(d, 'template_metadata', 'config.json'))
311+
# python dictionary
312+
dictionary = json.load(f)
313+
# append current path
314+
dictionary['path'] = d
315+
# use 'key' as key
316+
name = dictionary['key']
317+
valid_templates[name] = dictionary
318+
except Exception:
319+
continue
320+
return valid_templates
321+
270322
def __init__(self, lang, templates_paths, current):
271323
self._lang = lang
272324
self._templates_paths = templates_paths
@@ -353,7 +405,12 @@ def __init__(self, lang, cocos_root, project_name, project_dir, tp_name, tp_dir,
353405
self.tp_dir = tp_dir
354406
self.tp_json = 'cocos-project-template.json'
355407

356-
tp_json_path = os.path.join(tp_dir, self.tp_json)
408+
# search in 'template_metadata' first
409+
tp_json_path = os.path.join(tp_dir, 'template_metadata', self.tp_json)
410+
if not os.path.exists(tp_json_path):
411+
# if not, search in the old place
412+
tp_json_path = os.path.join(tp_dir, self.tp_json)
413+
357414
if not os.path.exists(tp_json_path):
358415
message = MultiLanguage.get_string('NEW_WARNING_FILE_NOT_FOUND_FMT', tp_json_path)
359416
raise cocos.CCPluginError(message, cocos.CCPluginError.ERROR_PATH_NOT_FOUND)

plugins/plugin_test.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
__docformat__ = 'restructuredtext'
1515

16-
import re
17-
import os
1816
import cocos
19-
import inspect
2017

2118

2219
#

0 commit comments

Comments
 (0)