Skip to content

Commit

Permalink
Changed to output "using" if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
naitoh committed Mar 11, 2018
1 parent b865f9b commit 1f0b8f1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 30 deletions.
73 changes: 63 additions & 10 deletions py2rb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,34 @@ class RB(object):
with open(filename, 'r') as f:
module_map.update(yaml.load(f))

using_map = {
'EnumerableEx' : False,
'PythonZipEx' : False,
'PythonPrintEx' : True,
'PythonIsBoolEx' : False,
'PythonIndexEx' : False,
'PythonFindEx' : False,
'PythonSplitEx' : False,
'PythonStripEx' : False,
'PythonStringCountEx' : True,
'PythonRemoveEx' : False,
'PythonMethodEx' : False,
}

using_key_map = {
'all' : 'EnumerableEx',
'any' : 'EnumerableEx',
'zip' : 'PythonZipEx',
'find' : 'PythonIndexEx',
'rfind' : 'PythonIndexEx',
'split' : 'PythonSplitEx',
'strip' : 'PythonStripEx',
'lstrip' : 'PythonStripEx',
'rstrip' : 'PythonStripEx',
'remove' : 'PythonRemoveEx',
'getattr' : 'PythonMethodEx',
}

# python 3
name_constant_map = {
True : 'true',
Expand Down Expand Up @@ -204,6 +232,12 @@ def set_result(self, result):
def get_result(self):
return self._result

def set_using(self):
for key, value in self.using_map.items():
if value:
self.write("using %s" % key)
self.write('')

def __init__(self, path='', dir_path='', base_path_count=0, mod_paths = {}, verbose=False):
self._verbose = verbose
self._mode = 0 # Error Stop Mode : 0:stop(defalut), 1:warning(for all script mode), 2:no error(for module mode)
Expand Down Expand Up @@ -974,6 +1008,7 @@ def visit_IfExp(self, node):
if isinstance(node.test, (ast.NameConstant, ast.Compare)):
return "(%s) ? %s : %s" % (self.visit(node.test), body, or_else)
else:
self.using_map['PythonIsBoolEx'] = True
return "is_bool(%s) ? %s : %s" % (self.visit(node.test), body, or_else)

@scope
Expand All @@ -984,6 +1019,7 @@ def visit_If(self, node):
if isinstance(node.test, (ast.NameConstant, ast.Compare)):
self.write("if %s" % self.visit(node.test))
else:
self.using_map['PythonIsBoolEx'] = True
self.write("if is_bool(%s)" % self.visit(node.test))

self.indent()
Expand Down Expand Up @@ -1655,6 +1691,8 @@ def visit_Name(self, node):
id = node.id
try:
if self._call:
if id in self.using_key_map:
self.using_map[self.using_key_map[id]] = True
id = self.func_name_map[id]
else:
if id in self.methods_map.keys():
Expand Down Expand Up @@ -2053,6 +2091,8 @@ def visit_Call(self, node):
"""
return "%s.%s :@%s" % (rb_args[0], self.methods_map_middle[func], rb_args[1][1:-1])
elif func == 'getattr':
self.using_map[self.using_key_map[func]] = True

if len(rb_args) == 2:
return "%s.%s(%s)" % (rb_args[0], self.methods_map_middle[func], rb_args[1])
else:
Expand Down Expand Up @@ -2126,6 +2166,9 @@ def visit_Call(self, node):
<Python> float(foo)
<Ruby> (foo).to_f
"""
if func in self.using_key_map:
self.using_map[self.using_key_map[func]] = True

if not isinstance(self.reverse_methods[func], dict):
return "%s.%s" % (self.ope_filter(rb_args_s), self.reverse_methods[func])
if len(rb_args) == 1:
Expand Down Expand Up @@ -2286,6 +2329,8 @@ def visit_Attribute(self, node):
Attribute(expr value, identifier attr, expr_context ctx)
"""
attr = node.attr
if self._verbose:
print("Attribute attr_name[%s]" % attr)
if (attr != '') and isinstance(node.value, ast.Name) and (node.value.id != 'self'):
mod_attr = "%s.%s" % (self.visit(node.value), attr)
else:
Expand All @@ -2295,6 +2340,9 @@ def visit_Attribute(self, node):
""" [Attribute method converter]
<Python> fuga.append(bar)
<Ruby> fuga.push(bar) """
if attr in self.using_key_map:
self.using_map[self.using_key_map[attr]] = True

attr = self.attribute_map[attr]
if mod_attr in self.attribute_map.keys():
""" [Attribute method converter]
Expand All @@ -2320,6 +2368,8 @@ def visit_Attribute(self, node):
<Python> fuga.split(foo,bar)
<Ruby> fuga.split_p(foo,bar) """
if attr in self.attribute_with_arg.keys():
if attr in self.using_key_map:
self.using_map[self.using_key_map[attr]] = True
attr = self.attribute_with_arg[attr]

if isinstance(node.value, ast.Call):
Expand Down Expand Up @@ -2450,6 +2500,8 @@ def initialize(val, name)
return "%s(%s)" % (attr, self.visit(node.value))
v = self.visit(node.value)
if attr != '':
if attr in self.using_key_map:
self.using_map[self.using_key_map[attr]] = True
return "%s.%s" % (self.ope_filter(v), attr)
else:
return v
Expand Down Expand Up @@ -2634,7 +2686,14 @@ def convert_py2rb(s, dir_path, path='', base_path_count=0, modules=[], mod_paths
else:
v.mode(0)
v.visit(t)
return (v.get_result(), v.read())

data = v.read()
v.clear() # clear self.__buffer

v.set_using()
header = v.read()

return (v.get_result(), header, data)

def convert_py2rb_write(filename, base_path_count=0, subfilenames=[], base_path=None, require=None, builtins=None, output=None, force=None, no_stop=False, verbose=False):
if output:
Expand All @@ -2650,26 +2709,18 @@ def convert_py2rb_write(filename, base_path_count=0, subfilenames=[], base_path=
if require:
if builtins_dir:
require = open(os.path.join(builtins_dir, "require.rb"))
using = open(os.path.join(builtins_dir, "using.rb"))
else:
require = open("require.rb")
using = open("using.rb")
output.write(require.read())
output.write(using.read())
require.close
using.close

if builtins:
if builtins_dir:
builtins = open(os.path.join(builtins_dir, "module.rb"))
using = open(os.path.join(builtins_dir, "using.rb"))
else:
builtins = open("module.rb")
using = open("using.rb")
output.write(builtins.read())
output.write(using.read())
builtins.close
using.close

mods = []
mod_paths = OrderedDict()
Expand All @@ -2693,7 +2744,9 @@ def convert_py2rb_write(filename, base_path_count=0, subfilenames=[], base_path=
dir_path = ''
with open(filename, 'r') as f:
s = f.read() #unsafe for large files!
rtn, data = convert_py2rb(s, dir_path, name_path, base_path_count, mods, mod_paths, no_stop=no_stop, verbose=verbose)
rtn, header, data = convert_py2rb(s, dir_path, name_path, base_path_count, mods, mod_paths, no_stop=no_stop, verbose=verbose)
if require or builtins:
output.write(header)
output.write(data)
output.close
return rtn
Expand Down
12 changes: 0 additions & 12 deletions py2rb/builtins/using.rb

This file was deleted.

1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
'modules/numpy.yaml',
'modules/unittest.yaml',
'builtins/require.rb',
'builtins/using.rb',
'builtins/module.rb', ]
},
install_requires=[
Expand Down
6 changes: 3 additions & 3 deletions test_results.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/using.rb", "py2rb/builtins/require.rb" [4]:
.. [OK]
Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/require.rb" [3]:
.. [OK]
tests/test_builtins.rb [1]: . [OK]
tests/algorithms/sqrt.py [4]: .... [OK]
tests/algorithms/triangulation.py [4]: .... [OK]
Expand Down Expand Up @@ -248,6 +248,6 @@ tests/strings/replace2.py [4]: .. known to [FAIL]
tests/strings/string_format_efg.py [4]: .. known to [FAIL]
tests/strings/string_format_o.py [4]: ... known to [FAIL]
tests/strings/string_format_x.py [4]: ... known to [FAIL]
Ran 249 tests in 118.038s
Ran 249 tests in 118.464s

OK (expected failures=30)
5 changes: 1 addition & 4 deletions testtools/env_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ def runTest(self):
if not os.path.exists("py2rb/builtins/module.rb"):
self.stop()
raise RuntimeError("""Can't find the "py2rb/builtins/module.rb" command.""")
if not os.path.exists("py2rb/builtins/using.rb"):
self.stop()
raise RuntimeError("""Can't find the "py2rb/builtins/using.rb" command.""")
if not os.path.exists("py2rb/builtins/require.rb"):
self.stop()
raise RuntimeError("""Can't find the "py2rb/builtins/require.rb" command.""")
self.reportProgres()

def __str__(self):
return 'Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/using.rb", "py2rb/builtins/require.rb" [4]: '
return 'Looking for "ruby", "py2rb/builtins/module.rb", "py2rb/builtins/require.rb" [3]:'


0 comments on commit 1f0b8f1

Please sign in to comment.