libjam 0.0.9__py3-none-any.whl → 0.0.10__py3-none-any.whl

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.
libjam/drawer.py CHANGED
@@ -10,14 +10,6 @@ PLATFORM = platform.system()
10
10
 
11
11
  # Internal functions
12
12
  joinpath = os.path.join
13
- def realpath(path: str or list):
14
- if type(path) == str:
15
- return os.path.normpath(path)
16
- elif type(path) == list:
17
- result_list = []
18
- for item in path:
19
- result_list.append(os.path.normpath(item))
20
- return result_list
21
13
 
22
14
  def outpath(path: str or list):
23
15
  if type(path) == str:
@@ -28,12 +20,30 @@ def outpath(path: str or list):
28
20
  result_list.append(item.replace(os.sep, '/'))
29
21
  return result_list
30
22
 
23
+ HOME = str(pathlib.Path.home())
24
+
25
+ def realpath(path: str):
26
+ path_prefixes = ['/', '~']
27
+ first_char = path[0]
28
+ if first_char in path_prefixes:
29
+ path = path.replace('~', HOME)
30
+ return os.path.normpath(path)
31
+ else:
32
+ return path
33
+
34
+ def realpaths(path: list):
35
+ result_list = []
36
+ for item in path:
37
+ result_list.append(realpath(item))
38
+ return result_list
31
39
 
32
40
  # Deals with files
33
41
  class Drawer:
34
42
 
35
43
  # Returns True if give a path to folder
36
44
  def is_folder(self, path: str):
45
+ if path == '':
46
+ return False
37
47
  path = realpath(path)
38
48
  return os.path.isdir(path)
39
49
 
@@ -147,13 +157,14 @@ class Drawer:
147
157
 
148
158
  # Sends given file(s)/folder(s) to trash
149
159
  def trash(self, path: str or list):
150
- path = realpath(path)
151
160
  if type(path) == str:
161
+ path = realpath(path)
152
162
  try:
153
163
  send2trash.send2trash(path)
154
164
  except FileNotFoundError:
155
165
  print(f"File '{path}' wasn't found, skipping sending it to trash.")
156
166
  elif type(path) == list:
167
+ path = realpaths(path)
157
168
  for item in path:
158
169
  try:
159
170
  send2trash.send2trash(item)
@@ -166,11 +177,12 @@ class Drawer:
166
177
 
167
178
  # Deletes a given file (using trash_file() instead is recommended)
168
179
  def delete_file(self, path: str or list):
169
- path = realpath(path)
170
180
  if type(path) == str:
181
+ path = realpath(path)
171
182
  if self.is_file(path):
172
183
  os.remove(path)
173
184
  elif type(path) == list:
185
+ path = realpaths(path)
174
186
  for item in path:
175
187
  if self.is_file(path):
176
188
  os.remove(item)
@@ -195,13 +207,14 @@ class Drawer:
195
207
 
196
208
  # Returns the parent folder of given file/folder
197
209
  def get_parent(self, path: str or list):
198
- path = realpath(path)
199
210
  if type(path) == str:
211
+ path = realpath(path)
200
212
  basename = self.basename(path)
201
213
  parent = path.removesuffix(basename)
202
214
  parent = parent.removesuffix(os.sep)
203
215
  return outpath(parent)
204
216
  elif type(path) == list:
217
+ path = realpaths(path)
205
218
  result_list = []
206
219
  for file in path:
207
220
  basename = self.basename(file)
@@ -218,14 +231,15 @@ class Drawer:
218
231
  return depth
219
232
 
220
233
  def basename(self, path: str or list):
221
- path = realpath(path)
222
234
  if type(path) == str:
235
+ path = realpath(path)
223
236
  if self.is_folder(path):
224
237
  path = os.path.basename(os.path.normpath(path))
225
238
  else:
226
239
  path = path.rsplit(os.sep,1)[-1]
227
240
  return outpath(path)
228
241
  elif type(path) == list:
242
+ path = realpaths(path)
229
243
  return_list = []
230
244
  for file in path:
231
245
  if self.is_file(file):
@@ -237,7 +251,7 @@ class Drawer:
237
251
 
238
252
  # Searches for string in list of basenames
239
253
  def search_for_files(self, search_term: str, path: list):
240
- path = realpath(path)
254
+ path = realpaths(path)
241
255
  result_list = []
242
256
  files = self.get_files_recursive(path)
243
257
  for file in files:
@@ -247,15 +261,16 @@ class Drawer:
247
261
  return outpath(result_list)
248
262
 
249
263
  def search_for_folder(self, search_term: str, path: str or list):
250
- path = realpath(path)
251
264
  result_list = []
252
265
  if type(path) == str:
266
+ path = realpath(path)
253
267
  subfolders = self.get_folders(path)
254
268
  for item in subfolders:
255
269
  basename = self.basename(item)
256
270
  if basename == search_term:
257
271
  result_list.append(item)
258
272
  elif type(path) == list:
273
+ path = realpaths(path)
259
274
  for subfolder in path:
260
275
  subfolders = self.get_folders(subfolder)
261
276
  for item in subfolders:
@@ -265,7 +280,6 @@ class Drawer:
265
280
  return outpath(result_list)
266
281
 
267
282
  def search_for_folders(self, search_term: str, path: str or list):
268
- path = realpath(path)
269
283
  result_list = []
270
284
  def search(search_term: str, path: str):
271
285
  folders = self.get_folders_recursive(path)
@@ -274,8 +288,10 @@ class Drawer:
274
288
  if search_term.lower() in basename.lower():
275
289
  result_list.append(folder)
276
290
  if type(path) == str:
291
+ path = realpath(path)
277
292
  search(search_term, path)
278
293
  elif type(path) == list:
294
+ path = realpaths(path)
279
295
  for subfolder in path:
280
296
  search(search_term, subfolder)
281
297
  return outpath(result_list)
@@ -302,6 +318,8 @@ class Drawer:
302
318
  return False
303
319
 
304
320
  # Extracts a given archive
321
+ # progress_function is called every time a file is extracted from archive, and
322
+ # it passes number of extracted files and number of files that need to be extracted (done, total)
305
323
  def extract_archive(self, archive: str, extract_location: str, progress_function=None):
306
324
  archive = realpath(archive)
307
325
  extract_location = realpath(extract_location)
@@ -338,8 +356,7 @@ class Drawer:
338
356
 
339
357
  # Returns the home folder
340
358
  def get_home(self):
341
- home = str(pathlib.Path.home())
342
- return outpath(home)
359
+ return outpath(HOME)
343
360
 
344
361
  # Returns the temporary folder
345
362
  def get_temp(self):
@@ -361,17 +378,17 @@ class Drawer:
361
378
  typewriter.print('Program aborted while gathering size of files.')
362
379
  sys.exit(1)
363
380
 
364
- def open_path(self, path: str):
381
+ def open(self, path: str):
365
382
  path = realpath(path)
366
383
  if PLATFORM == 'Linux':
367
- subprocess.run(['xdg-open', path])
384
+ command = 'xdg-open'
368
385
  elif PLATFORM == 'Windows':
369
- subprocess.run(['start', path])
386
+ command = 'start'
370
387
  elif PLATFORM == 'Darwin':
371
- subprocess.run(['open', path])
388
+ command = 'open'
372
389
  else:
373
390
  return 1
374
- return 0
391
+ return subprocess.run([command, path])
375
392
 
376
393
  def get_platform(self):
377
394
  return PLATFORM
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: libjam
3
- Version: 0.0.9
3
+ Version: 0.0.10
4
4
  Summary: A library jam for Python.
5
5
  Project-URL: Homepage, https://github.com/philippkosarev/libjam
6
6
  Project-URL: Issues, https://github.com/philippkosarev/libjam/issues
@@ -1,11 +1,11 @@
1
1
  libjam/__init__.py,sha256=iLE2y9r0Sfe3oIeoFHKwFIyLL6d_KcLP7fINd7pPAlY,145
2
2
  libjam/captain.py,sha256=igx-ecKJBI_vBN-pW7KmSEnmYMIHQEb9tFzpy5qHmI8,5636
3
3
  libjam/clipboard.py,sha256=5HxlO8ztLJPlnSCq4PncGvY4JGc9C4J2uHcepjC6zmg,3496
4
- libjam/drawer.py,sha256=gy887B5heBbzqT0R_36U3vaGiiR4avsZ4b52Uabhz2I,11491
4
+ libjam/drawer.py,sha256=ZK0baCuLqgmgwABgmig935HQVbQP05UXjd3P0drSSKQ,11975
5
5
  libjam/flashcard.py,sha256=ulV4KPC3BRWLxwkQ87vsY0aM38nYpzOjUOISxTIRVDg,437
6
6
  libjam/notebook.py,sha256=4aLVRVTefiHMip44TERLJAXv88U8G6mYLqc1UvWsEfs,3108
7
7
  libjam/typewriter.py,sha256=waKY1sDxGzI2ZT5dSzFmLGbaNTIpEM5Zhg_OlPFUAng,3730
8
- libjam-0.0.9.dist-info/METADATA,sha256=PDUnOoV4F7RduZA4OJYTL95W-E7tx4eBqWcFJCGE7mo,2140
9
- libjam-0.0.9.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
- libjam-0.0.9.dist-info/licenses/LICENSE,sha256=gXf5dRMhNSbfLPYYTY_5hsZ1r7UU1OaKQEAQUhuIBkM,18092
11
- libjam-0.0.9.dist-info/RECORD,,
8
+ libjam-0.0.10.dist-info/METADATA,sha256=Ql6PwuKRwtHaf01ad0sqxZwtjLmwwTxsMrrE7YFO0Zg,2141
9
+ libjam-0.0.10.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
10
+ libjam-0.0.10.dist-info/licenses/LICENSE,sha256=gXf5dRMhNSbfLPYYTY_5hsZ1r7UU1OaKQEAQUhuIBkM,18092
11
+ libjam-0.0.10.dist-info/RECORD,,