1. Ed Brannin
  2. app_sizer

Commits

Ed Brannin  committed eeecdda

Improved iTunes Library & Backup location on Windows; extracted a common method.

  • Participants
  • Parent commits 247276e
  • Branches default

Comments (0)

Files changed (4)

File app_sizer/app_import.py

View file
 from zipfile import ZipFile
 
 from .chart.models import App
+from .utils import first_existing_home_subdir
+
 # import logging_config
 
 log = logging.getLogger(__name__)
             u"My Documents/My Music/iTunes/",
             u"My Music/iTunes/",
             ]
-    paths_tried = []
-    for path in paths:
-        for root in [os.environ[x] for x in ["HOME", "USERPROFILE"] if x in os.environ]:
-            ep = os.path.join(root, path)
-            paths_tried.append(ep)
-            if os.path.exists(ep):
-                return ep
-    raise IOError("Couldn't find any iTunes libraries in {}".format(paths_tried))
+    answer = first_existing_home_subdir(*paths)
+    if answer is None:
+        raise IOError("Couldn't find any iTunes libraries in {}".format(paths_tried))
+    else:
+        return answer
 
 def apps_path():
     "Find this system's iOS Apps folder in the iTunes library"
-    return os.path.join(itunes_library_path(),
+    return os.path.normpath(
+            os.path.join(itunes_library_path(),
             u'iTunes Music/Mobile Applications/')
+            )
 
 def scan_all(path=None):
     if path == None:

File app_sizer/backup_import.py

View file
 
 # import treemap
 
+from .utils import first_existing_home_subdir
+
 def backups_path():
     """
     Find this system's iOS Backup folders.
     See also http://support.apple.com/kb/ht4946
     """
     paths = [
-            '~/Library/Application Support/MobileSync/Backup/',
-            '~/Application Data/Apple Computer/MobileSync/Backup/',
-            '~/AppData/Roaming/Apple Computer/MobileSync/Backup/',
+            'Library/Application Support/MobileSync/Backup/',
+            'Application Data/Apple Computer/MobileSync/Backup/',
+            'AppData/Roaming/Apple Computer/MobileSync/Backup/',
             ]
-    for path in paths:
-        ep = os.path.expanduser(path)
-        if os.path.exists(ep):
-            return ep
-    raise IOError("Couldn't find any MobileSync Backup folers in {}".format(paths))
+    answer = first_existing_home_subdir(*paths)
+    if answer is None:
+        raise IOError("Couldn't find any MobileSync Backup folers in {}".format(paths))
+    else:
+        return answer
 
 def backup_names(parent_path=None):
     if parent_path is None:

File app_sizer/utils.py

View file
 '''Helper utilities and decorators.'''
 from flask import flash
 
+import os
+
 def flash_errors(form, category="warning"):
     '''Flash all errors for a form.'''
     for field, errors in form.errors.items():
         for error in errors:
             flash("{0} - {1}"
-                    .format(getattr(form, field).label.text, error), category)
+                    .format(getattr(form, field).label.text, error), category)
+
+def first_existing_home_subdir(*paths):
+    paths_tried = []
+    for path in paths:
+        for root in [os.environ[x] for x in ["HOME", "USERPROFILE"] if x in os.environ]:
+            ep = os.path.join(root, path)
+            paths_tried.append(ep)
+            if os.path.exists(ep):
+                return os.path.normpath(ep)
+    return None

File manage.py

View file
 from app_sizer.chart.models import App, Backup
 from app_sizer.settings import DevConfig, ProdConfig
 from app_sizer.database import db
-from app_sizer.app_import import scan_all
-from app_sizer.backup_import import import_backups as do_import_backups
+from app_sizer.app_import import scan_all, apps_path
+from app_sizer.backup_import import backups_path, import_backups as do_import_backups
 
 from tqdm import tqdm
 
     sys.exit(status)
 
 @manager.command
+def paths():
+    """Print the paths where the app will find iTunes Library & Backups."""
+    print "Apps are at {}".format(apps_path())
+    print "Backups are at {}".format(backups_path())
+
+
+@manager.command
 def load_all_apps(path=None, limit=None):
+    "Loads all App metadata into the database."
     for i, app in tqdm(enumerate(scan_all(path=path))):
         # Let lower-level methods do their logging
         if limit is not None and int(limit) == i: