-
Notifications
You must be signed in to change notification settings - Fork 160
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
tushushu
wants to merge
3
commits into
baidu:master
Choose a base branch
from
tushushu:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
||
def generate_line(byte_list): | ||
line = reduce(lambda x, y: x + ', ' + "%s" % str(y), byte_list) | ||
return line + ',' | ||
|
@@ -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" | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we replace all those |
||
print('extern "C" {', file=hfile) | ||
print('#endif', file=hfile) | ||
|
||
for (source, resource_name, c_filename, size) in resource_list: | ||
f = open(source, 'rb') | ||
|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
fromfunctools
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-inreduce
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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