How do I include and access package resources? - setuptools

I have this folder structure:
my-package -
|- src/mypackage
|- resources/some-resources
|- setup.py
|- setup.cfg
I want to load some-resources from within mypackage. How do I do this?
I have read online I should update my setup.py to include:
setup(
...
package_data={"": ["resources"]},
include_package_data=True,
...
)
And that I should then be able to use pkg_resources to access my resource folder. But the examples seem like they are incomplete, e.g. pkg_resources.resource_listdir("mypackage", "") (one suggestion) just lists the python files in mypackage. So to reiterate - how do I include resources in my package, and then access them from my code?

Related

how to ignore folders in melos packages

working in a monorepo with this structure
monorepo/
examples/
# ...
foo/
packages/
# ...
foo/
# ...
example/ -> examples/foo/
where the example in packages/foo
is a deep link to examples/foo/
that contains a demo app called foo_example
I'm encountering a problem during melos bootstrap
Run melos bootstrap
melos.yaml: Multiple packages with the name `foo_example` found in the workspace, which is unsupported.
To fix this problem, consider renaming your packages to have a unique name.
The packages that caused the problem are:
- foo_example at packages/foo/example
- foo_example at examples/foo
Error: Process completed with exit code 1.
I would like to keep the folders for examples and packages separated
while providing an example readily available for https://pub.dev/
is there a way to exclude the packages/**/example/ folders from melos?
this is how I'm pointing at the packages right now
name: <my_mono_repo>
packages:
- examples/**
- packages/**
scripts:
# ...
always read the docs :)
https://melos.invertase.dev/configuration/overview#ignore
this does the job
ignore:
# e.g. ignore example apps
- "packages/**/example"

How to disable a specific analysis option rule in a folder

I have a project which can be simplified to:
lib/
|- main.dart
test/
test_environment/
|- test_test.dart
Where test_environment contains a set of "special" tests that cannot simply be run with
flutter test
They need some specific setting (for example: flutter test --dart-define=myVar=myValue).
In my lib/main.dart, I have something like:
class MyClass {
#visibleForTesting
MyClass.testConstructor();
}
I would like to use MyClass.testConstructor() from test/test_helper.dart. But when I do, I have a warning:
The member 'MyClass.testConstructor' can only be used within 'package:my_project/lib/main.dart' or a test. dart(invalid_use_of_visible_for_testing_member)`
I would like to disable this rule for the test_environment/ folder, but not for the lib/ folder. How can I do that?
The solution I found was to create a custom analysis_options.yaml for the folder test_environment/ that extends the root analysis_options.yaml:
lib/
|- main.dart
test/
test_environment/
|- test_test.dart
|- analysis_options.yaml
analysis_options.yaml
And in test_environment/analysis_options.yaml:
include: ../analysis_options.yaml
analyzer:
errors:
invalid_use_of_visible_for_testing_member: ignore # <- Disable the analyzer rule.
linter:
rules:
only_throw_errors: false # <- An example to show how to disable a linter rule.
This takes the analyzer/linter rules set by the root analysis_options.yaml and overrides the invalid_use_of_visible_for_testing_member to disable it in test_environment.

Can I specify the output folder for assets?

I have a project with this structure:
src
|- index.pug
| - layout
| |- index.less
| - scripts
| |- index.js
For now, when I run parcel build src/index.pug, all the files are bundled and I've got this on the distribution folder:
dist
|- index.html
|- index.12345.css
|- index.12345.js
When I was spected something like:
dist
|- index.html
|- layout
| |- index.12345.css
|- scripts
| |- index.12345.js
So, my question is: Can I specify the output path for my css, js and images using ParcelJS?
Parcel doesn't support this feature out of the box, but I've developed a plugin for it. Check it out https://www.npmjs.com/package/parcel-plugin-custom-dist-structure
It is suitable for all types of web/node development projects, it will generate the dist structure you specify while also handle your imports.
Let me know what you think!
With version 1 it's not supported. This is supposed to be available in version 2 through user plugins. See https://github.com/parcel-bundler/parcel/issues/233

Setting module name to be different from directory name in SwiftPM

I have a Swift library with a core module plus optional bonus modules. I would like to use the following directory layout, mapping to exported Swift package names as shown:
Taco/
Source/
Core/ → import Taco
Toppings/ → import TacoToppings
SideDishes/ → import TacoSideDishes
To my eyes, that’s a sensible-looking project layout. However, if I’m reading the docs right, this will pollute the global module namespace with unhelpful names like “Core”. It seems that SwiftPM will only export a module whose name is identical to the directory name, and thus I have to do this:
Taco/
Source/
Taco/
TacoToppings/
TacoSideDishes/
Is there a way to configure Package.swift to use the tidier directory layout above and still export the desired module names?
Alternatively, is it possible to make the Core, Toppings, and SideDishes modules internal to the project, and export them all to the world as one big Taco module?
There is not currently a clean way to do this, but it seems like a reasonable request. I recommend filing an enhancement request at http://bugs.swift.org for it.
There is one "hacky" way you can do this:
Create your sources in the desired internal layout:
Sources/Core
Sources/Toppings
Add additional symbolic links for the desired module names:
ln -s Core Sources/Taco
ln -s Toppings Sources/TacoToppings
Add an exclude directive to the manifest to ignore the non-desired module name:
let package = Package(
name: "Taco",
exclude: ["Sources/Core", "Sources/Toppings"]
)
is it possible to make the Core, Toppings, and SideDishes modules internal to the project, and export them all to the world as one big Taco module?
No, unfortunately there is no way to do this currently, and it requires substantial compiler work to be able to support.

How to install a new lexer in pygments?

I wrote a new lexer for pygments and I try to use it. Thus I look at this page
http://pygments.org/docs/lexerdevelopment/
where the install procedure is described. They said to do make mapfiles but I don't know where.
I look into this two directories where there is the other.py module they talk about.
/usr/share/pyshared/pygments/lexers/
and
/usr/share/pyshared/pygments/lexers/
But there is not any makefiles in there. Thus how can I do ?
The blog post Custom syntax in pygments explains another way to add a custom lexer to pygments:
Pygments enables custom plugins via something called entrypoints in setuptools.
Directory structure:
|- FantomLexer
|- fantomlexer
| |- __init__.py
| |- lexer.py
|- setup.py
The __init__.py file can be empty but it needs to be there so its enough to simply touch it. The lexer.py will contain the regex lexer for pygments.
The contents of the setup.py goes as following:
from setuptools import setup, find_packages
setup (
name='fantomlexer',
packages=find_packages(),
entry_points =
"""
[pygments.lexers]
fantomlexer = fantomlexer.lexer:FantomLexer
""",
)
You can then install your lexer via sudo python setup.py develop.
I found a solution which works. I guess you lexer is in the file mylex.py. I did the following under ubuntu 13.10. You need to have root permissions to do that.
Copy your new lexer into /usr/share/pyshared/pygments/lexers/
In the same directory run python _mapping.py
Do a symbolic link of your lexer into /usr/lib/python2.7/dist-packages/pygments/lexers/. For example :
cd /usr/lib/python2.7/dist-packages/pygments/lexers/
ln -s /usr/share/pyshared/pygments/lexers/algobox.py .