diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | python-from-root.spec | 372 | ||||
-rw-r--r-- | sources | 1 |
3 files changed, 374 insertions, 0 deletions
@@ -0,0 +1 @@ +/from_root-1.3.0.tar.gz diff --git a/python-from-root.spec b/python-from-root.spec new file mode 100644 index 0000000..3411088 --- /dev/null +++ b/python-from-root.spec @@ -0,0 +1,372 @@ +%global _empty_manifest_terminate_build 0 +Name: python-from-root +Version: 1.3.0 +Release: 1 +Summary: Forget about working directory errors +License: MIT License +URL: https://pypi.org/project/from-root/ +Source0: https://mirrors.nju.edu.cn/pypi/web/packages/ae/30/5259cfafc8372df008a5605ca19aba9d560285471ee043f39cbc5a7b7fa2/from_root-1.3.0.tar.gz +BuildArch: noarch + + +%description +# Usage guide + +Are you fed up with that **annoying FileNotFoundError** when your working directory turns out to be something different +from what you expected? Or, maybe, you are looking for an easy and robust way of declaring paths to configs and any data +files in your project? Yeah, that drives us all crazy. + +The package is really tiny, there are two function: + +* `from_root` helps with declaring absolute paths relative to your project root +* `from_here` comes in handy when you want to declare a path relative to the current file + +They are dead simple to use. + +Let's assume we have a project with the next structure: + +``` +├── .git # OPTIONAL. See explanation below +├── .project-root # OPTIONAL. See explanation below +├── config.json +├── assets +│ ├── animation.gif +│ └── logo.png +└── package + ├── __init__.py + ├── data.csv + ├── script.py + ├── FROM_HERE_DEMO.py + └── inner_package + ├── FROM_ROOT_DEMO.py + ├── __init__.py + └── insanely + └── deep + └── dir + └── file.txt +``` + +#### `from_root` examples: + +```python +# <PROJECT_ROOT>/package/inner_package/FROM_ROOT_DEMO.py +from from_root import from_root + +CONFIG_PATH = from_root('config.json') + +PACKAGE_DATA_PATH = from_root('package', 'data.csv') + +# `from_root` returns pathlib.Path object +# so we can take advantage of its fantastic "/" syntax +ASSETS_DIR = from_root('assets') +ANIMATION_PATH = ASSETS_DIR / 'animation.gif' +LOGO_PATH = ASSETS_DIR / 'logo.png' + +# no matter how deep it's located +FILE_TXT_PATH = from_root('package', 'inner_package', 'insanely', 'deep', 'dir', 'file.txt') + +# If `mkdirs` is set to True (False by default), all *args will be treated as dir names +# and created for you. +# If a directory already exists, nothing happens. + +import pickle + +RESULTS_DIR = from_root('package', 'deep', 'results', 'dir', mkdirs=True) +results = { + 'ones': [1, 1, 1], + 'zeros': [2, 2, 2] +} +for name, data in results.items(): + path = RESULTS_DIR / f'{name}.pkl' + # `FileNotFoundError` is not raised because `from_root` has created all non-existing directories + with path.open('wb') as file: + pickle.dump(data, file) + +# WARNING: don't do this, you'll end up with data.json directory: +with from_root('results', 'data.json', mkdirs=True).open('w') as file: + ... + +# Do this instead: +with (from_root('results', mkdirs=True) / 'data.json').open('w') as file: + ... +``` + +#### `from_here` examples: + +```python +# <PROJECT_ROOT>/package/FROM_HERE_DEMO.py +from from_root import from_here + +# The only difference from `from_root` is that `from_here` allows you to declare relative paths +# I think the examples speak for themselves quite good. +# Take a look at tree above and compare with `from_root` examples + +CONFIG_PATH = from_here('data.csv') +FILE_TXT_PATH = from_here('inner_package', 'insanely', 'deep', 'dir', 'file.txt') +``` + +# How does it work? + +When `from_root` is called, folders in the current traceback are looked through one by one in order to find `.git` +directory or `.project-root` file (might be empty; you have to create it on your own). The first one that contains at +least one of them are considered as a root directory. + + +%package -n python3-from-root +Summary: Forget about working directory errors +Provides: python-from-root +BuildRequires: python3-devel +BuildRequires: python3-setuptools +BuildRequires: python3-pip +%description -n python3-from-root +# Usage guide + +Are you fed up with that **annoying FileNotFoundError** when your working directory turns out to be something different +from what you expected? Or, maybe, you are looking for an easy and robust way of declaring paths to configs and any data +files in your project? Yeah, that drives us all crazy. + +The package is really tiny, there are two function: + +* `from_root` helps with declaring absolute paths relative to your project root +* `from_here` comes in handy when you want to declare a path relative to the current file + +They are dead simple to use. + +Let's assume we have a project with the next structure: + +``` +├── .git # OPTIONAL. See explanation below +├── .project-root # OPTIONAL. See explanation below +├── config.json +├── assets +│ ├── animation.gif +│ └── logo.png +└── package + ├── __init__.py + ├── data.csv + ├── script.py + ├── FROM_HERE_DEMO.py + └── inner_package + ├── FROM_ROOT_DEMO.py + ├── __init__.py + └── insanely + └── deep + └── dir + └── file.txt +``` + +#### `from_root` examples: + +```python +# <PROJECT_ROOT>/package/inner_package/FROM_ROOT_DEMO.py +from from_root import from_root + +CONFIG_PATH = from_root('config.json') + +PACKAGE_DATA_PATH = from_root('package', 'data.csv') + +# `from_root` returns pathlib.Path object +# so we can take advantage of its fantastic "/" syntax +ASSETS_DIR = from_root('assets') +ANIMATION_PATH = ASSETS_DIR / 'animation.gif' +LOGO_PATH = ASSETS_DIR / 'logo.png' + +# no matter how deep it's located +FILE_TXT_PATH = from_root('package', 'inner_package', 'insanely', 'deep', 'dir', 'file.txt') + +# If `mkdirs` is set to True (False by default), all *args will be treated as dir names +# and created for you. +# If a directory already exists, nothing happens. + +import pickle + +RESULTS_DIR = from_root('package', 'deep', 'results', 'dir', mkdirs=True) +results = { + 'ones': [1, 1, 1], + 'zeros': [2, 2, 2] +} +for name, data in results.items(): + path = RESULTS_DIR / f'{name}.pkl' + # `FileNotFoundError` is not raised because `from_root` has created all non-existing directories + with path.open('wb') as file: + pickle.dump(data, file) + +# WARNING: don't do this, you'll end up with data.json directory: +with from_root('results', 'data.json', mkdirs=True).open('w') as file: + ... + +# Do this instead: +with (from_root('results', mkdirs=True) / 'data.json').open('w') as file: + ... +``` + +#### `from_here` examples: + +```python +# <PROJECT_ROOT>/package/FROM_HERE_DEMO.py +from from_root import from_here + +# The only difference from `from_root` is that `from_here` allows you to declare relative paths +# I think the examples speak for themselves quite good. +# Take a look at tree above and compare with `from_root` examples + +CONFIG_PATH = from_here('data.csv') +FILE_TXT_PATH = from_here('inner_package', 'insanely', 'deep', 'dir', 'file.txt') +``` + +# How does it work? + +When `from_root` is called, folders in the current traceback are looked through one by one in order to find `.git` +directory or `.project-root` file (might be empty; you have to create it on your own). The first one that contains at +least one of them are considered as a root directory. + + +%package help +Summary: Development documents and examples for from-root +Provides: python3-from-root-doc +%description help +# Usage guide + +Are you fed up with that **annoying FileNotFoundError** when your working directory turns out to be something different +from what you expected? Or, maybe, you are looking for an easy and robust way of declaring paths to configs and any data +files in your project? Yeah, that drives us all crazy. + +The package is really tiny, there are two function: + +* `from_root` helps with declaring absolute paths relative to your project root +* `from_here` comes in handy when you want to declare a path relative to the current file + +They are dead simple to use. + +Let's assume we have a project with the next structure: + +``` +├── .git # OPTIONAL. See explanation below +├── .project-root # OPTIONAL. See explanation below +├── config.json +├── assets +│ ├── animation.gif +│ └── logo.png +└── package + ├── __init__.py + ├── data.csv + ├── script.py + ├── FROM_HERE_DEMO.py + └── inner_package + ├── FROM_ROOT_DEMO.py + ├── __init__.py + └── insanely + └── deep + └── dir + └── file.txt +``` + +#### `from_root` examples: + +```python +# <PROJECT_ROOT>/package/inner_package/FROM_ROOT_DEMO.py +from from_root import from_root + +CONFIG_PATH = from_root('config.json') + +PACKAGE_DATA_PATH = from_root('package', 'data.csv') + +# `from_root` returns pathlib.Path object +# so we can take advantage of its fantastic "/" syntax +ASSETS_DIR = from_root('assets') +ANIMATION_PATH = ASSETS_DIR / 'animation.gif' +LOGO_PATH = ASSETS_DIR / 'logo.png' + +# no matter how deep it's located +FILE_TXT_PATH = from_root('package', 'inner_package', 'insanely', 'deep', 'dir', 'file.txt') + +# If `mkdirs` is set to True (False by default), all *args will be treated as dir names +# and created for you. +# If a directory already exists, nothing happens. + +import pickle + +RESULTS_DIR = from_root('package', 'deep', 'results', 'dir', mkdirs=True) +results = { + 'ones': [1, 1, 1], + 'zeros': [2, 2, 2] +} +for name, data in results.items(): + path = RESULTS_DIR / f'{name}.pkl' + # `FileNotFoundError` is not raised because `from_root` has created all non-existing directories + with path.open('wb') as file: + pickle.dump(data, file) + +# WARNING: don't do this, you'll end up with data.json directory: +with from_root('results', 'data.json', mkdirs=True).open('w') as file: + ... + +# Do this instead: +with (from_root('results', mkdirs=True) / 'data.json').open('w') as file: + ... +``` + +#### `from_here` examples: + +```python +# <PROJECT_ROOT>/package/FROM_HERE_DEMO.py +from from_root import from_here + +# The only difference from `from_root` is that `from_here` allows you to declare relative paths +# I think the examples speak for themselves quite good. +# Take a look at tree above and compare with `from_root` examples + +CONFIG_PATH = from_here('data.csv') +FILE_TXT_PATH = from_here('inner_package', 'insanely', 'deep', 'dir', 'file.txt') +``` + +# How does it work? + +When `from_root` is called, folders in the current traceback are looked through one by one in order to find `.git` +directory or `.project-root` file (might be empty; you have to create it on your own). The first one that contains at +least one of them are considered as a root directory. + + +%prep +%autosetup -n from-root-1.3.0 + +%build +%py3_build + +%install +%py3_install +install -d -m755 %{buildroot}/%{_pkgdocdir} +if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi +if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi +if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi +if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi +pushd %{buildroot} +if [ -d usr/lib ]; then + find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/lib64 ]; then + find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/bin ]; then + find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst +fi +if [ -d usr/sbin ]; then + find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst +fi +touch doclist.lst +if [ -d usr/share/man ]; then + find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst +fi +popd +mv %{buildroot}/filelist.lst . +mv %{buildroot}/doclist.lst . + +%files -n python3-from-root -f filelist.lst +%dir %{python3_sitelib}/* + +%files help -f doclist.lst +%{_docdir}/* + +%changelog +* Thu May 18 2023 Python_Bot <Python_Bot@openeuler.org> - 1.3.0-1 +- Package Spec generated @@ -0,0 +1 @@ +91477e3bb16b6ef61abbf67ee4f3131d from_root-1.3.0.tar.gz |