blues-lib 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.1
2
+ Name: blues_lib
3
+ Version: 1.0.0
4
+ Summary: UNKNOWN
5
+ Home-page: UNKNOWN
6
+ License: UNKNOWN
7
+ Description: ## Description
8
+ Python library writed by Blues Liu.
9
+
10
+ ### Install
11
+ ```
12
+ pip install blues_lib
13
+ ```
14
+
15
+ ### Usage
16
+ ```py
17
+ from blues_lib.BluesFiler import BluesFiler
18
+
19
+ print(BluesFiler.exists('dir'))
20
+ ```
21
+ Platform: UNKNOWN
22
+ Description-Content-Type: text/markdown
@@ -0,0 +1,14 @@
1
+ ## Description
2
+ Python library writed by Blues Liu.
3
+
4
+ ### Install
5
+ ```
6
+ pip install blues_lib
7
+ ```
8
+
9
+ ### Usage
10
+ ```py
11
+ from blues_lib.BluesFiler import BluesFiler
12
+
13
+ print(BluesFiler.exists('dir'))
14
+ ```
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.1
2
+ Name: blues-lib
3
+ Version: 1.0.0
4
+ Summary: UNKNOWN
5
+ Home-page: UNKNOWN
6
+ License: UNKNOWN
7
+ Description: ## Description
8
+ Python library writed by Blues Liu.
9
+
10
+ ### Install
11
+ ```
12
+ pip install blues_lib
13
+ ```
14
+
15
+ ### Usage
16
+ ```py
17
+ from blues_lib.BluesFiler import BluesFiler
18
+
19
+ print(BluesFiler.exists('dir'))
20
+ ```
21
+ Platform: UNKNOWN
22
+ Description-Content-Type: text/markdown
@@ -0,0 +1,11 @@
1
+ README.md
2
+ setup.cfg
3
+ setup.py
4
+ blues_lib.egg-info/PKG-INFO
5
+ blues_lib.egg-info/SOURCES.txt
6
+ blues_lib.egg-info/dependency_links.txt
7
+ blues_lib.egg-info/requires.txt
8
+ blues_lib.egg-info/top_level.txt
9
+ blues_util/BluesFiler.py
10
+ blues_util/BluesImager.py
11
+ blues_util/__init__.py
@@ -0,0 +1,2 @@
1
+ Pillow>=10.3.0
2
+ Requests>=2.31.0
@@ -0,0 +1 @@
1
+ blues_util
@@ -0,0 +1,148 @@
1
+ import os,requests
2
+
3
+ class BluesFiler:
4
+
5
+ @classmethod
6
+ def removedirs(cls,directory):
7
+ '''
8
+ @description Remove all child dir and files
9
+ @param {string} directory
10
+ '''
11
+ for root, dirs, files in os.walk(directory):
12
+ for file in files:
13
+ os.remove(os.path.join(root, file))
14
+ for dir in dirs:
15
+ os.rmdir(os.path.join(root, dir))
16
+
17
+ @classmethod
18
+ def download(cls,urls,directory,success=None,error=None):
19
+ '''
20
+ @description Download multi files
21
+ @param {list} urls files' remote url
22
+ @param {string} directory : Local directory to save the downloaded files
23
+ @param {function} success : Callback function called on success
24
+ @param {function} error : Callback function called on failure
25
+ @returns {dict} complex result
26
+ '''
27
+
28
+ result = cls.__get_result()
29
+
30
+ if not urls:
31
+ return result
32
+
33
+ for url in urls:
34
+ # download the image
35
+ (code,file_or_msg) = cls.download_one(url,directory)
36
+ if code == 200:
37
+ item = {
38
+ 'url':url,
39
+ 'file':file_or_msg,
40
+ 'callback_value':None
41
+ }
42
+ if success:
43
+ item['callback_value'] = success(file_or_msg)
44
+ result['success']['count']+=1
45
+ result['success']['files'].append(item)
46
+ result['files'].append(file_or_msg)
47
+ result['code'] = 200
48
+ else:
49
+ item = {
50
+ 'url':url,
51
+ 'message':file_or_msg,
52
+ 'callback_value':None
53
+ }
54
+ if error:
55
+ item['callback_value'] = error(str(e))
56
+ result['error']['count']+=1
57
+ result['error']['files'].append(item)
58
+
59
+ return result
60
+
61
+ @classmethod
62
+ def download_one(cls,url,directory):
63
+ '''
64
+ @description : download one file
65
+ @param {str} url : file's remote url
66
+ @param {str} directory : The dir to save the download file
67
+ '''
68
+ try:
69
+ # Ensure directory existence
70
+ cls.makedirs(directory)
71
+ # Keep the file name unchanged
72
+ file_name = url.split('/')[-1]
73
+ local_file = directory+'/'+file_name
74
+
75
+ # 永远保持覆盖
76
+ res=requests.get(url)
77
+ res.raise_for_status()
78
+ with open(local_file,'wb') as f:
79
+ f.write(res.content)
80
+ f.close()
81
+ return (200,local_file)
82
+
83
+ except Exception as e:
84
+ return (500,str(e))
85
+
86
+ @classmethod
87
+ def __get_result(cls):
88
+ return {
89
+ 'code':500,
90
+ 'files':[],
91
+ 'success':{
92
+ 'count':0,
93
+ 'files':[],
94
+ },
95
+ 'error':{
96
+ 'count':0,
97
+ 'files':[],
98
+ },
99
+ }
100
+
101
+ @classmethod
102
+ def write(cls,file,text,mode='w'):
103
+ '''
104
+ @description : write text to file
105
+ @param {str} file : file's path
106
+ @param {str} text : content
107
+ @param {str} mode : write mode
108
+ - 'w' : clear the history content
109
+ - 'a' : append text
110
+ @returns {None}
111
+ '''
112
+ with open(file,mode,encoding='utf-8') as file:
113
+ file.write(text)
114
+
115
+ @classmethod
116
+ def exists(cls,path):
117
+ '''
118
+ @description : Does a dir or file exist
119
+ @param {str} path
120
+ @returns {bool}
121
+ '''
122
+ return os.path.exists(path)
123
+
124
+ @classmethod
125
+ def makedirs(cls,path):
126
+ '''
127
+ @description : Create dirs (support multilevel directory) if they don't exist
128
+ @param {str} path : multilevel dir
129
+ @returns {None}
130
+ '''
131
+ if not cls.exists(path):
132
+ os.makedirs(path)
133
+
134
+ @classmethod
135
+ def get_rename_file(cls,file_path,new_name='',prefix='',suffix='',separator='-'):
136
+ '''
137
+ @description : get the new file name path
138
+ '''
139
+ path_slices = file_path.split('/')
140
+ original_name = path_slices[-1]
141
+ copy_name = new_name if new_name else original_name
142
+ if prefix:
143
+ copy_name = prefix+separator+copy_name
144
+ if suffix:
145
+ copy_name = copy_name+separator+suffix
146
+ path_slices[-1]=copy_name
147
+ copy_path='/'.join(path_slices)
148
+ return copy_path
@@ -0,0 +1,82 @@
1
+ from PIL import Image
2
+ from .BluesFiler import BluesFiler
3
+
4
+ class BluesImager:
5
+
6
+ @classmethod
7
+ def download(cls,urls,directory,target_size=None):
8
+ success = lambda local_image : cls.resize(local_image,target_size)
9
+ return BluesFiler.download(urls,directory,success)
10
+
11
+ @classmethod
12
+ def __get_scale_size(cls,current_size,target_size):
13
+ '''
14
+ @description : Gets the smallest scale size
15
+ @param {tuple} current_size : current the image's real size
16
+ @param {tuple} target_size : the target size ,the will may be not scale
17
+ @returns {tuple} new size
18
+ '''
19
+ (current_width,current_height) = current_size
20
+ (target_width,target_height) = target_size
21
+
22
+ if not target_size or (not target_width and not target_height):
23
+ return current_size
24
+
25
+ if target_width and not target_height:
26
+ target_height = int(target_width*current_height/current_width)
27
+
28
+ if target_height and not target_width:
29
+ target_width = int(target_height*current_width/current_height)
30
+
31
+ # get the scale min target size
32
+ if target_width and target_height:
33
+ width_ratio = target_width/current_width
34
+ height_ratio = target_height/current_height
35
+ # With a bigger scale as the standard
36
+ if width_ratio>height_ratio:
37
+ target_height = int(target_width*current_height/current_width)
38
+ else:
39
+ target_width = int(target_height*current_width/current_height)
40
+ return (target_width,target_height)
41
+
42
+ @classmethod
43
+ def resize(cls,local_image,target_size):
44
+ '''
45
+ @description : set image's size
46
+ @param {str} local_image
47
+ @param {tuple} target_size : (width,height)
48
+ '''
49
+
50
+ # 等比例设置
51
+ with Image.open(local_image) as im:
52
+
53
+ current_size = im.size
54
+ scale_size = cls.__get_scale_size(current_size,target_size)
55
+
56
+ copy_image = cls.__copy(im,local_image,'','original')
57
+ new_image = im.resize(scale_size)
58
+
59
+ # convert rgba to rgb; or can't save to jpeg
60
+ rgb_image = new_image.convert('RGB')
61
+ rgb_image.save(local_image)
62
+
63
+ return {
64
+ 'original_image':copy_image,
65
+ 'original_size':current_size,
66
+ 'target_size':target_size,
67
+ 'size':scale_size
68
+ }
69
+
70
+ @classmethod
71
+ def copy(cls,local_image,new_name='',prefix='',suffix='',separator='-'):
72
+ with Image.open(local_image) as im:
73
+ return cls.__copy(im,local_image,new_name,prefix,suffix,separator)
74
+
75
+ @classmethod
76
+ def __copy(cls,im,local_image,new_name='',prefix='',suffix='',separator='-'):
77
+ img_copy = im.copy()
78
+ path = BluesFiler.get_rename_file(local_image,new_name,prefix,suffix,separator)
79
+ img_copy.save(path)
80
+ return path
81
+
82
+
File without changes
@@ -0,0 +1,7 @@
1
+ [metadata]
2
+ description-file = README.md
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+
@@ -0,0 +1,13 @@
1
+ from setuptools import setup,find_packages
2
+
3
+ setup(
4
+ name="blues_lib", # package name
5
+ version="1.0.0", # package version
6
+ long_description=open('README.md').read(),
7
+ long_description_content_type='text/markdown',
8
+ packages=find_packages(), # package module
9
+ install_requires=[
10
+ 'Pillow>=10.3.0',
11
+ 'Requests>=2.31.0'
12
+ ] # package dependency
13
+ )