dcicutils 8.8.4.1b19__py3-none-any.whl → 8.8.4.1b21__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
dcicutils/file_utils.py CHANGED
@@ -10,6 +10,8 @@ from tempfile import gettempdir as get_temporary_directory
10
10
  from typing import List, Optional, Union
11
11
  from uuid import uuid4 as uuid
12
12
 
13
+ HOME_DIRECTORY = str(pathlib.Path().home())
14
+
13
15
 
14
16
  def search_for_file(file: str,
15
17
  location: Union[str, Optional[List[str]]] = None,
@@ -44,7 +46,7 @@ def search_for_file(file: str,
44
46
  directory = str(directory)
45
47
  if not (directory := directory.strip()):
46
48
  continue
47
- if os.path.isfile(directory := os.path.normpath(directory)):
49
+ if os.path.isfile(directory := os.path.abspath(os.path.normpath(directory))):
48
50
  # Allow a file; assume its parent directory was intended.
49
51
  if not (directory := os.path.dirname(directory)):
50
52
  continue
@@ -77,24 +79,43 @@ def search_for_file(file: str,
77
79
  return None if single is True else []
78
80
 
79
81
 
80
- def normalize_file_path(path: str, home_directory: bool = True) -> str:
82
+ def normalize_path(value: Union[str, pathlib.Path], absolute: bool = False, home: Optional[bool] = None) -> str:
81
83
  """
82
- Normalizes the given file path name and returns. Does things like remove multiple
83
- consecutive slashes and redundant/unnecessary parent paths; if the home_directory
84
- argument is True (the default) then also handles the special tilde home directory
85
- component/convention and uses this in the result if applicable.
84
+ Normalizes the given path value and returns the result; does things like remove redundant
85
+ consecutive directory separators and redundant parent paths. If the given absolute argument
86
+ is True than converts the path to an absolute path. If the given home argument is True and
87
+ if the path can reasonably be represented with a home directory indicator (i.e. "~"), then
88
+ converts it to such. If the home argument is False and path starts with the home directory
89
+ indicator then expands it to the actual (absolute) home path. If the given value is not
90
+ actually even a string (or pathlib.Path) then returns an empty string.
86
91
  """
87
- if not isinstance(path, str) or not path:
88
- path = os.getcwd()
89
- path = os.path.normpath(path)
90
- home_directory = os.path.expanduser("~") if home_directory is True else None
91
- if home_directory and path.startswith("~"):
92
- path = os.path.join(home_directory, path[2 if path.startswith("~/") else 1:])
93
- path = os.path.abspath(path)
94
- if home_directory and (os.name == "posix"):
95
- if path.startswith(home_directory) and path != home_directory:
96
- path = "~/" + pathlib.Path(path).relative_to(home_directory).as_posix()
97
- return path
92
+ if isinstance(value, pathlib.Path):
93
+ value = str(value)
94
+ elif not isinstance(value, str):
95
+ return ""
96
+ if not (value := value.strip()) or not (value := os.path.normpath(value)):
97
+ return ""
98
+ if home is False:
99
+ value = os.path.expanduser(value)
100
+ elif (home is True) and (os.name == "posix") and value.startswith(home := HOME_DIRECTORY + os.sep):
101
+ value = "~/" + value[len(home):]
102
+ if absolute is True:
103
+ value = os.path.abspath(value)
104
+ return value
105
+
106
+
107
+ def get_file_size(file: str) -> Optional[int]:
108
+ try:
109
+ return os.path.getsize(file) if isinstance(file, str) else None
110
+ except Exception:
111
+ return None
112
+
113
+
114
+ def get_file_modified_datetime(file: str) -> Optional[datetime]:
115
+ try:
116
+ return datetime.fromtimestamp(os.path.getmtime(file)) if isinstance(file, str) else None
117
+ except Exception:
118
+ return None
98
119
 
99
120
 
100
121
  def are_files_equal(filea: str, fileb: str) -> bool:
dcicutils/misc_utils.py CHANGED
@@ -2551,6 +2551,19 @@ def normalize_spaces(value: str) -> str:
2551
2551
  return re.sub(r"\s+", " ", value).strip()
2552
2552
 
2553
2553
 
2554
+ def normalize_string(value: Optional[str]) -> Optional[str]:
2555
+ """
2556
+ Strips leading/trailing spaces, and converts multiple consecutive spaces to a single space
2557
+ in the given string value and returns the result. If the given value is None returns an
2558
+ empty string. If the given value is not actually even a string then return None.
2559
+ """
2560
+ if value is None:
2561
+ return ""
2562
+ elif isinstance(value, str):
2563
+ return value.strip()
2564
+ return None
2565
+
2566
+
2554
2567
  def find_nth_from_end(string: str, substring: str, nth: int) -> int:
2555
2568
  """
2556
2569
  Returns the index of the nth occurrence of the given substring within
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dcicutils
3
- Version: 8.8.4.1b19
3
+ Version: 8.8.4.1b21
4
4
  Summary: Utility package for interacting with the 4DN Data Portal and other 4DN resources
5
5
  Home-page: https://github.com/4dn-dcic/utils
6
6
  License: MIT
@@ -28,7 +28,7 @@ dcicutils/es_utils.py,sha256=ZksLh5ei7kRUfiFltk8sd2ZSfh15twbstrMzBr8HNw4,7541
28
28
  dcicutils/exceptions.py,sha256=4giQGtpak-omQv7BP6Ckeu91XK5fnDosC8gfdmN_ccA,9931
29
29
  dcicutils/ff_mocks.py,sha256=6RKS4eUiu_Wl8yP_8V0CaV75w4ZdWxdCuL1CVlnMrek,36918
30
30
  dcicutils/ff_utils.py,sha256=oIhuZPnGtfwj6bWyCc1u23JbMB_6InPp01ZqUOljd8M,73123
31
- dcicutils/file_utils.py,sha256=qiK6aNVamFt3OIARsLAgk2b0ggeUOurLpM9MZ6zperw,9164
31
+ dcicutils/file_utils.py,sha256=haP4JIqE5T0pmAY_pjibEIN8a98_B3v-BXq8fTifzL0,9905
32
32
  dcicutils/function_cache_decorator.py,sha256=XMyiEGODVr2WoAQ68vcoX_9_Xb9p8pZXdXl7keU8i2g,10026
33
33
  dcicutils/glacier_utils.py,sha256=Q4CVXsZCbP-SoZIsZ5NMcawDfelOLzbQnIlQn-GdlTo,34149
34
34
  dcicutils/http_utils.py,sha256=Je5ErNjR5e6lfSXGRncK_lcR_-zP38PIpmHjApy9Wi4,1289
@@ -44,7 +44,7 @@ dcicutils/license_policies/park-lab-gpl-pipeline.jsonc,sha256=vLZkwm3Js-kjV44nug
44
44
  dcicutils/license_policies/park-lab-pipeline.jsonc,sha256=9qlY0ASy3iUMQlr3gorVcXrSfRHnVGbLhkS427UaRy4,283
45
45
  dcicutils/license_utils.py,sha256=d1cq6iwv5Ju-VjdoINi6q7CPNNL7Oz6rcJdLMY38RX0,46978
46
46
  dcicutils/log_utils.py,sha256=7pWMc6vyrorUZQf-V-M3YC6zrPgNhuV_fzm9xqTPph0,10883
47
- dcicutils/misc_utils.py,sha256=eVZ3lEkDebweKCeza2GIo7x3qEqqkj61Ilr17eMFlR0,105744
47
+ dcicutils/misc_utils.py,sha256=ZW2ElovbsrPoMFco6GmZ24zJHyvx6Ky-csr3TDlOli4,106213
48
48
  dcicutils/obfuscation_utils.py,sha256=fo2jOmDRC6xWpYX49u80bVNisqRRoPskFNX3ymFAmjw,5963
49
49
  dcicutils/opensearch_utils.py,sha256=V2exmFYW8Xl2_pGFixF4I2Cc549Opwe4PhFi5twC0M8,1017
50
50
  dcicutils/portal_object_utils.py,sha256=gDXRgPsRvqCFwbC8WatsuflAxNiigOnqr0Hi93k3AgE,15422
@@ -73,8 +73,8 @@ dcicutils/trace_utils.py,sha256=g8kwV4ebEy5kXW6oOrEAUsurBcCROvwtZqz9fczsGRE,1769
73
73
  dcicutils/validation_utils.py,sha256=cMZIU2cY98FYtzK52z5WUYck7urH6JcqOuz9jkXpqzg,14797
74
74
  dcicutils/variant_utils.py,sha256=2H9azNx3xAj-MySg-uZ2SFqbWs4kZvf61JnK6b-h4Qw,4343
75
75
  dcicutils/zip_utils.py,sha256=_Y9EmL3D2dUZhxucxHvrtmmlbZmK4FpSsHEb7rGSJLU,3265
76
- dcicutils-8.8.4.1b19.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
77
- dcicutils-8.8.4.1b19.dist-info/METADATA,sha256=IwpM9ti6SQF6OSqSmVrbebNxVZvBppO4k49ndV7JHAA,3440
78
- dcicutils-8.8.4.1b19.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
79
- dcicutils-8.8.4.1b19.dist-info/entry_points.txt,sha256=51Q4F_2V10L0282W7HFjP4jdzW4K8lnWDARJQVFy_hw,270
80
- dcicutils-8.8.4.1b19.dist-info/RECORD,,
76
+ dcicutils-8.8.4.1b21.dist-info/LICENSE.txt,sha256=qnwSmfnEWMl5l78VPDEzAmEbLVrRqQvfUQiHT0ehrOo,1102
77
+ dcicutils-8.8.4.1b21.dist-info/METADATA,sha256=L64XCFbcLBijgZiY6g823l5rAh7tFDsYU1OmsD1KuKo,3440
78
+ dcicutils-8.8.4.1b21.dist-info/WHEEL,sha256=7Z8_27uaHI_UZAc4Uox4PpBhQ9Y5_modZXWMxtUi4NU,88
79
+ dcicutils-8.8.4.1b21.dist-info/entry_points.txt,sha256=51Q4F_2V10L0282W7HFjP4jdzW4K8lnWDARJQVFy_hw,270
80
+ dcicutils-8.8.4.1b21.dist-info/RECORD,,