1 2 3 4 5 6 7 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#!/usr/bin/env python
# vim: expandtab sw=4 ts=4 sts=4:
#
# Copyright © 2003 - 2018 Matthias Blaesing <matthias.blaesing@rwth-aachen.de>
#
# This file is part of python-gammu <https://wammu.eu/python-gammu/>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
Example for usage of GetNextFileFolder, which is oriented at
gammu --getfilesystem
Without argument you get a hierarchical list, provide flat as
argument and get somethink like (not exactly!)
gammu --getfilesystem -flatall
"""
import argparse
import locale
import gammu
parser = argparse.ArgumentParser(usage="usage: %(prog)s [options]")
parser.add_argument(
"-c",
"--config",
action="store",
type=str,
dest="config",
default=None,
help="Config file path",
)
parser.add_argument(
"-f", "--flat", action="store_true", dest="flat", default=False, help="Flat listing"
)
parser.add_argument(
"-l",
"--level",
action="store_true",
dest="level",
default=False,
help="Level listing",
)
args = parser.parse_args()
# Init gammu module
state_machine = gammu.StateMachine()
if args.config is not None:
state_machine.ReadConfig(Filename=args.config)
else:
state_machine.ReadConfig()
state_machine.Init()
# Get wished listing from commandline (if provided - else asume level)
# On commandline level or flat can be provided as parameters
if args.flat:
mode = "flat"
else:
mode = "level"
# Set locale to default locale (here relevant for printing of date)
locale.setlocale(locale.LC_ALL, "")
# Wrapper around GetNextFileFolder, catching gammu.ERR_EMPTY
# for me, which should be after the last entry and returning "None"
#
# GetNextFileFolder gives us a dict with:
# 'Name' => Symbolic Name of the file (String)
# 'ID_FullName' => unique ID of the file (String)
# 'Used' => space used in bytes (integer)
# 'Modified' => Date of last change (datetime)
# 'Type' => Filetype as reported by phone (String)
# 'Folder' => Entry is a Folder (Bool)
# 'Level' => On which level of FS (Integer)
# 'Buffer' => ?? (String)
# File Attributes (Bool):
# 'Protected'
# 'ReadOnly'
# 'Hidden'
# 'System'
def NextFile(start=0):
try:
return state_machine.GetNextFileFolder(start)
except gammu.ERR_EMPTY:
return None
# Format File Attributes as String as a shorted Version
def FileToAttributeString(file_obj, filled=1):
protected = readonly = hidden = system = ""
if filled:
protected = readonly = hidden = system = " "
if file_obj["Protected"]:
protected = "P"
if file_obj["ReadOnly"]:
readonly = "R"
if file_obj["Hidden"]:
hidden = "H"
if file_obj["System"]:
system = "S"
return protected + readonly + hidden + system
def Main():
# Make sure we reset the pointer of the current entry to the first
file_obj = NextFile(1)
# Iterate over Files and print the Info
while file_obj:
if mode == "flat":
# Output:
# <ID>;<NAME>;<TYPE>;<MODDATE>;<SIZE>;<ATTRIBUTES>
# We have to catch the situations, where no Modification Time is
# provided
try:
time = file_obj["Modified"].strftime("%x %X") + ";"
except AttributeError:
time = ";"
print(
file_obj["ID_FullName"]
+ ";"
+ file_obj["Name"]
+ ";"
+ file_obj["Type"]
+ ";"
+ time
+ str(file_obj["Used"])
+ ";"
+ FileToAttributeString(file_obj, 0)
)
elif mode == "level":
attrib = FileToAttributeString(file_obj, 1)
level = file_obj["Level"]
spacer = " | " * (level - 2)
if level > 1:
spacer += " |-- "
title = '"' + file_obj["Name"] + '"'
if file_obj["Folder"]:
title = "Folder " + title
print(attrib + spacer + title)
file_obj = NextFile()
if __name__ == "__main__":
Main()
|