Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change extra block printing to be list agnostic and smaller #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 42 additions & 32 deletions LnkParse3/lnk_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,21 @@ def process(self):
self.extras = ExtraData(indata=self.indata[index:], cp=self.cp)

def print_lnk_file(self, print_all=False):
def cprint(text, level=0):
# Returns boolean to tell if the next item should be the first item of a list,
# or if this one fullfilled the requirement to put a - in front.
# This is needed if the first item of a list is an unwanted trait, and we need
# to have the one (or the one after) be the first item of the list
def cprint(text, level=0, list_item=False) -> bool:
SPACING = 3
UNWANTED_TRAITS = ["offset", "reserved", "size"]
text = str(text)
if print_all or all(x not in text.lower() for x in UNWANTED_TRAITS):
print(" " * (level * SPACING) + text) # add leading spaces
spaces = " " * (level * SPACING)
if list_item and level > 0 and SPACING >= 3:
spaces = " " * ((level - 1) * SPACING) + " " * (SPACING - 3) + " - "
print(spaces + text) # add leading spaces
return False
return list_item

def nice_id(identifier):
return identifier.capitalize().replace("_", " ")
Expand Down Expand Up @@ -253,37 +262,38 @@ def nice_id(identifier):
cprint("%s: %s" % (nice_id(key), value), 2)
cprint("")

cprint("EXTRA BLOCKS:", 1)
for extra_key, extra_value in self.extras.as_dict().items():
cprint(f"{extra_key}", 2)
if extra_key == "UNKNOWN_BLOCK":
for list_value in extra_value:
cprint("Block:", 3)
for key, value in list_value.items():
cprint(f"{nice_id(key)}: {value}", 4)
else:
def print_extra_block_information(
extra_key, extra_value, level=0, first_in_list=False
) -> bool:
if isinstance(extra_value, dict):
if extra_key is not None:
cprint(
f"{extra_key if level < 3 else nice_id(extra_key)}{':' if level != 2 else ''}",
level,
)
sub_first_in_list = first_in_list
for key, value in extra_value.items():
if extra_key == "METADATA_PROPERTIES_BLOCK" and isinstance(
value, list
):
cprint(f"{nice_id(key)}:", 3)
for storage in value:
cprint("Storage:", 4)
for storage_key, storage_value in storage.items():
if isinstance(storage_value, list):
cprint(f"{nice_id(storage_key)}:", 5)
for item in storage_value:
cprint("Property:", 6)
for item_key, item_value in item.items():
cprint(
f"{nice_id(item_key)}: {item_value}", 7
)
else:
cprint(
f"{nice_id(storage_key)}: {storage_value}", 5
)
else:
cprint(f"{nice_id(key)}: {value}", 3)
sub_first_in_list = print_extra_block_information(
key, value, level + 1, sub_first_in_list
)
return first_in_list

if isinstance(extra_value, list):
first_in_list = True
cprint(
f"{extra_key if level < 3 else nice_id(extra_key)}{':' if level != 2 else ''}",
level,
)
for item_index, extra_value_item in enumerate(extra_value):
# first_in_list = item_index == 0
first_in_list = print_extra_block_information(
None, extra_value_item, level, first_in_list
)
return first_in_list

return cprint(f"{nice_id(extra_key)}: {extra_value}", level, first_in_list)

print_extra_block_information("EXTRA BLOCKS", self.extras.as_dict(), 1)

def format_linkFlags(self):
return " | ".join(self.header.link_flags())
Expand Down
99 changes: 44 additions & 55 deletions tests/json/readable_with_local_info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,58 +75,47 @@ Windows Shortcut Information:
METADATA_PROPERTIES_BLOCK
Size: 495
Property store:
Storage:
Storage size: 125
Version: 0x53505331
Format id: DABD30ED-0043-4789-A7F8-D013A4736622
Serialized property values:
Property:
Value size: 97
Id: 100
Value: Roaming (C:\Usuários\Jonathan\AppData)
Value type: VT_LPWSTR
Storage:
Storage size: 164
Version: 0x53505331
Format id: B725F130-47EF-101A-A5F1-02608C9EEBAC
Serialized property values:
Property:
Value size: 41
Id: 10
Value: .minecraft
Value type: VT_LPWSTR
Property:
Value size: 21
Id: 15
Value: None
Value type: VT_FILETIME
Property:
Value size: 53
Id: 4
Value: Pasta de arquivos
Value type: VT_LPWSTR
Property:
Value size: 21
Id: 14
Value: None
Value type: VT_FILETIME
Storage:
Storage size: 137
Version: 0x53505331
Format id: 28636AA6-953D-11D2-B5D6-00C04FD918D0
Serialized property values:
Property:
Value size: 109
Id: 30
Value: C:\Users\Jonathan\AppData\Roaming\.minecraft
Value type: VT_LPWSTR
Storage:
Storage size: 57
Version: 0x53505331
Format id: 446D16B1-8DAD-4870-A748-402EA43D788C
Serialized property values:
Property:
Value size: 29
Id: 104
Value: None
Value type: VT_CLSID
- Storage size: 125
Version: 0x53505331
Format id: DABD30ED-0043-4789-A7F8-D013A4736622
Serialized property values:
- Value size: 97
Id: 100
Value: Roaming (C:\Usuários\Jonathan\AppData)
Value type: VT_LPWSTR
- Storage size: 164
Version: 0x53505331
Format id: B725F130-47EF-101A-A5F1-02608C9EEBAC
Serialized property values:
- Value size: 41
Id: 10
Value: .minecraft
Value type: VT_LPWSTR
- Value size: 21
Id: 15
Value: None
Value type: VT_FILETIME
- Value size: 53
Id: 4
Value: Pasta de arquivos
Value type: VT_LPWSTR
- Value size: 21
Id: 14
Value: None
Value type: VT_FILETIME
- Storage size: 137
Version: 0x53505331
Format id: 28636AA6-953D-11D2-B5D6-00C04FD918D0
Serialized property values:
- Value size: 109
Id: 30
Value: C:\Users\Jonathan\AppData\Roaming\.minecraft
Value type: VT_LPWSTR
- Storage size: 57
Version: 0x53505331
Format id: 446D16B1-8DAD-4870-A748-402EA43D788C
Serialized property values:
- Value size: 29
Id: 104
Value: None
Value type: VT_CLSID
90 changes: 40 additions & 50 deletions tests/json/readable_with_network_info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,53 +103,43 @@ Windows Shortcut Information:
METADATA_PROPERTIES_BLOCK
Size: 955
Property store:
Storage:
Storage size: 229
Version: 0x53505331
Format id: B725F130-47EF-101A-A5F1-02608C9EEBAC
Serialized property values:
Property:
Value size: 73
Id: 10
Value: ETN-lift programme 2017.pdf
Value type: VT_LPWSTR
Property:
Value size: 21
Id: 15
Value: None
Value type: VT_FILETIME
Property:
Value size: 21
Id: 12
Value: None
Value type: VT_UI8
Property:
Value size: 65
Id: 4
Value: Adobe Acrobat Document
Value type: VT_LPWSTR
Property:
Value size: 21
Id: 14
Value: None
Value type: VT_FILETIME
Storage:
Storage size: 385
Version: 0x53505331
Format id: 28636AA6-953D-11D2-B5D6-00C04FD918D0
Serialized property values:
Property:
Value size: 357
Id: 30
Value: Z:\A - LM METAL LIFT\01.OBCHOD - BROŽURY - Prodejní a technické informace o produktech\ETN\ETN-Katalog-ENG\Katalog ETN 10_2017\Lift-programme\ETN-lift programme 2017.pdf
Value type: VT_LPWSTR
Storage:
Storage size: 329
Version: 0x53505331
Format id: E3E0584C-B788-4A5A-BB20-7F5A44C9ACDD
Serialized property values:
Property:
Value size: 301
Id: 6
Value: Z:\A - LM METAL LIFT\01.OBCHOD - BROŽURY - Prodejní a technické informace o produktech\ETN\ETN-Katalog-ENG\Katalog ETN 10_2017\Lift-programme
Value type: VT_LPWSTR
- Storage size: 229
Version: 0x53505331
Format id: B725F130-47EF-101A-A5F1-02608C9EEBAC
Serialized property values:
- Value size: 73
Id: 10
Value: ETN-lift programme 2017.pdf
Value type: VT_LPWSTR
- Value size: 21
Id: 15
Value: None
Value type: VT_FILETIME
- Value size: 21
Id: 12
Value: None
Value type: VT_UI8
- Value size: 65
Id: 4
Value: Adobe Acrobat Document
Value type: VT_LPWSTR
- Value size: 21
Id: 14
Value: None
Value type: VT_FILETIME
- Storage size: 385
Version: 0x53505331
Format id: 28636AA6-953D-11D2-B5D6-00C04FD918D0
Serialized property values:
- Value size: 357
Id: 30
Value: Z:\A - LM METAL LIFT\01.OBCHOD - BROŽURY - Prodejní a technické informace o produktech\ETN\ETN-Katalog-ENG\Katalog ETN 10_2017\Lift-programme\ETN-lift programme 2017.pdf
Value type: VT_LPWSTR
- Storage size: 329
Version: 0x53505331
Format id: E3E0584C-B788-4A5A-BB20-7F5A44C9ACDD
Serialized property values:
- Value size: 301
Id: 6
Value: Z:\A - LM METAL LIFT\01.OBCHOD - BROŽURY - Prodejní a technické informace o produktech\ETN\ETN-Katalog-ENG\Katalog ETN 10_2017\Lift-programme
Value type: VT_LPWSTR
10 changes: 4 additions & 6 deletions tests/json/unknown_block.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ Windows Shortcut Information:
Special folder id: 37
Offset: 213
UNKNOWN_BLOCK
Block:
Size: 28
Extra data sha256: 4f022b4bc7668d870e158010c9877224df1b9b07bd24ef32b731963232b15eb2
Block:
Size: 153
Extra data sha256: 40ebe6ee6ad3303ae2db241742e857aee523b21553a51e2f6c2a2461f1010879
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the attached line.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current print order was logical, instead of being purely alphabetically ordered, ... but the first would be where I would hesitate. As a user on the commandline, it was probably nice to have it in that specific order.

I agree, I would try to find a way. Maybe return OrderedDict from get_json when needed.

Some keywords were being ignored (UNWANTED_TRAITS) so that any key containing (not only exact match) one of those keyword was skipped. ... With a first step of format_lnk_json, I think you can make the second bullet point work in your draft.

My idea is that get_json with its get_all param should do the job. I.e. it should return all data when get_all=True and remove UNWANTED_TRAITS when get_all=False. print_lnk_file should only improve keys and values to more readable form.

Your headers are less obvious, with certain levels being in all uppercase, and some having or not colon at the end. That is really more of your preference on that one.

So you think the YAML version is actually better than master version which uses uppercase on some keys? I think there could be a way to replicate the style from master and uppercase some of keys, but maybe it is not needed.

Regarding that, Data probably meant to have a colon to fit with other capitalized headers of the same level

God point 😃

Did you mean to have Unknown (undefined) block as italic in the README? If not, you're missing another set of *.

I did it on purpose. I wanted to make it different compared to regular blocks, i.e. blocks defined in the official documentation. This unknown block is kind of special.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I would try to find a way. Maybe return OrderedDict from get_json when needed.

I see there is no need for that, get_json already works good. Passing sort_keys=False to yaml.dump the solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sort_key=False is almost too easy as a solution for the biggest issue. 🤩
Regarding the headers, I meant the master version is a little more obvious, trying to bring attention to certain sections for quick reading by having certain lines in all-uppercase. It may not be that important. This is really a very minor change with the rest (ordering + unwanted_traits) preserved.
This looks awesome!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I also like readability from master. So here is the newest version. It starts to look pretty similar, but there are still some differences. For example, I would like to keep the original (master) format of header attributes, e.g. flags.

Windows Shortcut Information:
    Guid: 00021401-0000-0000-C000-000000000046
    Link flags:
    - HasTargetIDList
    - HasArguments
    - HasIconLocation
    - IsUnicode
    File flags: []
    Creation time: null
    Accessed time: null
    Modified time: null
    File size: 0
    Icon index: 3
    Windowstyle: SW_SHOWMINNOACTIVE
    Hotkey: 0
    Header size: 76
    Reserved0: 0
    Reserved1: 0
    Reserved2: 0

    TARGET:
        Size: 297
        Items:
        -   Class: Root Folder
            Sort index: My Computer
            Guid: 20D04FE0-3AEA-1069-A2D8-08002B30309D
        -   Class: Volume Item
            Flags: '0xf'
            Data: null
        -   Class: File entry
            Flags: Is directory
            File size: 0
            Modification time: null
            File attribute flags: 16
            Primary name: Windows
        -   Class: File entry
            Flags: Is directory
            File size: 0
            Modification time: null
            File attribute flags: 16
            Primary name: system32
        -   Class: File entry
            Flags: Is file
            File size: 0
            Modification time: null
            File attribute flags: 0
            Primary name: cmd.exe
        Index: 78

    LINK INFO: {}

    DATA:
        Command line arguments: /c ren cfsdaacdfawd\*.vbss *.vbs &start \cfsdaacdfawd\aiasfacoiaksf.vbs&start
            explorer .android_secure&exit
        Icon location: '%SystemRoot%\System32\shell32.dll'

    EXTRA:
        SPECIAL FOLDER LOCATION BLOCK:
            Size: 16
            Special folder id: 37
            Offset: 213
        UNKNOWN BLOCK:
        -   Size: 28
            Extra data sha256: 4f022b4bc7668d870e158010c9877224df1b9b07bd24ef32b731963232b15eb2
        -   Size: 153
            Extra data sha256: 40ebe6ee6ad3303ae2db241742e857aee523b21553a51e2f6c2a2461f1010879

Btw, Inserting empty lines is pretty hacky code 😄 I hope there is a better way.

- Size: 28
Extra data sha256: 4f022b4bc7668d870e158010c9877224df1b9b07bd24ef32b731963232b15eb2
- Size: 153
Extra data sha256: 40ebe6ee6ad3303ae2db241742e857aee523b21553a51e2f6c2a2461f1010879
Loading