Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resource_library.py #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions resource_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
${resource_src_name} is mapped from src_name by replacing '/' or '.' to '_'.

"""

from __future__ import print_function
import os
import sys


# Assume 'reduce' fuction needs to be imported in this way in future versions.
version_info = int(sys.version.split('.')[0])
if version_info > 2:
from functools import reduce
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just use reduce from functools in python2 and python3 since the doc https://docs.python.org/2/library/functools.html#functools.reduce states that it's the same function with built-in reduce

Copy link
Contributor Author

@tushushu tushushu Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for letting me know this, and I prefer to use "from functools import reduce" directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I will wait until you push a new change



def generate_line(byte_list):
line = reduce(lambda x, y: x + ', ' + "%s" % str(y), byte_list)
return line + ','
Expand All @@ -46,7 +52,7 @@ def generate_line(byte_list):
def generate_resource_and_src_list(base_dir, src_list):
resource_list = []
for src in src_list:
prefix = os.path.commonprefix({base_dir, src})
prefix = os.path.commonprefix([base_dir, src])
component = src[len(prefix):].lstrip('/')
resource_name = component.replace('/', '_').replace('.', '_')
c_filename = resource_name + ".c"
Expand All @@ -68,9 +74,9 @@ def generate_resource_and_src_list(base_dir, src_list):
resource_list = generate_resource_and_src_list(base_dir, srcs)

hfile = open(header_file, 'w')
print >> hfile, '#ifdef __cplusplus'
print >> hfile, 'extern "C" {'
print >> hfile, '#endif'
print('#ifdef __cplusplus', file=hfile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we replace all those print calls? You can use ''' ''' to output multiple lines of text for better understanding.

print('extern "C" {', file=hfile)
print('#endif', file=hfile)

for (source, resource_name, c_filename, size) in resource_list:
f = open(source, 'rb')
Expand All @@ -87,16 +93,16 @@ def generate_resource_and_src_list(base_dir, src_list):
if len(byte_list) > 0:
line_list.append(generate_line(byte_list))

print >> hfile, "extern const char RESOURCE_%s[%d];" % (resource_name, size)
print("extern const char RESOURCE_%s[%d];" % (resource_name, size), file=hfile)
with open(c_filename, "w") as c_file:
print >> c_file, "const char RESOURCE_%s[] = {" % resource_name
line_list[len(line_list) - 1] = line_list[len(line_list) - 1][ : -1]
print("const char RESOURCE_%s[] = {" % resource_name, file=c_file)
line_list[len(line_list) - 1] = line_list[len(line_list) - 1][:-1]
for line in line_list:
print >> c_file, ' ', line
print >> c_file, '};'
print >> c_file, "unsigned int %s_len = %d;" % (resource_name, size)
print(' ' + line, file=c_file)
print('};', file=c_file)
print("unsigned int %s_len = %d;" % (resource_name, size), file=c_file)

print >> hfile, '#ifdef __cplusplus'
print >> hfile, '}'
print >> hfile, '#endif'
print('#ifdef __cplusplus', file=hfile)
print('}', file=hfile)
print('#endif', file = hfile)
hfile.close()