aboutsummaryrefslogtreecommitdiff
path: root/py/frozenmod.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-05-03 01:47:08 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-05-03 01:47:08 +0300
commitd7da2dba07778fa3c0c7873b7c253bde48066e9c (patch)
treee03df2d1f0cda9842914a7c752b9b2893ce031c4 /py/frozenmod.c
parent4c2fa83f2afa1dc9522755261c8a6969857a2031 (diff)
py/modio: Implement uio.resource_stream(package, resource_path).
The with semantics of this function is close to pkg_resources.resource_stream() function from setuptools, which is the canonical way to access non-source files belonging to a package (resources), regardless of what medium the package uses (e.g. individual source files vs zip archive). In the case of MicroPython, this function allows to access resources which are frozen into the executable, besides accessing resources in the file system. This is initial stage of the implementation, which actually doesn't implement "package" part of the semantics, just accesses frozen resources from "root", or filesystem resource - from current dir.
Diffstat (limited to 'py/frozenmod.c')
-rw-r--r--py/frozenmod.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/py/frozenmod.c b/py/frozenmod.c
index 660167eed..1eaaf574a 100644
--- a/py/frozenmod.c
+++ b/py/frozenmod.c
@@ -43,16 +43,16 @@ extern const char mp_frozen_str_names[];
extern const uint32_t mp_frozen_str_sizes[];
extern const char mp_frozen_str_content[];
-STATIC mp_lexer_t *mp_find_frozen_str(const char *str, size_t len) {
+// On input, *len contains size of name, on output - size of content
+const char *mp_find_frozen_str(const char *str, size_t *len) {
const char *name = mp_frozen_str_names;
size_t offset = 0;
for (int i = 0; *name != 0; i++) {
size_t l = strlen(name);
- if (l == len && !memcmp(str, name, l)) {
- qstr source = qstr_from_strn(name, l);
- mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, mp_frozen_str_content + offset, mp_frozen_str_sizes[i], 0);
- return lex;
+ if (l == *len && !memcmp(str, name, l)) {
+ *len = mp_frozen_str_sizes[i];
+ return mp_frozen_str_content + offset;
}
name += l + 1;
offset += mp_frozen_str_sizes[i] + 1;
@@ -60,6 +60,19 @@ STATIC mp_lexer_t *mp_find_frozen_str(const char *str, size_t len) {
return NULL;
}
+STATIC mp_lexer_t *mp_lexer_frozen_str(const char *str, size_t len) {
+ size_t name_len = len;
+ const char *content = mp_find_frozen_str(str, &len);
+
+ if (content == NULL) {
+ return NULL;
+ }
+
+ qstr source = qstr_from_strn(str, name_len);
+ mp_lexer_t *lex = MICROPY_MODULE_FROZEN_LEXER(source, content, len, 0);
+ return lex;
+}
+
#endif
#if MICROPY_MODULE_FROZEN_MPY
@@ -124,7 +137,7 @@ mp_import_stat_t mp_frozen_stat(const char *str) {
int mp_find_frozen_module(const char *str, size_t len, void **data) {
#if MICROPY_MODULE_FROZEN_STR
- mp_lexer_t *lex = mp_find_frozen_str(str, len);
+ mp_lexer_t *lex = mp_lexer_frozen_str(str, len);
if (lex != NULL) {
*data = lex;
return MP_FROZEN_STR;