62
62
# URL to file. (TODO)
63
63
FILE_TYPE_HTTP = 2
64
64
65
+ # Default list of libraries in micropython-lib to search for library packages.
66
+ BASE_LIBRARY_NAMES = ("micropython" , "python-stdlib" , "python-ecosys" )
67
+
65
68
66
69
class ManifestFileError (Exception ):
67
70
pass
@@ -196,6 +199,12 @@ def __init__(self, mode, path_vars=None):
196
199
self ._metadata = [ManifestPackageMetadata ()]
197
200
# Registered external libraries.
198
201
self ._libraries = {}
202
+ # List of directories to search for packages.
203
+ self ._library_dirs = []
204
+ # Add default micropython-lib libraries if $(MPY_LIB_DIR) has been specified.
205
+ if self ._path_vars ["MPY_LIB_DIR" ]:
206
+ for lib in BASE_LIBRARY_NAMES :
207
+ self .add_library (lib , os .path .join ("$(MPY_LIB_DIR)" , lib ))
199
208
10000
200
209
def _resolve_path (self , path ):
201
210
# Convert path to an absolute path, applying variable substitutions.
@@ -398,18 +407,16 @@ def _require_from_path(self, library_path, name, version, extra_kwargs):
398
407
return True
399
408
return False
400
409
401
- def require (self , name , version = None , unix_ffi = False , pypi = None , library = None , ** kwargs ):
410
+ def require (self , name , version = None , pypi = None , library = None , ** kwargs ):
402
411
"""
403
412
Require a package by name from micropython-lib.
404
413
405
- Optionally specify unix_ffi=True to use a module from the unix-ffi directory.
406
-
407
414
Optionally specify pipy="package-name" to indicate that this should
408
415
use the named package from PyPI when building for CPython.
409
416
410
417
Optionally specify library="name" to reference a package from a
411
418
library that has been previously registered with add_library(). Otherwise
412
- micropython-lib will be used.
419
+ the list of library paths will be used.
413
420
"""
414
421
self ._metadata [- 1 ].check_initialised (self ._mode )
415
422
@@ -426,39 +433,35 @@ def require(self, name, version=None, unix_ffi=False, pypi=None, library=None, *
426
433
raise ValueError ("Unknown library '{}' for require('{}')." .format (library , name ))
427
434
library_path = self ._libraries [library ]
428
435
# Search for {library_path}/**/{name}/manifest.py.
429
- if not self ._require_from_path (library_path , name , version , kwargs ):
430
- raise ValueError (
431
- "Package '{}' not found in external library '{}' ({})." . format (
432
- name , library , library_path
433
- )
436
+ if self ._require_from_path (library_path , name , version , kwargs ):
437
+ return
438
+ raise ValueError (
439
+ "Package '{}' not found in external library '{}' ({})." . format (
440
+ name , library , library_path
434
441
)
435
- elif self ._path_vars ["MPY_LIB_DIR" ]:
436
- # Find package in micropython-lib, in one of the three top-level directories.
437
- lib_dirs = ["micropython" , "python-stdlib" , "python-ecosys" ]
438
- if unix_ffi :
439
- # Additionally search unix-ffi only if unix_ffi=True, and make unix-ffi modules
440
- # take precedence.
441
- lib_dirs = ["unix-ffi" ] + lib_dirs
442
-
443
- for lib_dir in lib_dirs :
444
- # Search for {lib_dir}/**/{name}/manifest.py.
445
- if self ._require_from_path (
446
- os .path .join (self ._path_vars ["MPY_LIB_DIR" ], lib_dir ), name , version , kwargs
447
- ):
448
- return
449
-
450
- raise ValueError ("Package '{}' not found in local micropython-lib." .format (name ))
451
- else :
452
- # TODO: HTTP request to obtain URLs from manifest.json.
453
- raise ValueError ("micropython-lib not available for require('{}')." , name )
442
+ )
454
443
455
- def add_library (self , library , library_path ):
444
+ for lib_dir in self ._library_dirs :
445
+ # Search for {lib_dir}/**/{name}/manifest.py.
446
+ if self ._require_from_path (lib_dir , name , version , kwargs ):
447
+ return
448
+
449
+ raise ValueError ("Package '{}' not found in any known library." .format (name ))
450
+
451
+ def add_library (self , library , library_path , prepend = False ):
456
452
"""
457
453
Register the path to an external named library.
458
454
459
- This allows require("name", library="library") to find packages in that library.
455
+ The path will be automatically searched when using require(). By default the
456
+ added library is added to the end of the list of libraries to search. Pass
457
+ `prepend=True` to add it to the start of the list.
458
+
459
+ Additionally, the added library can be explicitly requested by using
460
+ `require("name", library="library")`.
460
461
"""
461
- self ._libraries [library ] = self ._resolve_path (library_path )
462
+ library_path = self ._resolve_path (library_path )
463
+ self ._libraries [library ] = library_path
464
+ self ._library_dirs .insert (0 if prepend else len (self ._library_dirs ), library_path )
462
465
463
466
def package (self , package_path , files = None , base_path = "." , opt = None ):
464
467
"""
0 commit comments