Skip to content

Commit 1c0232e

Browse files
feat: add library structure, togheter with exception and logger classes (#1) (#2)
* feat: add library structure, togheter with exception and logger classes (#1) * feat: configured ZcsLogging to support enhanced local logging and cloud format (#1) --------- Co-authored-by: Sofia Galante <s.galante@zcscompany.com>
1 parent bc48e47 commit 1c0232e

16 files changed

Lines changed: 510 additions & 0 deletions

File tree

.build/dockerfiles/Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#
2+
# STAGE 1 - Dev
3+
#
4+
# Dev image used for local development
5+
#
6+
FROM zcscompany/python:3.11-dev AS dev
7+
8+
ARG FIX_UID
9+
ARG FIX_GID
10+
ARG DOCKER_USER=bob
11+
ARG DOCKER_GROUP=bob
12+
13+
USER 0
14+
RUN /fix-perm.sh
15+
16+
RUN pip install --upgrade pip
17+
18+
# Run as normal user
19+
USER ${DOCKER_USER}
20+
21+
22+
#
23+
# STAGE 2 - Dist
24+
#
25+
# Image used for application distribution
26+
#
27+
FROM zcscompany/python:3.11-dist AS dist
28+
29+
ARG DOCKER_USER=bob
30+
ARG DOCKER_GROUP=bob
31+
32+
USER 0
33+
RUN pip install --upgrade pip
34+
35+
# Run as normal user
36+
USER ${DOCKER_USER}
37+
38+
# Copy application requirement file
39+
COPY --chown=$DOCKER_USER:$DOCKER_GROUP app/requirements.txt .
40+
41+
# Install app requirements
42+
RUN pip install --user --no-cache-dir --disable-pip-version-check -r requirements.txt
43+
44+
# Copy application code
45+
COPY --chown=$DOCKER_USER:$DOCKER_GROUP app/ .
46+
47+
# Build and install the library
48+
RUN python -m build && \
49+
pip install --user --no-cache-dir --disable-pip-version-check --editable .

.data/bob-s-home/.bashrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
PS1='\[\033[01;32m\][\[\033[01;32;4m\]${APP_NAME}\[\033[00m\]\[\033[01;32m\]]\[\033[00m\]:\[\033[01;33m\]\w\[\033[00m\]\$ \[\033[00m\]'
3+
4+
alias ls="ls --color"

.dockerignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
**/__pycache__
3+
**/.pytest_cache
4+
**/*.egg-info/
5+
6+
.data/
7+
.github/
8+
.vscode/
9+
10+
app/dist
11+
app/ai-accounting*json
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: PR - Build and test library
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
docker-build-and-test:
9+
name: Build dist docker image and run tests
10+
runs-on: ubuntu-latest
11+
12+
permissions:
13+
contents: 'read'
14+
id-token: 'write'
15+
16+
steps:
17+
- id: checkout
18+
name: Checkout project
19+
uses: actions/checkout@v4
20+
21+
- id: build
22+
name: Build dist docker image
23+
run: docker build -f .build/dockerfiles/Dockerfile -t dist .
24+
25+
- id: test
26+
name: Run python tests on dist image
27+
run: docker run --rm dist pytest

.github/workflows/publish-tag.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Publish application tag
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
setup-build-publish-deploy:
10+
name: Setup, Build, Publish tagged package
11+
runs-on: ubuntu-latest
12+
13+
permissions:
14+
contents: 'read'
15+
id-token: 'write'
16+
17+
steps:
18+
- id: checkout
19+
name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- id: auth
23+
name: Authenticate with Google Cloud
24+
uses: google-github-actions/auth@v2
25+
with:
26+
token_format: access_token
27+
workload_identity_provider: projects/51853309262/locations/global/workloadIdentityPools/my-pool/providers/my-provider
28+
service_account: github-actions-sa@ai-accounting-405809.iam.gserviceaccount.com
29+
access_token_lifetime: 600s
30+
31+
- name: Setup Python
32+
uses: actions/setup-python@v3
33+
34+
- name: Install Dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
python -m pip install build
38+
python -m pip install wheel
39+
python -m pip install twine
40+
python -m pip install keyring keyrings.google-artifactregistry-auth
41+
42+
- name: Build wheel
43+
working-directory: ./app
44+
run: python -m build
45+
46+
- name: 'Set up Cloud SDK'
47+
uses: 'google-github-actions/setup-gcloud@v2'
48+
with:
49+
version: '>= 363.0.0'
50+
51+
- name: Upload to Artifact Registry
52+
working-directory: ./app
53+
run: |
54+
gcloud config set artifacts/repository python
55+
gcloud config set artifacts/location europe-west1
56+
gcloud artifacts print-settings python > ~/.pypirc
57+
python -m twine upload --repository python dist/*

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# ZCS Python Core Library
2+
3+
This repository contains the core python library developed by Madness Lab Team at Zucchetti Centro Sistemi.
4+
5+
The library provides a `Logger` allowing the user to receive logging messages from Uvicorn and send them to Google Cloud.
6+
7+
```text
8+
9+
░▒▓████████▓▒░░▒▓██████▓▒░ ░▒▓███████▓▒░
10+
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
11+
░▒▓██▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
12+
░▒▓██▓▒░ ░▒▓█▓▒░ ░▒▓██████▓▒░
13+
░▒▓██▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░
14+
░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
15+
░▒▓████████▓▒░░▒▓██████▓▒░░▒▓███████▓▒░
16+
17+
18+
░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓███████▓▒░
19+
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
20+
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
21+
░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░ ░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
22+
░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
23+
░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
24+
░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░
25+
26+
27+
░▒▓██████▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░░▒▓████████▓▒░
28+
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
29+
░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
30+
░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░░▒▓██████▓▒░
31+
░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
32+
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
33+
░▒▓██████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░
34+
35+
36+
```
37+
38+
## Usage
39+
40+
### Requirements
41+
42+
The library doesn't require directly third party dependencies. This will let you install only the requirements needed by the utils you will effectively use.
43+
44+
### Use the library in your project
45+
46+
To use the library from ZCS repository you need to have a valid access.
47+
48+
Add the following line to the top of your `requirements.txt` file:
49+
50+
```bash
51+
--extra-index-url https://europe-west1-python.pkg.dev/ai-accounting-405809/python/simple/
52+
```
53+
54+
Then add the library `zcs-python-core` to the same file.
55+
56+
In your code, you can import and use the library like this:
57+
58+
```python
59+
from zcs.core.exception import ZCSException
60+
```
61+
62+
```python
63+
from zcs.core.logger import Logger
64+
```
65+
66+
## Local development
67+
68+
### Run library tests
69+
70+
If you want to run libraray tests against your local docker development environment, you can run:
71+
72+
```bash
73+
./run.sh -t
74+
```
75+
76+
### Run tests in a dist docker image
77+
78+
To be sure that anything is going the right way, you can run the library tests against a dist docker image. This will assure that anything is working properly in a fresh installed environment.
79+
80+
```bash
81+
./run.sh -i
82+
```
83+
84+
### Other commands and help
85+
86+
```bash
87+
./run.sh -h
88+
```
89+
90+
## Support
91+
92+
[Claudio Cavina](mailto:c.cavina@zcscompany.com)
93+
[Michele Mondelli](mailto:m.mondelli@zcscompany.com)

app/pyproject.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "zcs-python-core"
7+
version = "0.1.0"
8+
description = "A Python library that provides core functionalities for ZCS projects"
9+
10+
[tool.setuptools.packages.find]
11+
where = ["./src"]

app/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build==1.2.1
2+
pytest==8.3.3
3+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .exception import ZcsException as ZcsException
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import random
2+
import string
3+
4+
class ZcsException(Exception):
5+
6+
def __init__(self, user_message, error_source = "Unspecified", status_code = 500, internal_message = None):
7+
super().__init__(user_message)
8+
9+
self.__error_source = error_source
10+
self.__status_code = status_code
11+
self.__user_message = user_message
12+
self.__internal_message = internal_message
13+
self.__error_code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))
14+
15+
def get_status_code(self):
16+
return self.__status_code
17+
18+
def get_error_source(self):
19+
return self.__error_source
20+
21+
def get_user_message(self):
22+
return self.__user_message
23+
24+
def get_internal_message(self):
25+
return self.__internal_message
26+
27+
def get_error_code(self):
28+
return self.__error_code
29+
30+
def get_as_dict(self):
31+
return {
32+
'error_code': self.get_error_code(),
33+
'user_message': self.get_user_message(),
34+
'internal_message': self.get_internal_message(),
35+
'error_source': self.get_error_source(),
36+
'status_code': self.get_status_code()
37+
}

0 commit comments

Comments
 (0)